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.cpp302
1 files changed, 149 insertions, 153 deletions
diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp
index aaee801..1222242 100644
--- a/kabc/plugins/opie/resourceopie.cpp
+++ b/kabc/plugins/opie/resourceopie.cpp
@@ -50,6 +50,7 @@ $Id$
50 50
51#include "resourceopieconfig.h" 51#include "resourceopieconfig.h"
52#include "stdaddressbook.h" 52#include "stdaddressbook.h"
53#include "syncwidget.h"
53 54
54#include "opieconverter.h" 55#include "opieconverter.h"
55 56
@@ -60,28 +61,28 @@ extern "C"
60{ 61{
61 void *init_microkabc_opie() 62 void *init_microkabc_opie()
62 { 63 {
63 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>(); 64 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, KRES::SyncWidget>();
64 } 65 }
65} 66}
66 67
67ResourceOpie::ResourceOpie( const KConfig *config ) 68ResourceOpie::ResourceOpie( const KConfig *config, bool syncable )
68 : Resource( config ), mConverter (0) 69 : Resource( config, syncable ), mAccess(0), mConverter (0)
69{ 70{
70 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; 71 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml";
71 72
72 KConfig *cfg = (KConfig *)config; 73 KConfig *cfg = (KConfig *)config;
73 if ( cfg ) { 74 if ( cfg ) {
74 fileName = cfg->readEntry( "FileName", fileName ); 75 fileName = cfg->readEntry( "FileName", fileName );
75 76
76 } 77 }
77 78
78// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() ); 79// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() );
79 80
80 init( fileName ); 81 init( fileName );
81} 82}
82 83
83ResourceOpie::ResourceOpie( const QString &fileName ) 84ResourceOpie::ResourceOpie( const QString &fileName, bool syncable )
84 : Resource( 0 ) 85 : Resource( 0, syncable )
85{ 86{
86// qDebug("ResourceOpie::ResourceOpie : 3 %s", fileName.latin1()); 87// qDebug("ResourceOpie::ResourceOpie : 3 %s", fileName.latin1());
87 init( fileName ); 88 init( fileName );
@@ -93,37 +94,40 @@ void ResourceOpie::init( const QString &fileName )
93 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 94 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
94 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 95 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
95 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 96 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
96 97
97 98
98 setFileName( fileName ); 99 setFileName( fileName );
99 100
100} 101}
101 102
102ResourceOpie::~ResourceOpie() 103ResourceOpie::~ResourceOpie()
103{ 104{
104 if (mConverter != 0) 105 if (mConverter != 0)
105 delete mConverter; 106 delete mConverter;
107
108 if(mAccess != 0)
109 delete mAccess;
110
106} 111}
107 112
108void ResourceOpie::writeConfig( KConfig *config ) 113void ResourceOpie::writeConfig( KConfig *config )
109{ 114{
110 Resource::writeConfig( config ); 115 Resource::writeConfig( config );
111 116
112 config->writeEntry( "FileName", mFileName ); 117 config->writeEntry( "FileName", fileName() );
113
114// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
115
116} 118}
117 119
118Ticket *ResourceOpie::requestSaveTicket() 120Ticket *ResourceOpie::requestSaveTicket()
119{ 121{
120 kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl; 122 kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl;
121 123
124 qDebug("ResourceOpie::requestSaveTicket: %s", fileName().latin1());
125
122 if ( !addressBook() ) return 0; 126 if ( !addressBook() ) return 0;
123 127
124 if ( !lock( mFileName ) ) { 128 if ( !lock( fileName() ) ) {
125 kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '" 129 kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '"
126 << mFileName << "'" << endl; 130 << fileName() << "'" << endl;
127 return 0; 131 return 0;
128 } 132 }
129 return createTicket( this ); 133 return createTicket( this );
@@ -132,177 +136,142 @@ Ticket *ResourceOpie::requestSaveTicket()
132 136
133bool ResourceOpie::doOpen() 137bool ResourceOpie::doOpen()
134{ 138{
135// qDebug("ResourceOpie::doOpen() %s", mFileName.latin1()); 139 qDebug("ResourceOpie::doOpen: %s", fileName().latin1());
136/*US
137 QFile file( mFileName );
138
139 if ( !file.exists() ) {
140 // try to create the file
141 bool ok = file.open( IO_WriteOnly );
142 if ( ok )
143 file.close();
144
145 return ok;
146 } else {
147 if ( !file.open( IO_ReadWrite ) )
148 return false;
149
150 if ( file.size() == 0 ) {
151 file.close();
152 return true;
153 }
154 140
155//US bool ok = mFormat->checkFormat( &file ); 141 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() );
156 bool ok = true; 142 mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false);
157
158 file.close();
159 143
160 return ok; 144 if ( !mAccess ) {
145 qDebug("Unable to load file() %s", fileName().latin1());
146 return false;
161 } 147 }
162*/
163 qDebug("ResourceOpie::doOpen() has to be fixed - %s", mFileName.latin1());
164 return true;
165}
166 148
167void ResourceOpie::doClose() 149 mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available
168{
169// qDebug("ResourceOpie::doClose() %s", mFileName.latin1());
170}
171 150
172bool ResourceOpie::load()
173{
174// qDebug("ResourceOpie::load() %s", mFileName.latin1());
175 kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl;
176
177 qDebug("ResourceOpie::load: Try to load file() %s", mFileName.latin1());
178
179 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", mFileName );
180 OContactAccess* access = new OContactAccess("KDEPim/Pi", 0l, backend, false);
181
182 if ( !access ) {
183 qDebug("Unable to load file() %s", mFileName.latin1());
184 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
185 return false;
186 }
187
188 access -> setReadAhead( 32 ); // Use ReadAhead-Cache if available
189 151
190 bool res = false;
191 if (mConverter == 0) 152 if (mConverter == 0)
192 { 153 {
193 mConverter = new OpieConverter(); 154 mConverter = new OpieConverter();
194 res = mConverter->init(); 155 bool res = mConverter->init();
195 if ( !res ) 156 if ( !res )
196 { 157 {
197 qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); 158 qDebug("Unable to initialize opie converter. Most likely a problem with the category file");
198 addressBook()->error( i18n( "Unable to initialize opie converter. Most likely a problem with the category file" ) ); 159 delete mAccess;
199 delete access; 160 mAccess = 0;
200 return false; 161 return false;
201 } 162 }
202 } 163 }
203 164
204 165
205 OContactAccess::List::Iterator it; 166
206 OContactAccess::List allList = access->allRecords(); 167 return true;
168}
169
170void ResourceOpie::doClose()
171{
172 qDebug("ResourceOpie::doClose: %s", fileName().latin1());
173
174 if(mAccess)
175 {
176 delete mAccess;
177 mAccess = 0;
178 }
179 // it seems so, that deletion of access deletes backend as well
180 //delete backend;
181
182 return;
183
184}
185
186
187bool ResourceOpie::load()
188{
189 qDebug("ResourceOpie::load: %s", fileName().latin1());
190
191 bool res = false;
192
193 OContactAccess::List::Iterator it;
194 OContactAccess::List allList = mAccess->allRecords();
207 for ( it = allList.begin(); it != allList.end(); ++it ) 195 for ( it = allList.begin(); it != allList.end(); ++it )
208 { 196 {
209 const OContact c = (*it); 197 const OContact c = (*it);
210 198
211 KABC::Addressee addressee; 199 KABC::Addressee addressee;
212 200
213 res = mConverter->opieToAddressee( c, addressee ); 201 res = mConverter->opieToAddressee( c, addressee );
214 202
215 if ( !addressee.isEmpty() && res ) 203 if ( !addressee.isEmpty() && res )
216 { 204 {
217 addressee.setResource( this ); 205 addressee.setResource( this );
218 addressBook()->insertAddressee( addressee ); 206 addressBook()->insertAddressee( addressee );
219 } 207 }
220 208
221// qDebug("found %s", c.fullName().latin1());
222 } 209 }
223 210
224 delete access; 211
225 // it seems so, that deletion of access deletes backend as well
226 //delete backend;
227
228 return true; 212 return true;
229} 213}
230 214
231bool ResourceOpie::save( Ticket *ticket ) 215bool ResourceOpie::save( Ticket *ticket )
232{ 216{
233 qDebug("ResourceOpie::save() has to be fixed - %s", mFileName.latin1()); 217 qDebug("ResourceOpie::save: %s", fileName().latin1());
234/*US 218
235 219
236 qDebug("ResourceOpie::save %s", mFileName.latin1()); 220
237 kdDebug(5700) << "ResourceOpie::save()" << endl; 221 KABC::AddressBook::Iterator it;
238 222 bool res;
239 // create backup file 223
240 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 224 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
241*/ 225 OContact c;
242/*US we use a simpler method to create a backupfile 226 KABC::Addressee addressee = (*it);
243 227
244 (void) KSaveFile::backupFile( mFileName, QString::null 228 res = mConverter->addresseeToOpie( *it, c );
245 ,extension ); 229 if (res == true)
246 230 {
247 KSaveFile saveFile( mFileName ); 231 res = mAccess->add(c);
248 bool ok = false; 232 if (res == false)
249 if ( saveFile.status() == 0 && saveFile.file() ) 233 qDebug("Unable to append Contact %s", c.fullName().latin1());
250 { 234 }
251 mFormat->saveAll( addressBook(), this, saveFile.file() ); 235 else
252 ok = saveFile.close(); 236 {
253 } 237 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1());
254*/ 238 }
255/*US
256//US ToDo: write backupfile
257 QFile info;
258 info.setName( mFileName );
259 bool ok = info.open( IO_WriteOnly );
260 if ( ok ) {
261//US mFormat->saveAll( addressBook(), this, &info );
262
263 info.close();
264 ok = true;
265 }
266 else {
267
268 } 239 }
269 240
270 if ( !ok ) 241 mAccess->save();
271 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 242
272
273 delete ticket; 243 delete ticket;
274 unlock( mFileName ); 244 unlock( fileName() );
275
276 return ok;
277 245
278 qDebug("ResourceOpie::save has to be changed");
279*/
280 return true; 246 return true;
281 247
282} 248}
283 249
284bool ResourceOpie::lock( const QString &fileName ) 250bool ResourceOpie::lock( const QString &lockfileName )
285{ 251{
252 qDebug("ResourceOpie::lock: %s", fileName().latin1());
253
254
286// qDebug("ResourceOpie::lock() %s", fileName.latin1()); 255// qDebug("ResourceOpie::lock() %s", fileName.latin1());
287 kdDebug(5700) << "ResourceOpie::lock()" << endl; 256 kdDebug(5700) << "ResourceOpie::lock()" << endl;
288 257
289 QString fn = fileName; 258 QString fn = lockfileName;
290 259
291//US change the implementation how the lockfilename is getting created 260//US change the implementation how the lockfilename is getting created
292//US fn.replace( QRegExp("/"), "_" ); 261//US fn.replace( QRegExp("/"), "_" );
293//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 262//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
294 263
295 KURL url(fn); 264 KURL url(fn);
296 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 265 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
297 266
298 kdDebug(5700) << "-- lock name: " << lockName << endl; 267 kdDebug(5700) << "-- lock name: " << lockName << endl;
299 268
300 if (QFile::exists( lockName )) return false; 269 if (QFile::exists( lockName )) return false;
301 270
302 QString lockUniqueName; 271 QString lockUniqueName;
303 lockUniqueName = fn + KApplication::randomString( 8 ); 272 lockUniqueName = fn + KApplication::randomString( 8 );
304 273
305 url = lockUniqueName; 274 url = lockUniqueName;
306//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 275//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
307 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 276 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
308 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 277 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
@@ -330,39 +299,33 @@ bool ResourceOpie::lock( const QString &fileName )
330 299
331void ResourceOpie::unlock( const QString &fileName ) 300void ResourceOpie::unlock( const QString &fileName )
332{ 301{
333// qDebug("ResourceOpie::unlock() %s", fileName.latin1()); 302 qDebug("ResourceOpie::unlock() %s", fileName.latin1());
334 303
335 QString fn = fileName; 304 QString fn = fileName;
336//US change the implementation how the lockfilename is getting created 305//US change the implementation how the lockfilename is getting created
337//US fn.replace( QRegExp( "/" ), "_" ); 306//US fn.replace( QRegExp( "/" ), "_" );
338//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 307//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
339//US QString lockName = fn + ".lock"; 308//US QString lockName = fn + ".lock";
340 KURL url(fn); 309 KURL url(fn);
341 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 310 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
342 311
343 QFile::remove( lockName ); 312 QFile::remove( lockName );
344 QFile::remove( mLockUniqueName ); 313 QFile::remove( mLockUniqueName );
345 addressBook()->emitAddressBookUnlocked(); 314 addressBook()->emitAddressBookUnlocked();
346} 315}
347 316
348void ResourceOpie::setFileName( const QString &fileName ) 317void ResourceOpie::setFileName( const QString &newFileName )
349{ 318{
350 mDirWatch.stopScan(); 319 mDirWatch.stopScan();
351 mDirWatch.removeFile( mFileName ); 320 mDirWatch.removeFile( fileName() );
352 321
353 mFileName = fileName; 322 Resource::setFileName( newFileName );
354 323
355 mDirWatch.addFile( mFileName ); 324 mDirWatch.addFile( fileName() );
356 mDirWatch.startScan(); 325 mDirWatch.startScan();
357 326
358//US simulate KDirWatch event
359//US fileChanged();
360} 327}
361 328
362QString ResourceOpie::fileName() const
363{
364 return mFileName;
365}
366 329
367void ResourceOpie::fileChanged() 330void ResourceOpie::fileChanged()
368{ 331{
@@ -370,13 +333,13 @@ void ResourceOpie::fileChanged()
370 // we are fully constructed 333 // we are fully constructed
371 if (!addressBook()) 334 if (!addressBook())
372 return; 335 return;
373 336
374 QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 337 QString text( i18n( "Opie resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
375 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 338 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
376 load(); 339 load();
377 addressBook()->emitAddressBookChanged(); 340 addressBook()->emitAddressBookChanged();
378 } 341 }
379 342
380 load(); 343 load();
381 addressBook()->emitAddressBookChanged(); 344 addressBook()->emitAddressBookChanged();
382} 345}
@@ -389,7 +352,40 @@ void ResourceOpie::cleanUp()
389{ 352{
390// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1()); 353// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1());
391 354
392 unlock( mFileName ); 355 unlock( fileName() );
356}
357
358
359/**
360 * This method returns the number of elements that are currently in the resource.
361 */
362int ResourceOpie::count() const
363{
364 qDebug("ResourceOpie::count: %x", mAccess);
365
366 if (mAccess != 0)
367 {
368 OContactAccess::List contactList = mAccess->allRecords();
369 return contactList.count();
370 }
371 else
372 return 0;
373}
374
375
376/**
377 * This method removes all elements from the resource!! (Not from the addressbook)
378 */
379bool ResourceOpie::clear()
380{
381 if (mAccess != 0) {
382 mAccess->clear();
383 return true;
384 }
385 else
386 return false;
393} 387}
394 388
389
390
395//US #include "resourceopie.moc" 391//US #include "resourceopie.moc"