author | ulf69 <ulf69> | 2004-10-01 00:04:23 (UTC) |
---|---|---|
committer | ulf69 <ulf69> | 2004-10-01 00:04:23 (UTC) |
commit | 3138ae138248637d901528ee5d6cfc894b0ed7c2 (patch) (unidiff) | |
tree | 86ec7eb64285edbdb063a30242b5d589d33ec948 /libkdepim/ksyncmanager.cpp | |
parent | 18133bd2cce41a83dab0f2feee01b7b3edd189dd (diff) | |
download | kdepimpi-3138ae138248637d901528ee5d6cfc894b0ed7c2.zip kdepimpi-3138ae138248637d901528ee5d6cfc894b0ed7c2.tar.gz kdepimpi-3138ae138248637d901528ee5d6cfc894b0ed7c2.tar.bz2 |
initial revision of a globalized syncmanager that can be used by all apps
-rw-r--r-- | libkdepim/ksyncmanager.cpp | 455 |
1 files changed, 455 insertions, 0 deletions
diff --git a/libkdepim/ksyncmanager.cpp b/libkdepim/ksyncmanager.cpp new file mode 100644 index 0000000..52e3bd9 --- a/dev/null +++ b/libkdepim/ksyncmanager.cpp | |||
@@ -0,0 +1,455 @@ | |||
1 | /* | ||
2 | This file is part of KDE-Pim/Pi. | ||
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 | // $Id$ | ||
22 | |||
23 | #include "ksyncmanager.h" | ||
24 | |||
25 | |||
26 | KSyncManager::KSyncManager(TargetApp ta, QPopupMenu* syncmenu) | ||
27 | : mSyncMenu(syncmenu), mTargetApp(ta) | ||
28 | { | ||
29 | |||
30 | } | ||
31 | |||
32 | KSyncManager::~KSyncManager() | ||
33 | { | ||
34 | |||
35 | } | ||
36 | |||
37 | |||
38 | void KABCore::fillSyncMenu() | ||
39 | { | ||
40 | if ( mSyncMenu->count() ) | ||
41 | msyncMenu->clear(); | ||
42 | |||
43 | mSyncMenu->insertItem( i18n("Configure..."), 0 ); | ||
44 | mSyncMenu->insertSeparator(); | ||
45 | mSyncMenu->insertItem( i18n("Multiple sync"), 1 ); | ||
46 | mSyncMenu->insertSeparator(); | ||
47 | |||
48 | KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); | ||
49 | config.setGroup("General"); | ||
50 | QStringList prof = config.readListEntry("SyncProfileNames"); | ||
51 | //US KABPrefs::instance()->mLocalMachineName = config.readEntry("LocalMachineName","undefined"); | ||
52 | if ( prof.count() < 3 ) { | ||
53 | prof.clear(); | ||
54 | prof << i18n("Sharp_DTM"); | ||
55 | prof << i18n("Local_file"); | ||
56 | prof << i18n("Last_file"); | ||
57 | KSyncProfile* temp = new KSyncProfile (); | ||
58 | temp->setName( prof[0] ); | ||
59 | temp->writeConfig(&config); | ||
60 | temp->setName( prof[1] ); | ||
61 | temp->writeConfig(&config); | ||
62 | temp->setName( prof[2] ); | ||
63 | temp->writeConfig(&config); | ||
64 | config.setGroup("General"); | ||
65 | config.writeEntry("SyncProfileNames",prof); | ||
66 | config.writeEntry("ExternSyncProfiles","Sharp_DTM"); | ||
67 | config.sync(); | ||
68 | delete temp; | ||
69 | } | ||
70 | /*US | ||
71 | KABPrefs::instance()->mExternSyncProfiles = config.readListEntry("ExternSyncProfiles"); | ||
72 | KABPrefs::instance()->mSyncProfileNames = prof; | ||
73 | */ | ||
74 | int i; | ||
75 | for ( i = 0; i < prof.count(); ++i ) { | ||
76 | |||
77 | mSyncMenu->insertItem( prof[i], 1000+i ); | ||
78 | if ( i == 2 ) | ||
79 | mSyncMenu->insertSeparator(); | ||
80 | } | ||
81 | QDir app_dir; | ||
82 | if ( !app_dir.exists(QDir::homeDirPath()+"/Applications/dtm" ) ) { | ||
83 | mSyncMenu->setItemEnabled( false , 1000 ); | ||
84 | } | ||
85 | //probaly useless | ||
86 | //mView->setupExternSyncProfiles(); | ||
87 | } | ||
88 | void KABCore::slotSyncMenu( int action ) | ||
89 | { | ||
90 | //qDebug("syncaction %d ", action); | ||
91 | if ( action == 0 ) { | ||
92 | |||
93 | // seems to be a Qt2 event handling bug | ||
94 | // syncmenu.clear causes a segfault at first time | ||
95 | // when we call it after the main event loop, it is ok | ||
96 | // same behaviour when calling OM/Pi via QCOP for the first time | ||
97 | QTimer::singleShot ( 1, this, SLOT ( confSync() ) ); | ||
98 | //confSync(); | ||
99 | |||
100 | return; | ||
101 | } | ||
102 | if ( action == 1 ) { | ||
103 | multiSync( true ); | ||
104 | return; | ||
105 | } | ||
106 | |||
107 | if (mBlockSaveFlag) | ||
108 | return; | ||
109 | mBlockSaveFlag = true; | ||
110 | mCurrentSyncProfile = action - 1000 ; | ||
111 | //US mCurrentSyncDevice = KABPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile] ; | ||
112 | //US mCurrentSyncName = KABPrefs::instance()->mLocalMachineName ; | ||
113 | KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); | ||
114 | KSyncProfile* temp = new KSyncProfile (); | ||
115 | //US temp->setName(KABPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile]); | ||
116 | temp->readConfig(&config); | ||
117 | /*US | ||
118 | KABPrefs::instance()->mAskForPreferences = temp->getAskForPreferences(); | ||
119 | KABPrefs::instance()->mSyncAlgoPrefs = temp->getSyncPrefs(); | ||
120 | KABPrefs::instance()->mWriteBackFile = temp->getWriteBackFile(); | ||
121 | KABPrefs::instance()->mWriteBackExistingOnly = temp->getWriteBackExisting(); | ||
122 | KABPrefs::instance()->mWriteBackInFuture = 0; | ||
123 | if ( temp->getWriteBackFuture() ) | ||
124 | KABPrefs::instance()->mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); | ||
125 | KABPrefs::instance()->mShowSyncSummary = temp->getShowSummaryAfterSync(); | ||
126 | */ | ||
127 | if ( action == 1000 ) { | ||
128 | syncSharp(); | ||
129 | |||
130 | } else if ( action == 1001 ) { | ||
131 | syncLocalFile(); | ||
132 | |||
133 | } else if ( action == 1002 ) { | ||
134 | quickSyncLocalFile(); | ||
135 | |||
136 | } else if ( action >= 1003 ) { | ||
137 | if ( temp->getIsLocalFileSync() ) { | ||
138 | /*US if ( syncWithFile( temp->getRemoteFileNameAB( ), false ) ) | ||
139 | KABPrefs::instance()->mLastSyncedLocalFile = temp->getRemoteFileNameAB(); | ||
140 | */ | ||
141 | } else { | ||
142 | if ( temp->getIsPhoneSync() ) { | ||
143 | /* | ||
144 | KABPrefs::instance()->mPhoneDevice = temp->getPhoneDevice( ) ; | ||
145 | KABPrefs::instance()->mPhoneConnection = temp->getPhoneConnection( ); | ||
146 | KABPrefs::instance()->mPhoneModel = temp->getPhoneModel( ); | ||
147 | */ | ||
148 | syncPhone(); | ||
149 | } else | ||
150 | syncRemote( temp ); | ||
151 | |||
152 | } | ||
153 | } | ||
154 | delete temp; | ||
155 | mBlockSaveFlag = false; | ||
156 | } | ||
157 | |||
158 | void KABCore::syncLocalFile() | ||
159 | { | ||
160 | |||
161 | QString fn =KABPrefs::instance()->mLastSyncedLocalFile; | ||
162 | |||
163 | fn =KFileDialog:: getOpenFileName( fn, i18n("Sync filename(*.ics/*.vcs)"), this ); | ||
164 | if ( fn == "" ) | ||
165 | return; | ||
166 | if ( syncWithFile( fn, false ) ) { | ||
167 | qDebug("syncLocalFile() successful "); | ||
168 | } | ||
169 | |||
170 | } | ||
171 | bool KABCore::syncWithFile( QString fn , bool quick ) | ||
172 | { | ||
173 | bool ret = false; | ||
174 | QFileInfo info; | ||
175 | info.setFile( fn ); | ||
176 | QString mess; | ||
177 | bool loadbup = true; | ||
178 | if ( !info. exists() ) { | ||
179 | mess = i18n( "Sync file \n...%1\ndoes not exist!\nNothing synced!\n").arg(fn.right( 30) ); | ||
180 | int result = QMessageBox::warning( this, i18n("Warning!"), | ||
181 | mess ); | ||
182 | return ret; | ||
183 | } | ||
184 | int result = 0; | ||
185 | if ( !quick ) { | ||
186 | mess = i18n("Sync with file \n...%1\nfrom:\n%2\n").arg(fn.right( 25)).arg(KGlobal::locale()->formatDateTime(info.lastModified (), true, false )); | ||
187 | result = QMessageBox::warning( this, i18n("Warning!"), | ||
188 | mess, | ||
189 | i18n("Sync"), i18n("Cancel"), 0, | ||
190 | 0, 1 ); | ||
191 | if ( result ) | ||
192 | return false; | ||
193 | } | ||
194 | if ( KABPrefs::instance()->mAskForPreferences ) | ||
195 | edit_sync_options(); | ||
196 | if ( result == 0 ) { | ||
197 | //qDebug("Now sycing ... "); | ||
198 | if ( ret = syncAB( fn, KABPrefs::instance()->mSyncAlgoPrefs ) ) | ||
199 | setCaption( i18n("Synchronization successful") ); | ||
200 | else | ||
201 | setCaption( i18n("Sync cancelled or failed. Nothing synced.") ); | ||
202 | if ( ! quick ) | ||
203 | KABPrefs::instance()->mLastSyncedLocalFile = fn; | ||
204 | setModified(); | ||
205 | } | ||
206 | return ret; | ||
207 | } | ||
208 | void KABCore::quickSyncLocalFile() | ||
209 | { | ||
210 | |||
211 | if ( syncWithFile( KABPrefs::instance()->mLastSyncedLocalFile, false ) ) { | ||
212 | qDebug("quick syncLocalFile() successful "); | ||
213 | |||
214 | } | ||
215 | } | ||
216 | void KABCore::multiSync( bool askforPrefs ) | ||
217 | { | ||
218 | if (mBlockSaveFlag) | ||
219 | return; | ||
220 | mBlockSaveFlag = true; | ||
221 | QString question = i18n("Do you really want\nto multiple sync\nwith all checked profiles?\nSyncing takes some\ntime - all profiles\nare synced twice!"); | ||
222 | if ( QMessageBox::information( this, i18n("KO/Pi Sync"), | ||
223 | question, | ||
224 | i18n("Yes"), i18n("No"), | ||
225 | 0, 0 ) != 0 ) { | ||
226 | mBlockSaveFlag = false; | ||
227 | setCaption(i18n("Aborted! Nothing synced!")); | ||
228 | return; | ||
229 | } | ||
230 | mCurrentSyncDevice = i18n("Multiple profiles") ; | ||
231 | KABPrefs::instance()->mSyncAlgoPrefs = KABPrefs::instance()->mRingSyncAlgoPrefs; | ||
232 | if ( askforPrefs ) { | ||
233 | edit_sync_options(); | ||
234 | KABPrefs::instance()->mRingSyncAlgoPrefs = KABPrefs::instance()->mSyncAlgoPrefs; | ||
235 | } | ||
236 | setCaption(i18n("Multiple sync started.") ); | ||
237 | qApp->processEvents(); | ||
238 | int num = ringSync() ; | ||
239 | if ( num > 1 ) | ||
240 | ringSync(); | ||
241 | mBlockSaveFlag = false; | ||
242 | if ( num ) | ||
243 | save(); | ||
244 | if ( num ) | ||
245 | setCaption(i18n("%1 profiles synced. Multiple sync completed!").arg(num) ); | ||
246 | else | ||
247 | setCaption(i18n("Nothing synced! No profiles defined for multisync!")); | ||
248 | return; | ||
249 | } | ||
250 | int KABCore::ringSync() | ||
251 | { | ||
252 | int syncedProfiles = 0; | ||
253 | int i; | ||
254 | QTime timer; | ||
255 | KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); | ||
256 | QStringList syncProfileNames = KABPrefs::instance()->mSyncProfileNames; | ||
257 | KSyncProfile* temp = new KSyncProfile (); | ||
258 | KABPrefs::instance()->mAskForPreferences = false; | ||
259 | for ( i = 0; i < syncProfileNames.count(); ++i ) { | ||
260 | mCurrentSyncProfile = i; | ||
261 | temp->setName(syncProfileNames[mCurrentSyncProfile]); | ||
262 | temp->readConfig(&config); | ||
263 | if ( temp->getIncludeInRingSyncAB() && ( i < 1 || i > 2 )) { | ||
264 | setCaption(i18n("Profile ")+syncProfileNames[mCurrentSyncProfile]+ i18n(" is synced ... ")); | ||
265 | ++syncedProfiles; | ||
266 | // KABPrefs::instance()->mAskForPreferences = temp->getAskForPreferences(); | ||
267 | KABPrefs::instance()->mWriteBackFile = temp->getWriteBackFile(); | ||
268 | KABPrefs::instance()->mWriteBackExistingOnly = temp->getWriteBackExisting(); | ||
269 | KABPrefs::instance()->mWriteBackInFuture = 0; | ||
270 | if ( temp->getWriteBackFuture() ) | ||
271 | KABPrefs::instance()->mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); | ||
272 | KABPrefs::instance()->mShowSyncSummary = false; | ||
273 | mCurrentSyncDevice = syncProfileNames[i] ; | ||
274 | mCurrentSyncName = KABPrefs::instance()->mLocalMachineName; | ||
275 | if ( i == 0 ) { | ||
276 | syncSharp(); | ||
277 | } else { | ||
278 | if ( temp->getIsLocalFileSync() ) { | ||
279 | if ( syncWithFile( temp->getRemoteFileNameAB( ), true ) ) | ||
280 | KABPrefs::instance()->mLastSyncedLocalFile = temp->getRemoteFileNameAB(); | ||
281 | } else { | ||
282 | if ( temp->getIsPhoneSync() ) { | ||
283 | KABPrefs::instance()->mPhoneDevice = temp->getPhoneDevice( ) ; | ||
284 | KABPrefs::instance()->mPhoneConnection = temp->getPhoneConnection( ); | ||
285 | KABPrefs::instance()->mPhoneModel = temp->getPhoneModel( ); | ||
286 | syncPhone(); | ||
287 | } else | ||
288 | syncRemote( temp, false ); | ||
289 | |||
290 | } | ||
291 | } | ||
292 | timer.start(); | ||
293 | setCaption(i18n("Multiple sync in progress ... please wait!") ); | ||
294 | while ( timer.elapsed () < 2000 ) { | ||
295 | qApp->processEvents(); | ||
296 | #ifndef _WIN32_ | ||
297 | sleep (1); | ||
298 | #endif | ||
299 | } | ||
300 | |||
301 | } | ||
302 | |||
303 | } | ||
304 | delete temp; | ||
305 | return syncedProfiles; | ||
306 | } | ||
307 | |||
308 | void KABCore::syncRemote( KSyncProfile* prof, bool ask) | ||
309 | { | ||
310 | QString question; | ||
311 | if ( ask ) { | ||
312 | question = i18n("Do you really want\nto remote sync\nwith profile \n")+ prof->getName()+" ?\n"; | ||
313 | if ( QMessageBox::information( this, i18n("KO/Pi Sync"), | ||
314 | question, | ||
315 | i18n("Yes"), i18n("No"), | ||
316 | 0, 0 ) != 0 ) | ||
317 | return; | ||
318 | } | ||
319 | QString command = prof->getPreSyncCommandAB(); | ||
320 | int fi; | ||
321 | if ( (fi = command.find("$PWD$")) > 0 ) { | ||
322 | QString pwd = getPassword(); | ||
323 | command = command.left( fi )+ pwd + command.mid( fi+5 ); | ||
324 | |||
325 | } | ||
326 | int maxlen = 30; | ||
327 | if ( QApplication::desktop()->width() > 320 ) | ||
328 | maxlen += 25; | ||
329 | setCaption ( i18n( "Copy remote file to local machine..." ) ); | ||
330 | int fileSize = 0; | ||
331 | int result = system ( command ); | ||
332 | // 0 : okay | ||
333 | // 256: no such file or dir | ||
334 | // | ||
335 | qDebug("KO: Remote copy result(0 = okay): %d ",result ); | ||
336 | if ( result != 0 ) { | ||
337 | int len = maxlen; | ||
338 | while ( len < command.length() ) { | ||
339 | command.insert( len , "\n" ); | ||
340 | len += maxlen +2; | ||
341 | } | ||
342 | question = i18n("Sorry, the copy command failed!\nCommand was:\n%1\n \nTry command on console to get more\ndetailed info about the reason.\n").arg (command) ; | ||
343 | QMessageBox::information( this, i18n("KO/Pi Sync - ERROR"), | ||
344 | question, | ||
345 | i18n("Okay!")) ; | ||
346 | setCaption ("KO/Pi"); | ||
347 | return; | ||
348 | } | ||
349 | setCaption ( i18n( "Copying succeed." ) ); | ||
350 | //qDebug(" file **%s** ",prof->getLocalTempFile().latin1() ); | ||
351 | if ( syncWithFile( prof->getLocalTempFileAB(), true ) ) { | ||
352 | // Event* e = mView->getLastSyncEvent(); | ||
353 | // e->setReadOnly( false ); | ||
354 | // e->setLocation( KOPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile]); | ||
355 | // e->setReadOnly( true ); | ||
356 | if ( KABPrefs::instance()->mWriteBackFile ) { | ||
357 | command = prof->getPostSyncCommandAB(); | ||
358 | int fi; | ||
359 | if ( (fi = command.find("$PWD$")) > 0 ) { | ||
360 | QString pwd = getPassword(); | ||
361 | command = command.left( fi )+ pwd + command.mid( fi+5 ); | ||
362 | |||
363 | } | ||
364 | setCaption ( i18n( "Writing back file ..." ) ); | ||
365 | result = system ( command ); | ||
366 | qDebug("KO: Writing back file result: %d ", result); | ||
367 | if ( result != 0 ) { | ||
368 | setCaption ( i18n( "Writing back file result: " )+QString::number( result ) ); | ||
369 | return; | ||
370 | } else { | ||
371 | setCaption ( i18n( "Syncronization sucessfully completed" ) ); | ||
372 | } | ||
373 | } | ||
374 | } | ||
375 | return; | ||
376 | } | ||
377 | #include <qpushbutton.h> | ||
378 | #include <qradiobutton.h> | ||
379 | #include <qbuttongroup.h> | ||
380 | void KABCore::edit_sync_options() | ||
381 | { | ||
382 | //mDialogManager->showSyncOptions(); | ||
383 | //KABPrefs::instance()->mSyncAlgoPrefs | ||
384 | QDialog dia( this, "dia", true ); | ||
385 | dia.setCaption( i18n("Device: " ) +mCurrentSyncDevice ); | ||
386 | QButtonGroup gr ( 1, Qt::Horizontal, i18n("Sync preferences"), &dia); | ||
387 | QVBoxLayout lay ( &dia ); | ||
388 | lay.setSpacing( 2 ); | ||
389 | lay.setMargin( 3 ); | ||
390 | lay.addWidget(&gr); | ||
391 | QRadioButton loc ( i18n("Take local entry on conflict"), &gr ); | ||
392 | QRadioButton rem ( i18n("Take remote entry on conflict"), &gr ); | ||
393 | QRadioButton newest( i18n("Take newest entry on conflict"), &gr ); | ||
394 | QRadioButton ask( i18n("Ask for every entry on conflict"), &gr ); | ||
395 | QRadioButton f_loc( i18n("Force: Take local entry always"), &gr ); | ||
396 | QRadioButton f_rem( i18n("Force: Take remote entry always"), &gr ); | ||
397 | //QRadioButton both( i18n("Take both on conflict"), &gr ); | ||
398 | QPushButton pb ( "OK", &dia); | ||
399 | lay.addWidget( &pb ); | ||
400 | connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); | ||
401 | switch ( KABPrefs::instance()->mSyncAlgoPrefs ) { | ||
402 | case 0: | ||
403 | loc.setChecked( true); | ||
404 | break; | ||
405 | case 1: | ||
406 | rem.setChecked( true ); | ||
407 | break; | ||
408 | case 2: | ||
409 | newest.setChecked( true); | ||
410 | break; | ||
411 | case 3: | ||
412 | ask.setChecked( true); | ||
413 | break; | ||
414 | case 4: | ||
415 | f_loc.setChecked( true); | ||
416 | break; | ||
417 | case 5: | ||
418 | f_rem.setChecked( true); | ||
419 | break; | ||
420 | case 6: | ||
421 | // both.setChecked( true); | ||
422 | break; | ||
423 | default: | ||
424 | break; | ||
425 | } | ||
426 | if ( dia.exec() ) { | ||
427 | KABPrefs::instance()->mSyncAlgoPrefs = rem.isChecked()*1+newest.isChecked()*2+ ask.isChecked()*3+ f_loc.isChecked()*4+ f_rem.isChecked()*5;//+ both.isChecked()*6 ; | ||
428 | } | ||
429 | |||
430 | |||
431 | } | ||
432 | QString KABCore::getPassword( ) | ||
433 | { | ||
434 | QString retfile = ""; | ||
435 | QDialog dia ( this, "input-dialog", true ); | ||
436 | QLineEdit lab ( &dia ); | ||
437 | lab.setEchoMode( QLineEdit::Password ); | ||
438 | QVBoxLayout lay( &dia ); | ||
439 | lay.setMargin(7); | ||
440 | lay.setSpacing(7); | ||
441 | lay.addWidget( &lab); | ||
442 | dia.setFixedSize( 230,50 ); | ||
443 | dia.setCaption( i18n("Enter password") ); | ||
444 | QPushButton pb ( "OK", &dia); | ||
445 | lay.addWidget( &pb ); | ||
446 | connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); | ||
447 | dia.show(); | ||
448 | int res = dia.exec(); | ||
449 | if ( res ) | ||
450 | retfile = lab.text(); | ||
451 | dia.hide(); | ||
452 | qApp->processEvents(); | ||
453 | return retfile; | ||
454 | |||
455 | } | ||