summaryrefslogtreecommitdiffabout
path: root/kabc
authorulf69 <ulf69>2004-08-02 18:32:08 (UTC)
committer ulf69 <ulf69>2004-08-02 18:32:08 (UTC)
commit863c4c3678e59ef125c08c00e9532ded5b540f67 (patch) (unidiff)
tree449428c45689ffe10ff1ce4caa18f959c4dfe402 /kabc
parent983127ce46c9ff6663d08dedd450da3fe7dd549b (diff)
downloadkdepimpi-863c4c3678e59ef125c08c00e9532ded5b540f67.zip
kdepimpi-863c4c3678e59ef125c08c00e9532ded5b540f67.tar.gz
kdepimpi-863c4c3678e59ef125c08c00e9532ded5b540f67.tar.bz2
added support for syncprofiles for resources and export/import capabilities
Diffstat (limited to 'kabc') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/dir/dir.pro2
-rw-r--r--kabc/plugins/dir/dirE.pro2
-rw-r--r--kabc/plugins/dir/resourcedir.cpp35
-rw-r--r--kabc/plugins/dir/resourcedir.h6
-rw-r--r--kabc/plugins/file/file.pro2
-rw-r--r--kabc/plugins/file/fileE.pro2
-rw-r--r--kabc/plugins/file/resourcefile.cpp74
-rw-r--r--kabc/plugins/file/resourcefile.h8
-rw-r--r--kabc/plugins/ldap/ldapE.pro2
-rw-r--r--kabc/plugins/ldap/resourceldap.cpp7
-rw-r--r--kabc/plugins/ldap/resourceldap.h2
-rw-r--r--kabc/plugins/opie/opieE.pro2
-rw-r--r--kabc/plugins/opie/resourceopie.cpp302
-rw-r--r--kabc/plugins/opie/resourceopie.h40
-rw-r--r--kabc/plugins/qtopia/qtopiaE.pro2
-rw-r--r--kabc/plugins/qtopia/resourceqtopia.cpp182
-rw-r--r--kabc/plugins/qtopia/resourceqtopia.h35
-rw-r--r--kabc/plugins/sharpdtm/resourcesharpdtm.cpp35
-rw-r--r--kabc/plugins/sharpdtm/resourcesharpdtm.h32
-rw-r--r--kabc/plugins/sharpdtm/sharpdtmE.pro2
20 files changed, 419 insertions, 355 deletions
diff --git a/kabc/plugins/dir/dir.pro b/kabc/plugins/dir/dir.pro
index 0555484..a7c1503 100644
--- a/kabc/plugins/dir/dir.pro
+++ b/kabc/plugins/dir/dir.pro
@@ -1,34 +1,34 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on release 2CONFIG += qt warn_on release
3#release debug 3#release debug
4 4
5TARGET = microkabc_dir 5TARGET = microkabc_dir
6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../qtcompat 6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../microkde/kresources ../../../qtcompat
7DESTDIR = ../../../bin 7DESTDIR = ../../../bin
8#LIBS += -lmicrokde -lmicrokabc 8#LIBS += -lmicrokde -lmicrokabc
9#LIBS += -L$(QPEDIR)/lib 9#LIBS += -L$(QPEDIR)/lib
10 10
11INTERFACES = \ 11INTERFACES = \
12 12
13HEADERS = \ 13HEADERS = \
14 resourcedir.h \ 14 resourcedir.h \
15 resourcedirconfig.h 15 resourcedirconfig.h
16 16
17SOURCES = \ 17SOURCES = \
18 resourcedir.cpp \ 18 resourcedir.cpp \
19 resourcedirconfig.cpp 19 resourcedirconfig.cpp
20 20
21unix : { 21unix : {
22OBJECTS_DIR = obj/unix 22OBJECTS_DIR = obj/unix
23MOC_DIR = moc/unix 23MOC_DIR = moc/unix
24} 24}
25win32: { 25win32: {
26CONFIG += dll 26CONFIG += dll
27DEFINES += _WIN32_ 27DEFINES += _WIN32_
28OBJECTS_DIR = obj/win 28OBJECTS_DIR = obj/win
29MOC_DIR = moc/win 29MOC_DIR = moc/win
30LIBS += ../../../bin/microkdepim.lib 30LIBS += ../../../bin/microkdepim.lib
31LIBS += ../../../bin/microkcal.lib 31LIBS += ../../../bin/microkcal.lib
32LIBS += ../../../bin/microkde.lib 32LIBS += ../../../bin/microkde.lib
33LIBS += ../../../bin/microkabc.lib 33LIBS += ../../../bin/microkabc.lib
34} 34}
diff --git a/kabc/plugins/dir/dirE.pro b/kabc/plugins/dir/dirE.pro
index 729f4ce..9066556 100644
--- a/kabc/plugins/dir/dirE.pro
+++ b/kabc/plugins/dir/dirE.pro
@@ -1,21 +1,21 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on release 2CONFIG += qt warn_on release
3#release debug 3#release debug
4 4
5TARGET = microkabc_dir 5TARGET = microkabc_dir
6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../qtcompat 6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../microkde/kresources ../../../qtcompat
7OBJECTS_DIR = obj/$(PLATFORM) 7OBJECTS_DIR = obj/$(PLATFORM)
8MOC_DIR = moc/$(PLATFORM) 8MOC_DIR = moc/$(PLATFORM)
9DESTDIR = $(QPEDIR)/lib 9DESTDIR = $(QPEDIR)/lib
10LIBS += -lmicrokde -lmicrokabc 10LIBS += -lmicrokde -lmicrokabc
11LIBS += -L$(QPEDIR)/lib 11LIBS += -L$(QPEDIR)/lib
12 12
13INTERFACES = \ 13INTERFACES = \
14 14
15HEADERS = \ 15HEADERS = \
16 resourcedir.h \ 16 resourcedir.h \
17 resourcedirconfig.h 17 resourcedirconfig.h
18 18
19SOURCES = \ 19SOURCES = \
20 resourcedir.cpp \ 20 resourcedir.cpp \
21 resourcedirconfig.cpp 21 resourcedirconfig.cpp
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp
index 52863d5..d978e61 100644
--- a/kabc/plugins/dir/resourcedir.cpp
+++ b/kabc/plugins/dir/resourcedir.cpp
@@ -1,364 +1,365 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28 28
29#include <sys/types.h> 29#include <sys/types.h>
30#include <sys/stat.h> 30#include <sys/stat.h>
31#ifndef _WIN32_ 31#ifndef _WIN32_
32#include <unistd.h> 32#include <unistd.h>
33#endif 33#endif
34 34
35#include <qregexp.h> 35#include <qregexp.h>
36#include <qtimer.h> 36#include <qtimer.h>
37#include <qwidget.h> 37#include <qwidget.h>
38 38
39#include <kapplication.h> 39#include <kapplication.h>
40#include <kconfig.h> 40#include <kconfig.h>
41#include <kdebug.h> 41#include <kdebug.h>
42//US #include <kgenericfactory.h> 42//US #include <kgenericfactory.h>
43#include <kglobal.h> 43#include <kglobal.h>
44#include <klocale.h> 44#include <klocale.h>
45#include <kstandarddirs.h> 45#include <kstandarddirs.h>
46#include <kurlrequester.h> 46#include <kurlrequester.h>
47#include <kmessagebox.h> 47#include <kmessagebox.h>
48 48
49#include "addressbook.h" 49#include "addressbook.h"
50 50
51#include "formatfactory.h" 51#include "formatfactory.h"
52 52
53#include "resourcedirconfig.h" 53#include "resourcedirconfig.h"
54#include "stdaddressbook.h" 54#include "stdaddressbook.h"
55 55
56//US 56//US
57#include <qdir.h> 57#include <qdir.h>
58 58
59#include "resourcedir.h" 59#include "resourcedir.h"
60#include "syncwidget.h"
60 61
61using namespace KABC; 62using namespace KABC;
62 63
63extern "C" 64extern "C"
64#ifdef _WIN32_ 65#ifdef _WIN32_
65__declspec(dllexport) 66__declspec(dllexport)
66#else 67#else
67{ 68{
68#endif 69#endif
69 70
70//US void *init_kabc_dir() 71//US void *init_kabc_dir()
71 void *init_microkabc_dir() 72 void *init_microkabc_dir()
72 { 73 {
73 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig>(); 74 return new KRES::PluginFactory<ResourceDir,ResourceDirConfig, KRES::SyncWidget>();
74 } 75 }
75#ifndef _WIN32_ 76#ifndef _WIN32_
76} 77}
77#endif 78#endif
78 79
79ResourceDir::ResourceDir( const KConfig *config ) 80ResourceDir::ResourceDir( const KConfig *config, bool syncable )
80 : Resource( config ) 81 : Resource( config, syncable )
81{ 82{
82 QString path; 83 QString path;
83 84
84 KConfig *cfg = (KConfig *)config; 85 KConfig *cfg = (KConfig *)config;
85 if ( cfg ) { 86 if ( cfg ) {
86//US path = config->readEntry( "FilePath" ); 87//US path = config->readEntry( "FilePath" );
87 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() ); 88 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() );
88//US mFormatName = config->readEntry( "FileFormat" ); 89//US mFormatName = config->readEntry( "FileFormat" );
89 mFormatName = cfg->readEntry( "FileFormat", "vcard" ); 90 mFormatName = cfg->readEntry( "FileFormat", "vcard" );
90 } else { 91 } else {
91 path = StdAddressBook::directoryName(); 92 path = StdAddressBook::directoryName();
92 mFormatName = "vcard"; 93 mFormatName = "vcard";
93 } 94 }
94 95
95 96
96 FormatFactory *factory = FormatFactory::self(); 97 FormatFactory *factory = FormatFactory::self();
97 mFormat = factory->format( mFormatName ); 98 mFormat = factory->format( mFormatName );
98 99
99 if ( !mFormat ) { 100 if ( !mFormat ) {
100 mFormatName = "vcard"; 101 mFormatName = "vcard";
101 mFormat = factory->format( mFormatName ); 102 mFormat = factory->format( mFormatName );
102 } 103 }
103 104
104/*US 105/*US
105//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1()); 106//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1());
106 if (mFormatName == "vcard") 107 if (mFormatName == "vcard")
107 mFormat = new VCardFormatPlugin2(); 108 mFormat = new VCardFormatPlugin2();
108 else if (mFormatName == "binary") 109 else if (mFormatName == "binary")
109 mFormat = new BinaryFormat(); 110 mFormat = new BinaryFormat();
110 else 111 else
111 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1()); 112 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1());
112*/ 113*/
113 114
114 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) ); 115 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) );
115 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) ); 116 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) );
116 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) ); 117 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) );
117 118
118 setPath( path ); 119 setPath( path );
119} 120}
120 121
121ResourceDir::~ResourceDir() 122ResourceDir::~ResourceDir()
122{ 123{
123 delete mFormat; 124 delete mFormat;
124 mFormat = 0; 125 mFormat = 0;
125} 126}
126 127
127void ResourceDir::writeConfig( KConfig *config ) 128void ResourceDir::writeConfig( KConfig *config )
128{ 129{
129 config->setGroup( "Resource_" + identifier() ); 130 config->setGroup( "Resource_" + identifier() );
130 Resource::writeConfig( config ); 131 Resource::writeConfig( config );
131 132
132 config->writeEntry( "FilePath", mPath ); 133 config->writeEntry( "FilePath", mPath );
133 config->writeEntry( "FileFormat", mFormatName ); 134 config->writeEntry( "FileFormat", mFormatName );
134} 135}
135 136
136Ticket *ResourceDir::requestSaveTicket() 137Ticket *ResourceDir::requestSaveTicket()
137{ 138{
138 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl; 139 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl;
139 140
140 if ( !addressBook() ) return 0; 141 if ( !addressBook() ) return 0;
141 142
142 if ( !lock( mPath ) ) { 143 if ( !lock( mPath ) ) {
143 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '" 144 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '"
144 << mPath << "'" << endl; 145 << mPath << "'" << endl;
145 return 0; 146 return 0;
146 } 147 }
147 return createTicket( this ); 148 return createTicket( this );
148} 149}
149 150
150 151
151bool ResourceDir::doOpen() 152bool ResourceDir::doOpen()
152{ 153{
153 QDir dir( mPath ); 154 QDir dir( mPath );
154 if ( !dir.exists() ) { // no directory available 155 if ( !dir.exists() ) { // no directory available
155 return dir.mkdir( dir.path() ); 156 return dir.mkdir( dir.path() );
156 } else { 157 } else {
157 QString testName = dir.entryList( QDir::Files )[0]; 158 QString testName = dir.entryList( QDir::Files )[0];
158 if ( testName.isNull() || testName.isEmpty() ) // no file in directory 159 if ( testName.isNull() || testName.isEmpty() ) // no file in directory
159 return true; 160 return true;
160 161
161 QFile file( mPath + "/" + testName ); 162 QFile file( mPath + "/" + testName );
162 if ( file.open( IO_ReadOnly ) ) 163 if ( file.open( IO_ReadOnly ) )
163 return true; 164 return true;
164 165
165 if ( file.size() == 0 ) 166 if ( file.size() == 0 )
166 return true; 167 return true;
167 168
168 bool ok = mFormat->checkFormat( &file ); 169 bool ok = mFormat->checkFormat( &file );
169 file.close(); 170 file.close();
170 return ok; 171 return ok;
171 } 172 }
172} 173}
173 174
174void ResourceDir::doClose() 175void ResourceDir::doClose()
175{ 176{
176} 177}
177 178
178bool ResourceDir::load() 179bool ResourceDir::load()
179{ 180{
180 kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl; 181 kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl;
181 182
182 QDir dir( mPath ); 183 QDir dir( mPath );
183 QStringList files = dir.entryList( QDir::Files ); 184 QStringList files = dir.entryList( QDir::Files );
184 185
185 QStringList::Iterator it; 186 QStringList::Iterator it;
186 bool ok = true; 187 bool ok = true;
187 for ( it = files.begin(); it != files.end(); ++it ) { 188 for ( it = files.begin(); it != files.end(); ++it ) {
188 QFile file( mPath + "/" + (*it) ); 189 QFile file( mPath + "/" + (*it) );
189 190
190 if ( !file.open( IO_ReadOnly ) ) { 191 if ( !file.open( IO_ReadOnly ) ) {
191 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) ); 192 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) );
192 ok = false; 193 ok = false;
193 continue; 194 continue;
194 } 195 }
195 196
196 if ( !mFormat->loadAll( addressBook(), this, &file ) ) 197 if ( !mFormat->loadAll( addressBook(), this, &file ) )
197 ok = false; 198 ok = false;
198 199
199 file.close(); 200 file.close();
200 } 201 }
201 202
202 return ok; 203 return ok;
203} 204}
204 205
205bool ResourceDir::save( Ticket *ticket ) 206bool ResourceDir::save( Ticket *ticket )
206{ 207{
207 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl; 208 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl;
208 209
209 AddressBook::Iterator it; 210 AddressBook::Iterator it;
210 bool ok = true; 211 bool ok = true;
211 212
212 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 213 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
213 if ( (*it).resource() != this || !(*it).changed() ) 214 if ( (*it).resource() != this || !(*it).changed() )
214 continue; 215 continue;
215 216
216 QFile file( mPath + "/" + (*it).uid() ); 217 QFile file( mPath + "/" + (*it).uid() );
217 if ( !file.open( IO_WriteOnly ) ) { 218 if ( !file.open( IO_WriteOnly ) ) {
218 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) ); 219 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) );
219 continue; 220 continue;
220 } 221 }
221 222
222 mFormat->save( *it, &file ); 223 mFormat->save( *it, &file );
223 224
224 // mark as unchanged 225 // mark as unchanged
225 (*it).setChanged( false ); 226 (*it).setChanged( false );
226 227
227 file.close(); 228 file.close();
228 } 229 }
229 230
230 delete ticket; 231 delete ticket;
231 unlock( mPath ); 232 unlock( mPath );
232 233
233 return ok; 234 return ok;
234} 235}
235 236
236bool ResourceDir::lock( const QString &path ) 237bool ResourceDir::lock( const QString &path )
237{ 238{
238 kdDebug(5700) << "ResourceDir::lock()" << endl; 239 kdDebug(5700) << "ResourceDir::lock()" << endl;
239 240
240 QString p = path; 241 QString p = path;
241//US change the implementation how the lockfilename is getting created 242//US change the implementation how the lockfilename is getting created
242//US p.replace( QRegExp("/"), "_" ); 243//US p.replace( QRegExp("/"), "_" );
243//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" ); 244//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" );
244 KURL url(p); 245 KURL url(p);
245 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 246 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
246 247
247 248
248 kdDebug(5700) << "-- lock name: " << lockName << endl; 249 kdDebug(5700) << "-- lock name: " << lockName << endl;
249 250
250 if ( QFile::exists( lockName ) ) return false; 251 if ( QFile::exists( lockName ) ) return false;
251 252
252 QString lockUniqueName; 253 QString lockUniqueName;
253 lockUniqueName = p + KApplication::randomString( 8 ); 254 lockUniqueName = p + KApplication::randomString( 8 );
254 255
255 url = lockUniqueName; 256 url = lockUniqueName;
256//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 257//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
257 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 258 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
258 259
259 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 260 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
260 261
261 // Create unique file 262 // Create unique file
262 QFile file( mLockUniqueName ); 263 QFile file( mLockUniqueName );
263 file.open( IO_WriteOnly ); 264 file.open( IO_WriteOnly );
264 file.close(); 265 file.close();
265 266
266 // Create lock file 267 // Create lock file
267 int result = 0; 268 int result = 0;
268#ifndef _WIN32_ 269#ifndef _WIN32_
269 result = ::link( QFile::encodeName( mLockUniqueName ), 270 result = ::link( QFile::encodeName( mLockUniqueName ),
270 QFile::encodeName( lockName ) ); 271 QFile::encodeName( lockName ) );
271#endif 272#endif
272 if ( result == 0 ) { 273 if ( result == 0 ) {
273 addressBook()->emitAddressBookLocked(); 274 addressBook()->emitAddressBookLocked();
274 return true; 275 return true;
275 } 276 }
276 277
277 // TODO: check stat 278 // TODO: check stat
278 279
279 return false; 280 return false;
280} 281}
281 282
282void ResourceDir::unlock( const QString &path ) 283void ResourceDir::unlock( const QString &path )
283{ 284{
284 QString p = path; 285 QString p = path;
285//US change the implementation how the lockfilename is getting created 286//US change the implementation how the lockfilename is getting created
286//US p.replace( QRegExp( "/" ), "_" ); 287//US p.replace( QRegExp( "/" ), "_" );
287//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" ); 288//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" );
288 KURL url(p); 289 KURL url(p);
289 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" ); 290 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" );
290 291
291 ::unlink( QFile::encodeName( lockName ) ); 292 ::unlink( QFile::encodeName( lockName ) );
292 QFile::remove( mLockUniqueName ); 293 QFile::remove( mLockUniqueName );
293 addressBook()->emitAddressBookUnlocked(); 294 addressBook()->emitAddressBookUnlocked();
294} 295}
295 296
296void ResourceDir::setPath( const QString &path ) 297void ResourceDir::setPath( const QString &path )
297{ 298{
298 mDirWatch.stopScan(); 299 mDirWatch.stopScan();
299 mDirWatch.removeDir( mPath ); 300 mDirWatch.removeDir( mPath );
300 301
301 mPath = path; 302 mPath = path;
302 303
303 mDirWatch.addDir( mPath, true ); 304 mDirWatch.addDir( mPath, true );
304 mDirWatch.startScan(); 305 mDirWatch.startScan();
305 306
306//US simulate KDirWatch event 307//US simulate KDirWatch event
307//US pathChanged(); 308//US pathChanged();
308 309
309} 310}
310 311
311QString ResourceDir::path() const 312QString ResourceDir::path() const
312{ 313{
313 return mPath; 314 return mPath;
314} 315}
315 316
316void ResourceDir::setFormat( const QString &format ) 317void ResourceDir::setFormat( const QString &format )
317{ 318{
318 mFormatName = format; 319 mFormatName = format;
319 320
320 if ( mFormat ) 321 if ( mFormat )
321 delete mFormat; 322 delete mFormat;
322 323
323 FormatFactory *factory = FormatFactory::self(); 324 FormatFactory *factory = FormatFactory::self();
324 mFormat = factory->format( mFormatName ); 325 mFormat = factory->format( mFormatName );
325/*US 326/*US
326qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1()); 327qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1());
327 if (mFormatName == "vcard") 328 if (mFormatName == "vcard")
328 mFormat = new VCardFormatPlugin2(); 329 mFormat = new VCardFormatPlugin2();
329 else if (mFormatName == "binary") 330 else if (mFormatName == "binary")
330 mFormat = new BinaryFormat(); 331 mFormat = new BinaryFormat();
331 else 332 else
332 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1()); 333 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1());
333*/ 334*/
334 335
335} 336}
336 337
337QString ResourceDir::format() const 338QString ResourceDir::format() const
338{ 339{
339 return mFormatName; 340 return mFormatName;
340} 341}
341 342
342void ResourceDir::pathChanged() 343void ResourceDir::pathChanged()
343{ 344{
344 if ( !addressBook() ) 345 if ( !addressBook() )
345 return; 346 return;
346 347
347 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) ); 348 QString text( i18n( "Dir resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mPath ) );
348 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 349 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
349 load(); 350 load();
350 addressBook()->emitAddressBookChanged(); 351 addressBook()->emitAddressBookChanged();
351 } 352 }
352} 353}
353 354
354void ResourceDir::removeAddressee( const Addressee& addr ) 355void ResourceDir::removeAddressee( const Addressee& addr )
355{ 356{
356 QFile::remove( mPath + "/" + addr.uid() ); 357 QFile::remove( mPath + "/" + addr.uid() );
357} 358}
358 359
359void ResourceDir::cleanUp() 360void ResourceDir::cleanUp()
360{ 361{
361 unlock( mPath ); 362 unlock( mPath );
362} 363}
363 364
364//US #include "resourcedir.moc" 365//US #include "resourcedir.moc"
diff --git a/kabc/plugins/dir/resourcedir.h b/kabc/plugins/dir/resourcedir.h
index 6e35695..c2aedad 100644
--- a/kabc/plugins/dir/resourcedir.h
+++ b/kabc/plugins/dir/resourcedir.h
@@ -1,115 +1,115 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28#ifndef KABC_RESOURCEDIR_H 28#ifndef KABC_RESOURCEDIR_H
29#define KABC_RESOURCEDIR_H 29#define KABC_RESOURCEDIR_H
30 30
31#include <kconfig.h> 31#include <kconfig.h>
32#include <kdirwatch.h> 32#include <kdirwatch.h>
33 33
34#include <sys/types.h> 34#include <sys/types.h>
35 35
36#include "resource.h" 36#include "resource.h"
37 37
38class QTimer; 38class QTimer;
39 39
40namespace KABC { 40namespace KABC {
41 41
42class FormatPlugin; 42class FormatPlugin;
43 43
44/** 44/**
45 @internal 45 @internal
46*/ 46*/
47class ResourceDir : public Resource 47class ResourceDir : public Resource
48{ 48{
49 Q_OBJECT 49 Q_OBJECT
50 50
51public: 51public:
52 ResourceDir( const KConfig* ); 52 ResourceDir( const KConfig*, bool syncable );
53 ~ResourceDir(); 53 ~ResourceDir();
54 54
55 virtual void writeConfig( KConfig* ); 55 virtual void writeConfig( KConfig* );
56 56
57 virtual bool doOpen(); 57 virtual bool doOpen();
58 virtual void doClose(); 58 virtual void doClose();
59 59
60 virtual Ticket *requestSaveTicket(); 60 virtual Ticket *requestSaveTicket();
61 61
62 virtual bool load(); 62 virtual bool load();
63 virtual bool save( Ticket * ); 63 virtual bool save( Ticket * );
64 64
65 /** 65 /**
66 * Set path to be used for saving. 66 * Set path to be used for saving.
67 */ 67 */
68 void setPath( const QString & ); 68 void setPath( const QString & );
69 69
70 /** 70 /**
71 * Return path used for loading and saving the address book. 71 * Return path used for loading and saving the address book.
72 */ 72 */
73 QString path() const; 73 QString path() const;
74 74
75 /** 75 /**
76 * Set the format by name. 76 * Set the format by name.
77 */ 77 */
78 void setFormat( const QString &format ); 78 void setFormat( const QString &format );
79 79
80 /** 80 /**
81 * Returns the format name. 81 * Returns the format name.
82 */ 82 */
83 QString format() const; 83 QString format() const;
84 84
85 /** 85 /**
86 * Remove a addressee from its source. 86 * Remove a addressee from its source.
87 * This method is mainly called by KABC::AddressBook. 87 * This method is mainly called by KABC::AddressBook.
88 */ 88 */
89 virtual void removeAddressee( const Addressee& addr ); 89 virtual void removeAddressee( const Addressee& addr );
90 90
91 /** 91 /**
92 * This method is called by an error handler if the application 92 * This method is called by an error handler if the application
93 * crashed 93 * crashed
94 */ 94 */
95 virtual void cleanUp(); 95 virtual void cleanUp();
96 96
97protected slots: 97protected slots:
98 void pathChanged(); 98 void pathChanged();
99 99
100protected: 100protected:
101 bool lock( const QString &path ); 101 bool lock( const QString &path );
102 void unlock( const QString &path ); 102 void unlock( const QString &path );
103 103
104private: 104private:
105 FormatPlugin *mFormat; 105 FormatPlugin *mFormat;
106 106
107 KDirWatch mDirWatch; 107 KDirWatch mDirWatch;
108 108
109 QString mPath; 109 QString mPath;
110 QString mFormatName; 110 QString mFormatName;
111 QString mLockUniqueName; 111 QString mLockUniqueName;
112}; 112};
113 113
114} 114}
115#endif 115#endif
diff --git a/kabc/plugins/file/file.pro b/kabc/plugins/file/file.pro
index 32555a2..154ef2a 100644
--- a/kabc/plugins/file/file.pro
+++ b/kabc/plugins/file/file.pro
@@ -1,35 +1,35 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on release 2CONFIG += qt warn_on release
3#release debug 3#release debug
4 4
5TARGET = microkabc_file 5TARGET = microkabc_file
6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../qtcompat 6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../microkde/kresources ../../../qtcompat
7 7
8DESTDIR = ../../../bin 8DESTDIR = ../../../bin
9#LIBS += -lmicrokde -lmicrokabc 9#LIBS += -lmicrokde -lmicrokabc
10#LIBS += -L$(QPEDIR)/lib 10#LIBS += -L$(QPEDIR)/lib
11 11
12INTERFACES = \ 12INTERFACES = \
13 13
14HEADERS = \ 14HEADERS = \
15 resourcefile.h \ 15 resourcefile.h \
16 resourcefileconfig.h 16 resourcefileconfig.h
17 17
18SOURCES = \ 18SOURCES = \
19 resourcefile.cpp \ 19 resourcefile.cpp \
20 resourcefileconfig.cpp 20 resourcefileconfig.cpp
21 21
22unix : { 22unix : {
23OBJECTS_DIR = obj/unix 23OBJECTS_DIR = obj/unix
24MOC_DIR = moc/unix 24MOC_DIR = moc/unix
25} 25}
26win32: { 26win32: {
27CONFIG += dll 27CONFIG += dll
28DEFINES += _WIN32_ 28DEFINES += _WIN32_
29OBJECTS_DIR = obj/win 29OBJECTS_DIR = obj/win
30MOC_DIR = moc/win 30MOC_DIR = moc/win
31LIBS += ../../../bin/microkdepim.lib 31LIBS += ../../../bin/microkdepim.lib
32LIBS += ../../../bin/microkcal.lib 32LIBS += ../../../bin/microkcal.lib
33LIBS += ../../../bin/microkde.lib 33LIBS += ../../../bin/microkde.lib
34LIBS += ../../../bin/microkabc.lib 34LIBS += ../../../bin/microkabc.lib
35} 35}
diff --git a/kabc/plugins/file/fileE.pro b/kabc/plugins/file/fileE.pro
index d19a26d..3015806 100644
--- a/kabc/plugins/file/fileE.pro
+++ b/kabc/plugins/file/fileE.pro
@@ -1,21 +1,21 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on release 2CONFIG += qt warn_on release
3#release debug 3#release debug
4 4
5TARGET = microkabc_file 5TARGET = microkabc_file
6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../qtcompat 6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../microkde/kresources ../../../qtcompat
7OBJECTS_DIR = obj/$(PLATFORM) 7OBJECTS_DIR = obj/$(PLATFORM)
8MOC_DIR = moc/$(PLATFORM) 8MOC_DIR = moc/$(PLATFORM)
9DESTDIR = $(QPEDIR)/lib 9DESTDIR = $(QPEDIR)/lib
10LIBS += -lmicrokde -lmicrokabc 10LIBS += -lmicrokde -lmicrokabc
11LIBS += -L$(QPEDIR)/lib 11LIBS += -L$(QPEDIR)/lib
12 12
13INTERFACES = \ 13INTERFACES = \
14 14
15HEADERS = \ 15HEADERS = \
16 resourcefile.h \ 16 resourcefile.h \
17 resourcefileconfig.h 17 resourcefileconfig.h
18 18
19SOURCES = \ 19SOURCES = \
20 resourcefile.cpp \ 20 resourcefile.cpp \
21 resourcefileconfig.cpp 21 resourcefileconfig.cpp
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp
index 1d3acec..361b36b 100644
--- a/kabc/plugins/file/resourcefile.cpp
+++ b/kabc/plugins/file/resourcefile.cpp
@@ -1,397 +1,399 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$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#ifndef _WIN32_ 30#ifndef _WIN32_
31#include <unistd.h> 31#include <unistd.h>
32#endif 32#endif
33 33
34#include <qfile.h> 34#include <qfile.h>
35#include <qfileinfo.h> 35#include <qfileinfo.h>
36#include <qregexp.h> 36#include <qregexp.h>
37#include <qtimer.h> 37#include <qtimer.h>
38 38
39#include <kapplication.h> 39#include <kapplication.h>
40#include <kconfig.h> 40#include <kconfig.h>
41#include <kdebug.h> 41#include <kdebug.h>
42#include <klocale.h> 42#include <klocale.h>
43//US #include <ksavefile.h> 43//US #include <ksavefile.h>
44#include <kstandarddirs.h> 44#include <kstandarddirs.h>
45#include <kmessagebox.h> 45#include <kmessagebox.h>
46 46
47#include "formatfactory.h" 47#include "formatfactory.h"
48 48
49#include "resource.h" 49#include "resource.h"
50#include "resourcefileconfig.h" 50#include "resourcefileconfig.h"
51#include "stdaddressbook.h" 51#include "stdaddressbook.h"
52 52
53#include "resourcefile.h" 53#include "resourcefile.h"
54#include "syncwidget.h"
55
54 56
55using namespace KABC; 57using namespace KABC;
56 58
57extern "C" 59extern "C"
58#ifdef _WIN32_ 60#ifdef _WIN32_
59__declspec(dllexport) 61__declspec(dllexport)
60#else 62#else
61{ 63{
62#endif 64#endif
63 65
64//US void *init_kabc_file() 66//US void *init_kabc_file()
65 void *init_microkabc_file() 67 void *init_microkabc_file()
66 { 68 {
67 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); 69 return new KRES::PluginFactory<ResourceFile,ResourceFileConfig, KRES::SyncWidget>();
68 } 70 }
69#ifndef _WIN32_ 71#ifndef _WIN32_
70} 72}
71#endif 73#endif
72 74
73ResourceFile::ResourceFile( const KConfig *config ) 75ResourceFile::ResourceFile( const KConfig *config, bool syncable )
74 : Resource( config ) , mFormat( 0 ) 76 : Resource( config, syncable ) , mFormat( 0 )
75{ 77{
76 QString fileName, formatName; 78 QString fileName, formatName;
77 79
78 KConfig *cfg = (KConfig *)config; 80 KConfig *cfg = (KConfig *)config;
79 if ( cfg ) { 81 if ( cfg ) {
80 fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() ); 82 fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() );
81 formatName = cfg->readEntry( "FileFormat", "vcard" ); 83 formatName = cfg->readEntry( "FileFormat", "vcard" );
82// qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() ); 84// qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() );
83 } else { 85 } else {
84 fileName = StdAddressBook::fileName(); 86 fileName = StdAddressBook::fileName();
85 formatName = "vcard"; 87 formatName = "vcard";
86// qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() ); 88// qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() );
87 } 89 }
88 90
89 init( fileName, formatName ); 91 init( fileName, formatName );
90} 92}
91 93
92ResourceFile::ResourceFile( const QString &fileName, 94ResourceFile::ResourceFile( const QString &fileName, bool syncable ,
93 const QString &formatName ) 95 const QString &formatName )
94 : Resource( 0 ) 96 : Resource( 0, syncable )
95{ 97{
96// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); 98// qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1());
97 init( fileName, formatName ); 99 init( fileName, formatName );
98} 100}
99 101
100void ResourceFile::init( const QString &fileName, const QString &formatName ) 102void ResourceFile::init( const QString &fileName, const QString &formatName )
101{ 103{
102 mFormatName = formatName; 104 mFormatName = formatName;
103 105
104 FormatFactory *factory = FormatFactory::self(); 106 FormatFactory *factory = FormatFactory::self();
105 mFormat = factory->format( mFormatName ); 107 mFormat = factory->format( mFormatName );
106 108
107 if ( !mFormat ) { 109 if ( !mFormat ) {
108 mFormatName = "vcard"; 110 mFormatName = "vcard";
109 mFormat = factory->format( mFormatName ); 111 mFormat = factory->format( mFormatName );
110 } 112 }
111 113
112/*US 114/*US
113//US qDebug("ResourceFile::init initialized with format %s ", formatName.latin1()); 115//US qDebug("ResourceFile::init initialized with format %s ", formatName.latin1());
114 if (mFormatName == "vcard") { 116 if (mFormatName == "vcard") {
115 mFormat = new VCardFormatPlugin2(); 117 mFormat = new VCardFormatPlugin2();
116// qDebug("ResourceFile::init format VCardFormatPlugin2"); 118// qDebug("ResourceFile::init format VCardFormatPlugin2");
117 } 119 }
118 else if (mFormatName == "binary") { 120 else if (mFormatName == "binary") {
119 mFormat = new BinaryFormat(); 121 mFormat = new BinaryFormat();
120// qDebug("ResourceFile::init format BinaryFormat"); 122// qDebug("ResourceFile::init format BinaryFormat");
121 } 123 }
122 else 124 else
123 qDebug("ResourceFile::init format unknown !!! %s ", formatName.latin1()); 125 qDebug("ResourceFile::init format unknown !!! %s ", formatName.latin1());
124*/ 126*/
125 127
126 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 128 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
127 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 129 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
128 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 130 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
129 131
130 setFileName( fileName ); 132 setFileName( fileName );
131} 133}
132 134
133ResourceFile::~ResourceFile() 135ResourceFile::~ResourceFile()
134{ 136{
135 delete mFormat; 137 delete mFormat;
136 mFormat = 0; 138 mFormat = 0;
137} 139}
138 140
139void ResourceFile::writeConfig( KConfig *config ) 141void ResourceFile::writeConfig( KConfig *config )
140{ 142{
141 143
142 config->setGroup( "Resource_" + identifier() ); 144 config->setGroup( "Resource_" + identifier() );
143 Resource::writeConfig( config ); 145 Resource::writeConfig( config );
144 146
145 config->writeEntry( "FileName", mFileName ); 147 config->writeEntry( "FileName", mFileName );
146 config->writeEntry( "FileFormat", mFormatName ); 148 config->writeEntry( "FileFormat", mFormatName );
147 149
148// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); 150// qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1());
149 151
150} 152}
151 153
152Ticket *ResourceFile::requestSaveTicket() 154Ticket *ResourceFile::requestSaveTicket()
153{ 155{
154 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; 156 kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl;
155 157
156 if ( !addressBook() ) return 0; 158 if ( !addressBook() ) return 0;
157 159
158 if ( !lock( mFileName ) ) { 160 if ( !lock( mFileName ) ) {
159 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '" 161 kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '"
160 << mFileName << "'" << endl; 162 << mFileName << "'" << endl;
161 return 0; 163 return 0;
162 } 164 }
163 return createTicket( this ); 165 return createTicket( this );
164} 166}
165 167
166 168
167bool ResourceFile::doOpen() 169bool ResourceFile::doOpen()
168{ 170{
169 QFile file( mFileName ); 171 QFile file( mFileName );
170 172
171 if ( !file.exists() ) { 173 if ( !file.exists() ) {
172 // try to create the file 174 // try to create the file
173 bool ok = file.open( IO_WriteOnly ); 175 bool ok = file.open( IO_WriteOnly );
174 if ( ok ) 176 if ( ok )
175 file.close(); 177 file.close();
176 178
177 return ok; 179 return ok;
178 } else { 180 } else {
179 if ( !file.open( IO_ReadWrite ) ) 181 if ( !file.open( IO_ReadWrite ) )
180 return false; 182 return false;
181 183
182 if ( file.size() == 0 ) { 184 if ( file.size() == 0 ) {
183 file.close(); 185 file.close();
184 return true; 186 return true;
185 } 187 }
186 188
187 bool ok = mFormat->checkFormat( &file ); 189 bool ok = mFormat->checkFormat( &file );
188 file.close(); 190 file.close();
189 191
190 return ok; 192 return ok;
191 } 193 }
192} 194}
193 195
194void ResourceFile::doClose() 196void ResourceFile::doClose()
195{ 197{
196} 198}
197 199
198bool ResourceFile::load() 200bool ResourceFile::load()
199{ 201{
200 202
201 203
202 QFile file( mFileName ); 204 QFile file( mFileName );
203 if ( !file.open( IO_ReadOnly ) ) { 205 if ( !file.open( IO_ReadOnly ) ) {
204 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); 206 addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) );
205 return false; 207 return false;
206 } 208 }
207 209
208// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); 210// qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1());
209 211
210 return mFormat->loadAll( addressBook(), this, &file ); 212 return mFormat->loadAll( addressBook(), this, &file );
211} 213}
212 214
213bool ResourceFile::save( Ticket *ticket ) 215bool ResourceFile::save( Ticket *ticket )
214{ 216{
215// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 217// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
216 218
217 219
218 // create backup file 220 // create backup file
219 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 221 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
220 222
221/*US we use a simpler method to create a backupfile 223/*US we use a simpler method to create a backupfile
222 224
223 (void) KSaveFile::backupFile( mFileName, QString::null 225 (void) KSaveFile::backupFile( mFileName, QString::null
224 ,extension ); 226 ,extension );
225 227
226 KSaveFile saveFile( mFileName ); 228 KSaveFile saveFile( mFileName );
227 bool ok = false; 229 bool ok = false;
228 if ( saveFile.status() == 0 && saveFile.file() ) 230 if ( saveFile.status() == 0 && saveFile.file() )
229 { 231 {
230 mFormat->saveAll( addressBook(), this, saveFile.file() ); 232 mFormat->saveAll( addressBook(), this, saveFile.file() );
231 ok = saveFile.close(); 233 ok = saveFile.close();
232 } 234 }
233*/ 235*/
234 236
235//US ToDo: write backupfile 237//US ToDo: write backupfile
236 QFile info; 238 QFile info;
237 info.setName( mFileName ); 239 info.setName( mFileName );
238 bool ok = info.open( IO_WriteOnly ); 240 bool ok = info.open( IO_WriteOnly );
239 if ( ok ) { 241 if ( ok ) {
240 mFormat->saveAll( addressBook(), this, &info ); 242 mFormat->saveAll( addressBook(), this, &info );
241 243
242 info.close(); 244 info.close();
243 ok = true; 245 ok = true;
244 } 246 }
245 else { 247 else {
246 248
247 } 249 }
248 250
249 if ( !ok ) 251 if ( !ok )
250 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 252 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
251 253
252 delete ticket; 254 delete ticket;
253 unlock( mFileName ); 255 unlock( mFileName );
254 256
255 return ok; 257 return ok;
256 258
257 qDebug("ResourceFile::save has to be changed"); 259 qDebug("ResourceFile::save has to be changed");
258 return true; 260 return true;
259} 261}
260 262
261bool ResourceFile::lock( const QString &fileName ) 263bool ResourceFile::lock( const QString &fileName )
262{ 264{
263 265
264 266
265 QString fn = fileName; 267 QString fn = fileName;
266 268
267//US change the implementation how the lockfilename is getting created 269//US change the implementation how the lockfilename is getting created
268//US fn.replace( QRegExp("/"), "_" ); 270//US fn.replace( QRegExp("/"), "_" );
269//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 271//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
270 272
271 KURL url(fn); 273 KURL url(fn);
272 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 274 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
273 275
274 276
275 277
276 if (QFile::exists( lockName )) return false; 278 if (QFile::exists( lockName )) return false;
277 279
278 QString lockUniqueName; 280 QString lockUniqueName;
279 lockUniqueName = fn + KApplication::randomString( 8 ); 281 lockUniqueName = fn + KApplication::randomString( 8 );
280 282
281 url = lockUniqueName; 283 url = lockUniqueName;
282//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 284//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
283 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 285 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
284 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 286 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
285 287
286 // Create unique file 288 // Create unique file
287 QFile file( mLockUniqueName ); 289 QFile file( mLockUniqueName );
288 file.open( IO_WriteOnly ); 290 file.open( IO_WriteOnly );
289 file.close(); 291 file.close();
290 292
291 // Create lock file 293 // Create lock file
292 int result = 0; 294 int result = 0;
293#ifndef _WIN32_ 295#ifndef _WIN32_
294 result = ::link( QFile::encodeName( mLockUniqueName ), 296 result = ::link( QFile::encodeName( mLockUniqueName ),
295 QFile::encodeName( lockName ) ); 297 QFile::encodeName( lockName ) );
296#endif 298#endif
297 if ( result == 0 ) { 299 if ( result == 0 ) {
298 addressBook()->emitAddressBookLocked(); 300 addressBook()->emitAddressBookLocked();
299 return true; 301 return true;
300 } 302 }
301 303
302 // TODO: check stat 304 // TODO: check stat
303 305
304 return false; 306 return false;
305} 307}
306 308
307void ResourceFile::unlock( const QString &fileName ) 309void ResourceFile::unlock( const QString &fileName )
308{ 310{
309 QString fn = fileName; 311 QString fn = fileName;
310//US change the implementation how the lockfilename is getting created 312//US change the implementation how the lockfilename is getting created
311//US fn.replace( QRegExp( "/" ), "_" ); 313//US fn.replace( QRegExp( "/" ), "_" );
312//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 314//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
313//US QString lockName = fn + ".lock"; 315//US QString lockName = fn + ".lock";
314 KURL url(fn); 316 KURL url(fn);
315 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 317 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
316 318
317 QFile::remove( lockName ); 319 QFile::remove( lockName );
318 QFile::remove( mLockUniqueName ); 320 QFile::remove( mLockUniqueName );
319 addressBook()->emitAddressBookUnlocked(); 321 addressBook()->emitAddressBookUnlocked();
320} 322}
321 323
322void ResourceFile::setFileName( const QString &fileName ) 324void ResourceFile::setFileName( const QString &fileName )
323{ 325{
324 mDirWatch.stopScan(); 326 mDirWatch.stopScan();
325 mDirWatch.removeFile( mFileName ); 327 mDirWatch.removeFile( mFileName );
326 328
327 mFileName = fileName; 329 mFileName = fileName;
328 330
329 331
330 mDirWatch.addFile( mFileName ); 332 mDirWatch.addFile( mFileName );
331 mDirWatch.startScan(); 333 mDirWatch.startScan();
332 334
333//US simulate KDirWatch event 335//US simulate KDirWatch event
334//US fileChanged(); 336//US fileChanged();
335} 337}
336 338
337QString ResourceFile::fileName() const 339QString ResourceFile::fileName() const
338{ 340{
339 return mFileName; 341 return mFileName;
340} 342}
341 343
342void ResourceFile::setFormat( const QString &format ) 344void ResourceFile::setFormat( const QString &format )
343{ 345{
344 mFormatName = format; 346 mFormatName = format;
345 delete mFormat; 347 delete mFormat;
346 348
347 FormatFactory *factory = FormatFactory::self(); 349 FormatFactory *factory = FormatFactory::self();
348 mFormat = factory->format( mFormatName ); 350 mFormat = factory->format( mFormatName );
349/*US 351/*US
350//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); 352//qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1());
351 if (mFormatName == "vcard") { 353 if (mFormatName == "vcard") {
352 mFormat = new VCardFormatPlugin2(); 354 mFormat = new VCardFormatPlugin2();
353// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 355// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
354 } 356 }
355 else if (mFormatName == "binary") { 357 else if (mFormatName == "binary") {
356 mFormat = new BinaryFormat(); 358 mFormat = new BinaryFormat();
357// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); 359// qDebug("ResourceFile::setFormat format %s", mFormatName.latin1());
358 } 360 }
359 else 361 else
360 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); 362 qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1());
361*/ 363*/
362 364
363} 365}
364 366
365QString ResourceFile::format() const 367QString ResourceFile::format() const
366{ 368{
367 return mFormatName; 369 return mFormatName;
368} 370}
369 371
370void ResourceFile::fileChanged() 372void ResourceFile::fileChanged()
371{ 373{
372 // There is a small theoretical chance that KDirWatch calls us before 374 // There is a small theoretical chance that KDirWatch calls us before
373 // we are fully constructed 375 // we are fully constructed
374 if (!addressBook()) 376 if (!addressBook())
375 return; 377 return;
376 378
377 379
378 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); 380 QString text( i18n( "File resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) );
379 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 381 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
380 load(); 382 load();
381 addressBook()->emitAddressBookChanged(); 383 addressBook()->emitAddressBookChanged();
382 } 384 }
383} 385}
384 386
385void ResourceFile::removeAddressee( const Addressee &addr ) 387void ResourceFile::removeAddressee( const Addressee &addr )
386{ 388{
387 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); 389 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) );
388 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); 390 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) );
389 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); 391 QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) );
390} 392}
391 393
392void ResourceFile::cleanUp() 394void ResourceFile::cleanUp()
393{ 395{
394 unlock( mFileName ); 396 unlock( mFileName );
395} 397}
396 398
397//US #include "resourcefile.moc" 399//US #include "resourcefile.moc"
diff --git a/kabc/plugins/file/resourcefile.h b/kabc/plugins/file/resourcefile.h
index 4522d78..0a3027c 100644
--- a/kabc/plugins/file/resourcefile.h
+++ b/kabc/plugins/file/resourcefile.h
@@ -1,162 +1,162 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28 28
29#ifndef KABC_RESOURCEFILE_H 29#ifndef KABC_RESOURCEFILE_H
30#define KABC_RESOURCEFILE_H 30#define KABC_RESOURCEFILE_H
31 31
32#include <kconfig.h> 32#include <kconfig.h>
33#include <kdirwatch.h> 33#include <kdirwatch.h>
34 34
35#include <sys/types.h> 35#include <sys/types.h>
36 36
37#include <resource.h> 37#include <resource.h>
38 38
39class QTimer; 39class QTimer;
40class FormatPlugin; 40class FormatPlugin;
41 41
42namespace KABC { 42namespace KABC {
43 43
44//US class FormatPlugin; 44//US class FormatPlugin;
45class ResourceConfigWidget; 45class ResourceConfigWidget;
46 46
47/** 47/**
48 @internal 48 @internal
49*/ 49*/
50class ResourceFile : public Resource 50class ResourceFile : public Resource
51{ 51{
52 Q_OBJECT 52 Q_OBJECT
53 53
54public: 54public:
55 55
56 /** 56 /**
57 Constructor. 57 Constructor.
58 58
59 @param cfg The config object where custom resource settings are stored. 59 @param cfg The config object where custom resource settings are stored.
60 */ 60 */
61 ResourceFile( const KConfig *cfg ); 61 ResourceFile( const KConfig *cfg, bool syncable );
62 62
63 /** 63 /**
64 Construct file resource on file @arg fileName using format @arg formatName. 64 Construct file resource on file @arg fileName using format @arg formatName.
65 */ 65 */
66 ResourceFile( const QString &fileName, const QString &formatName = "vcard" ); 66 ResourceFile( const QString &fileName, bool syncable , const QString &formatName = "vcard" );
67 67
68 /** 68 /**
69 * Destructor. 69 * Destructor.
70 */ 70 */
71 ~ResourceFile(); 71 ~ResourceFile();
72 72
73 /** 73 /**
74 Writes the config back. 74 Writes the config back.
75 */ 75 */
76 virtual void writeConfig( KConfig *cfg ); 76 virtual void writeConfig( KConfig *cfg );
77 77
78 /** 78 /**
79 * Tries to open the file and checks for the proper format. 79 * Tries to open the file and checks for the proper format.
80 * This method should be called before @ref load(). 80 * This method should be called before @ref load().
81 */ 81 */
82 virtual bool doOpen(); 82 virtual bool doOpen();
83 83
84 /** 84 /**
85 * Closes the file again. 85 * Closes the file again.
86 */ 86 */
87 virtual void doClose(); 87 virtual void doClose();
88 88
89 /** 89 /**
90 * Requests a save ticket, that is used by @ref save() 90 * Requests a save ticket, that is used by @ref save()
91 */ 91 */
92 virtual Ticket *requestSaveTicket(); 92 virtual Ticket *requestSaveTicket();
93 93
94 /** 94 /**
95 * Loads all addressees from file to the address book. 95 * Loads all addressees from file to the address book.
96 * Returns true if all addressees could be loaded otherwise false. 96 * Returns true if all addressees could be loaded otherwise false.
97 */ 97 */
98 virtual bool load(); 98 virtual bool load();
99 99
100 /** 100 /**
101 * Saves all addresses from address book to file. 101 * Saves all addresses from address book to file.
102 * Returns true if all addressees could be saved otherwise false. 102 * Returns true if all addressees could be saved otherwise false.
103 * 103 *
104 * @param ticket The ticket returned by @ref requestSaveTicket() 104 * @param ticket The ticket returned by @ref requestSaveTicket()
105 */ 105 */
106 virtual bool save( Ticket *ticket ); 106 virtual bool save( Ticket *ticket );
107 107
108 /** 108 /**
109 * Set name of file to be used for saving. 109 * Set name of file to be used for saving.
110 */ 110 */
111 void setFileName( const QString & ); 111 void setFileName( const QString & );
112 112
113 /** 113 /**
114 * Return name of file used for loading and saving the address book. 114 * Return name of file used for loading and saving the address book.
115 */ 115 */
116 QString fileName() const; 116 QString fileName() const;
117 117
118 /** 118 /**
119 Sets a new format by name. 119 Sets a new format by name.
120 */ 120 */
121 void setFormat( const QString &name ); 121 void setFormat( const QString &name );
122 122
123 /** 123 /**
124 Returns the format name. 124 Returns the format name.
125 */ 125 */
126 QString format() const; 126 QString format() const;
127 127
128 /** 128 /**
129 * Remove a addressee from its source. 129 * Remove a addressee from its source.
130 * This method is mainly called by KABC::AddressBook. 130 * This method is mainly called by KABC::AddressBook.
131 */ 131 */
132 virtual void removeAddressee( const Addressee& addr ); 132 virtual void removeAddressee( const Addressee& addr );
133 133
134 /** 134 /**
135 * This method is called by an error handler if the application 135 * This method is called by an error handler if the application
136 * crashed 136 * crashed
137 */ 137 */
138 virtual void cleanUp(); 138 virtual void cleanUp();
139 139
140protected slots: 140protected slots:
141 void fileChanged(); 141 void fileChanged();
142 142
143protected: 143protected:
144 void init( const QString &fileName, const QString &format ); 144 void init( const QString &fileName, const QString &format );
145 145
146 bool lock( const QString &fileName ); 146 bool lock( const QString &fileName );
147 void unlock( const QString &fileName ); 147 void unlock( const QString &fileName );
148 148
149private: 149private:
150 QString mFileName; 150 QString mFileName;
151 QString mFormatName; 151 QString mFormatName;
152 152
153 FormatPlugin *mFormat; 153 FormatPlugin *mFormat;
154 154
155 QString mLockUniqueName; 155 QString mLockUniqueName;
156 156
157 KDirWatch mDirWatch; 157 KDirWatch mDirWatch;
158}; 158};
159 159
160} 160}
161 161
162#endif 162#endif
diff --git a/kabc/plugins/ldap/ldapE.pro b/kabc/plugins/ldap/ldapE.pro
index f6ca586..2e79fc0 100644
--- a/kabc/plugins/ldap/ldapE.pro
+++ b/kabc/plugins/ldap/ldapE.pro
@@ -1,21 +1,21 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on release 2CONFIG += qt warn_on release
3#release debug 3#release debug
4 4
5TARGET = microkabc_ldap 5TARGET = microkabc_ldap
6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../qtcompat $(QPEDIR)/include 6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kresources ../../../qtcompat $(QPEDIR)/include
7OBJECTS_DIR = obj/$(PLATFORM) 7OBJECTS_DIR = obj/$(PLATFORM)
8MOC_DIR = moc/$(PLATFORM) 8MOC_DIR = moc/$(PLATFORM)
9DESTDIR = $(QPEDIR)/lib 9DESTDIR = $(QPEDIR)/lib
10LIBS += -lmicrokde -lmicrokabc 10LIBS += -lmicrokde -lmicrokabc
11LIBS += -L$(QPEDIR)/lib 11LIBS += -L$(QPEDIR)/lib
12 12
13INTERFACES = \ 13INTERFACES = \
14 14
15HEADERS = \ 15HEADERS = \
16 resourceldap.h \ 16 resourceldap.h \
17 resourceldapconfig.h 17 resourceldapconfig.h
18 18
19SOURCES = \ 19SOURCES = \
20 resourceldap.cpp \ 20 resourceldap.cpp \
21 resourceldapconfig.cpp 21 resourceldapconfig.cpp
diff --git a/kabc/plugins/ldap/resourceldap.cpp b/kabc/plugins/ldap/resourceldap.cpp
index 55c43af..4b9dede 100644
--- a/kabc/plugins/ldap/resourceldap.cpp
+++ b/kabc/plugins/ldap/resourceldap.cpp
@@ -1,444 +1,445 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28#include <kdebug.h> 28#include <kdebug.h>
29#include <kglobal.h> 29#include <kglobal.h>
30#include <klineedit.h> 30#include <klineedit.h>
31#include <klocale.h> 31#include <klocale.h>
32#include <kconfig.h> 32#include <kconfig.h>
33#include <kstringhandler.h> 33#include <kstringhandler.h>
34 34
35#include <stdlib.h> 35#include <stdlib.h>
36 36
37#include "resourceldap.h" 37#include "resourceldap.h"
38#include "resourceldapconfig.h" 38#include "resourceldapconfig.h"
39#include "syncwidget.h"
39 40
40using namespace KABC; 41using namespace KABC;
41 42
42extern "C" 43extern "C"
43{ 44{
44//US void *init_kabc_ldap() 45//US void *init_kabc_ldap()
45 void *init_microkabc_ldap() 46 void *init_microkabc_ldap()
46 { 47 {
47 return new KRES::PluginFactory<ResourceLDAP,ResourceLDAPConfig>(); 48 return new KRES::PluginFactory<ResourceLDAP,ResourceLDAPConfig, KRES::SyncWidget>();
48 } 49 }
49} 50}
50 51
51void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value ); 52void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value );
52 53
53 54
54ResourceLDAP::ResourceLDAP( const KConfig *config ) 55ResourceLDAP::ResourceLDAP( const KConfig *config, bool syncable )
55 : Resource( config ), mPort( 389 ), mLdap( 0 ) 56 : Resource( config, syncable ), mPort( 389 ), mLdap( 0 )
56{ 57{
57 KConfig *cfg = (KConfig *)config; 58 KConfig *cfg = (KConfig *)config;
58 if ( cfg ) { 59 if ( cfg ) {
59 mUser = cfg->readEntry( "LdapUser" ); 60 mUser = cfg->readEntry( "LdapUser" );
60 mPassword = KStringHandler::obscure( cfg->readEntry( "LdapPassword" ) ); 61 mPassword = KStringHandler::obscure( cfg->readEntry( "LdapPassword" ) );
61 mDn = cfg->readEntry( "LdapDn" ); 62 mDn = cfg->readEntry( "LdapDn" );
62 mHost = cfg->readEntry( "LdapHost" ); 63 mHost = cfg->readEntry( "LdapHost" );
63 mPort = cfg->readNumEntry( "LdapPort", 389 ); 64 mPort = cfg->readNumEntry( "LdapPort", 389 );
64 mFilter = cfg->readEntry( "LdapFilter" ); 65 mFilter = cfg->readEntry( "LdapFilter" );
65 mAnonymous = cfg->readBoolEntry( "LdapAnonymous" ); 66 mAnonymous = cfg->readBoolEntry( "LdapAnonymous" );
66 67
67 QStringList attributes = cfg->readListEntry( "LdapAttributes" ); 68 QStringList attributes = cfg->readListEntry( "LdapAttributes" );
68 for ( uint pos = 0; pos < attributes.count(); pos += 2 ) 69 for ( uint pos = 0; pos < attributes.count(); pos += 2 )
69 mAttributes.insert( attributes[ pos ], attributes[ pos + 1 ] ); 70 mAttributes.insert( attributes[ pos ], attributes[ pos + 1 ] );
70 } 71 }
71 72
72 /** 73 /**
73 If you want to add new attributes, append them here, add a 74 If you want to add new attributes, append them here, add a
74 translation string in the ctor of AttributesDialog and 75 translation string in the ctor of AttributesDialog and
75 handle them in the load() method below. 76 handle them in the load() method below.
76 These are the default values from 77 These are the default values from
77 */ 78 */
78 if ( mAttributes.count() == 0 ) { 79 if ( mAttributes.count() == 0 ) {
79 mAttributes.insert( "commonName", "cn" ); 80 mAttributes.insert( "commonName", "cn" );
80 mAttributes.insert( "formattedName", "displayName" ); 81 mAttributes.insert( "formattedName", "displayName" );
81 mAttributes.insert( "familyName", "sn" ); 82 mAttributes.insert( "familyName", "sn" );
82 mAttributes.insert( "givenName", "givenName" ); 83 mAttributes.insert( "givenName", "givenName" );
83 mAttributes.insert( "mail", "mail" ); 84 mAttributes.insert( "mail", "mail" );
84 mAttributes.insert( "mailAlias", "" ); 85 mAttributes.insert( "mailAlias", "" );
85 mAttributes.insert( "phoneNumber", "telephoneNumber" ); 86 mAttributes.insert( "phoneNumber", "telephoneNumber" );
86 mAttributes.insert( "uid", "uid" ); 87 mAttributes.insert( "uid", "uid" );
87 } 88 }
88} 89}
89 90
90void ResourceLDAP::writeConfig( KConfig *config ) 91void ResourceLDAP::writeConfig( KConfig *config )
91{ 92{
92 Resource::writeConfig( config ); 93 Resource::writeConfig( config );
93 94
94 config->writeEntry( "LdapUser", mUser ); 95 config->writeEntry( "LdapUser", mUser );
95 config->writeEntry( "LdapPassword", KStringHandler::obscure( mPassword ) ); 96 config->writeEntry( "LdapPassword", KStringHandler::obscure( mPassword ) );
96 config->writeEntry( "LdapDn", mDn ); 97 config->writeEntry( "LdapDn", mDn );
97 config->writeEntry( "LdapHost", mHost ); 98 config->writeEntry( "LdapHost", mHost );
98 config->writeEntry( "LdapPort", mPort ); 99 config->writeEntry( "LdapPort", mPort );
99 config->writeEntry( "LdapFilter", mFilter ); 100 config->writeEntry( "LdapFilter", mFilter );
100 config->writeEntry( "LdapAnonymous", mAnonymous ); 101 config->writeEntry( "LdapAnonymous", mAnonymous );
101 102
102 QStringList attributes; 103 QStringList attributes;
103 QMap<QString, QString>::Iterator it; 104 QMap<QString, QString>::Iterator it;
104 for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) 105 for ( it = mAttributes.begin(); it != mAttributes.end(); ++it )
105 attributes << it.key() << it.data(); 106 attributes << it.key() << it.data();
106 107
107 config->writeEntry( "LdapAttributes", attributes ); 108 config->writeEntry( "LdapAttributes", attributes );
108} 109}
109 110
110Ticket *ResourceLDAP::requestSaveTicket() 111Ticket *ResourceLDAP::requestSaveTicket()
111{ 112{
112 if ( !addressBook() ) { 113 if ( !addressBook() ) {
113 kdDebug(5700) << "no addressbook" << endl; 114 kdDebug(5700) << "no addressbook" << endl;
114 return 0; 115 return 0;
115 } 116 }
116 117
117 return createTicket( this ); 118 return createTicket( this );
118} 119}
119 120
120bool ResourceLDAP::doOpen() 121bool ResourceLDAP::doOpen()
121{ 122{
122 if ( mLdap ) 123 if ( mLdap )
123 return false; 124 return false;
124 125
125 if ( !mPort ) 126 if ( !mPort )
126 mPort = 389; 127 mPort = 389;
127 128
128 mLdap = ldap_init( mHost.local8Bit(), mPort ); 129 mLdap = ldap_init( mHost.local8Bit(), mPort );
129 if ( !mLdap ) { 130 if ( !mLdap ) {
130 addressBook()->error( i18n( "Unable to connect to server '%1' on port '%2'" ).arg( mHost ).arg( mPort ) ); 131 addressBook()->error( i18n( "Unable to connect to server '%1' on port '%2'" ).arg( mHost ).arg( mPort ) );
131 return false; 132 return false;
132 } 133 }
133 134
134 if ( !mUser.isEmpty() && !mAnonymous ) { 135 if ( !mUser.isEmpty() && !mAnonymous ) {
135 if ( ldap_simple_bind_s( mLdap, mUser.local8Bit(), mPassword.local8Bit() ) != LDAP_SUCCESS ) { 136 if ( ldap_simple_bind_s( mLdap, mUser.local8Bit(), mPassword.local8Bit() ) != LDAP_SUCCESS ) {
136 addressBook()->error( i18n( "Unable to bind to server '%1'" ).arg( mHost ) ); 137 addressBook()->error( i18n( "Unable to bind to server '%1'" ).arg( mHost ) );
137 return false; 138 return false;
138 } 139 }
139 140
140 kdDebug(5700) << "ResourceLDAP: bind to server successfully" << endl; 141 kdDebug(5700) << "ResourceLDAP: bind to server successfully" << endl;
141 } else { 142 } else {
142 if ( ldap_simple_bind_s( mLdap, NULL, NULL ) != LDAP_SUCCESS ) { 143 if ( ldap_simple_bind_s( mLdap, NULL, NULL ) != LDAP_SUCCESS ) {
143 addressBook()->error( i18n( "Unable to bind anonymously to server '%1'" ).arg( mHost ) ); 144 addressBook()->error( i18n( "Unable to bind anonymously to server '%1'" ).arg( mHost ) );
144 return false; 145 return false;
145 } 146 }
146 147
147 kdDebug( 5700 ) << "ResourceLDAP: bind anonymously to server successfully" << endl; 148 kdDebug( 5700 ) << "ResourceLDAP: bind anonymously to server successfully" << endl;
148 } 149 }
149 150
150 int deref = LDAP_DEREF_ALWAYS; 151 int deref = LDAP_DEREF_ALWAYS;
151 if ( ldap_set_option( mLdap, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS ) { 152 if ( ldap_set_option( mLdap, LDAP_OPT_DEREF, (void *) &deref ) != LDAP_OPT_SUCCESS ) {
152 kdDebug(5700) << "ResourceLDAP: can't set 'deref' option" << endl; 153 kdDebug(5700) << "ResourceLDAP: can't set 'deref' option" << endl;
153 return false; 154 return false;
154 } 155 }
155 156
156 if ( ldap_set_option( mLdap, LDAP_OPT_REFERRALS, LDAP_OPT_ON ) != LDAP_OPT_SUCCESS ) { 157 if ( ldap_set_option( mLdap, LDAP_OPT_REFERRALS, LDAP_OPT_ON ) != LDAP_OPT_SUCCESS ) {
157 kdDebug(5700) << "ResourceLDAP: can't set 'referrals' option" << endl; 158 kdDebug(5700) << "ResourceLDAP: can't set 'referrals' option" << endl;
158 return false; 159 return false;
159 } 160 }
160 161
161 return true; 162 return true;
162} 163}
163 164
164void ResourceLDAP::doClose() 165void ResourceLDAP::doClose()
165{ 166{
166 if ( ldap_unbind_s( mLdap ) != LDAP_SUCCESS ) { 167 if ( ldap_unbind_s( mLdap ) != LDAP_SUCCESS ) {
167 kdDebug(5700) << "ResourceLDAP: can't unbind from server" << endl; 168 kdDebug(5700) << "ResourceLDAP: can't unbind from server" << endl;
168 return; 169 return;
169 } 170 }
170 171
171 mLdap = 0; 172 mLdap = 0;
172} 173}
173 174
174bool ResourceLDAP::load() 175bool ResourceLDAP::load()
175{ 176{
176 LDAPMessage *res; 177 LDAPMessage *res;
177 LDAPMessage *msg; 178 LDAPMessage *msg;
178 BerElement *track; 179 BerElement *track;
179 char *names; 180 char *names;
180 char **values; 181 char **values;
181 182
182 char **LdapSearchAttr = new char*[ mAttributes.count() + 1 ]; 183 char **LdapSearchAttr = new char*[ mAttributes.count() + 1 ];
183 184
184 QMap<QString, QString>::Iterator it; 185 QMap<QString, QString>::Iterator it;
185 int i = 0; 186 int i = 0;
186 for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) { 187 for ( it = mAttributes.begin(); it != mAttributes.end(); ++it ) {
187 if ( !it.data().isEmpty() ) { 188 if ( !it.data().isEmpty() ) {
188 unsigned int len = it.data().utf8().length(); 189 unsigned int len = it.data().utf8().length();
189 LdapSearchAttr[ i ] = new char[ len+1 ]; 190 LdapSearchAttr[ i ] = new char[ len+1 ];
190 memcpy( LdapSearchAttr[ i ], it.data().utf8(), len ); 191 memcpy( LdapSearchAttr[ i ], it.data().utf8(), len );
191 LdapSearchAttr[ i ][ len ] = 0; 192 LdapSearchAttr[ i ][ len ] = 0;
192 ++i; 193 ++i;
193 } 194 }
194 } 195 }
195 LdapSearchAttr[ i ] = 0; 196 LdapSearchAttr[ i ] = 0;
196 197
197 QString filter = mFilter; 198 QString filter = mFilter;
198 if ( filter.isEmpty() ) 199 if ( filter.isEmpty() )
199 filter = "cn=*"; 200 filter = "cn=*";
200 201
201 int result; 202 int result;
202 if ( ( result = ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, QString( "(%1)" ).arg( filter ).local8Bit(), 203 if ( ( result = ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, QString( "(%1)" ).arg( filter ).local8Bit(),
203 LdapSearchAttr, 0, &res ) != LDAP_SUCCESS ) ) { 204 LdapSearchAttr, 0, &res ) != LDAP_SUCCESS ) ) {
204 addressBook()->error( i18n( "Unable to search on server '%1': %2" ) 205 addressBook()->error( i18n( "Unable to search on server '%1': %2" )
205 .arg( mHost ) 206 .arg( mHost )
206 .arg( ldap_err2string( result ) ) ); 207 .arg( ldap_err2string( result ) ) );
207 208
208 for ( i = 0; LdapSearchAttr[ i ]; ++i ) 209 for ( i = 0; LdapSearchAttr[ i ]; ++i )
209 delete [] LdapSearchAttr[ i ]; 210 delete [] LdapSearchAttr[ i ];
210 delete [] LdapSearchAttr; 211 delete [] LdapSearchAttr;
211 212
212 return false; 213 return false;
213 } 214 }
214 215
215 for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) { 216 for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) {
216 Addressee addr; 217 Addressee addr;
217 addr.setResource( this ); 218 addr.setResource( this );
218 for ( names = ldap_first_attribute( mLdap, msg, &track ); names; names = ldap_next_attribute( mLdap, msg, track ) ) { 219 for ( names = ldap_first_attribute( mLdap, msg, &track ); names; names = ldap_next_attribute( mLdap, msg, track ) ) {
219 values = ldap_get_values( mLdap, msg, names ); 220 values = ldap_get_values( mLdap, msg, names );
220 for ( int i = 0; i < ldap_count_values( values ); ++i ) { 221 for ( int i = 0; i < ldap_count_values( values ); ++i ) {
221 QString name = QString::fromUtf8( names ).lower(); 222 QString name = QString::fromUtf8( names ).lower();
222 QString value = QString::fromUtf8( values[ i ] ); 223 QString value = QString::fromUtf8( values[ i ] );
223 224
224 if ( name == mAttributes[ "commonName" ].lower() ) { 225 if ( name == mAttributes[ "commonName" ].lower() ) {
225 if ( !addr.formattedName().isEmpty() ) { 226 if ( !addr.formattedName().isEmpty() ) {
226 QString fn = addr.formattedName(); 227 QString fn = addr.formattedName();
227 addr.setNameFromString( value ); 228 addr.setNameFromString( value );
228 addr.setFormattedName( fn ); 229 addr.setFormattedName( fn );
229 } else 230 } else
230 addr.setNameFromString( value ); 231 addr.setNameFromString( value );
231 } else if ( name == mAttributes[ "formattedName" ].lower() ) { 232 } else if ( name == mAttributes[ "formattedName" ].lower() ) {
232 addr.setFormattedName( value ); 233 addr.setFormattedName( value );
233 } else if ( name == mAttributes[ "givenName" ].lower() ) { 234 } else if ( name == mAttributes[ "givenName" ].lower() ) {
234 addr.setGivenName( value ); 235 addr.setGivenName( value );
235 } else if ( name == mAttributes[ "mail" ].lower() ) { 236 } else if ( name == mAttributes[ "mail" ].lower() ) {
236 addr.insertEmail( value, true ); 237 addr.insertEmail( value, true );
237 } else if ( name == mAttributes[ "mailAlias" ].lower() ) { 238 } else if ( name == mAttributes[ "mailAlias" ].lower() ) {
238 addr.insertEmail( value, false ); 239 addr.insertEmail( value, false );
239 } else if ( name == mAttributes[ "phoneNumber" ].lower() ) { 240 } else if ( name == mAttributes[ "phoneNumber" ].lower() ) {
240 PhoneNumber phone; 241 PhoneNumber phone;
241 phone.setNumber( value ); 242 phone.setNumber( value );
242 addr.insertPhoneNumber( phone ); 243 addr.insertPhoneNumber( phone );
243 break; // read only the home number 244 break; // read only the home number
244 } else if ( name == mAttributes[ "familyName" ].lower() ) { 245 } else if ( name == mAttributes[ "familyName" ].lower() ) {
245 addr.setFamilyName( value ); 246 addr.setFamilyName( value );
246 } else if ( name == mAttributes[ "uid" ].lower() ) { 247 } else if ( name == mAttributes[ "uid" ].lower() ) {
247 addr.setUid( value ); 248 addr.setUid( value );
248 } 249 }
249 } 250 }
250 ldap_value_free( values ); 251 ldap_value_free( values );
251 } 252 }
252 ber_free( track, 0 ); 253 ber_free( track, 0 );
253 254
254 addressBook()->insertAddressee( addr ); 255 addressBook()->insertAddressee( addr );
255 } 256 }
256 257
257 ldap_msgfree( res ); 258 ldap_msgfree( res );
258 259
259 for ( i = 0; LdapSearchAttr[ i ]; ++i ) 260 for ( i = 0; LdapSearchAttr[ i ]; ++i )
260 delete [] LdapSearchAttr[ i ]; 261 delete [] LdapSearchAttr[ i ];
261 delete [] LdapSearchAttr; 262 delete [] LdapSearchAttr;
262 263
263 return true; 264 return true;
264} 265}
265 266
266bool ResourceLDAP::save( Ticket * ) 267bool ResourceLDAP::save( Ticket * )
267{ 268{
268 AddressBook::Iterator it; 269 AddressBook::Iterator it;
269 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 270 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
270 if ( (*it).resource() == this && (*it).changed() ) { 271 if ( (*it).resource() == this && (*it).changed() ) {
271 LDAPMod **mods = NULL; 272 LDAPMod **mods = NULL;
272 273
273 addModOp( &mods, "objectClass", "organizationalPerson" ); 274 addModOp( &mods, "objectClass", "organizationalPerson" );
274 addModOp( &mods, "objectClass", "person" ); 275 addModOp( &mods, "objectClass", "person" );
275 addModOp( &mods, "objectClass", "Top" ); 276 addModOp( &mods, "objectClass", "Top" );
276 addModOp( &mods, mAttributes[ "commonName" ].utf8(), (*it).assembledName() ); 277 addModOp( &mods, mAttributes[ "commonName" ].utf8(), (*it).assembledName() );
277 addModOp( &mods, mAttributes[ "formattedName" ].utf8(), (*it).formattedName() ); 278 addModOp( &mods, mAttributes[ "formattedName" ].utf8(), (*it).formattedName() );
278 addModOp( &mods, mAttributes[ "givenName" ].utf8(), (*it).givenName() ); 279 addModOp( &mods, mAttributes[ "givenName" ].utf8(), (*it).givenName() );
279 addModOp( &mods, mAttributes[ "familyName" ].utf8(), (*it).familyName() ); 280 addModOp( &mods, mAttributes[ "familyName" ].utf8(), (*it).familyName() );
280 addModOp( &mods, mAttributes[ "uid" ].utf8(), (*it).uid() ); 281 addModOp( &mods, mAttributes[ "uid" ].utf8(), (*it).uid() );
281 282
282 QStringList emails = (*it).emails(); 283 QStringList emails = (*it).emails();
283 QStringList::ConstIterator mailIt; 284 QStringList::ConstIterator mailIt;
284 bool first = true; 285 bool first = true;
285 for ( mailIt = emails.begin(); mailIt != emails.end(); ++mailIt ) { 286 for ( mailIt = emails.begin(); mailIt != emails.end(); ++mailIt ) {
286 if ( first ) { 287 if ( first ) {
287 addModOp( &mods, mAttributes[ "mail" ].utf8(), (*mailIt) ); 288 addModOp( &mods, mAttributes[ "mail" ].utf8(), (*mailIt) );
288 first = false; 289 first = false;
289 } else 290 } else
290 addModOp( &mods, mAttributes[ "mailAlias" ].utf8(), (*mailIt) ); 291 addModOp( &mods, mAttributes[ "mailAlias" ].utf8(), (*mailIt) );
291 } 292 }
292 293
293 PhoneNumber number = (*it).phoneNumber( PhoneNumber::Home ); 294 PhoneNumber number = (*it).phoneNumber( PhoneNumber::Home );
294 addModOp( &mods, mAttributes[ "phoneNumber" ].utf8(), number.number() ); 295 addModOp( &mods, mAttributes[ "phoneNumber" ].utf8(), number.number() );
295 296
296 QString dn = "cn=" + (*it).assembledName() + "," + mDn; 297 QString dn = "cn=" + (*it).assembledName() + "," + mDn;
297 298
298 int retval; 299 int retval;
299 if ( (retval = ldap_add_s( mLdap, dn.local8Bit(), mods )) != LDAP_SUCCESS ) 300 if ( (retval = ldap_add_s( mLdap, dn.local8Bit(), mods )) != LDAP_SUCCESS )
300 addressBook()->error( i18n( "Unable to modify '%1' on server '%2'" ).arg( (*it).uid() ).arg( mHost ) ); 301 addressBook()->error( i18n( "Unable to modify '%1' on server '%2'" ).arg( (*it).uid() ).arg( mHost ) );
301 302
302 ldap_mods_free( mods, 1 ); 303 ldap_mods_free( mods, 1 );
303 304
304 // mark as unchanged 305 // mark as unchanged
305 (*it).setChanged( false ); 306 (*it).setChanged( false );
306 } 307 }
307 } 308 }
308 309
309 return true; 310 return true;
310} 311}
311 312
312void ResourceLDAP::removeAddressee( const Addressee &addr ) 313void ResourceLDAP::removeAddressee( const Addressee &addr )
313{ 314{
314 LDAPMessage *res; 315 LDAPMessage *res;
315 LDAPMessage *msg; 316 LDAPMessage *msg;
316 317
317 QString filter = QString( "(&(uid=%1)(%2))" ).arg( addr.uid() ).arg( mFilter ); 318 QString filter = QString( "(&(uid=%1)(%2))" ).arg( addr.uid() ).arg( mFilter );
318 319
319 kdDebug(5700) << "ldap:removeAddressee" << filter << endl; 320 kdDebug(5700) << "ldap:removeAddressee" << filter << endl;
320 321
321 ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, filter.local8Bit(), 322 ldap_search_s( mLdap, mDn.local8Bit(), LDAP_SCOPE_SUBTREE, filter.local8Bit(),
322 0, 0, &res ); 323 0, 0, &res );
323 324
324 for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) { 325 for ( msg = ldap_first_entry( mLdap, res ); msg; msg = ldap_next_entry( mLdap, msg ) ) {
325 char *dn = ldap_get_dn( mLdap, msg ); 326 char *dn = ldap_get_dn( mLdap, msg );
326 kdDebug(5700) << "found " << dn << endl; 327 kdDebug(5700) << "found " << dn << endl;
327 if ( ldap_delete_s( mLdap, dn ) != LDAP_SUCCESS ) 328 if ( ldap_delete_s( mLdap, dn ) != LDAP_SUCCESS )
328 addressBook()->error( i18n( "Unable to delete '%1' on server '%2'" ).arg( dn ).arg( mHost ) ); 329 addressBook()->error( i18n( "Unable to delete '%1' on server '%2'" ).arg( dn ).arg( mHost ) );
329 ldap_memfree( dn ); 330 ldap_memfree( dn );
330 } 331 }
331 332
332 ldap_msgfree( res ); 333 ldap_msgfree( res );
333} 334}
334 335
335void ResourceLDAP::setUser( const QString &user ) 336void ResourceLDAP::setUser( const QString &user )
336{ 337{
337 mUser = user; 338 mUser = user;
338} 339}
339 340
340QString ResourceLDAP::user() const 341QString ResourceLDAP::user() const
341{ 342{
342 return mUser; 343 return mUser;
343} 344}
344 345
345void ResourceLDAP::setPassword( const QString &password ) 346void ResourceLDAP::setPassword( const QString &password )
346{ 347{
347 mPassword = password; 348 mPassword = password;
348} 349}
349 350
350QString ResourceLDAP::password() const 351QString ResourceLDAP::password() const
351{ 352{
352 return mPassword; 353 return mPassword;
353} 354}
354 355
355void ResourceLDAP::setDn( const QString &dn ) 356void ResourceLDAP::setDn( const QString &dn )
356{ 357{
357 mDn = dn; 358 mDn = dn;
358} 359}
359 360
360QString ResourceLDAP::dn() const 361QString ResourceLDAP::dn() const
361{ 362{
362 return mDn; 363 return mDn;
363} 364}
364 365
365void ResourceLDAP::setHost( const QString &host ) 366void ResourceLDAP::setHost( const QString &host )
366{ 367{
367 mHost = host; 368 mHost = host;
368} 369}
369 370
370QString ResourceLDAP::host() const 371QString ResourceLDAP::host() const
371{ 372{
372 return mHost; 373 return mHost;
373} 374}
374 375
375void ResourceLDAP::setPort( int port ) 376void ResourceLDAP::setPort( int port )
376{ 377{
377 mPort = port; 378 mPort = port;
378} 379}
379 380
380int ResourceLDAP::port() const 381int ResourceLDAP::port() const
381{ 382{
382 return mPort; 383 return mPort;
383} 384}
384 385
385void ResourceLDAP::setFilter( const QString &filter ) 386void ResourceLDAP::setFilter( const QString &filter )
386{ 387{
387 mFilter = filter; 388 mFilter = filter;
388} 389}
389 390
390QString ResourceLDAP::filter() const 391QString ResourceLDAP::filter() const
391{ 392{
392 return mFilter; 393 return mFilter;
393} 394}
394 395
395void ResourceLDAP::setIsAnonymous( bool value ) 396void ResourceLDAP::setIsAnonymous( bool value )
396{ 397{
397 mAnonymous = value; 398 mAnonymous = value;
398} 399}
399 400
400bool ResourceLDAP::isAnonymous() const 401bool ResourceLDAP::isAnonymous() const
401{ 402{
402 return mAnonymous; 403 return mAnonymous;
403} 404}
404 405
405void ResourceLDAP::setAttributes( const QMap<QString, QString> &attributes ) 406void ResourceLDAP::setAttributes( const QMap<QString, QString> &attributes )
406{ 407{
407 mAttributes = attributes; 408 mAttributes = attributes;
408} 409}
409 410
410QMap<QString, QString> ResourceLDAP::attributes() const 411QMap<QString, QString> ResourceLDAP::attributes() const
411{ 412{
412 return mAttributes; 413 return mAttributes;
413} 414}
414 415
415void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value ) 416void addModOp( LDAPMod ***pmods, const QString &attr, const QString &value )
416{ 417{
417 if ( value.isNull() ) 418 if ( value.isNull() )
418 return; 419 return;
419 420
420 LDAPMod**mods; 421 LDAPMod**mods;
421 422
422 mods = *pmods; 423 mods = *pmods;
423 424
424 uint i = 0; 425 uint i = 0;
425 if ( mods != 0 ) 426 if ( mods != 0 )
426 for ( ; mods[ i ] != 0; ++i ); 427 for ( ; mods[ i ] != 0; ++i );
427 428
428 if (( mods = (LDAPMod **)realloc( mods, (i + 2) * sizeof( LDAPMod * ))) == 0 ) { 429 if (( mods = (LDAPMod **)realloc( mods, (i + 2) * sizeof( LDAPMod * ))) == 0 ) {
429 kdError() << "ResourceLDAP: realloc" << endl; 430 kdError() << "ResourceLDAP: realloc" << endl;
430 return; 431 return;
431 } 432 }
432 433
433 *pmods = mods; 434 *pmods = mods;
434 mods[ i + 1 ] = 0; 435 mods[ i + 1 ] = 0;
435 436
436 mods[ i ] = new LDAPMod; 437 mods[ i ] = new LDAPMod;
437 438
438 mods[ i ]->mod_op = 0; 439 mods[ i ]->mod_op = 0;
439 mods[ i ]->mod_type = strdup( attr.utf8() ); 440 mods[ i ]->mod_type = strdup( attr.utf8() );
440 mods[ i ]->mod_values = new char*[ 2 ]; 441 mods[ i ]->mod_values = new char*[ 2 ];
441 mods[ i ]->mod_values[ 0 ] = strdup( value.utf8() ); 442 mods[ i ]->mod_values[ 0 ] = strdup( value.utf8() );
442 mods[ i ]->mod_values[ 1 ] = 0; 443 mods[ i ]->mod_values[ 1 ] = 0;
443} 444}
444 445
diff --git a/kabc/plugins/ldap/resourceldap.h b/kabc/plugins/ldap/resourceldap.h
index 0625f30..0aad3c1 100644
--- a/kabc/plugins/ldap/resourceldap.h
+++ b/kabc/plugins/ldap/resourceldap.h
@@ -1,99 +1,99 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28#ifndef KABC_RESOURCELDAP_H 28#ifndef KABC_RESOURCELDAP_H
29#define KABC_RESOURCELDAP_H 29#define KABC_RESOURCELDAP_H
30 30
31 31
32#include <lber.h> 32#include <lber.h>
33#include <ldap.h> 33#include <ldap.h>
34 34
35#include "addressbook.h" 35#include "addressbook.h"
36#include "resource.h" 36#include "resource.h"
37 37
38class KConfig; 38class KConfig;
39 39
40namespace KABC { 40namespace KABC {
41 41
42class ResourceLDAP : public Resource 42class ResourceLDAP : public Resource
43{ 43{
44public: 44public:
45 45
46 ResourceLDAP( const KConfig* ); 46 ResourceLDAP( const KConfig*, bool syncable );
47 47
48 virtual void writeConfig( KConfig* ); 48 virtual void writeConfig( KConfig* );
49 49
50 virtual bool doOpen(); 50 virtual bool doOpen();
51 virtual void doClose(); 51 virtual void doClose();
52 52
53 virtual Ticket *requestSaveTicket(); 53 virtual Ticket *requestSaveTicket();
54 54
55 virtual bool load(); 55 virtual bool load();
56 virtual bool save( Ticket * ); 56 virtual bool save( Ticket * );
57 57
58 virtual void removeAddressee( const Addressee& addr ); 58 virtual void removeAddressee( const Addressee& addr );
59 59
60 void setUser( const QString &user ); 60 void setUser( const QString &user );
61 QString user() const; 61 QString user() const;
62 62
63 void setPassword( const QString &password ); 63 void setPassword( const QString &password );
64 QString password() const; 64 QString password() const;
65 65
66 void setDn( const QString &dn ); 66 void setDn( const QString &dn );
67 QString dn() const; 67 QString dn() const;
68 68
69 void setHost( const QString &host ); 69 void setHost( const QString &host );
70 QString host() const; 70 QString host() const;
71 71
72 void setPort( int port ); 72 void setPort( int port );
73 int port() const; 73 int port() const;
74 74
75 void setFilter( const QString &filter ); 75 void setFilter( const QString &filter );
76 QString filter() const; 76 QString filter() const;
77 77
78 void setIsAnonymous( bool value ); 78 void setIsAnonymous( bool value );
79 bool isAnonymous() const; 79 bool isAnonymous() const;
80 80
81 void setAttributes( const QMap<QString, QString> &attributes ); 81 void setAttributes( const QMap<QString, QString> &attributes );
82 QMap<QString, QString> attributes() const; 82 QMap<QString, QString> attributes() const;
83 83
84private: 84private:
85 QString mUser; 85 QString mUser;
86 QString mPassword; 86 QString mPassword;
87 QString mDn; 87 QString mDn;
88 QString mHost; 88 QString mHost;
89 QString mFilter; 89 QString mFilter;
90 int mPort; 90 int mPort;
91 bool mAnonymous; 91 bool mAnonymous;
92 QMap<QString, QString> mAttributes; 92 QMap<QString, QString> mAttributes;
93 93
94 LDAP *mLdap; 94 LDAP *mLdap;
95}; 95};
96 96
97} 97}
98 98
99#endif 99#endif
diff --git a/kabc/plugins/opie/opieE.pro b/kabc/plugins/opie/opieE.pro
index 8d45c3d..912e596 100644
--- a/kabc/plugins/opie/opieE.pro
+++ b/kabc/plugins/opie/opieE.pro
@@ -1,31 +1,31 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on 2CONFIG += qt warn_on
3#release debug 3#release debug
4TARGET = microkabc_opie 4TARGET = microkabc_opie
5 5
6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../qtcompat $(QPEDIR)/include $(OPIEDIR)/include 6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../microkde/kresources ../../../qtcompat $(QPEDIR)/include $(OPIEDIR)/include
7 7
8 8
9OBJECTS_DIR = obj/$(PLATFORM) 9OBJECTS_DIR = obj/$(PLATFORM)
10MOC_DIR = moc/$(PLATFORM) 10MOC_DIR = moc/$(PLATFORM)
11DESTDIR = $(QPEDIR)/lib 11DESTDIR = $(QPEDIR)/lib
12LIBS += -lmicrokde 12LIBS += -lmicrokde
13LIBS += -lmicrokabc 13LIBS += -lmicrokabc
14LIBS += -L$(QPEDIR)/lib 14LIBS += -L$(QPEDIR)/lib
15LIBS += -L$(OPIEDIR)/lib 15LIBS += -L$(OPIEDIR)/lib
16LIBS += -lopie 16LIBS += -lopie
17LIBS += -lqpe 17LIBS += -lqpe
18LIBS += -lqte 18LIBS += -lqte
19 19
20INTERFACES = \ 20INTERFACES = \
21 21
22HEADERS = \ 22HEADERS = \
23 resourceopie.h \ 23 resourceopie.h \
24 resourceopieconfig.h \ 24 resourceopieconfig.h \
25 opieconverter.h \ 25 opieconverter.h \
26 26
27SOURCES = \ 27SOURCES = \
28 resourceopie.cpp \ 28 resourceopie.cpp \
29 resourceopieconfig.cpp \ 29 resourceopieconfig.cpp \
30 opieconverter.cpp \ 30 opieconverter.cpp \
31 31
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
@@ -1,395 +1,391 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$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 36
37#include <kapplication.h> 37#include <kapplication.h>
38#include <kconfig.h> 38#include <kconfig.h>
39#include <kdebug.h> 39#include <kdebug.h>
40#include <klocale.h> 40#include <klocale.h>
41//US #include <ksavefile.h> 41//US #include <ksavefile.h>
42#include <kstandarddirs.h> 42#include <kstandarddirs.h>
43#include <kmessagebox.h> 43#include <kmessagebox.h>
44 44
45//US #include "formatfactory.h" 45//US #include "formatfactory.h"
46//US #include <qpe/qpeapplication.h> 46//US #include <qpe/qpeapplication.h>
47 47
48#include <opie/ocontactaccess.h> 48#include <opie/ocontactaccess.h>
49#include <opie/ocontactaccessbackend_xml.h> 49#include <opie/ocontactaccessbackend_xml.h>
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
56#include "resourceopie.h" 57#include "resourceopie.h"
57 58
58using namespace KABC; 59using namespace KABC;
59extern "C" 60extern "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
72 KConfig *cfg = (KConfig *)config; 73 KConfig *cfg = (KConfig *)config;
73 if ( cfg ) { 74 if ( cfg ) {
74 fileName = cfg->readEntry( "FileName", fileName ); 75 fileName = cfg->readEntry( "FileName", fileName );
75 76
76 } 77 }
77 78
78// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() ); 79// qDebug("ResourceOpie::ResourceOpie : %s", fileName.latin1() );
79 80
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 );
88} 89}
89 90
90void ResourceOpie::init( const QString &fileName ) 91void ResourceOpie::init( const QString &fileName )
91{ 92{
92 93
93 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 94 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
94 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 95 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
95 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 96 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
96 97
97 98
98 setFileName( fileName ); 99 setFileName( fileName );
99 100
100} 101}
101 102
102ResourceOpie::~ResourceOpie() 103ResourceOpie::~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 );
130} 134}
131 135
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
145 return ok;
146 } else {
147 if ( !file.open( IO_ReadWrite ) )
148 return false;
149
150 if ( file.size() == 0 ) {
151 file.close();
152 return true;
153 }
154 140
155//US bool ok = mFormat->checkFormat( &file ); 141 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", fileName() );
156 bool ok = true; 142 mAccess = new OContactAccess("KDEPim/Pi", 0l, backend, false);
157
158 file.close();
159 143
160 return ok; 144 if ( !mAccess ) {
145 qDebug("Unable to load file() %s", fileName().latin1());
146 return false;
161 } 147 }
162*/
163 qDebug("ResourceOpie::doOpen() has to be fixed - %s", mFileName.latin1());
164 return true;
165}
166 148
167void ResourceOpie::doClose() 149 mAccess->setReadAhead( 32 ); // Use ReadAhead-Cache if available
168{
169// qDebug("ResourceOpie::doClose() %s", mFileName.latin1());
170}
171 150
172bool ResourceOpie::load()
173{
174// qDebug("ResourceOpie::load() %s", mFileName.latin1());
175 kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl;
176
177 qDebug("ResourceOpie::load: Try to load file() %s", mFileName.latin1());
178
179 OContactAccessBackend_XML* backend = new OContactAccessBackend_XML( "KDEPim/Pi", mFileName );
180 OContactAccess* access = new OContactAccess("KDEPim/Pi", 0l, backend, false);
181
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 }
187
188 access -> setReadAhead( 32 ); // Use ReadAhead-Cache if available
189 151
190 bool res = false;
191 if (mConverter == 0) 152 if (mConverter == 0)
192 { 153 {
193 mConverter = new OpieConverter(); 154 mConverter = new OpieConverter();
194 res = mConverter->init(); 155 bool res = mConverter->init();
195 if ( !res ) 156 if ( !res )
196 { 157 {
197 qDebug("Unable to initialize opie converter. Most likely a problem with the category file"); 158 qDebug("Unable to initialize opie converter. Most likely a problem with the category file");
198 addressBook()->error( i18n( "Unable to initialize opie converter. Most likely a problem with the category file" ) ); 159 delete mAccess;
199 delete access; 160 mAccess = 0;
200 return false; 161 return false;
201 } 162 }
202 } 163 }
203 164
204 165
205 OContactAccess::List::Iterator it; 166
206 OContactAccess::List allList = access->allRecords(); 167 return true;
168}
169
170void ResourceOpie::doClose()
171{
172 qDebug("ResourceOpie::doClose: %s", fileName().latin1());
173
174 if(mAccess)
175 {
176 delete mAccess;
177 mAccess = 0;
178 }
179 // it seems so, that deletion of access deletes backend as well
180 //delete backend;
181
182 return;
183
184}
185
186
187bool ResourceOpie::load()
188{
189 qDebug("ResourceOpie::load: %s", fileName().latin1());
190
191 bool res = false;
192
193 OContactAccess::List::Iterator it;
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);
210 198
211 KABC::Addressee addressee; 199 KABC::Addressee addressee;
212 200
213 res = mConverter->opieToAddressee( c, addressee ); 201 res = mConverter->opieToAddressee( c, addressee );
214 202
215 if ( !addressee.isEmpty() && res ) 203 if ( !addressee.isEmpty() && res )
216 { 204 {
217 addressee.setResource( this ); 205 addressee.setResource( this );
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; 211
225 // it seems so, that deletion of access deletes backend as well
226 //delete backend;
227
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 218
235 219
236 qDebug("ResourceOpie::save %s", mFileName.latin1()); 220
237 kdDebug(5700) << "ResourceOpie::save()" << endl; 221 KABC::AddressBook::Iterator it;
238 222 bool res;
239 // create backup file 223
240 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 224 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
241*/ 225 OContact c;
242/*US we use a simpler method to create a backupfile 226 KABC::Addressee addressee = (*it);
243 227
244 (void) KSaveFile::backupFile( mFileName, QString::null 228 res = mConverter->addresseeToOpie( *it, c );
245 ,extension ); 229 if (res == true)
246 230 {
247 KSaveFile saveFile( mFileName ); 231 res = mAccess->add(c);
248 bool ok = false; 232 if (res == false)
249 if ( saveFile.status() == 0 && saveFile.file() ) 233 qDebug("Unable to append Contact %s", c.fullName().latin1());
250 { 234 }
251 mFormat->saveAll( addressBook(), this, saveFile.file() ); 235 else
252 ok = saveFile.close(); 236 {
253 } 237 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1());
254*/ 238 }
255/*US
256//US ToDo: write backupfile
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 }
266 else {
267
268 } 239 }
269 240
270 if ( !ok ) 241 mAccess->save();
271 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 242
272
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("/"), "_" );
293//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 262//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
294 263
295 KURL url(fn); 264 KURL url(fn);
296 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 265 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
297 266
298 kdDebug(5700) << "-- lock name: " << lockName << endl; 267 kdDebug(5700) << "-- lock name: " << lockName << endl;
299 268
300 if (QFile::exists( lockName )) return false; 269 if (QFile::exists( lockName )) return false;
301 270
302 QString lockUniqueName; 271 QString lockUniqueName;
303 lockUniqueName = fn + KApplication::randomString( 8 ); 272 lockUniqueName = fn + KApplication::randomString( 8 );
304 273
305 url = lockUniqueName; 274 url = lockUniqueName;
306//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 275//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
307 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 276 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
308 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 277 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
309 278
310 // Create unique file 279 // Create unique file
311 QFile file( mLockUniqueName ); 280 QFile file( mLockUniqueName );
312 file.open( IO_WriteOnly ); 281 file.open( IO_WriteOnly );
313 file.close(); 282 file.close();
314 283
315 // Create lock file 284 // Create lock file
316 int result = 0; 285 int result = 0;
317#ifndef _WIN32_ 286#ifndef _WIN32_
318 result = ::link( QFile::encodeName( mLockUniqueName ), 287 result = ::link( QFile::encodeName( mLockUniqueName ),
319 QFile::encodeName( lockName ) ); 288 QFile::encodeName( lockName ) );
320#endif 289#endif
321 if ( result == 0 ) { 290 if ( result == 0 ) {
322 addressBook()->emitAddressBookLocked(); 291 addressBook()->emitAddressBookLocked();
323 return true; 292 return true;
324 } 293 }
325 294
326 // TODO: check stat 295 // TODO: check stat
327 296
328 return false; 297 return false;
329} 298}
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
337//US fn.replace( QRegExp( "/" ), "_" ); 306//US fn.replace( QRegExp( "/" ), "_" );
338//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 307//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
339//US QString lockName = fn + ".lock"; 308//US QString lockName = fn + ".lock";
340 KURL url(fn); 309 KURL url(fn);
341 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 310 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
342 311
343 QFile::remove( lockName ); 312 QFile::remove( lockName );
344 QFile::remove( mLockUniqueName ); 313 QFile::remove( mLockUniqueName );
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{
369 // There is a small theoretical chance that KDirWatch calls us before 332 // There is a small theoretical chance that KDirWatch calls us before
370 // we are fully constructed 333 // we are fully constructed
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();
378 } 341 }
379 342
380 load(); 343 load();
381 addressBook()->emitAddressBookChanged(); 344 addressBook()->emitAddressBookChanged();
382} 345}
383 346
384void ResourceOpie::removeAddressee( const Addressee &addr ) 347void ResourceOpie::removeAddressee( const Addressee &addr )
385{ 348{
386} 349}
387 350
388void ResourceOpie::cleanUp() 351void 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() );
356}
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;
393} 387}
394 388
389
390
395//US #include "resourceopie.moc" 391//US #include "resourceopie.moc"
diff --git a/kabc/plugins/opie/resourceopie.h b/kabc/plugins/opie/resourceopie.h
index 9db9485..ca30fee 100644
--- a/kabc/plugins/opie/resourceopie.h
+++ b/kabc/plugins/opie/resourceopie.h
@@ -1,152 +1,156 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28 28
29#ifndef KABC_RESOURCEOPIE_H 29#ifndef KABC_RESOURCEOPIE_H
30#define KABC_RESOURCEOPIE_H 30#define KABC_RESOURCEOPIE_H
31 31
32#include <kconfig.h> 32#include <kconfig.h>
33#include <kdirwatch.h> 33#include <kdirwatch.h>
34 34
35#include <sys/types.h> 35#include <sys/types.h>
36 36
37#include "resource.h" 37#include "resource.h"
38 38
39 39
40//class QTimer; 40class OContactAccess;
41//class FormatPlugin;
42 41
43namespace KABC { 42namespace KABC {
44 43
45//US class FormatPlugin;
46class ResourceConfigWidget; 44class ResourceConfigWidget;
47class OpieConverter; 45class OpieConverter;
48/** 46/**
49 @internal 47 @internal
50*/ 48*/
51class ResourceOpie : public Resource 49class ResourceOpie : public Resource
52{ 50{
53 Q_OBJECT 51 Q_OBJECT
54 52
55public: 53public:
56 54
57 /** 55 /**
58 Constructor. 56 Constructor.
59 57
60 @param cfg The config object where custom resource settings are stored. 58 @param cfg The config object where custom resource settings are stored.
61 */ 59 */
62 ResourceOpie( const KConfig *cfg ); 60 ResourceOpie( const KConfig *cfg, bool syncable );
63 61
64 /** 62 /**
65 Construct file resource on file @arg fileName using format @arg formatName. 63 Construct file resource on file @arg fileName using format @arg formatName.
66 */ 64 */
67 ResourceOpie( const QString &fileName ); 65 ResourceOpie( const QString &fileName, bool syncable );
68 66
69 /** 67 /**
70 * Destructor. 68 * Destructor.
71 */ 69 */
72 ~ResourceOpie(); 70 ~ResourceOpie();
73 71
74 /** 72 /**
75 Writes the config back. 73 Writes the config back.
76 */ 74 */
77 virtual void writeConfig( KConfig *cfg ); 75 virtual void writeConfig( KConfig *cfg );
78 76
79 /** 77 /**
80 * Tries to open the file and checks for the proper format. 78 * Tries to open the file and checks for the proper format.
81 * This method should be called before @ref load(). 79 * This method should be called before @ref load().
82 */ 80 */
83 virtual bool doOpen(); 81 virtual bool doOpen();
84 82
85 /** 83 /**
86 * Closes the file again. 84 * Closes the file again.
87 */ 85 */
88 virtual void doClose(); 86 virtual void doClose();
89 87
90 /** 88 /**
91 * Requests a save ticket, that is used by @ref save() 89 * Requests a save ticket, that is used by @ref save()
92 */ 90 */
93 virtual Ticket *requestSaveTicket(); 91 virtual Ticket *requestSaveTicket();
94 92
95 /** 93 /**
96 * Loads all addressees from file to the address book. 94 * Loads all addressees from file to the address book.
97 * Returns true if all addressees could be loaded otherwise false. 95 * Returns true if all addressees could be loaded otherwise false.
98 */ 96 */
99 virtual bool load(); 97 virtual bool load();
100 98
101 /** 99 /**
102 * Saves all addresses from address book to file. 100 * Saves all addresses from address book to file.
103 * Returns true if all addressees could be saved otherwise false. 101 * Returns true if all addressees could be saved otherwise false.
104 * 102 *
105 * @param ticket The ticket returned by @ref requestSaveTicket() 103 * @param ticket The ticket returned by @ref requestSaveTicket()
106 */ 104 */
107 virtual bool save( Ticket *ticket ); 105 virtual bool save( Ticket *ticket );
108 106
109 /**
110 * Set name of file to be used for saving.
111 */
112 void setFileName( const QString & );
113
114 /**
115 * Return name of file used for loading and saving the address book.
116 */
117 QString fileName() const;
118 107
119 /** 108 /**
120 * Remove a addressee from its source. 109 * Remove a addressee from its source.
121 * This method is mainly called by KABC::AddressBook. 110 * This method is mainly called by KABC::AddressBook.
122 */ 111 */
123 virtual void removeAddressee( const Addressee& addr ); 112 virtual void removeAddressee( const Addressee& addr );
124 113
125 /** 114 /**
115 * Set name of file to be used for saving.
116 */
117 virtual void setFileName( const QString & );
118
119 /**
126 * This method is called by an error handler if the application 120 * This method is called by an error handler if the application
127 * crashed 121 * crashed
128 */ 122 */
129 virtual void cleanUp(); 123 virtual void cleanUp();
130 124
125 /**
126 * This method returns the number of elements that are currently in the resource.
127 */
128 virtual int count() const;
129
130 /**
131 * This method removes all elements from the resource!! (Not from the addressbook)
132 */
133 virtual bool clear();
134
135
131protected slots: 136protected slots:
132 void fileChanged(); 137 void fileChanged();
133 138
134protected: 139protected:
135 void init( const QString &fileName ); 140 void init( const QString &fileName );
136 141
137 bool lock( const QString &fileName ); 142 bool lock( const QString &fileName );
138 void unlock( const QString &fileName ); 143 void unlock( const QString &fileName );
139 144
140private: 145private:
146 OContactAccess* mAccess;
141 OpieConverter* mConverter; 147 OpieConverter* mConverter;
142
143 QString mFileName;
144 148
145 QString mLockUniqueName; 149 QString mLockUniqueName;
146 150
147 KDirWatch mDirWatch; 151 KDirWatch mDirWatch;
148}; 152};
149 153
150} 154}
151 155
152#endif 156#endif
diff --git a/kabc/plugins/qtopia/qtopiaE.pro b/kabc/plugins/qtopia/qtopiaE.pro
index 56eae87..d956519 100644
--- a/kabc/plugins/qtopia/qtopiaE.pro
+++ b/kabc/plugins/qtopia/qtopiaE.pro
@@ -1,27 +1,27 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on 2CONFIG += qt warn_on
3 3
4TARGET = microkabc_qtopia 4TARGET = microkabc_qtopia
5 5
6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../qtcompat $(QPEDIR)/include 6INCLUDEPATH += ../.. ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../microkde/kresources ../../../qtcompat $(QPEDIR)/include
7 7
8OBJECTS_DIR = obj/$(PLATFORM) 8OBJECTS_DIR = obj/$(PLATFORM)
9MOC_DIR = moc/$(PLATFORM) 9MOC_DIR = moc/$(PLATFORM)
10DESTDIR = $(QPEDIR)/lib 10DESTDIR = $(QPEDIR)/lib
11LIBS += -lmicrokde 11LIBS += -lmicrokde
12LIBS += -lmicrokabc 12LIBS += -lmicrokabc
13LIBS += -L$(QPEDIR)/lib 13LIBS += -L$(QPEDIR)/lib
14LIBS += -lqpepim 14LIBS += -lqpepim
15LIBS += -lqpe 15LIBS += -lqpe
16 16
17INTERFACES = \ 17INTERFACES = \
18 18
19HEADERS = \ 19HEADERS = \
20 resourceqtopia.h \ 20 resourceqtopia.h \
21 resourceqtopiaconfig.h \ 21 resourceqtopiaconfig.h \
22 qtopiaconverter.h 22 qtopiaconverter.h
23 23
24SOURCES = \ 24SOURCES = \
25 resourceqtopia.cpp \ 25 resourceqtopia.cpp \
26 resourceqtopiaconfig.cpp \ 26 resourceqtopiaconfig.cpp \
27 qtopiaconverter.cpp 27 qtopiaconverter.cpp
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
@@ -1,357 +1,383 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27#include <sys/types.h> 27#include <sys/types.h>
28#include <sys/stat.h> 28#include <sys/stat.h>
29#include <unistd.h> 29#include <unistd.h>
30 30
31#include <qdir.h> 31#include <qdir.h>
32#include <qfile.h> 32#include <qfile.h>
33#include <qfileinfo.h> 33#include <qfileinfo.h>
34#include <qregexp.h> 34#include <qregexp.h>
35//US #include <qtimer.h> 35//US #include <qtimer.h>
36 36
37#include <kapplication.h> 37#include <kapplication.h>
38#include <kconfig.h> 38#include <kconfig.h>
39#include <kdebug.h> 39#include <kdebug.h>
40#include <klocale.h> 40#include <klocale.h>
41//US #include <ksavefile.h> 41//US #include <ksavefile.h>
42#include <kstandarddirs.h> 42#include <kstandarddirs.h>
43#include <kmessagebox.h> 43#include <kmessagebox.h>
44 44
45#include <qpe/pim/addressbookaccess.h> 45#include <qpe/pim/addressbookaccess.h>
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 )
81{ 82{
82 83
83 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 84 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
84 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 85 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
85 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 86 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
86 87
87 setFileName( fileName ); 88 setFileName( fileName );
88} 89}
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}
101 104
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
122 if ( !file.exists() ) {
123 // try to create the file
124 bool ok = file.open( IO_WriteOnly );
125 if ( ok )
126 file.close();
127
128 return ok;
129 } else {
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 123
153bool ResourceQtopia::load() 124 mAccess = new AddressBookAccess();
154{
155 kdDebug(5700) << "ResourceQtopia::load(): '" << mFileName << "'" << endl;
156
157// qDebug("ResourceQtopia::load: Try to load file() %s", mFileName.latin1());
158 125
159 AddressBookAccess* access = new AddressBookAccess(); 126 if ( !mAccess ) {
160 127 qDebug("Unable to load file() %s", fileName().latin1());
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}
184 const PimContact* contact;
185 148
186 for (contact=it.toFirst(); it.current(); ++it) { 149void ResourceQtopia::doClose()
187 contact = it.current(); 150{
151 qDebug("ResourceQtopia::doClose: %s", fileName().latin1());
188 152
189 KABC::Addressee addressee; 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());
190 167
191 res = mConverter->qtopiaToAddressee( (*contact), addressee ); 168 AddressBookIterator it(*mAccess);
169 const PimContact* contact;
170 bool res;
171
172 for (contact=it.toFirst(); it.current(); ++it)
173 {
174 contact = it.current();
192 175
193 if ( !addressee.isEmpty() && res ) 176 KABC::Addressee addressee;
194 { 177
195 addressee.setResource( this ); 178 res = mConverter->qtopiaToAddressee( (*contact), addressee );
196 addressBook()->insertAddressee( addressee ); 179
197 } 180 if ( !addressee.isEmpty() && res )
181 {
182 addressee.setResource( this );
183 addressBook()->insertAddressee( addressee );
198 } 184 }
199 } 185 }
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
208// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 193// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
209 kdDebug(5700) << "ResourceQtopia::save()" << endl; 194 kdDebug(5700) << "ResourceQtopia::save()" << endl;
210 195
211 // create backup file 196 // create backup file
212 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 197 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
213 198
214/*US we use a simpler method to create a backupfile 199/*US we use a simpler method to create a backupfile
215 200
216 (void) KSaveFile::backupFile( mFileName, QString::null 201 (void) KSaveFile::backupFile( mFileName, QString::null
217 ,extension ); 202 ,extension );
218 203
219 KSaveFile saveFile( mFileName ); 204 KSaveFile saveFile( mFileName );
220 bool ok = false; 205 bool ok = false;
221 if ( saveFile.status() == 0 && saveFile.file() ) 206 if ( saveFile.status() == 0 && saveFile.file() )
222 { 207 {
223 mFormat->saveAll( addressBook(), this, saveFile.file() ); 208 mFormat->saveAll( addressBook(), this, saveFile.file() );
224 ok = saveFile.close(); 209 ok = saveFile.close();
225 } 210 }
226*/ 211*/
227 212
228/*US 213/*US
229//US ToDo: write backupfile 214//US ToDo: write backupfile
230 QFile info; 215 QFile info;
231 info.setName( mFileName ); 216 info.setName( mFileName );
232 bool ok = info.open( IO_WriteOnly ); 217 bool ok = info.open( IO_WriteOnly );
233 if ( ok ) { 218 if ( ok ) {
234//US mFormat->saveAll( addressBook(), this, &info ); 219//US mFormat->saveAll( addressBook(), this, &info );
235 220
236 info.close(); 221 info.close();
237 ok = true; 222 ok = true;
238 } 223 }
239 else { 224 else {
240 225
241 } 226 }
242 227
243 if ( !ok ) 228 if ( !ok )
244 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 229 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
245 230
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;
259 249
260 QString fn = fileName; 250 QString fn = fileName;
261 251
262//US change the implementation how the lockfilename is getting created 252//US change the implementation how the lockfilename is getting created
263//US fn.replace( QRegExp("/"), "_" ); 253//US fn.replace( QRegExp("/"), "_" );
264//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 254//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
265 255
266 KURL url(fn); 256 KURL url(fn);
267 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 257 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
268 258
269 kdDebug(5700) << "-- lock name: " << lockName << endl; 259 kdDebug(5700) << "-- lock name: " << lockName << endl;
270 260
271 if (QFile::exists( lockName )) return false; 261 if (QFile::exists( lockName )) return false;
272 262
273 QString lockUniqueName; 263 QString lockUniqueName;
274 lockUniqueName = fn + KApplication::randomString( 8 ); 264 lockUniqueName = fn + KApplication::randomString( 8 );
275 265
276 url = lockUniqueName; 266 url = lockUniqueName;
277//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 267//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
278 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 268 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
279 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 269 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
280 270
281 // Create unique file 271 // Create unique file
282 QFile file( mLockUniqueName ); 272 QFile file( mLockUniqueName );
283 file.open( IO_WriteOnly ); 273 file.open( IO_WriteOnly );
284 file.close(); 274 file.close();
285 275
286 // Create lock file 276 // Create lock file
287 int result = ::link( QFile::encodeName( mLockUniqueName ), 277 int result = ::link( QFile::encodeName( mLockUniqueName ),
288 QFile::encodeName( lockName ) ); 278 QFile::encodeName( lockName ) );
289 279
290 if ( result == 0 ) { 280 if ( result == 0 ) {
291 addressBook()->emitAddressBookLocked(); 281 addressBook()->emitAddressBookLocked();
292 return true; 282 return true;
293 } 283 }
294 284
295 // TODO: check stat 285 // TODO: check stat
296 286
297 return false; 287 return false;
298} 288}
299 289
300void ResourceQtopia::unlock( const QString &fileName ) 290void ResourceQtopia::unlock( const QString &fileName )
301{ 291{
302 QString fn = fileName; 292 QString fn = fileName;
303//US change the implementation how the lockfilename is getting created 293//US change the implementation how the lockfilename is getting created
304//US fn.replace( QRegExp( "/" ), "_" ); 294//US fn.replace( QRegExp( "/" ), "_" );
305//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 295//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
306//US QString lockName = fn + ".lock"; 296//US QString lockName = fn + ".lock";
307 KURL url(fn); 297 KURL url(fn);
308 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 298 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
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() );
355} 338}
356 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;
359}
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"
diff --git a/kabc/plugins/qtopia/resourceqtopia.h b/kabc/plugins/qtopia/resourceqtopia.h
index bf9dd45..90ab1f4 100644
--- a/kabc/plugins/qtopia/resourceqtopia.h
+++ b/kabc/plugins/qtopia/resourceqtopia.h
@@ -1,150 +1,155 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28 28
29#ifndef KABC_RESOURCEOPIE_H 29#ifndef KABC_RESOURCEOPIE_H
30#define KABC_RESOURCEOPIE_H 30#define KABC_RESOURCEOPIE_H
31 31
32#include <kconfig.h> 32#include <kconfig.h>
33#include <kdirwatch.h> 33#include <kdirwatch.h>
34 34
35#include <sys/types.h> 35#include <sys/types.h>
36 36
37#include "resource.h" 37#include "resource.h"
38 38
39class AddressBookAccess;
39 40
40namespace KABC { 41namespace KABC {
41 42
42//US class FormatPlugin; 43//US class FormatPlugin;
43class ResourceConfigWidget; 44class ResourceConfigWidget;
44class QtopiaConverter; 45class QtopiaConverter;
45 46
46/** 47/**
47 @internal 48 @internal
48*/ 49*/
49class ResourceQtopia : public Resource 50class ResourceQtopia : public Resource
50{ 51{
51 Q_OBJECT 52 Q_OBJECT
52 53
53public: 54public:
54 55
55 /** 56 /**
56 Constructor. 57 Constructor.
57 58
58 @param cfg The config object where custom resource settings are stored. 59 @param cfg The config object where custom resource settings are stored.
59 */ 60 */
60 ResourceQtopia( const KConfig *cfg ); 61 ResourceQtopia( const KConfig *cfg, bool syncable );
61 62
62 /** 63 /**
63 Construct file resource on file @arg fileName using format @arg formatName. 64 Construct file resource on file @arg fileName using format @arg formatName.
64 */ 65 */
65 ResourceQtopia( const QString &fileName ); 66 ResourceQtopia( const QString &fileName, bool syncable );
66 67
67 /** 68 /**
68 * Destructor. 69 * Destructor.
69 */ 70 */
70 ~ResourceQtopia(); 71 ~ResourceQtopia();
71 72
72 /** 73 /**
73 Writes the config back. 74 Writes the config back.
74 */ 75 */
75 virtual void writeConfig( KConfig *cfg ); 76 virtual void writeConfig( KConfig *cfg );
76 77
77 /** 78 /**
78 * Tries to open the file and checks for the proper format. 79 * Tries to open the file and checks for the proper format.
79 * This method should be called before @ref load(). 80 * This method should be called before @ref load().
80 */ 81 */
81 virtual bool doOpen(); 82 virtual bool doOpen();
82 83
83 /** 84 /**
84 * Closes the file again. 85 * Closes the file again.
85 */ 86 */
86 virtual void doClose(); 87 virtual void doClose();
87 88
88 /** 89 /**
89 * Requests a save ticket, that is used by @ref save() 90 * Requests a save ticket, that is used by @ref save()
90 */ 91 */
91 virtual Ticket *requestSaveTicket(); 92 virtual Ticket *requestSaveTicket();
92 93
93 /** 94 /**
94 * Loads all addressees from file to the address book. 95 * Loads all addressees from file to the address book.
95 * Returns true if all addressees could be loaded otherwise false. 96 * Returns true if all addressees could be loaded otherwise false.
96 */ 97 */
97 virtual bool load(); 98 virtual bool load();
98 99
99 /** 100 /**
100 * Saves all addresses from address book to file. 101 * Saves all addresses from address book to file.
101 * Returns true if all addressees could be saved otherwise false. 102 * Returns true if all addressees could be saved otherwise false.
102 * 103 *
103 * @param ticket The ticket returned by @ref requestSaveTicket() 104 * @param ticket The ticket returned by @ref requestSaveTicket()
104 */ 105 */
105 virtual bool save( Ticket *ticket ); 106 virtual bool save( Ticket *ticket );
106 107
107 /** 108 /**
109 * Remove a addressee from its source.
110 * This method is mainly called by KABC::AddressBook.
111 */
112 virtual void removeAddressee( const Addressee& addr );
113
114 /**
108 * Set name of file to be used for saving. 115 * Set name of file to be used for saving.
109 */ 116 */
110 void setFileName( const QString & ); 117 virtual void setFileName( const QString & );
111 118
112 /** 119 /**
113 * Return name of file used for loading and saving the address book. 120 * This method is called by an error handler if the application
121 * crashed
114 */ 122 */
115 QString fileName() const; 123 virtual void cleanUp();
116 124
117 /** 125 /**
118 * Remove a addressee from its source. 126 * This method returns the number of elements that are currently in the resource.
119 * This method is mainly called by KABC::AddressBook.
120 */ 127 */
121 virtual void removeAddressee( const Addressee& addr ); 128 virtual int count() const;
122 129
123 /** 130 /**
124 * This method is called by an error handler if the application 131 * This method removes all elements from the resource!! (Not from the addressbook)
125 * crashed
126 */ 132 */
127 virtual void cleanUp(); 133 virtual bool clear();
128 134
129protected slots: 135protected slots:
130 void fileChanged(); 136 void fileChanged();
131 137
132protected: 138protected:
133 void init( const QString &fileName ); 139 void init( const QString &fileName );
134 140
135 bool lock( const QString &fileName ); 141 bool lock( const QString &fileName );
136 void unlock( const QString &fileName ); 142 void unlock( const QString &fileName );
137 143
138private: 144private:
145 AddressBookAccess* mAccess;
139 QtopiaConverter* mConverter; 146 QtopiaConverter* mConverter;
140
141 QString mFileName;
142 147
143 QString mLockUniqueName; 148 QString mLockUniqueName;
144 149
145 KDirWatch mDirWatch; 150 KDirWatch mDirWatch;
146}; 151};
147 152
148} 153}
149 154
150#endif 155#endif
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
index 31b99ca..7b909de 100644
--- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
+++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp
@@ -1,357 +1,382 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2004 Ulf Schenk
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$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 "resourcesharpdtm.h" 50#include "resourcesharpdtm.h"
51 51
52#include "stdaddressbook.h" 52#include "stdaddressbook.h"
53 53
54#include "sharpdtmconverter.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, bool syncable )
66 : Resource( config ), mConverter (0) 66 : Resource( config, syncable ), 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, bool syncable )
75 : Resource( 0 ) 75 : Resource( 0, syncable )
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;
95 95
96} 96}
97 97
98void ResourceSharpDTM::writeConfig( KConfig *config ) 98void ResourceSharpDTM::writeConfig( KConfig *config )
99{ 99{
100 Resource::writeConfig( config ); 100 Resource::writeConfig( config );
101} 101}
102 102
103Ticket *ResourceSharpDTM::requestSaveTicket() 103Ticket *ResourceSharpDTM::requestSaveTicket()
104{ 104{
105 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; 105 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl;
106 106
107 if ( !addressBook() ) return 0; 107 if ( !addressBook() ) return 0;
108 108
109 if ( !lock( mFileName ) ) { 109 if ( !lock( mFileName ) ) {
110 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" 110 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '"
111 << mFileName << "'" << endl; 111 << mFileName << "'" << endl;
112 return 0; 112 return 0;
113 } 113 }
114 return createTicket( this ); 114 return createTicket( this );
115} 115}
116 116
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 // the last parameter in the SlZDataBase constructor means "readonly"
161 SlZDataBase* access = new SlZDataBase(mFileName, 161 SlZDataBase* access = new SlZDataBase(mFileName,
162 SlZDataBase::addressbookItems(), 162 SlZDataBase::addressbookItems(),
163 NULL, true); 163 NULL, true);
164 if ( !access ) { 164 if ( !access ) {
165 qDebug("Unable to load file() %s", mFileName.latin1()); 165 qDebug("Unable to load file() %s", mFileName.latin1());
166 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) ); 166 addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) );
167 return false; 167 return false;
168 } 168 }
169 169
170 bool res = false; 170 bool res = false;
171 if (mConverter == 0) 171 if (mConverter == 0)
172 { 172 {
173 mConverter = new SharpDTMConverter(); 173 mConverter = new SharpDTMConverter();
174 res = mConverter->init(); 174 res = mConverter->init();
175 if ( !res ) 175 if ( !res )
176 { 176 {
177 QString msg("Unable to initialize sharp 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");
178 178
179 qDebug(msg); 179 qDebug(msg);
180 addressBook()->error( i18n( msg ) ); 180 addressBook()->error( i18n( msg ) );
181 delete access; 181 delete access;
182 return false; 182 return false;
183 } 183 }
184 } 184 }
185 185
186 { //create a new scope 186 { //create a new scope
187 CardId id; 187 CardId id;
188 188
189 for (bool res=access->first(); res == true; res=access->next()) { 189 for (bool res=access->first(); res == true; res=access->next()) {
190 id = access->cardId(); 190 id = access->cardId();
191 191
192 KABC::Addressee addressee; 192 KABC::Addressee addressee;
193 193
194 res = mConverter->sharpToAddressee( id, access, addressee ); 194 res = mConverter->sharpToAddressee( id, access, addressee );
195 195
196 if ( !addressee.isEmpty() && res ) 196 if ( !addressee.isEmpty() && res )
197 { 197 {
198 addressee.setResource( this ); 198 addressee.setResource( this );
199 addressBook()->insertAddressee( addressee ); 199 addressBook()->insertAddressee( addressee );
200 } 200 }
201 } 201 }
202 } 202 }
203 203
204 delete access; 204 delete access;
205 return true; 205 return true;
206} 206}
207 207
208bool ResourceSharpDTM::save( Ticket *ticket ) 208bool ResourceSharpDTM::save( Ticket *ticket )
209{ 209{
210/*US 210/*US
211// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); 211// qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1());
212 kdDebug(5700) << "ResourceQtopia::save()" << endl; 212 kdDebug(5700) << "ResourceQtopia::save()" << endl;
213 213
214 // create backup file 214 // create backup file
215 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); 215 QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() );
216 216
217/*US we use a simpler method to create a backupfile 217/*US we use a simpler method to create a backupfile
218 218
219 (void) KSaveFile::backupFile( mFileName, QString::null 219 (void) KSaveFile::backupFile( mFileName, QString::null
220 ,extension ); 220 ,extension );
221 221
222 KSaveFile saveFile( mFileName ); 222 KSaveFile saveFile( mFileName );
223 bool ok = false; 223 bool ok = false;
224 if ( saveFile.status() == 0 && saveFile.file() ) 224 if ( saveFile.status() == 0 && saveFile.file() )
225 { 225 {
226 mFormat->saveAll( addressBook(), this, saveFile.file() ); 226 mFormat->saveAll( addressBook(), this, saveFile.file() );
227 ok = saveFile.close(); 227 ok = saveFile.close();
228 } 228 }
229*/ 229*/
230 230
231/*US 231/*US
232//US ToDo: write backupfile 232//US ToDo: write backupfile
233 QFile info; 233 QFile info;
234 info.setName( mFileName ); 234 info.setName( mFileName );
235 bool ok = info.open( IO_WriteOnly ); 235 bool ok = info.open( IO_WriteOnly );
236 if ( ok ) { 236 if ( ok ) {
237//US mFormat->saveAll( addressBook(), this, &info ); 237//US mFormat->saveAll( addressBook(), this, &info );
238 238
239 info.close(); 239 info.close();
240 ok = true; 240 ok = true;
241 } 241 }
242 else { 242 else {
243 243
244 } 244 }
245 245
246 if ( !ok ) 246 if ( !ok )
247 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); 247 addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) );
248 248
249 delete ticket; 249 delete ticket;
250 unlock( mFileName ); 250 unlock( mFileName );
251 251
252 return ok; 252 return ok;
253 253
254 qDebug("ResourceQtopia::save has to be changed"); 254 qDebug("ResourceQtopia::save has to be changed");
255*/ 255*/
256 return true; 256 return true;
257} 257}
258 258
259bool ResourceSharpDTM::lock( const QString &fileName ) 259bool ResourceSharpDTM::lock( const QString &fileName )
260{ 260{
261 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; 261 kdDebug(5700) << "ResourceSharpDTM::lock()" << endl;
262 262
263 QString fn = fileName; 263 QString fn = fileName;
264 264
265//US change the implementation how the lockfilename is getting created 265//US change the implementation how the lockfilename is getting created
266//US fn.replace( QRegExp("/"), "_" ); 266//US fn.replace( QRegExp("/"), "_" );
267//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 267//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
268 268
269 KURL url(fn); 269 KURL url(fn);
270 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 270 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
271 271
272 kdDebug(5700) << "-- lock name: " << lockName << endl; 272 kdDebug(5700) << "-- lock name: " << lockName << endl;
273 273
274 if (QFile::exists( lockName )) return false; 274 if (QFile::exists( lockName )) return false;
275 275
276 QString lockUniqueName; 276 QString lockUniqueName;
277 lockUniqueName = fn + KApplication::randomString( 8 ); 277 lockUniqueName = fn + KApplication::randomString( 8 );
278 278
279 url = lockUniqueName; 279 url = lockUniqueName;
280//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 280//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
281 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 281 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
282 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 282 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
283 283
284 // Create unique file 284 // Create unique file
285 QFile file( mLockUniqueName ); 285 QFile file( mLockUniqueName );
286 file.open( IO_WriteOnly ); 286 file.open( IO_WriteOnly );
287 file.close(); 287 file.close();
288 288
289 // Create lock file 289 // Create lock file
290 int result = ::link( QFile::encodeName( mLockUniqueName ), 290 int result = ::link( QFile::encodeName( mLockUniqueName ),
291 QFile::encodeName( lockName ) ); 291 QFile::encodeName( lockName ) );
292 292
293 if ( result == 0 ) { 293 if ( result == 0 ) {
294 addressBook()->emitAddressBookLocked(); 294 addressBook()->emitAddressBookLocked();
295 return true; 295 return true;
296 } 296 }
297 297
298 // TODO: check stat 298 // TODO: check stat
299 299
300 return false; 300 return false;
301} 301}
302 302
303void ResourceSharpDTM::unlock( const QString &fileName ) 303void ResourceSharpDTM::unlock( const QString &fileName )
304{ 304{
305 QString fn = fileName; 305 QString fn = fileName;
306//US change the implementation how the lockfilename is getting created 306//US change the implementation how the lockfilename is getting created
307//US fn.replace( QRegExp( "/" ), "_" ); 307//US fn.replace( QRegExp( "/" ), "_" );
308//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); 308//US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" );
309//US QString lockName = fn + ".lock"; 309//US QString lockName = fn + ".lock";
310 KURL url(fn); 310 KURL url(fn);
311 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 311 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
312 312
313 QFile::remove( lockName ); 313 QFile::remove( lockName );
314 QFile::remove( mLockUniqueName ); 314 QFile::remove( mLockUniqueName );
315 addressBook()->emitAddressBookUnlocked(); 315 addressBook()->emitAddressBookUnlocked();
316} 316}
317 317
318void ResourceSharpDTM::setFileName( const QString &fileName ) 318void ResourceSharpDTM::setFileName( const QString &fileName )
319{ 319{
320 mDirWatch.stopScan(); 320 mDirWatch.stopScan();
321 mDirWatch.removeFile( mFileName ); 321 mDirWatch.removeFile( mFileName );
322 322
323 mFileName = fileName; 323 mFileName = fileName;
324 324
325 mDirWatch.addFile( mFileName ); 325 mDirWatch.addFile( mFileName );
326 mDirWatch.startScan(); 326 mDirWatch.startScan();
327 327
328} 328}
329 329
330QString ResourceSharpDTM::fileName() const 330QString ResourceSharpDTM::fileName() const
331{ 331{
332 return mFileName; 332 return mFileName;
333} 333}
334 334
335void ResourceSharpDTM::fileChanged() 335void ResourceSharpDTM::fileChanged()
336{ 336{
337 // There is a small theoretical chance that KDirWatch calls us before 337 // There is a small theoretical chance that KDirWatch calls us before
338 // we are fully constructed 338 // we are fully constructed
339 if (!addressBook()) 339 if (!addressBook())
340 return; 340 return;
341 341
342 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 ) );
343 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 343 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
344 load(); 344 load();
345 addressBook()->emitAddressBookChanged(); 345 addressBook()->emitAddressBookChanged();
346 } 346 }
347} 347}
348 348
349void ResourceSharpDTM::removeAddressee( const Addressee &addr ) 349void ResourceSharpDTM::removeAddressee( const Addressee &addr )
350{ 350{
351} 351}
352 352
353void ResourceSharpDTM::cleanUp() 353void ResourceSharpDTM::cleanUp()
354{ 354{
355 unlock( mFileName ); 355 unlock( mFileName );
356} 356}
357 357
358/**
359 * This method returns the number of elements that are currently in the resource.
360 */
361int ResourceSharpDTM::count() const
362{
363 if (mAccess != 0)
364 return mAccess->count();
365 else
366 return 0;
367}
368
369
370/**
371 * This method removes all elements from the resource!! (Not from the addressbook)
372 */
373bool ResourceSharpDTM::clear()
374{
375 if (mAccess != 0)
376 return mAccess->deleteCard(0, SlZDataBase::AllCard );
377 else
378 return false;
379}
380
381
382
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.h b/kabc/plugins/sharpdtm/resourcesharpdtm.h
index d2e2a52..f495e9d 100644
--- a/kabc/plugins/sharpdtm/resourcesharpdtm.h
+++ b/kabc/plugins/sharpdtm/resourcesharpdtm.h
@@ -1,149 +1,153 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> 3 Copyright (c) 2004 Ulf Schenk
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28 28
29#ifndef KABC_RESOURCESHARPDTM_H 29#ifndef KABC_RESOURCESHARPDTM_H
30#define KABC_RESOURCESHARPDTM_H 30#define KABC_RESOURCESHARPDTM_H
31 31
32#include <kconfig.h> 32#include <kconfig.h>
33#include <kdirwatch.h> 33#include <kdirwatch.h>
34 34
35#include <sys/types.h> 35#include <sys/types.h>
36 36
37#include "resource.h" 37#include "resource.h"
38 38
39 39
40namespace KABC { 40namespace KABC {
41 41
42class ResourceConfigWidget; 42class ResourceConfigWidget;
43class SharpDTMConverter; 43class SharpDTMConverter;
44 44
45/** 45/**
46 @internal 46 @internal
47*/ 47*/
48class ResourceSharpDTM : public Resource 48class ResourceSharpDTM : public Resource
49{ 49{
50 Q_OBJECT 50 Q_OBJECT
51 51
52public: 52public:
53 53
54 /** 54 /**
55 Constructor. 55 Constructor.
56 56
57 @param cfg The config object where custom resource settings are stored. 57 @param cfg The config object where custom resource settings are stored.
58 */ 58 */
59 ResourceSharpDTM( const KConfig *cfg ); 59 ResourceSharpDTM( const KConfig *cfg, bool syncable );
60 60
61 /** 61 /**
62 Construct file resource on file @arg fileName using format @arg formatName. 62 Construct file resource on file @arg fileName using format @arg formatName.
63 */ 63 */
64 ResourceSharpDTM( const QString &fileName ); 64 ResourceSharpDTM( const QString &fileName, bool syncable );
65 65
66 /** 66 /**
67 * Destructor. 67 * Destructor.
68 */ 68 */
69 ~ResourceSharpDTM(); 69 ~ResourceSharpDTM();
70 70
71 /** 71 /**
72 Writes the config back. 72 Writes the config back.
73 */ 73 */
74 virtual void writeConfig( KConfig *cfg ); 74 virtual void writeConfig( KConfig *cfg );
75 75
76 /** 76 /**
77 * Tries to open the file and checks for the proper format. 77 * Tries to open the file and checks for the proper format.
78 * This method should be called before @ref load(). 78 * This method should be called before @ref load().
79 */ 79 */
80 virtual bool doOpen(); 80 virtual bool doOpen();
81 81
82 /** 82 /**
83 * Closes the file again. 83 * Closes the file again.
84 */ 84 */
85 virtual void doClose(); 85 virtual void doClose();
86 86
87 /** 87 /**
88 * Requests a save ticket, that is used by @ref save() 88 * Requests a save ticket, that is used by @ref save()
89 */ 89 */
90 virtual Ticket *requestSaveTicket(); 90 virtual Ticket *requestSaveTicket();
91 91
92 /** 92 /**
93 * Loads all addressees from file to the address book. 93 * Loads all addressees from file to the address book.
94 * Returns true if all addressees could be loaded otherwise false. 94 * Returns true if all addressees could be loaded otherwise false.
95 */ 95 */
96 virtual bool load(); 96 virtual bool load();
97 97
98 /** 98 /**
99 * Saves all addresses from address book to file. 99 * Saves all addresses from address book to file.
100 * Returns true if all addressees could be saved otherwise false. 100 * Returns true if all addressees could be saved otherwise false.
101 * 101 *
102 * @param ticket The ticket returned by @ref requestSaveTicket() 102 * @param ticket The ticket returned by @ref requestSaveTicket()
103 */ 103 */
104 virtual bool save( Ticket *ticket ); 104 virtual bool save( Ticket *ticket );
105 105
106 /** 106 /**
107 * Remove a addressee from its source.
108 * This method is mainly called by KABC::AddressBook.
109 */
110 virtual void removeAddressee( const Addressee& addr );
111
112 /**
107 * Set name of file to be used for saving. 113 * Set name of file to be used for saving.
108 */ 114 */
109 void setFileName( const QString & ); 115 virtual void setFileName( const QString & );
110 116
111 /** 117 /**
112 * Return name of file used for loading and saving the address book. 118 * This method is called by an error handler if the application
119 * crashed
113 */ 120 */
114 QString fileName() const; 121 virtual void cleanUp();
115 122
116 /** 123 /**
117 * Remove a addressee from its source. 124 * This method returns the number of elements that are currently in the resource.
118 * This method is mainly called by KABC::AddressBook.
119 */ 125 */
120 virtual void removeAddressee( const Addressee& addr ); 126 virtual int count() const;
121 127
122 /** 128 /**
123 * This method is called by an error handler if the application 129 * This method removes all elements from the resource!! (Not from the addressbook)
124 * crashed
125 */ 130 */
126 virtual void cleanUp(); 131 virtual bool clear();
132
127 133
128protected slots: 134protected slots:
129 void fileChanged(); 135 void fileChanged();
130 136
131protected: 137protected:
132 void init( const QString &fileName ); 138 void init( const QString &fileName );
133 139
134 bool lock( const QString &fileName ); 140 bool lock( const QString &fileName );
135 void unlock( const QString &fileName ); 141 void unlock( const QString &fileName );
136 142
137private: 143private:
138 SharpDTMConverter* mConverter; 144 SharpDTMConverter* mConverter;
139 145
140 QString mFileName;
141
142 QString mLockUniqueName; 146 QString mLockUniqueName;
143 147
144 KDirWatch mDirWatch; 148 KDirWatch mDirWatch;
145}; 149};
146 150
147} 151}
148 152
149#endif 153#endif
diff --git a/kabc/plugins/sharpdtm/sharpdtmE.pro b/kabc/plugins/sharpdtm/sharpdtmE.pro
index 54ef083..52bc10f 100644
--- a/kabc/plugins/sharpdtm/sharpdtmE.pro
+++ b/kabc/plugins/sharpdtm/sharpdtmE.pro
@@ -1,31 +1,31 @@
1 TEMPLATE= lib 1 TEMPLATE= lib
2CONFIG += qt warn_on 2CONFIG += qt warn_on
3TARGET = microkabc_sharpdtm 3TARGET = microkabc_sharpdtm
4 4
5INCLUDEPATH += ../.. $(SHARPDTMSDK)/include ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../qtcompat $(QPEDIR)/include 5INCLUDEPATH += ../.. $(SHARPDTMSDK)/include ../../../microkde ../../../microkde/kdecore ../../../microkde/kio/kfile ../../../microkde/kio/kio ../../../microkde/kresources ../../../qtcompat $(QPEDIR)/include
6 6
7 7
8OBJECTS_DIR = obj/$(PLATFORM) 8OBJECTS_DIR = obj/$(PLATFORM)
9MOC_DIR = moc/$(PLATFORM) 9MOC_DIR = moc/$(PLATFORM)
10DESTDIR = $(QPEDIR)/lib 10DESTDIR = $(QPEDIR)/lib
11LIBS += -lmicrokde 11LIBS += -lmicrokde
12LIBS += -lmicrokabc 12LIBS += -lmicrokabc
13LIBS += -L$(QPEDIR)/lib 13LIBS += -L$(QPEDIR)/lib
14LIBS += -lqpe 14LIBS += -lqpe
15LIBS += -lqte 15LIBS += -lqte
16LIBS += -lzdtm 16LIBS += -lzdtm
17 17
18 18
19 19
20INTERFACES = \ 20INTERFACES = \
21 21
22HEADERS = \ 22HEADERS = \
23 resourcesharpdtm.h \ 23 resourcesharpdtm.h \
24 resourcesharpdtmconfig.h \ 24 resourcesharpdtmconfig.h \
25 sharpdtmconverter.h 25 sharpdtmconverter.h
26 26
27SOURCES = \ 27SOURCES = \
28 resourcesharpdtm.cpp \ 28 resourcesharpdtm.cpp \
29 resourcesharpdtmconfig.cpp \ 29 resourcesharpdtmconfig.cpp \
30 sharpdtmconverter.cpp 30 sharpdtmconverter.cpp
31 31