summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/sharpdtm/resourcesharpdtm.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/sharpdtm/resourcesharpdtm.cpp225
1 files changed, 99 insertions, 126 deletions
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
index 7b909de..14f272d 100644
--- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
+++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
@@ -28,352 +28,325 @@ $Id$
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//US #include <qtimer.h> 36//US #include <qtimer.h>
37 37
38#include <kapplication.h> 38#include <kapplication.h>
39#include <kconfig.h> 39#include <kconfig.h>
40#include <kdebug.h> 40#include <kdebug.h>
41#include <klocale.h> 41#include <klocale.h>
42//US #include <ksavefile.h> 42//US #include <ksavefile.h>
43#include <kstandarddirs.h> 43#include <kstandarddirs.h>
44#include <kmessagebox.h> 44#include <kmessagebox.h>
45 45
46#include <sl/slzdb.h> 46#include <sl/slzdb.h>
47 47
48 48
49#include "resourcesharpdtmconfig.h" 49#include "resourcesharpdtmconfig.h"
50#include "resourcesharpdtm.h" 50#include "resourcesharpdtm.h"
51 51
52#include "syncprefwidget.h"
53
52#include "stdaddressbook.h" 54#include "stdaddressbook.h"
53 55
54#include "sharpdtmconverter.h" 56#include "sharpdtmconverter.h"
55 57
56using namespace KABC; 58using namespace KABC;
57extern "C" 59extern "C"
58{ 60{
59 void *init_microkabc_sharpdtm() 61 void *init_microkabc_sharpdtm()
60 { 62 {
61 return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>(); 63 return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig,SyncPrefWidget>();
62 } 64 }
63} 65}
64 66
65ResourceSharpDTM::ResourceSharpDTM( const KConfig *config, bool syncable ) 67ResourceSharpDTM::ResourceSharpDTM( const KConfig *config, bool syncable )
66 : Resource( config, syncable ), mConverter (0) 68 : Resource( config, syncable ), mConverter (0)
67{ 69{
68 // we can not choose the filename. Therefore use the default to display 70 // we can not choose the filename. Therefore use the default to display
69 71
70 QString fileName = SlZDataBase::addressbookFileName(); 72 QString fileName = SlZDataBase::addressbookFileName();
71 init( fileName ); 73 init( fileName );
72} 74}
73 75
74ResourceSharpDTM::ResourceSharpDTM( const QString &fileName, bool syncable ) 76ResourceSharpDTM::ResourceSharpDTM( const QString &fileName, bool syncable )
75 : Resource( 0, syncable ) 77 : Resource( 0, syncable )
76{ 78{
77// qDebug("ResourceFile::ResourceSharpDTM : 3 %s, %s", fileName.latin1(), formatName.latin1());
78 init( fileName ); 79 init( fileName );
79} 80}
80 81
81void ResourceSharpDTM::init( const QString &fileName ) 82void ResourceSharpDTM::init( const QString &fileName )
82{ 83{
83 84
84 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 85 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
85 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 86 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
86 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 87 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
87 88
88 setFileName( fileName ); 89 setFileName( fileName );
89} 90}
90 91
91ResourceSharpDTM::~ResourceSharpDTM() 92ResourceSharpDTM::~ResourceSharpDTM()
92{ 93{
93// if (mConverter != 0) 94 if (mConverter != 0)
94// delete mConverter; 95 delete mConverter;
95 96
97 if(mAccess != 0)
98 delete mAccess;
96} 99}
97 100
98void ResourceSharpDTM::writeConfig( KConfig *config ) 101void ResourceSharpDTM::writeConfig( KConfig *config )
99{ 102{
100 Resource::writeConfig( config ); 103 Resource::writeConfig( config );
101} 104}
102 105
103Ticket *ResourceSharpDTM::requestSaveTicket() 106Ticket *ResourceSharpDTM::requestSaveTicket()
104{ 107{
105 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; 108 kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket()" << endl;
109
110 qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1());
106 111
107 if ( !addressBook() ) return 0; 112 if ( !addressBook() ) return 0;
108 113
109 if ( !lock( mFileName ) ) { 114 if ( !lock( fileName() ) ) {
110 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" 115 kdDebug(5700) << "ResourceSharpDTM::requestSaveTicket(): Unable to lock file '"
111 << mFileName << "'" << endl; 116 << fileName() << "'" << endl;
112 return 0; 117 return 0;
113 } 118 }
114 return createTicket( this ); 119 return createTicket( this );
115} 120}
116 121
117 122
118bool ResourceSharpDTM::doOpen() 123bool ResourceSharpDTM::doOpen()
119{ 124{
120/*US 125 qDebug("ResourceSharpDTM::doOpen: %s", fileName().latin1());
121 QFile file( mFileName );
122
123 if ( !file.exists() ) {
124 // try to create the file
125 bool ok = file.open( IO_WriteOnly );
126 if ( ok )
127 file.close();
128
129 return ok;
130 } else {
131 if ( !file.open( IO_ReadWrite ) )
132 return false;
133
134 if ( file.size() == 0 ) {
135 file.close();
136 return true;
137 }
138
139//US bool ok = mFormat->checkFormat( &file );
140 bool ok = true;
141
142 file.close();
143
144 return ok;
145 }
146*/
147 return true;
148}
149
150void ResourceSharpDTM::doClose()
151{
152}
153
154bool ResourceSharpDTM::load()
155{
156 kdDebug(5700) << "ResourcSharpDTM::load(): '" << mFileName << "'" << endl;
157
158// qDebug("ResourceSharpDTM::load: Try to load file() %s", mFileName.latin1());
159 126
160 // the last parameter in the SlZDataBase constructor means "readonly" 127 // the last parameter in the SlZDataBase constructor means "readonly"
161 SlZDataBase* access = new SlZDataBase(mFileName, 128 mAccess = new SlZDataBase(fileName(),
162 SlZDataBase::addressbookItems(), 129 SlZDataBase::addressbookItems(),
163 NULL, true); 130 NULL, true);
164 if ( !access ) { 131
165 qDebug("Unable to load file() %s", mFileName.latin1()); 132 if ( !mAccess ) {
166 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) ); 133 qDebug("Unable to load file() %s", fileName().latin1());
167 return false; 134 return false;
168 } 135 }
169 136
170 bool res = false;
171 if (mConverter == 0) 137 if (mConverter == 0)
172 { 138 {
173 mConverter = new SharpDTMConverter(); 139 mConverter = new SharpDTMConverter();
174 res = mConverter->init(); 140 bool res = mConverter->init();
175 if ( !res ) 141 if ( !res )
176 { 142 {
177 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file"); 143 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file");
178 144
179 qDebug(msg); 145 qDebug(msg);
180 addressBook()->error( i18n( msg ) ); 146 delete mAccess;
181 delete access; 147 mAccess = 0;
182 return false; 148 return false;
183 } 149 }
184 } 150 }
185 151
186 { //create a new scope 152 return true;
187 CardId id; 153}
154
155void ResourceSharpDTM::doClose()
156{
157 qDebug("ResourceSharpDTM::doClose: %s", fileName().latin1());
188 158
189 for (bool res=access->first(); res == true; res=access->next()) { 159 if(mAccess)
190 id = access->cardId(); 160 {
161 delete mAccess;
162 mAccess = 0;
163 }
164 // it seems so, that deletion of access deletes backend as well
165 //delete backend;
191 166
192 KABC::Addressee addressee; 167 return;
168}
193 169
194 res = mConverter->sharpToAddressee( id, access, addressee ); 170bool ResourceSharpDTM::load()
171{
172 qDebug("ResourceSharpDTM::load: %s", fileName().latin1());
195 173
196 if ( !addressee.isEmpty() && res ) 174 bool res = false;
197 { 175
198 addressee.setResource( this ); 176 CardId id;
199 addressBook()->insertAddressee( addressee ); 177
200 } 178 for (bool res=mAccess->first(); res == true; res=mAccess->next())
179 {
180 id = mAccess->cardId();
181
182 KABC::Addressee addressee;
183
184 res = mConverter->sharpToAddressee( id, mAccess, addressee );
185
186 if ( !addressee.isEmpty() && res )
187 {
188 addressee.setResource( this );
189 addressBook()->insertAddressee( addressee );
201 } 190 }
202 } 191 }
203 192
204 delete access;
205 return true; 193 return true;
206} 194}
207 195
208bool ResourceSharpDTM::save( Ticket *ticket ) 196bool ResourceSharpDTM::save( Ticket *ticket )
209{ 197{
210/*US 198 qDebug("ResourceSharpDTM::save: %s", fileName().latin1());
211// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
212 kdDebug(5700) << "ResourceQtopia::save()" << endl;
213 199
214 // create backup file
215 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
216 200
217/*US we use a simpler method to create a backupfile 201 KABC::AddressBook::Iterator it;
202 bool res;
218 203
219 (void) KSaveFile::backupFile( mFileName, QString::null 204 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
220 ,extension ); 205 CardId id = 0;// 0 means new card
206 KABC::Addressee addressee = (*it);
221 207
222 KSaveFile saveFile( mFileName ); 208 res = mAccess->startEditCard(id);
223 bool ok = false; 209 if (res == true)
224 if ( saveFile.status() == 0 && saveFile.file() ) 210 {
225 { 211 res = mConverter->addresseeToSharp( *it, mAccess, id );
226 mFormat->saveAll( addressBook(), this, saveFile.file() ); 212 if (res == true)
227 ok = saveFile.close(); 213 {
228 } 214 res = mAccess->finishEditCard(&id);
229*/ 215 if (res == false)
230 216 qDebug("Unable to append Contact: %s", addressee.formattedName().latin1());
231/*US 217 }
232//US ToDo: write backupfile 218 else
233 QFile info; 219 {
234 info.setName( mFileName ); 220 qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1());
235 bool ok = info.open( IO_WriteOnly ); 221 mAccess->cancelEditCard();
236 if ( ok ) { 222 }
237//US mFormat->saveAll( addressBook(), this, &info ); 223 }
238 224 else
239 info.close(); 225 {
240 ok = true; 226 qDebug("Unable to add new card. Addressee: %s", addressee.formattedName().latin1());
241 } 227 }
242 else {
243 228
244 } 229 }
245 230
246 if ( !ok ) 231//US mAccess->save();
247 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
248 232
249 delete ticket; 233 delete ticket;
250 unlock( mFileName ); 234 unlock( fileName() );
251 235
252 return ok;
253
254 qDebug("ResourceQtopia::save has to be changed");
255*/
256 return true; 236 return true;
257} 237}
258 238
259bool ResourceSharpDTM::lock( const QString &fileName ) 239bool ResourceSharpDTM::lock( const QString &lockfileName )
260{ 240{
261 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; 241 qDebug("ResourceSharpDTM::lock: %s", fileName().latin1());
262 242
263 QString fn = fileName; 243 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl;
264 244
265//US change the implementation how the lockfilename is getting created 245 QString fn = lockfileName;
266//US fn.replace( QRegExp("/"), "_" );
267//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
268 246
269 KURL url(fn); 247 KURL url(fn);
270 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 248 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
271 249
272 kdDebug(5700) << "-- lock name: " << lockName << endl; 250 kdDebug(5700) << "-- lock name: " << lockName << endl;
273 251
274 if (QFile::exists( lockName )) return false; 252 if (QFile::exists( lockName )) return false;
275 253
276 QString lockUniqueName; 254 QString lockUniqueName;
277 lockUniqueName = fn + KApplication::randomString( 8 ); 255 lockUniqueName = fn + KApplication::randomString( 8 );
278 256
279 url = lockUniqueName; 257 url = lockUniqueName;
280//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 258//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
281 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 259 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
282 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 260 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
283 261
284 // Create unique file 262 // Create unique file
285 QFile file( mLockUniqueName ); 263 QFile file( mLockUniqueName );
286 file.open( IO_WriteOnly ); 264 file.open( IO_WriteOnly );
287 file.close(); 265 file.close();
288 266
289 // Create lock file 267 // Create lock file
290 int result = ::link( QFile::encodeName( mLockUniqueName ), 268 int result = 0;
269#ifndef _WIN32_
270 result = ::link( QFile::encodeName( mLockUniqueName ),
291 QFile::encodeName( lockName ) ); 271 QFile::encodeName( lockName ) );
292 272#endif
293 if ( result == 0 ) { 273 if ( result == 0 ) {
294 addressBook()->emitAddressBookLocked(); 274 addressBook()->emitAddressBookLocked();
295 return true; 275 return true;
296 } 276 }
297 277
298 // TODO: check stat 278 // TODO: check stat
299 279
300 return false; 280 return false;
301} 281}
302 282
303void ResourceSharpDTM::unlock( const QString &fileName ) 283void ResourceSharpDTM::unlock( const QString &fileName )
304{ 284{
285 qDebug("ResourceSharpDTM::unlock() %s", fileName.latin1());
286
305 QString fn = fileName; 287 QString fn = fileName;
306//US change the implementation how the lockfilename is getting created
307//US fn.replace( QRegExp( "/" ), "_" );
308//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
309//US QString lockName = fn + ".lock";
310 KURL url(fn); 288 KURL url(fn);
311 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 289 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
312 290
313 QFile::remove( lockName ); 291 QFile::remove( lockName );
314 QFile::remove( mLockUniqueName ); 292 QFile::remove( mLockUniqueName );
315 addressBook()->emitAddressBookUnlocked(); 293 addressBook()->emitAddressBookUnlocked();
316} 294}
317 295
318void ResourceSharpDTM::setFileName( const QString &fileName ) 296void ResourceSharpDTM::setFileName( const QString &newFileName )
319{ 297{
320 mDirWatch.stopScan(); 298 mDirWatch.stopScan();
321 mDirWatch.removeFile( mFileName ); 299 mDirWatch.removeFile( fileName() );
322 300
323 mFileName = fileName; 301 Resource::setFileName( newFileName );
324 302
325 mDirWatch.addFile( mFileName ); 303 mDirWatch.addFile( fileName() );
326 mDirWatch.startScan(); 304 mDirWatch.startScan();
327 305
328} 306}
329 307
330QString ResourceSharpDTM::fileName() const
331{
332 return mFileName;
333}
334
335void ResourceSharpDTM::fileChanged() 308void ResourceSharpDTM::fileChanged()
336{ 309{
337 // There is a small theoretical chance that KDirWatch calls us before 310 // There is a small theoretical chance that KDirWatch calls us before
338 // we are fully constructed 311 // we are fully constructed
339 if (!addressBook()) 312 if (!addressBook())
340 return; 313 return;
341 314
342 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 315 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
343 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 316 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
344 load(); 317 load();
345 addressBook()->emitAddressBookChanged(); 318 addressBook()->emitAddressBookChanged();
346 } 319 }
347} 320}
348 321
349void ResourceSharpDTM::removeAddressee( const Addressee &addr ) 322void ResourceSharpDTM::removeAddressee( const Addressee &addr )
350{ 323{
351} 324}
352 325
353void ResourceSharpDTM::cleanUp() 326void ResourceSharpDTM::cleanUp()
354{ 327{
355 unlock( mFileName ); 328 unlock( fileName() );
356} 329}
357 330
358/** 331/**
359 * This method returns the number of elements that are currently in the resource. 332 * This method returns the number of elements that are currently in the resource.
360 */ 333 */
361int ResourceSharpDTM::count() const 334int ResourceSharpDTM::count() const
362{ 335{
363 if (mAccess != 0) 336 if (mAccess != 0)
364 return mAccess->count(); 337 return mAccess->count();
365 else 338 else
366 return 0; 339 return 0;
367} 340}
368 341
369 342
370/** 343/**
371 * This method removes all elements from the resource!! (Not from the addressbook) 344 * This method removes all elements from the resource!! (Not from the addressbook)
372 */ 345 */
373bool ResourceSharpDTM::clear() 346bool ResourceSharpDTM::clear()
374{ 347{
375 if (mAccess != 0) 348 if (mAccess != 0)
376 return mAccess->deleteCard(0, SlZDataBase::AllCard ); 349 return mAccess->deleteCard(0, SlZDataBase::AllCard );
377 else 350 else
378 return false; 351 return false;
379} 352}