korganizer

eventarchiver.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (c) 2004 David Faure <faure@kde.org>
00005     Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include "eventarchiver.h"
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 #include <ktempfile.h>
00030 #include <kio/netaccess.h>
00031 #include <kglobal.h>
00032 #include <libkcal/filestorage.h>
00033 #include <libkcal/calendarlocal.h>
00034 #include <libkcal/calendar.h>
00035 #include <kmessagebox.h>
00036 #include <kdebug.h>
00037 #include "koprefs.h"
00038 
00039 EventArchiver::EventArchiver( QObject* parent, const char* name )
00040  : QObject( parent, name )
00041 {
00042 }
00043 
00044 EventArchiver::~EventArchiver()
00045 {
00046 }
00047 
00048 void EventArchiver::runOnce( Calendar* calendar, const QDate& limitDate, QWidget* widget )
00049 {
00050   run( calendar, limitDate, widget, true, true );
00051 }
00052 
00053 void EventArchiver::runAuto( Calendar* calendar, QWidget* widget, bool withGUI )
00054 {
00055   QDate limitDate( QDate::currentDate() );
00056   int expiryTime = KOPrefs::instance()->mExpiryTime;
00057   switch (KOPrefs::instance()->mExpiryUnit) {
00058   case KOPrefs::UnitDays: // Days
00059     limitDate = limitDate.addDays( -expiryTime );
00060     break;
00061   case KOPrefs::UnitWeeks: // Weeks
00062     limitDate = limitDate.addDays( -expiryTime*7 );
00063     break;
00064   case KOPrefs::UnitMonths: // Months
00065     limitDate = limitDate.addMonths( -expiryTime );
00066     break;
00067   default:
00068     return;
00069   }
00070   run( calendar, limitDate, widget, withGUI, false );
00071 }
00072 
00073 void EventArchiver::run( Calendar* calendar, const QDate& limitDate, QWidget* widget, bool withGUI, bool errorIfNone )
00074 {
00075   // We need to use rawEvents, otherwise events hidden by filters will not be archived.
00076   Incidence::List incidences;
00077   Event::List events;
00078   Todo::List todos;
00079   Journal::List journals;
00080   
00081   if ( KOPrefs::instance()->mArchiveEvents ) {
00082     events = calendar->rawEvents(
00083       QDate( 1769, 12, 1 ),
00084       // #29555, also advertised by the "limitDate not included" in the class docu
00085       limitDate.addDays( -1 ),
00086       true );
00087   }
00088   if ( KOPrefs::instance()->mArchiveTodos ) {
00089     Todo::List t = calendar->rawTodos();
00090     Todo::List::ConstIterator it;
00091     for( it = t.begin(); it != t.end(); ++it ) {
00092       if ( (*it) && ( (*it)->isCompleted() ) &&  ( (*it)->completed().date() < limitDate ) ) {
00093         todos.append( *it );
00094       }
00095     }
00096   }
00097   
00098   incidences = Calendar::mergeIncidenceList( events, todos, journals );
00099   
00100 
00101   kdDebug(5850) << "EventArchiver: archiving incidences before " << limitDate << " -> " << incidences.count() << " incidences found." << endl;
00102   if ( incidences.isEmpty() ) {
00103     if ( withGUI && errorIfNone )
00104       KMessageBox::information( widget, i18n("There are no items before %1")
00105                           .arg(KGlobal::locale()->formatDate(limitDate)),
00106                           "ArchiverNoIncidences" );
00107     return;
00108   }
00109 
00110 
00111   switch ( KOPrefs::instance()->mArchiveAction ) {
00112   case KOPrefs::actionDelete:
00113     deleteIncidences( calendar, limitDate, widget, incidences, withGUI );
00114     break;
00115   case KOPrefs::actionArchive:
00116     archiveIncidences( calendar, limitDate, widget, incidences, withGUI );
00117     break;
00118   }
00119 }
00120 
00121 void EventArchiver::deleteIncidences( Calendar* calendar, const QDate& limitDate, QWidget* widget, const Incidence::List& incidences, bool withGUI )
00122 {
00123   QStringList incidenceStrs;
00124   Incidence::List::ConstIterator it;
00125   for( it = incidences.begin(); it != incidences.end(); ++it ) {
00126     incidenceStrs.append( (*it)->summary() );
00127   }
00128 
00129   if ( withGUI ) {
00130     int result = KMessageBox::warningContinueCancelList(
00131       widget, i18n("Delete all items before %1 without saving?\n"
00132                  "The following items will be deleted:")
00133       .arg(KGlobal::locale()->formatDate(limitDate)), incidenceStrs,
00134          i18n("Delete Old Items"),KStdGuiItem::del());
00135     if (result != KMessageBox::Continue)
00136       return;
00137   }
00138   for( it = incidences.begin(); it != incidences.end(); ++it ) {
00139     calendar->deleteIncidence( *it );
00140   }
00141   emit eventsDeleted();
00142 }
00143 
00144 void EventArchiver::archiveIncidences( Calendar* calendar, const QDate& /*limitDate*/, QWidget* widget, const Incidence::List& incidences, bool /*withGUI*/)
00145 {
00146   FileStorage storage( calendar );
00147 
00148   // Save current calendar to disk
00149   KTempFile tmpFile;
00150   tmpFile.setAutoDelete(true);
00151   storage.setFileName( tmpFile.name() );
00152   if ( !storage.save() ) {
00153     kdDebug(5850) << "EventArchiver::archiveEvents(): Can't save calendar to temp file" << endl;
00154     return;
00155   }
00156 
00157   // Duplicate current calendar by loading in new calendar object
00158   CalendarLocal archiveCalendar( KOPrefs::instance()->mTimeZoneId );
00159 
00160   FileStorage archiveStore( &archiveCalendar );
00161   archiveStore.setFileName( tmpFile.name() );
00162   if (!archiveStore.load()) {
00163     kdDebug(5850) << "EventArchiver::archiveEvents(): Can't load calendar from temp file" << endl;
00164     return;
00165   }
00166 
00167   // Strip active events from calendar so that only events to be archived
00168   // remain. This is not really efficient, but there is no other easy way.
00169   QStringList uids;
00170   Incidence::List allIncidences = archiveCalendar.rawIncidences();
00171   Incidence::List::ConstIterator it;
00172   for( it = incidences.begin(); it != incidences.end(); ++it ) {
00173     uids << (*it)->uid();
00174   }
00175   for( it = allIncidences.begin(); it != allIncidences.end(); ++it ) {
00176     if ( !uids.contains( (*it)->uid() ) ) {
00177       archiveCalendar.deleteIncidence( *it );
00178     }
00179   }
00180 
00181   // Get or create the archive file
00182   KURL archiveURL( KOPrefs::instance()->mArchiveFile );
00183   QString archiveFile;
00184 
00185   if ( KIO::NetAccess::exists( archiveURL, true, widget ) ) {
00186     if( !KIO::NetAccess::download( archiveURL, archiveFile, widget ) ) {
00187       kdDebug(5850) << "EventArchiver::archiveEvents(): Can't download archive file" << endl;
00188       return;
00189     }
00190     // Merge with events to be archived.
00191     archiveStore.setFileName( archiveFile );
00192     if ( !archiveStore.load() ) {
00193       kdDebug(5850) << "EventArchiver::archiveEvents(): Can't merge with archive file" << endl;
00194       return;
00195     }
00196   } else {
00197     archiveFile = tmpFile.name();
00198   }
00199 
00200   // Save archive calendar
00201   if ( !archiveStore.save() ) {
00202     KMessageBox::error(widget,i18n("Cannot write archive file %1.").arg( archiveStore.fileName() ));
00203     return;
00204   }
00205 
00206   // Upload if necessary
00207   KURL srcUrl;
00208   srcUrl.setPath(archiveFile);
00209   if (srcUrl != archiveURL) {
00210     if ( !KIO::NetAccess::upload( archiveFile, archiveURL, widget ) ) {
00211       KMessageBox::error(widget,i18n("Cannot write archive to final destination."));
00212       return;
00213     }
00214   }
00215 
00216   KIO::NetAccess::removeTempFile(archiveFile);
00217 
00218   // Delete archived events from calendar
00219   for( it = incidences.begin(); it != incidences.end(); ++it ) {
00220     calendar->deleteIncidence( *it );
00221   }
00222   emit eventsDeleted();
00223 }
00224 
00225 #include "eventarchiver.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys