-rw-r--r-- | libkdepim/ksyncmanager.cpp | 86 |
1 files changed, 65 insertions, 21 deletions
diff --git a/libkdepim/ksyncmanager.cpp b/libkdepim/ksyncmanager.cpp index d8ca3ba..47d00a4 100644 --- a/libkdepim/ksyncmanager.cpp +++ b/libkdepim/ksyncmanager.cpp @@ -881,661 +881,705 @@ QString KSyncManager::getPassword( ) 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(); QStringList oldSyncProfileNames = mSyncProfileNames; mSyncProfileNames = sp->getSyncProfileNames(); mLocalMachineName = sp->getLocalMachineName (); int ii; for ( ii = 0; ii < oldSyncProfileNames.count(); ++ii ) { if ( ! mSyncProfileNames.contains( oldSyncProfileNames[ii] ) ) mImplementation->removeSyncInfo( oldSyncProfileNames[ii] ); } QTimer::singleShot ( 1, this, SLOT ( fillSyncMenu() ) ); } void KSyncManager::syncKDE() { mSyncWithDesktop = true; emit save(); switch(mTargetApp) { case (KAPI): { #ifdef DESKTOP_VERSION QString command = qApp->applicationDirPath () + "/kdeabdump"; #else QString command = "kdeabdump"; #endif if ( ! QFile::exists ( command ) ) command = "kdeabdump"; QString fileName = QDir::homeDirPath ()+"/.kdeaddressbookdump.vcf"; system ( command.latin1()); if ( syncWithFile( fileName,true ) ) { if ( mWriteBackFile ) { command += " --read"; system ( command.latin1()); } } } break; case (KOPI): { #ifdef DESKTOP_VERSION QString command = qApp->applicationDirPath () + "/kdecaldump"; #else QString command = "kdecaldump"; #endif if ( ! QFile::exists ( command ) ) command = "kdecaldump"; QString fileName = QDir::homeDirPath ()+"/.kdecalendardump.ics"; system ( command.latin1()); if ( syncWithFile( fileName,true ) ) { if ( mWriteBackFile ) { command += " --read"; system ( command.latin1()); } } } break; case (PWMPI): break; default: qDebug("KSM::slotSyncMenu: invalid apptype selected"); break; } } void KSyncManager::syncSharp() { if ( ! syncExternalApplication("sharp") ) qDebug("KSM::ERROR sync sharp "); } bool KSyncManager::syncExternalApplication(QString resource) { emit save(); if ( mAskForPreferences ) if ( !edit_sync_options()) { mParent->topLevelWidget()->setCaption( i18n("Syncing aborted. Nothing synced.") ); return false; } qDebug("KSM::Sync extern %s", resource.latin1()); bool syncOK = mImplementation->syncExternal(this, resource); return syncOK; } void KSyncManager::syncPhone() { syncExternalApplication("phone"); } void KSyncManager::showProgressBar(int percentage, QString caption, int total) { if (!bar->isVisible()) { bar->setCaption (caption); bar->setTotalSteps ( total ) ; bar->show(); } bar->raise(); bar->setProgress( percentage ); qApp->processEvents(); } void KSyncManager::hideProgressBar() { bar->hide(); qApp->processEvents(); } bool KSyncManager::isProgressBarCanceled() { return !bar->isVisible(); } QString KSyncManager::syncFileName() { QString fn = "tempfile"; switch(mTargetApp) { case (KAPI): fn = "tempsyncab.vcf"; break; case (KOPI): fn = "tempsynccal.ics"; break; case (PWMPI): fn = "tempsyncpw.pwm"; break; default: break; } #ifdef _WIN32_ return locateLocal( "tmp", fn ); #else return (QString( "/tmp/" )+ fn ); #endif } void KSyncManager::syncPi() { mIsKapiFile = true; mPisyncFinished = false; qApp->processEvents(); if ( mAskForPreferences ) if ( !edit_pisync_options()) { mParent->topLevelWidget()->setCaption( i18n("Syncing aborted. Nothing synced.") ); mPisyncFinished = true; return; } bool ok; Q_UINT16 port = mActiveSyncPort.toUInt(&ok); if ( ! ok ) { mParent->topLevelWidget()->setCaption( i18n("Sorry, no valid port.Syncing cancelled.") ); mPisyncFinished = true; return; } KCommandSocket* commandSocket = new KCommandSocket( mPassWordPiSync, port, mActiveSyncIP, this, mParent->topLevelWidget() ); connect( commandSocket, SIGNAL(commandFinished( KCommandSocket*, int )), this, SLOT(deleteCommandSocket(KCommandSocket*, int)) ); commandSocket->readFile( syncFileName() ); } void KSyncManager::deleteCommandSocket(KCommandSocket*s, int state) { //enum { success, errorW, errorR, quiet }; - if ( state == KCommandSocket::errorR ||state == KCommandSocket::errorTO ||state == KCommandSocket::errorPW ) { + + + + if ( state == KCommandSocket::errorR ||state == KCommandSocket::errorTO ||state == KCommandSocket::errorPW || + state == KCommandSocket::errorCA ||state == KCommandSocket::errorFI ||state == KCommandSocket::errorUN ) { if ( state == KCommandSocket::errorPW ) mParent->topLevelWidget()->setCaption( i18n("Wrong password: Receiving remote file failed.") ); - else + else if ( state == KCommandSocket::errorR ||state == KCommandSocket::errorTO ) mParent->topLevelWidget()->setCaption( i18n("ERROR: Receiving remote file failed.") ); + else if ( state == KCommandSocket::errorCA ) + mParent->topLevelWidget()->setCaption( i18n("Sync cancelled from remote.") ); + else if ( state == KCommandSocket::errorFI ) + mParent->topLevelWidget()->setCaption( i18n("File error on remote.") ); + else if ( state == KCommandSocket::errorUN ) + mParent->topLevelWidget()->setCaption( i18n("Unknown error on remote.") ); delete s; if ( state == KCommandSocket::errorR ) { KCommandSocket* commandSocket = new KCommandSocket( mPassWordPiSync, mActiveSyncPort.toUInt(), mActiveSyncIP, this, mParent->topLevelWidget()); connect( commandSocket, SIGNAL(commandFinished( KCommandSocket*, int)), this, SLOT(deleteCommandSocket(KCommandSocket*, int )) ); commandSocket->sendStop(); } mPisyncFinished = true; return; } else if ( state == KCommandSocket::errorW ) { mParent->topLevelWidget()->setCaption( i18n("ERROR:Writing back file failed.") ); mPisyncFinished = true; } else if ( state == KCommandSocket::successR ) { QTimer::singleShot( 1, this , SLOT ( readFileFromSocket())); } else if ( state == KCommandSocket::successW ) { mParent->topLevelWidget()->setCaption( i18n("Pi-Sync successful!") ); mPisyncFinished = true; } else if ( state == KCommandSocket::quiet ){ qDebug("KSS: quiet "); mPisyncFinished = true; } else { qDebug("KSS: Error: unknown state: %d ", state); mPisyncFinished = true; } delete s; } void KSyncManager::readFileFromSocket() { QString fileName = syncFileName(); bool syncOK = true; mParent->topLevelWidget()->setCaption( i18n("Remote file saved to temp file.") ); if ( ! syncWithFile( fileName , true ) ) { mParent->topLevelWidget()->setCaption( i18n("Syncing failed.") ); syncOK = false; } KCommandSocket* commandSocket = new KCommandSocket( mPassWordPiSync, mActiveSyncPort.toUInt(), mActiveSyncIP, this, mParent->topLevelWidget() ); connect( commandSocket, SIGNAL(commandFinished( KCommandSocket*, int)), this, SLOT(deleteCommandSocket(KCommandSocket*, int )) ); if ( mWriteBackFile && syncOK ) commandSocket->writeFile( fileName ); else { commandSocket->sendStop(); if ( syncOK ) mParent->topLevelWidget()->setCaption( i18n("Pi-Sync succesful!") ); mPisyncFinished = true; } } KServerSocket:: KServerSocket ( QString pw, Q_UINT16 port, int backlog, QObject * parent, const char * name ) : QServerSocket( port, backlog, parent, name ) { mPassWord = pw; mSocket = 0; mSyncActionDialog = 0; blockRC = false; + mErrorMessage = 0; } void KServerSocket::newConnection ( int socket ) { // qDebug("KServerSocket:New connection %d ", socket); if ( mSocket ) { qDebug("KSS::newConnection Socket deleted! "); delete mSocket; mSocket = 0; } mSocket = new QSocket( this ); connect( mSocket , SIGNAL(readyRead()), this, SLOT(readClient()) ); connect( mSocket , SIGNAL(delayedCloseFinished()), this, SLOT(discardClient()) ); mSocket->setSocket( socket ); } void KServerSocket::discardClient() { - //qDebug(" KServerSocket::discardClient()"); + QTimer::singleShot( 10, this , SLOT ( deleteSocket())); +} +void KServerSocket::deleteSocket() +{ + qDebug("KSS::deleteSocket"); if ( mSocket ) { delete mSocket; mSocket = 0; } - //emit endConnect(); + if ( mErrorMessage ) + QTimer::singleShot( 10, this , SLOT ( displayErrorMessage())); } void KServerSocket::readClient() { if ( blockRC ) return; if ( mSocket == 0 ) { qDebug("ERROR::KSS::readClient(): mSocket == 0 "); return; } + if ( mErrorMessage ) { + mErrorMessage = 999; + error_connect("ERROR_UN\r\n\r\n"); + return; + } + mErrorMessage = 0; //qDebug("KServerSocket::readClient()"); if ( mSocket->canReadLine() ) { QString line = mSocket->readLine(); //qDebug("KServerSocket readline: %s ", line.latin1()); QStringList tokens = QStringList::split( QRegExp("[ \r\n][ \r\n]*"), line ); if ( tokens[0] == "GET" ) { if ( tokens[1] == mPassWord ) { //emit sendFile( mSocket ); bool ok = false; QDateTime dt = KGlobal::locale()->readDateTime( tokens[2], KLocale::ISODate, &ok); if ( ok ) { KSyncManager::mRequestedSyncEvent = dt; } else KSyncManager::mRequestedSyncEvent = QDateTime(); send_file(); } else { - error_connect(); - KMessageBox::error( 0, i18n("Got send file request\nwith invalid password")); - //qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); + mErrorMessage = 1; + error_connect("ERROR_PW\r\n\r\n"); } } if ( tokens[0] == "PUT" ) { if ( tokens[1] == mPassWord ) { //emit getFile( mSocket ); blockRC = true; get_file(); } else { - error_connect(); + mErrorMessage = 2; + error_connect("ERROR_PW\r\n\r\n"); end_connect(); - KMessageBox::error( 0, i18n("Got receive file request\nwith invalid password")); - //qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); } } if ( tokens[0] == "STOP" ) { //emit endConnect(); end_connect(); } } } -void KServerSocket::error_connect() +void KServerSocket::displayErrorMessage() +{ + if ( mErrorMessage == 1 ) + KMessageBox::error( 0, i18n("Got send file request\nwith invalid password")); + else if ( mErrorMessage == 2 ) + KMessageBox::error( 0, i18n("Got receive file request\nwith invalid password")); + mErrorMessage = 0; +} +void KServerSocket::error_connect( QString errmess ) { QTextStream os( mSocket ); os.setEncoding( QTextStream::Latin1 ); - os << "ERROR_PW\r\n\r\n"; + os << errmess ; mSocket->close(); - if ( mSocket->state() == QSocket::Idle ) - QTimer::singleShot( 10, this , SLOT ( discardClient())); + if ( mSocket->state() == QSocket::Idle ) { + QTimer::singleShot( 0, this , SLOT ( discardClient())); + } } void KServerSocket::end_connect() { delete mSyncActionDialog; mSyncActionDialog = 0; } void KServerSocket::send_file() { //qDebug("MainWindow::sendFile(QSocket* s) "); if ( mSyncActionDialog ) delete mSyncActionDialog; mSyncActionDialog = new QDialog ( 0, "input-dialog", true ); mSyncActionDialog->setCaption(i18n("Received sync request")); QLabel* label = new QLabel( i18n("Synchronizing from remote ...\n\nDo not use this application!\n\nIf syncing fails\nyou can close this dialog."), mSyncActionDialog ); label->setAlignment ( Qt::AlignHCenter ); QVBoxLayout* lay = new QVBoxLayout( mSyncActionDialog ); lay->addWidget( label); lay->setMargin(7); lay->setSpacing(7); if ( KSyncManager::mRequestedSyncEvent.isValid() ) { int secs = QDateTime::currentDateTime().secsTo( KSyncManager::mRequestedSyncEvent ); + secs = 333; if ( secs < 0 ) secs = secs * (-1); if ( secs > 30 ) //if ( true ) { QString warning = i18n("Clock skew of\nsyncing devices\nis %1 seconds!").arg( secs ); QLabel* label = new QLabel( warning, mSyncActionDialog ); label->setAlignment ( Qt::AlignHCenter ); lay->addWidget( label); if ( secs > 180 ) { if ( secs > 300 ) { if ( KMessageBox::Cancel == KMessageBox::warningContinueCancel(0, i18n("The clocks of the syncing\ndevices have a difference\nof more than 5 minutes.\nPlease adjust your clocks.\nYou may get wrong syncing results!\nPlease confirm synchronization!"), i18n("High clock skew!"),i18n("Synchronize!"))) { qDebug("KSS::Sync cancelled ,cs"); + mErrorMessage = 0; + end_connect(); + error_connect("ERROR_CA\r\n\r\n"); return ; } } QFont f = label->font(); f.setPointSize ( f.pointSize() *2 ); f. setBold (true ); QLabel* label = new QLabel( warning, mSyncActionDialog ); label->setFont( f ); warning = i18n("ADJUST\nYOUR\nCLOCKS!"); label->setText( warning ); label->setAlignment ( Qt::AlignHCenter ); lay->addWidget( label); mSyncActionDialog->setFixedSize( 230, 300); } else { mSyncActionDialog->setFixedSize( 230, 200); } } else { mSyncActionDialog->setFixedSize( 230, 120); } } else mSyncActionDialog->setFixedSize( 230, 120); mSyncActionDialog->show(); mSyncActionDialog->raise(); emit request_file(); qApp->processEvents(); QString fileName = mFileName; QFile file( fileName ); if (!file.open( IO_ReadOnly ) ) { - delete mSyncActionDialog; - mSyncActionDialog = 0; - qDebug("KSS::error open sync file: %s ", fileName.latin1()); - mSocket->close(); - if ( mSocket->state() == QSocket::Idle ) - QTimer::singleShot( 10, this , SLOT ( discardClient())); + mErrorMessage = 0; + end_connect(); + error_connect("ERROR_FI\r\n\r\n"); return ; - } mSyncActionDialog->setCaption( i18n("Sending file...") ); QTextStream ts( &file ); ts.setEncoding( QTextStream::Latin1 ); QTextStream os( mSocket ); os.setEncoding( QTextStream::Latin1 ); while ( ! ts.atEnd() ) { os << ts.readLine() << "\r\n"; } os << "\r\n"; //os << ts.read(); file.close(); mSyncActionDialog->setCaption( i18n("Waiting for synced file...") ); mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( discardClient())); } void KServerSocket::get_file() { mSyncActionDialog->setCaption( i18n("Receiving synced file...") ); piTime.start(); piFileString = ""; QTimer::singleShot( 1, this , SLOT (readBackFileFromSocket( ) )); } void KServerSocket::readBackFileFromSocket() { //qDebug("readBackFileFromSocket() %d ", piTime.elapsed ()); while ( mSocket->canReadLine () ) { piTime.restart(); QString line = mSocket->readLine (); piFileString += line; //qDebug("readline: %s ", line.latin1()); mSyncActionDialog->setCaption( i18n("Received %1 bytes").arg( piFileString.length() ) ); } if ( piTime.elapsed () < 3000 ) { // wait for more //qDebug("waitformore "); QTimer::singleShot( 100, this , SLOT (readBackFileFromSocket( ) )); return; } QString fileName = mFileName; QFile file ( fileName ); if (!file.open( IO_WriteOnly ) ) { delete mSyncActionDialog; mSyncActionDialog = 0; qDebug("KSS:Error open read back file "); piFileString = ""; emit file_received( false ); blockRC = false; return ; } // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); QTextStream ts ( &file ); ts.setEncoding( QTextStream::Latin1 ); mSyncActionDialog->setCaption( i18n("Writing file to disk...") ); ts << piFileString; mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( discardClient())); file.close(); piFileString = ""; emit file_received( true ); delete mSyncActionDialog; mSyncActionDialog = 0; blockRC = false; } KCommandSocket::KCommandSocket ( QString password, Q_UINT16 port, QString host, QObject * parent, QWidget * cap, const char * name ): QObject( parent, name ) { mPassWord = password; mSocket = 0; mFirst = false; mFirstLine = true; mPort = port; mHost = host; tlw = cap; mRetVal = quiet; mTimerSocket = new QTimer ( this ); connect( mTimerSocket, SIGNAL ( timeout () ), this, SLOT ( deleteSocket() ) ); } void KCommandSocket::sendFileRequest() { if ( tlw ) tlw->setCaption( i18n("Connected! Sending request for remote file ...") ); mTimerSocket->start( 300000 ); QTextStream os( mSocket ); os.setEncoding( QTextStream::Latin1 ); QString curDt = " " +KGlobal::locale()->formatDateTime(QDateTime::currentDateTime().addSecs(-1),true, true,KLocale::ISODate ); os << "GET " << mPassWord << curDt <<"\r\n\r\n"; } void KCommandSocket::readFile( QString fn ) { if ( !mSocket ) { mSocket = new QSocket( this ); connect( mSocket, SIGNAL(readyRead()), this, SLOT(startReadFileFromSocket()) ); connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); connect( mSocket, SIGNAL(connected ()), this, SLOT(sendFileRequest() )); } mFileString = ""; mFileName = fn; mFirst = true; if ( tlw ) tlw->setCaption( i18n("Trying to connect to remote...") ); mTimerSocket->start( 20000 ); mSocket->connectToHost( mHost, mPort ); qDebug("KSS: Waiting for connection"); } void KCommandSocket::writeFile( QString fileName ) { if ( !mSocket ) { mSocket = new QSocket( this ); connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); connect( mSocket, SIGNAL(connected ()), this, SLOT(writeFileToSocket()) ); } mFileName = fileName ; mTimerSocket->start( 20000 ); mSocket->connectToHost( mHost, mPort ); } void KCommandSocket::writeFileToSocket() { mTimerSocket->stop(); QFile file2( mFileName ); if (!file2.open( IO_ReadOnly ) ) { mRetVal= errorW; mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( deleteSocket())); return ; } QTextStream ts2( &file2 ); ts2.setEncoding( QTextStream::Latin1 ); QTextStream os2( mSocket ); os2.setEncoding( QTextStream::Latin1 ); os2 << "PUT " << mPassWord << "\r\n\r\n";; while ( ! ts2.atEnd() ) { os2 << ts2.readLine() << "\r\n"; } os2 << "\r\n"; mRetVal= successW; file2.close(); mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( deleteSocket())); } void KCommandSocket::sendStop() { if ( !mSocket ) { mSocket = new QSocket( this ); connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); } mSocket->connectToHost( mHost, mPort ); QTextStream os2( mSocket ); os2.setEncoding( QTextStream::Latin1 ); os2 << "STOP\r\n\r\n"; mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( deleteSocket())); } void KCommandSocket::startReadFileFromSocket() { if ( ! mFirst ) return; mFirst = false; mTimerSocket->stop(); mFileString = ""; mTime.start(); mFirstLine = true; QTimer::singleShot( 1, this , SLOT (readFileFromSocket( ) )); } void KCommandSocket::readFileFromSocket() { //qDebug("readBackFileFromSocket() %d ", mTime.elapsed ()); while ( mSocket->canReadLine () ) { mTime.restart(); QString line = mSocket->readLine (); if ( mFirstLine ) { mFirstLine = false; + if ( line.left( 6 ) == "ERROR_" ) { if ( line.left( 8 ) == "ERROR_PW" ) { mRetVal = errorPW; deleteSocket(); return ; } + if ( line.left( 8 ) == "ERROR_CA" ) { + mRetVal = errorCA; + deleteSocket(); + return ; + } + if ( line.left( 8 ) == "ERROR_FI" ) { + mRetVal = errorFI; + deleteSocket(); + return ; + } + mRetVal = errorUN; + deleteSocket(); + return ; + } } mFileString += line; //qDebug("readline: %s ", line.latin1()); } if ( mTime.elapsed () < 3000 ) { // wait for more //qDebug("waitformore "); QTimer::singleShot( 100, this , SLOT (readFileFromSocket( ) )); return; } QString fileName = mFileName; QFile file ( fileName ); if (!file.open( IO_WriteOnly ) ) { mFileString = ""; mRetVal = errorR; qDebug("KSS:Error open temp sync file for writing: %s",fileName.latin1() ); deleteSocket(); return ; } // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); QTextStream ts ( &file ); ts.setEncoding( QTextStream::Latin1 ); ts << mFileString; file.close(); mFileString = ""; mRetVal = successR; mSocket->close(); // if state is not idle, deleteSocket(); is called via // connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); if ( mSocket->state() == QSocket::Idle ) deleteSocket(); } void KCommandSocket::deleteSocket() { //qDebug("KCommandSocket::deleteSocket() "); if ( mTimerSocket->isActive () ) { mTimerSocket->stop(); mRetVal = errorTO; qDebug("KCS::Connection to remote host timed out"); if ( mSocket ) { mSocket->close(); //if ( mSocket->state() == QSocket::Idle ) // deleteSocket(); delete mSocket; mSocket = 0; } KMessageBox::error( 0, i18n("Connection to remote\nhost timed out!\nDid you forgot to enable\nsyncing on remote host?")); emit commandFinished( this, mRetVal ); return; } //qDebug("KCommandSocket::deleteSocket() %d", mRetVal ); if ( mSocket) delete mSocket; mSocket = 0; emit commandFinished( this, mRetVal ); } |