author | zautrix <zautrix> | 2004-10-07 07:28:54 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-10-07 07:28:54 (UTC) |
commit | 77280f6a176a15b60004f312e6cacdfbbd3909c1 (patch) (unidiff) | |
tree | e074d4abedc2f4cc575e5655575e038b9d460106 | |
parent | f1eb5b74c962909851607c4b4cb05ee18a347d37 (diff) | |
download | kdepimpi-77280f6a176a15b60004f312e6cacdfbbd3909c1.zip kdepimpi-77280f6a176a15b60004f312e6cacdfbbd3909c1.tar.gz kdepimpi-77280f6a176a15b60004f312e6cacdfbbd3909c1.tar.bz2 |
sync changes
-rw-r--r-- | kaddressbook/kabcore.cpp | 29 | ||||
-rw-r--r-- | kaddressbook/kabcore.h | 7 | ||||
-rw-r--r-- | korganizer/koprefs.cpp | 2 | ||||
-rw-r--r-- | korganizer/koprefs.h | 3 | ||||
-rw-r--r-- | korganizer/mainwindow.cpp | 321 | ||||
-rw-r--r-- | korganizer/mainwindow.h | 70 | ||||
-rw-r--r-- | libkdepim/kpimprefs.cpp | 3 | ||||
-rw-r--r-- | libkdepim/kpimprefs.h | 3 | ||||
-rw-r--r-- | libkdepim/ksyncmanager.cpp | 523 | ||||
-rw-r--r-- | libkdepim/ksyncmanager.h | 99 |
10 files changed, 619 insertions, 441 deletions
diff --git a/kaddressbook/kabcore.cpp b/kaddressbook/kabcore.cpp index d9eb391..83fede4 100644 --- a/kaddressbook/kabcore.cpp +++ b/kaddressbook/kabcore.cpp | |||
@@ -1685,2 +1685,6 @@ void KABCore::initGUI() | |||
1685 | 1685 | ||
1686 | connect(syncManager , SIGNAL( save() ), this, SLOT( save() ) ); | ||
1687 | connect(syncManager , SIGNAL( getFile( bool )), this, SLOT(getFile( bool ) ) ); | ||
1688 | syncManager->setDefaultFileName(locateLocal( "apps","kabc/std.vcf") ); | ||
1689 | //connect(syncManager , SIGNAL( ), this, SLOT( ) ); | ||
1686 | 1690 | ||
@@ -2839,3 +2843,2 @@ bool KABCore::syncExternal(KSyncManager* manager, QString resource) | |||
2839 | abLocal.preExternSync( mAddressBook ,mCurrentSyncDevice ); | 2843 | abLocal.preExternSync( mAddressBook ,mCurrentSyncDevice ); |
2840 | qDebug("KABCore::syncExternal: why do we acces here KABPrefs and not somehow KSyncProfile? "); | ||
2841 | syncOK = synchronizeAddressbooks( mAddressBook, &abLocal, syncManager->mSyncAlgoPrefs ); | 2844 | syncOK = synchronizeAddressbooks( mAddressBook, &abLocal, syncManager->mSyncAlgoPrefs ); |
@@ -2855,19 +2858,12 @@ bool KABCore::syncExternal(KSyncManager* manager, QString resource) | |||
2855 | 2858 | ||
2856 | //called by the syncmanager to indicate that the work has to marked as dirty. | 2859 | void KABCore::getFile( bool success ) |
2857 | void KABCore::sync_setModified() | ||
2858 | { | 2860 | { |
2859 | setModified(); | 2861 | if ( ! success ) { |
2860 | } | 2862 | setCaption( i18n("Error receiving file. Nothing changed!") ); |
2861 | 2863 | return; | |
2862 | //called by the syncmanager to ask if the dirty flag is set. | ||
2863 | bool KABCore::sync_isModified() | ||
2864 | { | ||
2865 | return mModified; | ||
2866 | } | 2864 | } |
2867 | 2865 | //mView->watchSavedFile(); | |
2868 | 2866 | //mView->openCalendar( defaultFileName() ); | |
2869 | //called by the syncmanager to indicate that the work has to be saved. | 2867 | // pending: reload received file! |
2870 | void KABCore::sync_save() | 2868 | setCaption( i18n("Pi-Sync successful!") ); |
2871 | { | ||
2872 | save(); | ||
2873 | } | 2869 | } |
@@ -2875,2 +2871 @@ void KABCore::sync_save() | |||
2875 | |||
2876 | |||
diff --git a/kaddressbook/kabcore.h b/kaddressbook/kabcore.h index c9c0d38..355e828 100644 --- a/kaddressbook/kabcore.h +++ b/kaddressbook/kabcore.h | |||
@@ -344,2 +344,3 @@ class KABCore : public QWidget, public KSyncInterface | |||
344 | public slots: | 344 | public slots: |
345 | void getFile( bool success ); | ||
345 | void setDetailsVisible( bool visible ); | 346 | void setDetailsVisible( bool visible ); |
@@ -463,8 +464,2 @@ class KABCore : public QWidget, public KSyncInterface | |||
463 | 464 | ||
464 | //called by the syncmanager to indicate that the work has to marked as dirty. | ||
465 | virtual void sync_setModified(); | ||
466 | //called by the syncmanager to ask if the dirty flag is set. | ||
467 | virtual bool sync_isModified(); | ||
468 | //called by the syncmanager to indicate that the work has to be saved. | ||
469 | virtual void sync_save(); | ||
470 | 465 | ||
diff --git a/korganizer/koprefs.cpp b/korganizer/koprefs.cpp index 84e3d00..1210094 100644 --- a/korganizer/koprefs.cpp +++ b/korganizer/koprefs.cpp | |||
@@ -194,4 +194,2 @@ KOPrefs::KOPrefs() : | |||
194 | // addItemBool("ShowSyncSummary",&mShowSyncSummary,true); | 194 | // addItemBool("ShowSyncSummary",&mShowSyncSummary,true); |
195 | addItemString("PassiveSyncPort",&mPassiveSyncPort,"9197" ); | ||
196 | addItemString("PassiveSyncPw",&mPassiveSyncPw,"abc" ); | ||
197 | addItemString("ActiveSyncPort",&mActiveSyncPort,"9197" ); | 195 | addItemString("ActiveSyncPort",&mActiveSyncPort,"9197" ); |
diff --git a/korganizer/koprefs.h b/korganizer/koprefs.h index d9ac851..ff09e19 100644 --- a/korganizer/koprefs.h +++ b/korganizer/koprefs.h | |||
@@ -280,4 +280,3 @@ class KOPrefs : public KPimPrefs | |||
280 | 280 | ||
281 | QString mPassiveSyncPort; | 281 | |
282 | QString mPassiveSyncPw; | ||
283 | QString mActiveSyncPort; | 282 | QString mActiveSyncPort; |
diff --git a/korganizer/mainwindow.cpp b/korganizer/mainwindow.cpp index 9104347..e3324ee 100644 --- a/korganizer/mainwindow.cpp +++ b/korganizer/mainwindow.cpp | |||
@@ -132,3 +132,2 @@ MainWindow::MainWindow( QWidget *parent, const char *name, QString msg) : | |||
132 | #endif | 132 | #endif |
133 | mSyncActionDialog = 0; | ||
134 | mServerSocket = 0; | 133 | mServerSocket = 0; |
@@ -2349,321 +2348 @@ void MainWindow::printCal() | |||
2349 | |||
2350 | |||
2351 | |||
2352 | KServerSocket:: KServerSocket ( QString pw, Q_UINT16 port, int backlog, QObject * parent, const char * name ) : QServerSocket( port, backlog, parent, name ) | ||
2353 | { | ||
2354 | mPassWord = pw; | ||
2355 | mSocket = 0; | ||
2356 | mSyncActionDialog = 0; | ||
2357 | blockRC = false; | ||
2358 | }; | ||
2359 | |||
2360 | void KServerSocket::newConnection ( int socket ) | ||
2361 | { | ||
2362 | // qDebug("KServerSocket:New connection %d ", socket); | ||
2363 | if ( mSocket ) { | ||
2364 | qDebug("KServerSocket::newConnection Socket deleted! "); | ||
2365 | delete mSocket; | ||
2366 | mSocket = 0; | ||
2367 | } | ||
2368 | mSocket = new QSocket( this ); | ||
2369 | connect( mSocket , SIGNAL(readyRead()), this, SLOT(readClient()) ); | ||
2370 | connect( mSocket , SIGNAL(delayedCloseFinished()), this, SLOT(discardClient()) ); | ||
2371 | mSocket->setSocket( socket ); | ||
2372 | } | ||
2373 | |||
2374 | void KServerSocket::discardClient() | ||
2375 | { | ||
2376 | //qDebug(" KServerSocket::discardClient()"); | ||
2377 | if ( mSocket ) { | ||
2378 | delete mSocket; | ||
2379 | mSocket = 0; | ||
2380 | } | ||
2381 | //emit endConnect(); | ||
2382 | } | ||
2383 | void KServerSocket::readClient() | ||
2384 | { | ||
2385 | if ( blockRC ) | ||
2386 | return; | ||
2387 | if ( mSocket == 0 ) { | ||
2388 | qDebug("ERROR::KServerSocket::readClient(): mSocket == 0 "); | ||
2389 | return; | ||
2390 | } | ||
2391 | qDebug("KServerSocket readClient()"); | ||
2392 | if ( mSocket->canReadLine() ) { | ||
2393 | QString line = mSocket->readLine(); | ||
2394 | qDebug("KServerSocket readline: %s ", line.latin1()); | ||
2395 | QStringList tokens = QStringList::split( QRegExp("[ \r\n][ \r\n]*"), line ); | ||
2396 | if ( tokens[0] == "GET" ) { | ||
2397 | if ( tokens[1] == mPassWord ) | ||
2398 | //emit sendFile( mSocket ); | ||
2399 | send_file(); | ||
2400 | else { | ||
2401 | KMessageBox::information( 0, i18n("ERROR:\nGot send file request\nwith invalid password")); | ||
2402 | qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); | ||
2403 | } | ||
2404 | } | ||
2405 | if ( tokens[0] == "PUT" ) { | ||
2406 | if ( tokens[1] == mPassWord ) { | ||
2407 | //emit getFile( mSocket ); | ||
2408 | blockRC = true; | ||
2409 | get_file(); | ||
2410 | } | ||
2411 | else { | ||
2412 | KMessageBox::information( 0, i18n("ERROR:\nGot receive file request\nwith invalid password")); | ||
2413 | qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); | ||
2414 | } | ||
2415 | } | ||
2416 | if ( tokens[0] == "STOP" ) { | ||
2417 | //emit endConnect(); | ||
2418 | end_connect(); | ||
2419 | } | ||
2420 | } | ||
2421 | } | ||
2422 | void KServerSocket::end_connect() | ||
2423 | { | ||
2424 | delete mSyncActionDialog; | ||
2425 | mSyncActionDialog = 0; | ||
2426 | } | ||
2427 | void KServerSocket::send_file() | ||
2428 | { | ||
2429 | //qDebug("MainWindow::sendFile(QSocket* s) "); | ||
2430 | if ( mSyncActionDialog ) | ||
2431 | delete mSyncActionDialog; | ||
2432 | mSyncActionDialog = new QDialog ( 0, "input-dialog", true ); | ||
2433 | mSyncActionDialog->setCaption(i18n("Received sync request")); | ||
2434 | QLabel* label = new QLabel( i18n("Synchronizing from remote ...\n\nDo not use this application!\n\nIf syncing fails\nyou can close this dialog."), mSyncActionDialog ); | ||
2435 | QVBoxLayout* lay = new QVBoxLayout( mSyncActionDialog ); | ||
2436 | lay->addWidget( label); | ||
2437 | lay->setMargin(7); | ||
2438 | lay->setSpacing(7); | ||
2439 | mSyncActionDialog->setFixedSize( 230, 120); | ||
2440 | mSyncActionDialog->show(); | ||
2441 | qDebug("KSS::saving ... "); | ||
2442 | emit saveFile(); | ||
2443 | qApp->processEvents(); | ||
2444 | QString fileName = mFileName; | ||
2445 | QFile file( fileName ); | ||
2446 | if (!file.open( IO_ReadOnly ) ) { | ||
2447 | delete mSyncActionDialog; | ||
2448 | mSyncActionDialog = 0; | ||
2449 | qDebug("KSS::error open file "); | ||
2450 | mSocket->close(); | ||
2451 | if ( mSocket->state() == QSocket::Idle ) | ||
2452 | QTimer::singleShot( 10, this , SLOT ( discardClient())); | ||
2453 | return ; | ||
2454 | |||
2455 | } | ||
2456 | mSyncActionDialog->setCaption( i18n("Sending file...") ); | ||
2457 | QTextStream ts( &file ); | ||
2458 | ts.setCodec( QTextCodec::codecForName("utf8") ); | ||
2459 | QTextStream os( mSocket ); | ||
2460 | os.setCodec( QTextCodec::codecForName("utf8") ); | ||
2461 | //os.setEncoding( QTextStream::UnicodeUTF8 ); | ||
2462 | while ( ! ts.atEnd() ) { | ||
2463 | os << ts.readLine() << "\n"; | ||
2464 | } | ||
2465 | //os << ts.read(); | ||
2466 | file.close(); | ||
2467 | mSyncActionDialog->setCaption( i18n("Waiting for synced file...") ); | ||
2468 | mSocket->close(); | ||
2469 | if ( mSocket->state() == QSocket::Idle ) | ||
2470 | QTimer::singleShot( 10, this , SLOT ( discardClient())); | ||
2471 | } | ||
2472 | void KServerSocket::get_file() | ||
2473 | { | ||
2474 | mSyncActionDialog->setCaption( i18n("Receiving synced file...") ); | ||
2475 | |||
2476 | piTime.start(); | ||
2477 | piFileString = ""; | ||
2478 | QTimer::singleShot( 1, this , SLOT (readBackFileFromSocket( ) )); | ||
2479 | } | ||
2480 | |||
2481 | |||
2482 | void KServerSocket::readBackFileFromSocket() | ||
2483 | { | ||
2484 | //qDebug("readBackFileFromSocket() %d ", piTime.elapsed ()); | ||
2485 | while ( mSocket->canReadLine () ) { | ||
2486 | piTime.restart(); | ||
2487 | QString line = mSocket->readLine (); | ||
2488 | piFileString += line; | ||
2489 | //qDebug("readline: %s ", line.latin1()); | ||
2490 | mSyncActionDialog->setCaption( i18n("Received %1 bytes").arg( piFileString.length() ) ); | ||
2491 | |||
2492 | } | ||
2493 | if ( piTime.elapsed () < 3000 ) { | ||
2494 | // wait for more | ||
2495 | //qDebug("waitformore "); | ||
2496 | QTimer::singleShot( 100, this , SLOT (readBackFileFromSocket( ) )); | ||
2497 | return; | ||
2498 | } | ||
2499 | QString fileName = mFileName; | ||
2500 | QFile file ( fileName ); | ||
2501 | if (!file.open( IO_WriteOnly ) ) { | ||
2502 | delete mSyncActionDialog; | ||
2503 | mSyncActionDialog = 0; | ||
2504 | qDebug("error open cal file "); | ||
2505 | piFileString = ""; | ||
2506 | emit file_received( false ); | ||
2507 | blockRC = false; | ||
2508 | return ; | ||
2509 | |||
2510 | } | ||
2511 | |||
2512 | // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); | ||
2513 | QTextStream ts ( &file ); | ||
2514 | ts.setCodec( QTextCodec::codecForName("utf8") ); | ||
2515 | mSyncActionDialog->setCaption( i18n("Writing file to disk...") ); | ||
2516 | ts << piFileString; | ||
2517 | mSocket->close(); | ||
2518 | if ( mSocket->state() == QSocket::Idle ) | ||
2519 | QTimer::singleShot( 10, this , SLOT ( discardClient())); | ||
2520 | file.close(); | ||
2521 | delete mSyncActionDialog; | ||
2522 | mSyncActionDialog = 0; | ||
2523 | piFileString = ""; | ||
2524 | blockRC = false; | ||
2525 | emit file_received( true ); | ||
2526 | |||
2527 | } | ||
2528 | |||
2529 | KCommandSocket::KCommandSocket ( QString password, Q_UINT16 port, QString host, QObject * parent, const char * name ): QObject( parent, name ) | ||
2530 | { | ||
2531 | mPassWord = password; | ||
2532 | mSocket = 0; | ||
2533 | mPort = port; | ||
2534 | mHost = host; | ||
2535 | |||
2536 | mRetVal = quiet; | ||
2537 | mTimerSocket = new QTimer ( this ); | ||
2538 | connect( mTimerSocket, SIGNAL ( timeout () ), this, SLOT ( deleteSocket() ) ); | ||
2539 | } | ||
2540 | void KCommandSocket::readFile( QString fn ) | ||
2541 | { | ||
2542 | if ( !mSocket ) { | ||
2543 | mSocket = new QSocket( this ); | ||
2544 | connect( mSocket, SIGNAL(readyRead()), this, SLOT(startReadFileFromSocket()) ); | ||
2545 | connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
2546 | } | ||
2547 | mFileString = ""; | ||
2548 | mFileName = fn; | ||
2549 | mFirst = true; | ||
2550 | mSocket->connectToHost( mHost, mPort ); | ||
2551 | QTextStream os( mSocket ); | ||
2552 | os.setEncoding( QTextStream::UnicodeUTF8 ); | ||
2553 | os << "GET " << mPassWord << "\r\n"; | ||
2554 | mTimerSocket->start( 10000 ); | ||
2555 | } | ||
2556 | |||
2557 | void KCommandSocket::writeFile( QString fileName ) | ||
2558 | { | ||
2559 | if ( !mSocket ) { | ||
2560 | mSocket = new QSocket( this ); | ||
2561 | connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
2562 | connect( mSocket, SIGNAL(connected ()), this, SLOT(writeFileToSocket()) ); | ||
2563 | } | ||
2564 | mFileName = fileName ; | ||
2565 | mSocket->connectToHost( mHost, mPort ); | ||
2566 | } | ||
2567 | void KCommandSocket::writeFileToSocket() | ||
2568 | { | ||
2569 | QFile file2( mFileName ); | ||
2570 | if (!file2.open( IO_ReadOnly ) ) { | ||
2571 | mRetVal= errorW; | ||
2572 | mSocket->close(); | ||
2573 | if ( mSocket->state() == QSocket::Idle ) | ||
2574 | QTimer::singleShot( 10, this , SLOT ( deleteSocket())); | ||
2575 | return ; | ||
2576 | } | ||
2577 | QTextStream ts2( &file2 ); | ||
2578 | ts2.setCodec( QTextCodec::codecForName("utf8") ); | ||
2579 | QTextStream os2( mSocket ); | ||
2580 | os2.setCodec( QTextCodec::codecForName("utf8") ); | ||
2581 | os2 << "PUT " << mPassWord << "\r\n";; | ||
2582 | while ( ! ts2.atEnd() ) { | ||
2583 | os2 << ts2.readLine() << "\n"; | ||
2584 | } | ||
2585 | mRetVal= successW; | ||
2586 | file2.close(); | ||
2587 | mSocket->close(); | ||
2588 | if ( mSocket->state() == QSocket::Idle ) | ||
2589 | QTimer::singleShot( 10, this , SLOT ( deleteSocket())); | ||
2590 | } | ||
2591 | void KCommandSocket::sendStop() | ||
2592 | { | ||
2593 | if ( !mSocket ) { | ||
2594 | mSocket = new QSocket( this ); | ||
2595 | connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
2596 | } | ||
2597 | mSocket->connectToHost( mHost, mPort ); | ||
2598 | QTextStream os2( mSocket ); | ||
2599 | os2.setCodec( QTextCodec::codecForName("utf8") ); | ||
2600 | os2 << "STOP\r\n"; | ||
2601 | mSocket->close(); | ||
2602 | if ( mSocket->state() == QSocket::Idle ) | ||
2603 | QTimer::singleShot( 10, this , SLOT ( deleteSocket())); | ||
2604 | } | ||
2605 | |||
2606 | void KCommandSocket::startReadFileFromSocket() | ||
2607 | { | ||
2608 | if ( ! mFirst ) | ||
2609 | return; | ||
2610 | mFirst = false; | ||
2611 | mTimerSocket->stop(); | ||
2612 | mFileString = ""; | ||
2613 | mTime.start(); | ||
2614 | QTimer::singleShot( 1, this , SLOT (readFileFromSocket( ) )); | ||
2615 | |||
2616 | } | ||
2617 | void KCommandSocket::readFileFromSocket() | ||
2618 | { | ||
2619 | //qDebug("readBackFileFromSocket() %d ", mTime.elapsed ()); | ||
2620 | while ( mSocket->canReadLine () ) { | ||
2621 | mTime.restart(); | ||
2622 | QString line = mSocket->readLine (); | ||
2623 | mFileString += line; | ||
2624 | //qDebug("readline: %s ", line.latin1()); | ||
2625 | } | ||
2626 | if ( mTime.elapsed () < 3000 ) { | ||
2627 | // wait for more | ||
2628 | //qDebug("waitformore "); | ||
2629 | QTimer::singleShot( 100, this , SLOT (readFileFromSocket( ) )); | ||
2630 | return; | ||
2631 | } | ||
2632 | QString fileName = mFileName; | ||
2633 | QFile file ( fileName ); | ||
2634 | if (!file.open( IO_WriteOnly ) ) { | ||
2635 | mFileString = ""; | ||
2636 | mRetVal = errorR; | ||
2637 | qDebug("Error open temp calender file for writing: %s",fileName.latin1() ); | ||
2638 | deleteSocket(); | ||
2639 | return ; | ||
2640 | |||
2641 | } | ||
2642 | // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); | ||
2643 | QTextStream ts ( &file ); | ||
2644 | ts.setCodec( QTextCodec::codecForName("utf8") ); | ||
2645 | ts << mFileString; | ||
2646 | file.close(); | ||
2647 | mFileString = ""; | ||
2648 | mRetVal = successR; | ||
2649 | mSocket->close(); | ||
2650 | // if state is not idle, deleteSocket(); is called via | ||
2651 | // connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
2652 | if ( mSocket->state() == QSocket::Idle ) | ||
2653 | deleteSocket(); | ||
2654 | } | ||
2655 | |||
2656 | void KCommandSocket::deleteSocket() | ||
2657 | { | ||
2658 | if ( mTimerSocket->isActive () ) { | ||
2659 | mTimerSocket->stop(); | ||
2660 | KMessageBox::information( 0, i18n("ERROR:\nConnection to remote host timed out!\nDid you forgot to enable\nsyncing on remote host? ")); | ||
2661 | mRetVal = errorR; | ||
2662 | } | ||
2663 | //qDebug("KCommandSocket::deleteSocket() %d", mRetVal ); | ||
2664 | if ( mSocket) | ||
2665 | delete mSocket; | ||
2666 | mSocket = 0; | ||
2667 | emit commandFinished( this, mRetVal ); | ||
2668 | } | ||
2669 | |||
diff --git a/korganizer/mainwindow.h b/korganizer/mainwindow.h index 4f89e03..0da0be0 100644 --- a/korganizer/mainwindow.h +++ b/korganizer/mainwindow.h | |||
@@ -7,3 +7,2 @@ | |||
7 | #include <qfile.h> | 7 | #include <qfile.h> |
8 | #include <qsocket.h> | ||
9 | #include <qtextstream.h> | 8 | #include <qtextstream.h> |
@@ -13,2 +12,3 @@ | |||
13 | #include "simplealarmclient.h" | 12 | #include "simplealarmclient.h" |
13 | #include <ksyncmanager.h> | ||
14 | 14 | ||
@@ -23,70 +23,3 @@ class KSyncProfile; | |||
23 | class QPEToolBar; | 23 | class QPEToolBar; |
24 | #include <qserversocket.h> | ||
25 | #include <qsocket.h> | ||
26 | #include <qnetworkprotocol.h> | ||
27 | 24 | ||
28 | class KServerSocket : public QServerSocket | ||
29 | { | ||
30 | Q_OBJECT | ||
31 | |||
32 | public: | ||
33 | KServerSocket ( QString password, Q_UINT16 port, int backlog = 0, QObject * parent=0, const char * name=0 ); | ||
34 | |||
35 | void newConnection ( int socket ) ; | ||
36 | void setFileName( QString fn ) {mFileName = fn;}; | ||
37 | |||
38 | signals: | ||
39 | //void sendFile(QSocket*); | ||
40 | //void getFile(QSocket*); | ||
41 | void file_received( bool ); | ||
42 | //void file_sent(); | ||
43 | void saveFile(); | ||
44 | void endConnect(); | ||
45 | private slots: | ||
46 | void discardClient(); | ||
47 | void readClient(); | ||
48 | void readBackFileFromSocket(); | ||
49 | private : | ||
50 | bool blockRC; | ||
51 | void send_file(); | ||
52 | void get_file(); | ||
53 | void end_connect(); | ||
54 | QDialog* mSyncActionDialog; | ||
55 | QSocket* mSocket; | ||
56 | QString mPassWord; | ||
57 | QString mFileName; | ||
58 | QTime piTime; | ||
59 | QString piFileString; | ||
60 | }; | ||
61 | |||
62 | class KCommandSocket : public QObject | ||
63 | { | ||
64 | Q_OBJECT | ||
65 | public: | ||
66 | enum state { successR, errorR, successW, errorW, quiet }; | ||
67 | KCommandSocket ( QString password, Q_UINT16 port, QString host, QObject * parent=0, const char * name=0 ); | ||
68 | void readFile( QString ); | ||
69 | void writeFile( QString ); | ||
70 | void sendStop(); | ||
71 | |||
72 | |||
73 | signals: | ||
74 | void commandFinished( KCommandSocket*, int ); | ||
75 | private slots: | ||
76 | void startReadFileFromSocket(); | ||
77 | void readFileFromSocket(); | ||
78 | void deleteSocket(); | ||
79 | void writeFileToSocket(); | ||
80 | private : | ||
81 | QSocket* mSocket; | ||
82 | QString mPassWord; | ||
83 | Q_UINT16 mPort; | ||
84 | QString mHost; | ||
85 | QString mFileName; | ||
86 | QTimer* mTimerSocket; | ||
87 | int mRetVal; | ||
88 | QTime mTime; | ||
89 | QString mFileString; | ||
90 | bool mFirst; | ||
91 | }; | ||
92 | 25 | ||
@@ -211,3 +144,2 @@ class MainWindow : public QMainWindow | |||
211 | QPixmap loadPixmap( QString ); | 144 | QPixmap loadPixmap( QString ); |
212 | QDialog * mSyncActionDialog; | ||
213 | }; | 145 | }; |
diff --git a/libkdepim/kpimprefs.cpp b/libkdepim/kpimprefs.cpp index a9ea330..0a2f5f1 100644 --- a/libkdepim/kpimprefs.cpp +++ b/libkdepim/kpimprefs.cpp | |||
@@ -40,2 +40,5 @@ KPimPrefs::KPimPrefs( const QString &name ) : | |||
40 | { | 40 | { |
41 | config()->setGroup("General"); | ||
42 | addItemString("PassiveSyncPort",&mPassiveSyncPort,"9197" ); | ||
43 | addItemString("PassiveSyncPw",&mPassiveSyncPw,"abc" ); | ||
41 | } | 44 | } |
diff --git a/libkdepim/kpimprefs.h b/libkdepim/kpimprefs.h index 6f92919..fde8093 100644 --- a/libkdepim/kpimprefs.h +++ b/libkdepim/kpimprefs.h | |||
@@ -57,3 +57,4 @@ class KPimPrefs : public KPrefs | |||
57 | QStringList mCustomCategories; | 57 | QStringList mCustomCategories; |
58 | 58 | QString mPassiveSyncPort; | |
59 | QString mPassiveSyncPw; | ||
59 | 60 | ||
diff --git a/libkdepim/ksyncmanager.cpp b/libkdepim/ksyncmanager.cpp index c0cc840..5d48884 100644 --- a/libkdepim/ksyncmanager.cpp +++ b/libkdepim/ksyncmanager.cpp | |||
@@ -34,2 +34,3 @@ | |||
34 | #include "kpimprefs.h" | 34 | #include "kpimprefs.h" |
35 | #include <kmessagebox.h> | ||
35 | 36 | ||
@@ -47,2 +48,4 @@ | |||
47 | #include <qlayout.h> | 48 | #include <qlayout.h> |
49 | #include <qtextcodec.h> | ||
50 | #include <qlabel.h> | ||
48 | 51 | ||
@@ -54,4 +57,5 @@ | |||
54 | KSyncManager::KSyncManager(QWidget* parent, KSyncInterface* implementation, TargetApp ta, KPimPrefs* prefs, QPopupMenu* syncmenu) | 57 | KSyncManager::KSyncManager(QWidget* parent, KSyncInterface* implementation, TargetApp ta, KPimPrefs* prefs, QPopupMenu* syncmenu) |
55 | : QObject(), mParent(parent), mImplementation(implementation), mTargetApp(ta), mSyncMenu(syncmenu) | 58 | : QObject(), mParent(parent), mImplementation(implementation), mTargetApp(ta), mPrefs(prefs ),mSyncMenu(syncmenu) |
56 | { | 59 | { |
60 | mServerSocket = 0; | ||
57 | bar = new QProgressBar ( 1, 0 ); | 61 | bar = new QProgressBar ( 1, 0 ); |
@@ -74,3 +78,3 @@ KSyncManager::~KSyncManager() | |||
74 | 78 | ||
75 | 79 | //LR ok | |
76 | void KSyncManager::fillSyncMenu() | 80 | void KSyncManager::fillSyncMenu() |
@@ -82,2 +86,8 @@ void KSyncManager::fillSyncMenu() | |||
82 | mSyncMenu->insertSeparator(); | 86 | mSyncMenu->insertSeparator(); |
87 | if ( mServerSocket == 0 ) { | ||
88 | mSyncMenu->insertItem( i18n("Enable Pi-Sync"), 2 ); | ||
89 | } else { | ||
90 | mSyncMenu->insertItem( i18n("Disable Pi-Sync"), 3 ); | ||
91 | } | ||
92 | mSyncMenu->insertSeparator(); | ||
83 | mSyncMenu->insertItem( i18n("Multiple sync"), 1 ); | 93 | mSyncMenu->insertItem( i18n("Multiple sync"), 1 ); |
@@ -89,3 +99,3 @@ void KSyncManager::fillSyncMenu() | |||
89 | mLocalMachineName = config.readEntry("LocalMachineName","undefined"); | 99 | mLocalMachineName = config.readEntry("LocalMachineName","undefined"); |
90 | if ( prof.count() < 3 ) { | 100 | if ( prof.count() < 2 ) { |
91 | prof.clear(); | 101 | prof.clear(); |
@@ -93,3 +103,2 @@ void KSyncManager::fillSyncMenu() | |||
93 | prof << i18n("Local_file"); | 103 | prof << i18n("Local_file"); |
94 | prof << i18n("Last_file"); | ||
95 | KSyncProfile* temp = new KSyncProfile (); | 104 | KSyncProfile* temp = new KSyncProfile (); |
@@ -99,4 +108,2 @@ void KSyncManager::fillSyncMenu() | |||
99 | temp->writeConfig(&config); | 108 | temp->writeConfig(&config); |
100 | temp->setName( prof[2] ); | ||
101 | temp->writeConfig(&config); | ||
102 | config.setGroup("General"); | 109 | config.setGroup("General"); |
@@ -143,2 +150,13 @@ void KSyncManager::slotSyncMenu( int action ) | |||
143 | } | 150 | } |
151 | if ( action == 2 ) { | ||
152 | enableQuick(); | ||
153 | QTimer::singleShot ( 1, this, SLOT ( fillSyncMenu() ) ); | ||
154 | return; | ||
155 | } | ||
156 | if ( action == 3 ) { | ||
157 | delete mServerSocket; | ||
158 | mServerSocket = 0; | ||
159 | QTimer::singleShot ( 1, this, SLOT ( fillSyncMenu() ) ); | ||
160 | return; | ||
161 | } | ||
144 | 162 | ||
@@ -200,3 +218,8 @@ void KSyncManager::slotSyncMenu( int action ) | |||
200 | syncPhone(); | 218 | syncPhone(); |
201 | } else | 219 | } else if ( temp->getIsPiSync() ) { |
220 | mPassWordPiSync = temp->getRemotePw(); | ||
221 | mActiveSyncPort = temp->getRemotePort(); | ||
222 | mActiveSyncIP = temp->getRemoteIP(); | ||
223 | syncPi(); | ||
224 | } | ||
202 | syncRemote( temp ); | 225 | syncRemote( temp ); |
@@ -208,2 +231,51 @@ void KSyncManager::slotSyncMenu( int action ) | |||
208 | } | 231 | } |
232 | void KSyncManager::enableQuick() | ||
233 | { | ||
234 | QDialog dia ( 0, "input-dialog", true ); | ||
235 | QLineEdit lab ( &dia ); | ||
236 | QVBoxLayout lay( &dia ); | ||
237 | lab.setText( mPrefs->mPassiveSyncPort ); | ||
238 | lay.setMargin(7); | ||
239 | lay.setSpacing(7); | ||
240 | int po = 9197+mTargetApp; | ||
241 | QLabel label ( i18n("Port number (Default: %1)").arg(po), &dia ); | ||
242 | lay.addWidget( &label); | ||
243 | lay.addWidget( &lab); | ||
244 | |||
245 | QLineEdit lepw ( &dia ); | ||
246 | lepw.setText( mPrefs->mPassiveSyncPw ); | ||
247 | QLabel label2 ( i18n("Password to enable\naccess from remote:"), &dia ); | ||
248 | lay.addWidget( &label2); | ||
249 | lay.addWidget( &lepw); | ||
250 | dia.setFixedSize( 230,80 ); | ||
251 | dia.setCaption( i18n("Enter port for Pi-Sync") ); | ||
252 | QPushButton pb ( "OK", &dia); | ||
253 | lay.addWidget( &pb ); | ||
254 | connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); | ||
255 | dia.show(); | ||
256 | if ( ! dia.exec() ) | ||
257 | return; | ||
258 | dia.hide(); | ||
259 | qApp->processEvents(); | ||
260 | mPrefs->mPassiveSyncPw = lepw.text(); | ||
261 | mPrefs->mPassiveSyncPort = lab.text(); | ||
262 | bool ok; | ||
263 | Q_UINT16 port = mPrefs->mPassiveSyncPort.toUInt(&ok); | ||
264 | if ( ! ok ) { | ||
265 | KMessageBox::information( 0, i18n("No valid port")); | ||
266 | return; | ||
267 | } | ||
268 | //qDebug("port %d ", port); | ||
269 | mServerSocket = new KServerSocket ( mPrefs->mPassiveSyncPw, port ,1 ); | ||
270 | mServerSocket->setFileName( defaultFileName() ); | ||
271 | //qDebug("connected "); | ||
272 | if ( !mServerSocket->ok() ) { | ||
273 | KMessageBox::information( 0, i18n("Failed to bind or\nlisten to the port!")); | ||
274 | delete mServerSocket; | ||
275 | mServerSocket = 0; | ||
276 | return; | ||
277 | } | ||
278 | connect( mServerSocket, SIGNAL ( saveFile() ),this, SIGNAL ( save() ) ); | ||
279 | connect( mServerSocket, SIGNAL ( file_received( bool ) ), this, SIGNAL ( getFile( bool ) ) ); | ||
280 | } | ||
209 | 281 | ||
@@ -273,3 +345,2 @@ bool KSyncManager::syncWithFile( QString fn , bool quick ) | |||
273 | mLastSyncedLocalFile = fn; | 345 | mLastSyncedLocalFile = fn; |
274 | mImplementation->sync_setModified(); | ||
275 | } | 346 | } |
@@ -312,3 +383,3 @@ void KSyncManager::multiSync( bool askforPrefs ) | |||
312 | if ( num ) | 383 | if ( num ) |
313 | mImplementation->sync_save(); | 384 | emit save(); |
314 | if ( num ) | 385 | if ( num ) |
@@ -616,4 +687,4 @@ bool KSyncManager::syncExternalApplication(QString resource) | |||
616 | { | 687 | { |
617 | if ( mImplementation->sync_isModified() ) | 688 | |
618 | mImplementation->sync_save(); | 689 | emit save(); |
619 | 690 | ||
@@ -632,4 +703,3 @@ void KSyncManager::syncPhone() | |||
632 | { | 703 | { |
633 | if ( mImplementation->sync_isModified() ) | 704 | emit save(); |
634 | mImplementation->sync_save(); | ||
635 | 705 | ||
@@ -637,3 +707,2 @@ void KSyncManager::syncPhone() | |||
637 | //mView->syncPhone(); | 707 | //mView->syncPhone(); |
638 | mImplementation->sync_setModified(); | ||
639 | 708 | ||
@@ -663 +732,427 @@ bool KSyncManager::isProgressBarCanceled() | |||
663 | } | 732 | } |
733 | |||
734 | QString KSyncManager::syncFileName() | ||
735 | { | ||
736 | |||
737 | QString fn = "tempfile"; | ||
738 | switch(mTargetApp) | ||
739 | { | ||
740 | case (KAPI): | ||
741 | fn = "addressbook.vcf"; | ||
742 | break; | ||
743 | case (KOPI): | ||
744 | fn = "synccalendar.ics"; | ||
745 | break; | ||
746 | case (PWMPI): | ||
747 | fn = "manager.pwm"; | ||
748 | break; | ||
749 | default: | ||
750 | break; | ||
751 | } | ||
752 | #ifdef _WIN32_ | ||
753 | return locateLocal( "tmp", fn ); | ||
754 | #else | ||
755 | return (QString( "/tmp/" )+ fn ); | ||
756 | #endif | ||
757 | } | ||
758 | |||
759 | void KSyncManager::syncPi() | ||
760 | { | ||
761 | qApp->processEvents(); | ||
762 | bool ok; | ||
763 | Q_UINT16 port = mActiveSyncPort.toUInt(&ok); | ||
764 | if ( ! ok ) { | ||
765 | mParent->setCaption( i18n("Sorry, no valid port.Syncing cancelled.") ); | ||
766 | return; | ||
767 | } | ||
768 | KCommandSocket* commandSocket = new KCommandSocket( mPassWordPiSync, port, mActiveSyncIP, this ); | ||
769 | connect( commandSocket, SIGNAL(commandFinished( KCommandSocket*, int )), this, SLOT(deleteCommandSocket(KCommandSocket*, int)) ); | ||
770 | mParent->setCaption( i18n("Sending request for remote file ...") ); | ||
771 | commandSocket->readFile( syncFileName() ); | ||
772 | } | ||
773 | |||
774 | void KSyncManager::deleteCommandSocket(KCommandSocket*s, int state) | ||
775 | { | ||
776 | qDebug("MainWindow::deleteCommandSocket %d", state); | ||
777 | |||
778 | //enum { success, errorW, errorR, quiet }; | ||
779 | if ( state == KCommandSocket::errorR ) { | ||
780 | mParent->setCaption( i18n("ERROR: Receiving remote file failed.") ); | ||
781 | delete s; | ||
782 | KCommandSocket* commandSocket = new KCommandSocket( mPassWordPiSync, mActiveSyncPort.toUInt(), mActiveSyncIP, this ); | ||
783 | connect( commandSocket, SIGNAL(commandFinished( KCommandSocket*, int)), this, SLOT(deleteCommandSocket(KCommandSocket*, int )) ); | ||
784 | commandSocket->sendStop(); | ||
785 | return; | ||
786 | |||
787 | } else if ( state == KCommandSocket::errorW ) { | ||
788 | mParent->setCaption( i18n("ERROR:Writing back file failed.") ); | ||
789 | |||
790 | } else if ( state == KCommandSocket::successR ) { | ||
791 | QTimer::singleShot( 1, this , SLOT ( readFileFromSocket())); | ||
792 | |||
793 | } else if ( state == KCommandSocket::successW ) { | ||
794 | mParent->setCaption( i18n("Pi-Sync succesful!") ); | ||
795 | } | ||
796 | |||
797 | delete s; | ||
798 | } | ||
799 | |||
800 | void KSyncManager::readFileFromSocket() | ||
801 | { | ||
802 | QString fileName = syncFileName(); | ||
803 | mParent->setCaption( i18n("Remote file saved to temp file.") ); | ||
804 | if ( ! syncWithFile( fileName , true ) ) { | ||
805 | mParent->setCaption( i18n("Syncing failed.") ); | ||
806 | qDebug("Syncing failed "); | ||
807 | return; | ||
808 | } | ||
809 | KCommandSocket* commandSocket = new KCommandSocket( mPassWordPiSync, mActiveSyncPort.toUInt(), mActiveSyncIP, this ); | ||
810 | connect( commandSocket, SIGNAL(commandFinished( KCommandSocket*, int)), this, SLOT(deleteCommandSocket(KCommandSocket*, int )) ); | ||
811 | if ( mWriteBackFile ) | ||
812 | commandSocket->writeFile( fileName ); | ||
813 | else { | ||
814 | commandSocket->sendStop(); | ||
815 | mParent->setCaption( i18n("Pi-Sync succesful!") ); | ||
816 | } | ||
817 | } | ||
818 | |||
819 | |||
820 | |||
821 | |||
822 | |||
823 | |||
824 | |||
825 | |||
826 | |||
827 | |||
828 | |||
829 | |||
830 | |||
831 | |||
832 | |||
833 | |||
834 | |||
835 | |||
836 | |||
837 | |||
838 | |||
839 | |||
840 | |||
841 | KServerSocket:: KServerSocket ( QString pw, Q_UINT16 port, int backlog, QObject * parent, const char * name ) : QServerSocket( port, backlog, parent, name ) | ||
842 | { | ||
843 | mPassWord = pw; | ||
844 | mSocket = 0; | ||
845 | mSyncActionDialog = 0; | ||
846 | blockRC = false; | ||
847 | }; | ||
848 | |||
849 | void KServerSocket::newConnection ( int socket ) | ||
850 | { | ||
851 | // qDebug("KServerSocket:New connection %d ", socket); | ||
852 | if ( mSocket ) { | ||
853 | qDebug("KServerSocket::newConnection Socket deleted! "); | ||
854 | delete mSocket; | ||
855 | mSocket = 0; | ||
856 | } | ||
857 | mSocket = new QSocket( this ); | ||
858 | connect( mSocket , SIGNAL(readyRead()), this, SLOT(readClient()) ); | ||
859 | connect( mSocket , SIGNAL(delayedCloseFinished()), this, SLOT(discardClient()) ); | ||
860 | mSocket->setSocket( socket ); | ||
861 | } | ||
862 | |||
863 | void KServerSocket::discardClient() | ||
864 | { | ||
865 | //qDebug(" KServerSocket::discardClient()"); | ||
866 | if ( mSocket ) { | ||
867 | delete mSocket; | ||
868 | mSocket = 0; | ||
869 | } | ||
870 | //emit endConnect(); | ||
871 | } | ||
872 | void KServerSocket::readClient() | ||
873 | { | ||
874 | if ( blockRC ) | ||
875 | return; | ||
876 | if ( mSocket == 0 ) { | ||
877 | qDebug("ERROR::KServerSocket::readClient(): mSocket == 0 "); | ||
878 | return; | ||
879 | } | ||
880 | qDebug("KServerSocket readClient()"); | ||
881 | if ( mSocket->canReadLine() ) { | ||
882 | QString line = mSocket->readLine(); | ||
883 | qDebug("KServerSocket readline: %s ", line.latin1()); | ||
884 | QStringList tokens = QStringList::split( QRegExp("[ \r\n][ \r\n]*"), line ); | ||
885 | if ( tokens[0] == "GET" ) { | ||
886 | if ( tokens[1] == mPassWord ) | ||
887 | //emit sendFile( mSocket ); | ||
888 | send_file(); | ||
889 | else { | ||
890 | KMessageBox::information( 0, i18n("ERROR:\nGot send file request\nwith invalid password")); | ||
891 | qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); | ||
892 | } | ||
893 | } | ||
894 | if ( tokens[0] == "PUT" ) { | ||
895 | if ( tokens[1] == mPassWord ) { | ||
896 | //emit getFile( mSocket ); | ||
897 | blockRC = true; | ||
898 | get_file(); | ||
899 | } | ||
900 | else { | ||
901 | KMessageBox::information( 0, i18n("ERROR:\nGot receive file request\nwith invalid password")); | ||
902 | qDebug("password %s, invalid password %s ",mPassWord.latin1(), tokens[1].latin1() ); | ||
903 | } | ||
904 | } | ||
905 | if ( tokens[0] == "STOP" ) { | ||
906 | //emit endConnect(); | ||
907 | end_connect(); | ||
908 | } | ||
909 | } | ||
910 | } | ||
911 | void KServerSocket::end_connect() | ||
912 | { | ||
913 | delete mSyncActionDialog; | ||
914 | mSyncActionDialog = 0; | ||
915 | } | ||
916 | void KServerSocket::send_file() | ||
917 | { | ||
918 | //qDebug("MainWindow::sendFile(QSocket* s) "); | ||
919 | if ( mSyncActionDialog ) | ||
920 | delete mSyncActionDialog; | ||
921 | mSyncActionDialog = new QDialog ( 0, "input-dialog", true ); | ||
922 | mSyncActionDialog->setCaption(i18n("Received sync request")); | ||
923 | QLabel* label = new QLabel( i18n("Synchronizing from remote ...\n\nDo not use this application!\n\nIf syncing fails\nyou can close this dialog."), mSyncActionDialog ); | ||
924 | QVBoxLayout* lay = new QVBoxLayout( mSyncActionDialog ); | ||
925 | lay->addWidget( label); | ||
926 | lay->setMargin(7); | ||
927 | lay->setSpacing(7); | ||
928 | mSyncActionDialog->setFixedSize( 230, 120); | ||
929 | mSyncActionDialog->show(); | ||
930 | qDebug("KSS::saving ... "); | ||
931 | emit saveFile(); | ||
932 | qApp->processEvents(); | ||
933 | QString fileName = mFileName; | ||
934 | QFile file( fileName ); | ||
935 | if (!file.open( IO_ReadOnly ) ) { | ||
936 | delete mSyncActionDialog; | ||
937 | mSyncActionDialog = 0; | ||
938 | qDebug("KSS::error open file "); | ||
939 | mSocket->close(); | ||
940 | if ( mSocket->state() == QSocket::Idle ) | ||
941 | QTimer::singleShot( 10, this , SLOT ( discardClient())); | ||
942 | return ; | ||
943 | |||
944 | } | ||
945 | mSyncActionDialog->setCaption( i18n("Sending file...") ); | ||
946 | QTextStream ts( &file ); | ||
947 | ts.setCodec( QTextCodec::codecForName("utf8") ); | ||
948 | QTextStream os( mSocket ); | ||
949 | os.setCodec( QTextCodec::codecForName("utf8") ); | ||
950 | //os.setEncoding( QTextStream::UnicodeUTF8 ); | ||
951 | while ( ! ts.atEnd() ) { | ||
952 | os << ts.readLine() << "\n"; | ||
953 | } | ||
954 | //os << ts.read(); | ||
955 | file.close(); | ||
956 | mSyncActionDialog->setCaption( i18n("Waiting for synced file...") ); | ||
957 | mSocket->close(); | ||
958 | if ( mSocket->state() == QSocket::Idle ) | ||
959 | QTimer::singleShot( 10, this , SLOT ( discardClient())); | ||
960 | } | ||
961 | void KServerSocket::get_file() | ||
962 | { | ||
963 | mSyncActionDialog->setCaption( i18n("Receiving synced file...") ); | ||
964 | |||
965 | piTime.start(); | ||
966 | piFileString = ""; | ||
967 | QTimer::singleShot( 1, this , SLOT (readBackFileFromSocket( ) )); | ||
968 | } | ||
969 | |||
970 | |||
971 | void KServerSocket::readBackFileFromSocket() | ||
972 | { | ||
973 | //qDebug("readBackFileFromSocket() %d ", piTime.elapsed ()); | ||
974 | while ( mSocket->canReadLine () ) { | ||
975 | piTime.restart(); | ||
976 | QString line = mSocket->readLine (); | ||
977 | piFileString += line; | ||
978 | //qDebug("readline: %s ", line.latin1()); | ||
979 | mSyncActionDialog->setCaption( i18n("Received %1 bytes").arg( piFileString.length() ) ); | ||
980 | |||
981 | } | ||
982 | if ( piTime.elapsed () < 3000 ) { | ||
983 | // wait for more | ||
984 | //qDebug("waitformore "); | ||
985 | QTimer::singleShot( 100, this , SLOT (readBackFileFromSocket( ) )); | ||
986 | return; | ||
987 | } | ||
988 | QString fileName = mFileName; | ||
989 | QFile file ( fileName ); | ||
990 | if (!file.open( IO_WriteOnly ) ) { | ||
991 | delete mSyncActionDialog; | ||
992 | mSyncActionDialog = 0; | ||
993 | qDebug("error open cal file "); | ||
994 | piFileString = ""; | ||
995 | emit file_received( false ); | ||
996 | blockRC = false; | ||
997 | return ; | ||
998 | |||
999 | } | ||
1000 | |||
1001 | // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); | ||
1002 | QTextStream ts ( &file ); | ||
1003 | ts.setCodec( QTextCodec::codecForName("utf8") ); | ||
1004 | mSyncActionDialog->setCaption( i18n("Writing file to disk...") ); | ||
1005 | ts << piFileString; | ||
1006 | mSocket->close(); | ||
1007 | if ( mSocket->state() == QSocket::Idle ) | ||
1008 | QTimer::singleShot( 10, this , SLOT ( discardClient())); | ||
1009 | file.close(); | ||
1010 | delete mSyncActionDialog; | ||
1011 | mSyncActionDialog = 0; | ||
1012 | piFileString = ""; | ||
1013 | blockRC = false; | ||
1014 | emit file_received( true ); | ||
1015 | |||
1016 | } | ||
1017 | |||
1018 | KCommandSocket::KCommandSocket ( QString password, Q_UINT16 port, QString host, QObject * parent, const char * name ): QObject( parent, name ) | ||
1019 | { | ||
1020 | mPassWord = password; | ||
1021 | mSocket = 0; | ||
1022 | mPort = port; | ||
1023 | mHost = host; | ||
1024 | |||
1025 | mRetVal = quiet; | ||
1026 | mTimerSocket = new QTimer ( this ); | ||
1027 | connect( mTimerSocket, SIGNAL ( timeout () ), this, SLOT ( deleteSocket() ) ); | ||
1028 | } | ||
1029 | void KCommandSocket::readFile( QString fn ) | ||
1030 | { | ||
1031 | if ( !mSocket ) { | ||
1032 | mSocket = new QSocket( this ); | ||
1033 | connect( mSocket, SIGNAL(readyRead()), this, SLOT(startReadFileFromSocket()) ); | ||
1034 | connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
1035 | } | ||
1036 | mFileString = ""; | ||
1037 | mFileName = fn; | ||
1038 | mFirst = true; | ||
1039 | mSocket->connectToHost( mHost, mPort ); | ||
1040 | QTextStream os( mSocket ); | ||
1041 | os.setEncoding( QTextStream::UnicodeUTF8 ); | ||
1042 | os << "GET " << mPassWord << "\r\n"; | ||
1043 | mTimerSocket->start( 10000 ); | ||
1044 | } | ||
1045 | |||
1046 | void KCommandSocket::writeFile( QString fileName ) | ||
1047 | { | ||
1048 | if ( !mSocket ) { | ||
1049 | mSocket = new QSocket( this ); | ||
1050 | connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
1051 | connect( mSocket, SIGNAL(connected ()), this, SLOT(writeFileToSocket()) ); | ||
1052 | } | ||
1053 | mFileName = fileName ; | ||
1054 | mSocket->connectToHost( mHost, mPort ); | ||
1055 | } | ||
1056 | void KCommandSocket::writeFileToSocket() | ||
1057 | { | ||
1058 | QFile file2( mFileName ); | ||
1059 | if (!file2.open( IO_ReadOnly ) ) { | ||
1060 | mRetVal= errorW; | ||
1061 | mSocket->close(); | ||
1062 | if ( mSocket->state() == QSocket::Idle ) | ||
1063 | QTimer::singleShot( 10, this , SLOT ( deleteSocket())); | ||
1064 | return ; | ||
1065 | } | ||
1066 | QTextStream ts2( &file2 ); | ||
1067 | ts2.setCodec( QTextCodec::codecForName("utf8") ); | ||
1068 | QTextStream os2( mSocket ); | ||
1069 | os2.setCodec( QTextCodec::codecForName("utf8") ); | ||
1070 | os2 << "PUT " << mPassWord << "\r\n";; | ||
1071 | while ( ! ts2.atEnd() ) { | ||
1072 | os2 << ts2.readLine() << "\n"; | ||
1073 | } | ||
1074 | mRetVal= successW; | ||
1075 | file2.close(); | ||
1076 | mSocket->close(); | ||
1077 | if ( mSocket->state() == QSocket::Idle ) | ||
1078 | QTimer::singleShot( 10, this , SLOT ( deleteSocket())); | ||
1079 | } | ||
1080 | void KCommandSocket::sendStop() | ||
1081 | { | ||
1082 | if ( !mSocket ) { | ||
1083 | mSocket = new QSocket( this ); | ||
1084 | connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
1085 | } | ||
1086 | mSocket->connectToHost( mHost, mPort ); | ||
1087 | QTextStream os2( mSocket ); | ||
1088 | os2.setCodec( QTextCodec::codecForName("utf8") ); | ||
1089 | os2 << "STOP\r\n"; | ||
1090 | mSocket->close(); | ||
1091 | if ( mSocket->state() == QSocket::Idle ) | ||
1092 | QTimer::singleShot( 10, this , SLOT ( deleteSocket())); | ||
1093 | } | ||
1094 | |||
1095 | void KCommandSocket::startReadFileFromSocket() | ||
1096 | { | ||
1097 | if ( ! mFirst ) | ||
1098 | return; | ||
1099 | mFirst = false; | ||
1100 | mTimerSocket->stop(); | ||
1101 | mFileString = ""; | ||
1102 | mTime.start(); | ||
1103 | QTimer::singleShot( 1, this , SLOT (readFileFromSocket( ) )); | ||
1104 | |||
1105 | } | ||
1106 | void KCommandSocket::readFileFromSocket() | ||
1107 | { | ||
1108 | //qDebug("readBackFileFromSocket() %d ", mTime.elapsed ()); | ||
1109 | while ( mSocket->canReadLine () ) { | ||
1110 | mTime.restart(); | ||
1111 | QString line = mSocket->readLine (); | ||
1112 | mFileString += line; | ||
1113 | //qDebug("readline: %s ", line.latin1()); | ||
1114 | } | ||
1115 | if ( mTime.elapsed () < 3000 ) { | ||
1116 | // wait for more | ||
1117 | //qDebug("waitformore "); | ||
1118 | QTimer::singleShot( 100, this , SLOT (readFileFromSocket( ) )); | ||
1119 | return; | ||
1120 | } | ||
1121 | QString fileName = mFileName; | ||
1122 | QFile file ( fileName ); | ||
1123 | if (!file.open( IO_WriteOnly ) ) { | ||
1124 | mFileString = ""; | ||
1125 | mRetVal = errorR; | ||
1126 | qDebug("Error open temp calender file for writing: %s",fileName.latin1() ); | ||
1127 | deleteSocket(); | ||
1128 | return ; | ||
1129 | |||
1130 | } | ||
1131 | // mView->setLoadedFileVersion(QDateTime::currentDateTime().addSecs( -1)); | ||
1132 | QTextStream ts ( &file ); | ||
1133 | ts.setCodec( QTextCodec::codecForName("utf8") ); | ||
1134 | ts << mFileString; | ||
1135 | file.close(); | ||
1136 | mFileString = ""; | ||
1137 | mRetVal = successR; | ||
1138 | mSocket->close(); | ||
1139 | // if state is not idle, deleteSocket(); is called via | ||
1140 | // connect( mSocket, SIGNAL(delayedCloseFinished ()), this, SLOT(deleteSocket()) ); | ||
1141 | if ( mSocket->state() == QSocket::Idle ) | ||
1142 | deleteSocket(); | ||
1143 | } | ||
1144 | |||
1145 | void KCommandSocket::deleteSocket() | ||
1146 | { | ||
1147 | if ( mTimerSocket->isActive () ) { | ||
1148 | mTimerSocket->stop(); | ||
1149 | KMessageBox::information( 0, i18n("ERROR:\nConnection to remote host timed out!\nDid you forgot to enable\nsyncing on remote host? ")); | ||
1150 | mRetVal = errorR; | ||
1151 | } | ||
1152 | //qDebug("KCommandSocket::deleteSocket() %d", mRetVal ); | ||
1153 | if ( mSocket) | ||
1154 | delete mSocket; | ||
1155 | mSocket = 0; | ||
1156 | emit commandFinished( this, mRetVal ); | ||
1157 | } | ||
1158 | |||
diff --git a/libkdepim/ksyncmanager.h b/libkdepim/ksyncmanager.h index 6b10016..52e2772 100644 --- a/libkdepim/ksyncmanager.h +++ b/libkdepim/ksyncmanager.h | |||
@@ -26,2 +26,7 @@ $Id$ | |||
26 | #include <qstring.h> | 26 | #include <qstring.h> |
27 | #include <qsocket.h> | ||
28 | #include <qdatetime.h> | ||
29 | #include <qserversocket.h> | ||
30 | #include <qtextstream.h> | ||
31 | #include <qregexp.h> | ||
27 | 32 | ||
@@ -35,2 +40,69 @@ class QProgressBar; | |||
35 | 40 | ||
41 | |||
42 | class KServerSocket : public QServerSocket | ||
43 | { | ||
44 | Q_OBJECT | ||
45 | |||
46 | public: | ||
47 | KServerSocket ( QString password, Q_UINT16 port, int backlog = 0, QObject * parent=0, const char * name=0 ); | ||
48 | |||
49 | void newConnection ( int socket ) ; | ||
50 | void setFileName( QString fn ) {mFileName = fn;}; | ||
51 | |||
52 | signals: | ||
53 | //void sendFile(QSocket*); | ||
54 | //void getFile(QSocket*); | ||
55 | void file_received( bool ); | ||
56 | //void file_sent(); | ||
57 | void saveFile(); | ||
58 | void endConnect(); | ||
59 | private slots: | ||
60 | void discardClient(); | ||
61 | void readClient(); | ||
62 | void readBackFileFromSocket(); | ||
63 | private : | ||
64 | bool blockRC; | ||
65 | void send_file(); | ||
66 | void get_file(); | ||
67 | void end_connect(); | ||
68 | QDialog* mSyncActionDialog; | ||
69 | QSocket* mSocket; | ||
70 | QString mPassWord; | ||
71 | QString mFileName; | ||
72 | QTime piTime; | ||
73 | QString piFileString; | ||
74 | }; | ||
75 | |||
76 | class KCommandSocket : public QObject | ||
77 | { | ||
78 | Q_OBJECT | ||
79 | public: | ||
80 | enum state { successR, errorR, successW, errorW, quiet }; | ||
81 | KCommandSocket ( QString password, Q_UINT16 port, QString host, QObject * parent=0, const char * name=0 ); | ||
82 | void readFile( QString ); | ||
83 | void writeFile( QString ); | ||
84 | void sendStop(); | ||
85 | |||
86 | |||
87 | signals: | ||
88 | void commandFinished( KCommandSocket*, int ); | ||
89 | private slots: | ||
90 | void startReadFileFromSocket(); | ||
91 | void readFileFromSocket(); | ||
92 | void deleteSocket(); | ||
93 | void writeFileToSocket(); | ||
94 | private : | ||
95 | QSocket* mSocket; | ||
96 | QString mPassWord; | ||
97 | Q_UINT16 mPort; | ||
98 | QString mHost; | ||
99 | QString mFileName; | ||
100 | QTimer* mTimerSocket; | ||
101 | int mRetVal; | ||
102 | QTime mTime; | ||
103 | QString mFileString; | ||
104 | bool mFirst; | ||
105 | }; | ||
106 | |||
107 | |||
36 | class KSyncManager : public QObject | 108 | class KSyncManager : public QObject |
@@ -51,3 +123,5 @@ class KSyncManager : public QObject | |||
51 | void setBlockSave(bool sa) { mBlockSaveFlag = sa; } | 123 | void setBlockSave(bool sa) { mBlockSaveFlag = sa; } |
52 | 124 | void setDefaultFileName( QString s) { mDefFileName = s ;} | |
125 | QString defaultFileName() { return mDefFileName ;} | ||
126 | QString syncFileName(); | ||
53 | void fillSyncMenu(); | 127 | void fillSyncMenu(); |
@@ -78,4 +152,9 @@ class KSyncManager : public QObject | |||
78 | QString mLastSyncedLocalFile; // save! | 152 | QString mLastSyncedLocalFile; // save! |
153 | QString mPassWordPiSync; | ||
154 | QString mActiveSyncPort; | ||
155 | QString mActiveSyncIP ; | ||
79 | 156 | ||
80 | 157 | signals: | |
158 | void save(); | ||
159 | void getFile( bool ); | ||
81 | 160 | ||
@@ -87,2 +166,9 @@ class KSyncManager : public QObject | |||
87 | // sync stuff! | 166 | // sync stuff! |
167 | void syncPi(); | ||
168 | void deleteCommandSocket(KCommandSocket*s, int state); | ||
169 | void readFileFromSocket(); | ||
170 | KServerSocket * mServerSocket; | ||
171 | void enableQuick(); | ||
172 | KPimPrefs* mPrefs; | ||
173 | QString mDefFileName; | ||
88 | QString mCurrentSyncDevice; | 174 | QString mCurrentSyncDevice; |
@@ -126,2 +212,4 @@ class KSyncInterface | |||
126 | { | 212 | { |
213 | |||
214 | |||
127 | public: | 215 | public: |
@@ -135,10 +223,3 @@ class KSyncInterface | |||
135 | 223 | ||
136 | //called by the syncmanager to indicate that the work has to be marked as dirty. | ||
137 | virtual void sync_setModified() = 0; | ||
138 | |||
139 | //called by the syncmanager to ask if the dirty flag is set. | ||
140 | virtual bool sync_isModified() = 0; | ||
141 | 224 | ||
142 | //called by the syncmanager to indicate that the work has to be saved. | ||
143 | virtual void sync_save() = 0; | ||
144 | }; | 225 | }; |