summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/opie/resourceopie.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/opie/resourceopie.cpp') (more/less context) (ignore 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
@@ -5,154 +5,148 @@
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
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/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$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 "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}
135 129
136 130
137bool ResourceOpie::doOpen() 131bool ResourceOpie::doOpen()
138{ 132{
139 qDebug("ResourceOpie::doOpen: %s", fileName().latin1()); 133 qDebug("ResourceOpie::doOpen: %s", fileName().latin1());
140 134
141 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() ); 135 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() );
142 mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false); 136 mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false);
143 137
144 if ( !mAccess ) { 138 if ( !mAccess ) {
145 qDebug("Unable to load file() %s", fileName().latin1()); 139 qDebug("Unable to load file() %s", fileName().latin1());
146 return false; 140 return false;
147 } 141 }
148 142
149 mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available 143 mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available
150 144
151 145
152 if (mConverter == 0) 146 if (mConverter == 0)
153 { 147 {
154 mConverter = new OpieConverter(); 148 mConverter = new OpieConverter();
155 bool res = mConverter->init(); 149 bool res = mConverter->init();
156 if ( !res ) 150 if ( !res )
157 { 151 {
158 qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); 152 qDebug("Unable to initialize opie converter. Most likely a problem with the category file");
@@ -206,151 +200,141 @@ bool ResourceOpie::load()
206 addressBook()->insertAddressee( addressee ); 200 addressBook()->insertAddressee( addressee );
207 } 201 }
208 202
209 } 203 }
210 204
211 205
212 return true; 206 return true;
213} 207}
214 208
215bool ResourceOpie::save( Ticket *ticket ) 209bool ResourceOpie::save( Ticket *ticket )
216{ 210{
217 qDebug("ResourceOpie::save: %s", fileName().latin1()); 211 qDebug("ResourceOpie::save: %s", fileName().latin1());
218 212
219 213
220 214
221 KABC::AddressBook::Iterator it; 215 KABC::AddressBook::Iterator it;
222 bool res; 216 bool res;
223 217
224 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 218 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
225 OContact c; 219 OContact c;
226 KABC::Addressee addressee = (*it); 220 KABC::Addressee addressee = (*it);
227 221
228 res = mConverter->addresseeToOpie( *it, c ); 222 res = mConverter->addresseeToOpie( *it, c );
229 if (res == true) 223 if (res == true)
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
333 // we are fully constructed 317 // we are fully constructed
334 if (!addressBook()) 318 if (!addressBook())
335 return; 319 return;
336 320
337 QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); 321 QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
338 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 322 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
339 load(); 323 load();
340 addressBook()->emitAddressBookChanged(); 324 addressBook()->emitAddressBookChanged();
341 } 325 }
342 326
343 load(); 327 load();
344 addressBook()->emitAddressBookChanged(); 328 addressBook()->emitAddressBookChanged();
345} 329}
346 330
347void ResourceOpie::removeAddressee( const Addressee &addr ) 331void ResourceOpie::removeAddressee( const Addressee &addr )
348{ 332{
349} 333}
350 334
351void ResourceOpie::cleanUp() 335void ResourceOpie::cleanUp()
352{ 336{
353// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1()); 337// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1());
354 338
355 unlock( fileName() ); 339 unlock( fileName() );
356} 340}