• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

korganizer

eventarchiver.cpp

Go to the documentation of this file.
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 along
00018   with this program; if not, write to the Free Software Foundation, Inc.,
00019   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 "koprefs.h"
00028 
00029 #include <kio/netaccess.h>
00030 #include <kcal/icalformat.h>
00031 #include <kcal/filestorage.h>
00032 #include <kcal/calendarlocal.h>
00033 #include <kcal/calendar.h>
00034 
00035 #include <kdebug.h>
00036 #include <kglobal.h>
00037 #include <klocale.h>
00038 #include <ktemporaryfile.h>
00039 #include <kmessagebox.h>
00040 
00041 EventArchiver::EventArchiver( QObject *parent )
00042  : QObject( parent )
00043 {
00044 }
00045 
00046 EventArchiver::~EventArchiver()
00047 {
00048 }
00049 
00050 void EventArchiver::runOnce( Calendar *calendar, const QDate &limitDate, QWidget *widget )
00051 {
00052   run( calendar, limitDate, widget, true, true );
00053 }
00054 
00055 void EventArchiver::runAuto( Calendar *calendar, QWidget *widget, bool withGUI )
00056 {
00057   QDate limitDate( QDate::currentDate() );
00058   int expiryTime = KOPrefs::instance()->mExpiryTime;
00059   switch ( KOPrefs::instance()->mExpiryUnit ) {
00060   case KOPrefs::UnitDays: // Days
00061     limitDate = limitDate.addDays( -expiryTime );
00062     break;
00063   case KOPrefs::UnitWeeks: // Weeks
00064     limitDate = limitDate.addDays( -expiryTime * 7 );
00065     break;
00066   case KOPrefs::UnitMonths: // Months
00067     limitDate = limitDate.addMonths( -expiryTime );
00068     break;
00069   default:
00070     return;
00071   }
00072   run( calendar, limitDate, widget, withGUI, false );
00073 }
00074 
00075 void EventArchiver::run( Calendar *calendar, const QDate &limitDate, QWidget *widget,
00076                          bool withGUI, bool errorIfNone )
00077 {
00078   // We need to use rawEvents, otherwise events hidden by filters will not be archived.
00079   Incidence::List incidences;
00080   Event::List events;
00081   Todo::List todos;
00082   Journal::List journals;
00083 
00084   if ( KOPrefs::instance()->mArchiveEvents ) {
00085     events = calendar->rawEvents(
00086       QDate( 1769, 12, 1 ),
00087       // #29555, also advertised by the "limitDate not included" in the class docu
00088       limitDate.addDays( -1 ),
00089       KOPrefs::instance()->timeSpec(),
00090       true );
00091   }
00092   if ( KOPrefs::instance()->mArchiveTodos ) {
00093     Todo::List t = calendar->rawTodos();
00094     Todo::List::ConstIterator it;
00095     for ( it = t.begin(); it != t.end(); ++it ) {
00096       if ( (*it) && ( (*it)->isCompleted() ) &&  ( (*it)->completed().date() < limitDate ) ) {
00097         todos.append( *it );
00098       }
00099     }
00100   }
00101 
00102   incidences = Calendar::mergeIncidenceList( events, todos, journals );
00103 
00104   kDebug() << "archiving incidences before" << limitDate
00105            << " ->" << incidences.count() <<" incidences found.";
00106   if ( incidences.isEmpty() ) {
00107     if ( withGUI && errorIfNone ) {
00108       KMessageBox::information( widget,
00109                                 i18n( "There are no items before %1",
00110                                       KGlobal::locale()->formatDate( limitDate ) ),
00111                                 "ArchiverNoIncidences" );
00112     }
00113     return;
00114   }
00115 
00116   switch ( KOPrefs::instance()->mArchiveAction ) {
00117   case KOPrefs::actionDelete:
00118     deleteIncidences( calendar, limitDate, widget, incidences, withGUI );
00119     break;
00120   case KOPrefs::actionArchive:
00121     archiveIncidences( calendar, limitDate, widget, incidences, withGUI );
00122     break;
00123   }
00124 }
00125 
00126 void EventArchiver::deleteIncidences( Calendar *calendar, const QDate &limitDate, QWidget *widget,
00127                                       const Incidence::List &incidences, bool withGUI )
00128 {
00129   QStringList incidenceStrs;
00130   Incidence::List::ConstIterator it;
00131   for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00132     incidenceStrs.append( (*it)->summary() );
00133   }
00134 
00135   if ( withGUI ) {
00136     int result = KMessageBox::warningContinueCancelList(
00137       widget,
00138       i18n( "Delete all items before %1 without saving?\n"
00139             "The following items will be deleted:",
00140             KGlobal::locale()->formatDate( limitDate ) ),
00141       incidenceStrs,
00142       i18n( "Delete Old Items" ), KStandardGuiItem::del() );
00143     if ( result != KMessageBox::Continue ) {
00144       return;
00145     }
00146   }
00147   for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00148     calendar->deleteIncidence( *it );
00149   }
00150   emit eventsDeleted();
00151 }
00152 
00153 void EventArchiver::archiveIncidences( Calendar *calendar, const QDate &limitDate, QWidget *widget,
00154                                        const Incidence::List &incidences, bool withGUI )
00155 {
00156   Q_UNUSED( limitDate );
00157   Q_UNUSED( withGUI );
00158   FileStorage storage( calendar );
00159 
00160   // Save current calendar to disk
00161   KTemporaryFile tmpFile;
00162   tmpFile.open();
00163   storage.setFileName( tmpFile.fileName() );
00164   if ( !storage.save() ) {
00165     kDebug() << "Can't save calendar to temp file";
00166     return;
00167   }
00168 
00169   // Duplicate current calendar by loading in new calendar object
00170   CalendarLocal archiveCalendar( KOPrefs::instance()->timeSpec() );
00171 
00172   FileStorage archiveStore( &archiveCalendar );
00173   archiveStore.setFileName( tmpFile.fileName() );
00174   ICalFormat *format = new ICalFormat();
00175   archiveStore.setSaveFormat( format );
00176   if ( !archiveStore.load() ) {
00177     kDebug() << "Can't load calendar from temp file";
00178     return;
00179   }
00180 
00181   // Strip active events from calendar so that only events to be archived
00182   // remain. This is not really efficient, but there is no other easy way.
00183   QStringList uids;
00184   Incidence::List allIncidences = archiveCalendar.rawIncidences();
00185   Incidence::List::ConstIterator it;
00186   for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00187     uids << (*it)->uid();
00188   }
00189   for ( it = allIncidences.begin(); it != allIncidences.end(); ++it ) {
00190     if ( !uids.contains( (*it)->uid() ) ) {
00191       archiveCalendar.deleteIncidence( *it );
00192     }
00193   }
00194 
00195   // Get or create the archive file
00196   KUrl archiveURL( KOPrefs::instance()->mArchiveFile );
00197   QString archiveFile;
00198 
00199   if ( KIO::NetAccess::exists( archiveURL, KIO::NetAccess::SourceSide, widget ) ) {
00200     if( !KIO::NetAccess::download( archiveURL, archiveFile, widget ) ) {
00201       kDebug() << "Can't download archive file";
00202       return;
00203     }
00204     // Merge with events to be archived.
00205     archiveStore.setFileName( archiveFile );
00206     if ( !archiveStore.load() ) {
00207       kDebug() << "Can't merge with archive file";
00208       return;
00209     }
00210   } else {
00211     archiveFile = tmpFile.fileName();
00212   }
00213 
00214   // Save archive calendar
00215   if ( !archiveStore.save() ) {
00216     QString errmess;
00217     if ( format->exception() ) {
00218       errmess = format->exception()->message();
00219     } else {
00220       errmess = i18nc( "save failure cause unknown", "Reason unknown" );
00221     }
00222     KMessageBox::error( widget, i18n( "Cannot write archive file %1. %2",
00223                                       archiveStore.fileName(), errmess ) );
00224     return;
00225   }
00226 
00227   // Upload if necessary
00228   KUrl srcUrl;
00229   srcUrl.setPath( archiveFile );
00230   if ( srcUrl != archiveURL ) {
00231     if ( !KIO::NetAccess::upload( archiveFile, archiveURL, widget ) ) {
00232       KMessageBox::error( widget, i18n( "Cannot write archive. %1",
00233                                         KIO::NetAccess::lastErrorString() ) );
00234       return;
00235     }
00236   }
00237 
00238   KIO::NetAccess::removeTempFile( archiveFile );
00239 
00240   // Delete archived events from calendar
00241   for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00242     calendar->deleteIncidence( *it );
00243   }
00244   emit eventsDeleted();
00245 }
00246 
00247 #include "eventarchiver.moc"

korganizer

Skip menu "korganizer"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal