author | zautrix <zautrix> | 2004-10-05 06:23:42 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-10-05 06:23:42 (UTC) |
commit | 0dd4cb1ff2bbd47788a5220363758226e5639edf (patch) (side-by-side diff) | |
tree | c062e2d2a1572810118a1c3f38057fa573716b29 /korganizer/mainwindow.cpp | |
parent | c22811d11414872fc0525350a8a1afdae61be346 (diff) | |
download | kdepimpi-0dd4cb1ff2bbd47788a5220363758226e5639edf.zip kdepimpi-0dd4cb1ff2bbd47788a5220363758226e5639edf.tar.gz kdepimpi-0dd4cb1ff2bbd47788a5220363758226e5639edf.tar.bz2 |
sync fixes
-rw-r--r-- | korganizer/mainwindow.cpp | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/korganizer/mainwindow.cpp b/korganizer/mainwindow.cpp index fe7e6d3..b7176a1 100644 --- a/korganizer/mainwindow.cpp +++ b/korganizer/mainwindow.cpp @@ -2337,299 +2337,304 @@ KServerSocket:: KServerSocket ( QString pw, Q_UINT16 port, int backlog, QObject void KServerSocket::newConnection ( int socket ) { // qDebug("KServerSocket:New connection %d ", socket); if ( mSocket ) { qDebug("KServerSocket::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()"); if ( mSocket ) { delete mSocket; mSocket = 0; } //emit endConnect(); } void KServerSocket::readClient() { if ( mSocket == 0 ) { qDebug("ERROR::KServerSocket::readClient(): mSocket == 0 "); return; } //qDebug("KServerSocket readClient()"); if ( mSocket->canReadLine() ) { QStringList tokens = QStringList::split( QRegExp("[ \r\n][ \r\n]*"), mSocket->readLine() ); if ( tokens[0] == "GET" ) { if ( tokens[1] == mPassWord ) //emit sendFile( mSocket ); send_file(); else { KMessageBox::information( 0, i18n("ERROR:\nGot send file request\nwith invalid password")); qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); } } if ( tokens[0] == "PUT" ) { if ( tokens[1] == mPassWord ) //emit getFile( mSocket ); get_file(); else { KMessageBox::information( 0, i18n("ERROR:\nGot 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::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...\nDo not use this application!\n\nIf syncing fails you can close this dialog."), mSyncActionDialog ); QVBoxLayout* lay = new QVBoxLayout( mSyncActionDialog ); lay->addWidget( label); lay->setMargin(7); lay->setSpacing(7); mSyncActionDialog->setFixedSize( 230, 120); mSyncActionDialog->show(); qDebug("KSS::saving ... "); emit saveFile(); qApp->processEvents(); QString fileName = mFileName; QFile file( fileName ); if (!file.open( IO_ReadOnly ) ) { delete mSyncActionDialog; mSyncActionDialog = 0; qDebug("KSS::error open file "); mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( discardClient())); return ; } mSyncActionDialog->setCaption( i18n("Sending file...") ); QTextStream ts( &file ); ts.setCodec( QTextCodec::codecForName("utf8") ); QTextStream os( mSocket ); os.setCodec( QTextCodec::codecForName("utf8") ); //os.setEncoding( QTextStream::UnicodeUTF8 ); while ( ! ts.atEnd() ) { os << ts.readLine() << "\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("error open cal file "); piFileString = ""; emit file_received( false ); return ; } // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); QTextStream ts ( &file ); ts.setCodec( QTextCodec::codecForName("utf8") ); mSyncActionDialog->setCaption( i18n("Writing file to disk...") ); ts << piFileString; mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( discardClient())); file.close(); delete mSyncActionDialog; mSyncActionDialog = 0; piFileString = ""; emit file_received( true ); } KCommandSocket::KCommandSocket ( QString password, Q_UINT16 port, QString host, QObject * parent, const char * name ): QObject( parent, name ) { mPassWord = password; mSocket = 0; mPort = port; mHost = host; mRetVal = false; mTimerSocket = new QTimer ( this ); connect( mTimerSocket, SIGNAL ( timeout () ), this, SLOT ( deleteSocket() ) ); } 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()) ); } mFileString = ""; mFileName = fn; mFirst = true; mSocket->connectToHost( mHost, mPort ); QTextStream os( mSocket ); os.setEncoding( QTextStream::UnicodeUTF8 ); os << "GET " << mPassWord << "\r\n"; mTimerSocket->start( 10000 ); } void KCommandSocket::writeFile( QString fileName ) { - QFile file2( fileName ); - if (!file2.open( IO_ReadOnly ) ) { - mRetVal= false; - deleteSocket(); - return ; - - } if ( !mSocket ) { mSocket = new QSocket( this ); connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); + connect( mSocket, SIGNAL(connected ()), this, SLOT(writeFileToSocket()) ); } + mFileName = fileName ; mSocket->connectToHost( mHost, mPort ); +} +void KCommandSocket::writeFileToSocket() +{ + QFile file2( mFileName ); + if (!file2.open( IO_ReadOnly ) ) { + mRetVal= false; + mSocket->close(); + if ( mSocket->state() == QSocket::Idle ) + QTimer::singleShot( 10, this , SLOT ( deleteSocket())); + return ; + } QTextStream ts2( &file2 ); ts2.setCodec( QTextCodec::codecForName("utf8") ); QTextStream os2( mSocket ); os2.setCodec( QTextCodec::codecForName("utf8") ); os2 << "PUT " << mPassWord << "\r\n";; while ( ! ts2.atEnd() ) { os2 << ts2.readLine() << "\n"; } mRetVal= true; + file2.close(); mSocket->close(); if ( mSocket->state() == QSocket::Idle ) QTimer::singleShot( 10, this , SLOT ( deleteSocket())); - file2.close(); } - void KCommandSocket::sendStop() { if ( !mSocket ) { mSocket = new QSocket( this ); connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); } mSocket->connectToHost( mHost, mPort ); QTextStream os2( mSocket ); os2.setCodec( QTextCodec::codecForName("utf8") ); os2 << "STOP\r\n"; mRetVal= true; 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(); QTimer::singleShot( 1, this , SLOT (readFileFromSocket( ) )); } void KCommandSocket::readFileFromSocket() { //qDebug("readBackFileFromSocket() %d ", mTime.elapsed ()); while ( mSocket->canReadLine () ) { mTime.restart(); QString line = mSocket->readLine (); 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 = false; qDebug("Error open temp calender file for writing: %s",fileName.latin1() ); deleteSocket(); return ; } // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); QTextStream ts ( &file ); ts.setCodec( QTextCodec::codecForName("utf8") ); ts << mFileString; file.close(); mFileString = ""; mRetVal = true; 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() { if ( mTimerSocket->isActive () ) { mTimerSocket->stop(); KMessageBox::information( 0, i18n("ERROR:\nConnection to remote host timed out!\nDid you forgot to enable\nsyncing on remote host? ")); mRetVal = false; } //qDebug("KCommandSocket::deleteSocket() %d", mRetVal ); if ( mSocket) delete mSocket; mSocket = 0; emit commandFinished( this, mRetVal ); } |