author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /kabc/plugins/file | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
-rw-r--r-- | kabc/plugins/file/resourcefile.cpp | 389 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefile.h | 162 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefileconfig.cpp | 144 | ||||
-rw-r--r-- | kabc/plugins/file/resourcefileconfig.h | 65 |
4 files changed, 760 insertions, 0 deletions
diff --git a/kabc/plugins/file/resourcefile.cpp b/kabc/plugins/file/resourcefile.cpp new file mode 100644 index 0000000..d30ed2f --- a/dev/null +++ b/kabc/plugins/file/resourcefile.cpp | |||
@@ -0,0 +1,389 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@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 | /* | ||
22 | Enhanced Version of the file for platform independent KDE tools. | ||
23 | Copyright (c) 2004 Ulf Schenk | ||
24 | |||
25 | $Id$ | ||
26 | */ | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/stat.h> | ||
30 | #ifndef _WIN32_ | ||
31 | #include <unistd.h> | ||
32 | #endif | ||
33 | |||
34 | #include <qfile.h> | ||
35 | #include <qfileinfo.h> | ||
36 | #include <qregexp.h> | ||
37 | #include <qtimer.h> | ||
38 | |||
39 | #include <kapplication.h> | ||
40 | #include <kconfig.h> | ||
41 | #include <kdebug.h> | ||
42 | #include <klocale.h> | ||
43 | //US #include <ksavefile.h> | ||
44 | #include <kstandarddirs.h> | ||
45 | |||
46 | //US #include "formatfactory.h" | ||
47 | |||
48 | #include "resource.h" | ||
49 | #include "resourcefileconfig.h" | ||
50 | #include "stdaddressbook.h" | ||
51 | |||
52 | #include <formats/vcardformatplugin2.h> | ||
53 | #include <formats/binaryformat.h> | ||
54 | |||
55 | |||
56 | #include "resourcefile.h" | ||
57 | |||
58 | using namespace KABC; | ||
59 | |||
60 | extern "C" | ||
61 | { | ||
62 | void *init_kabc_file() | ||
63 | { | ||
64 | qDebug("!!!resourcefile.cpp : init_kabc_file has to be changed"); | ||
65 | //US return new KRES::PluginFactory<ResourceFile,ResourceFileConfig>(); | ||
66 | return 0; | ||
67 | } | ||
68 | } | ||
69 | |||
70 | |||
71 | ResourceFile::ResourceFile( const KConfig *config ) | ||
72 | : Resource( config ) , mFormat( 0 ) | ||
73 | { | ||
74 | QString fileName, formatName; | ||
75 | |||
76 | KConfig *cfg = (KConfig *)config; | ||
77 | if ( cfg ) { | ||
78 | fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() ); | ||
79 | formatName = cfg->readEntry( "FileFormat", "vcard" ); | ||
80 | // qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() ); | ||
81 | } else { | ||
82 | fileName = StdAddressBook::fileName(); | ||
83 | formatName = "vcard"; | ||
84 | // qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() ); | ||
85 | } | ||
86 | |||
87 | init( fileName, formatName ); | ||
88 | } | ||
89 | |||
90 | ResourceFile::ResourceFile( const QString &fileName, | ||
91 | const QString &formatName ) | ||
92 | : Resource( 0 ) | ||
93 | { | ||
94 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); | ||
95 | init( fileName, formatName ); | ||
96 | } | ||
97 | |||
98 | void ResourceFile::init( const QString &fileName, const QString &formatName ) | ||
99 | { | ||
100 | mFormatName = formatName; | ||
101 | |||
102 | /*US FormatFactory *factory = FormatFactory::self(); | ||
103 | mFormat = factory->format( mFormatName ); | ||
104 | |||
105 | if ( !mFormat ) { | ||
106 | mFormatName = "vcard"; | ||
107 | mFormat = factory->format( mFormatName ); | ||
108 | } | ||
109 | */ | ||
110 | |||
111 | //US qDebug("ResourceFile::init initialized with format %s ", formatName.latin1()); | ||
112 | if (mFormatName == "vcard") { | ||
113 | mFormat = new VCardFormatPlugin2(); | ||
114 | // qDebug("ResourceFile::init format VCardFormatPlugin2"); | ||
115 | } | ||
116 | else if (mFormatName == "binary") { | ||
117 | mFormat = new BinaryFormat(); | ||
118 | // qDebug("ResourceFile::init format BinaryFormat"); | ||
119 | } | ||
120 | else | ||
121 | qDebug("ResourceFile::init format unknown !!! %s ", formatName.latin1()); | ||
122 | |||
123 | /*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods | ||
124 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | ||
125 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | ||
126 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | ||
127 | */ | ||
128 | |||
129 | setFileName( fileName ); | ||
130 | } | ||
131 | |||
132 | ResourceFile::~ResourceFile() | ||
133 | { | ||
134 | delete mFormat; | ||
135 | mFormat = 0; | ||
136 | } | ||
137 | |||
138 | void ResourceFile::writeConfig( KConfig *config ) | ||
139 | { | ||
140 | Resource::writeConfig( config ); | ||
141 | |||
142 | config->writeEntry( "FileName", mFileName ); | ||
143 | config->writeEntry( "FileFormat", mFormatName ); | ||
144 | |||
145 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); | ||
146 | |||
147 | } | ||
148 | |||
149 | Ticket *ResourceFile::requestSaveTicket() | ||
150 | { | ||
151 | kdDebug(5700) << "ResourceFile::requestSaveTicket()" << endl; | ||
152 | |||
153 | if ( !addressBook() ) return 0; | ||
154 | |||
155 | if ( !lock( mFileName ) ) { | ||
156 | kdDebug(5700) << "ResourceFile::requestSaveTicket(): Unable to lock file '" | ||
157 | << mFileName << "'" << endl; | ||
158 | return 0; | ||
159 | } | ||
160 | return createTicket( this ); | ||
161 | } | ||
162 | |||
163 | |||
164 | bool ResourceFile::doOpen() | ||
165 | { | ||
166 | QFile file( mFileName ); | ||
167 | |||
168 | if ( !file.exists() ) { | ||
169 | // try to create the file | ||
170 | bool ok = file.open( IO_WriteOnly ); | ||
171 | if ( ok ) | ||
172 | file.close(); | ||
173 | |||
174 | return ok; | ||
175 | } else { | ||
176 | if ( !file.open( IO_ReadWrite ) ) | ||
177 | return false; | ||
178 | |||
179 | if ( file.size() == 0 ) { | ||
180 | file.close(); | ||
181 | return true; | ||
182 | } | ||
183 | |||
184 | bool ok = mFormat->checkFormat( &file ); | ||
185 | file.close(); | ||
186 | |||
187 | return ok; | ||
188 | } | ||
189 | } | ||
190 | |||
191 | void ResourceFile::doClose() | ||
192 | { | ||
193 | } | ||
194 | |||
195 | bool ResourceFile::load() | ||
196 | { | ||
197 | kdDebug(5700) << "ResourceFile::load(): '" << mFileName << "'" << endl; | ||
198 | |||
199 | QFile file( mFileName ); | ||
200 | if ( !file.open( IO_ReadOnly ) ) { | ||
201 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); | ||
202 | return false; | ||
203 | } | ||
204 | |||
205 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); | ||
206 | |||
207 | return mFormat->loadAll( addressBook(), this, &file ); | ||
208 | } | ||
209 | |||
210 | bool ResourceFile::save( Ticket *ticket ) | ||
211 | { | ||
212 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | ||
213 | kdDebug(5700) << "ResourceFile::save()" << endl; | ||
214 | |||
215 | // create backup file | ||
216 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | ||
217 | |||
218 | /*US we use a simpler method to create a backupfile | ||
219 | |||
220 | (void) KSaveFile::backupFile( mFileName, QString::null /*directory*/ | ||
221 | /*US ,extension ); | ||
222 | |||
223 | KSaveFile saveFile( mFileName ); | ||
224 | bool ok = false; | ||
225 | if ( saveFile.status() == 0 && saveFile.file() ) | ||
226 | { | ||
227 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | ||
228 | ok = saveFile.close(); | ||
229 | } | ||
230 | */ | ||
231 | |||
232 | //US ToDo: write backupfile | ||
233 | QFile info; | ||
234 | info.setName( mFileName ); | ||
235 | bool ok = info.open( IO_WriteOnly ); | ||
236 | if ( ok ) { | ||
237 | mFormat->saveAll( addressBook(), this, &info ); | ||
238 | |||
239 | info.close(); | ||
240 | ok = true; | ||
241 | } | ||
242 | else { | ||
243 | |||
244 | } | ||
245 | |||
246 | if ( !ok ) | ||
247 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); | ||
248 | |||
249 | delete ticket; | ||
250 | unlock( mFileName ); | ||
251 | |||
252 | return ok; | ||
253 | |||
254 | qDebug("ResourceFile::save has to be changed"); | ||
255 | return true; | ||
256 | } | ||
257 | |||
258 | bool ResourceFile::lock( const QString &fileName ) | ||
259 | { | ||
260 | kdDebug(5700) << "ResourceFile::lock()" << endl; | ||
261 | |||
262 | QString fn = fileName; | ||
263 | |||
264 | //US change the implementation how the lockfilename is getting created | ||
265 | //US fn.replace( QRegExp("/"), "_" ); | ||
266 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | ||
267 | |||
268 | KURL url(fn); | ||
269 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | ||
270 | |||
271 | kdDebug(5700) << "-- lock name: " << lockName << endl; | ||
272 | |||
273 | if (QFile::exists( lockName )) return false; | ||
274 | |||
275 | QString lockUniqueName; | ||
276 | lockUniqueName = fn + KApplication::randomString( 8 ); | ||
277 | |||
278 | url = lockUniqueName; | ||
279 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | ||
280 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | ||
281 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | ||
282 | |||
283 | // Create unique file | ||
284 | QFile file( mLockUniqueName ); | ||
285 | file.open( IO_WriteOnly ); | ||
286 | file.close(); | ||
287 | |||
288 | // Create lock file | ||
289 | int result = 0;//::link( QFile::encodeName( mLockUniqueName ), | ||
290 | // QFile::encodeName( lockName ) ); | ||
291 | qDebug("lock files %s, %s needs to be fixed", mLockUniqueName.latin1(), lockName.latin1() ); | ||
292 | |||
293 | if ( result == 0 ) { | ||
294 | addressBook()->emitAddressBookLocked(); | ||
295 | return true; | ||
296 | } | ||
297 | |||
298 | // TODO: check stat | ||
299 | |||
300 | return false; | ||
301 | } | ||
302 | |||
303 | void ResourceFile::unlock( const QString &fileName ) | ||
304 | { | ||
305 | QString fn = fileName; | ||
306 | //US change the implementation how the lockfilename is getting created | ||
307 | //US fn.replace( QRegExp( "/" ), "_" ); | ||
308 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | ||
309 | //US QString lockName = fn + ".lock"; | ||
310 | KURL url(fn); | ||
311 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | ||
312 | |||
313 | QFile::remove( lockName ); | ||
314 | QFile::remove( mLockUniqueName ); | ||
315 | addressBook()->emitAddressBookUnlocked(); | ||
316 | } | ||
317 | |||
318 | void ResourceFile::setFileName( const QString &fileName ) | ||
319 | { | ||
320 | /*US ToDo: no synchronization so far. Has to be changed in the future | ||
321 | mDirWatch.stopScan(); | ||
322 | mDirWatch.removeFile( mFileName ); | ||
323 | */ | ||
324 | mFileName = fileName; | ||
325 | |||
326 | |||
327 | /*US ToDo: no synchronization so far. Has to be changed in the future | ||
328 | mDirWatch.addFile( mFileName ); | ||
329 | mDirWatch.startScan(); | ||
330 | */ | ||
331 | //US simulate KDirWatch event | ||
332 | fileChanged(); | ||
333 | } | ||
334 | |||
335 | QString ResourceFile::fileName() const | ||
336 | { | ||
337 | return mFileName; | ||
338 | } | ||
339 | |||
340 | void ResourceFile::setFormat( const QString &format ) | ||
341 | { | ||
342 | mFormatName = format; | ||
343 | delete mFormat; | ||
344 | |||
345 | //US FormatFactory *factory = FormatFactory::self(); | ||
346 | //US mFormat = factory->format( mFormatName ); | ||
347 | |||
348 | //qDebug("ResourceFile::setFormat initialized with format %s ", format.latin1()); | ||
349 | if (mFormatName == "vcard") { | ||
350 | mFormat = new VCardFormatPlugin2(); | ||
351 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); | ||
352 | } | ||
353 | else if (mFormatName == "binary") { | ||
354 | mFormat = new BinaryFormat(); | ||
355 | // qDebug("ResourceFile::setFormat format %s", mFormatName.latin1()); | ||
356 | } | ||
357 | else | ||
358 | qDebug("ResourceFile::setFormat format unknown !!! %s ", format.latin1()); | ||
359 | |||
360 | } | ||
361 | |||
362 | QString ResourceFile::format() const | ||
363 | { | ||
364 | return mFormatName; | ||
365 | } | ||
366 | |||
367 | void ResourceFile::fileChanged() | ||
368 | { | ||
369 | // There is a small theoretical chance that KDirWatch calls us before | ||
370 | // we are fully constructed | ||
371 | if (!addressBook()) | ||
372 | return; | ||
373 | load(); | ||
374 | addressBook()->emitAddressBookChanged(); | ||
375 | } | ||
376 | |||
377 | void ResourceFile::removeAddressee( const Addressee &addr ) | ||
378 | { | ||
379 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); | ||
380 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); | ||
381 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); | ||
382 | } | ||
383 | |||
384 | void ResourceFile::cleanUp() | ||
385 | { | ||
386 | unlock( mFileName ); | ||
387 | } | ||
388 | |||
389 | //US #include "resourcefile.moc" | ||
diff --git a/kabc/plugins/file/resourcefile.h b/kabc/plugins/file/resourcefile.h new file mode 100644 index 0000000..4522d78 --- a/dev/null +++ b/kabc/plugins/file/resourcefile.h | |||
@@ -0,0 +1,162 @@ | |||
1 | /* | ||
2 | This file is part of libkabc. | ||
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@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 | /* | ||
22 | Enhanced Version of the file for platform independent KDE tools. | ||
23 | Copyright (c) 2004 Ulf Schenk | ||
24 | |||
25 | $Id$ | ||
26 | */ | ||
27 | |||
28 | |||
29 | #ifndef KABC_RESOURCEFILE_H | ||
30 | #define KABC_RESOURCEFILE_H | ||
31 | |||
32 | #include <kconfig.h> | ||
33 | #include <kdirwatch.h> | ||
34 | |||
35 | #include <sys/types.h> | ||
36 | |||
37 | #include <resource.h> | ||
38 | |||
39 | class QTimer; | ||
40 | class FormatPlugin; | ||
41 | |||
42 | namespace KABC { | ||
43 | |||
44 | //US class FormatPlugin; | ||
45 | class ResourceConfigWidget; | ||
46 | |||
47 | /** | ||
48 | @internal | ||
49 | */ | ||
50 | class ResourceFile : public Resource | ||
51 | { | ||
52 | Q_OBJECT | ||
53 | |||
54 | public: | ||
55 | |||
56 | /** | ||
57 | Constructor. | ||
58 | |||
59 | @param cfg The config object where custom resource settings are stored. | ||
60 | */ | ||
61 | ResourceFile( const KConfig *cfg ); | ||
62 | |||
63 | /** | ||
64 | Construct file resource on file @arg fileName using format @arg formatName. | ||
65 | */ | ||
66 | ResourceFile( const QString &fileName, const QString &formatName = "vcard" ); | ||
67 | |||
68 | /** | ||
69 | * Destructor. | ||
70 | */ | ||
71 | ~ResourceFile(); | ||
72 | |||
73 | /** | ||
74 | Writes the config back. | ||
75 | */ | ||
76 | virtual void writeConfig( KConfig *cfg ); | ||
77 | |||
78 | /** | ||
79 | * Tries to open the file and checks for the proper format. | ||
80 | * This method should be called before @ref load(). | ||
81 | */ | ||
82 | virtual bool doOpen(); | ||
83 | |||
84 | /** | ||
85 | * Closes the file again. | ||
86 | */ | ||
87 | virtual void doClose(); | ||
88 | |||
89 | /** | ||
90 | * Requests a save ticket, that is used by @ref save() | ||
91 | */ | ||
92 | virtual Ticket *requestSaveTicket(); | ||
93 | |||
94 | /** | ||
95 | * Loads all addressees from file to the address book. | ||
96 | * Returns true if all addressees could be loaded otherwise false. | ||
97 | */ | ||
98 | virtual bool load(); | ||
99 | |||
100 | /** | ||
101 | * Saves all addresses from address book to file. | ||
102 | * Returns true if all addressees could be saved otherwise false. | ||
103 | * | ||
104 | * @param ticket The ticket returned by @ref requestSaveTicket() | ||
105 | */ | ||
106 | virtual bool save( Ticket *ticket ); | ||
107 | |||
108 | /** | ||
109 | * Set name of file to be used for saving. | ||
110 | */ | ||
111 | void setFileName( const QString & ); | ||
112 | |||
113 | /** | ||
114 | * Return name of file used for loading and saving the address book. | ||
115 | */ | ||
116 | QString fileName() const; | ||
117 | |||
118 | /** | ||
119 | Sets a new format by name. | ||
120 | */ | ||
121 | void setFormat( const QString &name ); | ||
122 | |||
123 | /** | ||
124 | Returns the format name. | ||
125 | */ | ||
126 | QString format() const; | ||
127 | |||
128 | /** | ||
129 | * Remove a addressee from its source. | ||
130 | * This method is mainly called by KABC::AddressBook. | ||
131 | */ | ||
132 | virtual void removeAddressee( const Addressee& addr ); | ||
133 | |||
134 | /** | ||
135 | * This method is called by an error handler if the application | ||
136 | * crashed | ||
137 | */ | ||
138 | virtual void cleanUp(); | ||
139 | |||
140 | protected slots: | ||
141 | void fileChanged(); | ||
142 | |||
143 | protected: | ||
144 | void init( const QString &fileName, const QString &format ); | ||
145 | |||
146 | bool lock( const QString &fileName ); | ||
147 | void unlock( const QString &fileName ); | ||
148 | |||
149 | private: | ||
150 | QString mFileName; | ||
151 | QString mFormatName; | ||
152 | |||
153 | FormatPlugin *mFormat; | ||
154 | |||
155 | QString mLockUniqueName; | ||
156 | |||
157 | KDirWatch mDirWatch; | ||
158 | }; | ||
159 | |||
160 | } | ||
161 | |||
162 | #endif | ||
diff --git a/kabc/plugins/file/resourcefileconfig.cpp b/kabc/plugins/file/resourcefileconfig.cpp new file mode 100644 index 0000000..b63775d --- a/dev/null +++ b/kabc/plugins/file/resourcefileconfig.cpp | |||
@@ -0,0 +1,144 @@ | |||
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 | /* | ||
22 | Enhanced Version of the file for platform independent KDE tools. | ||
23 | Copyright (c) 2004 Ulf Schenk | ||
24 | |||
25 | $Id$ | ||
26 | */ | ||
27 | |||
28 | #include <qlabel.h> | ||
29 | #include <qlayout.h> | ||
30 | #include <qfileinfo.h> | ||
31 | |||
32 | #include <kdebug.h> | ||
33 | #include <klocale.h> | ||
34 | #include <kstandarddirs.h> | ||
35 | #include <kdialog.h> | ||
36 | #ifndef _WIN32_ | ||
37 | #include <unistd.h> | ||
38 | #endif | ||
39 | //US #include "formatfactory.h" | ||
40 | #include <qfile.h> | ||
41 | #include "resourcefile.h" | ||
42 | #include "stdaddressbook.h" | ||
43 | |||
44 | #include "resourcefileconfig.h" | ||
45 | |||
46 | using namespace KABC; | ||
47 | |||
48 | ResourceFileConfig::ResourceFileConfig( QWidget* parent, const char* name ) | ||
49 | : ConfigWidget( parent, name ) | ||
50 | { | ||
51 | //qDebug("ResourceFileConfig::ResourceFileConfig"); | ||
52 | |||
53 | QGridLayout *mainLayout = new QGridLayout( this, 2, 2, 0, | ||
54 | KDialog::spacingHint() ); | ||
55 | |||
56 | QLabel *label = new QLabel( i18n( "Format:" ), this ); | ||
57 | mFormatBox = new KComboBox( this ); | ||
58 | |||
59 | mainLayout->addWidget( label, 0, 0 ); | ||
60 | mainLayout->addWidget( mFormatBox, 0, 1 ); | ||
61 | |||
62 | label = new QLabel( i18n( "Location:" ), this ); | ||
63 | mFileNameEdit = new KURLRequester( this ); | ||
64 | |||
65 | connect( mFileNameEdit, SIGNAL( textChanged( const QString & ) ), | ||
66 | SLOT( checkFilePermissions( const QString & ) ) ); | ||
67 | |||
68 | mainLayout->addWidget( label, 1, 0 ); | ||
69 | mainLayout->addWidget( mFileNameEdit, 1, 1 ); | ||
70 | |||
71 | |||
72 | /*US lets hardcode the formats instead of using a factory | ||
73 | FormatFactory *factory = FormatFactory::self(); | ||
74 | QStringList formats = factory->formats(); | ||
75 | QStringList::Iterator it; | ||
76 | |||
77 | for ( it = formats.begin(); it != formats.end(); ++it ) { | ||
78 | FormatInfo *info = factory->info( *it ); | ||
79 | if ( info ) { | ||
80 | mFormatTypes << (*it); | ||
81 | mFormatBox->insertItem( info->nameLabel ); | ||
82 | } | ||
83 | } | ||
84 | */ | ||
85 | mFormatTypes << "vcard"; | ||
86 | mFormatTypes << "binary"; | ||
87 | mFormatBox->insertItem( "vcard" ); | ||
88 | mFormatBox->insertItem( "binary" ); | ||
89 | |||
90 | mInEditMode = false; | ||
91 | } | ||
92 | |||
93 | void ResourceFileConfig::setEditMode( bool value ) | ||
94 | { | ||
95 | mFormatBox->setEnabled( !value ); | ||
96 | mInEditMode = value; | ||
97 | } | ||
98 | |||
99 | void ResourceFileConfig::loadSettings( KRES::Resource *res ) | ||
100 | { | ||
101 | //US ResourceFile *resource = dynamic_cast<ResourceFile*>( res ); | ||
102 | ResourceFile *resource = (ResourceFile*)( res ); | ||
103 | |||
104 | if ( !resource ) { | ||
105 | kdDebug(5700) << "ResourceFileConfig::loadSettings(): cast failed" << endl; | ||
106 | return; | ||
107 | } | ||
108 | |||
109 | mFormatBox->setCurrentItem( mFormatTypes.findIndex( resource->format() ) ); | ||
110 | |||
111 | mFileNameEdit->setURL( resource->fileName() ); | ||
112 | if ( mFileNameEdit->url().isEmpty() ) | ||
113 | mFileNameEdit->setURL( KABC::StdAddressBook::fileName() ); | ||
114 | } | ||
115 | |||
116 | void ResourceFileConfig::saveSettings( KRES::Resource *res ) | ||
117 | { | ||
118 | //US ResourceFile *resource = dynamic_cast<ResourceFile*>( res ); | ||
119 | ResourceFile *resource = (ResourceFile*)( res ); | ||
120 | |||
121 | if ( !resource ) { | ||
122 | kdDebug(5700) << "ResourceFileConfig::saveSettings(): cast failed" << endl; | ||
123 | return; | ||
124 | } | ||
125 | |||
126 | if ( !mInEditMode ) | ||
127 | resource->setFormat( mFormatTypes[ mFormatBox->currentItem() ] ); | ||
128 | |||
129 | resource->setFileName( mFileNameEdit->url() ); | ||
130 | } | ||
131 | void ResourceFileConfig::checkFilePermissions( const QString& fileName ) | ||
132 | { | ||
133 | // If file exist but is not writeable... | ||
134 | #ifdef _WIN32_ | ||
135 | QFileInfo fi ( QFile::encodeName( fileName ) ); | ||
136 | if ( fi.exists() ) | ||
137 | emit setReadOnly(!fi.isReadable() ); | ||
138 | #else | ||
139 | if ( access( QFile::encodeName( fileName ), F_OK ) == 0 ) | ||
140 | emit setReadOnly( access( QFile::encodeName( fileName ), W_OK ) < 0 ); | ||
141 | #endif | ||
142 | } | ||
143 | |||
144 | //US #include "resourcefileconfig.moc" | ||
diff --git a/kabc/plugins/file/resourcefileconfig.h b/kabc/plugins/file/resourcefileconfig.h new file mode 100644 index 0000000..31ccaaf --- a/dev/null +++ b/kabc/plugins/file/resourcefileconfig.h | |||
@@ -0,0 +1,65 @@ | |||
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 | |||
22 | /* | ||
23 | Enhanced Version of the file for platform independent KDE tools. | ||
24 | Copyright (c) 2004 Ulf Schenk | ||
25 | |||
26 | $Id$ | ||
27 | */ | ||
28 | |||
29 | #ifndef RESOURCEFILECONFIG_H | ||
30 | #define RESOURCEFILECONFIG_H | ||
31 | |||
32 | #include <kcombobox.h> | ||
33 | #include <kurlrequester.h> | ||
34 | |||
35 | #include <kresources/configwidget.h> | ||
36 | |||
37 | namespace KABC { | ||
38 | |||
39 | class ResourceFileConfig : public KRES::ConfigWidget | ||
40 | { | ||
41 | Q_OBJECT | ||
42 | |||
43 | public: | ||
44 | ResourceFileConfig( QWidget* parent = 0, const char* name = 0 ); | ||
45 | |||
46 | void setEditMode( bool value ); | ||
47 | |||
48 | public slots: | ||
49 | void loadSettings( KRES::Resource *resource ); | ||
50 | void saveSettings( KRES::Resource *resource ); | ||
51 | |||
52 | protected slots: | ||
53 | void checkFilePermissions( const QString& fileName ); | ||
54 | |||
55 | private: | ||
56 | KComboBox* mFormatBox; | ||
57 | KURLRequester* mFileNameEdit; | ||
58 | bool mInEditMode; | ||
59 | |||
60 | QStringList mFormatTypes; | ||
61 | }; | ||
62 | |||
63 | } | ||
64 | |||
65 | #endif | ||