-rw-r--r-- | kabc/plugins/qtopia/resourceqtopia.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp index 242e0c6..5565580 100644 --- a/kabc/plugins/qtopia/resourceqtopia.cpp +++ b/kabc/plugins/qtopia/resourceqtopia.cpp | |||
@@ -1,319 +1,319 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of libkabc. | 2 | This file is part of libkabc. |
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | 3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> |
4 | 4 | ||
5 | This library is free software; you can redistribute it and/or | 5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public | 6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either | 7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. | 8 | version 2 of the License, or (at your option) any later version. |
9 | 9 | ||
10 | This library is distributed in the hope that it will be useful, | 10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. | 13 | Library General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Library General Public License | 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 | 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, | 17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /* | 21 | /* |
22 | Enhanced Version of the file for platform independent KDE tools. | 22 | Enhanced Version of the file for platform independent KDE tools. |
23 | Copyright (c) 2004 Ulf Schenk | 23 | Copyright (c) 2004 Ulf Schenk |
24 | 24 | ||
25 | $Id$ | 25 | $Id$ |
26 | */ | 26 | */ |
27 | #include <sys/types.h> | 27 | #include <sys/types.h> |
28 | #include <sys/stat.h> | 28 | #include <sys/stat.h> |
29 | #include <unistd.h> | 29 | #include <unistd.h> |
30 | 30 | ||
31 | #include <qdir.h> | 31 | #include <qdir.h> |
32 | #include <qfile.h> | 32 | #include <qfile.h> |
33 | #include <qfileinfo.h> | 33 | #include <qfileinfo.h> |
34 | #include <qregexp.h> | 34 | #include <qregexp.h> |
35 | //US #include <qtimer.h> | 35 | //US #include <qtimer.h> |
36 | 36 | ||
37 | #include <kapplication.h> | 37 | #include <kapplication.h> |
38 | #include <kconfig.h> | 38 | #include <kconfig.h> |
39 | #include <kdebug.h> | 39 | #include <kdebug.h> |
40 | #include <klocale.h> | 40 | #include <klocale.h> |
41 | //US #include <ksavefile.h> | 41 | //US #include <ksavefile.h> |
42 | #include <kstandarddirs.h> | 42 | #include <kstandarddirs.h> |
43 | #include <kmessagebox.h> | 43 | #include <kmessagebox.h> |
44 | 44 | ||
45 | #include <qpe/pim/addressbookaccess.h> | 45 | #include <qpe/pim/addressbookaccess.h> |
46 | 46 | ||
47 | 47 | ||
48 | #include "resourceqtopiaconfig.h" | 48 | #include "resourceqtopiaconfig.h" |
49 | #include "stdaddressbook.h" | 49 | #include "stdaddressbook.h" |
50 | 50 | ||
51 | #include "qtopiaconverter.h" | 51 | #include "qtopiaconverter.h" |
52 | #include "syncprefwidget.h" | 52 | #include "syncprefwidget.h" |
53 | 53 | ||
54 | #include "resourceqtopia.h" | 54 | #include "resourceqtopia.h" |
55 | 55 | ||
56 | using namespace KABC; | 56 | using namespace KABC; |
57 | extern "C" | 57 | extern "C" |
58 | { | 58 | { |
59 | void *init_microkabc_qtopia() | 59 | void *init_microkabc_qtopia() |
60 | { | 60 | { |
61 | return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, SyncPrefWidget>(); | 61 | return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig, SyncPrefWidget>(); |
62 | } | 62 | } |
63 | } | 63 | } |
64 | 64 | ||
65 | ResourceQtopia::ResourceQtopia( const KConfig *config, bool syncable ) | 65 | ResourceQtopia::ResourceQtopia( const KConfig *config, bool syncable ) |
66 | : Resource( config, syncable ), mConverter (0) | 66 | : Resource( config, syncable ), mConverter (0) |
67 | { | 67 | { |
68 | // we can not choose the filename. Therefore use the default to display | 68 | // we can not choose the filename. Therefore use the default to display |
69 | QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; | 69 | QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; |
70 | init( fileName ); | 70 | init( fileName ); |
71 | } | 71 | } |
72 | 72 | ||
73 | ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable ) | 73 | ResourceQtopia::ResourceQtopia( const QString &fileName, bool syncable ) |
74 | : Resource( 0, syncable ) | 74 | : Resource( 0, syncable ) |
75 | { | 75 | { |
76 | init( fileName ); | 76 | init( fileName ); |
77 | } | 77 | } |
78 | 78 | ||
79 | void ResourceQtopia::init( const QString &fileName ) | 79 | void ResourceQtopia::init( const QString &fileName ) |
80 | { | 80 | { |
81 | 81 | ||
82 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); | 82 | connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); |
83 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); | 83 | connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); |
84 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); | 84 | connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); |
85 | 85 | ||
86 | setFileName( fileName ); | 86 | setFileName( fileName ); |
87 | } | 87 | } |
88 | 88 | ||
89 | ResourceQtopia::~ResourceQtopia() | 89 | ResourceQtopia::~ResourceQtopia() |
90 | { | 90 | { |
91 | if (mConverter != 0) | 91 | if (mConverter != 0) |
92 | delete mConverter; | 92 | delete mConverter; |
93 | 93 | ||
94 | if(mAccess != 0) | 94 | if(mAccess != 0) |
95 | delete mAccess; | 95 | delete mAccess; |
96 | } | 96 | } |
97 | 97 | ||
98 | void ResourceQtopia::writeConfig( KConfig *config ) | 98 | void ResourceQtopia::writeConfig( KConfig *config ) |
99 | { | 99 | { |
100 | Resource::writeConfig( config ); | 100 | Resource::writeConfig( config ); |
101 | } | 101 | } |
102 | 102 | ||
103 | Ticket *ResourceQtopia::requestSaveTicket() | 103 | Ticket *ResourceQtopia::requestSaveTicket() |
104 | { | 104 | { |
105 | kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; | 105 | kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; |
106 | 106 | ||
107 | qDebug("ResourceQtopia::requestSaveTicket: %s", fileName().latin1()); | 107 | qDebug("ResourceQtopia::requestSaveTicket: %s", fileName().latin1()); |
108 | 108 | ||
109 | if ( !addressBook() ) return 0; | 109 | if ( !addressBook() ) return 0; |
110 | 110 | ||
111 | if ( !lock( fileName() ) ) { | 111 | if ( !lock( fileName() ) ) { |
112 | kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" | 112 | kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" |
113 | << fileName() << "'" << endl; | 113 | << fileName() << "'" << endl; |
114 | return 0; | 114 | return 0; |
115 | } | 115 | } |
116 | return createTicket( this ); | 116 | return createTicket( this ); |
117 | } | 117 | } |
118 | 118 | ||
119 | 119 | ||
120 | bool ResourceQtopia::doOpen() | 120 | bool ResourceQtopia::doOpen() |
121 | { | 121 | { |
122 | qDebug("ResourceQtopia::doOpen(): %s", fileName().latin1()); | 122 | qDebug("ResourceQtopia::doOpen(): %s", fileName().latin1()); |
123 | 123 | ||
124 | mAccess = new AddressBookAccess(); | 124 | mAccess = new AddressBookAccess(); |
125 | 125 | ||
126 | if ( !mAccess ) { | 126 | if ( !mAccess ) { |
127 | qDebug("Unable to load file() %s", fileName().latin1()); | 127 | qDebug("Unable to load file() %s", fileName().latin1()); |
128 | return false; | 128 | return false; |
129 | } | 129 | } |
130 | 130 | ||
131 | 131 | ||
132 | if (mConverter == 0) | 132 | if (mConverter == 0) |
133 | { | 133 | { |
134 | mConverter = new QtopiaConverter(); | 134 | mConverter = new QtopiaConverter(); |
135 | bool res = mConverter->init(); | 135 | bool res = mConverter->init(); |
136 | if ( !res ) | 136 | if ( !res ) |
137 | { | 137 | { |
138 | QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file"); | 138 | QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file"); |
139 | qDebug(msg); | 139 | qDebug(msg); |
140 | delete mAccess; | 140 | delete mAccess; |
141 | mAccess = 0; | 141 | mAccess = 0; |
142 | return false; | 142 | return false; |
143 | } | 143 | } |
144 | } | 144 | } |
145 | 145 | ||
146 | return true; | 146 | return true; |
147 | } | 147 | } |
148 | 148 | ||
149 | void ResourceQtopia::doClose() | 149 | void ResourceQtopia::doClose() |
150 | { | 150 | { |
151 | qDebug("ResourceQtopia::doClose: %s", fileName().latin1()); | 151 | qDebug("ResourceQtopia::doClose: %s", fileName().latin1()); |
152 | 152 | ||
153 | if(mAccess) | 153 | if(mAccess) |
154 | { | 154 | { |
155 | delete mAccess; | 155 | delete mAccess; |
156 | mAccess = 0; | 156 | mAccess = 0; |
157 | } | 157 | } |
158 | // it seems so, that deletion of access deletes backend as well | 158 | // it seems so, that deletion of access deletes backend as well |
159 | //delete backend; | 159 | //delete backend; |
160 | 160 | ||
161 | return; | 161 | return; |
162 | } | 162 | } |
163 | 163 | ||
164 | bool ResourceQtopia::load() | 164 | bool ResourceQtopia::load() |
165 | { | 165 | { |
166 | qDebug("ResourceQtopia::load: %s", fileName().latin1()); | 166 | qDebug("ResourceQtopia::load: %s", fileName().latin1()); |
167 | 167 | ||
168 | AddressBookIterator it(*mAccess); | 168 | AddressBookIterator it(*mAccess); |
169 | const PimContact* contact; | 169 | const PimContact* contact; |
170 | bool res; | 170 | bool res; |
171 | 171 | ||
172 | for (contact=it.toFirst(); it.current(); ++it) | 172 | for (contact=it.toFirst(); it.current(); ++it) |
173 | { | 173 | { |
174 | contact = it.current(); | 174 | contact = it.current(); |
175 | 175 | ||
176 | KABC::Addressee addressee; | 176 | KABC::Addressee addressee; |
177 | 177 | ||
178 | res = mConverter->qtopiaToAddressee( (*contact), addressee ); | 178 | res = mConverter->qtopiaToAddressee( (*contact), addressee ); |
179 | 179 | ||
180 | if ( !addressee.isEmpty() && res ) | 180 | if ( !addressee.isEmpty() && res ) |
181 | { | 181 | { |
182 | addressee.setResource( this ); | 182 | addressee.setResource( this ); |
183 | addressBook()->insertAddressee( addressee ); | 183 | addressBook()->insertAddressee( addressee ); |
184 | } | 184 | } |
185 | } | 185 | } |
186 | 186 | ||
187 | return true; | 187 | return true; |
188 | } | 188 | } |
189 | 189 | ||
190 | bool ResourceQtopia::save( Ticket *ticket ) | 190 | bool ResourceQtopia::save( Ticket *ticket ) |
191 | { | 191 | { |
192 | qDebug("ResourceQtopia::save: %s", fileName().latin1()); | 192 | qDebug("ResourceQtopia::save: %s", fileName().latin1()); |
193 | 193 | ||
194 | 194 | ||
195 | KABC::AddressBook::Iterator it; | 195 | KABC::AddressBook::Iterator it; |
196 | bool res; | 196 | bool res; |
197 | 197 | ||
198 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | 198 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { |
199 | PimContact c; | 199 | PimContact c; |
200 | KABC::Addressee addressee = (*it); | 200 | KABC::Addressee addressee = (*it); |
201 | 201 | ||
202 | res = mConverter->addresseeToQtopia( *it, c ); | 202 | res = mConverter->addresseeToQtopia( *it, c ); |
203 | if (res == true) | 203 | if (res == true) |
204 | { | 204 | { |
205 | mAccess->addContact(c); | 205 | mAccess->addContact(c); |
206 | // if (res == false) | 206 | // if (res == false) |
207 | // qDebug("Unable to append Contact %s", c.fullName().latin1()); | 207 | // qDebug("Unable to append Contact %s", c.fullName().latin1()); |
208 | } | 208 | } |
209 | else | 209 | else |
210 | { | 210 | { |
211 | qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); | 211 | qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); |
212 | } | 212 | } |
213 | } | 213 | } |
214 | 214 | ||
215 | // mAccess->addressBookUpdated(); | 215 | // mAccess->addressBookUpdated(); |
216 | 216 | ||
217 | delete ticket; | 217 | delete ticket; |
218 | unlock( fileName() ); | 218 | unlock( fileName() ); |
219 | 219 | ||
220 | return true; | 220 | return true; |
221 | } | 221 | } |
222 | 222 | ||
223 | bool ResourceQtopia::lock( const QString &lockfileName ) | 223 | bool ResourceQtopia::lock( const QString &lockfileName ) |
224 | { | 224 | { |
225 | qDebug("ResourceQtopia::lock: %s", fileName().latin1()); | 225 | qDebug("ResourceQtopia::lock: %s", fileName().latin1()); |
226 | 226 | ||
227 | kdDebug(5700) << "ResourceQtopia::lock()" << endl; | 227 | kdDebug(5700) << "ResourceQtopia::lock()" << endl; |
228 | 228 | ||
229 | QString fn = lockfileName; | 229 | QString fn = lockfileName; |
230 | 230 | ||
231 | KURL url(fn); | 231 | KURL url(fn); |
232 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 232 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
233 | 233 | ||
234 | kdDebug(5700) << "-- lock name: " << lockName << endl; | 234 | kdDebug(5700) << "-- lock name: " << lockName << endl; |
235 | 235 | ||
236 | if (QFile::exists( lockName )) | 236 | if (QFile::exists( lockName )) |
237 | { | 237 | { |
238 | qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName()); | 238 | qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); |
239 | return false; | 239 | return false; |
240 | } | 240 | } |
241 | 241 | ||
242 | QString lockUniqueName; | 242 | QString lockUniqueName; |
243 | lockUniqueName = fn + KApplication::randomString( 8 ); | 243 | lockUniqueName = fn + KApplication::randomString( 8 ); |
244 | 244 | ||
245 | url = lockUniqueName; | 245 | url = lockUniqueName; |
246 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); | 246 | //US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); |
247 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); | 247 | mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); |
248 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; | 248 | kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; |
249 | 249 | ||
250 | // Create unique file | 250 | // Create unique file |
251 | QFile file( mLockUniqueName ); | 251 | QFile file( mLockUniqueName ); |
252 | file.open( IO_WriteOnly ); | 252 | file.open( IO_WriteOnly ); |
253 | file.close(); | 253 | file.close(); |
254 | 254 | ||
255 | // Create lock file | 255 | // Create lock file |
256 | int result = 0; | 256 | int result = 0; |
257 | #ifndef _WIN32_ | 257 | #ifndef _WIN32_ |
258 | result = ::link( QFile::encodeName( mLockUniqueName ), | 258 | result = ::link( QFile::encodeName( mLockUniqueName ), |
259 | QFile::encodeName( lockName ) ); | 259 | QFile::encodeName( lockName ) ); |
260 | #endif | 260 | #endif |
261 | if ( result == 0 ) { | 261 | if ( result == 0 ) { |
262 | addressBook()->emitAddressBookLocked(); | 262 | addressBook()->emitAddressBookLocked(); |
263 | return true; | 263 | return true; |
264 | } | 264 | } |
265 | 265 | ||
266 | // TODO: check stat | 266 | // TODO: check stat |
267 | 267 | ||
268 | return false; | 268 | return false; |
269 | } | 269 | } |
270 | 270 | ||
271 | void ResourceQtopia::unlock( const QString &fileName ) | 271 | void ResourceQtopia::unlock( const QString &fileName ) |
272 | { | 272 | { |
273 | qDebug("ResourceQtopia::unlock() %s", fileName.latin1()); | 273 | qDebug("ResourceQtopia::unlock() %s", fileName.latin1()); |
274 | 274 | ||
275 | QString fn = fileName; | 275 | QString fn = fileName; |
276 | KURL url(fn); | 276 | KURL url(fn); |
277 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); | 277 | QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); |
278 | 278 | ||
279 | QFile::remove( lockName ); | 279 | QFile::remove( lockName ); |
280 | QFile::remove( mLockUniqueName ); | 280 | QFile::remove( mLockUniqueName ); |
281 | addressBook()->emitAddressBookUnlocked(); | 281 | addressBook()->emitAddressBookUnlocked(); |
282 | } | 282 | } |
283 | 283 | ||
284 | void ResourceQtopia::setFileName( const QString &newFileName ) | 284 | void ResourceQtopia::setFileName( const QString &newFileName ) |
285 | { | 285 | { |
286 | mDirWatch.stopScan(); | 286 | mDirWatch.stopScan(); |
287 | mDirWatch.removeFile( fileName() ); | 287 | mDirWatch.removeFile( fileName() ); |
288 | 288 | ||
289 | Resource::setFileName( newFileName ); | 289 | Resource::setFileName( newFileName ); |
290 | 290 | ||
291 | mDirWatch.addFile( fileName() ); | 291 | mDirWatch.addFile( fileName() ); |
292 | mDirWatch.startScan(); | 292 | mDirWatch.startScan(); |
293 | } | 293 | } |
294 | 294 | ||
295 | 295 | ||
296 | void ResourceQtopia::fileChanged() | 296 | void ResourceQtopia::fileChanged() |
297 | { | 297 | { |
298 | // There is a small theoretical chance that KDirWatch calls us before | 298 | // There is a small theoretical chance that KDirWatch calls us before |
299 | // we are fully constructed | 299 | // we are fully constructed |
300 | if (!addressBook()) | 300 | if (!addressBook()) |
301 | return; | 301 | return; |
302 | 302 | ||
303 | QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); | 303 | QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); |
304 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { | 304 | if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { |
305 | load(); | 305 | load(); |
306 | addressBook()->emitAddressBookChanged(); | 306 | addressBook()->emitAddressBookChanged(); |
307 | } | 307 | } |
308 | } | 308 | } |
309 | 309 | ||
310 | void ResourceQtopia::removeAddressee( const Addressee &addr ) | 310 | void ResourceQtopia::removeAddressee( const Addressee &addr ) |
311 | { | 311 | { |
312 | } | 312 | } |
313 | 313 | ||
314 | void ResourceQtopia::cleanUp() | 314 | void ResourceQtopia::cleanUp() |
315 | { | 315 | { |
316 | unlock( fileName() ); | 316 | unlock( fileName() ); |
317 | } | 317 | } |
318 | 318 | ||
319 | //US #include "resourceqtopia.moc" | 319 | //US #include "resourceqtopia.moc" |