summaryrefslogtreecommitdiffabout
path: root/kaddressbook
Unidiff
Diffstat (limited to 'kaddressbook') (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/kabcore.cpp293
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
@@ -2506,80 +2506,341 @@ QString KABCore::getPassword( )
2506 connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) ); 2506 connect(&pb, SIGNAL( clicked() ), &dia, SLOT ( accept() ) );
2507 dia.show(); 2507 dia.show();
2508 int res = dia.exec(); 2508 int res = dia.exec();
2509 if ( res ) 2509 if ( res )
2510 retfile = lab.text(); 2510 retfile = lab.text();
2511 dia.hide(); 2511 dia.hide();
2512 qApp->processEvents(); 2512 qApp->processEvents();
2513 return retfile; 2513 return retfile;
2514 2514
2515} 2515}
2516bool KABCore::synchronizeAddressbooks( KABC::AddressBook* local, KABC::AddressBook* remote,int mode) 2516bool KABCore::synchronizeAddressbooks( KABC::AddressBook* local, KABC::AddressBook* remote,int mode)
2517{ 2517{
2518 2518#if 0
2519 bool syncOK = true;
2520 int addedAddressee = 0;
2521 int addedAddresseeR = 0;
2522 int deletedAddresseeR = 0;
2523 int deletedAddresseeL = 0;
2524 int changedLocal = 0;
2525 int changedRemote = 0;
2526 //QPtrList<Addressee> el = local->rawAddressees();
2527 Addressee* addresseeR;
2528 QString uid;
2529 int take;
2530 Addressee* addresseeL;
2531 Addressee* addresseeRSync;
2532 Addressee* addresseeLSync;
2533 QPtrList<Addressee> addresseeRSyncSharp = remote->getExternLastSyncAddressees();
2534 QPtrList<Addressee> addresseeLSyncSharp = local->getExternLastSyncAddressees();
2535 bool fullDateRange = false;
2536 local->resetTempSyncStat();
2537 mLastCalendarSync = QDateTime::currentDateTime();
2538 QDateTime modifiedCalendar = mLastCalendarSync;;
2539 addresseeLSync = getLastSyncAddressee();
2540 addresseeR = remote->addressee("last-syncAddressee-"+mCurrentSyncName );
2541 if ( addresseeR ) {
2542 addresseeRSync = (Addressee*) addresseeR->clone();
2543 remote->deleteAddressee(addresseeR );
2544
2545 } else {
2546 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) {
2547 addresseeRSync = (Addressee*)addresseeLSync->clone();
2548 } else {
2549 fullDateRange = true;
2550 addresseeRSync = new Addressee();
2551 addresseeRSync->setSummary(mCurrentSyncName + i18n(" - sync addressee"));
2552 addresseeRSync->setUid("last-syncAddressee-"+mCurrentSyncName );
2553 addresseeRSync->setDtStart( mLastCalendarSync );
2554 addresseeRSync->setDtEnd( mLastCalendarSync.addSecs( 7200 ) );
2555 addresseeRSync->setCategories( i18n("SyncAddressee") );
2556 }
2557 }
2558 if ( addresseeLSync->dtStart() == mLastCalendarSync )
2559 fullDateRange = true;
2560
2561 if ( ! fullDateRange ) {
2562 if ( addresseeLSync->dtStart() != addresseeRSync->dtStart() ) {
2563
2564 // qDebug("set fulldate to true %s %s" ,addresseeLSync->dtStart().toString().latin1(), addresseeRSync->dtStart().toString().latin1() );
2565 //qDebug("%d %d %d %d ", addresseeLSync->dtStart().time().second(), addresseeLSync->dtStart().time().msec() , addresseeRSync->dtStart().time().second(), addresseeRSync->dtStart().time().msec());
2566 fullDateRange = true;
2567 }
2568 }
2569 if ( fullDateRange )
2570 mLastCalendarSync = QDateTime::currentDateTime().addDays( -100*365);
2571 else
2572 mLastCalendarSync = addresseeLSync->dtStart();
2573 // for resyncing if own file has changed
2574 if ( mCurrentSyncDevice == "deleteaftersync" ) {
2575 mLastCalendarSync = loadedFileVersion;
2576 qDebug("setting mLastCalendarSync ");
2577 }
2578 //qDebug("*************************** ");
2579 qDebug("mLastCalendarSync %s ",mLastCalendarSync.toString().latin1() );
2580 QPtrList<Incidence> er = remote->rawIncidences();
2581 Incidence* inR = er.first();
2582 Incidence* inL;
2583 QProgressBar bar( er.count(),0 );
2584 bar.setCaption (i18n("Syncing - close to abort!") );
2585
2586 int w = 300;
2587 if ( QApplication::desktop()->width() < 320 )
2588 w = 220;
2589 int h = bar.sizeHint().height() ;
2590 int dw = QApplication::desktop()->width();
2591 int dh = QApplication::desktop()->height();
2592 bar.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h );
2593 bar.show();
2594 int modulo = (er.count()/10)+1;
2595 int incCounter = 0;
2596 while ( inR ) {
2597 if ( ! bar.isVisible() )
2598 return false;
2599 if ( incCounter % modulo == 0 )
2600 bar.setProgress( incCounter );
2601 ++incCounter;
2602 uid = inR->uid();
2603 bool skipIncidence = false;
2604 if ( uid.left(15) == QString("last-syncAddressee-") )
2605 skipIncidence = true;
2606 QString idS;
2607 qApp->processAddressees();
2608 if ( !skipIncidence ) {
2609 inL = local->incidence( uid );
2610 if ( inL ) { // maybe conflict - same uid in both calendars
2611 int maxrev = inL->revision();
2612 if ( maxrev < inR->revision() )
2613 maxrev = inR->revision();
2614 if ( (take = takeAddressee( inL, inR, mode, fullDateRange )) > 0 ) {
2615 //qDebug("take %d %s ", take, inL->summary().latin1());
2616 if ( take == 3 )
2617 return false;
2618 if ( take == 1 ) {// take local
2619 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL )
2620 inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) );
2621 else
2622 idS = inR->IDStr();
2623 remote->deleteIncidence( inR );
2624 if ( inL->revision() < maxrev )
2625 inL->setRevision( maxrev );
2626 inR = inL->clone();
2627 inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL );
2628 if ( mGlobalSyncMode != SYNC_MODE_EXTERNAL )
2629 inR->setIDStr( idS );
2630 remote->addIncidence( inR );
2631 ++changedRemote;
2632 } else {
2633 if ( inR->revision() < maxrev )
2634 inR->setRevision( maxrev );
2635 idS = inL->IDStr();
2636 local->deleteIncidence( inL );
2637 inL = inR->clone();
2638 inL->setIDStr( idS );
2639 local->addIncidence( inL );
2640 ++changedLocal;
2641 }
2642 }
2643 } else { // no conflict
2644 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) {
2645 QString des = addresseeLSync->description();
2646 QString pref = "e";
2647 if ( inR->type() == "Todo" )
2648 pref = "t";
2649 if ( des.find(pref+ inR->getID(mCurrentSyncDevice) +"," ) >= 0 && mode != 5) { // delete it
2650 inR->setTempSyncStat( SYNC_TEMPSTATE_DELETE );
2651 //remote->deleteIncidence( inR );
2652 ++deletedAddresseeR;
2653 } else {
2654 inR->setLastModified( modifiedCalendar );
2655 inL = inR->clone();
2656 local->addIncidence( inL );
2657 ++addedAddressee;
2658 }
2659 } else {
2660 if ( inR->lastModified() > mLastCalendarSync || mode == 5 ) {
2661 inR->setLastModified( modifiedCalendar );
2662 local->addIncidence( inR->clone() );
2663 ++addedAddressee;
2664 } else {
2665 checkExternSyncAddressee(addresseeRSyncSharp, inR);
2666 remote->deleteIncidence( inR );
2667 ++deletedAddresseeR;
2668 }
2669 }
2670 }
2671 }
2672 inR = er.next();
2673 }
2674 QPtrList<Incidence> el = local->rawIncidences();
2675 inL = el.first();
2676 modulo = (el.count()/10)+1;
2677 bar.setCaption (i18n("Add / remove addressees") );
2678 bar.setTotalSteps ( el.count() ) ;
2679 bar.show();
2680 incCounter = 0;
2681
2682 while ( inL ) {
2683
2684 qApp->processAddressees();
2685 if ( ! bar.isVisible() )
2686 return false;
2687 if ( incCounter % modulo == 0 )
2688 bar.setProgress( incCounter );
2689 ++incCounter;
2690 uid = inL->uid();
2691 bool skipIncidence = false;
2692 if ( uid.left(15) == QString("last-syncAddressee-") )
2693 skipIncidence = true;
2694 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL && inL->type() == "Journal" )
2695 skipIncidence = true;
2696 if ( !skipIncidence ) {
2697 inR = remote->incidence( uid );
2698 if ( ! inR ) {
2699 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) {
2700 if ( !inL->getID(mCurrentSyncDevice).isEmpty() && mode != 4 ) {
2701 checkExternSyncAddressee(addresseeLSyncSharp, inL);
2702 local->deleteIncidence( inL );
2703 ++deletedAddresseeL;
2704 } else {
2705 if ( ! KOPrefs::instance()->mWriteBackExistingOnly ) {
2706 inL->removeID(mCurrentSyncDevice );
2707 ++addedAddresseeR;
2708 //qDebug("remote added Incidence %s ", inL->summary().latin1());
2709 inL->setLastModified( modifiedCalendar );
2710 inR = inL->clone();
2711 inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL );
2712 remote->addIncidence( inR );
2713 }
2714 }
2715 } else {
2716 if ( inL->lastModified() < mLastCalendarSync && mode != 4 ) {
2717 checkExternSyncAddressee(addresseeLSyncSharp, inL);
2718 local->deleteIncidence( inL );
2719 ++deletedAddresseeL;
2720 } else {
2721 if ( ! KOPrefs::instance()->mWriteBackExistingOnly ) {
2722 ++addedAddresseeR;
2723 inL->setLastModified( modifiedCalendar );
2724 remote->addIncidence( inL->clone() );
2725 }
2726 }
2727 }
2728 }
2729 }
2730 inL = el.next();
2731 }
2732 int delFut = 0;
2733 if ( KOPrefs::instance()->mWriteBackInFuture ) {
2734 er = remote->rawIncidences();
2735 inR = er.first();
2736 QDateTime dt;
2737 QDateTime cur = QDateTime::currentDateTime();
2738 QDateTime end = cur.addSecs( KOPrefs::instance()->mWriteBackInFuture * 3600 *24 *7 );
2739 while ( inR ) {
2740 if ( inR->type() == "Todo" ) {
2741 Todo * t = (Todo*)inR;
2742 if ( t->hasDueDate() )
2743 dt = t->dtDue();
2744 else
2745 dt = cur.addSecs( 62 );
2746 }
2747 else if (inR->type() == "Addressee" ) {
2748 bool ok;
2749 dt = inR->getNextOccurence( cur, &ok );
2750 if ( !ok )
2751 dt = cur.addSecs( -62 );
2752 }
2753 else
2754 dt = inR->dtStart();
2755 if ( dt < cur || dt > end ) {
2756 remote->deleteIncidence( inR );
2757 ++delFut;
2758 }
2759 inR = er.next();
2760 }
2761 }
2762 bar.hide();
2763 mLastCalendarSync = QDateTime::currentDateTime().addSecs( 1 );
2764 addresseeLSync->setReadOnly( false );
2765 addresseeLSync->setDtStart( mLastCalendarSync );
2766 addresseeRSync->setDtStart( mLastCalendarSync );
2767 addresseeLSync->setDtEnd( mLastCalendarSync.addSecs( 3600 ) );
2768 addresseeRSync->setDtEnd( mLastCalendarSync.addSecs( 3600 ) );
2769 addresseeRSync->setLocation( i18n("Remote from: ")+mCurrentSyncName ) ;
2770 addresseeLSync->setLocation(i18n("Local from: ") + mCurrentSyncName );
2771 addresseeLSync->setReadOnly( true );
2772 if ( mGlobalSyncMode == SYNC_MODE_NORMAL)
2773 remote->addAddressee( addresseeRSync );
2774 QString mes;
2775 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 );
2776 QString delmess;
2777 if ( delFut ) {
2778 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 );
2779 mes += delmess;
2780 }
2781 if ( KOPrefs::instance()->mShowSyncSummary ) {
2782 KMessageBox::information(this, mes, i18n("KO/Pi Synchronization") );
2783 }
2784 qDebug( mes );
2785 mCalendar->checkAlarmForIncidence( 0, true );
2786 return syncOK;
2787#endif
2788 return false;
2519} 2789}
2520bool KABCore::syncAB(QString filename, int mode) 2790bool KABCore::syncAB(QString filename, int mode)
2521{ 2791{
2522 2792
2523 2793 //pending prepare addresseeview for output
2524 2794 //pending detect, if remote file has REV field. if not switch to external sync
2525 mGlobalSyncMode = SYNC_MODE_NORMAL; 2795 mGlobalSyncMode = SYNC_MODE_NORMAL;
2526 AddressBook abLocal(filename,"syncContact"); 2796 AddressBook abLocal(filename,"syncContact");
2527 bool syncOK = false; 2797 bool syncOK = false;
2528 if ( abLocal.load() ) { 2798 if ( abLocal.load() ) {
2529 qDebug("AB loaded %s mode %d",filename.latin1(), mode ); 2799 qDebug("AB loaded %s mode %d",filename.latin1(), mode );
2530 AddressBook::Iterator it; 2800 AddressBook::Iterator it;
2531 QStringList vcards; 2801 QStringList vcards;
2532 for ( it = abLocal.begin(); it != abLocal.end(); ++it ) { 2802 for ( it = abLocal.begin(); it != abLocal.end(); ++it ) {
2533 qDebug("Name %s ", (*it).familyName().latin1()); 2803 qDebug("Name %s ", (*it).familyName().latin1());
2534 } 2804 }
2535 syncOK = synchronizeAddressbooks( mAddressBook, &abLocal, mode ); 2805 syncOK = synchronizeAddressbooks( mAddressBook, &abLocal, mode );
2536 if ( syncOK ) { 2806 if ( syncOK ) {
2537 if ( KABPrefs::instance()->mWriteBackFile ) 2807 if ( KABPrefs::instance()->mWriteBackFile )
2538 { 2808 {
2809 qDebug("saving remote AB ");
2539 abLocal.saveAB(); 2810 abLocal.saveAB();
2540 } 2811 }
2541 } 2812 }
2542 setModified(); 2813 setModified();
2543 2814
2544 } 2815 }
2545 if ( syncOK ) 2816 if ( syncOK )
2546 ;//updateView(); 2817 mViewManager->refreshView();
2547 return syncOK; 2818 return syncOK;
2548#if 0 2819#if 0
2549 mGlobalSyncMode = SYNC_MODE_NORMAL; 2820
2550 CalendarLocal* calendar = new CalendarLocal();
2551 calendar->setTimeZoneId(KOPrefs::instance()->mTimeZoneId);
2552 FileStorage* storage = new FileStorage( calendar );
2553 bool syncOK = false;
2554 storage->setFileName( filename );
2555 // qDebug("loading ... ");
2556 if ( storage->load(KOPrefs::instance()->mUseQuicksave) ) { 2821 if ( storage->load(KOPrefs::instance()->mUseQuicksave) ) {
2557 getEventViewerDialog()->setSyncMode( true ); 2822 getEventViewerDialog()->setSyncMode( true );
2558 syncOK = synchronizeCalendar( mCalendar, calendar, mode ); 2823 syncOK = synchronizeCalendar( mCalendar, calendar, mode );
2559 getEventViewerDialog()->setSyncMode( false ); 2824 getEventViewerDialog()->setSyncMode( false );
2560 if ( syncOK ) { 2825 if ( syncOK ) {
2561 if ( KOPrefs::instance()->mWriteBackFile ) 2826 if ( KOPrefs::instance()->mWriteBackFile )
2562 { 2827 {
2563 storage->setSaveFormat( new ICalFormat( KOPrefs::instance()->mUseQuicksave) ); 2828 storage->setSaveFormat( new ICalFormat( KOPrefs::instance()->mUseQuicksave) );
2564 storage->save(); 2829 storage->save();
2565 } 2830 }
2566 } 2831 }
2567 setModified(); 2832 setModified();
2568 } 2833 }
2569 delete storage; 2834
2570 delete calendar;
2571 if ( syncOK )
2572 updateView();
2573 return syncOK;
2574#endif 2835#endif
2575} 2836}
2576 2837
2577 2838
2578void KABCore::confSync() 2839void KABCore::confSync()
2579{ 2840{
2580 static KSyncPrefsDialog* sp = 0; 2841 static KSyncPrefsDialog* sp = 0;
2581 if ( ! sp ) { 2842 if ( ! sp ) {
2582 sp = new KSyncPrefsDialog( this, "syncprefs", true ); 2843 sp = new KSyncPrefsDialog( this, "syncprefs", true );
2583 } 2844 }
2584 sp->usrReadConfig(); 2845 sp->usrReadConfig();
2585#ifndef DESKTOP_VERSION 2846#ifndef DESKTOP_VERSION