-rw-r--r-- | korganizer/mainwindow.cpp | 181 | ||||
-rw-r--r-- | korganizer/mainwindow.h | 14 |
2 files changed, 124 insertions, 71 deletions
diff --git a/korganizer/mainwindow.cpp b/korganizer/mainwindow.cpp index 7bf33d3..8e2f956 100644 --- a/korganizer/mainwindow.cpp +++ b/korganizer/mainwindow.cpp @@ -72,3 +72,5 @@ MainWindow::MainWindow( QWidget *parent, const char *name, QString msg) : { - + mPassWordPiSync = "abc"; + mTimerCommandSocket = new QTimer ( this ); + connect( mTimerCommandSocket, SIGNAL ( timeout () ), this, SLOT ( deleteCommandSocket() ) ); #ifdef DESKTOP_VERSION @@ -1891,2 +1893,3 @@ void MainWindow::enableQuick() { + QString passWordPiSync = "bhdrvmk"; QString retfile = ""; @@ -1901,4 +1904,10 @@ void MainWindow::enableQuick() lay.addWidget( &lab); + + QLineEdit lepw ( &dia ); + lepw.setText( "abc" ); + QLabel label2 ( i18n("Password to enable\naccess from remote:"), &dia ); + lay.addWidget( &label2); + lay.addWidget( &lepw); dia.setFixedSize( 230,80 ); - dia.setCaption( i18n("Enter port for Easy-Pi-Sync") ); + dia.setCaption( i18n("Enter port for Pi-Sync") ); QPushButton pb ( "OK", &dia); @@ -1913,2 +1922,3 @@ void MainWindow::enableQuick() dia.hide(); + passWordPiSync = lepw.text(); qApp->processEvents(); @@ -1918,3 +1928,3 @@ void MainWindow::enableQuick() if ( ! ok ) { - qDebug("no valid port "); + KMessageBox::information( this, i18n("No valid port")); return; @@ -1922,3 +1932,3 @@ void MainWindow::enableQuick() qDebug("port %d ", port); - mServerSocket = new KServerSocket ( port ,1 ); + mServerSocket = new KServerSocket ( passWordPiSync, port ,1 ); qDebug("connected "); @@ -1948,5 +1958,5 @@ void MainWindow::sendFile(QSocket* socket) mSyncActionDialog->show(); - qApp->processEvents(); qDebug("saving ... "); save(); + qApp->processEvents(); QString fileName = defaultFileName(); @@ -1980,6 +1990,30 @@ void MainWindow::getFile(QSocket* socket) + piTime.start(); + piSocket = socket; + piFileString = ""; + QTimer::singleShot( 1, this , SLOT (readBackFileFromSocket( ) )); + + +} +void MainWindow::readBackFileFromSocket() +{ + qDebug("readBackFileFromSocket() %d ", piTime.elapsed ()); + while ( piSocket->canReadLine () ) { + piTime.restart(); + QString line = piSocket->readLine (); + piFileString += line; + qDebug("readline: %s ", line.latin1()); + 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 = defaultFileName(); - QFile file( fileName ); + QFile file ( fileName ); if (!file.open( IO_WriteOnly ) ) { - setCaption( i18n("Error open file") ); + setCaption( i18n("Error open file for writing!") ); delete mSyncActionDialog; @@ -1987,2 +2021,3 @@ void MainWindow::getFile(QSocket* socket) qDebug("error open cal file "); + piFileString = ""; return ; @@ -1992,49 +2027,9 @@ void MainWindow::getFile(QSocket* socket) mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); - QTextStream ts( &file ); + QTextStream ts ( &file ); ts.setCodec( QTextCodec::codecForName("utf8") ); - bool first = true; - while ( socket->canReadLine () || first ) { - first = false; - while ( socket->canReadLine () ) { - qDebug("avail %d ", socket->bytesAvailable () ); - ts << socket->readLine (); - } - QTime ti; - ti.start(); - while ( ti.elapsed () < 5000 && !socket->canReadLine () ) { - qDebug("waiting1a %d %d ",ti.elapsed (), socket->bytesAvailable () ); - //qApp->processEvents(); - qDebug("waiting1b %d ",ti.elapsed () ); - if ( !socket->canReadLine () ) { - qDebug("waiting1c %d ",ti.elapsed () ); -#ifndef _WIN32_ - usleep( 100000); -#endif - } - //socket->waitForMore ( 100 ); - } - ts << socket->readLine (); -#if 0 -#ifdef DESKTOP_VERSION - socket->waitForMore ( 5000 ); -#else - // socket->waitForMore ( 5000 ); - // seems to be broken in qt2 - bool stop = false; - QTime ti; - ti.start(); - while ( ti.elapsed < 5000 && !stop ) { - qApp->processEvents(); - if ( socket->canReadLine () ) - stop = true ; - else { - usleep( 100000 ); - - } - } -#endif -#endif - } + qDebug("finish "); + setCaption( i18n("Writing file to disk...") ); + ts << piFileString; setCaption( i18n("File received - reloading calendar...") ); - socket->close(); + piSocket->close(); file.close(); @@ -2042,8 +2037,9 @@ void MainWindow::getFile(QSocket* socket) mView->openCalendar( defaultFileName() ); - setCaption( i18n("Easy-Pi-Sync successful!") ); + setCaption( i18n("Pi-Sync successful!") ); delete mSyncActionDialog; mSyncActionDialog = 0; + piFileString = ""; - -} + +} void MainWindow::endConnect() @@ -2056,2 +2052,3 @@ void MainWindow::performQuick() { + setCaption( i18n("Please input connection settings") ); @@ -2072,4 +2069,11 @@ void MainWindow::performQuick() lay.addWidget( &lab2); + + QLineEdit lepw ( &dia ); + lepw.setText( mPassWordPiSync ); + QLabel label3 ( i18n("Password to enable\naccess to remote:"), &dia ); + lay.addWidget( &label3); + lay.addWidget( &lepw); + dia.setFixedSize( 230,200 ); - dia.setCaption( i18n("Enter port for Easy-Pi-Sync ") ); + dia.setCaption( i18n("Enter port for Pi-Sync ") ); QPushButton pb ( "OK", &dia); @@ -2083,2 +2087,3 @@ void MainWindow::performQuick() } + mPassWordPiSync = lepw.text(); dia.hide(); @@ -2105,2 +2110,3 @@ void MainWindow::performQuickQuick() connect( mCommandSocket, SIGNAL(readyRead()), this, SLOT(readFileFromSocket()) ); + connect( mCommandSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteCommandSocket()) ); } @@ -2110,10 +2116,33 @@ void MainWindow::performQuickQuick() os.setEncoding( QTextStream::UnicodeUTF8 ); - os << "GET\r\n"; + os << "GET " << mPassWordPiSync << "\r\n"; setCaption( i18n("Sending request for remote file ...") ); + mTimerCommandSocket->start( 15000 ); } +void MainWindow::deleteCommandSocket() +{ + if ( !mCommandSocket) + return; + if ( mTimerCommandSocket->isActive () ) { + KMessageBox::information( 0, i18n("ERROR:\nConnection to remote host timed out ")); + mTimerCommandSocket->stop(); + } + qDebug("MainWindow::deletemCommandSocket() "); + delete mCommandSocket; + mCommandSocket = 0; +} +void MainWindow::deleteCommandSocketFinish() +{ + if ( ! mCommandSocketFinish) + return; + //KMessageBox::information( 0, i18n("MainWindow::deleteCommandSocketFinish() ")); + qDebug("MainWindow::deletemCommandSocketFinish() "); + delete mCommandSocketFinish; + mCommandSocketFinish = 0; +} void MainWindow::readFileFromSocket() { + mTimerCommandSocket->stop(); setCaption( i18n("Receiving remote file ...") ); - qDebug("MainWindow::readFileFromSocket() "); + qDebug("MainWindow::readFileFromSocket() "); QString fileName; @@ -2154,2 +2183,4 @@ void MainWindow::readFileFromSocket() mCommandSocket->close(); + if ( mCommandSocket->state() == QSocket::Idle ) + deleteCommandSocket(); // pending: deleting after signal SIGNAL(delayedCloseFinished()) @@ -2173,3 +2204,3 @@ void MainWindow::readFileFromSocket() KOPrefs::instance()->mShowSyncSummary = temp->getShowSummaryAfterSync(); - + delete temp; setCaption( i18n("Remote file saved to temp file.") ); @@ -2182,3 +2213,4 @@ void MainWindow::readFileFromSocket() if ( !mCommandSocketFinish ) { - mCommandSocketFinish = new QSocket( this ); + mCommandSocketFinish = new QSocket( this ); + connect( mCommandSocketFinish, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteCommandSocketFinish()) ); } @@ -2202,3 +2234,3 @@ void MainWindow::readFileFromSocket() if ( KOPrefs::instance()->mWriteBackFile ) { - os2 << "PUT\r\n"; + os2 << "PUT " << mPassWordPiSync << "\r\n";; while ( ! ts2.atEnd() ) { @@ -2210,8 +2242,7 @@ void MainWindow::readFileFromSocket() mCommandSocketFinish->close(); + if ( mCommandSocketFinish->state() == QSocket::Idle ) + deleteCommandSocketFinish(); file.close(); - // pending: deleting after signal SIGNAL(delayedCloseFinished()) - //delete ( mCommandSocket); - //mCommandSocket = 0; qDebug("Syncing succesful! "); - setCaption( i18n("Easy-Pi-Sync succesful!") ); + setCaption( i18n("Pi-Sync succesful!") ); @@ -2498,6 +2529,6 @@ void MainWindow::printCal() -KServerSocket:: KServerSocket ( Q_UINT16 port, int backlog, QObject * parent, const char * name ) : QServerSocket( port, backlog, parent, name ) +KServerSocket:: KServerSocket ( QString pw, Q_UINT16 port, int backlog, QObject * parent, const char * name ) : QServerSocket( port, backlog, parent, name ) { - - mSocket = 0; + mPassWord = pw; + mSocket = 0; }; @@ -2539,6 +2570,16 @@ void KServerSocket::readClient() if ( tokens[0] == "GET" ) { - emit sendFile( mSocket ); + if ( tokens[1] == mPassWord ) + emit sendFile( mSocket ); + 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" ) { - emit getFile( mSocket ); + if ( tokens[1] == mPassWord ) + emit getFile( mSocket ); + else { + KMessageBox::information( 0, i18n("ERROR:\nGot receive file request\nwith invalid password")); + qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); + } } diff --git a/korganizer/mainwindow.h b/korganizer/mainwindow.h index cc656a3..d217578 100644 --- a/korganizer/mainwindow.h +++ b/korganizer/mainwindow.h @@ -6,2 +6,5 @@ #include <qdict.h> +#include <qfile.h> +#include <qsocket.h> +#include <qtextstream.h> #include <qregexp.h> @@ -29,3 +32,3 @@ class KServerSocket : public QServerSocket public: - KServerSocket ( Q_UINT16 port, int backlog = 0, QObject * parent=0, const char * name=0 ); + KServerSocket ( QString password, Q_UINT16 port, int backlog = 0, QObject * parent=0, const char * name=0 ); @@ -42,2 +45,3 @@ private slots: QSocket* mSocket; + QString mPassWord; }; @@ -116,2 +120,7 @@ class MainWindow : public QMainWindow private slots: + QSocket* piSocket; + QString piFileString; + QTime piTime; + void deleteCommandSocket(); + void deleteCommandSocketFinish(); void fillSyncMenu(); @@ -120,4 +129,7 @@ class MainWindow : public QMainWindow void readFileFromSocket(); + void readBackFileFromSocket(); void endConnect(); private: + QTimer* mTimerCommandSocket; + QString mPassWordPiSync; QSocket* mCommandSocket; |