-rw-r--r-- | libkdepim/ksyncmanager.cpp | 100 |
1 files changed, 49 insertions, 51 deletions
diff --git a/libkdepim/ksyncmanager.cpp b/libkdepim/ksyncmanager.cpp index 07e6761..c0cc840 100644 --- a/libkdepim/ksyncmanager.cpp +++ b/libkdepim/ksyncmanager.cpp @@ -23,403 +23,401 @@ #include "ksyncmanager.h" #include <stdlib.h> #ifndef _WIN32_ #include <unistd.h> #endif #include "ksyncprofile.h" #include "ksyncprefsdialog.h" #include "kpimprefs.h" #include <qdir.h> #include <qprogressbar.h> #include <qpopupmenu.h> #include <qpushbutton.h> #include <qradiobutton.h> #include <qbuttongroup.h> #include <qtimer.h> #include <qmessagebox.h> #include <qapplication.h> #include <qlineedit.h> #include <qdialog.h> #include <qlayout.h> #include <klocale.h> #include <kglobal.h> #include <kconfig.h> #include <kfiledialog.h> KSyncManager::KSyncManager(QWidget* parent, KSyncInterface* implementation, TargetApp ta, KPimPrefs* prefs, QPopupMenu* syncmenu) - : QObject(), mParent(parent), mImplementation(implementation), mTargetApp(ta), mPrefs(prefs), mSyncMenu(syncmenu) + : QObject(), mParent(parent), mImplementation(implementation), mTargetApp(ta), mSyncMenu(syncmenu) { bar = new QProgressBar ( 1, 0 ); bar->setCaption (""); int w = 300; if ( QApplication::desktop()->width() < 320 ) w = 220; int h = bar->sizeHint().height() ; int dw = QApplication::desktop()->width(); int dh = QApplication::desktop()->height(); bar->setGeometry( (dw-w)/2, (dh - h )/2 ,w,h ); } KSyncManager::~KSyncManager() { delete bar; } void KSyncManager::fillSyncMenu() { if ( mSyncMenu->count() ) mSyncMenu->clear(); mSyncMenu->insertItem( i18n("Configure..."), 0 ); mSyncMenu->insertSeparator(); mSyncMenu->insertItem( i18n("Multiple sync"), 1 ); mSyncMenu->insertSeparator(); KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); config.setGroup("General"); QStringList prof = config.readListEntry("SyncProfileNames"); - mPrefs->mLocalMachineName = config.readEntry("LocalMachineName","undefined"); + mLocalMachineName = config.readEntry("LocalMachineName","undefined"); if ( prof.count() < 3 ) { prof.clear(); prof << i18n("Sharp_DTM"); prof << i18n("Local_file"); prof << i18n("Last_file"); KSyncProfile* temp = new KSyncProfile (); temp->setName( prof[0] ); temp->writeConfig(&config); temp->setName( prof[1] ); temp->writeConfig(&config); temp->setName( prof[2] ); temp->writeConfig(&config); config.setGroup("General"); config.writeEntry("SyncProfileNames",prof); config.writeEntry("ExternSyncProfiles","Sharp_DTM"); config.sync(); delete temp; } - mPrefs->mExternSyncProfiles = config.readListEntry("ExternSyncProfiles"); - mPrefs->mSyncProfileNames = prof; + mExternSyncProfiles = config.readListEntry("ExternSyncProfiles"); + mSyncProfileNames = prof; unsigned int i; for ( i = 0; i < prof.count(); ++i ) { mSyncMenu->insertItem( prof[i], 1000+i ); if ( i == 2 ) mSyncMenu->insertSeparator(); } QDir app_dir; //US do not display SharpDTM if app is pwmpi, or no sharpfiles available if ( mTargetApp == PWMPI) { mSyncMenu->removeItem( 1000 ); } else if (!app_dir.exists(QDir::homeDirPath()+"/Applications/dtm" ) ) { mSyncMenu->setItemEnabled( 1000, false ); } - //probaly useless - //mView->setupExternSyncProfiles(); } void KSyncManager::slotSyncMenu( int action ) { //qDebug("syncaction %d ", action); if ( action == 0 ) { // seems to be a Qt2 event handling bug // syncmenu.clear causes a segfault at first time // when we call it after the main event loop, it is ok // same behaviour when calling OM/Pi via QCOP for the first time QTimer::singleShot ( 1, this, SLOT ( confSync() ) ); //confSync(); return; } if ( action == 1 ) { multiSync( true ); return; } if (blockSave()) return; setBlockSave(true); mCurrentSyncProfile = action - 1000 ; - mCurrentSyncDevice = mPrefs->mSyncProfileNames[mCurrentSyncProfile] ; - mCurrentSyncName = mPrefs->mLocalMachineName ; + mCurrentSyncDevice = mSyncProfileNames[mCurrentSyncProfile] ; + mCurrentSyncName = mLocalMachineName ; KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); KSyncProfile* temp = new KSyncProfile (); - temp->setName(mPrefs->mSyncProfileNames[mCurrentSyncProfile]); + temp->setName(mSyncProfileNames[mCurrentSyncProfile]); temp->readConfig(&config); - mPrefs->mAskForPreferences = temp->getAskForPreferences(); - mPrefs->mSyncAlgoPrefs = temp->getSyncPrefs(); - mPrefs->mWriteBackFile = temp->getWriteBackFile(); - mPrefs->mWriteBackExistingOnly = temp->getWriteBackExisting(); - mPrefs->mWriteBackInFuture = 0; + mAskForPreferences = temp->getAskForPreferences(); + mSyncAlgoPrefs = temp->getSyncPrefs(); + mWriteBackFile = temp->getWriteBackFile(); + mWriteBackExistingOnly = temp->getWriteBackExisting(); + mWriteBackInFuture = 0; if ( temp->getWriteBackFuture() ) - mPrefs->mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); - mPrefs->mShowSyncSummary = temp->getShowSummaryAfterSync(); + mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); + mShowSyncSummary = temp->getShowSummaryAfterSync(); if ( action == 1000 ) { syncSharp(); } else if ( action == 1001 ) { syncLocalFile(); } else if ( action == 1002 ) { quickSyncLocalFile(); } else if ( action >= 1003 ) { if ( temp->getIsLocalFileSync() ) { switch(mTargetApp) { case (KAPI): if ( syncWithFile( temp->getRemoteFileNameAB( ), false ) ) - mPrefs->mLastSyncedLocalFile = temp->getRemoteFileNameAB(); + mLastSyncedLocalFile = temp->getRemoteFileNameAB(); break; case (KOPI): if ( syncWithFile( temp->getRemoteFileName( ), false ) ) - mPrefs->mLastSyncedLocalFile = temp->getRemoteFileName(); + mLastSyncedLocalFile = temp->getRemoteFileName(); break; case (PWMPI): if ( syncWithFile( temp->getRemoteFileNamePWM( ), false ) ) - mPrefs->mLastSyncedLocalFile = temp->getRemoteFileNamePWM(); + mLastSyncedLocalFile = temp->getRemoteFileNamePWM(); break; default: qDebug("KSyncManager::slotSyncMenu: invalid apptype selected"); break; } } else { if ( temp->getIsPhoneSync() ) { - mPrefs->mPhoneDevice = temp->getPhoneDevice( ) ; - mPrefs->mPhoneConnection = temp->getPhoneConnection( ); - mPrefs->mPhoneModel = temp->getPhoneModel( ); + mPhoneDevice = temp->getPhoneDevice( ) ; + mPhoneConnection = temp->getPhoneConnection( ); + mPhoneModel = temp->getPhoneModel( ); syncPhone(); } else syncRemote( temp ); } } delete temp; setBlockSave(false); } void KSyncManager::syncLocalFile() { - QString fn =mPrefs->mLastSyncedLocalFile; + QString fn =mLastSyncedLocalFile; QString ext; switch(mTargetApp) { case (KAPI): ext = "(*.vcf)"; break; case (KOPI): ext = "(*.ics/*.vcs)"; break; case (PWMPI): ext = "(*.pwm)"; break; default: qDebug("KSyncManager::syncLocalFile: invalid apptype selected"); break; } fn =KFileDialog:: getOpenFileName( fn, i18n("Sync filename"+ext), mParent ); if ( fn == "" ) return; if ( syncWithFile( fn, false ) ) { qDebug("syncLocalFile() successful "); } } bool KSyncManager::syncWithFile( QString fn , bool quick ) { bool ret = false; QFileInfo info; info.setFile( fn ); QString mess; bool loadbup = true; if ( !info. exists() ) { mess = i18n( "Sync file \n...%1\ndoes not exist!\nNothing synced!\n").arg(fn.right( 30) ); int result = QMessageBox::warning( mParent, i18n("Warning!"), mess ); return ret; } int result = 0; if ( !quick ) { mess = i18n("Sync with file \n...%1\nfrom:\n%2\n").arg(fn.right( 25)).arg(KGlobal::locale()->formatDateTime(info.lastModified (), true, false )); result = QMessageBox::warning( mParent, i18n("Warning!"), mess, i18n("Sync"), i18n("Cancel"), 0, 0, 1 ); if ( result ) return false; } - if ( mPrefs->mAskForPreferences ) + if ( mAskForPreferences ) edit_sync_options(); if ( result == 0 ) { //qDebug("Now sycing ... "); - if ( ret = mImplementation->sync( this, fn, mPrefs->mSyncAlgoPrefs ) ) + if ( ret = mImplementation->sync( this, fn, mSyncAlgoPrefs ) ) mParent->setCaption( i18n("Synchronization successful") ); else mParent->setCaption( i18n("Sync cancelled or failed. Nothing synced.") ); if ( ! quick ) - mPrefs->mLastSyncedLocalFile = fn; + mLastSyncedLocalFile = fn; mImplementation->sync_setModified(); } return ret; } void KSyncManager::quickSyncLocalFile() { - if ( syncWithFile( mPrefs->mLastSyncedLocalFile, false ) ) { + if ( syncWithFile( mLastSyncedLocalFile, false ) ) { qDebug("quick syncLocalFile() successful "); } } void KSyncManager::multiSync( bool askforPrefs ) { if (blockSave()) return; setBlockSave(true); QString question = i18n("Do you really want\nto multiple sync\nwith all checked profiles?\nSyncing takes some\ntime - all profiles\nare synced twice!"); if ( QMessageBox::information( mParent, i18n("Sync"), question, i18n("Yes"), i18n("No"), 0, 0 ) != 0 ) { setBlockSave(false); mParent->setCaption(i18n("Aborted! Nothing synced!")); return; } mCurrentSyncDevice = i18n("Multiple profiles") ; - mPrefs->mSyncAlgoPrefs = mPrefs->mRingSyncAlgoPrefs; + mSyncAlgoPrefs = mRingSyncAlgoPrefs; if ( askforPrefs ) { edit_sync_options(); - mPrefs->mRingSyncAlgoPrefs = mPrefs->mSyncAlgoPrefs; + mRingSyncAlgoPrefs = mSyncAlgoPrefs; } mParent->setCaption(i18n("Multiple sync started.") ); qApp->processEvents(); int num = ringSync() ; if ( num > 1 ) ringSync(); setBlockSave(false); if ( num ) mImplementation->sync_save(); if ( num ) mParent->setCaption(i18n("%1 profiles synced. Multiple sync completed!").arg(num) ); else mParent->setCaption(i18n("Nothing synced! No profiles defined for multisync!")); return; } int KSyncManager::ringSync() { int syncedProfiles = 0; unsigned int i; QTime timer; KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); - QStringList syncProfileNames = mPrefs->mSyncProfileNames; + QStringList syncProfileNames = mSyncProfileNames; KSyncProfile* temp = new KSyncProfile (); - mPrefs->mAskForPreferences = false; + mAskForPreferences = false; for ( i = 0; i < syncProfileNames.count(); ++i ) { mCurrentSyncProfile = i; temp->setName(syncProfileNames[mCurrentSyncProfile]); temp->readConfig(&config); QString includeInRingSync; switch(mTargetApp) { case (KAPI): includeInRingSync = temp->getIncludeInRingSyncAB(); break; case (KOPI): includeInRingSync = temp->getIncludeInRingSync(); break; case (PWMPI): includeInRingSync = temp->getIncludeInRingSyncPWM(); break; default: qDebug("KSyncManager::ringSync: invalid apptype selected"); break; } if ( includeInRingSync && ( i < 1 || i > 2 )) { mParent->setCaption(i18n("Profile ")+syncProfileNames[mCurrentSyncProfile]+ i18n(" is synced ... ")); ++syncedProfiles; - // mPrefs->mAskForPreferences = temp->getAskForPreferences(); - mPrefs->mWriteBackFile = temp->getWriteBackFile(); - mPrefs->mWriteBackExistingOnly = temp->getWriteBackExisting(); - mPrefs->mWriteBackInFuture = 0; + // mAskForPreferences = temp->getAskForPreferences(); + mWriteBackFile = temp->getWriteBackFile(); + mWriteBackExistingOnly = temp->getWriteBackExisting(); + mWriteBackInFuture = 0; if ( temp->getWriteBackFuture() ) - mPrefs->mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); - mPrefs->mShowSyncSummary = false; + mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); + mShowSyncSummary = false; mCurrentSyncDevice = syncProfileNames[i] ; - mCurrentSyncName = mPrefs->mLocalMachineName; + mCurrentSyncName = mLocalMachineName; if ( i == 0 ) { syncSharp(); } else { if ( temp->getIsLocalFileSync() ) { switch(mTargetApp) { case (KAPI): if ( syncWithFile( temp->getRemoteFileNameAB( ), false ) ) - mPrefs->mLastSyncedLocalFile = temp->getRemoteFileNameAB(); + mLastSyncedLocalFile = temp->getRemoteFileNameAB(); break; case (KOPI): if ( syncWithFile( temp->getRemoteFileName( ), false ) ) - mPrefs->mLastSyncedLocalFile = temp->getRemoteFileName(); + mLastSyncedLocalFile = temp->getRemoteFileName(); break; case (PWMPI): if ( syncWithFile( temp->getRemoteFileNamePWM( ), false ) ) - mPrefs->mLastSyncedLocalFile = temp->getRemoteFileNamePWM(); + mLastSyncedLocalFile = temp->getRemoteFileNamePWM(); break; default: qDebug("KSyncManager::slotSyncMenu: invalid apptype selected"); break; } } else { if ( temp->getIsPhoneSync() ) { - mPrefs->mPhoneDevice = temp->getPhoneDevice( ) ; - mPrefs->mPhoneConnection = temp->getPhoneConnection( ); - mPrefs->mPhoneModel = temp->getPhoneModel( ); + mPhoneDevice = temp->getPhoneDevice( ) ; + mPhoneConnection = temp->getPhoneConnection( ); + mPhoneModel = temp->getPhoneModel( ); syncPhone(); } else syncRemote( temp, false ); } } timer.start(); mParent->setCaption(i18n("Multiple sync in progress ... please wait!") ); while ( timer.elapsed () < 2000 ) { qApp->processEvents(); #ifndef _WIN32_ sleep (1); #endif } } } delete temp; return syncedProfiles; } void KSyncManager::syncRemote( KSyncProfile* prof, bool ask) { QString question; if ( ask ) { question = i18n("Do you really want\nto remote sync\nwith profile \n")+ prof->getName()+" ?\n"; if ( QMessageBox::information( mParent, i18n("Sync"), question, i18n("Yes"), i18n("No"), 0, 0 ) != 0 ) return; @@ -462,193 +460,193 @@ void KSyncManager::syncRemote( KSyncProfile* prof, bool ask) if ( QApplication::desktop()->width() > 320 ) maxlen += 25; mParent->setCaption ( i18n( "Copy remote file to local machine..." ) ); int fileSize = 0; int result = system ( preCommand ); // 0 : okay // 256: no such file or dir // qDebug("KO: Remote copy result(0 = okay): %d ",result ); if ( result != 0 ) { unsigned int len = maxlen; while ( len < preCommand.length() ) { preCommand.insert( len , "\n" ); len += maxlen +2; } 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 (preCommand) ; QMessageBox::information( mParent, i18n("Sync - ERROR"), question, i18n("Okay!")) ; mParent->setCaption (""); return; } mParent->setCaption ( i18n( "Copying succeed." ) ); //qDebug(" file **%s** ",prof->getLocalTempFile().latin1() ); if ( syncWithFile( localTempFile, true ) ) { // Event* e = mView->getLastSyncEvent(); // e->setReadOnly( false ); // e->setLocation( KOPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile]); // e->setReadOnly( true ); - if ( mPrefs->mWriteBackFile ) { + if ( mWriteBackFile ) { int fi; if ( (fi = postCommand.find("$PWD$")) > 0 ) { QString pwd = getPassword(); postCommand = postCommand.left( fi )+ pwd + postCommand.mid( fi+5 ); } mParent->setCaption ( i18n( "Writing back file ..." ) ); result = system ( postCommand ); qDebug("Writing back file result: %d ", result); if ( result != 0 ) { mParent->setCaption ( i18n( "Writing back file result: " )+QString::number( result ) ); return; } else { mParent->setCaption ( i18n( "Syncronization sucessfully completed" ) ); } } } return; } void KSyncManager::edit_sync_options() { //mDialogManager->showSyncOptions(); - //mPrefs->mSyncAlgoPrefs + //mSyncAlgoPrefs QDialog dia( mParent, "dia", true ); dia.setCaption( i18n("Device: " ) +mCurrentSyncDevice ); QButtonGroup gr ( 1, Qt::Horizontal, i18n("Sync preferences"), &dia); QVBoxLayout lay ( &dia ); lay.setSpacing( 2 ); lay.setMargin( 3 ); lay.addWidget(&gr); QRadioButton loc ( i18n("Take local entry on conflict"), &gr ); QRadioButton rem ( i18n("Take remote entry on conflict"), &gr ); QRadioButton newest( i18n("Take newest entry on conflict"), &gr ); QRadioButton ask( i18n("Ask for every entry on conflict"), &gr ); QRadioButton f_loc( i18n("Force: Take local entry always"), &gr ); QRadioButton f_rem( i18n("Force: Take remote entry always"), &gr ); //QRadioButton both( i18n("Take both on conflict"), &gr ); QPushButton pb ( "OK", &dia); lay.addWidget( &pb ); connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); - switch ( mPrefs->mSyncAlgoPrefs ) { + switch ( mSyncAlgoPrefs ) { case 0: loc.setChecked( true); break; case 1: rem.setChecked( true ); break; case 2: newest.setChecked( true); break; case 3: ask.setChecked( true); break; case 4: f_loc.setChecked( true); break; case 5: f_rem.setChecked( true); break; case 6: // both.setChecked( true); break; default: break; } if ( dia.exec() ) { - mPrefs->mSyncAlgoPrefs = rem.isChecked()*1+newest.isChecked()*2+ ask.isChecked()*3+ f_loc.isChecked()*4+ f_rem.isChecked()*5;//+ both.isChecked()*6 ; + mSyncAlgoPrefs = rem.isChecked()*1+newest.isChecked()*2+ ask.isChecked()*3+ f_loc.isChecked()*4+ f_rem.isChecked()*5;//+ both.isChecked()*6 ; } } QString KSyncManager::getPassword( ) { QString retfile = ""; QDialog dia ( mParent, "input-dialog", true ); QLineEdit lab ( &dia ); lab.setEchoMode( QLineEdit::Password ); QVBoxLayout lay( &dia ); lay.setMargin(7); lay.setSpacing(7); lay.addWidget( &lab); dia.setFixedSize( 230,50 ); dia.setCaption( i18n("Enter password") ); QPushButton pb ( "OK", &dia); lay.addWidget( &pb ); connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); dia.show(); int res = dia.exec(); if ( res ) retfile = lab.text(); dia.hide(); qApp->processEvents(); return retfile; } void KSyncManager::confSync() { static KSyncPrefsDialog* sp = 0; if ( ! sp ) { sp = new KSyncPrefsDialog( mParent, "syncprefs", true ); } sp->usrReadConfig(); #ifndef DESKTOP_VERSION sp->showMaximized(); #else sp->show(); #endif sp->exec(); - mPrefs->mSyncProfileNames = sp->getSyncProfileNames(); - mPrefs->mLocalMachineName = sp->getLocalMachineName (); + mSyncProfileNames = sp->getSyncProfileNames(); + mLocalMachineName = sp->getLocalMachineName (); fillSyncMenu(); } void KSyncManager::syncSharp() { if ( ! syncExternalApplication("sharp") ) qDebug("ERROR sync sharp ");; } bool KSyncManager::syncExternalApplication(QString resource) { if ( mImplementation->sync_isModified() ) mImplementation->sync_save(); - if ( mPrefs->mAskForPreferences ) + if ( mAskForPreferences ) edit_sync_options(); qDebug("sync %s", resource.latin1()); bool syncOK = mImplementation->syncExternal(this, resource); return syncOK; } void KSyncManager::syncPhone() { if ( mImplementation->sync_isModified() ) mImplementation->sync_save(); qDebug("pending syncPhone(); "); //mView->syncPhone(); mImplementation->sync_setModified(); } void KSyncManager::showProgressBar(int percentage, QString caption, int total) { if (!bar->isVisible()) { bar->setCaption (caption); bar->setTotalSteps ( total ) ; bar->show(); } bar->setProgress( percentage ); |