summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/qtopia/resourceqtopia.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/qtopia/resourceqtopia.cpp') (more/less context) (show whitespace changes)
-rw-r--r--kabc/plugins/qtopia/resourceqtopia.cpp166
1 files changed, 96 insertions, 70 deletions
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp
index 7e10e46..dc88272 100644
--- a/kabc/plugins/qtopia/resourceqtopia.cpp
+++ b/kabc/plugins/qtopia/resourceqtopia.cpp
@@ -46,35 +46,36 @@ $Id$
46 46
47 47
48#include "resourceqtopiaconfig.h" 48#include "resourceqtopiaconfig.h"
49#include "stdaddressbook.h" 49#include "stdaddressbook.h"
50 50
51#include "qtopiaconverter.h" 51#include "qtopiaconverter.h"
52#include "syncwidget.h"
52 53
53#include "resourceqtopia.h" 54#include "resourceqtopia.h"
54 55
55using namespace KABC; 56using namespace KABC;
56extern "C" 57extern "C"
57{ 58{
58 void *init_microkabc_qtopia() 59 void *init_microkabc_qtopia()
59 { 60 {
60 return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig>(); 61 return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, KRES::SyncWidget>();
61 } 62 }
62} 63}
63 64
64ResourceQtopia::ResourceQtopia( const KConfig *config ) 65ResourceQtopia::ResourceQtopia( const KConfig *config, bool syncable )
65 : Resource( config ), mConverter (0) 66 : Resource( config, syncable ), mConverter (0)
66{ 67{
67 // we can not choose the filename. Therefore use the default to display 68 // we can not choose the filename. Therefore use the default to display
68 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; 69 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml";
69 70
70 init( fileName ); 71 init( fileName );
71} 72}
72 73
73ResourceQtopia::ResourceQtopia( const QString &fileName ) 74ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable )
74 : Resource( 0 ) 75 : Resource( 0, syncable )
75{ 76{
76// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 77// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
77 init( fileName ); 78 init( fileName );
78} 79}
79 80
80void ResourceQtopia::init( const QString &fileName ) 81void ResourceQtopia::init( const QString &fileName )
@@ -89,12 +90,14 @@ void ResourceQtopia::init( const QString &fileName )
89 90
90ResourceQtopia::~ResourceQtopia() 91ResourceQtopia::~ResourceQtopia()
91{ 92{
92 if (mConverter != 0) 93 if (mConverter != 0)
93 delete mConverter; 94 delete mConverter;
94 95
96 if(mAccess != 0)
97 delete mAccess;
95} 98}
96 99
97void ResourceQtopia::writeConfig( KConfig *config ) 100void ResourceQtopia::writeConfig( KConfig *config )
98{ 101{
99 Resource::writeConfig( config ); 102 Resource::writeConfig( config );
100} 103}
@@ -102,106 +105,88 @@ void ResourceQtopia::writeConfig( KConfig *config )
102Ticket *ResourceQtopia::requestSaveTicket() 105Ticket *ResourceQtopia::requestSaveTicket()
103{ 106{
104 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; 107 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl;
105 108
106 if ( !addressBook() ) return 0; 109 if ( !addressBook() ) return 0;
107 110
108 if ( !lock( mFileName ) ) { 111 if ( !lock( fileName() ) ) {
109 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" 112 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '"
110 << mFileName << "'" << endl; 113 << fileName() << "'" << endl;
111 return 0; 114 return 0;
112 } 115 }
113 return createTicket( this ); 116 return createTicket( this );
114} 117}
115 118
116 119
117bool ResourceQtopia::doOpen() 120bool ResourceQtopia::doOpen()
118{ 121{
119/*US 122 qDebug("ResourceQtopia::doOpen(): %s", fileName().latin1());
120 QFile file( mFileName );
121 123
122 if ( !file.exists() ) { 124 mAccess = new AddressBookAccess();
123 // try to create the file
124 bool ok = file.open( IO_WriteOnly );
125 if ( ok )
126 file.close();
127 125
128 return ok; 126 if ( !mAccess ) {
129 } else { 127 qDebug("Unable to load file() %s", fileName().latin1());
130 if ( !file.open( IO_ReadWrite ) )
131 return false;
132
133 if ( file.size() == 0 ) {
134 file.close();
135 return true;
136 }
137
138//US bool ok = mFormat->checkFormat( &file );
139 bool ok = true;
140
141 file.close();
142
143 return ok;
144 }
145*/
146 return true;
147}
148
149void ResourceQtopia::doClose()
150{
151}
152
153bool ResourceQtopia::load()
154{
155 kdDebug(5700) << "ResourceQtopia::load(): '" << mFileName << "'" << endl;
156
157// qDebug("ResourceQtopia::load: Try to load file() %s", mFileName.latin1());
158
159 AddressBookAccess* access = new AddressBookAccess();
160
161 if ( !access ) {
162 qDebug("Unable to load file() %s", mFileName.latin1());
163 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
164 return false; 128 return false;
165 } 129 }
166 130
167 bool res = false; 131 bool res = false;
168 if (mConverter == 0) 132 if (mConverter == 0)
169 { 133 {
170 mConverter = new QtopiaConverter(); 134 mConverter = new QtopiaConverter();
171 res = mConverter->init(); 135 res = mConverter->init();
172 if ( !res ) 136 if ( !res )
173 { 137 {
174 QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file"); 138 QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file");
175 qDebug(msg); 139 qDebug(msg);
176 addressBook()->error( i18n( msg ) ); 140 delete mAccess;
177 delete access; 141 mAccess = 0;
178 return false; 142 return false;
179 } 143 }
180 } 144 }
181 145
182 { //create a new scope 146 return true;
183 AddressBookIterator it(*access); 147}
148
149void ResourceQtopia::doClose()
150{
151 qDebug("ResourceQtopia::doClose: %s", fileName().latin1());
152
153 if(mAccess)
154 {
155 delete mAccess;
156 mAccess = 0;
157 }
158 // it seems so, that deletion of access deletes backend as well
159 //delete backend;
160
161 return;
162}
163
164bool ResourceQtopia::load()
165{
166 qDebug("ResourceQtopia::load: %s", fileName().latin1());
167
168 AddressBookIterator it(*mAccess);
184 const PimContact* contact; 169 const PimContact* contact;
170 bool res;
185 171
186 for (contact=it.toFirst(); it.current(); ++it) { 172 for (contact=it.toFirst(); it.current(); ++it)
173 {
187 contact = it.current(); 174 contact = it.current();
188 175
189 KABC::Addressee addressee; 176 KABC::Addressee addressee;
190 177
191 res = mConverter->qtopiaToAddressee( (*contact), addressee ); 178 res = mConverter->qtopiaToAddressee( (*contact), addressee );
192 179
193 if ( !addressee.isEmpty() && res ) 180 if ( !addressee.isEmpty() && res )
194 { 181 {
195 addressee.setResource( this ); 182 addressee.setResource( this );
196 addressBook()->insertAddressee( addressee ); 183 addressBook()->insertAddressee( addressee );
197 } 184 }
198 } 185 }
199 }
200 186
201 delete access;
202 return true; 187 return true;
203} 188}
204 189
205bool ResourceQtopia::save( Ticket *ticket ) 190bool ResourceQtopia::save( Ticket *ticket )
206{ 191{
207/*US 192/*US
@@ -246,13 +231,18 @@ bool ResourceQtopia::save( Ticket *ticket )
246 delete ticket; 231 delete ticket;
247 unlock( mFileName ); 232 unlock( mFileName );
248 233
249 return ok; 234 return ok;
250 235
251 qDebug("ResourceQtopia::save has to be changed"); 236 qDebug("ResourceQtopia::save has to be changed");
237
238
239 access->save();
240
252*/ 241*/
242
253 return true; 243 return true;
254} 244}
255 245
256bool ResourceQtopia::lock( const QString &fileName ) 246bool ResourceQtopia::lock( const QString &fileName )
257{ 247{
258 kdDebug(5700) << "ResourceQtopia::lock()" << endl; 248 kdDebug(5700) << "ResourceQtopia::lock()" << endl;
@@ -309,49 +299,85 @@ void ResourceQtopia::unlock( const QString &fileName )
309 299
310 QFile::remove( lockName ); 300 QFile::remove( lockName );
311 QFile::remove( mLockUniqueName ); 301 QFile::remove( mLockUniqueName );
312 addressBook()->emitAddressBookUnlocked(); 302 addressBook()->emitAddressBookUnlocked();
313} 303}
314 304
315void ResourceQtopia::setFileName( const QString &fileName ) 305void ResourceQtopia::setFileName( const QString &newFileName )
316{ 306{
317 mDirWatch.stopScan(); 307 mDirWatch.stopScan();
318 mDirWatch.removeFile( mFileName ); 308 mDirWatch.removeFile( fileName() );
319 309
320 mFileName = fileName; 310 Resource::setFileName( newFileName );
321 311
322 mDirWatch.addFile( mFileName ); 312 mDirWatch.addFile( fileName() );
323 mDirWatch.startScan(); 313 mDirWatch.startScan();
324
325//US simulate KDirWatch event
326//US fileChanged();
327} 314}
328 315
329QString ResourceQtopia::fileName() const
330{
331 return mFileName;
332}
333 316
334void ResourceQtopia::fileChanged() 317void ResourceQtopia::fileChanged()
335{ 318{
336 // There is a small theoretical chance that KDirWatch calls us before 319 // There is a small theoretical chance that KDirWatch calls us before
337 // we are fully constructed 320 // we are fully constructed
338 if (!addressBook()) 321 if (!addressBook())
339 return; 322 return;
340 323
341 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 324 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
342 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 325 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
343 load(); 326 load();
344 addressBook()->emitAddressBookChanged(); 327 addressBook()->emitAddressBookChanged();
345 } 328 }
346} 329}
347 330
348void ResourceQtopia::removeAddressee( const Addressee &addr ) 331void ResourceQtopia::removeAddressee( const Addressee &addr )
349{ 332{
350} 333}
351 334
352void ResourceQtopia::cleanUp() 335void ResourceQtopia::cleanUp()
353{ 336{
354 unlock( mFileName ); 337 unlock( fileName() );
338}
339
340
341
342/**
343 * This method returns the number of elements that are currently in the resource.
344 */
345int ResourceQtopia::count() const
346{
347 if (mAccess != 0)
348 {
349 int counter = 0;
350 AddressBookIterator it2(*mAccess);
351 for (it2.toFirst(); it2.current(); ++it2) {
352 counter++;
353 }
354
355 return counter;
356 }
357 else
358 return 0;
355} 359}
356 360
361
362/**
363 * This method removes all elements from the resource!! (Not from the addressbook)
364 */
365bool ResourceQtopia::clear()
366{
367 if (mAccess != 0)
368 {
369 AddressBookIterator it2(*mAccess);
370 for (it2.toFirst(); it2.current(); ++it2) {
371 mAccess->removeContact(*it2.current());
372 }
373 return true;
374 }
375 else
376 return false;
377}
378
379
380
381
382
357//US #include "resourceqtopia.moc" 383//US #include "resourceqtopia.moc"