summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/sharpdtm/resourcesharpdtm.cpp90
1 files changed, 46 insertions, 44 deletions
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
index 347d51c..31b99ca 100644
--- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
+++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
@@ -26,69 +26,69 @@ $Id$
26*/ 26*/
27 27
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 "stdaddressbook.h" 50#include "resourcesharpdtm.h"
51 51
52//#include "qtopiaconverter.h" 52#include "stdaddressbook.h"
53 53
54#include "resourcesharpdtm.h" 54#include "sharpdtmconverter.h"
55 55
56using namespace KABC; 56using namespace KABC;
57extern "C" 57extern "C"
58{ 58{
59 void *init_microkabc_sharpdtm() 59 void *init_microkabc_sharpdtm()
60 { 60 {
61 return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>(); 61 return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>();
62 } 62 }
63} 63}
64 64
65ResourceSharpDTM::ResourceSharpDTM( const KConfig *config ) 65ResourceSharpDTM::ResourceSharpDTM( const KConfig *config )
66 : Resource( config ), mConverter (0) 66 : Resource( config ), mConverter (0)
67{ 67{
68 // 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
69 69
70 QString fileName = SlZDataBase::addressbookFileName(); 70 QString fileName = SlZDataBase::addressbookFileName();
71 init( fileName ); 71 init( fileName );
72} 72}
73 73
74ResourceSharpDTM::ResourceSharpDTM( const QString &fileName ) 74ResourceSharpDTM::ResourceSharpDTM( const QString &fileName )
75 : Resource( 0 ) 75 : Resource( 0 )
76{ 76{
77// qDebug("ResourceFile::ResourceSharpDTM : 3 %s, %s", fileName.latin1(), formatName.latin1()); 77// qDebug("ResourceFile::ResourceSharpDTM : 3 %s, %s", fileName.latin1(), formatName.latin1());
78 init( fileName ); 78 init( fileName );
79} 79}
80 80
81void ResourceSharpDTM::init( const QString &fileName ) 81void ResourceSharpDTM::init( const QString &fileName )
82{ 82{
83 83
84 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 84 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
85 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 85 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
86 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 86 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
87 87
88 setFileName( fileName ); 88 setFileName( fileName );
89} 89}
90 90
91ResourceSharpDTM::~ResourceSharpDTM() 91ResourceSharpDTM::~ResourceSharpDTM()
92{ 92{
93// if (mConverter != 0) 93// if (mConverter != 0)
94// delete mConverter; 94// delete mConverter;
@@ -117,239 +117,241 @@ Ticket *ResourceSharpDTM::requestSaveTicket()
117 117
118bool ResourceSharpDTM::doOpen() 118bool ResourceSharpDTM::doOpen()
119{ 119{
120/*US 120/*US
121 QFile file( mFileName ); 121 QFile file( mFileName );
122 122
123 if ( !file.exists() ) { 123 if ( !file.exists() ) {
124 // try to create the file 124 // try to create the file
125 bool ok = file.open( IO_WriteOnly ); 125 bool ok = file.open( IO_WriteOnly );
126 if ( ok ) 126 if ( ok )
127 file.close(); 127 file.close();
128 128
129 return ok; 129 return ok;
130 } else { 130 } else {
131 if ( !file.open( IO_ReadWrite ) ) 131 if ( !file.open( IO_ReadWrite ) )
132 return false; 132 return false;
133 133
134 if ( file.size() == 0 ) { 134 if ( file.size() == 0 ) {
135 file.close(); 135 file.close();
136 return true; 136 return true;
137 } 137 }
138 138
139//US bool ok = mFormat->checkFormat( &file ); 139//US bool ok = mFormat->checkFormat( &file );
140 bool ok = true; 140 bool ok = true;
141 141
142 file.close(); 142 file.close();
143 143
144 return ok; 144 return ok;
145 } 145 }
146*/ 146*/
147 return true; 147 return true;
148} 148}
149 149
150void ResourceSharpDTM::doClose() 150void ResourceSharpDTM::doClose()
151{ 151{
152} 152}
153 153
154bool ResourceSharpDTM::load() 154bool ResourceSharpDTM::load()
155{ 155{
156 kdDebug(5700) << "ResourcSharpDTM::load(): '" << mFileName << "'" << endl; 156 kdDebug(5700) << "ResourcSharpDTM::load(): '" << mFileName << "'" << endl;
157 157
158// qDebug("ResourceSharpDTM::load: Try to load file() %s", mFileName.latin1()); 158// qDebug("ResourceSharpDTM::load: Try to load file() %s", mFileName.latin1());
159 159
160 // the last parameter in the SlZDataBase constructor means "readonly"
160 SlZDataBase* access = new SlZDataBase(mFileName, 161 SlZDataBase* access = new SlZDataBase(mFileName,
161 SlZDataBase::addressbookItems()); 162 SlZDataBase::addressbookItems(),
163 NULL, true);
162 if ( !access ) { 164 if ( !access ) {
163 qDebug("Unable to load file() %s", mFileName.latin1()); 165 qDebug("Unable to load file() %s", mFileName.latin1());
164 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) ); 166 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
165 return false; 167 return false;
166 } 168 }
167 169
168 bool res = false; 170 bool res = false;
169 if (mConverter == 0) 171 if (mConverter == 0)
170 { 172 {
171// mConverter = new QtopiaConverter(); 173 mConverter = new SharpDTMConverter();
172// res = mConverter->init(); 174 res = mConverter->init();
173 res = true;
174 if ( !res ) 175 if ( !res )
175 { 176 {
176 qDebug("Unable to initialize qtopia converter. Most likely a problem with the category file"); 177 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file");
177 addressBook()->error( i18n( "Unable to initialize qtopia converter. Most likely a problem with the category file" ) ); 178
179 qDebug(msg);
180 addressBook()->error( i18n( msg ) );
178 delete access; 181 delete access;
179 return false; 182 return false;
180 } 183 }
181 } 184 }
182 /* 185
183 { //create a new scope 186 { //create a new scope
184 AddressBookIterator it(*access); 187 CardId id;
185 const PimContact* contact; 188
186 189 for (bool res=access->first(); res == true; res=access->next()) {
187 for (contact=it.toFirst(); it.current(); ++it) { 190 id = access->cardId();
188 contact = it.current(); 191
189
190 KABC::Addressee addressee; 192 KABC::Addressee addressee;
191 193
192 res = mConverter->qtopiaToAddressee( (*contact), addressee ); 194 res = mConverter->sharpToAddressee( id, access, addressee );
193 195
194 if ( !addressee.isEmpty() && res ) 196 if ( !addressee.isEmpty() && res )
195 { 197 {
196 addressee.setResource( this ); 198 addressee.setResource( this );
197 addressBook()->insertAddressee( addressee ); 199 addressBook()->insertAddressee( addressee );
198 } 200 }
199 } 201 }
200 } 202 }
201*/ 203
202 delete access; 204 delete access;
203 return true; 205 return true;
204} 206}
205 207
206bool ResourceSharpDTM::save( Ticket *ticket ) 208bool ResourceSharpDTM::save( Ticket *ticket )
207{ 209{
208/*US 210/*US
209// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 211// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
210 kdDebug(5700) << "ResourceQtopia::save()" << endl; 212 kdDebug(5700) << "ResourceQtopia::save()" << endl;
211 213
212 // create backup file 214 // create backup file
213 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 215 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
214 216
215/*US we use a simpler method to create a backupfile 217/*US we use a simpler method to create a backupfile
216 218
217 (void) KSaveFile::backupFile( mFileName, QString::null 219 (void) KSaveFile::backupFile( mFileName, QString::null
218 ,extension ); 220 ,extension );
219 221
220 KSaveFile saveFile( mFileName ); 222 KSaveFile saveFile( mFileName );
221 bool ok = false; 223 bool ok = false;
222 if ( saveFile.status() == 0 && saveFile.file() ) 224 if ( saveFile.status() == 0 && saveFile.file() )
223 { 225 {
224 mFormat->saveAll( addressBook(), this, saveFile.file() ); 226 mFormat->saveAll( addressBook(), this, saveFile.file() );
225 ok = saveFile.close(); 227 ok = saveFile.close();
226 } 228 }
227*/ 229*/
228 230
229/*US 231/*US
230//US ToDo: write backupfile 232//US ToDo: write backupfile
231 QFile info; 233 QFile info;
232 info.setName( mFileName ); 234 info.setName( mFileName );
233 bool ok = info.open( IO_WriteOnly ); 235 bool ok = info.open( IO_WriteOnly );
234 if ( ok ) { 236 if ( ok ) {
235//US mFormat->saveAll( addressBook(), this, &info ); 237//US mFormat->saveAll( addressBook(), this, &info );
236 238
237 info.close(); 239 info.close();
238 ok = true; 240 ok = true;
239 } 241 }
240 else { 242 else {
241 243
242 } 244 }
243 245
244 if ( !ok ) 246 if ( !ok )
245 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 247 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
246 248
247 delete ticket; 249 delete ticket;
248 unlock( mFileName ); 250 unlock( mFileName );
249 251
250 return ok; 252 return ok;
251 253
252 qDebug("ResourceQtopia::save has to be changed"); 254 qDebug("ResourceQtopia::save has to be changed");
253*/ 255*/
254 return true; 256 return true;
255} 257}
256 258
257bool ResourceSharpDTM::lock( const QString &fileName ) 259bool ResourceSharpDTM::lock( const QString &fileName )
258{ 260{
259 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; 261 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl;
260 262
261 QString fn = fileName; 263 QString fn = fileName;
262 264
263//US change the implementation how the lockfilename is getting created 265//US change the implementation how the lockfilename is getting created
264//US fn.replace( QRegExp("/"), "_" ); 266//US fn.replace( QRegExp("/"), "_" );
265//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 267//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
266 268
267 KURL url(fn); 269 KURL url(fn);
268 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 270 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
269 271
270 kdDebug(5700) << "-- lock name: " << lockName << endl; 272 kdDebug(5700) << "-- lock name: " << lockName << endl;
271 273
272 if (QFile::exists( lockName )) return false; 274 if (QFile::exists( lockName )) return false;
273 275
274 QString lockUniqueName; 276 QString lockUniqueName;
275 lockUniqueName = fn + KApplication::randomString( 8 ); 277 lockUniqueName = fn + KApplication::randomString( 8 );
276 278
277 url = lockUniqueName; 279 url = lockUniqueName;
278//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 280//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
279 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 281 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
280 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 282 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
281 283
282 // Create unique file 284 // Create unique file
283 QFile file( mLockUniqueName ); 285 QFile file( mLockUniqueName );
284 file.open( IO_WriteOnly ); 286 file.open( IO_WriteOnly );
285 file.close(); 287 file.close();
286 288
287 // Create lock file 289 // Create lock file
288 int result = ::link( QFile::encodeName( mLockUniqueName ), 290 int result = ::link( QFile::encodeName( mLockUniqueName ),
289 QFile::encodeName( lockName ) ); 291 QFile::encodeName( lockName ) );
290 292
291 if ( result == 0 ) { 293 if ( result == 0 ) {
292 addressBook()->emitAddressBookLocked(); 294 addressBook()->emitAddressBookLocked();
293 return true; 295 return true;
294 } 296 }
295 297
296 // TODO: check stat 298 // TODO: check stat
297 299
298 return false; 300 return false;
299} 301}
300 302
301void ResourceSharpDTM::unlock( const QString &fileName ) 303void ResourceSharpDTM::unlock( const QString &fileName )
302{ 304{
303 QString fn = fileName; 305 QString fn = fileName;
304//US change the implementation how the lockfilename is getting created 306//US change the implementation how the lockfilename is getting created
305//US fn.replace( QRegExp( "/" ), "_" ); 307//US fn.replace( QRegExp( "/" ), "_" );
306//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 308//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
307//US QString lockName = fn + ".lock"; 309//US QString lockName = fn + ".lock";
308 KURL url(fn); 310 KURL url(fn);
309 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 311 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
310 312
311 QFile::remove( lockName ); 313 QFile::remove( lockName );
312 QFile::remove( mLockUniqueName ); 314 QFile::remove( mLockUniqueName );
313 addressBook()->emitAddressBookUnlocked(); 315 addressBook()->emitAddressBookUnlocked();
314} 316}
315 317
316void ResourceSharpDTM::setFileName( const QString &fileName ) 318void ResourceSharpDTM::setFileName( const QString &fileName )
317{ 319{
318 mDirWatch.stopScan(); 320 mDirWatch.stopScan();
319 mDirWatch.removeFile( mFileName ); 321 mDirWatch.removeFile( mFileName );
320 322
321 mFileName = fileName; 323 mFileName = fileName;
322 324
323 mDirWatch.addFile( mFileName ); 325 mDirWatch.addFile( mFileName );
324 mDirWatch.startScan(); 326 mDirWatch.startScan();
325 327
326} 328}
327 329
328QString ResourceSharpDTM::fileName() const 330QString ResourceSharpDTM::fileName() const
329{ 331{
330 return mFileName; 332 return mFileName;
331} 333}
332 334
333void ResourceSharpDTM::fileChanged() 335void ResourceSharpDTM::fileChanged()
334{ 336{
335 // There is a small theoretical chance that KDirWatch calls us before 337 // There is a small theoretical chance that KDirWatch calls us before
336 // we are fully constructed 338 // we are fully constructed
337 if (!addressBook()) 339 if (!addressBook())
338 return; 340 return;
339 341
340 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 342 QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
341 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 343 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
342 load(); 344 load();
343 addressBook()->emitAddressBookChanged(); 345 addressBook()->emitAddressBookChanged();
344 } 346 }
345} 347}
346 348
347void ResourceSharpDTM::removeAddressee( const Addressee &addr ) 349void ResourceSharpDTM::removeAddressee( const Addressee &addr )
348{ 350{
349} 351}
350 352
351void ResourceSharpDTM::cleanUp() 353void ResourceSharpDTM::cleanUp()
352{ 354{
353 unlock( mFileName ); 355 unlock( mFileName );
354} 356}
355 357