Diffstat (limited to 'kabc/plugins/sharpdtm/resourcesharpdtm.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.cpp | 355 |
1 files changed, 355 insertions, 0 deletions
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp new file mode 100644 index 0000000..347d51c --- a/dev/null +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp | |||
@@ -0,0 +1,355 @@ | |||
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 <qdir.h> | ||
33 | #include <qfile.h> | ||
34 | #include <qfileinfo.h> | ||
35 | #include <qregexp.h> | ||
36 | //US #include <qtimer.h> | ||
37 | |||
38 | #include <kapplication.h> | ||
39 | #include <kconfig.h> | ||
40 | #include <kdebug.h> | ||
41 | #include <klocale.h> | ||
42 | //US #include <ksavefile.h> | ||
43 | #include <kstandarddirs.h> | ||
44 | #include <kmessagebox.h> | ||
45 | |||
46 | #include <sl/slzdb.h> | ||
47 | |||
48 | |||
49 | #include "resourcesharpdtmconfig.h" | ||
50 | #include "stdaddressbook.h" | ||
51 | |||
52 | //#include "qtopiaconverter.h" | ||
53 | |||
54 | #include "resourcesharpdtm.h" | ||
55 | |||
56 | using namespace KABC; | ||
57 | extern "C" | ||
58 | { | ||
59 | void *init_microkabc_sharpdtm() | ||
60 | { | ||
61 | return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>(); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | ResourceSharpDTM::ResourceSharpDTM( const KConfig *config ) | ||
66 | : Resource( config ), mConverter (0) | ||
67 | { | ||
68 | // we can not choose the filename. Therefore use the default to display | ||
69 | |||
70 | QString fileName = SlZDataBase::addressbookFileName(); | ||
71 | init( fileName ); | ||
72 | } | ||
73 | |||
74 | ResourceSharpDTM::ResourceSharpDTM( const QString &fileName ) | ||
75 | : Resource( 0 ) | ||
76 | { | ||
77 | // qDebug("ResourceFile::ResourceSharpDTM : 3 %s, %s", fileName.latin1(), formatName.latin1()); | ||
78 | init( fileName ); | ||
79 | } | ||
80 | |||
81 | void ResourceSharpDTM::init( const QString &fileName ) | ||
82 | { | ||
83 | |||
84 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | ||
85 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | ||
86 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | ||
87 | |||
88 | setFileName( fileName ); | ||
89 | } | ||
90 | |||
91 | ResourceSharpDTM::~ResourceSharpDTM() | ||
92 | { | ||
93 | // if (mConverter != 0) | ||
94 | // delete mConverter; | ||
95 | |||
96 | } | ||
97 | |||
98 | void ResourceSharpDTM::writeConfig( KConfig *config ) | ||
99 | { | ||
100 | Resource::writeConfig( config ); | ||
101 | } | ||
102 | |||
103 | Ticket *ResourceSharpDTM::requestSaveTicket() | ||
104 | { | ||
105 | kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; | ||
106 | |||
107 | if ( !addressBook() ) return 0; | ||
108 | |||
109 | if ( !lock( mFileName ) ) { | ||
110 | kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" | ||
111 | << mFileName << "'" << endl; | ||
112 | return 0; | ||
113 | } | ||
114 | return createTicket( this ); | ||
115 | } | ||
116 | |||
117 | |||
118 | bool ResourceSharpDTM::doOpen() | ||
119 | { | ||
120 | /*US | ||
121 | QFile file( mFileName ); | ||
122 | |||
123 | if ( !file.exists() ) { | ||
124 | // try to create the file | ||
125 | bool ok = file.open( IO_WriteOnly ); | ||
126 | if ( ok ) | ||
127 | file.close(); | ||
128 | |||
129 | return ok; | ||
130 | } else { | ||
131 | if ( !file.open( IO_ReadWrite ) ) | ||
132 | return false; | ||
133 | |||
134 | if ( file.size() == 0 ) { | ||
135 | file.close(); | ||
136 | return true; | ||
137 | } | ||
138 | |||
139 | //US bool ok = mFormat->checkFormat( &file ); | ||
140 | bool ok = true; | ||
141 | |||
142 | file.close(); | ||
143 | |||
144 | return ok; | ||
145 | } | ||
146 | */ | ||
147 | return true; | ||
148 | } | ||
149 | |||
150 | void ResourceSharpDTM::doClose() | ||
151 | { | ||
152 | } | ||
153 | |||
154 | bool ResourceSharpDTM::load() | ||
155 | { | ||
156 | kdDebug(5700) << "ResourcSharpDTM::load(): '" << mFileName << "'" << endl; | ||
157 | |||
158 | // qDebug("ResourceSharpDTM::load: Try to load file() %s", mFileName.latin1()); | ||
159 | |||
160 | SlZDataBase* access = new SlZDataBase(mFileName, | ||
161 | SlZDataBase::addressbookItems()); | ||
162 | if ( !access ) { | ||
163 | qDebug("Unable to load file() %s", mFileName.latin1()); | ||
164 | addressBook()->error( i18n( "Unable to load file '%1'." ).arg( mFileName ) ); | ||
165 | return false; | ||
166 | } | ||
167 | |||
168 | bool res = false; | ||
169 | if (mConverter == 0) | ||
170 | { | ||
171 | // mConverter = new QtopiaConverter(); | ||
172 | // res = mConverter->init(); | ||
173 | res = true; | ||
174 | if ( !res ) | ||
175 | { | ||
176 | qDebug("Unable to initialize qtopia converter. Most likely a problem with the category file"); | ||
177 | addressBook()->error( i18n( "Unable to initialize qtopia converter. Most likely a problem with the category file" ) ); | ||
178 | delete access; | ||
179 | return false; | ||
180 | } | ||
181 | } | ||
182 | /* | ||
183 | { //create a new scope | ||
184 | AddressBookIterator it(*access); | ||
185 | const PimContact* contact; | ||
186 | |||
187 | for (contact=it.toFirst(); it.current(); ++it) { | ||
188 | contact = it.current(); | ||
189 | |||
190 | KABC::Addressee addressee; | ||
191 | |||
192 | res = mConverter->qtopiaToAddressee( (*contact), addressee ); | ||
193 | |||
194 | if ( !addressee.isEmpty() && res ) | ||
195 | { | ||
196 | addressee.setResource( this ); | ||
197 | addressBook()->insertAddressee( addressee ); | ||
198 | } | ||
199 | } | ||
200 | } | ||
201 | */ | ||
202 | delete access; | ||
203 | return true; | ||
204 | } | ||
205 | |||
206 | bool ResourceSharpDTM::save( Ticket *ticket ) | ||
207 | { | ||
208 | /*US | ||
209 | // qDebug("ResourceFile::save format %s, %s", mFileName.latin1(), mFormatName.latin1()); | ||
210 | kdDebug(5700) << "ResourceQtopia::save()" << endl; | ||
211 | |||
212 | // create backup file | ||
213 | QString extension = "_" + QString::number( QDate::currentDate().dayOfWeek() ); | ||
214 | |||
215 | /*US we use a simpler method to create a backupfile | ||
216 | |||
217 | (void) KSaveFile::backupFile( mFileName, QString::null | ||
218 | ,extension ); | ||
219 | |||
220 | KSaveFile saveFile( mFileName ); | ||
221 | bool ok = false; | ||
222 | if ( saveFile.status() == 0 && saveFile.file() ) | ||
223 | { | ||
224 | mFormat->saveAll( addressBook(), this, saveFile.file() ); | ||
225 | ok = saveFile.close(); | ||
226 | } | ||
227 | */ | ||
228 | |||
229 | /*US | ||
230 | //US ToDo: write backupfile | ||
231 | QFile info; | ||
232 | info.setName( mFileName ); | ||
233 | bool ok = info.open( IO_WriteOnly ); | ||
234 | if ( ok ) { | ||
235 | //US mFormat->saveAll( addressBook(), this, &info ); | ||
236 | |||
237 | info.close(); | ||
238 | ok = true; | ||
239 | } | ||
240 | else { | ||
241 | |||
242 | } | ||
243 | |||
244 | if ( !ok ) | ||
245 | addressBook()->error( i18n( "Unable to save file '%1'." ).arg( mFileName ) ); | ||
246 | |||
247 | delete ticket; | ||
248 | unlock( mFileName ); | ||
249 | |||
250 | return ok; | ||
251 | |||
252 | qDebug("ResourceQtopia::save has to be changed"); | ||
253 | */ | ||
254 | return true; | ||
255 | } | ||
256 | |||
257 | bool ResourceSharpDTM::lock( const QString &fileName ) | ||
258 | { | ||
259 | kdDebug(5700) << "ResourceSharpDTM::lock()" << endl; | ||
260 | |||
261 | QString fn = fileName; | ||
262 | |||
263 | //US change the implementation how the lockfilename is getting created | ||
264 | //US fn.replace( QRegExp("/"), "_" ); | ||
265 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | ||
266 | |||
267 | KURL url(fn); | ||
268 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | ||
269 | |||
270 | kdDebug(5700) << "-- lock name: " << lockName << endl; | ||
271 | |||
272 | if (QFile::exists( lockName )) return false; | ||
273 | |||
274 | QString lockUniqueName; | ||
275 | lockUniqueName = fn + KApplication::randomString( 8 ); | ||
276 | |||
277 | url = lockUniqueName; | ||
278 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | ||
279 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | ||
280 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | ||
281 | |||
282 | // Create unique file | ||
283 | QFile file( mLockUniqueName ); | ||
284 | file.open( IO_WriteOnly ); | ||
285 | file.close(); | ||
286 | |||
287 | // Create lock file | ||
288 | int result = ::link( QFile::encodeName( mLockUniqueName ), | ||
289 | QFile::encodeName( lockName ) ); | ||
290 | |||
291 | if ( result == 0 ) { | ||
292 | addressBook()->emitAddressBookLocked(); | ||
293 | return true; | ||
294 | } | ||
295 | |||
296 | // TODO: check stat | ||
297 | |||
298 | return false; | ||
299 | } | ||
300 | |||
301 | void ResourceSharpDTM::unlock( const QString &fileName ) | ||
302 | { | ||
303 | QString fn = fileName; | ||
304 | //US change the implementation how the lockfilename is getting created | ||
305 | //US fn.replace( QRegExp( "/" ), "_" ); | ||
306 | //US QString lockName = locateLocal( "data", "kabc/lock/" + fn + ".lock" ); | ||
307 | //US QString lockName = fn + ".lock"; | ||
308 | KURL url(fn); | ||
309 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | ||
310 | |||
311 | QFile::remove( lockName ); | ||
312 | QFile::remove( mLockUniqueName ); | ||
313 | addressBook()->emitAddressBookUnlocked(); | ||
314 | } | ||
315 | |||
316 | void ResourceSharpDTM::setFileName( const QString &fileName ) | ||
317 | { | ||
318 | mDirWatch.stopScan(); | ||
319 | mDirWatch.removeFile( mFileName ); | ||
320 | |||
321 | mFileName = fileName; | ||
322 | |||
323 | mDirWatch.addFile( mFileName ); | ||
324 | mDirWatch.startScan(); | ||
325 | |||
326 | } | ||
327 | |||
328 | QString ResourceSharpDTM::fileName() const | ||
329 | { | ||
330 | return mFileName; | ||
331 | } | ||
332 | |||
333 | void ResourceSharpDTM::fileChanged() | ||
334 | { | ||
335 | // There is a small theoretical chance that KDirWatch calls us before | ||
336 | // we are fully constructed | ||
337 | if (!addressBook()) | ||
338 | return; | ||
339 | |||
340 | QString text( i18n( "Sharp DTM resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( mFileName ) ); | ||
341 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | ||
342 | load(); | ||
343 | addressBook()->emitAddressBookChanged(); | ||
344 | } | ||
345 | } | ||
346 | |||
347 | void ResourceSharpDTM::removeAddressee( const Addressee &addr ) | ||
348 | { | ||
349 | } | ||
350 | |||
351 | void ResourceSharpDTM::cleanUp() | ||
352 | { | ||
353 | unlock( mFileName ); | ||
354 | } | ||
355 | |||