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/dir/resourcedir.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'kabc/plugins/dir/resourcedir.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kabc/plugins/dir/resourcedir.cpp | 363 |
1 files changed, 363 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 | /* | ||
22 | Enhanced Version of the file for platform independent KDE tools. | ||
23 | Copyright (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 | |||
62 | using namespace KABC; | ||
63 | |||
64 | extern "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 | |||
75 | ResourceDir::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 | |||
119 | ResourceDir::~ResourceDir() | ||
120 | { | ||
121 | delete mFormat; | ||
122 | mFormat = 0; | ||
123 | } | ||
124 | |||
125 | void ResourceDir::writeConfig( KConfig *config ) | ||
126 | { | ||
127 | Resource::writeConfig( config ); | ||
128 | |||
129 | config->writeEntry( "FilePath", mPath ); | ||
130 | config->writeEntry( "FileFormat", mFormatName ); | ||
131 | } | ||
132 | |||
133 | Ticket *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 | |||
148 | bool 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 | |||
171 | void ResourceDir::doClose() | ||
172 | { | ||
173 | } | ||
174 | |||
175 | bool 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 | |||
202 | bool 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 | |||
233 | bool 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 | |||
281 | void 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 | |||
295 | void 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 | |||
313 | QString ResourceDir::path() const | ||
314 | { | ||
315 | return mPath; | ||
316 | } | ||
317 | |||
318 | void 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 | |||
328 | qDebug("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 | |||
339 | QString ResourceDir::format() const | ||
340 | { | ||
341 | return mFormatName; | ||
342 | } | ||
343 | |||
344 | void ResourceDir::pathChanged() | ||
345 | { | ||
346 | if ( !addressBook() ) | ||
347 | return; | ||
348 | |||
349 | load(); | ||
350 | addressBook()->emitAddressBookChanged(); | ||
351 | } | ||
352 | |||
353 | void ResourceDir::removeAddressee( const Addressee& addr ) | ||
354 | { | ||
355 | QFile::remove( mPath + "/" + addr.uid() ); | ||
356 | } | ||
357 | |||
358 | void ResourceDir::cleanUp() | ||
359 | { | ||
360 | unlock( mPath ); | ||
361 | } | ||
362 | |||
363 | //US #include "resourcedir.moc" | ||