-rw-r--r-- | kabc/plugins/opie/resourceopie.cpp | 39 | ||||
-rw-r--r-- | kabc/plugins/opie/resourceopie.h | 10 | ||||
-rw-r--r-- | kabc/plugins/qtopia/resourceqtopia.cpp | 49 | ||||
-rw-r--r-- | kabc/plugins/qtopia/resourceqtopia.h | 10 | ||||
-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.cpp | 30 | ||||
-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.h | 10 |
6 files changed, 17 insertions, 131 deletions
diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp index 5559827..9dfd473 100644 --- a/kabc/plugins/opie/resourceopie.cpp +++ b/kabc/plugins/opie/resourceopie.cpp | |||
@@ -26,128 +26,129 @@ $Id$ | |||
26 | */ | 26 | */ |
27 | 27 | ||
28 | #include <sys/types.h> | 28 | #include <sys/types.h> |
29 | #include <sys/stat.h> | 29 | #include <sys/stat.h> |
30 | #include <unistd.h> | 30 | #include <unistd.h> |
31 | 31 | ||
32 | #include <qdir.h> | 32 | #include <qdir.h> |
33 | #include <qfile.h> | 33 | #include <qfile.h> |
34 | #include <qfileinfo.h> | 34 | #include <qfileinfo.h> |
35 | #include <qregexp.h> | 35 | #include <qregexp.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 | //US #include "formatfactory.h" | 45 | //US #include "formatfactory.h" |
46 | //US #include <qpe/qpeapplication.h> | 46 | //US #include <qpe/qpeapplication.h> |
47 | 47 | ||
48 | #include <opie/ocontactaccess.h> | 48 | #include <opie/ocontactaccess.h> |
49 | #include <opie/ocontactaccessbackend_xml.h> | 49 | #include <opie/ocontactaccessbackend_xml.h> |
50 | 50 | ||
51 | #include "resourceopieconfig.h" | 51 | #include "resourceopieconfig.h" |
52 | #include "stdaddressbook.h" | 52 | #include "stdaddressbook.h" |
53 | #include "syncprefwidget.h" | 53 | #include "syncprefwidget.h" |
54 | 54 | ||
55 | #include "opieconverter.h" | 55 | #include "opieconverter.h" |
56 | 56 | ||
57 | #include "resourceopie.h" | 57 | #include "resourceopie.h" |
58 | 58 | ||
59 | using namespace KABC; | 59 | using namespace KABC; |
60 | extern "C" | 60 | extern "C" |
61 | { | 61 | { |
62 | void *init_microkabc_opie() | 62 | void *init_microkabc_opie() |
63 | { | 63 | { |
64 | return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, SyncPrefWidget>(); | 64 | return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, SyncPrefWidget>(); |
65 | } | 65 | } |
66 | } | 66 | } |
67 | 67 | ||
68 | ResourceOpie::ResourceOpie( const KConfig *config, bool syncable ) | 68 | ResourceOpie::ResourceOpie( const KConfig *config, bool syncable ) |
69 | : Resource( config, syncable ), mAccess(0), mConverter (0) | 69 | : Resource( config, syncable ), mAccess(0), mConverter (0) |
70 | { | 70 | { |
71 | QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; | 71 | QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; |
72 | 72 | ||
73 | KConfig *cfg = (KConfig *)config; | 73 | KConfig *cfg = (KConfig *)config; |
74 | if ( cfg ) { | 74 | if ( cfg ) { |
75 | fileName = cfg->readEntry( "FileName", fileName ); | 75 | fileName = cfg->readEntry( "FileName", fileName ); |
76 | 76 | ||
77 | } | 77 | } |
78 | 78 | ||
79 | init( fileName ); | 79 | init( fileName ); |
80 | } | 80 | } |
81 | 81 | ||
82 | ResourceOpie::ResourceOpie( const QString &fileName, bool syncable ) | 82 | ResourceOpie::ResourceOpie( const QString &fileName, bool syncable ) |
83 | : Resource( 0, syncable ) | 83 | : Resource( 0, syncable ) |
84 | { | 84 | { |
85 | init( fileName ); | 85 | init( fileName ); |
86 | } | 86 | } |
87 | 87 | ||
88 | void ResourceOpie::init( const QString &fileName ) | 88 | void ResourceOpie::init( const QString &fileName ) |
89 | { | 89 | { |
90 | qDebug("ResourceOpie::init()"); | ||
90 | 91 | ||
91 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 92 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
92 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 93 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
93 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 94 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
94 | 95 | ||
95 | setFileName( fileName ); | 96 | setFileName( fileName ); |
96 | } | 97 | } |
97 | 98 | ||
98 | ResourceOpie::~ResourceOpie() | 99 | ResourceOpie::~ResourceOpie() |
99 | { | 100 | { |
100 | if (mConverter != 0) | 101 | if (mConverter != 0) |
101 | delete mConverter; | 102 | delete mConverter; |
102 | 103 | ||
103 | if(mAccess != 0) | 104 | if(mAccess != 0) |
104 | delete mAccess; | 105 | delete mAccess; |
105 | } | 106 | } |
106 | 107 | ||
107 | void ResourceOpie::writeConfig( KConfig *config ) | 108 | void ResourceOpie::writeConfig( KConfig *config ) |
108 | { | 109 | { |
109 | Resource::writeConfig( config ); | 110 | Resource::writeConfig( config ); |
110 | 111 | ||
111 | config->writeEntry( "FileName", fileName() ); | 112 | config->writeEntry( "FileName", fileName() ); |
112 | } | 113 | } |
113 | 114 | ||
114 | Ticket *ResourceOpie::requestSaveTicket() | 115 | Ticket *ResourceOpie::requestSaveTicket() |
115 | { | 116 | { |
116 | kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl; | 117 | kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl; |
117 | 118 | ||
118 | qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1()); | 119 | qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1()); |
119 | 120 | ||
120 | if ( !addressBook() ) return 0; | 121 | if ( !addressBook() ) return 0; |
121 | 122 | ||
122 | if ( !lock( fileName() ) ) { | 123 | if ( !lock( fileName() ) ) { |
123 | kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '" | 124 | kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '" |
124 | << fileName() << "'" << endl; | 125 | << fileName() << "'" << endl; |
125 | return 0; | 126 | return 0; |
126 | } | 127 | } |
127 | return createTicket( this ); | 128 | return createTicket( this ); |
128 | } | 129 | } |
129 | 130 | ||
130 | 131 | ||
131 | bool ResourceOpie::doOpen() | 132 | bool ResourceOpie::doOpen() |
132 | { | 133 | { |
133 | qDebug("ResourceOpie::doOpen: %s", fileName().latin1()); | 134 | qDebug("ResourceOpie::doOpen: %s", fileName().latin1()); |
134 | 135 | ||
135 | OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() ); | 136 | OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() ); |
136 | mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false); | 137 | mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false); |
137 | 138 | ||
138 | if ( !mAccess ) { | 139 | if ( !mAccess ) { |
139 | qDebug("Unable to load file() %s", fileName().latin1()); | 140 | qDebug("Unable to load file() %s", fileName().latin1()); |
140 | return false; | 141 | return false; |
141 | } | 142 | } |
142 | 143 | ||
143 | mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available | 144 | mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available |
144 | 145 | ||
145 | 146 | ||
146 | if (mConverter == 0) | 147 | if (mConverter == 0) |
147 | { | 148 | { |
148 | mConverter = new OpieConverter(); | 149 | mConverter = new OpieConverter(); |
149 | bool res = mConverter->init(); | 150 | bool res = mConverter->init(); |
150 | if ( !res ) | 151 | if ( !res ) |
151 | { | 152 | { |
152 | qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); | 153 | qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); |
153 | delete mAccess; | 154 | delete mAccess; |
@@ -193,183 +194,155 @@ bool ResourceOpie::load() | |||
193 | KABC::Addressee addressee; | 194 | KABC::Addressee addressee; |
194 | 195 | ||
195 | res = mConverter->opieToAddressee( c, addressee ); | 196 | res = mConverter->opieToAddressee( c, addressee ); |
196 | 197 | ||
197 | if ( !addressee.isEmpty() && res ) | 198 | if ( !addressee.isEmpty() && res ) |
198 | { | 199 | { |
199 | addressee.setResource( this ); | 200 | addressee.setResource( this ); |
200 | addressBook()->insertAddressee( addressee ); | 201 | addressBook()->insertAddressee( addressee ); |
201 | } | 202 | } |
202 | 203 | ||
203 | } | 204 | } |
204 | 205 | ||
205 | 206 | ||
206 | return true; | 207 | return true; |
207 | } | 208 | } |
208 | 209 | ||
209 | bool ResourceOpie::save( Ticket *ticket ) | 210 | bool ResourceOpie::save( Ticket *ticket ) |
210 | { | 211 | { |
211 | qDebug("ResourceOpie::save: %s", fileName().latin1()); | 212 | qDebug("ResourceOpie::save: %s", fileName().latin1()); |
212 | 213 | ||
213 | 214 | ||
214 | 215 | ||
215 | KABC::AddressBook::Iterator it; | 216 | KABC::AddressBook::Iterator it; |
216 | bool res; | 217 | bool res; |
217 | 218 | ||
218 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | 219 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { |
219 | OContact c; | 220 | OContact c; |
220 | KABC::Addressee addressee = (*it); | 221 | KABC::Addressee addressee = (*it); |
221 | 222 | ||
222 | res = mConverter->addresseeToOpie( *it, c ); | 223 | res = mConverter->addresseeToOpie( *it, c ); |
223 | if (res == true) | 224 | if (res == true) |
224 | { | 225 | { |
225 | res = mAccess->add(c); | 226 | res = mAccess->add(c); |
226 | if (res == false) | 227 | if (res == false) |
227 | qDebug("Unable to append Contact %s", c.fullName().latin1()); | 228 | qDebug("Unable to append Contact %s", c.fullName().latin1()); |
228 | } | 229 | } |
229 | else | 230 | else |
230 | { | 231 | { |
231 | qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); | 232 | qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); |
232 | } | 233 | } |
233 | } | 234 | } |
234 | 235 | ||
235 | mAccess->save(); | 236 | mAccess->save(); |
236 | 237 | ||
237 | delete ticket; | 238 | delete ticket; |
238 | unlock( fileName() ); | 239 | unlock( fileName() ); |
239 | 240 | ||
240 | return true; | 241 | return true; |
241 | 242 | ||
242 | } | 243 | } |
243 | 244 | ||
244 | bool ResourceOpie::lock( const QString &lockfileName ) | 245 | bool ResourceOpie::lock( const QString &lockfileName ) |
245 | { | 246 | { |
246 | qDebug("ResourceOpie::lock: %s", fileName().latin1()); | 247 | qDebug("ResourceOpie::lock: %s", fileName().latin1()); |
247 | 248 | ||
248 | kdDebug(5700) << "ResourceOpie::lock()" << endl; | 249 | kdDebug(5700) << "ResourceOpie::lock()" << endl; |
249 | 250 | ||
250 | QString fn = lockfileName; | 251 | QString fn = lockfileName; |
251 | 252 | ||
252 | KURL url(fn); | 253 | KURL url(fn); |
253 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 254 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
254 | 255 | ||
255 | kdDebug(5700) << "-- lock name: " << lockName << endl; | 256 | kdDebug(5700) << "-- lock name: " << lockName << endl; |
256 | 257 | ||
257 | if (QFile::exists( lockName )) return false; | 258 | if (QFile::exists( lockName )) |
259 | { | ||
260 | qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); | ||
261 | return false; | ||
262 | } | ||
258 | 263 | ||
259 | QString lockUniqueName; | 264 | QString lockUniqueName; |
260 | lockUniqueName = fn + KApplication::randomString( 8 ); | 265 | lockUniqueName = fn + KApplication::randomString( 8 ); |
261 | 266 | ||
262 | url = lockUniqueName; | 267 | url = lockUniqueName; |
263 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 268 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
264 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 269 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
265 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 270 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
266 | 271 | ||
267 | // Create unique file | 272 | // Create unique file |
268 | QFile file( mLockUniqueName ); | 273 | QFile file( mLockUniqueName ); |
269 | file.open( IO_WriteOnly ); | 274 | file.open( IO_WriteOnly ); |
270 | file.close(); | 275 | file.close(); |
271 | 276 | ||
272 | // Create lock file | 277 | // Create lock file |
273 | int result = 0; | 278 | int result = 0; |
274 | #ifndef _WIN32_ | 279 | #ifndef _WIN32_ |
275 | result = ::link( QFile::encodeName( mLockUniqueName ), | 280 | result = ::link( QFile::encodeName( mLockUniqueName ), |
276 | QFile::encodeName( lockName ) ); | 281 | QFile::encodeName( lockName ) ); |
277 | #endif | 282 | #endif |
278 | if ( result == 0 ) { | 283 | if ( result == 0 ) { |
279 | addressBook()->emitAddressBookLocked(); | 284 | addressBook()->emitAddressBookLocked(); |
280 | return true; | 285 | return true; |
281 | } | 286 | } |
282 | 287 | ||
283 | // TODO: check stat | 288 | // TODO: check stat |
284 | 289 | ||
285 | return false; | 290 | return false; |
286 | } | 291 | } |
287 | 292 | ||
288 | void ResourceOpie::unlock( const QString &fileName ) | 293 | void ResourceOpie::unlock( const QString &fileName ) |
289 | { | 294 | { |
290 | qDebug("ResourceOpie::unlock() %s", fileName.latin1()); | 295 | qDebug("ResourceOpie::unlock() %s", fileName.latin1()); |
291 | 296 | ||
292 | QString fn = fileName; | 297 | QString fn = fileName; |
293 | KURL url(fn); | 298 | KURL url(fn); |
294 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 299 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
295 | 300 | ||
296 | QFile::remove( lockName ); | 301 | QFile::remove( lockName ); |
297 | QFile::remove( mLockUniqueName ); | 302 | QFile::remove( mLockUniqueName ); |
298 | addressBook()->emitAddressBookUnlocked(); | 303 | addressBook()->emitAddressBookUnlocked(); |
299 | } | 304 | } |
300 | 305 | ||
301 | void ResourceOpie::setFileName( const QString &newFileName ) | 306 | void ResourceOpie::setFileName( const QString &newFileName ) |
302 | { | 307 | { |
303 | mDirWatch.stopScan(); | 308 | mDirWatch.stopScan(); |
304 | mDirWatch.removeFile( fileName() ); | 309 | mDirWatch.removeFile( fileName() ); |
305 | 310 | ||
306 | Resource::setFileName( newFileName ); | 311 | Resource::setFileName( newFileName ); |
307 | 312 | ||
308 | mDirWatch.addFile( fileName() ); | 313 | mDirWatch.addFile( fileName() ); |
309 | mDirWatch.startScan(); | 314 | mDirWatch.startScan(); |
310 | 315 | ||
311 | } | 316 | } |
312 | 317 | ||
313 | 318 | ||
314 | void ResourceOpie::fileChanged() | 319 | void ResourceOpie::fileChanged() |
315 | { | 320 | { |
316 | // There is a small theoretical chance that KDirWatch calls us before | 321 | // There is a small theoretical chance that KDirWatch calls us before |
317 | // we are fully constructed | 322 | // we are fully constructed |
318 | if (!addressBook()) | 323 | if (!addressBook()) |
319 | return; | 324 | return; |
320 | 325 | ||
321 | QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); | 326 | QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); |
322 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | 327 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { |
323 | load(); | 328 | load(); |
324 | addressBook()->emitAddressBookChanged(); | 329 | addressBook()->emitAddressBookChanged(); |
325 | } | 330 | } |
326 | 331 | ||
327 | load(); | 332 | load(); |
328 | addressBook()->emitAddressBookChanged(); | 333 | addressBook()->emitAddressBookChanged(); |
329 | } | 334 | } |
330 | 335 | ||
331 | void ResourceOpie::removeAddressee( const Addressee &addr ) | 336 | void ResourceOpie::removeAddressee( const Addressee &addr ) |
332 | { | 337 | { |
333 | } | 338 | } |
334 | 339 | ||
335 | void ResourceOpie::cleanUp() | 340 | void ResourceOpie::cleanUp() |
336 | { | 341 | { |
337 | // qDebug("ResourceOpie::cleanup() %s", mFileName.latin1()); | 342 | // qDebug("ResourceOpie::cleanup() %s", mFileName.latin1()); |
338 | 343 | ||
339 | unlock( fileName() ); | 344 | unlock( fileName() ); |
340 | } | 345 | } |
341 | 346 | ||
342 | 347 | ||
343 | /** | ||
344 | * This method returns the number of elements that are currently in the resource. | ||
345 | */ | ||
346 | int ResourceOpie::count() const | ||
347 | { | ||
348 | qDebug("ResourceOpie::count: %x", mAccess); | ||
349 | |||
350 | if (mAccess != 0) | ||
351 | { | ||
352 | OContactAccess::List contactList = mAccess->allRecords(); | ||
353 | return contactList.count(); | ||
354 | } | ||
355 | else | ||
356 | return 0; | ||
357 | } | ||
358 | |||
359 | |||
360 | /** | ||
361 | * This method removes all elements from the resource!! (Not from the addressbook) | ||
362 | */ | ||
363 | bool ResourceOpie::clear() | ||
364 | { | ||
365 | if (mAccess != 0) { | ||
366 | mAccess->clear(); | ||
367 | return true; | ||
368 | } | ||
369 | else | ||
370 | return false; | ||
371 | } | ||
372 | |||
373 | |||
374 | |||
375 | //US #include "resourceopie.moc" | 348 | //US #include "resourceopie.moc" |
diff --git a/kabc/plugins/opie/resourceopie.h b/kabc/plugins/opie/resourceopie.h index ca30fee..d5b4ebd 100644 --- a/kabc/plugins/opie/resourceopie.h +++ b/kabc/plugins/opie/resourceopie.h | |||
@@ -61,96 +61,86 @@ public: | |||
61 | 61 | ||
62 | /** | 62 | /** |
63 | Construct file resource on file @arg fileName using format @arg formatName. | 63 | Construct file resource on file @arg fileName using format @arg formatName. |
64 | */ | 64 | */ |
65 | ResourceOpie( const QString &fileName, bool syncable ); | 65 | ResourceOpie( const QString &fileName, bool syncable ); |
66 | 66 | ||
67 | /** | 67 | /** |
68 | * Destructor. | 68 | * Destructor. |
69 | */ | 69 | */ |
70 | ~ResourceOpie(); | 70 | ~ResourceOpie(); |
71 | 71 | ||
72 | /** | 72 | /** |
73 | Writes the config back. | 73 | Writes the config back. |
74 | */ | 74 | */ |
75 | virtual void writeConfig( KConfig *cfg ); | 75 | virtual void writeConfig( KConfig *cfg ); |
76 | 76 | ||
77 | /** | 77 | /** |
78 | * Tries to open the file and checks for the proper format. | 78 | * Tries to open the file and checks for the proper format. |
79 | * This method should be called before @ref load(). | 79 | * This method should be called before @ref load(). |
80 | */ | 80 | */ |
81 | virtual bool doOpen(); | 81 | virtual bool doOpen(); |
82 | 82 | ||
83 | /** | 83 | /** |
84 | * Closes the file again. | 84 | * Closes the file again. |
85 | */ | 85 | */ |
86 | virtual void doClose(); | 86 | virtual void doClose(); |
87 | 87 | ||
88 | /** | 88 | /** |
89 | * Requests a save ticket, that is used by @ref save() | 89 | * Requests a save ticket, that is used by @ref save() |
90 | */ | 90 | */ |
91 | virtual Ticket *requestSaveTicket(); | 91 | virtual Ticket *requestSaveTicket(); |
92 | 92 | ||
93 | /** | 93 | /** |
94 | * Loads all addressees from file to the address book. | 94 | * Loads all addressees from file to the address book. |
95 | * Returns true if all addressees could be loaded otherwise false. | 95 | * Returns true if all addressees could be loaded otherwise false. |
96 | */ | 96 | */ |
97 | virtual bool load(); | 97 | virtual bool load(); |
98 | 98 | ||
99 | /** | 99 | /** |
100 | * Saves all addresses from address book to file. | 100 | * Saves all addresses from address book to file. |
101 | * Returns true if all addressees could be saved otherwise false. | 101 | * Returns true if all addressees could be saved otherwise false. |
102 | * | 102 | * |
103 | * @param ticket The ticket returned by @ref requestSaveTicket() | 103 | * @param ticket The ticket returned by @ref requestSaveTicket() |
104 | */ | 104 | */ |
105 | virtual bool save( Ticket *ticket ); | 105 | virtual bool save( Ticket *ticket ); |
106 | 106 | ||
107 | 107 | ||
108 | /** | 108 | /** |
109 | * Remove a addressee from its source. | 109 | * Remove a addressee from its source. |
110 | * This method is mainly called by KABC::AddressBook. | 110 | * This method is mainly called by KABC::AddressBook. |
111 | */ | 111 | */ |
112 | virtual void removeAddressee( const Addressee& addr ); | 112 | virtual void removeAddressee( const Addressee& addr ); |
113 | 113 | ||
114 | /** | 114 | /** |
115 | * Set name of file to be used for saving. | 115 | * Set name of file to be used for saving. |
116 | */ | 116 | */ |
117 | virtual void setFileName( const QString & ); | 117 | virtual void setFileName( const QString & ); |
118 | 118 | ||
119 | /** | 119 | /** |
120 | * This method is called by an error handler if the application | 120 | * This method is called by an error handler if the application |
121 | * crashed | 121 | * crashed |
122 | */ | 122 | */ |
123 | virtual void cleanUp(); | 123 | virtual void cleanUp(); |
124 | 124 | ||
125 | /** | ||
126 | * This method returns the number of elements that are currently in the resource. | ||
127 | */ | ||
128 | virtual int count() const; | ||
129 | |||
130 | /** | ||
131 | * This method removes all elements from the resource!! (Not from the addressbook) | ||
132 | */ | ||
133 | virtual bool clear(); | ||
134 | |||
135 | 125 | ||
136 | protected slots: | 126 | protected slots: |
137 | void fileChanged(); | 127 | void fileChanged(); |
138 | 128 | ||
139 | protected: | 129 | protected: |
140 | void init( const QString &fileName ); | 130 | void init( const QString &fileName ); |
141 | 131 | ||
142 | bool lock( const QString &fileName ); | 132 | bool lock( const QString &fileName ); |
143 | void unlock( const QString &fileName ); | 133 | void unlock( const QString &fileName ); |
144 | 134 | ||
145 | private: | 135 | private: |
146 | OContactAccess* mAccess; | 136 | OContactAccess* mAccess; |
147 | OpieConverter* mConverter; | 137 | OpieConverter* mConverter; |
148 | 138 | ||
149 | QString mLockUniqueName; | 139 | QString mLockUniqueName; |
150 | 140 | ||
151 | KDirWatch mDirWatch; | 141 | KDirWatch mDirWatch; |
152 | }; | 142 | }; |
153 | 143 | ||
154 | } | 144 | } |
155 | 145 | ||
156 | #endif | 146 | #endif |
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp index 4a35f19..242e0c6 100644 --- a/kabc/plugins/qtopia/resourceqtopia.cpp +++ b/kabc/plugins/qtopia/resourceqtopia.cpp | |||
@@ -172,187 +172,148 @@ bool ResourceQtopia::load() | |||
172 | for (contact=it.toFirst(); it.current(); ++it) | 172 | for (contact=it.toFirst(); it.current(); ++it) |
173 | { | 173 | { |
174 | contact = it.current(); | 174 | contact = it.current(); |
175 | 175 | ||
176 | KABC::Addressee addressee; | 176 | KABC::Addressee addressee; |
177 | 177 | ||
178 | res = mConverter->qtopiaToAddressee( (*contact), addressee ); | 178 | res = mConverter->qtopiaToAddressee( (*contact), addressee ); |
179 | 179 | ||
180 | if ( !addressee.isEmpty() && res ) | 180 | if ( !addressee.isEmpty() && res ) |
181 | { | 181 | { |
182 | addressee.setResource( this ); | 182 | addressee.setResource( this ); |
183 | addressBook()->insertAddressee( addressee ); | 183 | addressBook()->insertAddressee( addressee ); |
184 | } | 184 | } |
185 | } | 185 | } |
186 | 186 | ||
187 | return true; | 187 | return true; |
188 | } | 188 | } |
189 | 189 | ||
190 | bool ResourceQtopia::save( Ticket *ticket ) | 190 | bool ResourceQtopia::save( Ticket *ticket ) |
191 | { | 191 | { |
192 | qDebug("ResourceQtopia::save: %s", fileName().latin1()); | 192 | qDebug("ResourceQtopia::save: %s", fileName().latin1()); |
193 | 193 | ||
194 | 194 | ||
195 | KABC::AddressBook::Iterator it; | 195 | KABC::AddressBook::Iterator it; |
196 | bool res; | 196 | bool res; |
197 | 197 | ||
198 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | 198 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { |
199 | PimContact c; | 199 | PimContact c; |
200 | KABC::Addressee addressee = (*it); | 200 | KABC::Addressee addressee = (*it); |
201 | 201 | ||
202 | res = mConverter->addresseeToQtopia( *it, c ); | 202 | res = mConverter->addresseeToQtopia( *it, c ); |
203 | if (res == true) | 203 | if (res == true) |
204 | { | 204 | { |
205 | mAccess->addContact(c); | 205 | mAccess->addContact(c); |
206 | // if (res == false) | 206 | // if (res == false) |
207 | // qDebug("Unable to append Contact %s", c.fullName().latin1()); | 207 | // qDebug("Unable to append Contact %s", c.fullName().latin1()); |
208 | } | 208 | } |
209 | else | 209 | else |
210 | { | 210 | { |
211 | qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); | 211 | qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); |
212 | } | 212 | } |
213 | } | 213 | } |
214 | 214 | ||
215 | // mAccess->addressBookUpdated(); | 215 | // mAccess->addressBookUpdated(); |
216 | 216 | ||
217 | delete ticket; | 217 | delete ticket; |
218 | unlock( fileName() ); | 218 | unlock( fileName() ); |
219 | 219 | ||
220 | return true; | 220 | return true; |
221 | } | 221 | } |
222 | 222 | ||
223 | bool ResourceQtopia::lock( const QString &lockfileName ) | 223 | bool ResourceQtopia::lock( const QString &lockfileName ) |
224 | { | 224 | { |
225 | qDebug("ResourceQtopia::lock: %s", fileName().latin1()); | 225 | qDebug("ResourceQtopia::lock: %s", fileName().latin1()); |
226 | 226 | ||
227 | kdDebug(5700) << "ResourceQtopia::lock()" << endl; | 227 | kdDebug(5700) << "ResourceQtopia::lock()" << endl; |
228 | 228 | ||
229 | QString fn = lockfileName; | 229 | QString fn = lockfileName; |
230 | 230 | ||
231 | KURL url(fn); | 231 | KURL url(fn); |
232 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 232 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
233 | 233 | ||
234 | kdDebug(5700) << "-- lock name: " << lockName << endl; | 234 | kdDebug(5700) << "-- lock name: " << lockName << endl; |
235 | 235 | ||
236 | if (QFile::exists( lockName )) return false; | 236 | if (QFile::exists( lockName )) |
237 | { | ||
238 | qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); | ||
239 | return false; | ||
240 | } | ||
237 | 241 | ||
238 | QString lockUniqueName; | 242 | QString lockUniqueName; |
239 | lockUniqueName = fn + KApplication::randomString( 8 ); | 243 | lockUniqueName = fn + KApplication::randomString( 8 ); |
240 | 244 | ||
241 | url = lockUniqueName; | 245 | url = lockUniqueName; |
242 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 246 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
243 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 247 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
244 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 248 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
245 | 249 | ||
246 | // Create unique file | 250 | // Create unique file |
247 | QFile file( mLockUniqueName ); | 251 | QFile file( mLockUniqueName ); |
248 | file.open( IO_WriteOnly ); | 252 | file.open( IO_WriteOnly ); |
249 | file.close(); | 253 | file.close(); |
250 | 254 | ||
251 | // Create lock file | 255 | // Create lock file |
252 | int result = 0; | 256 | int result = 0; |
253 | #ifndef _WIN32_ | 257 | #ifndef _WIN32_ |
254 | result = ::link( QFile::encodeName( mLockUniqueName ), | 258 | result = ::link( QFile::encodeName( mLockUniqueName ), |
255 | QFile::encodeName( lockName ) ); | 259 | QFile::encodeName( lockName ) ); |
256 | #endif | 260 | #endif |
257 | if ( result == 0 ) { | 261 | if ( result == 0 ) { |
258 | addressBook()->emitAddressBookLocked(); | 262 | addressBook()->emitAddressBookLocked(); |
259 | return true; | 263 | return true; |
260 | } | 264 | } |
261 | 265 | ||
262 | // TODO: check stat | 266 | // TODO: check stat |
263 | 267 | ||
264 | return false; | 268 | return false; |
265 | } | 269 | } |
266 | 270 | ||
267 | void ResourceQtopia::unlock( const QString &fileName ) | 271 | void ResourceQtopia::unlock( const QString &fileName ) |
268 | { | 272 | { |
269 | qDebug("ResourceQtopia::unlock() %s", fileName.latin1()); | 273 | qDebug("ResourceQtopia::unlock() %s", fileName.latin1()); |
270 | 274 | ||
271 | QString fn = fileName; | 275 | QString fn = fileName; |
272 | KURL url(fn); | 276 | KURL url(fn); |
273 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 277 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
274 | 278 | ||
275 | QFile::remove( lockName ); | 279 | QFile::remove( lockName ); |
276 | QFile::remove( mLockUniqueName ); | 280 | QFile::remove( mLockUniqueName ); |
277 | addressBook()->emitAddressBookUnlocked(); | 281 | addressBook()->emitAddressBookUnlocked(); |
278 | } | 282 | } |
279 | 283 | ||
280 | void ResourceQtopia::setFileName( const QString &newFileName ) | 284 | void ResourceQtopia::setFileName( const QString &newFileName ) |
281 | { | 285 | { |
282 | mDirWatch.stopScan(); | 286 | mDirWatch.stopScan(); |
283 | mDirWatch.removeFile( fileName() ); | 287 | mDirWatch.removeFile( fileName() ); |
284 | 288 | ||
285 | Resource::setFileName( newFileName ); | 289 | Resource::setFileName( newFileName ); |
286 | 290 | ||
287 | mDirWatch.addFile( fileName() ); | 291 | mDirWatch.addFile( fileName() ); |
288 | mDirWatch.startScan(); | 292 | mDirWatch.startScan(); |
289 | } | 293 | } |
290 | 294 | ||
291 | 295 | ||
292 | void ResourceQtopia::fileChanged() | 296 | void ResourceQtopia::fileChanged() |
293 | { | 297 | { |
294 | // There is a small theoretical chance that KDirWatch calls us before | 298 | // There is a small theoretical chance that KDirWatch calls us before |
295 | // we are fully constructed | 299 | // we are fully constructed |
296 | if (!addressBook()) | 300 | if (!addressBook()) |
297 | return; | 301 | return; |
298 | 302 | ||
299 | QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); | 303 | QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); |
300 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | 304 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { |
301 | load(); | 305 | load(); |
302 | addressBook()->emitAddressBookChanged(); | 306 | addressBook()->emitAddressBookChanged(); |
303 | } | 307 | } |
304 | } | 308 | } |
305 | 309 | ||
306 | void ResourceQtopia::removeAddressee( const Addressee &addr ) | 310 | void ResourceQtopia::removeAddressee( const Addressee &addr ) |
307 | { | 311 | { |
308 | } | 312 | } |
309 | 313 | ||
310 | void ResourceQtopia::cleanUp() | 314 | void ResourceQtopia::cleanUp() |
311 | { | 315 | { |
312 | unlock( fileName() ); | 316 | unlock( fileName() ); |
313 | } | 317 | } |
314 | 318 | ||
315 | |||
316 | |||
317 | /** | ||
318 | * This method returns the number of elements that are currently in the resource. | ||
319 | */ | ||
320 | int ResourceQtopia::count() const | ||
321 | { | ||
322 | if (mAccess != 0) | ||
323 | { | ||
324 | int counter = 0; | ||
325 | AddressBookIterator it2(*mAccess); | ||
326 | for (it2.toFirst(); it2.current(); ++it2) { | ||
327 | counter++; | ||
328 | } | ||
329 | |||
330 | return counter; | ||
331 | } | ||
332 | else | ||
333 | return 0; | ||
334 | } | ||
335 | |||
336 | |||
337 | /** | ||
338 | * This method removes all elements from the resource!! (Not from the addressbook) | ||
339 | */ | ||
340 | bool ResourceQtopia::clear() | ||
341 | { | ||
342 | if (mAccess != 0) | ||
343 | { | ||
344 | AddressBookIterator it2(*mAccess); | ||
345 | for (it2.toFirst(); it2.current(); ++it2) { | ||
346 | mAccess->removeContact(*it2.current()); | ||
347 | } | ||
348 | return true; | ||
349 | } | ||
350 | else | ||
351 | return false; | ||
352 | } | ||
353 | |||
354 | |||
355 | |||
356 | |||
357 | |||
358 | //US #include "resourceqtopia.moc" | 319 | //US #include "resourceqtopia.moc" |
diff --git a/kabc/plugins/qtopia/resourceqtopia.h b/kabc/plugins/qtopia/resourceqtopia.h index 90ab1f4..eace280 100644 --- a/kabc/plugins/qtopia/resourceqtopia.h +++ b/kabc/plugins/qtopia/resourceqtopia.h | |||
@@ -61,95 +61,85 @@ public: | |||
61 | ResourceQtopia( const KConfig *cfg, bool syncable ); | 61 | ResourceQtopia( const KConfig *cfg, bool syncable ); |
62 | 62 | ||
63 | /** | 63 | /** |
64 | Construct file resource on file @arg fileName using format @arg formatName. | 64 | Construct file resource on file @arg fileName using format @arg formatName. |
65 | */ | 65 | */ |
66 | ResourceQtopia( const QString &fileName, bool syncable ); | 66 | ResourceQtopia( const QString &fileName, bool syncable ); |
67 | 67 | ||
68 | /** | 68 | /** |
69 | * Destructor. | 69 | * Destructor. |
70 | */ | 70 | */ |
71 | ~ResourceQtopia(); | 71 | ~ResourceQtopia(); |
72 | 72 | ||
73 | /** | 73 | /** |
74 | Writes the config back. | 74 | Writes the config back. |
75 | */ | 75 | */ |
76 | virtual void writeConfig( KConfig *cfg ); | 76 | virtual void writeConfig( KConfig *cfg ); |
77 | 77 | ||
78 | /** | 78 | /** |
79 | * Tries to open the file and checks for the proper format. | 79 | * Tries to open the file and checks for the proper format. |
80 | * This method should be called before @ref load(). | 80 | * This method should be called before @ref load(). |
81 | */ | 81 | */ |
82 | virtual bool doOpen(); | 82 | virtual bool doOpen(); |
83 | 83 | ||
84 | /** | 84 | /** |
85 | * Closes the file again. | 85 | * Closes the file again. |
86 | */ | 86 | */ |
87 | virtual void doClose(); | 87 | virtual void doClose(); |
88 | 88 | ||
89 | /** | 89 | /** |
90 | * Requests a save ticket, that is used by @ref save() | 90 | * Requests a save ticket, that is used by @ref save() |
91 | */ | 91 | */ |
92 | virtual Ticket *requestSaveTicket(); | 92 | virtual Ticket *requestSaveTicket(); |
93 | 93 | ||
94 | /** | 94 | /** |
95 | * Loads all addressees from file to the address book. | 95 | * Loads all addressees from file to the address book. |
96 | * Returns true if all addressees could be loaded otherwise false. | 96 | * Returns true if all addressees could be loaded otherwise false. |
97 | */ | 97 | */ |
98 | virtual bool load(); | 98 | virtual bool load(); |
99 | 99 | ||
100 | /** | 100 | /** |
101 | * Saves all addresses from address book to file. | 101 | * Saves all addresses from address book to file. |
102 | * Returns true if all addressees could be saved otherwise false. | 102 | * Returns true if all addressees could be saved otherwise false. |
103 | * | 103 | * |
104 | * @param ticket The ticket returned by @ref requestSaveTicket() | 104 | * @param ticket The ticket returned by @ref requestSaveTicket() |
105 | */ | 105 | */ |
106 | virtual bool save( Ticket *ticket ); | 106 | virtual bool save( Ticket *ticket ); |
107 | 107 | ||
108 | /** | 108 | /** |
109 | * Remove a addressee from its source. | 109 | * Remove a addressee from its source. |
110 | * This method is mainly called by KABC::AddressBook. | 110 | * This method is mainly called by KABC::AddressBook. |
111 | */ | 111 | */ |
112 | virtual void removeAddressee( const Addressee& addr ); | 112 | virtual void removeAddressee( const Addressee& addr ); |
113 | 113 | ||
114 | /** | 114 | /** |
115 | * Set name of file to be used for saving. | 115 | * Set name of file to be used for saving. |
116 | */ | 116 | */ |
117 | virtual void setFileName( const QString & ); | 117 | virtual void setFileName( const QString & ); |
118 | 118 | ||
119 | /** | 119 | /** |
120 | * This method is called by an error handler if the application | 120 | * This method is called by an error handler if the application |
121 | * crashed | 121 | * crashed |
122 | */ | 122 | */ |
123 | virtual void cleanUp(); | 123 | virtual void cleanUp(); |
124 | 124 | ||
125 | /** | ||
126 | * This method returns the number of elements that are currently in the resource. | ||
127 | */ | ||
128 | virtual int count() const; | ||
129 | |||
130 | /** | ||
131 | * This method removes all elements from the resource!! (Not from the addressbook) | ||
132 | */ | ||
133 | virtual bool clear(); | ||
134 | |||
135 | protected slots: | 125 | protected slots: |
136 | void fileChanged(); | 126 | void fileChanged(); |
137 | 127 | ||
138 | protected: | 128 | protected: |
139 | void init( const QString &fileName ); | 129 | void init( const QString &fileName ); |
140 | 130 | ||
141 | bool lock( const QString &fileName ); | 131 | bool lock( const QString &fileName ); |
142 | void unlock( const QString &fileName ); | 132 | void unlock( const QString &fileName ); |
143 | 133 | ||
144 | private: | 134 | private: |
145 | AddressBookAccess* mAccess; | 135 | AddressBookAccess* mAccess; |
146 | QtopiaConverter* mConverter; | 136 | QtopiaConverter* mConverter; |
147 | 137 | ||
148 | QString mLockUniqueName; | 138 | QString mLockUniqueName; |
149 | 139 | ||
150 | KDirWatch mDirWatch; | 140 | KDirWatch mDirWatch; |
151 | }; | 141 | }; |
152 | 142 | ||
153 | } | 143 | } |
154 | 144 | ||
155 | #endif | 145 | #endif |
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp index 14f272d..70c3718 100644 --- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp | |||
@@ -188,168 +188,150 @@ bool ResourceSharpDTM::load() | |||
188 | addressee.setResource( this ); | 188 | addressee.setResource( this ); |
189 | addressBook()->insertAddressee( addressee ); | 189 | addressBook()->insertAddressee( addressee ); |
190 | } | 190 | } |
191 | } | 191 | } |
192 | 192 | ||
193 | return true; | 193 | return true; |
194 | } | 194 | } |
195 | 195 | ||
196 | bool ResourceSharpDTM::save( Ticket *ticket ) | 196 | bool ResourceSharpDTM::save( Ticket *ticket ) |
197 | { | 197 | { |
198 | qDebug("ResourceSharpDTM::save: %s", fileName().latin1()); | 198 | qDebug("ResourceSharpDTM::save: %s", fileName().latin1()); |
199 | 199 | ||
200 | 200 | ||
201 | KABC::AddressBook::Iterator it; | 201 | KABC::AddressBook::Iterator it; |
202 | bool res; | 202 | bool res; |
203 | 203 | ||
204 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | 204 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { |
205 | CardId id = 0;// 0 means new card | 205 | CardId id = 0;// 0 means new card |
206 | KABC::Addressee addressee = (*it); | 206 | KABC::Addressee addressee = (*it); |
207 | 207 | ||
208 | res = mAccess->startEditCard(id); | 208 | res = mAccess->startEditCard(id); |
209 | if (res == true) | 209 | if (res == true) |
210 | { | 210 | { |
211 | res = mConverter->addresseeToSharp( *it, mAccess, id ); | 211 | res = mConverter->addresseeToSharp( *it, mAccess, id ); |
212 | if (res == true) | 212 | if (res == true) |
213 | { | 213 | { |
214 | res = mAccess->finishEditCard(&id); | 214 | res = mAccess->finishEditCard(&id); |
215 | if (res == false) | 215 | if (res == false) |
216 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); | 216 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); |
217 | } | 217 | } |
218 | else | 218 | else |
219 | { | 219 | { |
220 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); | 220 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); |
221 | mAccess->cancelEditCard(); | 221 | mAccess->cancelEditCard(); |
222 | } | 222 | } |
223 | } | 223 | } |
224 | else | 224 | else |
225 | { | 225 | { |
226 | qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1()); | 226 | qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1()); |
227 | } | 227 | } |
228 | 228 | ||
229 | } | 229 | } |
230 | 230 | ||
231 | //US mAccess->save(); | 231 | //US mAccess->save(); |
232 | 232 | ||
233 | delete ticket; | 233 | delete ticket; |
234 | unlock( fileName() ); | 234 | unlock( fileName() ); |
235 | 235 | ||
236 | return true; | 236 | return true; |
237 | } | 237 | } |
238 | 238 | ||
239 | bool ResourceSharpDTM::lock( const QString &lockfileName ) | 239 | bool ResourceSharpDTM::lock( const QString &lockfileName ) |
240 | { | 240 | { |
241 | qDebug("ResourceSharpDTM::lock: %s", fileName().latin1()); | 241 | qDebug("ResourceSharpDTM::lock: %s", fileName().latin1()); |
242 | 242 | ||
243 | kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; | 243 | kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; |
244 | 244 | ||
245 | QString fn = lockfileName; | 245 | QString fn = lockfileName; |
246 | 246 | ||
247 | KURL url(fn); | 247 | KURL url(fn); |
248 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 248 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
249 | 249 | ||
250 | kdDebug(5700) << "-- lock name: " << lockName << endl; | 250 | kdDebug(5700) << "-- lock name: " << lockName << endl; |
251 | 251 | ||
252 | if (QFile::exists( lockName )) return false; | 252 | if (QFile::exists( lockName )) |
253 | { | ||
254 | qDebug("ResourceSharpDTM::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); | ||
255 | return false; | ||
256 | } | ||
257 | |||
253 | 258 | ||
254 | QString lockUniqueName; | 259 | QString lockUniqueName; |
255 | lockUniqueName = fn + KApplication::randomString( 8 ); | 260 | lockUniqueName = fn + KApplication::randomString( 8 ); |
256 | 261 | ||
257 | url = lockUniqueName; | 262 | url = lockUniqueName; |
258 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 263 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
259 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 264 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
260 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 265 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
261 | 266 | ||
262 | // Create unique file | 267 | // Create unique file |
263 | QFile file( mLockUniqueName ); | 268 | QFile file( mLockUniqueName ); |
264 | file.open( IO_WriteOnly ); | 269 | file.open( IO_WriteOnly ); |
265 | file.close(); | 270 | file.close(); |
266 | 271 | ||
267 | // Create lock file | 272 | // Create lock file |
268 | int result = 0; | 273 | int result = 0; |
269 | #ifndef _WIN32_ | 274 | #ifndef _WIN32_ |
270 | result = ::link( QFile::encodeName( mLockUniqueName ), | 275 | result = ::link( QFile::encodeName( mLockUniqueName ), |
271 | QFile::encodeName( lockName ) ); | 276 | QFile::encodeName( lockName ) ); |
272 | #endif | 277 | #endif |
273 | if ( result == 0 ) { | 278 | if ( result == 0 ) { |
274 | addressBook()->emitAddressBookLocked(); | 279 | addressBook()->emitAddressBookLocked(); |
275 | return true; | 280 | return true; |
276 | } | 281 | } |
277 | 282 | ||
278 | // TODO: check stat | 283 | // TODO: check stat |
279 | 284 | ||
280 | return false; | 285 | return false; |
281 | } | 286 | } |
282 | 287 | ||
283 | void ResourceSharpDTM::unlock( const QString &fileName ) | 288 | void ResourceSharpDTM::unlock( const QString &fileName ) |
284 | { | 289 | { |
285 | qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1()); | 290 | qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1()); |
286 | 291 | ||
287 | QString fn = fileName; | 292 | QString fn = fileName; |
288 | KURL url(fn); | 293 | KURL url(fn); |
289 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 294 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
290 | 295 | ||
291 | QFile::remove( lockName ); | 296 | QFile::remove( lockName ); |
292 | QFile::remove( mLockUniqueName ); | 297 | QFile::remove( mLockUniqueName ); |
293 | addressBook()->emitAddressBookUnlocked(); | 298 | addressBook()->emitAddressBookUnlocked(); |
294 | } | 299 | } |
295 | 300 | ||
296 | void ResourceSharpDTM::setFileName( const QString &newFileName ) | 301 | void ResourceSharpDTM::setFileName( const QString &newFileName ) |
297 | { | 302 | { |
298 | mDirWatch.stopScan(); | 303 | mDirWatch.stopScan(); |
299 | mDirWatch.removeFile( fileName() ); | 304 | mDirWatch.removeFile( fileName() ); |
300 | 305 | ||
301 | Resource::setFileName( newFileName ); | 306 | Resource::setFileName( newFileName ); |
302 | 307 | ||
303 | mDirWatch.addFile( fileName() ); | 308 | mDirWatch.addFile( fileName() ); |
304 | mDirWatch.startScan(); | 309 | mDirWatch.startScan(); |
305 | 310 | ||
306 | } | 311 | } |
307 | 312 | ||
308 | void ResourceSharpDTM::fileChanged() | 313 | void ResourceSharpDTM::fileChanged() |
309 | { | 314 | { |
310 | // There is a small theoretical chance that KDirWatch calls us before | 315 | // There is a small theoretical chance that KDirWatch calls us before |
311 | // we are fully constructed | 316 | // we are fully constructed |
312 | if (!addressBook()) | 317 | if (!addressBook()) |
313 | return; | 318 | return; |
314 | 319 | ||
315 | QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); | 320 | QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); |
316 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | 321 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { |
317 | load(); | 322 | load(); |
318 | addressBook()->emitAddressBookChanged(); | 323 | addressBook()->emitAddressBookChanged(); |
319 | } | 324 | } |
320 | } | 325 | } |
321 | 326 | ||
322 | void ResourceSharpDTM::removeAddressee( const Addressee &addr ) | 327 | void ResourceSharpDTM::removeAddressee( const Addressee &addr ) |
323 | { | 328 | { |
324 | } | 329 | } |
325 | 330 | ||
326 | void ResourceSharpDTM::cleanUp() | 331 | void ResourceSharpDTM::cleanUp() |
327 | { | 332 | { |
328 | unlock( fileName() ); | 333 | unlock( fileName() ); |
329 | } | 334 | } |
330 | 335 | ||
331 | /** | ||
332 | * This method returns the number of elements that are currently in the resource. | ||
333 | */ | ||
334 | int ResourceSharpDTM::count() const | ||
335 | { | ||
336 | if (mAccess != 0) | ||
337 | return mAccess->count(); | ||
338 | else | ||
339 | return 0; | ||
340 | } | ||
341 | |||
342 | |||
343 | /** | ||
344 | * This method removes all elements from the resource!! (Not from the addressbook) | ||
345 | */ | ||
346 | bool ResourceSharpDTM::clear() | ||
347 | { | ||
348 | if (mAccess != 0) | ||
349 | return mAccess->deleteCard(0, SlZDataBase::AllCard ); | ||
350 | else | ||
351 | return false; | ||
352 | } | ||
353 | |||
354 | 336 | ||
355 | 337 | ||
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.h b/kabc/plugins/sharpdtm/resourcesharpdtm.h index 60d38a1..f386e69 100644 --- a/kabc/plugins/sharpdtm/resourcesharpdtm.h +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.h | |||
@@ -61,96 +61,86 @@ public: | |||
61 | ResourceSharpDTM( const KConfig *cfg, bool syncable ); | 61 | ResourceSharpDTM( const KConfig *cfg, bool syncable ); |
62 | 62 | ||
63 | /** | 63 | /** |
64 | Construct file resource on file @arg fileName using format @arg formatName. | 64 | Construct file resource on file @arg fileName using format @arg formatName. |
65 | */ | 65 | */ |
66 | ResourceSharpDTM( const QString &fileName, bool syncable ); | 66 | ResourceSharpDTM( const QString &fileName, bool syncable ); |
67 | 67 | ||
68 | /** | 68 | /** |
69 | * Destructor. | 69 | * Destructor. |
70 | */ | 70 | */ |
71 | ~ResourceSharpDTM(); | 71 | ~ResourceSharpDTM(); |
72 | 72 | ||
73 | /** | 73 | /** |
74 | Writes the config back. | 74 | Writes the config back. |
75 | */ | 75 | */ |
76 | virtual void writeConfig( KConfig *cfg ); | 76 | virtual void writeConfig( KConfig *cfg ); |
77 | 77 | ||
78 | /** | 78 | /** |
79 | * Tries to open the file and checks for the proper format. | 79 | * Tries to open the file and checks for the proper format. |
80 | * This method should be called before @ref load(). | 80 | * This method should be called before @ref load(). |
81 | */ | 81 | */ |
82 | virtual bool doOpen(); | 82 | virtual bool doOpen(); |
83 | 83 | ||
84 | /** | 84 | /** |
85 | * Closes the file again. | 85 | * Closes the file again. |
86 | */ | 86 | */ |
87 | virtual void doClose(); | 87 | virtual void doClose(); |
88 | 88 | ||
89 | /** | 89 | /** |
90 | * Requests a save ticket, that is used by @ref save() | 90 | * Requests a save ticket, that is used by @ref save() |
91 | */ | 91 | */ |
92 | virtual Ticket *requestSaveTicket(); | 92 | virtual Ticket *requestSaveTicket(); |
93 | 93 | ||
94 | /** | 94 | /** |
95 | * Loads all addressees from file to the address book. | 95 | * Loads all addressees from file to the address book. |
96 | * Returns true if all addressees could be loaded otherwise false. | 96 | * Returns true if all addressees could be loaded otherwise false. |
97 | */ | 97 | */ |
98 | virtual bool load(); | 98 | virtual bool load(); |
99 | 99 | ||
100 | /** | 100 | /** |
101 | * Saves all addresses from address book to file. | 101 | * Saves all addresses from address book to file. |
102 | * Returns true if all addressees could be saved otherwise false. | 102 | * Returns true if all addressees could be saved otherwise false. |
103 | * | 103 | * |
104 | * @param ticket The ticket returned by @ref requestSaveTicket() | 104 | * @param ticket The ticket returned by @ref requestSaveTicket() |
105 | */ | 105 | */ |
106 | virtual bool save( Ticket *ticket ); | 106 | virtual bool save( Ticket *ticket ); |
107 | 107 | ||
108 | /** | 108 | /** |
109 | * Remove a addressee from its source. | 109 | * Remove a addressee from its source. |
110 | * This method is mainly called by KABC::AddressBook. | 110 | * This method is mainly called by KABC::AddressBook. |
111 | */ | 111 | */ |
112 | virtual void removeAddressee( const Addressee& addr ); | 112 | virtual void removeAddressee( const Addressee& addr ); |
113 | 113 | ||
114 | /** | 114 | /** |
115 | * Set name of file to be used for saving. | 115 | * Set name of file to be used for saving. |
116 | */ | 116 | */ |
117 | virtual void setFileName( const QString & ); | 117 | virtual void setFileName( const QString & ); |
118 | 118 | ||
119 | /** | 119 | /** |
120 | * This method is called by an error handler if the application | 120 | * This method is called by an error handler if the application |
121 | * crashed | 121 | * crashed |
122 | */ | 122 | */ |
123 | virtual void cleanUp(); | 123 | virtual void cleanUp(); |
124 | 124 | ||
125 | /** | ||
126 | * This method returns the number of elements that are currently in the resource. | ||
127 | */ | ||
128 | virtual int count() const; | ||
129 | |||
130 | /** | ||
131 | * This method removes all elements from the resource!! (Not from the addressbook) | ||
132 | */ | ||
133 | virtual bool clear(); | ||
134 | |||
135 | 125 | ||
136 | protected slots: | 126 | protected slots: |
137 | void fileChanged(); | 127 | void fileChanged(); |
138 | 128 | ||
139 | protected: | 129 | protected: |
140 | void init( const QString &fileName ); | 130 | void init( const QString &fileName ); |
141 | 131 | ||
142 | bool lock( const QString &fileName ); | 132 | bool lock( const QString &fileName ); |
143 | void unlock( const QString &fileName ); | 133 | void unlock( const QString &fileName ); |
144 | 134 | ||
145 | private: | 135 | private: |
146 | SlZDataBase* mAccess; | 136 | SlZDataBase* mAccess; |
147 | SharpDTMConverter* mConverter; | 137 | SharpDTMConverter* mConverter; |
148 | 138 | ||
149 | QString mLockUniqueName; | 139 | QString mLockUniqueName; |
150 | 140 | ||
151 | KDirWatch mDirWatch; | 141 | KDirWatch mDirWatch; |
152 | }; | 142 | }; |
153 | 143 | ||
154 | } | 144 | } |
155 | 145 | ||
156 | #endif | 146 | #endif |