summaryrefslogtreecommitdiffabout
path: root/libkdepim/ksyncmanager.cpp
authorzautrix <zautrix>2004-10-07 07:28:54 (UTC)
committer zautrix <zautrix>2004-10-07 07:28:54 (UTC)
commit77280f6a176a15b60004f312e6cacdfbbd3909c1 (patch) (unidiff)
treee074d4abedc2f4cc575e5655575e038b9d460106 /libkdepim/ksyncmanager.cpp
parentf1eb5b74c962909851607c4b4cb05ee18a347d37 (diff)
downloadkdepimpi-77280f6a176a15b60004f312e6cacdfbbd3909c1.zip
kdepimpi-77280f6a176a15b60004f312e6cacdfbbd3909c1.tar.gz
kdepimpi-77280f6a176a15b60004f312e6cacdfbbd3909c1.tar.bz2
sync changes
Diffstat (limited to 'libkdepim/ksyncmanager.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkdepim/ksyncmanager.cpp567
1 files changed, 531 insertions, 36 deletions
diff --git a/libkdepim/ksyncmanager.cpp b/libkdepim/ksyncmanager.cpp
index c0cc840..5d48884 100644
--- a/libkdepim/ksyncmanager.cpp
+++ b/libkdepim/ksyncmanager.cpp
@@ -29,12 +29,13 @@
29#endif 29#endif
30 30
31 31
32#include "ksyncprofile.h" 32#include "ksyncprofile.h"
33#include "ksyncprefsdialog.h" 33#include "ksyncprefsdialog.h"
34#include "kpimprefs.h" 34#include "kpimprefs.h"
35#include <kmessagebox.h>
35 36
36#include <qdir.h> 37#include <qdir.h>
37#include <qprogressbar.h> 38#include <qprogressbar.h>
38#include <qpopupmenu.h> 39#include <qpopupmenu.h>
39#include <qpushbutton.h> 40#include <qpushbutton.h>
40#include <qradiobutton.h> 41#include <qradiobutton.h>
@@ -42,21 +43,24 @@
42#include <qtimer.h> 43#include <qtimer.h>
43#include <qmessagebox.h> 44#include <qmessagebox.h>
44#include <qapplication.h> 45#include <qapplication.h>
45#include <qlineedit.h> 46#include <qlineedit.h>
46#include <qdialog.h> 47#include <qdialog.h>
47#include <qlayout.h> 48#include <qlayout.h>
49#include <qtextcodec.h>
50#include <qlabel.h>
48 51
49#include <klocale.h> 52#include <klocale.h>
50#include <kglobal.h> 53#include <kglobal.h>
51#include <kconfig.h> 54#include <kconfig.h>
52#include <kfiledialog.h> 55#include <kfiledialog.h>
53 56
54KSyncManager::KSyncManager(QWidget* parent, KSyncInterface* implementation, TargetApp ta, KPimPrefs* prefs, QPopupMenu* syncmenu) 57KSyncManager::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 );
58 bar->setCaption (""); 62 bar->setCaption ("");
59 63
60 int w = 300; 64 int w = 300;
61 if ( QApplication::desktop()->width() < 320 ) 65 if ( QApplication::desktop()->width() < 320 )
62 w = 220; 66 w = 220;
@@ -69,39 +73,42 @@ KSyncManager::KSyncManager(QWidget* parent, KSyncInterface* implementation, Targ
69 73
70KSyncManager::~KSyncManager() 74KSyncManager::~KSyncManager()
71{ 75{
72 delete bar; 76 delete bar;
73} 77}
74 78
75 79//LR ok
76void KSyncManager::fillSyncMenu() 80void KSyncManager::fillSyncMenu()
77{ 81{
78 if ( mSyncMenu->count() ) 82 if ( mSyncMenu->count() )
79 mSyncMenu->clear(); 83 mSyncMenu->clear();
80 84
81 mSyncMenu->insertItem( i18n("Configure..."), 0 ); 85 mSyncMenu->insertItem( i18n("Configure..."), 0 );
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 );
84 mSyncMenu->insertSeparator(); 94 mSyncMenu->insertSeparator();
85 95
86 KConfig config ( locateLocal( "config","ksyncprofilesrc" ) ); 96 KConfig config ( locateLocal( "config","ksyncprofilesrc" ) );
87 config.setGroup("General"); 97 config.setGroup("General");
88 QStringList prof = config.readListEntry("SyncProfileNames"); 98 QStringList prof = config.readListEntry("SyncProfileNames");
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();
92 prof << i18n("Sharp_DTM"); 102 prof << i18n("Sharp_DTM");
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 ();
96 temp->setName( prof[0] ); 105 temp->setName( prof[0] );
97 temp->writeConfig(&config); 106 temp->writeConfig(&config);
98 temp->setName( prof[1] ); 107 temp->setName( prof[1] );
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");
103 config.writeEntry("SyncProfileNames",prof); 110 config.writeEntry("SyncProfileNames",prof);
104 config.writeEntry("ExternSyncProfiles","Sharp_DTM"); 111 config.writeEntry("ExternSyncProfiles","Sharp_DTM");
105 config.sync(); 112 config.sync();
106 delete temp; 113 delete temp;
107 } 114 }
@@ -138,12 +145,23 @@ void KSyncManager::slotSyncMenu( int action )
138 return; 145 return;
139 } 146 }
140 if ( action == 1 ) { 147 if ( action == 1 ) {
141 multiSync( true ); 148 multiSync( true );
142 return; 149 return;
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
145 if (blockSave()) 163 if (blockSave())
146 return; 164 return;
147 165
148 setBlockSave(true); 166 setBlockSave(true);
149 167
@@ -195,20 +213,74 @@ void KSyncManager::slotSyncMenu( int action )
195 } else { 213 } else {
196 if ( temp->getIsPhoneSync() ) { 214 if ( temp->getIsPhoneSync() ) {
197 mPhoneDevice = temp->getPhoneDevice( ) ; 215 mPhoneDevice = temp->getPhoneDevice( ) ;
198 mPhoneConnection = temp->getPhoneConnection( ); 216 mPhoneConnection = temp->getPhoneConnection( );
199 mPhoneModel = temp->getPhoneModel( ); 217 mPhoneModel = temp->getPhoneModel( );
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 );
203 226
204 } 227 }
205 } 228 }
206 delete temp; 229 delete temp;
207 setBlockSave(false); 230 setBlockSave(false);
208} 231}
232void 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
210void KSyncManager::syncLocalFile() 282void KSyncManager::syncLocalFile()
211{ 283{
212 284
213 QString fn =mLastSyncedLocalFile; 285 QString fn =mLastSyncedLocalFile;
214 QString ext; 286 QString ext;
@@ -268,13 +340,12 @@ bool KSyncManager::syncWithFile( QString fn , bool quick )
268 if ( ret = mImplementation->sync( this, fn, mSyncAlgoPrefs ) ) 340 if ( ret = mImplementation->sync( this, fn, mSyncAlgoPrefs ) )
269 mParent->setCaption( i18n("Synchronization successful") ); 341 mParent->setCaption( i18n("Synchronization successful") );
270 else 342 else
271 mParent->setCaption( i18n("Sync cancelled or failed. Nothing synced.") ); 343 mParent->setCaption( i18n("Sync cancelled or failed. Nothing synced.") );
272 if ( ! quick ) 344 if ( ! quick )
273 mLastSyncedLocalFile = fn; 345 mLastSyncedLocalFile = fn;
274 mImplementation->sync_setModified();
275 } 346 }
276 return ret; 347 return ret;
277} 348}
278void KSyncManager::quickSyncLocalFile() 349void KSyncManager::quickSyncLocalFile()
279{ 350{
280 351
@@ -307,13 +378,13 @@ void KSyncManager::multiSync( bool askforPrefs )
307 qApp->processEvents(); 378 qApp->processEvents();
308 int num = ringSync() ; 379 int num = ringSync() ;
309 if ( num > 1 ) 380 if ( num > 1 )
310 ringSync(); 381 ringSync();
311 setBlockSave(false); 382 setBlockSave(false);
312 if ( num ) 383 if ( num )
313 mImplementation->sync_save(); 384 emit save();
314 if ( num ) 385 if ( num )
315 mParent->setCaption(i18n("%1 profiles synced. Multiple sync completed!").arg(num) ); 386 mParent->setCaption(i18n("%1 profiles synced. Multiple sync completed!").arg(num) );
316 else 387 else
317 mParent->setCaption(i18n("Nothing synced! No profiles defined for multisync!")); 388 mParent->setCaption(i18n("Nothing synced! No profiles defined for multisync!"));
318 return; 389 return;
319} 390}
@@ -425,32 +496,32 @@ void KSyncManager::syncRemote( KSyncProfile* prof, bool ask)
425 496
426 QString preCommand; 497 QString preCommand;
427 QString localTempFile; 498 QString localTempFile;
428 QString postCommand; 499 QString postCommand;
429 500
430 switch(mTargetApp) 501 switch(mTargetApp)
431 { 502 {
432 case (KAPI): 503 case (KAPI):
433 preCommand = prof->getPreSyncCommandAB(); 504 preCommand = prof->getPreSyncCommandAB();
434 postCommand = prof->getPostSyncCommandAB(); 505 postCommand = prof->getPostSyncCommandAB();
435 localTempFile = prof->getLocalTempFileAB(); 506 localTempFile = prof->getLocalTempFileAB();
436 break; 507 break;
437 case (KOPI): 508 case (KOPI):
438 preCommand = prof->getPreSyncCommand(); 509 preCommand = prof->getPreSyncCommand();
439 postCommand = prof->getPostSyncCommand(); 510 postCommand = prof->getPostSyncCommand();
440 localTempFile = prof->getLocalTempFile(); 511 localTempFile = prof->getLocalTempFile();
441 break; 512 break;
442 case (PWMPI): 513 case (PWMPI):
443 preCommand = prof->getPreSyncCommandPWM(); 514 preCommand = prof->getPreSyncCommandPWM();
444 postCommand = prof->getPostSyncCommandPWM(); 515 postCommand = prof->getPostSyncCommandPWM();
445 localTempFile = prof->getLocalTempFilePWM(); 516 localTempFile = prof->getLocalTempFilePWM();
446 break; 517 break;
447 default: 518 default:
448 qDebug("KSyncManager::syncRemote: invalid apptype selected"); 519 qDebug("KSyncManager::syncRemote: invalid apptype selected");
449 break; 520 break;
450 } 521 }
451 522
452 523
453 int fi; 524 int fi;
454 if ( (fi = preCommand.find("$PWD$")) > 0 ) { 525 if ( (fi = preCommand.find("$PWD$")) > 0 ) {
455 QString pwd = getPassword(); 526 QString pwd = getPassword();
456 preCommand = preCommand.left( fi )+ pwd + preCommand.mid( fi+5 ); 527 preCommand = preCommand.left( fi )+ pwd + preCommand.mid( fi+5 );
@@ -611,14 +682,14 @@ void KSyncManager::syncSharp()
611 if ( ! syncExternalApplication("sharp") ) 682 if ( ! syncExternalApplication("sharp") )
612 qDebug("ERROR sync sharp ");; 683 qDebug("ERROR sync sharp ");;
613} 684}
614 685
615bool KSyncManager::syncExternalApplication(QString resource) 686bool KSyncManager::syncExternalApplication(QString resource)
616{ 687{
617 if ( mImplementation->sync_isModified() ) 688
618 mImplementation->sync_save(); 689 emit save();
619 690
620 if ( mAskForPreferences ) 691 if ( mAskForPreferences )
621 edit_sync_options(); 692 edit_sync_options();
622 693
623 qDebug("sync %s", resource.latin1()); 694 qDebug("sync %s", resource.latin1());
624 695
@@ -627,18 +698,16 @@ bool KSyncManager::syncExternalApplication(QString resource)
627 return syncOK; 698 return syncOK;
628 699
629} 700}
630 701
631void KSyncManager::syncPhone() 702void KSyncManager::syncPhone()
632{ 703{
633 if ( mImplementation->sync_isModified() ) 704 emit save();
634 mImplementation->sync_save();
635 705
636 qDebug("pending syncPhone(); "); 706 qDebug("pending syncPhone(); ");
637 //mView->syncPhone(); 707 //mView->syncPhone();
638 mImplementation->sync_setModified();
639 708
640} 709}
641 710
642void KSyncManager::showProgressBar(int percentage, QString caption, int total) 711void KSyncManager::showProgressBar(int percentage, QString caption, int total)
643{ 712{
644 if (!bar->isVisible()) 713 if (!bar->isVisible())
@@ -658,6 +727,432 @@ void KSyncManager::hideProgressBar()
658} 727}
659 728
660bool KSyncManager::isProgressBarCanceled() 729bool KSyncManager::isProgressBarCanceled()
661{ 730{
662 return !bar->isVisible(); 731 return !bar->isVisible();
663} 732}
733
734QString 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
759void 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
774void 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
800void 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
841KServerSocket:: 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
849void 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
863void KServerSocket::discardClient()
864{
865 //qDebug(" KServerSocket::discardClient()");
866 if ( mSocket ) {
867 delete mSocket;
868 mSocket = 0;
869 }
870 //emit endConnect();
871}
872void 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}
911void KServerSocket::end_connect()
912{
913 delete mSyncActionDialog;
914 mSyncActionDialog = 0;
915}
916void 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}
961void 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
971void 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
1018KCommandSocket::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}
1029void 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
1046void 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}
1056void 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}
1080void 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
1095void 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}
1106void 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
1145void 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