summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/opie/resourceopie.cpp
authorulf69 <ulf69>2004-08-02 18:32:08 (UTC)
committer ulf69 <ulf69>2004-08-02 18:32:08 (UTC)
commit863c4c3678e59ef125c08c00e9532ded5b540f67 (patch) (unidiff)
tree449428c45689ffe10ff1ce4caa18f959c4dfe402 /kabc/plugins/opie/resourceopie.cpp
parent983127ce46c9ff6663d08dedd450da3fe7dd549b (diff)
downloadkdepimpi-863c4c3678e59ef125c08c00e9532ded5b540f67.zip
kdepimpi-863c4c3678e59ef125c08c00e9532ded5b540f67.tar.gz
kdepimpi-863c4c3678e59ef125c08c00e9532ded5b540f67.tar.bz2
added support for syncprofiles for resources and export/import capabilities
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
@@ -52,2 +52,3 @@ $Id$
52#include "stdaddressbook.h" 52#include "stdaddressbook.h"
53#include "syncwidget.h"
53 54
@@ -62,3 +63,3 @@ extern "C"
62 { 63 {
63 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>(); 64 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig, KRES::SyncWidget>();
64 } 65 }
@@ -66,4 +67,4 @@ extern "C"
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{
@@ -74,3 +75,3 @@ ResourceOpie::ResourceOpie( const KConfig *config )
74 fileName = cfg->readEntry( "FileName", fileName ); 75 fileName = cfg->readEntry( "FileName", fileName );
75 76
76 } 77 }
@@ -78,3 +79,3 @@ ResourceOpie::ResourceOpie( const KConfig *config )
78// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() ); 79// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() );
79 80
80 init( fileName ); 81 init( fileName );
@@ -82,4 +83,4 @@ ResourceOpie::ResourceOpie( const KConfig *config )
82 83
83ResourceOpie::ResourceOpie( const QString &fileName ) 84ResourceOpie::ResourceOpie( const QString &fileName, bool syncable )
84 : Resource( 0 ) 85 : Resource( 0, syncable )
85{ 86{
@@ -95,6 +96,6 @@ void ResourceOpie::init( const QString &fileName )
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}
@@ -105,2 +106,6 @@ ResourceOpie::~ResourceOpie()
105 delete mConverter; 106 delete mConverter;
107
108 if(mAccess != 0)
109 delete mAccess;
110
106} 111}
@@ -111,6 +116,3 @@ void ResourceOpie::writeConfig( KConfig *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}
@@ -121,7 +123,9 @@ Ticket *ResourceOpie::requestSaveTicket()
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;
@@ -134,58 +138,15 @@ bool 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)
@@ -193,3 +154,3 @@ bool ResourceOpie::load()
193 mConverter = new OpieConverter(); 154 mConverter = new OpieConverter();
194 res = mConverter->init(); 155 bool res = mConverter->init();
195 if ( !res ) 156 if ( !res )
@@ -197,4 +158,4 @@ bool ResourceOpie::load()
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;
@@ -204,4 +165,31 @@ bool ResourceOpie::load()
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 )
@@ -209,7 +197,7 @@ bool ResourceOpie::load()
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 )
@@ -219,10 +207,6 @@ bool ResourceOpie::load()
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;
@@ -232,55 +216,40 @@ bool 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());
@@ -288,3 +257,3 @@ bool ResourceOpie::lock( const QString &fileName )
288 257
289 QString fn = fileName; 258 QString fn = lockfileName;
290 259
@@ -293,6 +262,6 @@ bool ResourceOpie::lock( const QString &fileName )
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;
@@ -303,4 +272,4 @@ bool ResourceOpie::lock( const QString &fileName )
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 );
@@ -332,4 +301,4 @@ void 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;
@@ -339,5 +308,5 @@ void ResourceOpie::unlock( const QString &fileName )
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 );
@@ -347,20 +316,14 @@ void ResourceOpie::unlock( const QString &fileName )
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
@@ -372,4 +335,4 @@ void ResourceOpie::fileChanged()
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 ) {
@@ -378,3 +341,3 @@ void ResourceOpie::fileChanged()
378 } 341 }
379 342
380 load(); 343 load();
@@ -391,5 +354,38 @@ void ResourceOpie::cleanUp()
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"