From 3138ae138248637d901528ee5d6cfc894b0ed7c2 Mon Sep 17 00:00:00 2001 From: ulf69 Date: Fri, 01 Oct 2004 00:04:23 +0000 Subject: initial revision of a globalized syncmanager that can be used by all apps --- (limited to 'libkdepim') 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 @@ +/* + This file is part of KDE-Pim/Pi. + Copyright (c) 2004 Ulf Schenk + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +// $Id$ + +#include "ksyncmanager.h" + + +KSyncManager::KSyncManager(TargetApp ta, QPopupMenu* syncmenu) + : mSyncMenu(syncmenu), mTargetApp(ta) +{ + +} + +KSyncManager::~KSyncManager() +{ + +} + + +void KABCore::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"); + //US KABPrefs::instance()->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; + } + /*US + KABPrefs::instance()->mExternSyncProfiles = config.readListEntry("ExternSyncProfiles"); + KABPrefs::instance()->mSyncProfileNames = prof; + */ + int i; + for ( i = 0; i < prof.count(); ++i ) { + + mSyncMenu->insertItem( prof[i], 1000+i ); + if ( i == 2 ) + mSyncMenu->insertSeparator(); + } + QDir app_dir; + if ( !app_dir.exists(QDir::homeDirPath()+"/Applications/dtm" ) ) { + mSyncMenu->setItemEnabled( false , 1000 ); + } + //probaly useless + //mView->setupExternSyncProfiles(); +} +void KABCore::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 (mBlockSaveFlag) + return; + mBlockSaveFlag = true; + mCurrentSyncProfile = action - 1000 ; + //US mCurrentSyncDevice = KABPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile] ; + //US mCurrentSyncName = KABPrefs::instance()->mLocalMachineName ; + KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); + KSyncProfile* temp = new KSyncProfile (); + //US temp->setName(KABPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile]); + temp->readConfig(&config); + /*US + KABPrefs::instance()->mAskForPreferences = temp->getAskForPreferences(); + KABPrefs::instance()->mSyncAlgoPrefs = temp->getSyncPrefs(); + KABPrefs::instance()->mWriteBackFile = temp->getWriteBackFile(); + KABPrefs::instance()->mWriteBackExistingOnly = temp->getWriteBackExisting(); + KABPrefs::instance()->mWriteBackInFuture = 0; + if ( temp->getWriteBackFuture() ) + KABPrefs::instance()->mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); + KABPrefs::instance()->mShowSyncSummary = temp->getShowSummaryAfterSync(); + */ + if ( action == 1000 ) { + syncSharp(); + + } else if ( action == 1001 ) { + syncLocalFile(); + + } else if ( action == 1002 ) { + quickSyncLocalFile(); + + } else if ( action >= 1003 ) { + if ( temp->getIsLocalFileSync() ) { + /*US if ( syncWithFile( temp->getRemoteFileNameAB( ), false ) ) + KABPrefs::instance()->mLastSyncedLocalFile = temp->getRemoteFileNameAB(); + */ + } else { + if ( temp->getIsPhoneSync() ) { + /* + KABPrefs::instance()->mPhoneDevice = temp->getPhoneDevice( ) ; + KABPrefs::instance()->mPhoneConnection = temp->getPhoneConnection( ); + KABPrefs::instance()->mPhoneModel = temp->getPhoneModel( ); + */ + syncPhone(); + } else + syncRemote( temp ); + + } + } + delete temp; + mBlockSaveFlag = false; +} + +void KABCore::syncLocalFile() +{ + + QString fn =KABPrefs::instance()->mLastSyncedLocalFile; + + fn =KFileDialog:: getOpenFileName( fn, i18n("Sync filename(*.ics/*.vcs)"), this ); + if ( fn == "" ) + return; + if ( syncWithFile( fn, false ) ) { + qDebug("syncLocalFile() successful "); + } + +} +bool KABCore::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( this, 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( this, i18n("Warning!"), + mess, + i18n("Sync"), i18n("Cancel"), 0, + 0, 1 ); + if ( result ) + return false; + } + if ( KABPrefs::instance()->mAskForPreferences ) + edit_sync_options(); + if ( result == 0 ) { + //qDebug("Now sycing ... "); + if ( ret = syncAB( fn, KABPrefs::instance()->mSyncAlgoPrefs ) ) + setCaption( i18n("Synchronization successful") ); + else + setCaption( i18n("Sync cancelled or failed. Nothing synced.") ); + if ( ! quick ) + KABPrefs::instance()->mLastSyncedLocalFile = fn; + setModified(); + } + return ret; +} +void KABCore::quickSyncLocalFile() +{ + + if ( syncWithFile( KABPrefs::instance()->mLastSyncedLocalFile, false ) ) { + qDebug("quick syncLocalFile() successful "); + + } +} +void KABCore::multiSync( bool askforPrefs ) +{ + if (mBlockSaveFlag) + return; + mBlockSaveFlag = 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( this, i18n("KO/Pi Sync"), + question, + i18n("Yes"), i18n("No"), + 0, 0 ) != 0 ) { + mBlockSaveFlag = false; + setCaption(i18n("Aborted! Nothing synced!")); + return; + } + mCurrentSyncDevice = i18n("Multiple profiles") ; + KABPrefs::instance()->mSyncAlgoPrefs = KABPrefs::instance()->mRingSyncAlgoPrefs; + if ( askforPrefs ) { + edit_sync_options(); + KABPrefs::instance()->mRingSyncAlgoPrefs = KABPrefs::instance()->mSyncAlgoPrefs; + } + setCaption(i18n("Multiple sync started.") ); + qApp->processEvents(); + int num = ringSync() ; + if ( num > 1 ) + ringSync(); + mBlockSaveFlag = false; + if ( num ) + save(); + if ( num ) + setCaption(i18n("%1 profiles synced. Multiple sync completed!").arg(num) ); + else + setCaption(i18n("Nothing synced! No profiles defined for multisync!")); + return; +} +int KABCore::ringSync() +{ + int syncedProfiles = 0; + int i; + QTime timer; + KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); + QStringList syncProfileNames = KABPrefs::instance()->mSyncProfileNames; + KSyncProfile* temp = new KSyncProfile (); + KABPrefs::instance()->mAskForPreferences = false; + for ( i = 0; i < syncProfileNames.count(); ++i ) { + mCurrentSyncProfile = i; + temp->setName(syncProfileNames[mCurrentSyncProfile]); + temp->readConfig(&config); + if ( temp->getIncludeInRingSyncAB() && ( i < 1 || i > 2 )) { + setCaption(i18n("Profile ")+syncProfileNames[mCurrentSyncProfile]+ i18n(" is synced ... ")); + ++syncedProfiles; + // KABPrefs::instance()->mAskForPreferences = temp->getAskForPreferences(); + KABPrefs::instance()->mWriteBackFile = temp->getWriteBackFile(); + KABPrefs::instance()->mWriteBackExistingOnly = temp->getWriteBackExisting(); + KABPrefs::instance()->mWriteBackInFuture = 0; + if ( temp->getWriteBackFuture() ) + KABPrefs::instance()->mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); + KABPrefs::instance()->mShowSyncSummary = false; + mCurrentSyncDevice = syncProfileNames[i] ; + mCurrentSyncName = KABPrefs::instance()->mLocalMachineName; + if ( i == 0 ) { + syncSharp(); + } else { + if ( temp->getIsLocalFileSync() ) { + if ( syncWithFile( temp->getRemoteFileNameAB( ), true ) ) + KABPrefs::instance()->mLastSyncedLocalFile = temp->getRemoteFileNameAB(); + } else { + if ( temp->getIsPhoneSync() ) { + KABPrefs::instance()->mPhoneDevice = temp->getPhoneDevice( ) ; + KABPrefs::instance()->mPhoneConnection = temp->getPhoneConnection( ); + KABPrefs::instance()->mPhoneModel = temp->getPhoneModel( ); + syncPhone(); + } else + syncRemote( temp, false ); + + } + } + timer.start(); + setCaption(i18n("Multiple sync in progress ... please wait!") ); + while ( timer.elapsed () < 2000 ) { + qApp->processEvents(); +#ifndef _WIN32_ + sleep (1); +#endif + } + + } + + } + delete temp; + return syncedProfiles; +} + +void KABCore::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( this, i18n("KO/Pi Sync"), + question, + i18n("Yes"), i18n("No"), + 0, 0 ) != 0 ) + return; + } + QString command = prof->getPreSyncCommandAB(); + int fi; + if ( (fi = command.find("$PWD$")) > 0 ) { + QString pwd = getPassword(); + command = command.left( fi )+ pwd + command.mid( fi+5 ); + + } + int maxlen = 30; + if ( QApplication::desktop()->width() > 320 ) + maxlen += 25; + setCaption ( i18n( "Copy remote file to local machine..." ) ); + int fileSize = 0; + int result = system ( command ); + // 0 : okay + // 256: no such file or dir + // + qDebug("KO: Remote copy result(0 = okay): %d ",result ); + if ( result != 0 ) { + int len = maxlen; + while ( len < command.length() ) { + command.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 (command) ; + QMessageBox::information( this, i18n("KO/Pi Sync - ERROR"), + question, + i18n("Okay!")) ; + setCaption ("KO/Pi"); + return; + } + setCaption ( i18n( "Copying succeed." ) ); + //qDebug(" file **%s** ",prof->getLocalTempFile().latin1() ); + if ( syncWithFile( prof->getLocalTempFileAB(), true ) ) { +// Event* e = mView->getLastSyncEvent(); +// e->setReadOnly( false ); +// e->setLocation( KOPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile]); +// e->setReadOnly( true ); + if ( KABPrefs::instance()->mWriteBackFile ) { + command = prof->getPostSyncCommandAB(); + int fi; + if ( (fi = command.find("$PWD$")) > 0 ) { + QString pwd = getPassword(); + command = command.left( fi )+ pwd + command.mid( fi+5 ); + + } + setCaption ( i18n( "Writing back file ..." ) ); + result = system ( command ); + qDebug("KO: Writing back file result: %d ", result); + if ( result != 0 ) { + setCaption ( i18n( "Writing back file result: " )+QString::number( result ) ); + return; + } else { + setCaption ( i18n( "Syncronization sucessfully completed" ) ); + } + } + } + return; +} +#include +#include +#include +void KABCore::edit_sync_options() +{ + //mDialogManager->showSyncOptions(); + //KABPrefs::instance()->mSyncAlgoPrefs + QDialog dia( this, "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 ( KABPrefs::instance()->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() ) { + KABPrefs::instance()->mSyncAlgoPrefs = rem.isChecked()*1+newest.isChecked()*2+ ask.isChecked()*3+ f_loc.isChecked()*4+ f_rem.isChecked()*5;//+ both.isChecked()*6 ; + } + + +} +QString KABCore::getPassword( ) +{ + QString retfile = ""; + QDialog dia ( this, "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; + +} diff --git a/libkdepim/ksyncmanager.h b/libkdepim/ksyncmanager.h new file mode 100644 index 0000000..d7c2a02 --- a/dev/null +++ b/libkdepim/ksyncmanager.h @@ -0,0 +1,68 @@ +/* + This file is part of KDE-Pim/Pi. + Copyright (c) 2004 Ulf Schenk + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. + +$Id$ +*/ +#ifndef _KSYNCMANAGER_H +#define _KSYNCMANAGER_H + +#include + +class QPopupMenu; +class KSyncProfile; + + + +class KSyncManager +{ + + public: + KSyncManager(TargetApp ta, QPopupMenu* syncmenu); + ~KSyncManager() ; + + enum TargetApp { + KOPI = 0, + KAPI = 1, + PWMPI = 2 }; + + private: + void fillSyncMenu(); + void syncLocalFile(); + bool syncWithFile( QString fn , bool quick ); + void quickSyncLocalFile(); + void multiSync( bool askforPrefs ); + int ringSync(); + void syncRemote( KSyncProfile* prof, bool ask); + void edit_sync_options(); + QString getPassword(); + + private: + QPopupMenu* mSyncMenu; + TargetApp mTargetApp; + + + + + + + + +}; + +#endif -- cgit v0.9.0.2