-rw-r--r-- | korganizer/mainwindow.cpp | 317 |
1 files changed, 314 insertions, 3 deletions
diff --git a/korganizer/mainwindow.cpp b/korganizer/mainwindow.cpp index 6315c64..f2e1bf8 100644 --- a/korganizer/mainwindow.cpp +++ b/korganizer/mainwindow.cpp @@ -6,6 +6,7 @@ #include <qwhatsthis.h> #include <qmessagebox.h> #include <qlineedit.h> +#include <qtextcodec.h> #include <qfile.h> #include <qdir.h> #include <qapp.h> @@ -71,7 +72,9 @@ MainWindow::MainWindow( QWidget *parent, const char *name, QString msg) : #ifdef DESKTOP_VERSION setFont( QFont("Arial"), 14 ); #endif - + mCommandSocket = 0; + mSyncActionDialog = 0; + mServerSocket = 0; mClosed = false; //QString confFile = KStandardDirs::appDir() + "config/korganizerrc"; QString confFile = locateLocal("config","korganizerrc"); @@ -213,7 +216,7 @@ MainWindow::~MainWindow() { //qDebug("MainWindow::~MainWindow() "); //save toolbar location - + delete mServerSocket; delete mCalendar; delete KOPrefs::instance(); delete KIncidenceFormatter::instance(); @@ -862,6 +865,15 @@ void MainWindow::fillSyncMenu() syncMenu->clear(); syncMenu->insertItem( i18n("Configure..."), 0 ); syncMenu->insertSeparator(); + if ( mServerSocket == 0 ) { + syncMenu->insertItem( i18n("Enable Easy-Pi-Sync"), 2 ); + } else { + syncMenu->insertItem( i18n("Disable Easy-Pi-Sync"), 3 ); + } + syncMenu->insertSeparator(); + syncMenu->insertItem( i18n("New Easy-Pi-Sync!"), 4 ); + syncMenu->insertItem( i18n("Quick Easy-Pi-Sync!"), 5 ); + syncMenu->insertSeparator(); syncMenu->insertItem( i18n("Multiple sync"), 1 ); syncMenu->insertSeparator(); KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); @@ -996,7 +1008,7 @@ void MainWindow::multiSync( bool askforPrefs ) } void MainWindow::slotSyncMenu( int action ) { - //qDebug("syncaction %d ", action); + qDebug("syncaction %d ", action); if ( action == 0 ) { // seems to be a Qt2 event handling bug @@ -1012,6 +1024,25 @@ void MainWindow::slotSyncMenu( int action ) multiSync( true ); return; } + if ( action == 2 ) { + enableQuick(); + QTimer::singleShot ( 1, this, SLOT ( fillSyncMenu() ) ); + return; + } + if ( action == 3 ) { + delete mServerSocket; + mServerSocket = 0; + QTimer::singleShot ( 1, this, SLOT ( fillSyncMenu() ) ); + return; + } + if ( action == 4 ) { + performQuick(); + return; + } + if ( action == 5 ) { + performQuickQuick(); + return; + } if (mBlockSaveFlag) return; @@ -1860,6 +1891,286 @@ QString MainWindow::getPassword( ) } +void MainWindow::enableQuick() +{ + QString retfile = ""; + QDialog dia ( this, "input-dialog", true ); + QLineEdit lab ( &dia ); + QVBoxLayout lay( &dia ); + lab.setText( KOPrefs::instance()->mPassiveSyncPort ); + lay.setMargin(7); + lay.setSpacing(7); + QLabel label ( i18n("Port number (Default: 9197)"), &dia ); + lay.addWidget( &label); + lay.addWidget( &lab); + dia.setFixedSize( 230,80 ); + dia.setCaption( i18n("Enter port for Easy-Pi-Sync") ); + 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(); + else + return; + dia.hide(); + qApp->processEvents(); + KOPrefs::instance()->mPassiveSyncPort = retfile; + bool ok; + Q_UINT16 port = retfile.toUInt(&ok); + if ( ! ok ) { + qDebug("no valid port "); + return; + } + qDebug("port %d ", port); + mServerSocket = new KServerSocket ( port ,1 ); + qDebug("connected "); + if ( !mServerSocket->ok() ) { + qWarning("Failed to bind to port %d", port); + delete mServerSocket; + mServerSocket = 0; + return; + } + connect( mServerSocket, SIGNAL ( sendFile(QSocket*) ), this, SLOT ( sendFile(QSocket*) ) ); + connect( mServerSocket, SIGNAL ( getFile(QSocket*) ), this, SLOT ( getFile(QSocket*) ) ); +} +void MainWindow::sendFile(QSocket* socket) +{ + setCaption( i18n("Received request for file") ); + qDebug("MainWindow::sendFile(QSocket* s) "); + mSyncActionDialog = new QDialog ( this, "input-dialog", true ); + mSyncActionDialog->setCaption(i18n("KO/Pi - WARNING")); + QLabel* label = new QLabel( i18n("Synchronizing...\nDo not use\nthis application!\n"), mSyncActionDialog ); + QVBoxLayout* lay = new QVBoxLayout( mSyncActionDialog ); + lay->addWidget( label); + lay->setMargin(7); + lay->setSpacing(7); + mSyncActionDialog->setFixedSize( 200,100 ); + mSyncActionDialog->show(); + qApp->processEvents(); + qDebug("saving ... "); + save(); + QString fileName = defaultFileName(); + QFile file( fileName ); + if (!file.open( IO_ReadOnly ) ) { + setCaption( i18n("Error open file") ); + delete mSyncActionDialog; + mSyncActionDialog = 0; + qDebug("error open cal file "); + return ; + + } + setCaption( i18n("Sending file...") ); + QTextStream ts( &file ); + ts.setCodec( QTextCodec::codecForName("utf8") ); + QTextStream os( socket ); + os.setCodec( QTextCodec::codecForName("utf8") ); + //os.setEncoding( QTextStream::UnicodeUTF8 ); + while ( ! ts.atEnd() ) { + os << ts.readLine() << "\n"; + } + //os << ts.read(); + socket->close(); + file.close(); + setCaption( i18n("File sent. Waiting to get back synced file") ); + qDebug("file sent "); +} +void MainWindow::getFile(QSocket* socket) +{ + setCaption( i18n("Receiving synced file...") ); + qDebug("MainWindow::sendFile(QSocket* s) "); + + QString fileName = defaultFileName(); + QFile file( fileName ); + if (!file.open( IO_WriteOnly ) ) { + setCaption( i18n("Error open file") ); + delete mSyncActionDialog; + mSyncActionDialog = 0; + qDebug("error open cal file "); + return ; + + } + + mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); + QTextStream ts( &file ); + ts.setCodec( QTextCodec::codecForName("utf8") ); + bool first = true; + while ( socket->canReadLine () || first ) { + first = false; + while ( socket->canReadLine () ) { + ts << socket->readLine (); + } + socket->waitForMore ( 5000 ); + } + setCaption( i18n("File received - reloading calendar...") ); + file.close(); + socket->close(); + mView->watchSavedFile(); + mView->openCalendar( defaultFileName() ); + setCaption( i18n("Easy-Pi-Sync successful!") ); + delete mSyncActionDialog; + mSyncActionDialog = 0; + + +} +void MainWindow::endConnect() +{ + setCaption( i18n("No file received - syncing successful") ); + delete mSyncActionDialog; + mSyncActionDialog = 0; +} +void MainWindow::performQuick() +{ + setCaption( i18n("Please input connection settings") ); + QString retfile = ""; + QDialog dia ( this, "input-dialog", true ); + QLineEdit lab ( &dia ); + QVBoxLayout lay( &dia ); + QLabel label ( i18n("IP address\n(Example: 192.168.0.40)"), &dia ); + lay.addWidget( &label); + lab.setText( KOPrefs::instance()->mActiveSyncIP ); + lay.setMargin(7); + lay.setSpacing(7); + lay.addWidget( &lab); + QLabel label2 ( i18n("Port number (Default: 9197)"), &dia ); + lay.addWidget( &label2); + QLineEdit lab2 ( &dia ); + lab2.setText( KOPrefs::instance()->mActiveSyncPort ); + lay.addWidget( &lab2); + dia.setFixedSize( 230,200 ); + dia.setCaption( i18n("Enter port for Easy-Pi-Sync ") ); + QPushButton pb ( "OK", &dia); + lay.addWidget( &pb ); + connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); + dia.show(); + int res = dia.exec(); + if ( !res ) { + setCaption( i18n("Syncing cancelled!") ); + return; + } + dia.hide(); + KOPrefs::instance()->mActiveSyncPort = lab2.text(); + KOPrefs::instance()->mActiveSyncIP = lab.text(); + qApp->processEvents(); + performQuickQuick(); +} + +void MainWindow::performQuickQuick() +{ + // setCaption( i18n("") ); + + bool ok; + Q_UINT16 port = KOPrefs::instance()->mActiveSyncPort.toUInt(&ok); + if ( ! ok ) { + setCaption( i18n("Sorry, no valid port.Syncing cancelled.") ); + return; + } + if ( mCommandSocket ) + delete mCommandSocket; + mCommandSocket = new QSocket( this ); + connect( mCommandSocket, SIGNAL(readyRead()), this, SLOT(readFileFromSocket()) ); + QString host = KOPrefs::instance()->mActiveSyncIP; + mCommandSocket->connectToHost( host, port ); + QTextStream os( mCommandSocket ); + os.setEncoding( QTextStream::UnicodeUTF8 ); + os << "GET\r\n"; + setCaption( i18n("Sending request for remote file ...") ); + +} +void MainWindow::readFileFromSocket() +{ + setCaption( i18n("Receiving remote file ...") ); + qDebug("MainWindow::readFileFromSocket() "); +QString fileName; +#ifdef _WIN32_ + fileName = defaultFileName() +"sync"; +#else + fileName = "/tmp/kopitempfile.ics"; +#endif + QFile file( fileName ); + if (!file.open( IO_WriteOnly ) ) { + setCaption( i18n("Error: Cannot open temp file for write.") ); + qDebug("Error open calender file for writing: %s",fileName.latin1() ); + return ; + } + + //QTextStream os2( mCommandSocket ); + //os2.setEncoding( QTextStream::UnicodeUTF8 ); + + QTextStream ts( &file ); + ts.setCodec( QTextCodec::codecForName("utf8") ); + bool first = true; + while ( mCommandSocket->canReadLine () || first) { + first = false; + while ( mCommandSocket->canReadLine () ) { + ts << mCommandSocket->readLine (); + } + mCommandSocket->waitForMore ( 5000 ); + } + file.close(); + mCommandSocket->close(); + delete mCommandSocket; + setCaption( i18n("Remote file saved to temp file.") ); + mCommandSocket = 0; + mCurrentSyncProfile = 2 ; // last file + mView->setSyncDevice(KOPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile] ); + mView->setSyncName( KOPrefs::instance()->mLocalMachineName ); + KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); + KSyncProfile* temp = new KSyncProfile (); + temp->setName(KOPrefs::instance()->mSyncProfileNames[mCurrentSyncProfile]); + temp->readConfig(&config); + KOPrefs::instance()->mAskForPreferences = temp->getAskForPreferences(); + KOPrefs::instance()->mSyncAlgoPrefs = temp->getSyncPrefs(); + KOPrefs::instance()->mWriteBackFile = temp->getWriteBackFile(); + KOPrefs::instance()->mWriteBackExistingOnly = temp->getWriteBackExisting(); + KOPrefs::instance()->mWriteBackInFuture = 0; + if ( temp->getWriteBackFuture() ) + KOPrefs::instance()->mWriteBackInFuture = temp->getWriteBackFutureWeeks( ); + KOPrefs::instance()->mShowSyncSummary = temp->getShowSummaryAfterSync(); + + setCaption( i18n("Remote file saved to temp file.") ); + if ( ! syncWithFile( fileName , true ) ) { + setCaption( i18n("Syncing failed.") ); + qDebug("Syncing failed "); + return; + } + + mCommandSocket = new QSocket( this ); + QString host = KOPrefs::instance()->mActiveSyncIP; + mCommandSocket->connectToHost( KOPrefs::instance()->mActiveSyncIP, KOPrefs::instance()->mActiveSyncPort.toUInt() ); + + QFile file2( fileName ); + if (!file2.open( IO_ReadOnly ) ) { + setCaption( i18n("Error: Cannot open temp file for read.") ); + qDebug("error open cal file "); + return ; + + } + setCaption( i18n("Sending back synced file...") ); + QTextStream ts2( &file2 ); + ts2.setCodec( QTextCodec::codecForName("utf8") ); + QTextStream os2( mCommandSocket ); + os2.setCodec( QTextCodec::codecForName("utf8") ); + //os.setEncoding( QTextStream::UnicodeUTF8 ); + if ( KOPrefs::instance()->mWriteBackFile ) { + os2 << "PUT\r\n"; + while ( ! ts2.atEnd() ) { + os2 << ts2.readLine() << "\n"; + } + } else { + os2 << "STOP\r\n"; + } + mCommandSocket->close(); + file.close(); + delete mCommandSocket; + mCommandSocket = 0; + qDebug("Syncing succesful! "); + setCaption( i18n("Easy-Pi-Sync succesful!") ); + + +} + void MainWindow::syncLocalFile() { |