-rw-r--r-- | kabc/plugins/sharpdtm/resourcesharpdtm.cpp | 242 |
1 files changed, 129 insertions, 113 deletions
diff --git a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp index 48b7d91..7069f5d 100644 --- a/kabc/plugins/sharpdtm/resourcesharpdtm.cpp +++ b/kabc/plugins/sharpdtm/resourcesharpdtm.cpp | |||
@@ -78,13 +78,23 @@ ResourceSharpDTM::ResourceSharpDTM( const QString &fileName ) | |||
78 | mAccess = 0; | 78 | mAccess = 0; |
79 | init( fileName ); | 79 | init( fileName ); |
80 | } | 80 | } |
81 | 81 | ||
82 | void ResourceSharpDTM::init( const QString &fileName ) | 82 | void ResourceSharpDTM::init( const QString &fileName ) |
83 | { | 83 | { |
84 | setFileName( fileName ); | 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 ); | ||
85 | } | 95 | } |
86 | 96 | ||
87 | ResourceSharpDTM::~ResourceSharpDTM() | 97 | ResourceSharpDTM::~ResourceSharpDTM() |
88 | { | 98 | { |
89 | if (mConverter != 0) | 99 | if (mConverter != 0) |
90 | delete mConverter; | 100 | delete mConverter; |
@@ -106,47 +116,36 @@ Ticket *ResourceSharpDTM::requestSaveTicket() | |||
106 | if ( !addressBook() ) return 0; | 116 | if ( !addressBook() ) return 0; |
107 | return createTicket( this ); | 117 | return createTicket( this ); |
108 | } | 118 | } |
109 | 119 | ||
110 | 120 | ||
111 | bool ResourceSharpDTM::doOpen() | 121 | bool ResourceSharpDTM::doOpen() |
112 | { | 122 | { |
113 | qDebug("%xResourceSharpDTM::doOpen: %s", this,fileName().latin1()); | 123 | if (!mConverter) |
114 | |||
115 | if ( ! mAccess ) { | ||
116 | // the last parameter in the SlZDataBase constructor means "readonly" | ||
117 | mAccess = new SlZDataBase(fileName(), | ||
118 | SlZDataBase::addressbookItems(), | ||
119 | NULL, false); | ||
120 | } | ||
121 | if ( !mAccess ) { | ||
122 | qDebug("Unable to load file() %s", fileName().latin1()); | ||
123 | return false; | 124 | return false; |
124 | } | ||
125 | if (mConverter == 0) { | ||
126 | mConverter = new SharpDTMConverter(); | ||
127 | bool res = mConverter->init(); | ||
128 | if ( !res ) | ||
129 | { | ||
130 | QString msg("Unable to initialize sharp converter. Most likely a problem with the category file"); | ||
131 | qDebug(msg); | ||
132 | delete mAccess; | ||
133 | mAccess = 0; | ||
134 | return false; | ||
135 | } | ||
136 | } | ||
137 | return true; | 125 | return true; |
138 | } | 126 | } |
139 | 127 | ||
140 | void ResourceSharpDTM::doClose() | 128 | void ResourceSharpDTM::doClose() |
141 | { | 129 | { |
142 | return; | 130 | return; |
143 | } | 131 | } |
144 | 132 | ||
145 | bool ResourceSharpDTM::load() | 133 | bool ResourceSharpDTM::load() |
146 | { | 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, false); | ||
142 | } | ||
143 | if(mAccess == 0) | ||
144 | return false; | ||
145 | |||
147 | qDebug("%xResourceSharpDTM::load: %s",this, fileName().latin1()); | 146 | qDebug("%xResourceSharpDTM::load: %s",this, fileName().latin1()); |
148 | bool res = false; | 147 | bool res = false; |
149 | CardId id; | 148 | CardId id; |
150 | for (bool res=mAccess->first(); res == true; res=mAccess->next()) | 149 | for (bool res=mAccess->first(); res == true; res=mAccess->next()) |
151 | { | 150 | { |
152 | id = mAccess->cardId(); | 151 | id = mAccess->cardId(); |
@@ -155,109 +154,126 @@ bool ResourceSharpDTM::load() | |||
155 | if ( !addressee.isEmpty() && res ) | 154 | if ( !addressee.isEmpty() && res ) |
156 | { | 155 | { |
157 | addressee.setResource( this ); | 156 | addressee.setResource( this ); |
158 | addressBook()->insertAddressee( addressee ); | 157 | addressBook()->insertAddressee( addressee ); |
159 | } | 158 | } |
160 | } | 159 | } |
160 | if(mAccess != 0) | ||
161 | delete mAccess; | ||
162 | mAccess = 0; | ||
163 | |||
161 | return true; | 164 | return true; |
162 | } | 165 | } |
163 | 166 | ||
164 | bool ResourceSharpDTM::save( Ticket *ticket ) | 167 | bool ResourceSharpDTM::save( Ticket *ticket ) |
165 | { | 168 | { |
166 | qDebug("ResourceSharpDTM::save: %s", fileName().latin1()); | 169 | if (!mConverter) |
167 | KABC::AddressBook::Iterator it; | 170 | return false; |
168 | bool res; | 171 | QString fileN = SlZDataBase::addressbookFileName(); |
169 | KABC::Addressee::List changedAddressees; | 172 | if ( ! mAccess ) { |
170 | typedef QMap<int,QString> AddresseeMap; | 173 | mAccess = new SlZDataBase(fileN, |
171 | AddresseeMap map; | 174 | SlZDataBase::addressbookItems(), |
172 | CardId id ; | 175 | 0, false); |
173 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { | 176 | } |
174 | 177 | if(mAccess == 0) | |
175 | if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { | 178 | return false; |
176 | QString uid = (*it).originalExternalUID(); | 179 | qDebug("ResourceSharpDTM::save: %s", fileName().latin1()); |
177 | bool res; | 180 | KABC::AddressBook::Iterator it; |
178 | if ( uid.isEmpty() ) | 181 | bool res; |
179 | id = 0; | 182 | KABC::Addressee::List changedAddressees; |
180 | else | 183 | typedef QMap<int,QString> AddresseeMap; |
181 | id = uid.toUInt(); | 184 | AddresseeMap map; |
182 | KABC::Addressee addressee = (*it); | 185 | CardId id ; |
183 | if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_ADDED_EXTERNAL ) { | 186 | for ( it = addressBook()->begin(); it != addressBook()->end(); ++it ) { |
184 | res = mAccess->startEditCard(id); | 187 | |
185 | if (res == true) | 188 | if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { |
186 | { | 189 | QString uid = (*it).originalExternalUID(); |
187 | res = mConverter->addresseeToSharp( (*it), mAccess, id ); | 190 | bool res; |
188 | if (res == true) | 191 | if ( uid.isEmpty() ) |
189 | { | 192 | id = 0; |
190 | res = mAccess->finishEditCard(&id);; | 193 | else |
191 | map.insert(id,(*it).uid()); | 194 | id = uid.toUInt(); |
192 | if (res == false) | 195 | KABC::Addressee addressee = (*it); |
193 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); | 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 | map.insert(id,(*it).uid()); | ||
205 | if (res == false) | ||
206 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); | ||
194 | 207 | ||
195 | } | 208 | } |
196 | else | 209 | else |
197 | { | 210 | { |
198 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); | 211 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); |
199 | mAccess->cancelEditCard(); | 212 | mAccess->cancelEditCard(); |
200 | } | 213 | } |
201 | } | 214 | } |
202 | 215 | ||
203 | } else if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { | 216 | } else if ( (*it).tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { |
204 | res = mAccess->deleteCard(&id); | 217 | res = mAccess->deleteCard(&id); |
205 | if ( !res ) | 218 | if ( !res ) |
206 | qDebug("delete error "); | 219 | qDebug("delete error "); |
207 | 220 | ||
208 | 221 | ||
209 | } else if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { | 222 | } else if ( (*it).tempSyncStat() != SYNC_TEMPSTATE_NEW_EXTERNAL ) { |
210 | //changed | 223 | //changed |
211 | res = mAccess->startEditCard(id); | 224 | res = mAccess->startEditCard(id); |
212 | if (res == true) | 225 | if (res == true) |
213 | { | 226 | { |
214 | res = mConverter->addresseeToSharp( (*it), mAccess, id ); | 227 | res = mConverter->addresseeToSharp( (*it), mAccess, id ); |
215 | if (res == true) | 228 | if (res == true) |
216 | { | 229 | { |
217 | res = mAccess->finishEditCard(&id); | 230 | res = mAccess->finishEditCard(&id); |
218 | map.insert(id,(*it).uid()); | 231 | map.insert(id,(*it).uid()); |
219 | if (res == false) | 232 | if (res == false) |
220 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); | 233 | qDebug("Unable to append Contact: %s", addressee.formattedName().latin1()); |
221 | } | 234 | } |
222 | else | 235 | else |
223 | { | 236 | { |
224 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); | 237 | qDebug("Unable to convert Addressee: %s", addressee.formattedName().latin1()); |
225 | mAccess->cancelEditCard(); | 238 | mAccess->cancelEditCard(); |
226 | } | 239 | } |
227 | } | 240 | } |
228 | } | 241 | } |
229 | } | 242 | } |
230 | 243 | ||
231 | } | 244 | } |
232 | AddresseeMap::Iterator itam; | 245 | AddresseeMap::Iterator itam; |
233 | for ( res=mAccess->first(); res == true; res=mAccess->next()) | 246 | for ( res=mAccess->first(); res == true; res=mAccess->next()) |
234 | { | 247 | { |
235 | id = mAccess->cardId(); | 248 | id = mAccess->cardId(); |
236 | int idint = id; | 249 | int idint = id; |
237 | itam = map.find( idint ); | 250 | itam = map.find( idint ); |
238 | if ( itam != map.end() ) { | 251 | if ( itam != map.end() ) { |
239 | KABC::Addressee addressee; | 252 | KABC::Addressee addressee; |
240 | res = mConverter->sharpToAddressee( id, mAccess, addressee ); | 253 | res = mConverter->sharpToAddressee( id, mAccess, addressee ); |
241 | 254 | ||
242 | if ( !addressee.isEmpty() && res ) | 255 | if ( !addressee.isEmpty() && res ) |
243 | { | 256 | { |
244 | addressee.setResource( this ); | 257 | addressee.setResource( this ); |
245 | addressee.setUid( itam.data() ); | 258 | addressee.setUid( itam.data() ); |
246 | addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); | 259 | addressee.setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); |
247 | addressBook()->insertAddressee( addressee , false ); | 260 | addressBook()->insertAddressee( addressee , false ); |
248 | } | 261 | } |
249 | } | 262 | } |
250 | } | 263 | } |
251 | delete ticket; | 264 | delete ticket; |
252 | 265 | if(mAccess != 0) | |
253 | return true; | 266 | delete mAccess; |
267 | mAccess = 0; | ||
268 | return true; | ||
254 | } | 269 | } |
255 | 270 | ||
256 | bool ResourceSharpDTM::lock( const QString &lockfileName ) | 271 | bool ResourceSharpDTM::lock( const QString &lockfileName ) |
257 | { | 272 | { |
273 | return true; | ||
258 | } | 274 | } |
259 | 275 | ||
260 | void ResourceSharpDTM::unlock( const QString &fileName ) | 276 | void ResourceSharpDTM::unlock( const QString &fileName ) |
261 | { | 277 | { |
262 | 278 | ||
263 | } | 279 | } |
@@ -275,11 +291,11 @@ void ResourceSharpDTM::fileChanged() | |||
275 | void ResourceSharpDTM::removeAddressee( const Addressee &addr ) | 291 | void ResourceSharpDTM::removeAddressee( const Addressee &addr ) |
276 | { | 292 | { |
277 | } | 293 | } |
278 | 294 | ||
279 | void ResourceSharpDTM::cleanUp() | 295 | void ResourceSharpDTM::cleanUp() |
280 | { | 296 | { |
281 | unlock( fileName() ); | 297 | |
282 | } | 298 | } |
283 | 299 | ||
284 | 300 | ||
285 | 301 | ||