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.cpp224
1 files changed, 110 insertions, 114 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,12 +61,12 @@ 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
@@ -80,8 +81,8 @@ ResourceOpie::ResourceOpie( const KConfig *config )
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 );
@@ -103,27 +104,30 @@ ResourceOpie::~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,78 +136,62 @@ 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 140
145 return ok; 141 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() );
146 } else { 142 mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false);
147 if ( !file.open( IO_ReadWrite ) )
148 return false;
149 143
150 if ( file.size() == 0 ) { 144 if ( !mAccess ) {
151 file.close(); 145 qDebug("Unable to load file() %s", fileName().latin1());
152 return true; 146 return false;
153 } 147 }
154 148
155//US bool ok = mFormat->checkFormat( &file ); 149 mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available
156 bool ok = true;
157 150
158 file.close();
159 151
160 return ok; 152 if (mConverter == 0)
153 {
154 mConverter = new OpieConverter();
155 bool res = mConverter->init();
156 if ( !res )
157 {
158 qDebug("Unable to initialize opie converter. Most likely a problem with the category file");
159 delete mAccess;
160 mAccess = 0;
161 return false;
161 } 162 }
162*/ 163 }
163 qDebug("ResourceOpie::doOpen() has to be fixed - %s", mFileName.latin1()); 164
165
166
164 return true; 167 return true;
165} 168}
166 169
167void ResourceOpie::doClose() 170void ResourceOpie::doClose()
168{ 171{
169// qDebug("ResourceOpie::doClose() %s", mFileName.latin1()); 172 qDebug("ResourceOpie::doClose: %s", fileName().latin1());
170}
171 173
172bool ResourceOpie::load() 174 if(mAccess)
173{ 175{
174// qDebug("ResourceOpie::load() %s", mFileName.latin1()); 176 delete mAccess;
175 kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl; 177 mAccess = 0;
176 178 }
177 qDebug("ResourceOpie::load: Try to load file() %s", mFileName.latin1()); 179 // it seems so, that deletion of access deletes backend as well
180 //delete backend;
178 181
179 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", mFileName ); 182 return;
180 OContactAccess* access = new OContactAccess("KDEPim/Pi", 0l, backend, false);
181 183
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 } 184 }
187 185
188 access -> setReadAhead( 32 ); // Use ReadAhead-Cache if available
189 186
190 bool res = false; 187bool ResourceOpie::load()
191 if (mConverter == 0)
192 {
193 mConverter = new OpieConverter();
194 res = mConverter->init();
195 if ( !res )
196 { 188 {
197 qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); 189 qDebug("ResourceOpie::load: %s", fileName().latin1());
198 addressBook()->error( i18n( "Unable to initialize opie converter. Most likely a problem with the category file" ) );
199 delete access;
200 return false;
201 }
202 }
203 190
191 bool res = false;
204 192
205 OContactAccess::List::Iterator it; 193 OContactAccess::List::Iterator it;
206 OContactAccess::List allList = access->allRecords(); 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);
@@ -218,75 +206,56 @@ bool ResourceOpie::load()
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;
225 // it seems so, that deletion of access deletes backend as well
226 //delete backend;
227 211
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
235 218
236 qDebug("ResourceOpie::save %s", mFileName.latin1());
237 kdDebug(5700) << "ResourceOpie::save()" << endl;
238 219
239 // create backup file
240 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
241*/
242/*US we use a simpler method to create a backupfile
243 220
244 (void) KSaveFile::backupFile( mFileName, QString::null 221 KABC::AddressBook::Iterator it;
245 ,extension ); 222 bool res;
223
224 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
225 OContact c;
226 KABC::Addressee addressee = (*it);
246 227
247 KSaveFile saveFile( mFileName ); 228 res = mConverter->addresseeToOpie( *it, c );
248 bool ok = false; 229 if (res == true)
249 if ( saveFile.status() == 0 && saveFile.file() )
250 { 230 {
251 mFormat->saveAll( addressBook(), this, saveFile.file() ); 231 res = mAccess->add(c);
252 ok = saveFile.close(); 232 if (res == false)
233 qDebug("Unable to append Contact %s", c.fullName().latin1());
253 } 234 }
254*/ 235 else
255/*US 236 {
256//US ToDo: write backupfile 237 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1());
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 } 238 }
266 else {
267
268 } 239 }
269 240
270 if ( !ok ) 241 mAccess->save();
271 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
272 242
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("/"), "_" );
@@ -330,7 +299,7 @@ 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
@@ -345,24 +314,18 @@ void ResourceOpie::unlock( const QString &fileName )
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{
@@ -371,7 +334,7 @@ void ResourceOpie::fileChanged()
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();
@@ -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() );
393} 356}
394 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;
387}
388
389
390
395//US #include "resourceopie.moc" 391//US #include "resourceopie.moc"