-rw-r--r-- | kaddressbook/kabcore.cpp | 293 |
1 files changed, 277 insertions, 16 deletions
diff --git a/kaddressbook/kabcore.cpp b/kaddressbook/kabcore.cpp index e912941..1196360 100644 --- a/kaddressbook/kabcore.cpp +++ b/kaddressbook/kabcore.cpp @@ -2494,104 +2494,365 @@ QString KABCore::getPassword( ) QString retfile = ""; QDialog dia ( this, "input-dialog", true ); QLineEdit lab ( &dia ); lab.setEchoMode( QLineEdit::Password ); QVBoxLayout lay( &dia ); lay.setMargin(7); lay.setSpacing(7); lay.addWidget( &lab); dia.setFixedSize( 230,50 ); dia.setCaption( i18n("Enter password") ); QPushButton pb ( "OK", &dia); lay.addWidget( &pb ); connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); dia.show(); int res = dia.exec(); if ( res ) retfile = lab.text(); dia.hide(); qApp->processEvents(); return retfile; } bool KABCore::synchronizeAddressbooks( KABC::AddressBook* local, KABC::AddressBook* remote,int mode) { - +#if 0 + bool syncOK = true; + int addedAddressee = 0; + int addedAddresseeR = 0; + int deletedAddresseeR = 0; + int deletedAddresseeL = 0; + int changedLocal = 0; + int changedRemote = 0; + //QPtrList<Addressee> el = local->rawAddressees(); + Addressee* addresseeR; + QString uid; + int take; + Addressee* addresseeL; + Addressee* addresseeRSync; + Addressee* addresseeLSync; + QPtrList<Addressee> addresseeRSyncSharp = remote->getExternLastSyncAddressees(); + QPtrList<Addressee> addresseeLSyncSharp = local->getExternLastSyncAddressees(); + bool fullDateRange = false; + local->resetTempSyncStat(); + mLastCalendarSync = QDateTime::currentDateTime(); + QDateTime modifiedCalendar = mLastCalendarSync;; + addresseeLSync = getLastSyncAddressee(); + addresseeR = remote->addressee("last-syncAddressee-"+mCurrentSyncName ); + if ( addresseeR ) { + addresseeRSync = (Addressee*) addresseeR->clone(); + remote->deleteAddressee(addresseeR ); + + } else { + if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) { + addresseeRSync = (Addressee*)addresseeLSync->clone(); + } else { + fullDateRange = true; + addresseeRSync = new Addressee(); + addresseeRSync->setSummary(mCurrentSyncName + i18n(" - sync addressee")); + addresseeRSync->setUid("last-syncAddressee-"+mCurrentSyncName ); + addresseeRSync->setDtStart( mLastCalendarSync ); + addresseeRSync->setDtEnd( mLastCalendarSync.addSecs( 7200 ) ); + addresseeRSync->setCategories( i18n("SyncAddressee") ); + } + } + if ( addresseeLSync->dtStart() == mLastCalendarSync ) + fullDateRange = true; + + if ( ! fullDateRange ) { + if ( addresseeLSync->dtStart() != addresseeRSync->dtStart() ) { + + // qDebug("set fulldate to true %s %s" ,addresseeLSync->dtStart().toString().latin1(), addresseeRSync->dtStart().toString().latin1() ); + //qDebug("%d %d %d %d ", addresseeLSync->dtStart().time().second(), addresseeLSync->dtStart().time().msec() , addresseeRSync->dtStart().time().second(), addresseeRSync->dtStart().time().msec()); + fullDateRange = true; + } + } + if ( fullDateRange ) + mLastCalendarSync = QDateTime::currentDateTime().addDays( -100*365); + else + mLastCalendarSync = addresseeLSync->dtStart(); + // for resyncing if own file has changed + if ( mCurrentSyncDevice == "deleteaftersync" ) { + mLastCalendarSync = loadedFileVersion; + qDebug("setting mLastCalendarSync "); + } + //qDebug("*************************** "); + qDebug("mLastCalendarSync %s ",mLastCalendarSync.toString().latin1() ); + QPtrList<Incidence> er = remote->rawIncidences(); + Incidence* inR = er.first(); + Incidence* inL; + QProgressBar bar( er.count(),0 ); + bar.setCaption (i18n("Syncing - close to abort!") ); + + int w = 300; + if ( QApplication::desktop()->width() < 320 ) + w = 220; + int h = bar.sizeHint().height() ; + int dw = QApplication::desktop()->width(); + int dh = QApplication::desktop()->height(); + bar.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h ); + bar.show(); + int modulo = (er.count()/10)+1; + int incCounter = 0; + while ( inR ) { + if ( ! bar.isVisible() ) + return false; + if ( incCounter % modulo == 0 ) + bar.setProgress( incCounter ); + ++incCounter; + uid = inR->uid(); + bool skipIncidence = false; + if ( uid.left(15) == QString("last-syncAddressee-") ) + skipIncidence = true; + QString idS; + qApp->processAddressees(); + if ( !skipIncidence ) { + inL = local->incidence( uid ); + if ( inL ) { // maybe conflict - same uid in both calendars + int maxrev = inL->revision(); + if ( maxrev < inR->revision() ) + maxrev = inR->revision(); + if ( (take = takeAddressee( inL, inR, mode, fullDateRange )) > 0 ) { + //qDebug("take %d %s ", take, inL->summary().latin1()); + if ( take == 3 ) + return false; + if ( take == 1 ) {// take local + if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) + inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) ); + else + idS = inR->IDStr(); + remote->deleteIncidence( inR ); + if ( inL->revision() < maxrev ) + inL->setRevision( maxrev ); + inR = inL->clone(); + inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL ); + if ( mGlobalSyncMode != SYNC_MODE_EXTERNAL ) + inR->setIDStr( idS ); + remote->addIncidence( inR ); + ++changedRemote; + } else { + if ( inR->revision() < maxrev ) + inR->setRevision( maxrev ); + idS = inL->IDStr(); + local->deleteIncidence( inL ); + inL = inR->clone(); + inL->setIDStr( idS ); + local->addIncidence( inL ); + ++changedLocal; + } + } + } else { // no conflict + if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) { + QString des = addresseeLSync->description(); + QString pref = "e"; + if ( inR->type() == "Todo" ) + pref = "t"; + if ( des.find(pref+ inR->getID(mCurrentSyncDevice) +"," ) >= 0 && mode != 5) { // delete it + inR->setTempSyncStat( SYNC_TEMPSTATE_DELETE ); + //remote->deleteIncidence( inR ); + ++deletedAddresseeR; + } else { + inR->setLastModified( modifiedCalendar ); + inL = inR->clone(); + local->addIncidence( inL ); + ++addedAddressee; + } + } else { + if ( inR->lastModified() > mLastCalendarSync || mode == 5 ) { + inR->setLastModified( modifiedCalendar ); + local->addIncidence( inR->clone() ); + ++addedAddressee; + } else { + checkExternSyncAddressee(addresseeRSyncSharp, inR); + remote->deleteIncidence( inR ); + ++deletedAddresseeR; + } + } + } + } + inR = er.next(); + } + QPtrList<Incidence> el = local->rawIncidences(); + inL = el.first(); + modulo = (el.count()/10)+1; + bar.setCaption (i18n("Add / remove addressees") ); + bar.setTotalSteps ( el.count() ) ; + bar.show(); + incCounter = 0; + + while ( inL ) { + + qApp->processAddressees(); + if ( ! bar.isVisible() ) + return false; + if ( incCounter % modulo == 0 ) + bar.setProgress( incCounter ); + ++incCounter; + uid = inL->uid(); + bool skipIncidence = false; + if ( uid.left(15) == QString("last-syncAddressee-") ) + skipIncidence = true; + if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL && inL->type() == "Journal" ) + skipIncidence = true; + if ( !skipIncidence ) { + inR = remote->incidence( uid ); + if ( ! inR ) { + if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) { + if ( !inL->getID(mCurrentSyncDevice).isEmpty() && mode != 4 ) { + checkExternSyncAddressee(addresseeLSyncSharp, inL); + local->deleteIncidence( inL ); + ++deletedAddresseeL; + } else { + if ( ! KOPrefs::instance()->mWriteBackExistingOnly ) { + inL->removeID(mCurrentSyncDevice ); + ++addedAddresseeR; + //qDebug("remote added Incidence %s ", inL->summary().latin1()); + inL->setLastModified( modifiedCalendar ); + inR = inL->clone(); + inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL ); + remote->addIncidence( inR ); + } + } + } else { + if ( inL->lastModified() < mLastCalendarSync && mode != 4 ) { + checkExternSyncAddressee(addresseeLSyncSharp, inL); + local->deleteIncidence( inL ); + ++deletedAddresseeL; + } else { + if ( ! KOPrefs::instance()->mWriteBackExistingOnly ) { + ++addedAddresseeR; + inL->setLastModified( modifiedCalendar ); + remote->addIncidence( inL->clone() ); + } + } + } + } + } + inL = el.next(); + } + int delFut = 0; + if ( KOPrefs::instance()->mWriteBackInFuture ) { + er = remote->rawIncidences(); + inR = er.first(); + QDateTime dt; + QDateTime cur = QDateTime::currentDateTime(); + QDateTime end = cur.addSecs( KOPrefs::instance()->mWriteBackInFuture * 3600 *24 *7 ); + while ( inR ) { + if ( inR->type() == "Todo" ) { + Todo * t = (Todo*)inR; + if ( t->hasDueDate() ) + dt = t->dtDue(); + else + dt = cur.addSecs( 62 ); + } + else if (inR->type() == "Addressee" ) { + bool ok; + dt = inR->getNextOccurence( cur, &ok ); + if ( !ok ) + dt = cur.addSecs( -62 ); + } + else + dt = inR->dtStart(); + if ( dt < cur || dt > end ) { + remote->deleteIncidence( inR ); + ++delFut; + } + inR = er.next(); + } + } + bar.hide(); + mLastCalendarSync = QDateTime::currentDateTime().addSecs( 1 ); + addresseeLSync->setReadOnly( false ); + addresseeLSync->setDtStart( mLastCalendarSync ); + addresseeRSync->setDtStart( mLastCalendarSync ); + addresseeLSync->setDtEnd( mLastCalendarSync.addSecs( 3600 ) ); + addresseeRSync->setDtEnd( mLastCalendarSync.addSecs( 3600 ) ); + addresseeRSync->setLocation( i18n("Remote from: ")+mCurrentSyncName ) ; + addresseeLSync->setLocation(i18n("Local from: ") + mCurrentSyncName ); + addresseeLSync->setReadOnly( true ); + if ( mGlobalSyncMode == SYNC_MODE_NORMAL) + remote->addAddressee( addresseeRSync ); + QString mes; + mes .sprintf( i18n("Synchronization summary:\n\n %d items added to local\n %d items added to remote\n %d items updated on local\n %d items updated on remote\n %d items deleted on local\n %d items deleted on remote\n"),addedAddressee, addedAddresseeR, changedLocal, changedRemote, deletedAddresseeL, deletedAddresseeR ); + QString delmess; + if ( delFut ) { + delmess.sprintf( i18n("%d items skipped on remote,\nbecause they are in the past or\nmore than %d weeks in the future.\n"),delFut, KOPrefs::instance()->mWriteBackInFuture ); + mes += delmess; + } + if ( KOPrefs::instance()->mShowSyncSummary ) { + KMessageBox::information(this, mes, i18n("KO/Pi Synchronization") ); + } + qDebug( mes ); + mCalendar->checkAlarmForIncidence( 0, true ); + return syncOK; +#endif + return false; } bool KABCore::syncAB(QString filename, int mode) { - - + //pending prepare addresseeview for output + //pending detect, if remote file has REV field. if not switch to external sync mGlobalSyncMode = SYNC_MODE_NORMAL; AddressBook abLocal(filename,"syncContact"); bool syncOK = false; if ( abLocal.load() ) { qDebug("AB loaded %s mode %d",filename.latin1(), mode ); AddressBook::Iterator it; QStringList vcards; for ( it = abLocal.begin(); it != abLocal.end(); ++it ) { qDebug("Name %s ", (*it).familyName().latin1()); } syncOK = synchronizeAddressbooks( mAddressBook, &abLocal, mode ); if ( syncOK ) { if ( KABPrefs::instance()->mWriteBackFile ) { + qDebug("saving remote AB "); abLocal.saveAB(); } } setModified(); } if ( syncOK ) - ;//updateView(); + mViewManager->refreshView(); return syncOK; #if 0 - mGlobalSyncMode = SYNC_MODE_NORMAL; - CalendarLocal* calendar = new CalendarLocal(); - calendar->setTimeZoneId(KOPrefs::instance()->mTimeZoneId); - FileStorage* storage = new FileStorage( calendar ); - bool syncOK = false; - storage->setFileName( filename ); - // qDebug("loading ... "); + if ( storage->load(KOPrefs::instance()->mUseQuicksave) ) { getEventViewerDialog()->setSyncMode( true ); syncOK = synchronizeCalendar( mCalendar, calendar, mode ); getEventViewerDialog()->setSyncMode( false ); if ( syncOK ) { if ( KOPrefs::instance()->mWriteBackFile ) { storage->setSaveFormat( new ICalFormat( KOPrefs::instance()->mUseQuicksave) ); storage->save(); } } setModified(); } - delete storage; - delete calendar; - if ( syncOK ) - updateView(); - return syncOK; + #endif } void KABCore::confSync() { static KSyncPrefsDialog* sp = 0; if ( ! sp ) { sp = new KSyncPrefsDialog( this, "syncprefs", true ); } sp->usrReadConfig(); #ifndef DESKTOP_VERSION sp->showMaximized(); #else sp->show(); #endif sp->exec(); KABPrefs::instance()->mSyncProfileNames = sp->getSyncProfileNames(); KABPrefs::instance()->mLocalMachineName = sp->getLocalMachineName (); fillSyncMenu(); } void KABCore::syncSharp() { if ( mModified ) |