author | ulf69 <ulf69> | 2004-06-29 03:21:44 (UTC) |
---|---|---|
committer | ulf69 <ulf69> | 2004-06-29 03:21:44 (UTC) |
commit | 659b21aed6e02154a1b38ff16a09a432fe3953cb (patch) (unidiff) | |
tree | 17d5e9791d4d3c47ec136b05328c1f402ba4c50a /kabc/plugins/opie/resourceopie.cpp | |
parent | a52c5f5a0b10cff057d9bca6046d50bfefb6896c (diff) | |
download | kdepimpi-659b21aed6e02154a1b38ff16a09a432fe3953cb.zip kdepimpi-659b21aed6e02154a1b38ff16a09a432fe3953cb.tar.gz kdepimpi-659b21aed6e02154a1b38ff16a09a432fe3953cb.tar.bz2 |
added support for opie addressbook resources
Diffstat (limited to 'kabc/plugins/opie/resourceopie.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kabc/plugins/opie/resourceopie.cpp | 345 |
1 files changed, 345 insertions, 0 deletions
diff --git a/kabc/plugins/opie/resourceopie.cpp b/kabc/plugins/opie/resourceopie.cpp new file mode 100644 index 0000000..47e22b4 --- a/dev/null +++ b/kabc/plugins/opie/resourceopie.cpp | |||
@@ -0,0 +1,345 @@ | |||
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 | #include <unistd.h> | ||
31 | |||
32 | #include <qfile.h> | ||
33 | #include <qfileinfo.h> | ||
34 | #include <qregexp.h> | ||
35 | #include <qtimer.h> | ||
36 | |||
37 | #include <kapplication.h> | ||
38 | #include <kconfig.h> | ||
39 | #include <kdebug.h> | ||
40 | #include <klocale.h> | ||
41 | //US #include <ksavefile.h> | ||
42 | #include <kstandarddirs.h> | ||
43 | |||
44 | //US #include "formatfactory.h" | ||
45 | |||
46 | #include "resource.h" | ||
47 | #include "resourceopieconfig.h" | ||
48 | #include "stdaddressbook.h" | ||
49 | |||
50 | #include "resourceopie.h" | ||
51 | |||
52 | using namespace KABC; | ||
53 | extern "C" | ||
54 | { | ||
55 | //US void *init_kabc_file() | ||
56 | void *init_microkabc_opie() | ||
57 | { | ||
58 | return new KRES::PluginFactory<ResourceOpie,ResourceOpieConfig>(); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | ResourceOpie::ResourceOpie( const KConfig *config ) | ||
63 | : Resource( config ) | ||
64 | { | ||
65 | QString fileName; | ||
66 | |||
67 | KConfig *cfg = (KConfig *)config; | ||
68 | if ( cfg ) { | ||
69 | fileName = cfg->readEntry( "FileName", StdAddressBook::fileName() ); | ||
70 | // qDebug("ResourceFile::ResourceFile : 1 %s, %s", fileName.latin1(), formatName.latin1() ); | ||
71 | } else { | ||
72 | fileName = StdAddressBook::fileName(); | ||
73 | // qDebug("ResourceFile::ResourceFile : 2 %s, %s", fileName.latin1(), formatName.latin1() ); | ||
74 | } | ||
75 | |||
76 | init( fileName ); | ||
77 | } | ||
78 | |||
79 | ResourceOpie::ResourceOpie( const QString &fileName ) | ||
80 | : Resource( 0 ) | ||
81 | { | ||
82 | // qDebug("ResourceFile::ResourceFile : 3 %s, %s", fileName.latin1(), formatName.latin1()); | ||
83 | init( fileName ); | ||
84 | } | ||
85 | |||
86 | void ResourceOpie::init( const QString &fileName ) | ||
87 | { | ||
88 | |||
89 | /*US we have no KDirWatch. SO simulate the signals from inside the apropriate methods | ||
90 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | ||
91 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | ||
92 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | ||
93 | */ | ||
94 | |||
95 | setFileName( fileName ); | ||
96 | } | ||
97 | |||
98 | ResourceOpie::~ResourceOpie() | ||
99 | { | ||
100 | } | ||
101 | |||
102 | void ResourceOpie::writeConfig( KConfig *config ) | ||
103 | { | ||
104 | Resource::writeConfig( config ); | ||
105 | |||
106 | config->writeEntry( "FileName", mFileName ); | ||
107 | |||
108 | // qDebug("ResourceFile::writeConfig format %s, %s", mFileName.latin1(), mFormatName.latin1()); | ||
109 | |||
110 | } | ||
111 | |||
112 | Ticket *ResourceOpie::requestSaveTicket() | ||
113 | { | ||
114 | kdDebug(5700) << "ResourceOpie::requestSaveTicket()" << endl; | ||
115 | |||
116 | if ( !addressBook() ) return 0; | ||
117 | |||
118 | if ( !lock( mFileName ) ) { | ||
119 | kdDebug(5700) << "ResourceOpie::requestSaveTicket(): Unable to lock file '" | ||
120 | << mFileName << "'" << endl; | ||
121 | return 0; | ||
122 | } | ||
123 | return createTicket( this ); | ||
124 | } | ||
125 | |||
126 | |||
127 | bool ResourceOpie::doOpen() | ||
128 | { | ||
129 | QFile file( mFileName ); | ||
130 | |||
131 | if ( !file.exists() ) { | ||
132 | // try to create the file | ||
133 | bool ok = file.open( IO_WriteOnly ); | ||
134 | if ( ok ) | ||
135 | file.close(); | ||
136 | |||
137 | return ok; | ||
138 | } else { | ||
139 | if ( !file.open( IO_ReadWrite ) ) | ||
140 | return false; | ||
141 | |||
142 | if ( file.size() == 0 ) { | ||
143 | file.close(); | ||
144 | return true; | ||
145 | } | ||
146 | |||
147 | //US bool ok = mFormat->checkFormat( &file ); | ||
148 | bool ok = true; | ||
149 | |||
150 | file.close(); | ||
151 | |||
152 | return ok; | ||
153 | } | ||
154 | } | ||
155 | |||
156 | void ResourceOpie::doClose() | ||
157 | { | ||
158 | } | ||
159 | |||
160 | bool ResourceOpie::load() | ||
161 | { | ||
162 | kdDebug(5700) << "ResourceOpie::load(): '" << mFileName << "'" << endl; | ||
163 | |||
164 | QFile file( mFileName ); | ||
165 | if ( !file.open( IO_ReadOnly ) ) { | ||
166 | addressBook()->error( i18n( "Unable to open file '%1'." ).arg( mFileName ) ); | ||
167 | return false; | ||
168 | } | ||
169 | |||
170 | // qDebug("ResourceFile::load format %s, %s", mFileName.latin1(), mFormatName.latin1()); | ||
171 | |||
172 | //US return mFormat->loadAll( addressBook(), this, &file ); | ||
173 | return true; | ||
174 | } | ||
175 | |||
176 | bool ResourceOpie::save( Ticket *ticket ) | ||
177 | { | ||
178 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | ||
179 | kdDebug(5700) << "ResourceOpie::save()" << endl; | ||
180 | |||
181 | // create backup file | ||
182 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | ||
183 | |||
184 | /*US we use a simpler method to create a backupfile | ||
185 | |||
186 | (void) KSaveFile::backupFile( mFileName, QString::null | ||
187 | ,extension ); | ||
188 | |||
189 | KSaveFile saveFile( mFileName ); | ||
190 | bool ok = false; | ||
191 | if ( saveFile.status() == 0 && saveFile.file() ) | ||
192 | { | ||
193 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | ||
194 | ok = saveFile.close(); | ||
195 | } | ||
196 | */ | ||
197 | |||
198 | //US ToDo: write backupfile | ||
199 | QFile info; | ||
200 | info.setName( mFileName ); | ||
201 | bool ok = info.open( IO_WriteOnly ); | ||
202 | if ( ok ) { | ||
203 | //US mFormat->saveAll( addressBook(), this, &info ); | ||
204 | |||
205 | info.close(); | ||
206 | ok = true; | ||
207 | } | ||
208 | else { | ||
209 | |||
210 | } | ||
211 | |||
212 | if ( !ok ) | ||
213 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); | ||
214 | |||
215 | delete ticket; | ||
216 | unlock( mFileName ); | ||
217 | |||
218 | return ok; | ||
219 | |||
220 | qDebug("ResourceOpie::save has to be changed"); | ||
221 | return true; | ||
222 | } | ||
223 | |||
224 | bool ResourceOpie::lock( const QString &fileName ) | ||
225 | { | ||
226 | kdDebug(5700) << "ResourceOpie::lock()" << endl; | ||
227 | |||
228 | QString fn = fileName; | ||
229 | |||
230 | //US change the implementation how the lockfilename is getting created | ||
231 | //US fn.replace( QRegExp("/"), "_" ); | ||
232 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | ||
233 | |||
234 | KURL url(fn); | ||
235 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | ||
236 | |||
237 | kdDebug(5700) << "-- lock name: " << lockName << endl; | ||
238 | |||
239 | if (QFile::exists( lockName )) return false; | ||
240 | |||
241 | QString lockUniqueName; | ||
242 | lockUniqueName = fn + KApplication::randomString( 8 ); | ||
243 | |||
244 | url = lockUniqueName; | ||
245 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | ||
246 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | ||
247 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | ||
248 | |||
249 | // Create unique file | ||
250 | QFile file( mLockUniqueName ); | ||
251 | file.open( IO_WriteOnly ); | ||
252 | file.close(); | ||
253 | |||
254 | // Create lock file | ||
255 | int result = ::link( QFile::encodeName( mLockUniqueName ), | ||
256 | QFile::encodeName( lockName ) ); | ||
257 | |||
258 | if ( result == 0 ) { | ||
259 | addressBook()->emitAddressBookLocked(); | ||
260 | return true; | ||
261 | } | ||
262 | |||
263 | // TODO: check stat | ||
264 | |||
265 | return false; | ||
266 | } | ||
267 | |||
268 | void ResourceOpie::unlock( const QString &fileName ) | ||
269 | { | ||
270 | QString fn = fileName; | ||
271 | //US change the implementation how the lockfilename is getting created | ||
272 | //US fn.replace( QRegExp( "/" ), "_" ); | ||
273 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | ||
274 | //US QString lockName = fn + ".lock"; | ||
275 | KURL url(fn); | ||
276 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | ||
277 | |||
278 | QFile::remove( lockName ); | ||
279 | QFile::remove( mLockUniqueName ); | ||
280 | addressBook()->emitAddressBookUnlocked(); | ||
281 | } | ||
282 | |||
283 | void ResourceOpie::setFileName( const QString &fileName ) | ||
284 | { | ||
285 | /*US ToDo: no synchronization so far. Has to be changed in the future | ||
286 | mDirWatch.stopScan(); | ||
287 | mDirWatch.removeFile( mFileName ); | ||
288 | */ | ||
289 | mFileName = fileName; | ||
290 | |||
291 | |||
292 | /*US ToDo: no synchronization so far. Has to be changed in the future | ||
293 | mDirWatch.addFile( mFileName ); | ||
294 | mDirWatch.startScan(); | ||
295 | */ | ||
296 | //US simulate KDirWatch event | ||
297 | fileChanged(); | ||
298 | } | ||
299 | |||
300 | QString ResourceOpie::fileName() const | ||
301 | { | ||
302 | return mFileName; | ||
303 | } | ||
304 | |||
305 | /*US | ||
306 | void ResourceOpie::setFormat( const QString &format ) | ||
307 | { | ||
308 | mFormatName = format; | ||
309 | delete mFormat; | ||
310 | |||
311 | FormatFactory *factory = FormatFactory::self(); | ||
312 | mFormat = factory->format( mFormatName ); | ||
313 | } | ||
314 | |||
315 | QString ResourceOpie::format() const | ||
316 | { | ||
317 | return mFormatName; | ||
318 | } | ||
319 | */ | ||
320 | |||
321 | void ResourceOpie::fileChanged() | ||
322 | { | ||
323 | // There is a small theoretical chance that KDirWatch calls us before | ||
324 | // we are fully constructed | ||
325 | if (!addressBook()) | ||
326 | return; | ||
327 | load(); | ||
328 | addressBook()->emitAddressBookChanged(); | ||
329 | } | ||
330 | |||
331 | void ResourceOpie::removeAddressee( const Addressee &addr ) | ||
332 | { | ||
333 | /*US | ||
334 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/photos/" ) + addr.uid() ) ); | ||
335 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/logos/" ) + addr.uid() ) ); | ||
336 | QFile::remove( QFile::encodeName( locateLocal( "data", "kabc/sounds/" ) + addr.uid() ) ); | ||
337 | */ | ||
338 | } | ||
339 | |||
340 | void ResourceOpie::cleanUp() | ||
341 | { | ||
342 | unlock( mFileName ); | ||
343 | } | ||
344 | |||
345 | //US #include "resourceopie.moc" | ||