summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--korganizer/calendarview.cpp44
-rw-r--r--libkcal/calendarlocal.cpp27
2 files changed, 41 insertions, 30 deletions
diff --git a/korganizer/calendarview.cpp b/korganizer/calendarview.cpp
index 1800cf2..8385bcc 100644
--- a/korganizer/calendarview.cpp
+++ b/korganizer/calendarview.cpp
@@ -1606,305 +1606,311 @@ void CalendarView::checkExternalId( Incidence * inc )
1606} 1606}
1607// SSSSSSSSSSSSSSSSSSSSSS 1607// SSSSSSSSSSSSSSSSSSSSSS
1608bool CalendarView::synchronizeCalendar( Calendar* local, Calendar* remote, int mode ) 1608bool CalendarView::synchronizeCalendar( Calendar* local, Calendar* remote, int mode )
1609{ 1609{
1610 1610
1611 bool syncOK = true; 1611 bool syncOK = true;
1612 int addedEvent = 0; 1612 int addedEvent = 0;
1613 int addedEventR = 0; 1613 int addedEventR = 0;
1614 int deletedEventR = 0; 1614 int deletedEventR = 0;
1615 int deletedEventL = 0; 1615 int deletedEventL = 0;
1616 int changedLocal = 0; 1616 int changedLocal = 0;
1617 int changedRemote = 0; 1617 int changedRemote = 0;
1618 int filteredIN = 0; 1618 int filteredIN = 0;
1619 int filteredOUT = 0; 1619 int filteredOUT = 0;
1620 //QPtrList<Event> el = local->rawEvents(); 1620 //QPtrList<Event> el = local->rawEvents();
1621 Event* eventR; 1621 Event* eventR;
1622 QString uid; 1622 QString uid;
1623 int take; 1623 int take;
1624 Event* eventRSync; 1624 Event* eventRSync;
1625 Event* eventLSync; 1625 Event* eventLSync;
1626 clearAllViews(); 1626 clearAllViews();
1627 QPtrList<Event> eventRSyncSharp = remote->getExternLastSyncEvents(); 1627 QPtrList<Event> eventRSyncSharp = remote->getExternLastSyncEvents();
1628 QPtrList<Event> eventLSyncSharp = local->getExternLastSyncEvents(); 1628 QPtrList<Event> eventLSyncSharp = local->getExternLastSyncEvents();
1629 bool fullDateRange = false; 1629 bool fullDateRange = false;
1630 local->resetTempSyncStat(); 1630 local->resetTempSyncStat();
1631 mLastCalendarSync = QDateTime::currentDateTime(); 1631 mLastCalendarSync = QDateTime::currentDateTime();
1632 if ( mSyncManager->syncWithDesktop() ) { 1632 if ( mSyncManager->syncWithDesktop() ) {
1633 remote->resetPilotStat(1); 1633 remote->resetPilotStat(1);
1634 if ( KSyncManager::mRequestedSyncEvent.isValid() ) { 1634 if ( KSyncManager::mRequestedSyncEvent.isValid() ) {
1635 mLastCalendarSync = KSyncManager::mRequestedSyncEvent; 1635 mLastCalendarSync = KSyncManager::mRequestedSyncEvent;
1636 qDebug("KO: using extern time for calendar sync: %s ", mLastCalendarSync.toString().latin1() ); 1636 qDebug("KO: using extern time for calendar sync: %s ", mLastCalendarSync.toString().latin1() );
1637 } else { 1637 } else {
1638 qDebug("KSyncManager::mRequestedSyncEvent has invalid datatime "); 1638 qDebug("KSyncManager::mRequestedSyncEvent has invalid datatime ");
1639 } 1639 }
1640 } 1640 }
1641 QDateTime modifiedCalendar = mLastCalendarSync; 1641 QDateTime modifiedCalendar = mLastCalendarSync;
1642 eventLSync = getLastSyncEvent(); 1642 eventLSync = getLastSyncEvent();
1643 eventR = remote->event("last-syncEvent-"+mCurrentSyncName ); 1643 eventR = remote->event("last-syncEvent-"+mCurrentSyncName );
1644 if ( eventR ) { 1644 if ( eventR ) {
1645 qDebug("KO: Last-syncEvent on remote found "); 1645 qDebug("KO: Last-syncEvent on remote found ");
1646 eventRSync = (Event*) eventR->clone(); 1646 eventRSync = (Event*) eventR->clone();
1647 remote->deleteEvent(eventR ); 1647 remote->deleteEvent(eventR );
1648 1648
1649 } else { 1649 } else {
1650 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL || mSyncManager->syncWithDesktop()) { 1650 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL || mSyncManager->syncWithDesktop()) {
1651 eventRSync = (Event*)eventLSync->clone(); 1651 eventRSync = (Event*)eventLSync->clone();
1652 } else { 1652 } else {
1653 fullDateRange = true; 1653 fullDateRange = true;
1654 eventRSync = new Event(); 1654 eventRSync = new Event();
1655 eventRSync->setSummary(mCurrentSyncName + i18n(" - sync event")); 1655 eventRSync->setSummary(mCurrentSyncName + i18n(" - sync event"));
1656 eventRSync->setUid("last-syncEvent-"+mCurrentSyncName ); 1656 eventRSync->setUid("last-syncEvent-"+mCurrentSyncName );
1657 eventRSync->setDtStart( mLastCalendarSync ); 1657 eventRSync->setDtStart( mLastCalendarSync );
1658 eventRSync->setDtEnd( mLastCalendarSync.addSecs( 7200 ) ); 1658 eventRSync->setDtEnd( mLastCalendarSync.addSecs( 7200 ) );
1659 eventRSync->setCategories( i18n("SyncEvent") ); 1659 eventRSync->setCategories( i18n("SyncEvent") );
1660 } 1660 }
1661 } 1661 }
1662 if ( eventLSync->dtStart() == mLastCalendarSync ) 1662 if ( eventLSync->dtStart() == mLastCalendarSync )
1663 fullDateRange = true; 1663 fullDateRange = true;
1664 1664
1665 if ( ! fullDateRange ) { 1665 if ( ! fullDateRange ) {
1666 if ( eventLSync->dtStart() != eventRSync->dtStart() ) { 1666 if ( eventLSync->dtStart() != eventRSync->dtStart() ) {
1667 1667
1668 qDebug("KO: Sync: Set fulldate to true! Local: %s --- Remote: %s" ,eventLSync->dtStart().toString().latin1(), eventRSync->dtStart().toString().latin1() ); 1668 qDebug("KO: Sync: Set fulldate to true! Local: %s --- Remote: %s" ,eventLSync->dtStart().toString().latin1(), eventRSync->dtStart().toString().latin1() );
1669 //qDebug("%d %d %d %d ", eventLSync->dtStart().time().second(), eventLSync->dtStart().time().msec() , eventRSync->dtStart().time().second(), eventRSync->dtStart().time().msec()); 1669 //qDebug("%d %d %d %d ", eventLSync->dtStart().time().second(), eventLSync->dtStart().time().msec() , eventRSync->dtStart().time().second(), eventRSync->dtStart().time().msec());
1670 fullDateRange = true; 1670 fullDateRange = true;
1671 } 1671 }
1672 } 1672 }
1673 if ( mSyncManager->syncWithDesktop() ) { 1673 if ( mSyncManager->syncWithDesktop() ) {
1674 fullDateRange = ( eventLSync->dtStart() <= mLastCalendarSync && eventLSync->dtStart().addSecs(1) >= mLastCalendarSync ); 1674 fullDateRange = ( eventLSync->dtStart() <= mLastCalendarSync && eventLSync->dtStart().addSecs(1) >= mLastCalendarSync );
1675 } 1675 }
1676 if ( fullDateRange ) 1676 if ( fullDateRange )
1677 mLastCalendarSync = QDateTime::currentDateTime().addDays( -100*365); 1677 mLastCalendarSync = QDateTime::currentDateTime().addDays( -100*365);
1678 else 1678 else
1679 mLastCalendarSync = eventLSync->dtStart(); 1679 mLastCalendarSync = eventLSync->dtStart();
1680 // for resyncing if own file has changed 1680 // for resyncing if own file has changed
1681 if ( mCurrentSyncDevice == "deleteaftersync" ) { 1681 if ( mCurrentSyncDevice == "deleteaftersync" ) {
1682 mLastCalendarSync = loadedFileVersion; 1682 mLastCalendarSync = loadedFileVersion;
1683 //qDebug("setting mLastCalendarSync "); 1683 //qDebug("setting mLastCalendarSync ");
1684 } 1684 }
1685 //qDebug("*************************** "); 1685 //qDebug("*************************** ");
1686 qDebug("KO: mLastCalendarSync %s .Full: %d",mLastCalendarSync.toString().latin1(), fullDateRange); 1686 qDebug("KO: mLastCalendarSync %s .Full: %d",mLastCalendarSync.toString().latin1(), fullDateRange);
1687 QPtrList<Incidence> er = remote->rawIncidences(); 1687 QPtrList<Incidence> er = remote->rawIncidences();
1688 Incidence* inR = er.first(); 1688 Incidence* inR = er.first();
1689 Incidence* inL; 1689 Incidence* inL;
1690 QProgressBar bar( er.count(),0 ); 1690 QProgressBar bar( er.count(),0 );
1691 bar.setCaption (i18n("Syncing - close to abort!") ); 1691 bar.setCaption (i18n("Syncing - close to abort!") );
1692 1692
1693 // ************** setting up filter ************* 1693 // ************** setting up filter *************
1694 CalFilter *filterIN = 0; 1694 CalFilter *filterIN = 0;
1695 CalFilter *filterOUT = 0; 1695 CalFilter *filterOUT = 0;
1696 CalFilter *filter = mFilters.first(); 1696 CalFilter *filter = mFilters.first();
1697 while(filter) { 1697 while(filter) {
1698 if ( filter->name() == mSyncManager->mFilterInCal ) 1698 if ( filter->name() == mSyncManager->mFilterInCal )
1699 filterIN = filter; 1699 filterIN = filter;
1700 if ( filter->name() == mSyncManager->mFilterOutCal ) 1700 if ( filter->name() == mSyncManager->mFilterOutCal )
1701 filterOUT = filter; 1701 filterOUT = filter;
1702 filter = mFilters.next(); 1702 filter = mFilters.next();
1703 } 1703 }
1704 int w = 300; 1704 int w = 300;
1705 if ( QApplication::desktop()->width() < 320 ) 1705 if ( QApplication::desktop()->width() < 320 )
1706 w = 220; 1706 w = 220;
1707 int h = bar.sizeHint().height() ; 1707 int h = bar.sizeHint().height() ;
1708 int dw = QApplication::desktop()->width(); 1708 int dw = QApplication::desktop()->width();
1709 int dh = QApplication::desktop()->height(); 1709 int dh = QApplication::desktop()->height();
1710 bar.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h ); 1710 bar.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h );
1711 bar.show(); 1711 bar.show();
1712 int modulo = (er.count()/10)+1; 1712 int modulo = (er.count()/10)+1;
1713 int incCounter = 0; 1713 int incCounter = 0;
1714 while ( inR ) { 1714 while ( inR ) {
1715 if ( ! bar.isVisible() ) 1715 if ( ! bar.isVisible() )
1716 return false; 1716 return false;
1717 if ( incCounter % modulo == 0 ) 1717 if ( incCounter % modulo == 0 )
1718 bar.setProgress( incCounter ); 1718 bar.setProgress( incCounter );
1719 ++incCounter; 1719 ++incCounter;
1720 uid = inR->uid(); 1720 uid = inR->uid();
1721 bool skipIncidence = false; 1721 bool skipIncidence = false;
1722 if ( uid.left(15) == QString("last-syncEvent-") ) 1722 if ( uid.left(15) == QString("last-syncEvent-") )
1723 skipIncidence = true; 1723 skipIncidence = true;
1724 QString idS; 1724 QString idS;
1725 qApp->processEvents(); 1725 qApp->processEvents();
1726 if ( !skipIncidence ) { 1726 if ( !skipIncidence ) {
1727 int hasCalId = 0; 1727 int hasCalId = 0;
1728 inL = local->incidenceForUid( uid , false , true, &hasCalId ); 1728 inL = local->incidenceForUid( uid , false , true, &hasCalId );
1729 if ( hasCalId && !inL ) 1729 if ( hasCalId && !inL )
1730 inL = local->incidenceForUid( uid , false , true, &hasCalId ); 1730 inL = local->incidenceForUid( uid , false , true, &hasCalId );
1731 else 1731 else
1732 hasCalId = 0; 1732 hasCalId = 0;
1733 if ( inL ) { // maybe conflict - same uid in both calendars 1733 if ( inL ) { // maybe conflict - same uid in both calendars
1734 if ( hasCalId ) 1734
1735 qDebug("KO: Cal id %d conflict detected: %s ", hasCalId, inL->summary().latin1());
1736 if ( (take = takeEvent( inL, inR, mode, fullDateRange )) > 0 ) { 1735 if ( (take = takeEvent( inL, inR, mode, fullDateRange )) > 0 ) {
1737 //qDebug("take %d %s ", take, inL->summary().latin1()); 1736 //qDebug("take %d %s ", take, inL->summary().latin1());
1738 if ( take == 3 ) 1737 if ( take == 3 )
1739 return false; 1738 return false;
1740 if ( take == 1 ) {// take local ********************** 1739 if ( take == 1 ) {// take local **********************
1741 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) 1740 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL )
1742 inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) ); 1741 inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) );
1743 else 1742 else
1744 idS = inR->IDStr(); 1743 idS = inR->IDStr();
1745 int calID = inR->calID(); 1744 int calID = inR->calID();
1746 remote->deleteIncidence( inR ); 1745 remote->deleteIncidence( inR );
1747 inR = inL->clone(); 1746 if ( !hasCalId ) {
1748 inR->setCalID_block( calID ); 1747 inR = inL->clone();
1749 inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL ); 1748 inR->setCalID_block( calID );
1750 if ( mGlobalSyncMode != SYNC_MODE_EXTERNAL ) 1749 inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL );
1751 inR->setIDStr( idS ); 1750 if ( mGlobalSyncMode != SYNC_MODE_EXTERNAL )
1752 remote->addIncidence( inR ); 1751 inR->setIDStr( idS );
1753 if ( mSyncManager->syncWithDesktop() ) 1752 remote->addIncidence( inR );
1754 inR->setPilotId( 2 ); 1753 if ( mSyncManager->syncWithDesktop() )
1755 ++changedRemote; 1754 inR->setPilotId( 2 );
1755 ++changedRemote;
1756 } else
1757 ++deletedEventR;
1756 } else {// take remote ********************** 1758 } else {// take remote **********************
1757 if ( !inL->isReadOnly() ) { 1759 if ( !inL->isReadOnly() ) {
1758 idS = inL->IDStr(); 1760 idS = inL->IDStr();
1759 int pid = inL->pilotId(); 1761 int pid = inL->pilotId();
1760 int calID = inL->calID(); 1762 int calID = inL->calID();
1761 if ( hasCalId ) 1763 if ( hasCalId )
1762 calID = hasCalId; 1764 calID = 0;// add to default calendar
1763 local->deleteIncidence( inL ); 1765 local->deleteIncidence( inL );
1764 inL = inR->clone(); 1766 inL = inR->clone();
1765 inL->setCalID_block( calID ); 1767 inL->setCalID_block( calID );
1766 if ( mSyncManager->syncWithDesktop() ) 1768 if ( mSyncManager->syncWithDesktop() )
1767 inL->setPilotId( pid ); 1769 inL->setPilotId( pid );
1768 inL->setIDStr( idS ); 1770 inL->setIDStr( idS );
1769 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) { 1771 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) {
1770 inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) ); 1772 inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) );
1771 inL->setID( mCurrentSyncDevice, inR->getID(mCurrentSyncDevice) ); 1773 inL->setID( mCurrentSyncDevice, inR->getID(mCurrentSyncDevice) );
1772 } 1774 }
1773 local->addIncidence( inL ); 1775 local->addIncidence( inL );
1774 ++changedLocal; 1776 ++changedLocal;
1775 } 1777 }
1776 } 1778 }
1777 } else { 1779 } else {
1778 // take == 0; events equal 1780 // take == 0; events equal
1779 if ( hasCalId ) 1781 if ( hasCalId ) {
1780 qDebug("EV EQUALLLL **************************** "); 1782 qDebug("KO: Changing Cal id %d to %d for : %s ", hasCalId, local->defaultCalendar(),inL->summary().latin1());
1783 inL->setCalID( local->defaultCalendar() );
1784 }
1781 1785
1782 } 1786 }
1787
1788
1783 } else { // no conflict ********** add or delete remote 1789 } else { // no conflict ********** add or delete remote
1784 if ( !filterIN || filterIN->filterCalendarItem( inR ) ){ 1790 if ( !filterIN || filterIN->filterCalendarItem( inR ) ){
1785 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) { 1791 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) {
1786 QString des = eventLSync->description(); 1792 QString des = eventLSync->description();
1787 QString pref = "e"; 1793 QString pref = "e";
1788 if ( inR->typeID() == todoID ) 1794 if ( inR->typeID() == todoID )
1789 pref = "t"; 1795 pref = "t";
1790 if ( des.find(pref+ inR->getID(mCurrentSyncDevice) +"," ) >= 0 && mode != 5) { // delete it 1796 if ( des.find(pref+ inR->getID(mCurrentSyncDevice) +"," ) >= 0 && mode != 5) { // delete it
1791 inR->setTempSyncStat( SYNC_TEMPSTATE_DELETE ); 1797 inR->setTempSyncStat( SYNC_TEMPSTATE_DELETE );
1792 //remote->deleteIncidence( inR ); 1798 //remote->deleteIncidence( inR );
1793 ++deletedEventR; 1799 ++deletedEventR;
1794 } else { 1800 } else {
1795 inR->setLastModified( modifiedCalendar ); 1801 inR->setLastModified( modifiedCalendar );
1796 inL = inR->clone(); 1802 inL = inR->clone();
1797 inL->setIDStr( ":" ); 1803 inL->setIDStr( ":" );
1798 inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) ); 1804 inL->setCsum( mCurrentSyncDevice, inR->getCsum(mCurrentSyncDevice) );
1799 inL->setID( mCurrentSyncDevice, inR->getID(mCurrentSyncDevice) ); 1805 inL->setID( mCurrentSyncDevice, inR->getID(mCurrentSyncDevice) );
1800 1806
1801 inL->setCalID_block( 0 );// add to default cal 1807 inL->setCalID_block( 0 );// add to default cal
1802 local->addIncidence( inL ); 1808 local->addIncidence( inL );
1803 ++addedEvent; 1809 ++addedEvent;
1804 1810
1805 } 1811 }
1806 } else { 1812 } else {
1807 if ( inR->lastModified() > mLastCalendarSync || mode == 5 ) { 1813 if ( inR->lastModified() > mLastCalendarSync || mode == 5 ) {
1808 inR->setLastModified( modifiedCalendar ); 1814 inR->setLastModified( modifiedCalendar );
1809 inL = inR->clone(); 1815 inL = inR->clone();
1810 inL->setIDStr( ":" ); 1816 inL->setIDStr( ":" );
1811 inL->setCalID_block( 0 );// add to default cal 1817 inL->setCalID_block( 0 );// add to default cal
1812 local->addIncidence( inL ); 1818 local->addIncidence( inL );
1813 ++addedEvent; 1819 ++addedEvent;
1814 1820
1815 } else { 1821 } else {
1816 checkExternSyncEvent(eventRSyncSharp, inR); 1822 checkExternSyncEvent(eventRSyncSharp, inR);
1817 remote->deleteIncidence( inR ); 1823 remote->deleteIncidence( inR );
1818 ++deletedEventR; 1824 ++deletedEventR;
1819 } 1825 }
1820 } 1826 }
1821 } else { 1827 } else {
1822 ++filteredIN; 1828 ++filteredIN;
1823 } 1829 }
1824 } 1830 }
1825 } 1831 }
1826 inR = er.next(); 1832 inR = er.next();
1827 } 1833 }
1828 QPtrList<Incidence> el = local->rawIncidences(); 1834 QPtrList<Incidence> el = local->rawIncidences();
1829 inL = el.first(); 1835 inL = el.first();
1830 modulo = (el.count()/10)+1; 1836 modulo = (el.count()/10)+1;
1831 bar.setCaption (i18n("Add / remove events") ); 1837 bar.setCaption (i18n("Add / remove events") );
1832 bar.setTotalSteps ( el.count() ) ; 1838 bar.setTotalSteps ( el.count() ) ;
1833 bar.show(); 1839 bar.show();
1834 incCounter = 0; 1840 incCounter = 0;
1835 1841
1836 while ( inL ) { 1842 while ( inL ) {
1837 1843
1838 qApp->processEvents(); 1844 qApp->processEvents();
1839 if ( ! bar.isVisible() ) 1845 if ( ! bar.isVisible() )
1840 return false; 1846 return false;
1841 if ( incCounter % modulo == 0 ) 1847 if ( incCounter % modulo == 0 )
1842 bar.setProgress( incCounter ); 1848 bar.setProgress( incCounter );
1843 ++incCounter; 1849 ++incCounter;
1844 uid = inL->uid(); 1850 uid = inL->uid();
1845 bool skipIncidence = false; 1851 bool skipIncidence = false;
1846 if ( uid.left(15) == QString("last-syncEvent-") ) 1852 if ( uid.left(15) == QString("last-syncEvent-") )
1847 skipIncidence = true; 1853 skipIncidence = true;
1848 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL && inL->typeID() == journalID ) 1854 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL && inL->typeID() == journalID )
1849 skipIncidence = true; 1855 skipIncidence = true;
1850 if ( !skipIncidence ) { 1856 if ( !skipIncidence ) {
1851 inR = remote->incidenceForUid( uid , true, true ); 1857 inR = remote->incidenceForUid( uid , true, true );
1852 if ( ! inR ) { 1858 if ( ! inR ) {
1853 if ( !filterOUT || filterOUT->filterCalendarItem( inL ) ){ 1859 if ( !filterOUT || filterOUT->filterCalendarItem( inL ) ){
1854 // no conflict ********** add or delete local 1860 // no conflict ********** add or delete local
1855 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) { 1861 if ( mGlobalSyncMode == SYNC_MODE_EXTERNAL ) {
1856 if ( !inL->getID(mCurrentSyncDevice).isEmpty() && mode != 4 ) { 1862 if ( !inL->getID(mCurrentSyncDevice).isEmpty() && mode != 4 ) {
1857 checkExternSyncEvent(eventLSyncSharp, inL); 1863 checkExternSyncEvent(eventLSyncSharp, inL);
1858 local->deleteIncidence( inL ); 1864 local->deleteIncidence( inL );
1859 ++deletedEventL; 1865 ++deletedEventL;
1860 } else { 1866 } else {
1861 if ( ! mSyncManager->mWriteBackExistingOnly ) { 1867 if ( ! mSyncManager->mWriteBackExistingOnly ) {
1862 inL->removeID(mCurrentSyncDevice ); 1868 inL->removeID(mCurrentSyncDevice );
1863 ++addedEventR; 1869 ++addedEventR;
1864 //qDebug("remote added Incidence %s ", inL->summary().latin1()); 1870 //qDebug("remote added Incidence %s ", inL->summary().latin1());
1865 inL->setLastModified( modifiedCalendar ); 1871 inL->setLastModified( modifiedCalendar );
1866 inR = inL->clone(); 1872 inR = inL->clone();
1867 inR->setIDStr( ":" ); 1873 inR->setIDStr( ":" );
1868 inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL ); 1874 inR->setTempSyncStat( SYNC_TEMPSTATE_INITIAL );
1869 inR->setCalID_block( 0 );// add to default cal 1875 inR->setCalID_block( 0 );// add to default cal
1870 remote->addIncidence( inR ); 1876 remote->addIncidence( inR );
1871 } 1877 }
1872 } 1878 }
1873 } else { 1879 } else {
1874 if ( inL->lastModified() < mLastCalendarSync && mode != 4 ) { 1880 if ( inL->lastModified() < mLastCalendarSync && mode != 4 ) {
1875 checkExternSyncEvent(eventLSyncSharp, inL); 1881 checkExternSyncEvent(eventLSyncSharp, inL);
1876 local->deleteIncidence( inL ); 1882 local->deleteIncidence( inL );
1877 ++deletedEventL; 1883 ++deletedEventL;
1878 } else { 1884 } else {
1879 if ( ! mSyncManager->mWriteBackExistingOnly ) { 1885 if ( ! mSyncManager->mWriteBackExistingOnly ) {
1880 ++addedEventR; 1886 ++addedEventR;
1881 inL->setLastModified( modifiedCalendar ); 1887 inL->setLastModified( modifiedCalendar );
1882 inR = inL->clone(); 1888 inR = inL->clone();
1883 inR->setIDStr( ":" ); 1889 inR->setIDStr( ":" );
1884 inR->setCalID_block( 0 );// add to default cal 1890 inR->setCalID_block( 0 );// add to default cal
1885 remote->addIncidence( inR ); 1891 remote->addIncidence( inR );
1886 } 1892 }
1887 } 1893 }
1888 } 1894 }
1889 } else { 1895 } else {
1890 ++filteredOUT; 1896 ++filteredOUT;
1891 } 1897 }
1892 } 1898 }
1893 } 1899 }
1894 inL = el.next(); 1900 inL = el.next();
1895 } 1901 }
1896 int delFut = 0; 1902 int delFut = 0;
1897 int remRem = 0; 1903 int remRem = 0;
1898 if ( mSyncManager->mWriteBackInFuture ) { 1904 if ( mSyncManager->mWriteBackInFuture ) {
1899 er = remote->rawIncidences(); 1905 er = remote->rawIncidences();
1900 remRem = er.count(); 1906 remRem = er.count();
1901 inR = er.first(); 1907 inR = er.first();
1902 QDateTime dt; 1908 QDateTime dt;
1903 QDateTime cur = QDateTime::currentDateTime().addDays( -(mSyncManager->mWriteBackInPast * 7) ); 1909 QDateTime cur = QDateTime::currentDateTime().addDays( -(mSyncManager->mWriteBackInPast * 7) );
1904 QDateTime end = QDateTime::currentDateTime().addDays( (mSyncManager->mWriteBackInFuture ) *7 ); 1910 QDateTime end = QDateTime::currentDateTime().addDays( (mSyncManager->mWriteBackInFuture ) *7 );
1905 while ( inR ) { 1911 while ( inR ) {
1906 if ( inR->typeID() == todoID ) { 1912 if ( inR->typeID() == todoID ) {
1907 Todo * t = (Todo*)inR; 1913 Todo * t = (Todo*)inR;
1908 if ( t->hasDueDate() ) 1914 if ( t->hasDueDate() )
1909 dt = t->dtDue(); 1915 dt = t->dtDue();
1910 else 1916 else
@@ -2357,266 +2363,262 @@ bool CalendarView::loadCalendars()
2357 return true; 2363 return true;
2358} 2364}
2359bool CalendarView::restoreCalendarSettings() 2365bool CalendarView::restoreCalendarSettings()
2360{ 2366{
2361 QPtrList<KopiCalendarFile> calendars = KOPrefs::instance()->mCalendars; 2367 QPtrList<KopiCalendarFile> calendars = KOPrefs::instance()->mCalendars;
2362 KopiCalendarFile * cal = calendars.first(); 2368 KopiCalendarFile * cal = calendars.first();
2363 while ( cal ) { 2369 while ( cal ) {
2364 mCalendar->setCalendarEnabled( cal->mCalNumber,cal->isEnabled ); 2370 mCalendar->setCalendarEnabled( cal->mCalNumber,cal->isEnabled );
2365 mCalendar->setAlarmEnabled( cal->mCalNumber, cal->isAlarmEnabled ); 2371 mCalendar->setAlarmEnabled( cal->mCalNumber, cal->isAlarmEnabled );
2366 mCalendar->setReadOnly( cal->mCalNumber, cal->isReadOnly ); 2372 mCalendar->setReadOnly( cal->mCalNumber, cal->isReadOnly );
2367 if ( cal->isStandard ) 2373 if ( cal->isStandard )
2368 mCalendar->setDefaultCalendar( cal->mCalNumber ); 2374 mCalendar->setDefaultCalendar( cal->mCalNumber );
2369 cal = calendars.next(); 2375 cal = calendars.next();
2370 } 2376 }
2371 setSyncEventsReadOnly(); 2377 setSyncEventsReadOnly();
2372 mCalendar->reInitAlarmSettings(); 2378 mCalendar->reInitAlarmSettings();
2373 updateUnmanagedViews(); 2379 updateUnmanagedViews();
2374 updateView(); 2380 updateView();
2375 return true; 2381 return true;
2376} 2382}
2377void CalendarView::addCalendarId( int id ) 2383void CalendarView::addCalendarId( int id )
2378{ 2384{
2379 KopiCalendarFile * cal = KOPrefs::instance()->getCalendar( id ); 2385 KopiCalendarFile * cal = KOPrefs::instance()->getCalendar( id );
2380 if ( cal ) 2386 if ( cal )
2381 addCalendar( cal ); 2387 addCalendar( cal );
2382} 2388}
2383bool CalendarView::addCalendar( KopiCalendarFile * cal ) 2389bool CalendarView::addCalendar( KopiCalendarFile * cal )
2384{ 2390{
2385 cal->mErrorOnLoad = false; 2391 cal->mErrorOnLoad = false;
2386 if ( mCalendar->addCalendarFile( cal->mFileName, cal->mCalNumber )) { 2392 if ( mCalendar->addCalendarFile( cal->mFileName, cal->mCalNumber )) {
2387 cal->mLoadDt = QDateTime::currentDateTime(); 2393 cal->mLoadDt = QDateTime::currentDateTime();
2388 return true; 2394 return true;
2389 } 2395 }
2390 qDebug("KO: Error adding calendar file %s ",cal->mFileName.latin1() ); 2396 qDebug("KO: Error adding calendar file %s ",cal->mFileName.latin1() );
2391 cal->mErrorOnLoad = true; 2397 cal->mErrorOnLoad = true;
2392 return false; 2398 return false;
2393} 2399}
2394bool CalendarView::openCalendar(QString filename, bool merge) 2400bool CalendarView::openCalendar(QString filename, bool merge)
2395{ 2401{
2396 2402
2397 if (filename.isEmpty()) { 2403 if (filename.isEmpty()) {
2398 return false; 2404 return false;
2399 } 2405 }
2400 2406
2401 if (!QFile::exists(filename)) { 2407 if (!QFile::exists(filename)) {
2402 KMessageBox::error(this,i18n("File does not exist:\n '%1'.").arg(filename)); 2408 KMessageBox::error(this,i18n("File does not exist:\n '%1'.").arg(filename));
2403 return false; 2409 return false;
2404 } 2410 }
2405 2411
2406 globalFlagBlockAgenda = 1; 2412 globalFlagBlockAgenda = 1;
2407 clearAllViews(); 2413 clearAllViews();
2408 if (!merge) { 2414 if (!merge) {
2409 mViewManager->setDocumentId( filename ); 2415 mViewManager->setDocumentId( filename );
2410 mCalendar->close(); 2416 mCalendar->close();
2411 } 2417 }
2412 mStorage->setFileName( filename ); 2418 mStorage->setFileName( filename );
2413 2419
2414 if ( mStorage->load() ) { 2420 if ( mStorage->load() ) {
2415 if ( merge ) ;//setModified( true ); 2421 if ( merge ) ;//setModified( true );
2416 else { 2422 else {
2417 //setModified( true ); 2423 //setModified( true );
2418 mViewManager->setDocumentId( filename ); 2424 mViewManager->setDocumentId( filename );
2419 mDialogManager->setDocumentId( filename ); 2425 mDialogManager->setDocumentId( filename );
2420 mTodoList->setDocumentId( filename ); 2426 mTodoList->setDocumentId( filename );
2421 } 2427 }
2422 globalFlagBlockAgenda = 2; 2428 globalFlagBlockAgenda = 2;
2423 // if ( getLastSyncEvent() ) 2429 // if ( getLastSyncEvent() )
2424 // getLastSyncEvent()->setReadOnly( true ); 2430 // getLastSyncEvent()->setReadOnly( true );
2425 mCalendar->reInitAlarmSettings(); 2431 mCalendar->reInitAlarmSettings();
2426 setSyncEventsReadOnly(); 2432 setSyncEventsReadOnly();
2427 //updateUnmanagedViews(); 2433 //updateUnmanagedViews();
2428 //updateView(); 2434 //updateView();
2429 if ( filename != MainWindow::defaultFileName() ) { 2435 if ( filename != MainWindow::defaultFileName() ) {
2430 saveCalendar( MainWindow::defaultFileName() ); 2436 saveCalendar( MainWindow::defaultFileName() );
2431 } else { 2437 } else {
2432 QFileInfo finf ( MainWindow::defaultFileName()); 2438 QFileInfo finf ( MainWindow::defaultFileName());
2433 if ( finf.exists() ) { 2439 if ( finf.exists() ) {
2434 setLoadedFileVersion( finf.lastModified () ); 2440 setLoadedFileVersion( finf.lastModified () );
2435 } 2441 }
2436 } 2442 }
2437 return true; 2443 return true;
2438 } else { 2444 } else {
2439 // while failing to load, the calendar object could 2445 // while failing to load, the calendar object could
2440 // have become partially populated. Clear it out. 2446 // have become partially populated. Clear it out.
2441 if ( !merge ) { 2447 if ( !merge ) {
2442 mCalendar->close(); 2448 mCalendar->close();
2443 mViewManager->setDocumentId( filename ); 2449 mViewManager->setDocumentId( filename );
2444 mDialogManager->setDocumentId( filename ); 2450 mDialogManager->setDocumentId( filename );
2445 mTodoList->setDocumentId( filename ); 2451 mTodoList->setDocumentId( filename );
2446 } 2452 }
2447 2453
2448 //KMessageBox::error(this,i18n("Couldn't load calendar\n '%1'.").arg(filename)); 2454 //KMessageBox::error(this,i18n("Couldn't load calendar\n '%1'.").arg(filename));
2449 2455
2450 QTimer::singleShot ( 1, this, SLOT ( showOpenError() ) ); 2456 QTimer::singleShot ( 1, this, SLOT ( showOpenError() ) );
2451 globalFlagBlockAgenda = 2; 2457 globalFlagBlockAgenda = 2;
2452 mCalendar->reInitAlarmSettings(); 2458 mCalendar->reInitAlarmSettings();
2453 setSyncEventsReadOnly(); 2459 setSyncEventsReadOnly();
2454 updateUnmanagedViews(); 2460 updateUnmanagedViews();
2455 updateView(); 2461 updateView();
2456 } 2462 }
2457 return false; 2463 return false;
2458} 2464}
2459void CalendarView::mergeFile( QString fn ) 2465void CalendarView::mergeFile( QString fn )
2460{ 2466{
2461 clearAllViews(); 2467 clearAllViews();
2462 mCalendar->mergeCalendarFile( fn ); 2468 mCalendar->mergeCalendarFile( fn );
2463 mCalendar->reInitAlarmSettings(); 2469 mCalendar->reInitAlarmSettings();
2464 setSyncEventsReadOnly(); 2470 setSyncEventsReadOnly();
2465 updateUnmanagedViews(); 2471 updateUnmanagedViews();
2466 updateView(); 2472 updateView();
2467} 2473}
2468void CalendarView::mergeFileResource( QString fn ,QString resource ) 2474void CalendarView::mergeFileResource( QString fn ,QString resource )
2469{ 2475{
2470 2476
2471 if ( resource == "ALL" ) { 2477 if ( resource == "ALL" ) {
2472 mCalendar->setAllCalendarEnabled( true ); 2478 mCalendar->setAllCalendarEnabled( true );
2473 mergeFile( fn ); 2479 mergeFile( fn );
2474 restoreCalendarSettings(); 2480 restoreCalendarSettings();
2475 return; 2481 return;
2476 } 2482 }
2477 2483
2478 int exclusiveResource = KOPrefs::instance()->getFuzzyCalendarID( resource ); 2484 int exclusiveResource = KOPrefs::instance()->getFuzzyCalendarID( resource );
2479 if ( !exclusiveResource ) { 2485 if ( !exclusiveResource ) {
2480 qDebug("KO: CalendarView::mergeFileResource: resource not found %s", resource.latin1() ); 2486 qDebug("KO: CalendarView::mergeFileResource: resource not found %s", resource.latin1() );
2481 return; 2487 return;
2482 } 2488 }
2483 clearAllViews(); 2489 clearAllViews();
2484 mCalendar->setCalendarRemove( exclusiveResource ); 2490 mCalendar->setCalendarRemove( exclusiveResource );
2485 int def = mCalendar->defaultCalendar();
2486 mCalendar->setDefaultCalendar(exclusiveResource); 2491 mCalendar->setDefaultCalendar(exclusiveResource);
2492 mCalendar->setAllCalendarEnabled( true );
2487 if ( !mCalendar->addCalendarFile( fn, exclusiveResource )) { 2493 if ( !mCalendar->addCalendarFile( fn, exclusiveResource )) {
2488 qDebug("KO: CalendarView::mergeFileResource: error adding file %s", fn.latin1() ); 2494 qDebug("KO: CalendarView::mergeFileResource: error adding file %s", fn.latin1() );
2489 } 2495 }
2490 mCalendar->setDefaultCalendar( def ); 2496 restoreCalendarSettings();
2491 mCalendar->reInitAlarmSettings();
2492 setSyncEventsReadOnly();
2493 updateUnmanagedViews();
2494 updateView();
2495} 2497}
2496void CalendarView::showOpenError() 2498void CalendarView::showOpenError()
2497{ 2499{
2498 KMessageBox::error(this,i18n("Couldn't load calendar\n.")); 2500 KMessageBox::error(this,i18n("Couldn't load calendar\n."));
2499} 2501}
2500void CalendarView::setLoadedFileVersion(QDateTime dt) 2502void CalendarView::setLoadedFileVersion(QDateTime dt)
2501{ 2503{
2502 loadedFileVersion = dt; 2504 loadedFileVersion = dt;
2503} 2505}
2504bool CalendarView::checkFileChanged(QString fn) 2506bool CalendarView::checkFileChanged(QString fn)
2505{ 2507{
2506 QFileInfo finf ( fn ); 2508 QFileInfo finf ( fn );
2507 if ( !finf.exists() ) 2509 if ( !finf.exists() )
2508 return true; 2510 return true;
2509 QDateTime dt = finf.lastModified (); 2511 QDateTime dt = finf.lastModified ();
2510 if ( dt <= loadedFileVersion ) 2512 if ( dt <= loadedFileVersion )
2511 return false; 2513 return false;
2512 return true; 2514 return true;
2513 2515
2514} 2516}
2515void CalendarView::watchSavedFile() 2517void CalendarView::watchSavedFile()
2516{ 2518{
2517 QFileInfo finf ( MainWindow::defaultFileName()); 2519 QFileInfo finf ( MainWindow::defaultFileName());
2518 if ( !finf.exists() ) 2520 if ( !finf.exists() )
2519 return; 2521 return;
2520 QDateTime dt = finf.lastModified (); 2522 QDateTime dt = finf.lastModified ();
2521 if ( dt < loadedFileVersion ) { 2523 if ( dt < loadedFileVersion ) {
2522 //qDebug("watch %s %s ", dt.toString().latin1(), loadedFileVersion.toString().latin1()); 2524 //qDebug("watch %s %s ", dt.toString().latin1(), loadedFileVersion.toString().latin1());
2523 QTimer::singleShot( 1000 , this, SLOT ( watchSavedFile() ) ); 2525 QTimer::singleShot( 1000 , this, SLOT ( watchSavedFile() ) );
2524 return; 2526 return;
2525 } 2527 }
2526 loadedFileVersion = dt; 2528 loadedFileVersion = dt;
2527} 2529}
2528bool CalendarView::checkAllFileVersions() 2530bool CalendarView::checkAllFileVersions()
2529{ 2531{
2530 QPtrList<KopiCalendarFile> calendars = KOPrefs::instance()->mCalendars; 2532 QPtrList<KopiCalendarFile> calendars = KOPrefs::instance()->mCalendars;
2531 KopiCalendarFile * cal = calendars.first(); 2533 KopiCalendarFile * cal = calendars.first();
2532 mCalendar->setDefaultCalendar( 1 ); 2534 mCalendar->setDefaultCalendar( 1 );
2533 mCalendar->setDefaultCalendarEnabledOnly(); 2535 mCalendar->setDefaultCalendarEnabledOnly();
2534 if ( !cal->isReadOnly && !cal->mErrorOnLoad ) { 2536 if ( !cal->isReadOnly && !cal->mErrorOnLoad ) {
2535 if ( !checkFileVersion(MainWindow::defaultFileName())) { 2537 if ( !checkFileVersion(MainWindow::defaultFileName())) {
2536 restoreCalendarSettings(); 2538 restoreCalendarSettings();
2537 return false; 2539 return false;
2538 } 2540 }
2539 } 2541 }
2540 cal = calendars.next(); 2542 cal = calendars.next();
2541 QDateTime storeTemp = loadedFileVersion; 2543 QDateTime storeTemp = loadedFileVersion;
2542 while ( cal ) { 2544 while ( cal ) {
2543 if ( !cal->isReadOnly && !cal->mErrorOnLoad ) { 2545 if ( !cal->isReadOnly && !cal->mErrorOnLoad ) {
2544 mCalendar->setDefaultCalendar( cal->mCalNumber ); 2546 mCalendar->setDefaultCalendar( cal->mCalNumber );
2545 mCalendar->setDefaultCalendarEnabledOnly(); 2547 mCalendar->setDefaultCalendarEnabledOnly();
2546 loadedFileVersion = cal->mLoadDt.addSecs( 15 ); 2548 loadedFileVersion = cal->mLoadDt.addSecs( 15 );
2547 if ( !checkFileVersion(cal->mFileName )) { 2549 if ( !checkFileVersion(cal->mFileName )) {
2548 loadedFileVersion = storeTemp; 2550 loadedFileVersion = storeTemp;
2549 restoreCalendarSettings(); 2551 restoreCalendarSettings();
2550 return false; 2552 return false;
2551 } 2553 }
2552 } 2554 }
2553 cal = calendars.next(); 2555 cal = calendars.next();
2554 } 2556 }
2555 loadedFileVersion = storeTemp; 2557 loadedFileVersion = storeTemp;
2556 return true; 2558 return true;
2557} 2559}
2558bool CalendarView::checkFileVersion(QString fn) 2560bool CalendarView::checkFileVersion(QString fn)
2559{ 2561{
2560 QFileInfo finf ( fn ); 2562 QFileInfo finf ( fn );
2561 if ( !finf.exists() ) 2563 if ( !finf.exists() )
2562 return true; 2564 return true;
2563 QDateTime dt = finf.lastModified (); 2565 QDateTime dt = finf.lastModified ();
2564 //qDebug("loaded file version %s %s", fn.latin1(), loadedFileVersion.toString().latin1()); 2566 //qDebug("loaded file version %s %s", fn.latin1(), loadedFileVersion.toString().latin1());
2565 //qDebug("file on disk version %s %s", fn.latin1(),dt.toString().latin1()); 2567 //qDebug("file on disk version %s %s", fn.latin1(),dt.toString().latin1());
2566 if ( dt <= loadedFileVersion ) 2568 if ( dt <= loadedFileVersion )
2567 return true; 2569 return true;
2568 int km = KMessageBox::warningYesNoCancel(this, i18n("\nThe file\n%1\non disk has changed!\nFile size: %2 bytes.\nLast modified: %3\nDo you want to:\n\n - Save and overwrite file?\n - Sync with file, then save?\n - Cancel without saving? \n").arg(KGlobal::formatMessage(fn,0)).arg( QString::number( finf.size())).arg( KGlobal::locale()->formatDateTime(finf.lastModified (), true, true)) , 2570 int km = KMessageBox::warningYesNoCancel(this, i18n("\nThe file\n%1\non disk has changed!\nFile size: %2 bytes.\nLast modified: %3\nDo you want to:\n\n - Save and overwrite file?\n - Sync with file, then save?\n - Cancel without saving? \n").arg(KGlobal::formatMessage(fn,0)).arg( QString::number( finf.size())).arg( KGlobal::locale()->formatDateTime(finf.lastModified (), true, true)) ,
2569 i18n("KO/Pi Warning"),i18n("Overwrite"), 2571 i18n("KO/Pi Warning"),i18n("Overwrite"),
2570 i18n("Sync+save")); 2572 i18n("Sync+save"));
2571 2573
2572 if ( km == KMessageBox::Cancel ) 2574 if ( km == KMessageBox::Cancel )
2573 return false; 2575 return false;
2574 if ( km == KMessageBox::Yes ) 2576 if ( km == KMessageBox::Yes )
2575 return true; 2577 return true;
2576 2578
2577 setSyncDevice("deleteaftersync" ); 2579 setSyncDevice("deleteaftersync" );
2578 mSyncManager->mAskForPreferences = true; 2580 mSyncManager->mAskForPreferences = true;
2579 mSyncManager->mSyncAlgoPrefs = 3; 2581 mSyncManager->mSyncAlgoPrefs = 3;
2580 mSyncManager->mWriteBackFile = false; 2582 mSyncManager->mWriteBackFile = false;
2581 mSyncManager->mWriteBackExistingOnly = false; 2583 mSyncManager->mWriteBackExistingOnly = false;
2582 mSyncManager->mShowSyncSummary = false; 2584 mSyncManager->mShowSyncSummary = false;
2583 mMultiResourceSync = false; 2585 mMultiResourceSync = false;
2584 syncCalendar( fn, 3 ); 2586 syncCalendar( fn, 3 );
2585 Event * e = getLastSyncEvent(); 2587 Event * e = getLastSyncEvent();
2586 if ( e ) 2588 if ( e )
2587 mCalendar->deleteEvent( e ); 2589 mCalendar->deleteEvent( e );
2588 return true; 2590 return true;
2589} 2591}
2590bool CalendarView::saveCalendars() 2592bool CalendarView::saveCalendars()
2591{ 2593{
2592 QPtrList<KopiCalendarFile> calendars = KOPrefs::instance()->mCalendars; 2594 QPtrList<KopiCalendarFile> calendars = KOPrefs::instance()->mCalendars;
2593 KopiCalendarFile * cal = calendars.first(); 2595 KopiCalendarFile * cal = calendars.first();
2594 mCalendar->setDefaultCalendar( 1 ); 2596 mCalendar->setDefaultCalendar( 1 );
2595 mCalendar->setDefaultCalendarEnabledOnly(); 2597 mCalendar->setDefaultCalendarEnabledOnly();
2596 QString saveError; 2598 QString saveError;
2597 if ( !saveCalendar( MainWindow::defaultFileName() ) ) 2599 if ( !saveCalendar( MainWindow::defaultFileName() ) )
2598 saveError = cal->mName +"\n"; 2600 saveError = cal->mName +"\n";
2599 cal = calendars.next(); 2601 cal = calendars.next();
2600 while ( cal ) { 2602 while ( cal ) {
2601 if ( !cal->isReadOnly && !cal->mErrorOnLoad ) { 2603 if ( !cal->isReadOnly && !cal->mErrorOnLoad ) {
2602 mCalendar->setDefaultCalendar( cal->mCalNumber ); 2604 mCalendar->setDefaultCalendar( cal->mCalNumber );
2603 mCalendar->setDefaultCalendarEnabledOnly(); 2605 mCalendar->setDefaultCalendarEnabledOnly();
2604 if ( saveCalendar( cal->mFileName ) ) 2606 if ( saveCalendar( cal->mFileName ) )
2605 cal->mLoadDt = QDateTime::currentDateTime(); 2607 cal->mLoadDt = QDateTime::currentDateTime();
2606 else 2608 else
2607 saveError += cal->mName + "\n"; 2609 saveError += cal->mName + "\n";
2608 } 2610 }
2609 cal = calendars.next(); 2611 cal = calendars.next();
2610 } 2612 }
2611 restoreCalendarSettings(); 2613 restoreCalendarSettings();
2612 //saveError = "test error"; 2614 //saveError = "test error";
2613 if ( !saveError.isEmpty() ) { 2615 if ( !saveError.isEmpty() ) {
2614 saveError = KGlobal::formatMessage( i18n("Calendar(s) not saved:"),0 )+"\n" + saveError; 2616 saveError = KGlobal::formatMessage( i18n("Calendar(s) not saved:"),0 )+"\n" + saveError;
2615 KMessageBox::error(this, saveError, i18n("Error saving data")); 2617 KMessageBox::error(this, saveError, i18n("Error saving data"));
2616 return false; 2618 return false;
2617 } 2619 }
2618 return true; 2620 return true;
2619} 2621}
2620bool CalendarView::saveCalendarResource(QString filename, QString resource) 2622bool CalendarView::saveCalendarResource(QString filename, QString resource)
2621{ 2623{
2622 if ( resource == "ALL" ) { 2624 if ( resource == "ALL" ) {
diff --git a/libkcal/calendarlocal.cpp b/libkcal/calendarlocal.cpp
index 980663f..8a5a76f 100644
--- a/libkcal/calendarlocal.cpp
+++ b/libkcal/calendarlocal.cpp
@@ -134,304 +134,313 @@ Incidence* CalendarLocal::incidenceForUid( const QString& uid , bool doNotCheckD
134 if ( event->calID() != calID ) 134 if ( event->calID() != calID )
135 continue; 135 continue;
136 } 136 }
137 else { 137 else {
138 if ( enabledOnly ) { 138 if ( enabledOnly ) {
139 if ( !event->calEnabled() ) { 139 if ( !event->calEnabled() ) {
140 if ( isDup ) 140 if ( isDup )
141 *isDup =event->calID() ; 141 *isDup =event->calID() ;
142 continue; 142 continue;
143 } 143 }
144 } 144 }
145 } 145 }
146 if ( doNotCheckDuplicates ) return event; 146 if ( doNotCheckDuplicates ) return event;
147 if ( retVal ) { 147 if ( retVal ) {
148 if ( retVal->calID() > event->calID() ) { 148 if ( retVal->calID() > event->calID() ) {
149 if ( isDup ) 149 if ( isDup )
150 *isDup = retVal->calID(); 150 *isDup = retVal->calID();
151 retVal = event; 151 retVal = event;
152 } 152 }
153 } else { 153 } else {
154 retVal = event; 154 retVal = event;
155 } 155 }
156 } 156 }
157 } 157 }
158 if ( retVal ) return retVal; 158 if ( retVal ) return retVal;
159 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 159 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
160 if ( it->uid() == uid ) { 160 if ( it->uid() == uid ) {
161 if( calID ) { 161 if( calID ) {
162 if ( event->calID() != calID ) 162 if ( event->calID() != calID )
163 continue; 163 continue;
164 } 164 }
165 else { 165 else {
166 if ( enabledOnly ) { 166 if ( enabledOnly ) {
167 if ( !it->calEnabled() ) { 167 if ( !it->calEnabled() ) {
168 if ( isDup ) 168 if ( isDup )
169 *isDup = it->calID(); 169 *isDup = it->calID();
170 continue; 170 continue;
171 } 171 }
172 } 172 }
173 } 173 }
174 if ( doNotCheckDuplicates ) return it; 174 if ( doNotCheckDuplicates ) return it;
175 if ( retVal ) { 175 if ( retVal ) {
176 if ( retVal->calID() > it->calID() ) { 176 if ( retVal->calID() > it->calID() ) {
177 if ( isDup ) 177 if ( isDup )
178 *isDup = retVal->calID(); 178 *isDup = retVal->calID();
179 retVal = it; 179 retVal = it;
180 } 180 }
181 } else { 181 } else {
182 retVal = it; 182 retVal = it;
183 } 183 }
184 } 184 }
185 return retVal; 185 return retVal;
186} 186}
187 187
188bool CalendarLocal::mergeCalendar( Calendar* remote ) 188bool CalendarLocal::mergeCalendar( Calendar* remote )
189{ 189{
190 // 1 look for raw inc in local 190 // 1 look for raw inc in local
191 // if inc not in remote, delete in local 191 // if inc not in remote, delete in local
192 // 2 look for raw inc in remote 192 // 2 look for raw inc in remote
193 // if inc in local, replace it 193 // if inc in local, replace it
194 // if not in local, add it to default calendar 194 // if not in local, add it to default calendar
195 QPtrList<Incidence> localInc = rawIncidences(); 195 QPtrList<Incidence> localInc = rawIncidences();
196 Incidence* inL = localInc.first(); 196 Incidence* inL = localInc.first();
197 while ( inL ) { 197 while ( inL ) {
198 if ( ! inL->isReadOnly () ) 198 if ( ! inL->isReadOnly () )
199 if ( !remote->incidenceForUid( inL->uid(), true )) 199 if ( !remote->incidenceForUid( inL->uid(), true ))
200 deleteIncidence( inL ); 200 deleteIncidence( inL );
201 inL = localInc.next(); 201 inL = localInc.next();
202 } 202 }
203 QPtrList<Incidence> er = remote->rawIncidences(); 203 QPtrList<Incidence> er = remote->rawIncidences();
204 Incidence* inR = er.first(); 204 Incidence* inR = er.first();
205 while ( inR ) { 205 while ( inR ) {
206 inL = incidenceForUid( inR->uid(),false ); 206 inL = incidenceForUid( inR->uid(),false );
207 if ( inL ) { 207 if ( inL ) {
208 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) { 208 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) {
209 int calID = inL->calID(); 209 int calID = inL->calID();
210 deleteIncidence( inL ); 210 deleteIncidence( inL );
211 inL = inR->clone(); 211 inL = inR->clone();
212 inL->setCalID_block( calID ); 212 inL->setCalID_block( calID );
213 addIncidence( inL ); 213 addIncidence( inL );
214 } 214 }
215 } else { 215 } else {
216 inL = inR->clone(); 216 inL = inR->clone();
217 inL->setCalID_block( 0 );// add to default cal 217 inL->setCalID_block( 0 );// add to default cal
218 addIncidence( inL ); 218 addIncidence( inL );
219 } 219 }
220 inR = er.next(); 220 inR = er.next();
221 } 221 }
222 return true; 222 return true;
223} 223}
224 224
225 225
226bool CalendarLocal::addCalendarFile( QString name, int id ) 226bool CalendarLocal::addCalendarFile( QString name, int id )
227{ 227{
228 CalendarLocal calendar( timeZoneId() ); 228 CalendarLocal calendar( timeZoneId() );
229 calendar.setDefaultCalendar( id ); 229 calendar.setDefaultCalendar( id );
230 if ( calendar.load( name ) ) { 230 if ( calendar.load( name ) ) {
231 addCalendar( &calendar ); 231 addCalendar( &calendar );
232 return true; 232 return true;
233 } 233 }
234 return false; 234 return false;
235} 235}
236void CalendarLocal::setSyncEventsEnabled() 236void CalendarLocal::setSyncEventsEnabled()
237{ 237{
238 Event * ev; 238 Event * ev;
239 ev = mEventList.first(); 239 ev = mEventList.first();
240 while ( ev ) { 240 while ( ev ) {
241 if ( ev->uid().left(15) == QString("last-syncEvent-") ) 241 if ( ev->uid().left(15) == QString("last-syncEvent-") )
242 ev->setCalEnabled( true ); 242 ev->setCalEnabled( true );
243 ev = mEventList.next(); 243 ev = mEventList.next();
244 } 244 }
245} 245}
246void CalendarLocal::setSyncEventsReadOnly() 246void CalendarLocal::setSyncEventsReadOnly()
247{ 247{
248 Event * ev; 248 Event * ev;
249 ev = mEventList.first(); 249 ev = mEventList.first();
250 while ( ev ) { 250 while ( ev ) {
251 if ( ev->uid().left(15) == QString("last-syncEvent-") ) { 251 if ( ev->uid().left(15) == QString("last-syncEvent-") ) {
252 ev->setReadOnly( true ); 252 ev->setReadOnly( true );
253 } 253 }
254 ev = mEventList.next(); 254 ev = mEventList.next();
255 } 255 }
256} 256}
257 257
258void CalendarLocal::addCalendar( Calendar* cal ) 258void CalendarLocal::addCalendar( Calendar* cal )
259{ 259{
260 cal->setDontDeleteIncidencesOnClose(); 260 cal->setDontDeleteIncidencesOnClose();
261 setSyncEventsEnabled(); 261 setSyncEventsEnabled();
262 QPtrList<Incidence> incList;
262 { 263 {
263 QPtrList<Event> EventList = cal->rawEvents(); 264 QPtrList<Event> EventList = cal->rawEvents();
264 QPtrList<Event> el;
265 Event * ev = EventList.first(); 265 Event * ev = EventList.first();
266 while ( ev ) { 266 while ( ev ) {
267 if ( ev->uid().left( 15 ) == QString("last-syncEvent-") ) { 267 if ( ev->uid().left( 15 ) == QString("last-syncEvent-") ) {
268 Event * se = event( ev->uid() );
269 if ( se )
270 el.append( se );
271 ev->setCalID_block( 1 ); 268 ev->setCalID_block( 1 );
272 } 269 }
270 Event * se = event( ev->uid() );
271 if ( se )
272 incList.append( se );
273 ev->unRegisterObserver( cal ); 273 ev->unRegisterObserver( cal );
274 ev->registerObserver( this ); 274 ev->registerObserver( this );
275 mEventList.append( ev ); 275 mEventList.append( ev );
276 ev = EventList.next(); 276 ev = EventList.next();
277 } 277 }
278 for ( ev = el.first(); ev; ev = el.next() ) { 278
279 deleteIncidence ( ev );
280 }
281 } 279 }
282 { 280 {
283 281
284 QPtrList<Todo> TodoList = cal->rawTodos(); 282 QPtrList<Todo> TodoList = cal->rawTodos();
285 Todo * ev = TodoList.first(); 283 Todo * ev = TodoList.first();
286 while ( ev ) { 284 while ( ev ) {
287 ev->resetRelatedTo(); 285 ev->resetRelatedTo();
288 ev = TodoList.next(); 286 ev = TodoList.next();
289 } 287 }
290 //TodoList = cal->rawTodos(); 288 //TodoList = cal->rawTodos();
291 ev = TodoList.first(); 289 ev = TodoList.first();
292 while ( ev ) { 290 while ( ev ) {
291 Todo * se = todo( ev->uid() );
292 if ( se )
293 incList.append( se );
293 ev->unRegisterObserver( cal ); 294 ev->unRegisterObserver( cal );
294 ev->registerObserver( this ); 295 ev->registerObserver( this );
295 mTodoList.append( ev ); 296 mTodoList.append( ev );
296 setupRelations( ev ); 297 setupRelations( ev );
297 ev = TodoList.next(); 298 ev = TodoList.next();
298 } 299 }
299 } 300 }
300 { 301 {
301 QPtrList<Journal> JournalList = cal->journals(); 302 QPtrList<Journal> JournalList = cal->journals();
302 Journal * ev = JournalList.first(); 303 Journal * ev = JournalList.first();
303 while ( ev ) { 304 while ( ev ) {
305 Journal * se = journal( ev->uid() );
306 if ( se )
307 incList.append( se );
304 ev->unRegisterObserver( cal ); 308 ev->unRegisterObserver( cal );
305 ev->registerObserver( this ); 309 ev->registerObserver( this );
306 mJournalList.append( ev ); 310 mJournalList.append( ev );
307 ev = JournalList.next(); 311 ev = JournalList.next();
308 } 312 }
309 } 313 }
314 {
315 for (Incidence * ev = incList.first(); ev; ev = incList.next() ) {
316 deleteIncidence ( ev );
317 }
318 }
310 setModified( true ); 319 setModified( true );
311} 320}
312bool CalendarLocal::load( const QString &fileName ) 321bool CalendarLocal::load( const QString &fileName )
313{ 322{
314 FileStorage storage( this, fileName ); 323 FileStorage storage( this, fileName );
315 return storage.load(); 324 return storage.load();
316} 325}
317 326
318bool CalendarLocal::save( const QString &fileName, CalFormat *format ) 327bool CalendarLocal::save( const QString &fileName, CalFormat *format )
319{ 328{
320 FileStorage storage( this, fileName, format ); 329 FileStorage storage( this, fileName, format );
321 return storage.save(); 330 return storage.save();
322} 331}
323 332
324void CalendarLocal::stopAllTodos() 333void CalendarLocal::stopAllTodos()
325{ 334{
326 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 335 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
327 it->setRunning( false ); 336 it->setRunning( false );
328 337
329} 338}
330void CalendarLocal::close() 339void CalendarLocal::close()
331{ 340{
332 341
333 Todo * i; 342 Todo * i;
334 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false); 343 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false);
335 344
336 mEventList.setAutoDelete( true ); 345 mEventList.setAutoDelete( true );
337 mTodoList.setAutoDelete( true ); 346 mTodoList.setAutoDelete( true );
338 mJournalList.setAutoDelete( true ); 347 mJournalList.setAutoDelete( true );
339 348
340 mEventList.clear(); 349 mEventList.clear();
341 mTodoList.clear(); 350 mTodoList.clear();
342 mJournalList.clear(); 351 mJournalList.clear();
343 352
344 mEventList.setAutoDelete( false ); 353 mEventList.setAutoDelete( false );
345 mTodoList.setAutoDelete( false ); 354 mTodoList.setAutoDelete( false );
346 mJournalList.setAutoDelete( false ); 355 mJournalList.setAutoDelete( false );
347 356
348 setModified( false ); 357 setModified( false );
349} 358}
350 359
351bool CalendarLocal::addAnniversaryNoDup( Event *event ) 360bool CalendarLocal::addAnniversaryNoDup( Event *event )
352{ 361{
353 QString cat; 362 QString cat;
354 bool isBirthday = true; 363 bool isBirthday = true;
355 if( event->categoriesStr() == i18n( "Anniversary" ) ) { 364 if( event->categoriesStr() == i18n( "Anniversary" ) ) {
356 isBirthday = false; 365 isBirthday = false;
357 cat = i18n( "Anniversary" ); 366 cat = i18n( "Anniversary" );
358 } else if( event->categoriesStr() == i18n( "Birthday" ) ) { 367 } else if( event->categoriesStr() == i18n( "Birthday" ) ) {
359 isBirthday = true; 368 isBirthday = true;
360 cat = i18n( "Birthday" ); 369 cat = i18n( "Birthday" );
361 } else { 370 } else {
362 qDebug("addAnniversaryNoDup called without fitting category! "); 371 qDebug("addAnniversaryNoDup called without fitting category! ");
363 return false; 372 return false;
364 } 373 }
365 Event * eve; 374 Event * eve;
366 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 375 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
367 if ( !(eve->categories().contains( cat ) )) 376 if ( !(eve->categories().contains( cat ) ))
368 continue; 377 continue;
369 // now we have an event with fitting category 378 // now we have an event with fitting category
370 if ( eve->dtStart().date() != event->dtStart().date() ) 379 if ( eve->dtStart().date() != event->dtStart().date() )
371 continue; 380 continue;
372 // now we have an event with fitting category+date 381 // now we have an event with fitting category+date
373 if ( eve->summary() != event->summary() ) 382 if ( eve->summary() != event->summary() )
374 continue; 383 continue;
375 // now we have an event with fitting category+date+summary 384 // now we have an event with fitting category+date+summary
376 return false; 385 return false;
377 } 386 }
378 return addEvent( event ); 387 return addEvent( event );
379 388
380} 389}
381bool CalendarLocal::addEventNoDup( Event *event ) 390bool CalendarLocal::addEventNoDup( Event *event )
382{ 391{
383 Event * eve; 392 Event * eve;
384 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 393 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
385 if ( *eve == *event ) { 394 if ( *eve == *event ) {
386 //qDebug("CalendarLocal::Duplicate event found! Not inserted! "); 395 //qDebug("CalendarLocal::Duplicate event found! Not inserted! ");
387 return false; 396 return false;
388 } 397 }
389 } 398 }
390 return addEvent( event ); 399 return addEvent( event );
391} 400}
392 401
393bool CalendarLocal::addEvent( Event *event ) 402bool CalendarLocal::addEvent( Event *event )
394{ 403{
395 insertEvent( event ); 404 insertEvent( event );
396 405
397 event->registerObserver( this ); 406 event->registerObserver( this );
398 407
399 setModified( true ); 408 setModified( true );
400 if ( event->calID() == 0 ) 409 if ( event->calID() == 0 )
401 event->setCalID_block( mDefaultCalendar ); 410 event->setCalID_block( mDefaultCalendar );
402 event->setCalEnabled( true ); 411 event->setCalEnabled( true );
403 412
404 return true; 413 return true;
405} 414}
406 415
407void CalendarLocal::deleteEvent( Event *event ) 416void CalendarLocal::deleteEvent( Event *event )
408{ 417{
409 clearUndo(event); 418 clearUndo(event);
410 if ( mEventList.removeRef( event ) ) { 419 if ( mEventList.removeRef( event ) ) {
411 setModified( true ); 420 setModified( true );
412 } 421 }
413} 422}
414 423
415 424
416Event *CalendarLocal::event( const QString &uid ) 425Event *CalendarLocal::event( const QString &uid )
417{ 426{
418 Event *event; 427 Event *event;
419 Event *retVal = 0; 428 Event *retVal = 0;
420 for ( event = mEventList.first(); event; event = mEventList.next() ) { 429 for ( event = mEventList.first(); event; event = mEventList.next() ) {
421 if ( event->calEnabled() && event->uid() == uid ) { 430 if ( event->calEnabled() && event->uid() == uid ) {
422 if ( retVal ) { 431 if ( retVal ) {
423 if ( retVal->calID() > event->calID() ) { 432 if ( retVal->calID() > event->calID() ) {
424 retVal = event; 433 retVal = event;
425 } 434 }
426 } else { 435 } else {
427 retVal = event; 436 retVal = event;
428 } 437 }
429 } 438 }
430 } 439 }
431 return retVal; 440 return retVal;
432} 441}
433bool CalendarLocal::addTodoNoDup( Todo *todo ) 442bool CalendarLocal::addTodoNoDup( Todo *todo )
434{ 443{
435 Todo * eve; 444 Todo * eve;
436 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) { 445 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) {
437 if ( *eve == *todo ) { 446 if ( *eve == *todo ) {