summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/opie/resourceopie.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/opie/resourceopie.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/opie/resourceopie.cpp117
1 files changed, 83 insertions, 34 deletions
diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp
index 47e22b4..f992301 100644
--- a/kabc/plugins/opie/resourceopie.cpp
+++ b/kabc/plugins/opie/resourceopie.cpp
@@ -29,6 +29,7 @@ $Id$
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 <qfile.h> 33#include <qfile.h>
33#include <qfileinfo.h> 34#include <qfileinfo.h>
34#include <qregexp.h> 35#include <qregexp.h>
@@ -42,17 +43,21 @@ $Id$
42#include <kstandarddirs.h> 43#include <kstandarddirs.h>
43 44
44//US #include "formatfactory.h" 45//US #include "formatfactory.h"
46//US #include <qpe/qpeapplication.h>
47
48#include <opie/ocontactaccess.h>
45 49
46#include "resource.h" 50#include "resource.h"
47#include "resourceopieconfig.h" 51#include "resourceopieconfig.h"
48#include "stdaddressbook.h" 52#include "stdaddressbook.h"
49 53
54#include "opieconverter.h"
55
50#include "resourceopie.h" 56#include "resourceopie.h"
51 57
52using namespace KABC; 58using namespace KABC;
53extern "C" 59extern "C"
54{ 60{
55//US void *init_kabc_file()
56 void *init_microkabc_opie() 61 void *init_microkabc_opie()
57 { 62 {
58 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>(); 63 return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>();
@@ -60,26 +65,25 @@ extern "C"
60} 65}
61 66
62ResourceOpie::ResourceOpie( const KConfig *config ) 67ResourceOpie::ResourceOpie( const KConfig *config )
63 : Resource( config ) 68 : Resource( config ), mConverter (0)
64{ 69{
65 QString fileName; 70 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml";
66 71
67 KConfig *cfg = (KConfig *)config; 72 KConfig *cfg = (KConfig *)config;
68 if ( cfg ) { 73 if ( cfg ) {
69 fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() ); 74 fileName = cfg->readEntry( "FileName", fileName );
70// qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() ); 75
71 } else {
72 fileName = StdAddressBook::fileName();
73// qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() );
74 } 76 }
75 77
78// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() );
79
76 init( fileName ); 80 init( fileName );
77} 81}
78 82
79ResourceOpie::ResourceOpie( const QString &fileName ) 83ResourceOpie::ResourceOpie( const QString &fileName )
80 : Resource( 0 ) 84 : Resource( 0 )
81{ 85{
82// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 86// qDebug("ResourceOpie::ResourceOpie : 3 %s", fileName.latin1());
83 init( fileName ); 87 init( fileName );
84} 88}
85 89
@@ -91,16 +95,24 @@ void ResourceOpie::init( const QString &fileName )
91 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 95 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
92 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 96 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
93*/ 97*/
98 //US opie addressbook is always readonly
99 setReadOnly( true );
94 100
95 setFileName( fileName ); 101 setFileName( fileName );
102
96} 103}
97 104
98ResourceOpie::~ResourceOpie() 105ResourceOpie::~ResourceOpie()
99{ 106{
107 if (mConverter != 0)
108 delete mConverter;
100} 109}
101 110
102void ResourceOpie::writeConfig( KConfig *config ) 111void ResourceOpie::writeConfig( KConfig *config )
103{ 112{
113 //US opie addressbook is always readonly
114 setReadOnly( true );
115
104 Resource::writeConfig( config ); 116 Resource::writeConfig( config );
105 117
106 config->writeEntry( "FileName", mFileName ); 118 config->writeEntry( "FileName", mFileName );
@@ -126,6 +138,8 @@ Ticket *ResourceOpie::requestSaveTicket()
126 138
127bool ResourceOpie::doOpen() 139bool ResourceOpie::doOpen()
128{ 140{
141// qDebug("ResourceOpie::doOpen() %s", mFileName.latin1());
142/*US
129 QFile file( mFileName ); 143 QFile file( mFileName );
130 144
131 if ( !file.exists() ) { 145 if ( !file.exists() ) {
@@ -151,36 +165,71 @@ bool ResourceOpie::doOpen()
151 165
152 return ok; 166 return ok;
153 } 167 }
168*/
169 qDebug("ResourceOpie::doOpen() has to be fixed - %s", mFileName.latin1());
170 return true;
154} 171}
155 172
156void ResourceOpie::doClose() 173void ResourceOpie::doClose()
157{ 174{
175// qDebug("ResourceOpie::doClose() %s", mFileName.latin1());
158} 176}
159 177
160bool ResourceOpie::load() 178bool ResourceOpie::load()
161{ 179{
180// qDebug("ResourceOpie::load() %s", mFileName.latin1());
162 kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl; 181 kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl;
163 182
164 QFile file( mFileName ); 183 OContactAccess* access = new OContactAccess("KDEPim/Pi", mFileName, 0l, false);
165 if ( !file.open( IO_ReadOnly ) ) { 184
166 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); 185 if ( !access ) {
186 qDebug("Unable to load file() %s", mFileName.latin1());
187 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
167 return false; 188 return false;
168 } 189 }
190
191 access -> setReadAhead( 32 ); // Use ReadAhead-Cache if available
192
193 if (mConverter == 0)
194 mConverter = new OpieConverter();
169 195
170// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 196
197 OContactAccess::List::Iterator it;
198 OContactAccess::List allList = access->allRecords();
199 bool res = false;
200 for ( it = allList.begin(); it != allList.end(); ++it )
201 {
202 OContact c = (*it);
203
204 KABC::Addressee addressee;
205
206 res = mConverter->opieToAddressee( c, addressee );
207
208 if ( !addressee.isEmpty() && res )
209 {
210 addressee.setResource( this );
211 addressBook()->insertAddressee( addressee );
212 }
213
214// qDebug("found %s", c.fullName().latin1());
215 }
216
217 delete access;
171 218
172 //US return mFormat->loadAll( addressBook(), this, &file );
173 return true; 219 return true;
174} 220}
175 221
176bool ResourceOpie::save( Ticket *ticket ) 222bool ResourceOpie::save( Ticket *ticket )
177{ 223{
178// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 224 qDebug("ResourceOpie::save() has to be fixed - %s", mFileName.latin1());
225/*US
226
227 qDebug("ResourceOpie::save %s", mFileName.latin1());
179 kdDebug(5700) << "ResourceOpie::save()" << endl; 228 kdDebug(5700) << "ResourceOpie::save()" << endl;
180 229
181 // create backup file 230 // create backup file
182 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 231 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
183 232*/
184/*US we use a simpler method to create a backupfile 233/*US we use a simpler method to create a backupfile
185 234
186 (void) KSaveFile::backupFile( mFileName, QString::null 235 (void) KSaveFile::backupFile( mFileName, QString::null
@@ -194,7 +243,7 @@ bool ResourceOpie::save( Ticket *ticket )
194 ok = saveFile.close(); 243 ok = saveFile.close();
195 } 244 }
196*/ 245*/
197 246/*US
198//US ToDo: write backupfile 247//US ToDo: write backupfile
199 QFile info; 248 QFile info;
200 info.setName( mFileName ); 249 info.setName( mFileName );
@@ -218,11 +267,17 @@ bool ResourceOpie::save( Ticket *ticket )
218 return ok; 267 return ok;
219 268
220 qDebug("ResourceOpie::save has to be changed"); 269 qDebug("ResourceOpie::save has to be changed");
270*/
221 return true; 271 return true;
272
222} 273}
223 274
224bool ResourceOpie::lock( const QString &fileName ) 275bool ResourceOpie::lock( const QString &fileName )
225{ 276{
277 qDebug("ResourceOpie::lock() has to be fixed - %s", mFileName.latin1());
278
279/*US
280// qDebug("ResourceOpie::lock() %s", fileName.latin1());
226 kdDebug(5700) << "ResourceOpie::lock()" << endl; 281 kdDebug(5700) << "ResourceOpie::lock()" << endl;
227 282
228 QString fn = fileName; 283 QString fn = fileName;
@@ -263,10 +318,17 @@ bool ResourceOpie::lock( const QString &fileName )
263 // TODO: check stat 318 // TODO: check stat
264 319
265 return false; 320 return false;
321*/
322
323 return true;
266} 324}
267 325
268void ResourceOpie::unlock( const QString &fileName ) 326void ResourceOpie::unlock( const QString &fileName )
269{ 327{
328 qDebug("ResourceOpie::unlock() has to be fixed - %s", mFileName.latin1());
329/*US
330// qDebug("ResourceOpie::unlock() %s", fileName.latin1());
331
270 QString fn = fileName; 332 QString fn = fileName;
271//US change the implementation how the lockfilename is getting created 333//US change the implementation how the lockfilename is getting created
272//US fn.replace( QRegExp( "/" ), "_" ); 334//US fn.replace( QRegExp( "/" ), "_" );
@@ -278,6 +340,7 @@ void ResourceOpie::unlock( const QString &fileName )
278 QFile::remove( lockName ); 340 QFile::remove( lockName );
279 QFile::remove( mLockUniqueName ); 341 QFile::remove( mLockUniqueName );
280 addressBook()->emitAddressBookUnlocked(); 342 addressBook()->emitAddressBookUnlocked();
343*/
281} 344}
282 345
283void ResourceOpie::setFileName( const QString &fileName ) 346void ResourceOpie::setFileName( const QString &fileName )
@@ -302,22 +365,6 @@ QString ResourceOpie::fileName() const
302 return mFileName; 365 return mFileName;
303} 366}
304 367
305/*US
306void ResourceOpie::setFormat( const QString &format )
307{
308 mFormatName = format;
309 delete mFormat;
310
311 FormatFactory *factory = FormatFactory::self();
312 mFormat = factory->format( mFormatName );
313}
314
315QString ResourceOpie::format() const
316{
317 return mFormatName;
318}
319*/
320
321void ResourceOpie::fileChanged() 368void ResourceOpie::fileChanged()
322{ 369{
323 // There is a small theoretical chance that KDirWatch calls us before 370 // There is a small theoretical chance that KDirWatch calls us before
@@ -339,6 +386,8 @@ void ResourceOpie::removeAddressee( const Addressee &addr )
339 386
340void ResourceOpie::cleanUp() 387void ResourceOpie::cleanUp()
341{ 388{
389// qDebug("ResourceOpie::cleanup() %s", mFileName.latin1());
390
342 unlock( mFileName ); 391 unlock( mFileName );
343} 392}
344 393