summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/olaccess/resourceolaccess.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/olaccess/resourceolaccess.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/olaccess/resourceolaccess.cpp303
1 files changed, 303 insertions, 0 deletions
diff --git a/kabc/plugins/olaccess/resourceolaccess.cpp b/kabc/plugins/olaccess/resourceolaccess.cpp
new file mode 100644
index 0000000..e8f8bdd
--- a/dev/null
+++ b/kabc/plugins/olaccess/resourceolaccess.cpp
@@ -0,0 +1,303 @@
1/*
2 This file is part of libkabc.
3 Copyright (c) 2004 Ulf Schenk
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/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (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#include <libkdepim/ksyncprofile.h>
49
50#include "resourcesharpdtmconfig.h"
51#include "resourcesharpdtm.h"
52
53#include "stdaddressbook.h"
54
55#include "sharpdtmconverter.h"
56//#define ALLOW_LOCKING
57using namespace KABC;
58extern "C"
59{
60 void *init_microkabc_sharpdtm()
61 {
62 return new KRES::PluginFactory<ResourceSharpDTM,ResourceSharpDTMConfig>();
63 }
64}
65
66ResourceSharpDTM::ResourceSharpDTM( const KConfig *config )
67 : Resource( config ), mConverter (0)
68{
69 // we can not choose the filename. Therefore use the default to display
70 mAccess = 0;
71 QString fileName = SlZDataBase::addressbookFileName();
72 init( fileName );
73}
74
75ResourceSharpDTM::ResourceSharpDTM( const QString &fileName )
76 : Resource( 0 )
77{
78 mAccess = 0;
79 init( fileName );
80}
81
82void ResourceSharpDTM::init( const QString &fileName )
83{
84 if (mConverter == 0) {
85 mConverter = new SharpDTMConverter();
86 bool res = mConverter->init();
87 if ( !res )
88 {
89 QString msg("Unable to initialize sharp converter. Most likely a problem with the category file");
90 qDebug(msg);
91 return;
92 }
93 }
94 setFileName( fileName );
95}
96
97ResourceSharpDTM::~ResourceSharpDTM()
98{
99 if (mConverter != 0)
100 delete mConverter;
101
102 if(mAccess != 0)
103 delete mAccess;
104}
105
106void ResourceSharpDTM::writeConfig( KConfig *config )
107{
108 Resource::writeConfig( config );
109}
110
111Ticket *ResourceSharpDTM::requestSaveTicket()
112{
113
114 qDebug("ResourceSharpDTM::requestSaveTicket: %s", fileName().latin1());
115
116 if ( !addressBook() ) return 0;
117 return createTicket( this );
118}
119
120
121bool ResourceSharpDTM::doOpen()
122{
123 if (!mConverter)
124 return false;
125 return true;
126}
127
128void ResourceSharpDTM::doClose()
129{
130 return;
131}
132
133bool ResourceSharpDTM::load()
134{
135 if (!mConverter)
136 return false;
137 QString fileN = SlZDataBase::addressbookFileName();
138 if ( ! mAccess ) {
139 mAccess = new SlZDataBase(fileN,
140 SlZDataBase::addressbookItems(),
141 0, true);
142 }
143 if(mAccess == 0)
144 return false;
145
146 qDebug("ResourceSharpDTM:: %x load: %s",this, fileName().latin1());
147 bool res = false;
148 CardId id;
149 for (bool res=mAccess->first(); res == true; res=mAccess->next())
150 {
151 id = mAccess->cardId();
152 KABC::Addressee addressee;
153 res = mConverter->sharpToAddressee( id, mAccess, addressee );
154 if ( !addressee.isEmpty() && res )
155 {
156 addressee.setResource( this );
157 addressBook()->insertAddressee( addressee );
158 }
159 }
160 if(mAccess != 0)
161 delete mAccess;
162 mAccess = 0;
163
164 return true;
165}
166
167bool ResourceSharpDTM::save( Ticket *ticket )
168{
169 if (!mConverter)
170 return false;
171 QString fileN = SlZDataBase::addressbookFileName();
172 if ( ! mAccess ) {
173 mAccess = new SlZDataBase(fileN,
174 SlZDataBase::addressbookItems(),
175 0, false);
176 }
177 if(mAccess == 0)
178 return false;
179 qDebug("ResourceSharpDTM::save: %s", fileName().latin1());
180 KABC::AddressBook::Iterator it;
181 bool res;
182 KABC::Addressee::List changedAddressees;
183 typedef QMap<int,QString> AddresseeMap;
184 AddresseeMap map;
185 CardId id ;
186 for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) {
187
188 if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) {
189 QString uid = (*it).originalExternalUID();
190 bool res;
191 if ( uid.isEmpty() )
192 id = 0;
193 else
194 id = uid.toUInt();
195 KABC::Addressee addressee = (*it);
196 if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL ) {
197 res = mAccess->startEditCard(id);
198 if (res == true)
199 {
200 res = mConverter->addresseeToSharp( (*it), mAccess, id );
201 if (res == true)
202 {
203 res = mAccess->finishEditCard(&id);;
204 mConverter->setCategories( (*it), mAccess, id );
205 map.insert(id,(*it).uid());
206 if (res == false)
207 qDebug("Unable to append Contact: %s", addressee.formattedName().latin1());
208
209 }
210 else
211 {
212 qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1());
213 mAccess->cancelEditCard();
214 }
215 }
216
217 } else if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) {
218 res = mAccess->deleteCard(&id);
219 if ( !res )
220 qDebug("delete error ");
221
222
223 } else if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) {
224 //changed
225 res = mAccess->startEditCard(id);
226 if (res == true)
227 {
228 res = mConverter->addresseeToSharp( (*it), mAccess, id );
229 if (res == true)
230 {
231 res = mAccess->finishEditCard(&id);
232 mConverter->setCategories( (*it), mAccess, id );
233 map.insert(id,(*it).uid());
234 if (res == false)
235 qDebug("Unable to append Contact: %s", addressee.formattedName().latin1());
236 }
237 else
238 {
239 qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1());
240 mAccess->cancelEditCard();
241 }
242 }
243 }
244 }
245
246 }
247 AddresseeMap::Iterator itam;
248 for ( res=mAccess->first(); res == true; res=mAccess->next())
249 {
250 id = mAccess->cardId();
251 int idint = id;
252 itam = map.find( idint );
253 if ( itam != map.end() ) {
254 KABC::Addressee addressee;
255 res = mConverter->sharpToAddressee( id, mAccess, addressee );
256
257 if ( !addressee.isEmpty() && res )
258 {
259 addressee.setResource( this );
260 addressee.setUid( itam.data() );
261 addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
262 addressBook()->insertAddressee( addressee , false );
263 }
264 }
265 }
266 delete ticket;
267 if(mAccess != 0)
268 delete mAccess;
269 mAccess = 0;
270 return true;
271}
272
273bool ResourceSharpDTM::lock( const QString &lockfileName )
274{
275 return true;
276}
277
278void ResourceSharpDTM::unlock( const QString &fileName )
279{
280
281}
282
283void ResourceSharpDTM::setFileName( const QString &newFileName )
284{
285 Resource::setFileName( newFileName );
286}
287
288void ResourceSharpDTM::fileChanged()
289{
290
291}
292
293void ResourceSharpDTM::removeAddressee( const Addressee &addr )
294{
295}
296
297void ResourceSharpDTM::cleanUp()
298{
299
300}
301
302
303