summaryrefslogtreecommitdiffabout
path: root/kabc
Unidiff
Diffstat (limited to 'kabc') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/qtopia/resourceqtopia.cpp5
1 files changed, 3 insertions, 2 deletions
diff --git a/kabc/plugins/qtopia/resourceqtopia.cpp b/kabc/plugins/qtopia/resourceqtopia.cpp
index 99e8618..d6df981 100644
--- a/kabc/plugins/qtopia/resourceqtopia.cpp
+++ b/kabc/plugins/qtopia/resourceqtopia.cpp
@@ -1,335 +1,336 @@
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/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (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#ifndef _WIN32_
30 30#include <unistd.h>
31#endif
31#include <qdir.h> 32#include <qdir.h>
32#include <qfile.h> 33#include <qfile.h>
33#include <qtextstream.h> 34#include <qtextstream.h>
34#include <qfileinfo.h> 35#include <qfileinfo.h>
35#include <qregexp.h> 36#include <qregexp.h>
36//US #include <qtimer.h> 37//US #include <qtimer.h>
37 38
38#include <kapplication.h> 39#include <kapplication.h>
39#include <kconfig.h> 40#include <kconfig.h>
40#include <kdebug.h> 41#include <kdebug.h>
41#include <klocale.h> 42#include <klocale.h>
42//US #include <ksavefile.h> 43//US #include <ksavefile.h>
43#include <kstandarddirs.h> 44#include <kstandarddirs.h>
44#include <kmessagebox.h> 45#include <kmessagebox.h>
45 46
46 47
47#include "resourceqtopiaconfig.h" 48#include "resourceqtopiaconfig.h"
48#include "stdaddressbook.h" 49#include "stdaddressbook.h"
49 50
50#include "qtopiaconverter.h" 51#include "qtopiaconverter.h"
51 52
52#include "resourceqtopia.h" 53#include "resourceqtopia.h"
53 54
54using namespace KABC; 55using namespace KABC;
55 56
56extern "C" 57extern "C"
57#ifdef _WIN32_ 58#ifdef _WIN32_
58__declspec(dllexport) 59__declspec(dllexport)
59#else 60#else
60{ 61{
61#endif 62#endif
62 void *init_microkabc_qtopia() 63 void *init_microkabc_qtopia()
63 { 64 {
64 return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig>(); 65 return new KRES::PluginFactory<ResourceQtopia,ResourceQtopiaConfig>();
65 } 66 }
66#ifndef _WIN32_ 67#ifndef _WIN32_
67} 68}
68#endif 69#endif
69 70
70ResourceQtopia::ResourceQtopia( const KConfig *config ) 71ResourceQtopia::ResourceQtopia( const KConfig *config )
71 : Resource( config ), mConverter (0) 72 : Resource( config ), mConverter (0)
72{ 73{
73 // we can not choose the filename. Therefore use the default to display 74 // we can not choose the filename. Therefore use the default to display
74 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml"; 75 QString fileName = QDir::homeDirPath() + "/Applications/addressbook/addressbook.xml";
75 init( fileName ); 76 init( fileName );
76} 77}
77 78
78ResourceQtopia::ResourceQtopia( const QString &fileName ) 79ResourceQtopia::ResourceQtopia( const QString &fileName )
79 : Resource( 0 ) 80 : Resource( 0 )
80{ 81{
81 init( fileName ); 82 init( fileName );
82} 83}
83 84
84void ResourceQtopia::init( const QString &fileName ) 85void ResourceQtopia::init( const QString &fileName )
85{ 86{
86#ifdef _USE_DIRWATCH_ 87#ifdef _USE_DIRWATCH_
87 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) ); 88 connect( &mDirWatch, SIGNAL( dirty(const QString&) ), SLOT( fileChanged() ) );
88 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) ); 89 connect( &mDirWatch, SIGNAL( created(const QString&) ), SLOT( fileChanged() ) );
89 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) ); 90 connect( &mDirWatch, SIGNAL( deleted(const QString&) ), SLOT( fileChanged() ) );
90#endif 91#endif
91 setFileName( fileName ); 92 setFileName( fileName );
92} 93}
93 94
94ResourceQtopia::~ResourceQtopia() 95ResourceQtopia::~ResourceQtopia()
95{ 96{
96 if (mConverter != 0) 97 if (mConverter != 0)
97 delete mConverter; 98 delete mConverter;
98 99
99} 100}
100 101
101void ResourceQtopia::writeConfig( KConfig *config ) 102void ResourceQtopia::writeConfig( KConfig *config )
102{ 103{
103 Resource::writeConfig( config ); 104 Resource::writeConfig( config );
104} 105}
105 106
106Ticket *ResourceQtopia::requestSaveTicket() 107Ticket *ResourceQtopia::requestSaveTicket()
107{ 108{
108 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl; 109 kdDebug(5700) << "ResourceQtopia::requestSaveTicket()" << endl;
109 110
110 qDebug("ResourceQtopia::requestSaveTicket: %s", fileName().latin1()); 111 qDebug("ResourceQtopia::requestSaveTicket: %s", fileName().latin1());
111 112
112 if ( !addressBook() ) return 0; 113 if ( !addressBook() ) return 0;
113 114
114 if ( !lock( fileName() ) ) { 115 if ( !lock( fileName() ) ) {
115 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '" 116 kdDebug(5700) << "ResourceQtopia::requestSaveTicket(): Unable to lock file '"
116 << fileName() << "'" << endl; 117 << fileName() << "'" << endl;
117 return 0; 118 return 0;
118 } 119 }
119 return createTicket( this ); 120 return createTicket( this );
120} 121}
121 122
122 123
123bool ResourceQtopia::doOpen() 124bool ResourceQtopia::doOpen()
124{ 125{
125 qDebug("ResourceQtopia::doOpen(): %s", fileName().latin1()); 126 qDebug("ResourceQtopia::doOpen(): %s", fileName().latin1());
126 127
127 128
128 if (mConverter == 0) 129 if (mConverter == 0)
129 { 130 {
130 mConverter = new QtopiaConverter(); 131 mConverter = new QtopiaConverter();
131 bool res = mConverter->init(); 132 bool res = mConverter->init();
132 if ( !res ) 133 if ( !res )
133 { 134 {
134 QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file"); 135 QString msg("Unable to initialize qtopia converter. Most likely a problem with the category file");
135 qDebug(msg); 136 qDebug(msg);
136 return false; 137 return false;
137 } 138 }
138 } 139 }
139 140
140 return true; 141 return true;
141} 142}
142 143
143void ResourceQtopia::doClose() 144void ResourceQtopia::doClose()
144{ 145{
145 qDebug("ResourceQtopia::doClose: %s", fileName().latin1()); 146 qDebug("ResourceQtopia::doClose: %s", fileName().latin1());
146 147
147 148
148 // it seems so, that deletion of access deletes backend as well 149 // it seems so, that deletion of access deletes backend as well
149 //delete backend; 150 //delete backend;
150 151
151 return; 152 return;
152} 153}
153 154
154bool ResourceQtopia::load() 155bool ResourceQtopia::load()
155{ 156{
156 157
157 QFile file( fileName() ); 158 QFile file( fileName() );
158 if ( !file.open(IO_ReadOnly ) ) { 159 if ( !file.open(IO_ReadOnly ) ) {
159 return false; 160 return false;
160 } 161 }
161 162
162 QDomDocument doc("mydocument" ); 163 QDomDocument doc("mydocument" );
163 if ( !doc.setContent( &file ) ) { 164 if ( !doc.setContent( &file ) ) {
164 file.close(); 165 file.close();
165 return false; 166 return false;
166 } 167 }
167 bool res; 168 bool res;
168 QDomElement docElem = doc.documentElement( ); 169 QDomElement docElem = doc.documentElement( );
169 QDomNode n = docElem.firstChild(); 170 QDomNode n = docElem.firstChild();
170 while ( !n.isNull() ) { 171 while ( !n.isNull() ) {
171 QDomElement e = n.toElement(); 172 QDomElement e = n.toElement();
172 if ( !e.isNull() ) { 173 if ( !e.isNull() ) {
173 if ( e.tagName() == QString::fromLatin1( "Contacts" ) ) { 174 if ( e.tagName() == QString::fromLatin1( "Contacts" ) ) {
174 QDomNode no = e.firstChild(); 175 QDomNode no = e.firstChild();
175 while ( !no.isNull() ) { 176 while ( !no.isNull() ) {
176 QDomElement el = no.toElement(); 177 QDomElement el = no.toElement();
177 if ( !el.isNull() ) { 178 if ( !el.isNull() ) {
178 KABC::Addressee addressee; 179 KABC::Addressee addressee;
179 res = mConverter->qtopiaToAddressee( el, addressee ); 180 res = mConverter->qtopiaToAddressee( el, addressee );
180 if ( !addressee.isEmpty() && res ) 181 if ( !addressee.isEmpty() && res )
181 { 182 {
182 addressee.setResource( this ); 183 addressee.setResource( this );
183 addressBook()->insertAddressee( addressee ); 184 addressBook()->insertAddressee( addressee );
184 } 185 }
185 } 186 }
186 187
187 no = no.nextSibling(); 188 no = no.nextSibling();
188 } 189 }
189 } 190 }
190 } 191 }
191 192
192 n = n.nextSibling(); 193 n = n.nextSibling();
193 } 194 }
194 return true; 195 return true;
195} 196}
196 197
197bool ResourceQtopia::save( Ticket *ticket ) 198bool ResourceQtopia::save( Ticket *ticket )
198{ 199{
199#ifdef _USE_DIRWATCH_ 200#ifdef _USE_DIRWATCH_
200 mDirWatch.stopScan(); 201 mDirWatch.stopScan();
201#endif 202#endif
202 KABC::AddressBook::Iterator it; 203 KABC::AddressBook::Iterator it;
203 bool res; 204 bool res;
204 QFile file( fileName() ); 205 QFile file( fileName() );
205 if (!file.open( IO_WriteOnly ) ) { 206 if (!file.open( IO_WriteOnly ) ) {
206 return false; 207 return false;
207 } 208 }
208 QTextStream ts( &file ); 209 QTextStream ts( &file );
209 QTextStream *stream = &ts; 210 QTextStream *stream = &ts;
210 stream->setEncoding( QTextStream::UnicodeUTF8 ); 211 stream->setEncoding( QTextStream::UnicodeUTF8 );
211 *stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>" << endl; 212 *stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE Addressbook ><AddressBook>" << endl;
212 *stream << " <Groups>" << endl; 213 *stream << " <Groups>" << endl;
213 *stream << " </Groups>" << endl; 214 *stream << " </Groups>" << endl;
214 *stream << " <Contacts> " << endl; 215 *stream << " <Contacts> " << endl;
215 // for all entries 216 // for all entries
216 KABC::Addressee ab; 217 KABC::Addressee ab;
217 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { 218 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
218 KABC::Addressee addressee = (*it); 219 KABC::Addressee addressee = (*it);
219 res = mConverter->addresseeToQtopia( addressee, stream ); 220 res = mConverter->addresseeToQtopia( addressee, stream );
220 if (!res == true) 221 if (!res == true)
221 { 222 {
222 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1()); 223 qDebug("Unable to convert Addressee %s", addressee.formattedName().latin1());
223 } 224 }
224 } 225 }
225 *stream << "</Contacts>" << endl; 226 *stream << "</Contacts>" << endl;
226 *stream << "</AddressBook>" << endl; 227 *stream << "</AddressBook>" << endl;
227 file.close(); 228 file.close();
228#ifdef _USE_DIRWATCH_ 229#ifdef _USE_DIRWATCH_
229 mDirWatch.startScan(); 230 mDirWatch.startScan();
230#endif 231#endif
231 delete ticket; 232 delete ticket;
232 unlock( fileName() ); 233 unlock( fileName() );
233 return true; 234 return true;
234} 235}
235 236
236bool ResourceQtopia::lock( const QString &lockfileName ) 237bool ResourceQtopia::lock( const QString &lockfileName )
237{ 238{
238 //disabled 239 //disabled
239 return true; 240 return true;
240 qDebug("ResourceQtopia::lock: %s", fileName().latin1()); 241 qDebug("ResourceQtopia::lock: %s", fileName().latin1());
241 QString fn = lockfileName; 242 QString fn = lockfileName;
242 243
243 KURL url(fn); 244 KURL url(fn);
244 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 245 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
245 246
246 if (QFile::exists( lockName )) 247 if (QFile::exists( lockName ))
247 { 248 {
248 qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1()); 249 qDebug("ResourceOpie::lock: lockfile %s already exists. (Delete it before continuing if nobody else is accessing the resourcefile %s)", lockName.latin1(), fileName().latin1());
249 return false; 250 return false;
250 } 251 }
251 252
252 QString lockUniqueName; 253 QString lockUniqueName;
253 lockUniqueName = fn + KApplication::randomString( 8 ); 254 lockUniqueName = fn + KApplication::randomString( 8 );
254 255
255 url = lockUniqueName; 256 url = lockUniqueName;
256//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName ); 257//US mLockUniqueName = locateLocal( "data", "kabc/lock/" + lockUniqueName );
257 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() ); 258 mLockUniqueName = locateLocal( "data", "kabc/lock/" + url.fileName() );
258 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl; 259 kdDebug(5700) << "-- lock unique name: " << mLockUniqueName << endl;
259 260
260 // Create unique file 261 // Create unique file
261 QFile file( mLockUniqueName ); 262 QFile file( mLockUniqueName );
262 file.open( IO_WriteOnly ); 263 file.open( IO_WriteOnly );
263 file.close(); 264 file.close();
264 265
265 // Create lock file 266 // Create lock file
266 int result = 0; 267 int result = 0;
267#ifndef _WIN32_ 268#ifndef _WIN32_
268 result = ::link( QFile::encodeName( mLockUniqueName ), 269 result = ::link( QFile::encodeName( mLockUniqueName ),
269 QFile::encodeName( lockName ) ); 270 QFile::encodeName( lockName ) );
270#endif 271#endif
271 if ( result == 0 ) { 272 if ( result == 0 ) {
272 addressBook()->emitAddressBookLocked(); 273 addressBook()->emitAddressBookLocked();
273 return true; 274 return true;
274 } 275 }
275 276
276 // TODO: check stat 277 // TODO: check stat
277 278
278 return false; 279 return false;
279} 280}
280 281
281void ResourceQtopia::unlock( const QString &fileName ) 282void ResourceQtopia::unlock( const QString &fileName )
282{ 283{
283 //disabled 284 //disabled
284 return; 285 return;
285 qDebug("ResourceQtopia::unlock() %s", fileName.latin1()); 286 qDebug("ResourceQtopia::unlock() %s", fileName.latin1());
286 287
287 QString fn = fileName; 288 QString fn = fileName;
288 KURL url(fn); 289 KURL url(fn);
289 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" ); 290 QString lockName = locateLocal( "data", "kabc/lock/" + url.fileName() + ".lock" );
290 291
291 QFile::remove( lockName ); 292 QFile::remove( lockName );
292 QFile::remove( mLockUniqueName ); 293 QFile::remove( mLockUniqueName );
293 addressBook()->emitAddressBookUnlocked(); 294 addressBook()->emitAddressBookUnlocked();
294} 295}
295 296
296void ResourceQtopia::setFileName( const QString &newFileName ) 297void ResourceQtopia::setFileName( const QString &newFileName )
297{ 298{
298#ifdef _USE_DIRWATCH_ 299#ifdef _USE_DIRWATCH_
299 mDirWatch.stopScan(); 300 mDirWatch.stopScan();
300#endif 301#endif
301 mDirWatch.removeFile( fileName() ); 302 mDirWatch.removeFile( fileName() );
302 303
303 Resource::setFileName( newFileName ); 304 Resource::setFileName( newFileName );
304 305
305 mDirWatch.addFile( fileName() ); 306 mDirWatch.addFile( fileName() );
306#ifdef _USE_DIRWATCH_ 307#ifdef _USE_DIRWATCH_
307 mDirWatch.startScan(); 308 mDirWatch.startScan();
308#endif 309#endif
309} 310}
310 311
311 312
312void ResourceQtopia::fileChanged() 313void ResourceQtopia::fileChanged()
313{ 314{
314 // There is a small theoretical chance that KDirWatch calls us before 315 // There is a small theoretical chance that KDirWatch calls us before
315 // we are fully constructed 316 // we are fully constructed
316 if (!addressBook()) 317 if (!addressBook())
317 return; 318 return;
318 319
319 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) ); 320 QString text( i18n( "Qtopia resource '%1'<br> has been changed by third party.<br>Do you want to reload?").arg( fileName() ) );
320 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) { 321 if ( readOnly() || KMessageBox::questionYesNo( 0, text ) == KMessageBox::Yes ) {
321 load(); 322 load();
322 addressBook()->emitAddressBookChanged(); 323 addressBook()->emitAddressBookChanged();
323 } 324 }
324} 325}
325 326
326void ResourceQtopia::removeAddressee( const Addressee &addr ) 327void ResourceQtopia::removeAddressee( const Addressee &addr )
327{ 328{
328} 329}
329 330
330void ResourceQtopia::cleanUp() 331void ResourceQtopia::cleanUp()
331{ 332{
332 unlock( fileName() ); 333 unlock( fileName() );
333} 334}
334 335
335//US #include "resourceqtopia.moc" 336//US #include "resourceqtopia.moc"