-rw-r--r-- | kabc/plugins/qtopia/resourceqtopia.cpp | 135 |
1 files changed, 26 insertions, 109 deletions
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp index 1f90fcb..f9cabcb 100644 --- a/kabc/plugins/qtopia/resourceqtopia.cpp +++ b/kabc/plugins/qtopia/resourceqtopia.cpp | |||
@@ -7,98 +7,96 @@ | |||
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. | 8 | version 2 of the License, or (at your option) any later version. |
9 | 9 | ||
10 | This library is distributed in the hope that it will be useful, | 10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. | 13 | Library General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /* | 21 | /* |
22 | Enhanced Version of the file for platform independent KDE tools. | 22 | Enhanced Version of the file for platform independent KDE tools. |
23 | Copyright (c) 2004 Ulf Schenk | 23 | Copyright (c) 2004 Ulf Schenk |
24 | 24 | ||
25 | $Id$ | 25 | $Id$ |
26 | */ | 26 | */ |
27 | #include <sys/types.h> | 27 | #include <sys/types.h> |
28 | #include <sys/stat.h> | 28 | #include <sys/stat.h> |
29 | #include <unistd.h> | 29 | #include <unistd.h> |
30 | 30 | ||
31 | #include <qdir.h> | 31 | #include <qdir.h> |
32 | #include <qfile.h> | 32 | #include <qfile.h> |
33 | #include <qfileinfo.h> | 33 | #include <qfileinfo.h> |
34 | #include <qregexp.h> | 34 | #include <qregexp.h> |
35 | //US #include <qtimer.h> | 35 | //US #include <qtimer.h> |
36 | 36 | ||
37 | #include <kapplication.h> | 37 | #include <kapplication.h> |
38 | #include <kconfig.h> | 38 | #include <kconfig.h> |
39 | #include <kdebug.h> | 39 | #include <kdebug.h> |
40 | #include <klocale.h> | 40 | #include <klocale.h> |
41 | //US #include <ksavefile.h> | 41 | //US #include <ksavefile.h> |
42 | #include <kstandarddirs.h> | 42 | #include <kstandarddirs.h> |
43 | #include <kmessagebox.h> | 43 | #include <kmessagebox.h> |
44 | 44 | ||
45 | #include <qpe/pim/addressbookaccess.h> | 45 | #include <qpe/pim/addressbookaccess.h> |
46 | 46 | ||
47 | 47 | ||
48 | #include "resourceqtopiaconfig.h" | 48 | #include "resourceqtopiaconfig.h" |
49 | #include "stdaddressbook.h" | 49 | #include "stdaddressbook.h" |
50 | 50 | ||
51 | #include "qtopiaconverter.h" | 51 | #include "qtopiaconverter.h" |
52 | #include "syncprefwidget.h" | 52 | #include "syncprefwidget.h" |
53 | 53 | ||
54 | #include "resourceqtopia.h" | 54 | #include "resourceqtopia.h" |
55 | #include <libkdepim/ksyncprofile.h> | ||
56 | #include <qpe/quuid.h> | ||
57 | 55 | ||
58 | using namespace KABC; | 56 | using namespace KABC; |
59 | extern "C" | 57 | extern "C" |
60 | { | 58 | { |
61 | void *init_microkabc_qtopia() | 59 | void *init_microkabc_qtopia() |
62 | { | 60 | { |
63 | return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, SyncPrefWidgetContainer>(); | 61 | return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, SyncPrefWidgetContainer>(); |
64 | } | 62 | } |
65 | } | 63 | } |
66 | 64 | ||
67 | ResourceQtopia::ResourceQtopia( const KConfig *config, bool syncable ) | 65 | ResourceQtopia::ResourceQtopia( const KConfig *config, bool syncable ) |
68 | : Resource( config, syncable ), mConverter (0) | 66 | : Resource( config, syncable ), mConverter (0) |
69 | { | 67 | { |
70 | // we can not choose the filename. Therefore use the default to display | 68 | // we can not choose the filename. Therefore use the default to display |
71 | QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; | 69 | QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; |
72 | init( fileName ); | 70 | init( fileName ); |
73 | } | 71 | } |
74 | 72 | ||
75 | ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable ) | 73 | ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable ) |
76 | : Resource( 0, syncable ) | 74 | : Resource( 0, syncable ) |
77 | { | 75 | { |
78 | init( fileName ); | 76 | init( fileName ); |
79 | } | 77 | } |
80 | 78 | ||
81 | void ResourceQtopia::init( const QString &fileName ) | 79 | void ResourceQtopia::init( const QString &fileName ) |
82 | { | 80 | { |
83 | 81 | ||
84 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 82 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
85 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 83 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
86 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 84 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
87 | 85 | ||
88 | setFileName( fileName ); | 86 | setFileName( fileName ); |
89 | } | 87 | } |
90 | 88 | ||
91 | ResourceQtopia::~ResourceQtopia() | 89 | ResourceQtopia::~ResourceQtopia() |
92 | { | 90 | { |
93 | if (mConverter != 0) | 91 | if (mConverter != 0) |
94 | delete mConverter; | 92 | delete mConverter; |
95 | 93 | ||
96 | if(mAccess != 0) | 94 | if(mAccess != 0) |
97 | delete mAccess; | 95 | delete mAccess; |
98 | } | 96 | } |
99 | 97 | ||
100 | void ResourceQtopia::writeConfig( KConfig *config ) | 98 | void ResourceQtopia::writeConfig( KConfig *config ) |
101 | { | 99 | { |
102 | Resource::writeConfig( config ); | 100 | Resource::writeConfig( config ); |
103 | } | 101 | } |
104 | 102 | ||
@@ -146,209 +144,128 @@ bool ResourceQtopia::doOpen() | |||
146 | } | 144 | } |
147 | 145 | ||
148 | return true; | 146 | return true; |
149 | } | 147 | } |
150 | 148 | ||
151 | void ResourceQtopia::doClose() | 149 | void ResourceQtopia::doClose() |
152 | { | 150 | { |
153 | qDebug("ResourceQtopia::doClose: %s", fileName().latin1()); | 151 | qDebug("ResourceQtopia::doClose: %s", fileName().latin1()); |
154 | 152 | ||
155 | if(mAccess) | 153 | if(mAccess) |
156 | { | 154 | { |
157 | delete mAccess; | 155 | delete mAccess; |
158 | mAccess = 0; | 156 | mAccess = 0; |
159 | } | 157 | } |
160 | // it seems so, that deletion of access deletes backend as well | 158 | // it seems so, that deletion of access deletes backend as well |
161 | //delete backend; | 159 | //delete backend; |
162 | 160 | ||
163 | return; | 161 | return; |
164 | } | 162 | } |
165 | 163 | ||
166 | bool ResourceQtopia::load() | 164 | bool ResourceQtopia::load() |
167 | { | 165 | { |
168 | qDebug("ResourceQtopia::load: %s", fileName().latin1()); | 166 | qDebug("ResourceQtopia::load: %s", fileName().latin1()); |
169 | 167 | ||
170 | AddressBookIterator it(*mAccess); | 168 | AddressBookIterator it(*mAccess); |
171 | const PimContact* contact; | 169 | const PimContact* contact; |
172 | bool res; | 170 | bool res; |
173 | 171 | ||
174 | for (contact=it.toFirst(); it.current(); ++it) | 172 | for (contact=it.toFirst(); it.current(); ++it) |
175 | { | 173 | { |
176 | contact = it.current(); | 174 | contact = it.current(); |
177 | 175 | ||
178 | KABC::Addressee addressee; | 176 | KABC::Addressee addressee; |
179 | 177 | ||
180 | res = mConverter->qtopiaToAddressee( (*contact), addressee ); | 178 | res = mConverter->qtopiaToAddressee( (*contact), addressee ); |
181 | 179 | ||
182 | if ( !addressee.isEmpty() && res ) | 180 | if ( !addressee.isEmpty() && res ) |
183 | { | 181 | { |
184 | addressee.setResource( this ); | 182 | addressee.setResource( this ); |
185 | addressBook()->insertAddressee( addressee ); | 183 | addressBook()->insertAddressee( addressee ); |
186 | } | 184 | } |
187 | } | 185 | } |
188 | 186 | ||
189 | return true; | 187 | return true; |
190 | } | 188 | } |
191 | 189 | ||
192 | bool ResourceQtopia::save( Ticket *ticket ) | 190 | bool ResourceQtopia::save( Ticket *ticket ) |
193 | { | 191 | { |
194 | qDebug("ResourceQtopia::save: %s", fileName().latin1()); | 192 | qDebug("ResourceQtopia::save: %s", fileName().latin1()); |
195 | |||
196 | mDirWatch.stopScan(); | ||
197 | |||
198 | KABC::AddressBook::Iterator it; | ||
199 | bool res; | ||
200 | KABC::Addressee::List changedAddressees; | ||
201 | QStringList addedUID; | ||
202 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | ||
203 | //KABC::Addressee addressee = (*it); | ||
204 | // qDebug("AAAfor uid %s ", (*it).originalExternalUID().latin1() ); | ||
205 | if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { | ||
206 | QUuid uid( (*it).originalExternalUID() ); | ||
207 | bool ok; | ||
208 | PimContact c = mAccess->contactForId( uid, &ok ); | ||
209 | qDebug("ResourceQtopia::save:Found %d for uid %s ", ok,(*it).originalExternalUID().latin1() ); | ||
210 | res = mConverter->addresseeToQtopia( *it, c ); | ||
211 | if (res == true) { | ||
212 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL ) { | ||
213 | qDebug("skipping adding of contact "); | ||
214 | continue; | ||
215 | PimContact e; | ||
216 | AddressBookAccess Access2; | ||
217 | Access2.addContact(e); | ||
218 | const PimContact* contact; | ||
219 | bool res; | ||
220 | QString uid2, Ouid; | ||
221 | AddressBookIterator itt(Access2); | ||
222 | for (contact=itt.toFirst(); itt.current(); ++itt) | ||
223 | { | ||
224 | contact = itt.current(); | ||
225 | QString exuid = contact->uid().toString(); | ||
226 | Ouid =exuid; | ||
227 | int ente = exuid.find( "-0000"); | ||
228 | if ( exuid.left(1) == "{" ) | ||
229 | exuid = exuid.mid(1); | ||
230 | if ( ente > -1 ) | ||
231 | exuid = exuid.left( ente-1 ); | ||
232 | uid2 = exuid; | ||
233 | qDebug("trying find uid "); | ||
234 | if ( !addressBook()->containsExternalUid( uid2 ) ) { | ||
235 | qDebug("ab not contains "); | ||
236 | if ( !addedUID.contains( uid2 )) { | ||
237 | qDebug("list not contains "); | ||
238 | break; | ||
239 | } | ||
240 | } | ||
241 | |||
242 | } | ||
243 | if ( !itt.current()) { | ||
244 | qDebug("ERROR uid "); | ||
245 | continue; | ||
246 | } | ||
247 | else | ||
248 | qDebug("ADD uid detected "); | ||
249 | |||
250 | |||
251 | QUuid uid3( Ouid); | ||
252 | PimContact d = Access2.contactForId( uid3, &ok ); | ||
253 | if ( ! ok ) { | ||
254 | qDebug("ADD: Error getting new uid "); | ||
255 | } else { | ||
256 | qDebug("NEW UID found "); | ||
257 | PimContact g = mAccess->contactForId( c.uid(), &ok ); | ||
258 | KABC::Addressee addressee; | ||
259 | mConverter->qtopiaToAddressee( g, addressee ); | ||
260 | addressee.setUid((*it).uid() ); | ||
261 | addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | ||
262 | changedAddressees.append( addressee ); | ||
263 | addedUID.append( uid2 ); | ||
264 | } | ||
265 | |||
266 | |||
267 | } else if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { | ||
268 | qDebug("Remove contact "); | ||
269 | if ( ok ) | ||
270 | mAccess->removeContact(c); | ||
271 | else | ||
272 | qDebug("Error remove contact from qtopia "); | ||
273 | } else if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { | ||
274 | if ( ok ) { | ||
275 | qDebug("Update contact "); | ||
276 | mAccess->updateContact(c); | ||
277 | PimContact g = mAccess->contactForId( c.uid(), &ok ); | ||
278 | KABC::Addressee addressee; | ||
279 | mConverter->qtopiaToAddressee( g, addressee ); | ||
280 | addressee.setUid((*it).uid() ); | ||
281 | addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_CSUM ); | ||
282 | changedAddressees.append( addressee ); | ||
283 | } | ||
284 | else | ||
285 | qDebug("Error update contact from qtopia "); | ||
286 | |||
287 | } | ||
288 | |||
289 | } else { | ||
290 | qDebug("Unable to convert Addressee %s", (*it).formattedName().latin1()); | ||
291 | } | ||
292 | } | ||
293 | } | ||
294 | 193 | ||
295 | KABC::Addressee::List::Iterator it2; | 194 | mDirWatch.stopScan(); |
296 | for ( it2 = changedAddressees.begin(); it2 != changedAddressees.end(); ++it2 ) | ||
297 | addressBook()->insertAddressee((*it2)); | ||
298 | 195 | ||
196 | KABC::AddressBook::Iterator it; | ||
197 | bool res; | ||
299 | 198 | ||
199 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | ||
200 | PimContact c; | ||
201 | KABC::Addressee addressee = (*it); | ||
300 | 202 | ||
301 | mDirWatch.startScan(); | 203 | res = mConverter->addresseeToQtopia( *it, c ); |
204 | if (res == true) | ||
205 | { | ||
206 | mAccess->addContact(c); | ||
207 | // if (res == false) | ||
208 | // qDebug("Unable to append Contact %s", c.fullName().latin1()); | ||
209 | } | ||
210 | else | ||
211 | { | ||
212 | qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); | ||
213 | } | ||
214 | } | ||
302 | 215 | ||
303 | delete ticket; | 216 | // mAccess->addressBookUpdated(); |
304 | unlock( fileName() ); | ||
305 | 217 | ||
306 | return true; | 218 | mDirWatch.startScan(); |
219 | |||
220 | delete ticket; | ||
221 | unlock( fileName() ); | ||
222 | |||
223 | return true; | ||
307 | } | 224 | } |
308 | 225 | ||
309 | bool ResourceQtopia::lock( const QString &lockfileName ) | 226 | bool ResourceQtopia::lock( const QString &lockfileName ) |
310 | { | 227 | { |
311 | qDebug("ResourceQtopia::lock: %s", fileName().latin1()); | 228 | qDebug("ResourceQtopia::lock: %s", fileName().latin1()); |
312 | 229 | ||
313 | kdDebug(5700) << "ResourceQtopia::lock()" << endl; | 230 | kdDebug(5700) << "ResourceQtopia::lock()" << endl; |
314 | 231 | ||
315 | QString fn = lockfileName; | 232 | QString fn = lockfileName; |
316 | 233 | ||
317 | KURL url(fn); | 234 | KURL url(fn); |
318 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 235 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
319 | 236 | ||
320 | kdDebug(5700) << "-- lock name: " << lockName << endl; | 237 | kdDebug(5700) << "-- lock name: " << lockName << endl; |
321 | 238 | ||
322 | if (QFile::exists( lockName )) | 239 | if (QFile::exists( lockName )) |
323 | { | 240 | { |
324 | qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); | 241 | qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); |
325 | return false; | 242 | return false; |
326 | } | 243 | } |
327 | 244 | ||
328 | QString lockUniqueName; | 245 | QString lockUniqueName; |
329 | lockUniqueName = fn + KApplication::randomString( 8 ); | 246 | lockUniqueName = fn + KApplication::randomString( 8 ); |
330 | 247 | ||
331 | url = lockUniqueName; | 248 | url = lockUniqueName; |
332 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 249 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
333 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 250 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
334 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 251 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
335 | 252 | ||
336 | // Create unique file | 253 | // Create unique file |
337 | QFile file( mLockUniqueName ); | 254 | QFile file( mLockUniqueName ); |
338 | file.open( IO_WriteOnly ); | 255 | file.open( IO_WriteOnly ); |
339 | file.close(); | 256 | file.close(); |
340 | 257 | ||
341 | // Create lock file | 258 | // Create lock file |
342 | int result = 0; | 259 | int result = 0; |
343 | #ifndef _WIN32_ | 260 | #ifndef _WIN32_ |
344 | result = ::link( QFile::encodeName( mLockUniqueName ), | 261 | result = ::link( QFile::encodeName( mLockUniqueName ), |
345 | QFile::encodeName( lockName ) ); | 262 | QFile::encodeName( lockName ) ); |
346 | #endif | 263 | #endif |
347 | if ( result == 0 ) { | 264 | if ( result == 0 ) { |
348 | addressBook()->emitAddressBookLocked(); | 265 | addressBook()->emitAddressBookLocked(); |
349 | return true; | 266 | return true; |
350 | } | 267 | } |
351 | 268 | ||
352 | // TODO: check stat | 269 | // TODO: check stat |
353 | 270 | ||
354 | return false; | 271 | return false; |