kitchensync Library API Documentation

socket.cpp

00001 /* 00002 This file is part of KitchenSync. 00003 00004 Copyright (c) 2002,2003 Holger Freyther <freyther@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 */ 00021 00022 #include <qsocket.h> 00023 #include <qdir.h> 00024 #include <qtimer.h> 00025 00026 #include <kdebug.h> 00027 #include <klocale.h> 00028 #include <kio/netaccess.h> 00029 #include <libkcal/calendarlocal.h> 00030 00031 #include <addressbooksyncee.h> 00032 #include <calendarsyncee.h> 00033 #include <todosyncee.h> 00034 00035 #include <idhelper.h> 00036 00037 #include "device.h" 00038 #include "desktop.h" 00039 #include "datebook.h" 00040 #include "addressbook.h" 00041 00042 #include "metaaddressbook.h" 00043 #include "metadatebook.h" 00044 #include "metatodo.h" 00045 00046 #include "todo.h" 00047 00048 #include "socket.h" 00049 00050 using namespace KSync; 00051 00052 namespace { 00053 void outputIt( int area, Syncee* ); 00054 } 00055 00056 class QtopiaSocket::Private 00057 { 00058 public: 00059 enum CallIt 00060 { 00061 NotStarted = 0, 00062 Handshake = 0, 00063 ABook, 00064 Todo, 00065 Calendar, 00066 Transactions, 00067 Files, 00068 Desktops, 00069 Flush 00070 }; 00071 00072 enum Status { 00073 Start = 0, 00074 User, 00075 Pass, 00076 Call, 00077 Noop, 00078 Done, 00079 Connected 00080 }; 00081 00082 Private(){} 00083 00084 bool connected : 1; 00085 bool startSync : 1; 00086 bool isSyncing : 1; 00087 bool isConnecting : 1; 00088 bool first : 1; 00089 bool meta : 1; 00090 QString src; 00091 QString dest; 00092 QSocket* socket; 00093 QTimer* timer; 00094 QString path; 00095 int mode; 00096 int getMode; 00097 SynceeList m_sync; 00098 00099 QValueList<OpieCategories> categories; 00100 QString partnerId; 00101 QStringList files; 00102 QString tz; 00103 OpieHelper::CategoryEdit* edit; 00104 KonnectorUIDHelper* helper; 00105 OpieHelper::Device* device; 00106 OpieHelper::ExtraMap extras; 00107 }; 00108 00109 namespace { 00110 void parseTZ( const QString& fileName, QString& tz ); 00111 } 00112 00118 QtopiaSocket::QtopiaSocket( QObject* obj, const char* name ) 00119 : QObject( obj, name ) 00120 { 00121 d = new Private; 00122 d->socket = 0; 00123 d->timer = 0; 00124 d->connected = false; 00125 d->startSync = false; 00126 d->isSyncing = false; 00127 d->isConnecting = false; 00128 d->meta = true ; 00129 d->helper = 0; 00130 d->edit = 0; 00131 d->first = false; 00132 d->device = new OpieHelper::Device; 00133 m_flushedApps = 0; 00134 } 00135 00136 QtopiaSocket::~QtopiaSocket() 00137 { 00138 delete d->device; 00139 delete d; 00140 } 00141 00142 void QtopiaSocket::setUser( const QString& user ) 00143 { 00144 d->device->setUser( user ); 00145 } 00146 00147 void QtopiaSocket::setPassword( const QString& pass ) 00148 { 00149 d->device->setPassword( pass ); 00150 } 00151 00152 void QtopiaSocket::setSrcIP( const QString& src) 00153 { 00154 d->src = src; 00155 } 00156 00157 void QtopiaSocket::setDestIP( const QString& dest) 00158 { 00159 d->dest = dest; 00160 } 00161 00162 void QtopiaSocket::setModel( const QString& model, const QString& name ) 00163 { 00164 if( model == QString::fromLatin1("Sharp Zaurus ROM") ){ 00165 kdDebug(5225) << "Sharp Zaurus ROM match " << endl; 00166 d->device->setDistribution( OpieHelper::Device::Zaurus ); 00167 }else 00168 d->device->setDistribution( OpieHelper::Device::Opie ); 00169 00170 d->device->setMeta( name ); 00171 } 00172 00173 void QtopiaSocket::startUp() 00174 { 00175 kdDebug(5225) << "Start Up " << endl; 00176 delete d->socket; 00177 d->socket = new QSocket(this, "Qtopia Socket" ); 00178 00179 /* now connect to some slots */ 00180 connect(d->socket, SIGNAL(error(int) ), 00181 this, SLOT(slotError(int) ) ); 00182 connect(d->socket, SIGNAL(connected() ), 00183 this, SLOT(slotConnected() ) ); 00184 connect(d->socket, SIGNAL(connectionClosed() ), 00185 this, SLOT(slotClosed() ) ); 00186 connect(d->socket, SIGNAL(readyRead() ), 00187 this, SLOT(process() ) ); 00188 00189 d->connected = false; 00190 d->startSync = false; 00191 d->isConnecting = true; 00192 00193 d->categories.clear(); 00194 d->isSyncing = false; 00195 d->socket->connectToHost(d->dest, 4243 ); 00196 } 00197 00198 void QtopiaSocket::hangUP() 00199 { 00200 if (d->isSyncing ) { 00201 emit error( Error(Error::CouldNotDisconnect, i18n("Can not disconnect now. Try again after syncing was finished") ) ); 00202 return; 00203 } 00204 /* now connect to some slots */ 00205 disconnect(d->socket, SIGNAL(error(int) ), 00206 this, SLOT(slotError(int) ) ); 00207 disconnect(d->socket, SIGNAL(connected() ), 00208 this, SLOT(slotConnected() ) ); 00209 disconnect(d->socket, SIGNAL(connectionClosed() ), 00210 this, SLOT(slotClosed() ) ); 00211 disconnect(d->socket, SIGNAL(readyRead() ), 00212 this, SLOT(process() ) ); 00213 delete d->socket; 00214 d->socket = 0; 00215 d->isSyncing = false; 00216 d->connected = false; 00217 d->startSync = false; 00218 d->isConnecting = false; 00219 d->categories.clear(); 00220 d->getMode = d->NotStarted; 00221 d->mode = d->Start; 00222 emit prog( Progress(i18n("Disconnected from the device.") ) ); 00223 } 00224 00225 void QtopiaSocket::setResources( const QStringList& list ) 00226 { 00227 d->files = list; 00228 } 00229 00230 bool QtopiaSocket::startSync() 00231 { 00232 if ( d->isSyncing ) 00233 return false; 00234 d->isSyncing = true; 00235 d->getMode = d->NotStarted; 00236 00237 if (d->isConnecting ) { 00238 d->startSync = true; 00239 return true; 00240 } 00241 if (!isConnected() ) { 00242 startUp(); 00243 d->startSync = true; 00244 return true; 00245 } 00246 slotStartSync(); 00247 00248 return true; 00249 } 00250 00251 /* 00252 * check if we're connected 00253 */ 00254 bool QtopiaSocket::isConnected() 00255 { 00256 if ( d->connected || d->mode == d->Call || d->mode == d->Noop || d->mode == d->Connected ) 00257 return true; 00258 else 00259 return false; 00260 } 00261 00262 void QtopiaSocket::write( SynceeList list ) 00263 { 00264 if (!isConnected() ) { 00265 emit error( Error( i18n("<qt>Can not write the data back.\n There is no connection to the device") ) ); 00266 emit prog( StdProgress::done() ); 00267 return; 00268 } 00269 00270 kdDebug(5225) << "Writing information back now. Count is " << list.count() << endl; 00271 00272 AddressBookSyncee *abSyncee = list.addressBookSyncee(); 00273 if ( abSyncee ) writeAddressbook( abSyncee ); 00274 00275 CalendarSyncee *evSyncee = list.calendarSyncee(); 00276 if ( evSyncee ) writeDatebook( evSyncee ); 00277 00278 CalendarSyncee *toSyncee = list.calendarSyncee(); 00279 if ( toSyncee ) writeTodoList( toSyncee ); 00280 00281 /* 00282 * write new category information 00283 */ 00284 writeCategory(); 00285 d->helper->save(); 00286 00287 00288 /* trigger reload for apps on pda */ 00289 sendCommand( "call QPE/Application/datebook reload()" ); 00290 sendCommand( "call QPE/Application/addressbook reload()" ); 00291 sendCommand( "call QPE/Application/todolist reload()" ); 00292 00293 /* 00294 * tell Opie/Qtopia that we're ready 00295 */ 00296 sendCommand( "call QPE/System stopSync()" ); 00297 d->isSyncing = false; 00298 00299 /* 00300 * now we need that it's not first sync 00301 */ 00302 d->first = false; 00303 emit prog(StdProgress::done() ); 00304 } 00305 00306 QString QtopiaSocket::metaId() const 00307 { 00308 return d->partnerId; 00309 } 00310 00311 void QtopiaSocket::slotError( int err ) 00312 { 00313 d->isSyncing = false; 00314 d->isConnecting = false; 00315 kdDebug(5225) << "Error " << err << " for ip = " << d->dest << endl; 00316 00317 emit error( StdError::connectionLost() ); 00318 } 00319 00320 void QtopiaSocket::slotConnected() 00321 { 00322 emit prog( StdProgress::connection() ); 00323 d->connected = true; 00324 delete d->timer; 00325 d->mode = d->Start; 00326 } 00327 00328 void QtopiaSocket::slotClosed() 00329 { 00330 d->connected = false; 00331 d->isConnecting = false; 00332 d->isSyncing = false; 00333 emit error( StdError::connectionLost() ); 00334 } 00335 00336 void QtopiaSocket::slotNOOP() 00337 { 00338 if (!d->socket ) return; 00339 sendCommand( "NOOP" ); 00340 } 00341 00342 void QtopiaSocket::process() 00343 { 00344 while ( d->socket->canReadLine() ) { 00345 QTextStream stream( d->socket ); 00346 QString line = d->socket->readLine(); 00347 kdDebug() << "100O " << line << endl; 00348 //kdDebug(5225) << line << endl; 00349 //kdDebug(5225) << d->mode << endl; 00350 switch( d->mode ) { 00351 case QtopiaSocket::Private::Start: 00352 start(line); 00353 break; 00354 case QtopiaSocket::Private::User: 00355 user(line); 00356 break; 00357 case QtopiaSocket::Private::Pass: 00358 pass(line); 00359 break; 00360 case QtopiaSocket::Private::Call: 00361 call(line); 00362 break; 00363 case QtopiaSocket::Private::Noop: 00364 noop(line); 00365 break; 00366 default: 00367 break; 00368 } 00369 } 00370 } 00371 00372 void QtopiaSocket::slotStartSync() 00373 { 00374 emit prog( Progress( i18n("Starting to sync now") ) ); 00375 d->startSync = false; 00376 sendCommand( "call QPE/System sendHandshakeInfo()" ); 00377 d->getMode = d->Handshake; 00378 d->mode = d->Call; 00379 } 00380 00381 KURL QtopiaSocket::url( Type t ) 00382 { 00383 QString uri; 00384 uri = d->path + "/Applications/"; 00385 switch( t ) { 00386 case AddressBook: 00387 uri += "addressbook/addressbook.xml"; 00388 break; 00389 case TodoList: 00390 uri += "todolist/todolist.xml"; 00391 break; 00392 case DateBook: 00393 uri += "datebook/datebook.xml"; 00394 break; 00395 } 00396 return url( uri ); 00397 } 00398 00399 KURL QtopiaSocket::url( const QString& path ) 00400 { 00401 KURL url; 00402 url.setProtocol("ftp" ); 00403 url.setUser( d->device->user() ); 00404 url.setPass( d->device->password() ); 00405 url.setHost( d->dest ); 00406 url.setPort( 4242 ); 00407 url.setPath( path ); 00408 00409 return url; 00410 } 00411 00412 /* 00413 * write the categories file 00414 */ 00415 void QtopiaSocket::writeCategory() 00416 { 00417 QString fileName = QDir::homeDirPath() + "/.kitchensync/meta/" +d->partnerId + "/categories.xml"; 00418 d->edit->save( fileName ); 00419 KURL uri = url( d->path + "/Settings/Categories.xml" ); 00420 KIO::NetAccess::upload( fileName, uri, 0 ); 00421 } 00422 00423 void QtopiaSocket::writeAddressbook( AddressBookSyncee* syncee ) 00424 { 00425 emit prog(Progress(i18n("Writing AddressBook back to the device") ) ); 00426 OpieHelper::AddressBook abDB(d->edit, d->helper, d->tz, d->meta, d->device ); 00427 KTempFile* file = abDB.fromKDE( syncee, d->extras ); 00428 KURL uri = url( AddressBook ); 00429 00430 KIO::NetAccess::upload( file->name(), uri, 0 ); 00431 file->unlink(); 00432 delete file; 00433 00434 if ( d->meta ) { 00435 OpieHelper::MD5Map map(QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId + "/contacts.md5.qtopia"); 00436 OpieHelper::MetaAddressbook metaBook; 00437 metaBook.saveMeta( syncee, map ); 00438 map.save( ); 00439 } 00440 } 00441 00442 void QtopiaSocket::writeDatebook( CalendarSyncee* syncee ) 00443 { 00444 OpieHelper::DateBook dbDB(d->edit, d->helper, d->tz, d->meta, d->device ); 00445 KTempFile* file = dbDB.fromKDE( syncee, d->extras ); 00446 KURL uri = url( DateBook ); 00447 00448 KIO::NetAccess::upload( file->name(), uri, 0 ); 00449 file->unlink(); 00450 delete file; 00451 00452 if ( d->meta ) { 00453 OpieHelper::MD5Map map(QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId + "/datebook.md5.qtopia"); 00454 OpieHelper::MetaDatebook metaBook; 00455 metaBook.saveMeta( syncee, map ); 00456 map.save( ); 00457 } 00458 } 00459 00460 void QtopiaSocket::writeTodoList( CalendarSyncee* syncee) 00461 { 00462 OpieHelper::ToDo toDB(d->edit, d->helper, d->tz, d->meta, d->device ); 00463 KTempFile* file = toDB.fromKDE( syncee, d->extras ); 00464 KURL uri = url( TodoList ); 00465 00466 KIO::NetAccess::upload( file->name(), uri, 0 ); 00467 file->unlink(); 00468 delete file; 00469 00470 if ( d->meta ) { 00471 OpieHelper::MD5Map map(QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId + "/todolist.md5.qtopia"); 00472 OpieHelper::MetaTodo metaBook; 00473 metaBook.saveMeta( syncee, map ); 00474 map.save(); 00475 } 00476 } 00477 00478 void QtopiaSocket::readAddressbook() 00479 { 00480 KSync::AddressBookSyncee* syncee = 0; 00481 emit prog( StdProgress::downloading(i18n("Addressbook") ) ); 00482 QString tempfile; 00483 00484 if (!downloadFile( "/Applications/addressbook/addressbook.xml", tempfile ) ) { 00485 emit error( StdError::downloadError(i18n("Addressbook") ) ); 00486 syncee = new KSync::AddressBookSyncee; 00487 tempfile = QString::null; 00488 } 00489 00490 emit prog( StdProgress::converting(i18n("Addressbook") ) ); 00491 00492 if (!syncee) { 00493 OpieHelper::AddressBook abDB( d->edit, d->helper, d->tz, d->meta, d->device ); 00494 syncee = abDB.toKDE( tempfile, d->extras ); 00495 } 00496 00497 if (!syncee ) { 00498 KIO::NetAccess::removeTempFile( tempfile ); 00499 emit error( i18n("Cannot read the addressbook file. It is corrupted.") ); 00500 return; 00501 } 00502 00503 syncee->setFirstSync( d->first ); 00504 00505 /* 00506 * If in meta mode but not the first syncee 00507 * collect some meta infos 00508 */ 00509 if ( d->meta && !d->first ) { 00510 emit prog( Progress(i18n("Not first sync collecting the changes now") ) ); 00511 syncee->setSyncMode( KSync::Syncee::MetaMode ); 00512 OpieHelper::MD5Map map( QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId + "/contacts.md5.qtopia" ); 00513 OpieHelper::MetaAddressbook metaBook; 00514 metaBook.doMeta( syncee, map ); 00515 } 00516 d->m_sync.append( syncee ); 00517 00518 if (!tempfile.isEmpty() ) 00519 KIO::NetAccess::removeTempFile( tempfile ); 00520 } 00521 00522 CalendarSyncee *QtopiaSocket::defaultCalendarSyncee() 00523 { 00524 CalendarSyncee* syncee = d->m_sync.calendarSyncee(); 00525 if ( syncee == 0 ) 00526 syncee = new KSync::CalendarSyncee( new KCal::CalendarLocal() ); 00527 00528 return syncee; 00529 } 00530 00531 void QtopiaSocket::readDatebook() 00532 { 00533 KSync::CalendarSyncee* syncee = defaultCalendarSyncee(); 00534 emit prog( StdProgress::downloading(i18n("Datebook") ) ); 00535 QString tempfile; 00536 00537 bool ok = downloadFile( "/Applications/datebook/datebook.xml", tempfile ); 00538 if ( !ok ) { 00539 emit error( StdError::downloadError(i18n("Datebook") ) ); 00540 tempfile = QString::null; 00541 } 00542 emit prog( StdProgress::converting(i18n("Datebook") ) ); 00543 00544 /* the datebook.xml might not exist in this case we created an empty Entry 00545 * and there is no need to parse a non existint file 00546 */ 00547 if ( ok ) { 00548 OpieHelper::DateBook dateDB( d->edit, d->helper, d->tz, d->meta, d->device ); 00549 ok = dateDB.toKDE( tempfile, d->extras, syncee ); 00550 } 00551 00552 if ( !ok ) { 00553 KIO::NetAccess::removeTempFile( tempfile ); 00554 emit error( i18n("Cannot read the datebook file. It is corrupted.") ); 00555 return; 00556 } 00557 00558 syncee->setFirstSync( d->first ); 00559 00560 /* 00561 * for meta mode get meta info 00562 */ 00563 if ( d->meta && !d->first ) { 00564 emit prog( StdProgress::converting(i18n("Datebook") ) ); 00565 syncee->setSyncMode( KSync::Syncee::MetaMode ); 00566 OpieHelper::MD5Map map( QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId + "/datebook.md5.qtopia" ); 00567 OpieHelper::MetaDatebook metaBook; 00568 metaBook.doMeta( syncee, map ); 00569 kdDebug(5229) << "Did Meta" << endl; 00570 outputIt(5229, syncee ); 00571 } 00572 00573 if ( d->m_sync.find( syncee ) == d->m_sync.end() ) 00574 d->m_sync.append( syncee ); 00575 00576 if (!tempfile.isEmpty() ) 00577 KIO::NetAccess::removeTempFile( tempfile ); 00578 } 00579 00580 void QtopiaSocket::readTodoList() 00581 { 00582 KSync::CalendarSyncee* syncee = defaultCalendarSyncee(); 00583 QString tempfile; 00584 emit prog( StdProgress::downloading(i18n("TodoList") ) ); 00585 00586 bool ok = downloadFile( "/Applications/todolist/todolist.xml", tempfile ); 00587 if ( !ok ) { 00588 emit error( StdError::downloadError(i18n("TodoList") ) ); 00589 tempfile = QString::null; 00590 } 00591 00592 if ( ok ) { 00593 OpieHelper::ToDo toDB( d->edit, d->helper, d->tz, d->meta, d->device ); 00594 ok = toDB.toKDE( tempfile, d->extras, syncee ); 00595 } 00596 00597 if ( !ok ) { 00598 KIO::NetAccess::removeTempFile( tempfile ); 00599 emit error( i18n("Cannot read the TodoList file. It is corrupted.") ); 00600 return; 00601 } 00602 00603 syncee->setFirstSync( d->first ); 00604 00605 if ( d->meta && !d->first ) { 00606 emit prog( Progress(i18n("Not first sync collecting the changes now") ) ); 00607 syncee->setSyncMode( KSync::Syncee::MetaMode ); 00608 OpieHelper::MD5Map map( QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId + "/todolist.md5.qtopia" ); 00609 OpieHelper::MetaTodo metaBook; 00610 metaBook.doMeta( syncee, map ); 00611 kdDebug(5227) << "Did Meta " << endl; 00612 outputIt(5227, syncee ); 00613 } 00614 00615 if ( d->m_sync.find( syncee ) == d->m_sync.end() ) 00616 d->m_sync.append( syncee ); 00617 00618 if (!tempfile.isEmpty() ) 00619 KIO::NetAccess::removeTempFile( tempfile ); 00620 } 00621 00622 void QtopiaSocket::start( const QString& line ) 00623 { 00624 if ( line.left(3) != QString::fromLatin1("220") ) { 00625 emit error( Error(i18n("The device returned bogus data. giving up now.") ) ); 00626 // something went wrong 00627 d->socket->close(); 00628 d->mode = d->Done; 00629 d->connected = false; 00630 d->isConnecting = false; 00631 } else { 00632 /* 00633 * parse the uuid 00634 * here if not zaurus 00635 */ 00636 if( d->device->distribution() == OpieHelper::Device::Zaurus ){ 00637 d->partnerId = d->device->meta(); 00638 } else { 00639 QStringList list = QStringList::split(";", line ); 00640 QString uid = list[1]; 00641 uid = uid.mid(11, uid.length()-12 ); 00642 d->partnerId = uid; 00643 } 00644 initFiles(); 00645 sendCommand( "USER " + d->device->user() ); 00646 d->mode = d->User; 00647 } 00648 } 00649 00650 void QtopiaSocket::user( const QString &line ) 00651 { 00652 emit prog( StdProgress::connected() ); 00653 // emit prog( StdProgress::authentication() ); 00654 if ( line.left(3) != QString::fromLatin1("331") ) { 00655 emit error( StdError::wrongUser( d->device->user() ) ); 00656 // wrong user name 00657 d->socket->close(); 00658 d->mode = d->Done; 00659 d->connected = false; 00660 d->isConnecting = false; 00661 } else{ 00662 sendCommand( "PASS " + d->device->password() ); 00663 d->mode = d->Pass; 00664 } 00665 } 00666 00667 void QtopiaSocket::pass( const QString& line) 00668 { 00669 if ( line.left(3) != QString::fromLatin1("230") ) { 00670 emit error( StdError::wrongPassword() ); 00671 // wrong password 00672 d->socket->close(); 00673 d->mode = d->Done; 00674 d->connected = false; 00675 d->isConnecting = false; 00676 } else { 00677 emit prog( StdProgress::authenticated() ); 00678 kdDebug(5225) << "Konnected" << endl; 00679 d->mode = d->Noop; 00680 QTimer::singleShot(10000, this, SLOT(slotNOOP() ) ); 00681 } 00682 } 00683 00684 void QtopiaSocket::call( const QString& line) 00685 { 00686 if ( line.contains("220 Command okay" ) && 00687 ( d->getMode == d->Handshake || d->getMode == d->ABook ) ) 00688 return; 00689 00690 if ( line.startsWith("CALL QPE/Desktop docLinks(QString)") ) { 00691 emit prog( Progress(i18n("Getting the Document Links of the Document Tab") ) ); 00692 OpieHelper::Desktop desk( d->edit ); 00693 Syncee* sync = desk.toSyncee( line ); 00694 if ( sync ) 00695 d->m_sync.append( sync ); 00696 } 00697 00698 00699 switch( d->getMode ) { 00700 case QtopiaSocket::Private::Handshake: 00701 handshake( line ); 00702 break; 00703 case QtopiaSocket::Private::Flush: 00704 flush( line ); 00705 break; 00706 case QtopiaSocket::Private::ABook: 00707 download(); 00708 break; 00709 case QtopiaSocket::Private::Desktops: 00710 initSync( line ); 00711 break; 00712 } 00713 } 00714 00715 void QtopiaSocket::flush( const QString& _line ) 00716 { 00717 if ( _line.startsWith("CALL QPE/Desktop flushDone(QString)") || 00718 _line.startsWith("599 ChannelNotRegistered") ) { 00719 00720 QString line = _line.stripWhiteSpace(); 00721 QString appName; 00722 00723 if ( line.endsWith( "datebook" ) ) { 00724 readDatebook(); 00725 appName = i18n( "datebook" ); 00726 m_flushedApps++; 00727 } else if ( line.endsWith( "todolist" ) ) { 00728 readTodoList(); 00729 appName = i18n( "todolist" ); 00730 m_flushedApps++; 00731 } else if ( line.endsWith( "addressbook" ) ) { 00732 readAddressbook(); 00733 appName = i18n( "addressbook" ); 00734 m_flushedApps++; 00735 } 00736 emit prog( Progress( i18n( "Flushed " ) + appName ) ); 00737 } 00738 00739 /* all apps have been flushed or have not been running */ 00740 if ( m_flushedApps == 3 ) { 00741 /* 00742 * now we can progress during sync 00743 */ 00744 d->getMode = d->ABook; 00745 sendCommand( "call QPE/System getAllDocLinks()" ); 00746 m_flushedApps = 0; 00747 } 00748 } 00749 00750 void QtopiaSocket::noop( const QString & ) 00751 { 00752 d->isConnecting = false; 00753 if (!d->startSync ) { 00754 d->mode = d->Noop; 00755 QTimer::singleShot(10000, this, SLOT(slotNOOP() ) ); 00756 }else 00757 slotStartSync(); 00758 } 00759 00760 void QtopiaSocket::handshake( const QString &line ) 00761 { 00762 QStringList list = QStringList::split( QString::fromLatin1(" "), line ); 00763 d->path = list[3]; 00764 if (!d->path.isEmpty() ) { 00765 d->getMode = d->Desktops; 00766 sendCommand( "call QPE/System startSync(QString) KitchenSync" ); 00767 } 00768 } 00769 00770 void QtopiaSocket::download() 00771 { 00772 /* 00773 * we're all set now 00774 * start sync 00775 * and clear our list 00776 */ 00777 emit sync( d->m_sync ); 00778 d->mode = d->Noop; 00779 d->m_sync.clear(); 00780 } 00781 00782 void QtopiaSocket::initSync( const QString& ) 00783 { 00784 /* clear the extra map for the next round */ 00785 d->extras.clear(); 00786 emit prog( StdProgress::downloading("Categories.xml") ); 00787 QString tmpFileName; 00788 downloadFile( "/Settings/Categories.xml", tmpFileName ); 00789 00790 /* Category Edit */ 00791 delete d->edit; 00792 d->edit = new OpieHelper::CategoryEdit( tmpFileName ); 00793 KIO::NetAccess::removeTempFile( tmpFileName ); 00794 00795 /* KonnectorUIDHelper */ 00796 delete d->helper; 00797 d->helper = new KonnectorUIDHelper( partnerIdPath() ); 00798 00799 /* TimeZones */ 00800 readTimeZones(); 00801 00802 /* flush the data on pda side to make sure to get the latest version */ 00803 sendCommand( "call QPE/Application/datebook flush()" ); 00804 sendCommand( "call QPE/Application/addressbook flush()" ); 00805 sendCommand( "call QPE/Application/todolist flush()" ); 00806 d->getMode = d->Flush; 00807 } 00808 00809 void QtopiaSocket::initFiles() 00810 { 00811 QDir di( QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId ); 00812 /* 00813 * if our meta path exists do not recreate it 00814 */ 00815 if ( di.exists() ) { 00816 d->first = false; 00817 return; 00818 } 00819 00820 d->first = true; 00821 QDir dir; 00822 dir.mkdir(QDir::homeDirPath() + "/.kitchensync"); 00823 dir.mkdir(QDir::homeDirPath() + "/.kitchensync/meta"); 00824 dir.mkdir(QDir::homeDirPath() + "/.kitchensync/meta/" + d->partnerId ); 00825 } 00826 00827 QString QtopiaSocket::partnerIdPath() const 00828 { 00829 QString str = QDir::homeDirPath(); 00830 str += "/.kitchensync/meta/"; 00831 str += d->partnerId; 00832 00833 return str; 00834 } 00835 00836 /* 00837 * As long as Qtopia/Opie is broken 00838 * in regards to handling timezones and events 00839 * we set the TimeZone to the one from Korganizer 00840 * for evolution we need to fix that!!! 00841 * 00842 */ 00843 void QtopiaSocket::readTimeZones() 00844 { 00845 KConfig conf("korganizerrc"); 00846 conf.setGroup("Time & Date"); 00847 d->tz = conf.readEntry("TimeZoneId", QString::fromLatin1("UTC") ); 00848 kdDebug(5225) << "TimeZone of Korg is " << d->tz << endl; 00849 } 00850 00851 bool QtopiaSocket::downloadFile( const QString& str, QString& dest ) 00852 { 00853 KURL uri = url( d->path + str ); 00854 bool b = KIO::NetAccess::download( uri, dest, 0 ); 00855 kdDebug(5225) << "Getting " << str << " " << b << endl; 00856 return b; 00857 } 00858 00859 void QtopiaSocket::download( const QString& res ) 00860 { 00861 Q_UNUSED( res ); 00862 } 00863 00864 void QtopiaSocket::sendCommand( const QString& cmd ) 00865 { 00866 if ( !d->socket ) 00867 kdError() << "No socket available" << endl; 00868 00869 kdDebug() << "100I: " << cmd << endl; 00870 00871 QTextStream stream( d->socket ); 00872 stream << cmd << endl; 00873 } 00874 00875 namespace { 00876 00877 void forAll( int area, QPtrList<SyncEntry> list ) 00878 { 00879 for (SyncEntry* entry = list.first(); entry != 0; entry = list.next() ) { 00880 kdDebug(area) << "State " << entry->state() << endl; 00881 kdDebug(area) << "Summary " << entry->name() << endl; 00882 kdDebug(area) << "Uid " << entry->id() << endl; 00883 } 00884 } 00885 00886 void outputIt( int area, Syncee *s ) 00887 { 00888 kdDebug(area) << "Added entries" << endl; 00889 forAll( area, s->added() ); 00890 00891 kdDebug(area) << "Modified " <<endl; 00892 forAll( area, s->modified() ); 00893 00894 kdDebug(area) << "Removed " << endl; 00895 forAll( area, s->removed() ); 00896 } 00897 00898 } 00899 00900 #include "socket.moc"
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:18:59 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003