summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/dir
Unidiff
Diffstat (limited to 'kabc/plugins/dir') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/dir/resourcedir.cpp363
-rw-r--r--kabc/plugins/dir/resourcedir.h115
-rw-r--r--kabc/plugins/dir/resourcedirconfig.cpp123
-rw-r--r--kabc/plugins/dir/resourcedirconfig.h61
4 files changed, 662 insertions, 0 deletions
diff --git a/kabc/plugins/dir/resourcedir.cpp b/kabc/plugins/dir/resourcedir.cpp
new file mode 100644
index 0000000..f354a9e
--- a/dev/null
+++ b/kabc/plugins/dir/resourcedir.cpp
@@ -0,0 +1,363 @@
1/*
2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
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
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk
24
25$Id$
26*/
27
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#ifndef _WIN32_
32#include <unistd.h>
33#endif
34
35#include <qregexp.h>
36#include <qtimer.h>
37#include <qwidget.h>
38
39#include <kapplication.h>
40#include <kconfig.h>
41#include <kdebug.h>
42//US #include <kgenericfactory.h>
43#include <kglobal.h>
44#include <klocale.h>
45#include <kstandarddirs.h>
46#include <kurlrequester.h>
47
48#include "addressbook.h"
49
50//US #include "formatfactory.h"
51
52#include "resourcedirconfig.h"
53#include "stdaddressbook.h"
54
55//US
56#include <qdir.h>
57#include <formats/vcardformatplugin2.h>
58#include <formats/binaryformat.h>
59
60#include "resourcedir.h"
61
62using namespace KABC;
63
64extern "C"
65{
66 void *init_kabc_dir()
67 {
68 qDebug("resourcedir.cpp : init_kabc_dir has to be changed");
69//US return new KRES::PluginFactory<ResourceDir,ResourceDirConfig>();
70 return 0;
71 }
72}
73
74
75ResourceDir::ResourceDir( const KConfig *config )
76 : Resource( config )
77{
78 QString path;
79
80 KConfig *cfg = (KConfig *)config;
81 if ( cfg ) {
82//US path = config->readEntry( "FilePath" );
83 path = cfg->readEntry( "FilePath", StdAddressBook::directoryName() );
84//US mFormatName = config->readEntry( "FileFormat" );
85 mFormatName = cfg->readEntry( "FileFormat", "vcard" );
86 } else {
87 path = StdAddressBook::directoryName();
88 mFormatName = "vcard";
89 }
90
91
92/*US FormatFactory *factory = FormatFactory::self();
93 mFormat = factory->format( mFormatName );
94
95 if ( !mFormat ) {
96 mFormatName = "vcard";
97 mFormat = factory->format( mFormatName );
98 }
99*/
100
101//US qDebug("ResourceDir::ResourceDir initialized with format %s ", mFormatName.latin1());
102 if (mFormatName == "vcard")
103 mFormat = new VCardFormatPlugin2();
104 else if (mFormatName == "binary")
105 mFormat = new BinaryFormat();
106 else
107 qDebug("ResourceFile::init format unknown !!! %s ", mFormatName.latin1());
108
109
110/*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods
111 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( pathChanged() ) );
112 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( pathChanged() ) );
113 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( pathChanged() ) );
114*/
115
116 setPath( path );
117}
118
119ResourceDir::~ResourceDir()
120{
121 delete mFormat;
122 mFormat = 0;
123}
124
125void ResourceDir::writeConfig( KConfig *config )
126{
127 Resource::writeConfig( config );
128
129 config->writeEntry( "FilePath", mPath );
130 config->writeEntry( "FileFormat", mFormatName );
131}
132
133Ticket *ResourceDir::requestSaveTicket()
134{
135 kdDebug(5700) << "ResourceDir::requestSaveTicket()" << endl;
136
137 if ( !addressBook() ) return 0;
138
139 if ( !lock( mPath ) ) {
140 kdDebug(5700) << "ResourceDir::requestSaveTicket(): Unable to lock path '"
141 << mPath << "'" << endl;
142 return 0;
143 }
144 return createTicket( this );
145}
146
147
148bool ResourceDir::doOpen()
149{
150 QDir dir( mPath );
151 if ( !dir.exists() ) { // no directory available
152 return dir.mkdir( dir.path() );
153 } else {
154 QString testName = dir.entryList( QDir::Files )[0];
155 if ( testName.isNull() || testName.isEmpty() ) // no file in directory
156 return true;
157
158 QFile file( mPath + "/" + testName );
159 if ( file.open( IO_ReadOnly ) )
160 return true;
161
162 if ( file.size() == 0 )
163 return true;
164
165 bool ok = mFormat->checkFormat( &file );
166 file.close();
167 return ok;
168 }
169}
170
171void ResourceDir::doClose()
172{
173}
174
175bool ResourceDir::load()
176{
177 kdDebug(5700) << "ResourceDir::load(): '" << mPath << "'" << endl;
178
179 QDir dir( mPath );
180 QStringList files = dir.entryList( QDir::Files );
181
182 QStringList::Iterator it;
183 bool ok = true;
184 for ( it = files.begin(); it != files.end(); ++it ) {
185 QFile file( mPath + "/" + (*it) );
186
187 if ( !file.open( IO_ReadOnly ) ) {
188 addressBook()->error( i18n( "Unable to open file '%1' for reading" ).arg( file.name() ) );
189 ok = false;
190 continue;
191 }
192
193 if ( !mFormat->loadAll( addressBook(), this, &file ) )
194 ok = false;
195
196 file.close();
197 }
198
199 return ok;
200}
201
202bool ResourceDir::save( Ticket *ticket )
203{
204 kdDebug(5700) << "ResourceDir::save(): '" << mPath << "'" << endl;
205
206 AddressBook::Iterator it;
207 bool ok = true;
208
209 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
210 if ( (*it).resource() != this || !(*it).changed() )
211 continue;
212
213 QFile file( mPath + "/" + (*it).uid() );
214 if ( !file.open( IO_WriteOnly ) ) {
215 addressBook()->error( i18n( "Unable to open file '%1' for writing" ).arg( file.name() ) );
216 continue;
217 }
218
219 mFormat->save( *it, &file );
220
221 // mark as unchanged
222 (*it).setChanged( false );
223
224 file.close();
225 }
226
227 delete ticket;
228 unlock( mPath );
229
230 return ok;
231}
232
233bool ResourceDir::lock( const QString &path )
234{
235 kdDebug(5700) << "ResourceDir::lock()" << endl;
236
237 QString p = path;
238//US change the implementation how the lockfilename is getting created
239//US p.replace( QRegExp("/"), "_" );
240//US QString lockName = locateLocal( "data", "kabc/lock/" + p + ".lock" );
241 KURL url(p);
242 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
243
244
245 kdDebug(5700) << "-- lock name: " << lockName << endl;
246
247 if ( QFile::exists( lockName ) ) return false;
248
249 QString lockUniqueName;
250 lockUniqueName = p + KApplication::randomString( 8 );
251
252 url = lockUniqueName;
253//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
254 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
255
256 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
257
258 // Create unique file
259 QFile file( mLockUniqueName );
260 file.open( IO_WriteOnly );
261 file.close();
262
263 // Create lock file
264#ifdef _WIN32_
265 int result = 0;
266 qDebug("WARNING: ResourceDir::lock cannot link ");
267#else
268 int result = ::link( QFile::encodeName( mLockUniqueName ),
269 QFile::encodeName( lockName ) );
270#endif
271 if ( result == 0 ) {
272 addressBook()->emitAddressBookLocked();
273 return true;
274 }
275
276 // TODO: check stat
277
278 return false;
279}
280
281void ResourceDir::unlock( const QString &path )
282{
283 QString p = path;
284//US change the implementation how the lockfilename is getting created
285//US p.replace( QRegExp( "/" ), "_" );
286//US QString lockName = locate( "data", "kabc/lock/" + p + ".lock" );
287 KURL url(p);
288 QString lockName = locate( "data", "kabc/lock/" + url.fileName() + ".lock" );
289
290 ::unlink( QFile::encodeName( lockName ) );
291 QFile::remove( mLockUniqueName );
292 addressBook()->emitAddressBookUnlocked();
293}
294
295void ResourceDir::setPath( const QString &path )
296{
297/*US ToDo: no synchronization so far. Has to be changed in the future
298 mDirWatch.stopScan();
299 mDirWatch.removeDir( mPath );
300*/
301 mPath = path;
302
303/*US ToDo: no synchronization so far. Has to be changed in the future
304 mDirWatch.addDir( mPath, true );
305 mDirWatch.startScan();
306*/
307
308//US simulate KDirWatch event
309 pathChanged();
310
311}
312
313QString ResourceDir::path() const
314{
315 return mPath;
316}
317
318void ResourceDir::setFormat( const QString &format )
319{
320 mFormatName = format;
321
322 if ( mFormat )
323 delete mFormat;
324
325//US FormatFactory *factory = FormatFactory::self();
326//US mFormat = factory->format( mFormatName );
327
328qDebug("ResourceDir::setFormat initialized with format %s ", format.latin1());
329 if (mFormatName == "vcard")
330 mFormat = new VCardFormatPlugin2();
331 else if (mFormatName == "binary")
332 mFormat = new BinaryFormat();
333 else
334 qDebug("ResourceDir::setFormat format unknown !!! %s ", format.latin1());
335
336
337}
338
339QString ResourceDir::format() const
340{
341 return mFormatName;
342}
343
344void ResourceDir::pathChanged()
345{
346 if ( !addressBook() )
347 return;
348
349 load();
350 addressBook()->emitAddressBookChanged();
351}
352
353void ResourceDir::removeAddressee( const Addressee& addr )
354{
355 QFile::remove( mPath + "/" + addr.uid() );
356}
357
358void ResourceDir::cleanUp()
359{
360 unlock( mPath );
361}
362
363//US #include "resourcedir.moc"
diff --git a/kabc/plugins/dir/resourcedir.h b/kabc/plugins/dir/resourcedir.h
new file mode 100644
index 0000000..6e35695
--- a/dev/null
+++ b/kabc/plugins/dir/resourcedir.h
@@ -0,0 +1,115 @@
1/*
2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
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
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk
24
25$Id$
26*/
27
28#ifndef KABC_RESOURCEDIR_H
29#define KABC_RESOURCEDIR_H
30
31#include <kconfig.h>
32#include <kdirwatch.h>
33
34#include <sys/types.h>
35
36#include "resource.h"
37
38class QTimer;
39
40namespace KABC {
41
42class FormatPlugin;
43
44/**
45 @internal
46*/
47class ResourceDir : public Resource
48{
49 Q_OBJECT
50
51public:
52 ResourceDir( const KConfig* );
53 ~ResourceDir();
54
55 virtual void writeConfig( KConfig* );
56
57 virtual bool doOpen();
58 virtual void doClose();
59
60 virtual Ticket *requestSaveTicket();
61
62 virtual bool load();
63 virtual bool save( Ticket * );
64
65 /**
66 * Set path to be used for saving.
67 */
68 void setPath( const QString & );
69
70 /**
71 * Return path used for loading and saving the address book.
72 */
73 QString path() const;
74
75 /**
76 * Set the format by name.
77 */
78 void setFormat( const QString &format );
79
80 /**
81 * Returns the format name.
82 */
83 QString format() const;
84
85 /**
86 * Remove a addressee from its source.
87 * This method is mainly called by KABC::AddressBook.
88 */
89 virtual void removeAddressee( const Addressee& addr );
90
91 /**
92 * This method is called by an error handler if the application
93 * crashed
94 */
95 virtual void cleanUp();
96
97protected slots:
98 void pathChanged();
99
100protected:
101 bool lock( const QString &path );
102 void unlock( const QString &path );
103
104private:
105 FormatPlugin *mFormat;
106
107 KDirWatch mDirWatch;
108
109 QString mPath;
110 QString mFormatName;
111 QString mLockUniqueName;
112};
113
114}
115#endif
diff --git a/kabc/plugins/dir/resourcedirconfig.cpp b/kabc/plugins/dir/resourcedirconfig.cpp
new file mode 100644
index 0000000..98d18fe
--- a/dev/null
+++ b/kabc/plugins/dir/resourcedirconfig.cpp
@@ -0,0 +1,123 @@
1/*
2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
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
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk
24
25$Id$
26*/
27
28#include <qlabel.h>
29#include <qlayout.h>
30
31#include <kdebug.h>
32#include <klocale.h>
33#include <kstandarddirs.h>
34#include <kdialog.h>
35
36//US #include "formatfactory.h"
37#include "resourcedir.h"
38#include "stdaddressbook.h"
39
40#include "resourcedirconfig.h"
41
42using namespace KABC;
43
44ResourceDirConfig::ResourceDirConfig( QWidget* parent, const char* name )
45 : KRES::ConfigWidget( parent, name )
46{
47 QGridLayout *mainLayout = new QGridLayout( this, 2, 2, 0,
48 KDialog::spacingHint() );
49
50 QLabel *label = new QLabel( i18n( "Format:" ), this );
51 mFormatBox = new KComboBox( this );
52
53 mainLayout->addWidget( label, 0, 0 );
54 mainLayout->addWidget( mFormatBox, 0, 1 );
55
56 label = new QLabel( i18n( "Location:" ), this );
57 mFileNameEdit = new KURLRequester( this );
58//US mFileNameEdit->setMode( KFile::Directory );
59
60 mainLayout->addWidget( label, 1, 0 );
61 mainLayout->addWidget( mFileNameEdit, 1, 1 );
62
63/*US lets hardcode the formats instead of using a factory
64 FormatFactory *factory = FormatFactory::self();
65 QStringList formats = factory->formats();
66 QStringList::Iterator it;
67 for ( it = formats.begin(); it != formats.end(); ++it ) {
68 FormatInfo *info = factory->info( *it );
69 if ( info ) {
70 mFormatTypes << (*it);
71 mFormatBox->insertItem( info->nameLabel );
72 }
73 }
74*/
75 mFormatTypes << "vcard";
76 mFormatTypes << "binary";
77 mFormatBox->insertItem( "vcard" );
78 mFormatBox->insertItem( "binary" );
79
80
81 mInEditMode = false;
82}
83
84void ResourceDirConfig::setEditMode( bool value )
85{
86 mFormatBox->setEnabled( !value );
87 mInEditMode = value;
88}
89
90void ResourceDirConfig::loadSettings( KRES::Resource *res )
91{
92//US ResourceDir *resource = dynamic_cast<ResourceDir*>( res );
93 ResourceDir *resource = (ResourceDir*)( res );
94
95 if ( !resource ) {
96 kdDebug(5700) << "ResourceDirConfig::loadSettings(): cast failed" << endl;
97 return;
98 }
99
100 mFormatBox->setCurrentItem( mFormatTypes.findIndex( resource->format() ) );
101
102 mFileNameEdit->setURL( resource->path() );
103 if ( mFileNameEdit->url().isEmpty() )
104 mFileNameEdit->setURL( KABC::StdAddressBook::directoryName() );
105}
106
107void ResourceDirConfig::saveSettings( KRES::Resource *res )
108{
109//US ResourceDir *resource = dynamic_cast<ResourceDir*>( res );
110 ResourceDir *resource = (ResourceDir*)( res );
111
112 if ( !resource ) {
113 kdDebug(5700) << "ResourceDirConfig::loadSettings(): cast failed" << endl;
114 return;
115 }
116
117 if ( mInEditMode )
118 resource->setFormat( mFormatTypes[ mFormatBox->currentItem() ] );
119
120 resource->setPath( mFileNameEdit->url() );
121}
122
123//US #include "resourcedirconfig.moc"
diff --git a/kabc/plugins/dir/resourcedirconfig.h b/kabc/plugins/dir/resourcedirconfig.h
new file mode 100644
index 0000000..5af38a5
--- a/dev/null
+++ b/kabc/plugins/dir/resourcedirconfig.h
@@ -0,0 +1,61 @@
1/*
2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
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
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk
24
25$Id$
26*/
27
28#ifndef RESOURCEDIRCONFIG_H
29#define RESOURCEDIRCONFIG_H
30
31#include <kcombobox.h>
32#include <kurlrequester.h>
33
34#include <kresources/configwidget.h>
35
36namespace KABC {
37
38class ResourceDirConfig : public KRES::ConfigWidget
39{
40 Q_OBJECT
41
42public:
43 ResourceDirConfig( QWidget* parent = 0, const char* name = 0 );
44
45 void setEditMode( bool value );
46
47public slots:
48 void loadSettings( KRES::Resource* );
49 void saveSettings( KRES::Resource* );
50
51private:
52 KComboBox* mFormatBox;
53 KURLRequester* mFileNameEdit;
54
55 QStringList mFormatTypes;
56
57 bool mInEditMode;
58};
59
60}
61#endif