summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/opie/resourceopie.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/opie/resourceopie.cpp') (more/less context) (show whitespace changes)
-rw-r--r--kabc/plugins/opie/resourceopie.cpp20
1 files changed, 2 insertions, 18 deletions
diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp
index 1222242..5559827 100644
--- a/kabc/plugins/opie/resourceopie.cpp
+++ b/kabc/plugins/opie/resourceopie.cpp
@@ -29,106 +29,100 @@ $Id$
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 "syncwidget.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
59using namespace KABC; 59using namespace KABC;
60extern "C" 60extern "C"
61{ 61{
62 void *init_microkabc_opie() 62 void *init_microkabc_opie()
63 { 63 {
64 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, KRES::SyncWidget>(); 64 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, SyncPrefWidget>();
65 } 65 }
66} 66}
67 67
68ResourceOpie::ResourceOpie( const KConfig *config, bool syncable ) 68ResourceOpie::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// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() );
80
81 init( fileName ); 79 init( fileName );
82} 80}
83 81
84ResourceOpie::ResourceOpie( const QString &fileName, bool syncable ) 82ResourceOpie::ResourceOpie( const QString &fileName, bool syncable )
85 : Resource( 0, syncable ) 83 : Resource( 0, syncable )
86{ 84{
87// qDebug("ResourceOpie::ResourceOpie : 3 %s", fileName.latin1());
88 init( fileName ); 85 init( fileName );
89} 86}
90 87
91void ResourceOpie::init( const QString &fileName ) 88void ResourceOpie::init( const QString &fileName )
92{ 89{
93 90
94 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 91 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
95 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 92 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
96 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 93 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
97 94
98
99 setFileName( fileName ); 95 setFileName( fileName );
100
101} 96}
102 97
103ResourceOpie::~ResourceOpie() 98ResourceOpie::~ResourceOpie()
104{ 99{
105 if (mConverter != 0) 100 if (mConverter != 0)
106 delete mConverter; 101 delete mConverter;
107 102
108 if(mAccess != 0) 103 if(mAccess != 0)
109 delete mAccess; 104 delete mAccess;
110
111} 105}
112 106
113void ResourceOpie::writeConfig( KConfig *config ) 107void ResourceOpie::writeConfig( KConfig *config )
114{ 108{
115 Resource::writeConfig( config ); 109 Resource::writeConfig( config );
116 110
117 config->writeEntry( "FileName", fileName() ); 111 config->writeEntry( "FileName", fileName() );
118} 112}
119 113
120Ticket *ResourceOpie::requestSaveTicket() 114Ticket *ResourceOpie::requestSaveTicket()
121{ 115{
122 kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl; 116 kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl;
123 117
124 qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1()); 118 qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1());
125 119
126 if ( !addressBook() ) return 0; 120 if ( !addressBook() ) return 0;
127 121
128 if ( !lock( fileName() ) ) { 122 if ( !lock( fileName() ) ) {
129 kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '" 123 kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '"
130 << fileName() << "'" << endl; 124 << fileName() << "'" << endl;
131 return 0; 125 return 0;
132 } 126 }
133 return createTicket( this ); 127 return createTicket( this );
134} 128}
@@ -230,103 +224,93 @@ bool ResourceOpie::save( Ticket *ticket )
230 { 224 {
231 res = mAccess->add(c); 225 res = mAccess->add(c);
232 if (res == false) 226 if (res == false)
233 qDebug("Unable to append Contact %s", c.fullName().latin1()); 227 qDebug("Unable to append Contact %s", c.fullName().latin1());
234 } 228 }
235 else 229 else
236 { 230 {
237 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); 231 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1());
238 } 232 }
239 } 233 }
240 234
241 mAccess->save(); 235 mAccess->save();
242 236
243 delete ticket; 237 delete ticket;
244 unlock( fileName() ); 238 unlock( fileName() );
245 239
246 return true; 240 return true;
247 241
248} 242}
249 243
250bool ResourceOpie::lock( const QString &lockfileName ) 244bool ResourceOpie::lock( const QString &lockfileName )
251{ 245{
252 qDebug("ResourceOpie::lock: %s", fileName().latin1()); 246 qDebug("ResourceOpie::lock: %s", fileName().latin1());
253 247
254
255// qDebug("ResourceOpie::lock() %s", fileName.latin1());
256 kdDebug(5700) << "ResourceOpie::lock()" << endl; 248 kdDebug(5700) << "ResourceOpie::lock()" << endl;
257 249
258 QString fn = lockfileName; 250 QString fn = lockfileName;
259 251
260//US change the implementation how the lockfilename is getting created
261//US fn.replace( QRegExp("/"), "_" );
262//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
263
264 KURL url(fn); 252 KURL url(fn);
265 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 253 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
266 254
267 kdDebug(5700) << "-- lock name: " << lockName << endl; 255 kdDebug(5700) << "-- lock name: " << lockName << endl;
268 256
269 if (QFile::exists( lockName )) return false; 257 if (QFile::exists( lockName )) return false;
270 258
271 QString lockUniqueName; 259 QString lockUniqueName;
272 lockUniqueName = fn + KApplication::randomString( 8 ); 260 lockUniqueName = fn + KApplication::randomString( 8 );
273 261
274 url = lockUniqueName; 262 url = lockUniqueName;
275//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 263//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
276 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 264 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
277 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 265 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
278 266
279 // Create unique file 267 // Create unique file
280 QFile file( mLockUniqueName ); 268 QFile file( mLockUniqueName );
281 file.open( IO_WriteOnly ); 269 file.open( IO_WriteOnly );
282 file.close(); 270 file.close();
283 271
284 // Create lock file 272 // Create lock file
285 int result = 0; 273 int result = 0;
286#ifndef _WIN32_ 274#ifndef _WIN32_
287 result = ::link( QFile::encodeName( mLockUniqueName ), 275 result = ::link( QFile::encodeName( mLockUniqueName ),
288 QFile::encodeName( lockName ) ); 276 QFile::encodeName( lockName ) );
289#endif 277#endif
290 if ( result == 0 ) { 278 if ( result == 0 ) {
291 addressBook()->emitAddressBookLocked(); 279 addressBook()->emitAddressBookLocked();
292 return true; 280 return true;
293 } 281 }
294 282
295 // TODO: check stat 283 // TODO: check stat
296 284
297 return false; 285 return false;
298} 286}
299 287
300void ResourceOpie::unlock( const QString &fileName ) 288void ResourceOpie::unlock( const QString &fileName )
301{ 289{
302 qDebug("ResourceOpie::unlock() %s", fileName.latin1()); 290 qDebug("ResourceOpie::unlock() %s", fileName.latin1());
303 291
304 QString fn = fileName; 292 QString fn = fileName;
305//US change the implementation how the lockfilename is getting created
306//US fn.replace( QRegExp( "/" ), "_" );
307//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
308//US QString lockName = fn + ".lock";
309 KURL url(fn); 293 KURL url(fn);
310 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 294 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
311 295
312 QFile::remove( lockName ); 296 QFile::remove( lockName );
313 QFile::remove( mLockUniqueName ); 297 QFile::remove( mLockUniqueName );
314 addressBook()->emitAddressBookUnlocked(); 298 addressBook()->emitAddressBookUnlocked();
315} 299}
316 300
317void ResourceOpie::setFileName( const QString &newFileName ) 301void ResourceOpie::setFileName( const QString &newFileName )
318{ 302{
319 mDirWatch.stopScan(); 303 mDirWatch.stopScan();
320 mDirWatch.removeFile( fileName() ); 304 mDirWatch.removeFile( fileName() );
321 305
322 Resource::setFileName( newFileName ); 306 Resource::setFileName( newFileName );
323 307
324 mDirWatch.addFile( fileName() ); 308 mDirWatch.addFile( fileName() );
325 mDirWatch.startScan(); 309 mDirWatch.startScan();
326 310
327} 311}
328 312
329 313
330void ResourceOpie::fileChanged() 314void ResourceOpie::fileChanged()
331{ 315{
332 // There is a small theoretical chance that KDirWatch calls us before 316 // There is a small theoretical chance that KDirWatch calls us before