korganizer Library API Documentation

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 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 00011 This program is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with this program; if not, write to the Free Software 00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00019 00020 As a special exception, permission is given to link this program 00021 with any edition of Qt, and distribute the resulting executable, 00022 without including the source code for Qt in the source distribution. 00023 */ 00024 00025 #include "eventarchiver.h" 00026 #include <kglobal.h> 00027 #include <klocale.h> 00028 #include <ktempfile.h> 00029 #include <kio/netaccess.h> 00030 #include <kglobal.h> 00031 #include <libkcal/filestorage.h> 00032 #include <libkcal/calendarlocal.h> 00033 #include <libkcal/calendar.h> 00034 #include <kmessagebox.h> 00035 #include <kdebug.h> 00036 #include "koprefs.h" 00037 00038 EventArchiver::EventArchiver( QObject* parent, const char* name ) 00039 : QObject( parent, name ) 00040 { 00041 } 00042 00043 EventArchiver::~EventArchiver() 00044 { 00045 } 00046 00047 void EventArchiver::runOnce( Calendar* calendar, const QDate& limitDate, QWidget* widget ) 00048 { 00049 run( calendar, limitDate, widget, true, true ); 00050 } 00051 00052 void EventArchiver::runAuto( Calendar* calendar, QWidget* widget, bool withGUI ) 00053 { 00054 QDate limitDate( QDate::currentDate() ); 00055 int expiryTime = KOPrefs::instance()->mExpiryTime; 00056 switch (KOPrefs::instance()->mExpiryUnit) { 00057 case KOPrefs::UnitDays: // Days 00058 limitDate = limitDate.addDays( -expiryTime ); 00059 break; 00060 case KOPrefs::UnitWeeks: // Weeks 00061 limitDate = limitDate.addDays( -expiryTime*7 ); 00062 break; 00063 case KOPrefs::UnitMonths: // Months 00064 limitDate = limitDate.addMonths( -expiryTime ); 00065 break; 00066 default: 00067 return; 00068 } 00069 run( calendar, limitDate, widget, withGUI, false ); 00070 } 00071 00072 void EventArchiver::run( Calendar* calendar, const QDate& limitDate, QWidget* widget, bool withGUI, bool errorIfNone ) 00073 { 00074 Event::List events = calendar->events( 00075 QDate( 1769, 12, 1 ), 00076 // #29555, also advertised by the "limitDate not included" in the class docu 00077 limitDate.addDays( -1 ), 00078 true ); 00079 00080 kdDebug(5850) << "EventArchiver: archiving events before " << limitDate << " -> " << events.count() << " events found." << endl; 00081 if ( events.isEmpty() ) { 00082 if ( withGUI && errorIfNone ) 00083 KMessageBox::sorry(widget, i18n("There are no events before %1") 00084 .arg(KGlobal::locale()->formatDate(limitDate))); 00085 return; 00086 } 00087 00088 00089 switch ( KOPrefs::instance()->mArchiveAction ) { 00090 case KOPrefs::actionDelete: 00091 deleteEvents( calendar, limitDate, widget, events, withGUI ); 00092 break; 00093 case KOPrefs::actionArchive: 00094 archiveEvents( calendar, limitDate, widget, events, withGUI ); 00095 break; 00096 } 00097 } 00098 00099 void EventArchiver::deleteEvents( Calendar* calendar, const QDate& limitDate, QWidget* widget, const Event::List& events, bool withGUI ) 00100 { 00101 QStringList eventStrs; 00102 Event::List::ConstIterator it; 00103 for( it = events.begin(); it != events.end(); ++it ) { 00104 eventStrs.append( (*it)->summary() ); 00105 } 00106 00107 if ( withGUI ) { 00108 int result = KMessageBox::warningContinueCancelList( 00109 widget, i18n("Delete all events before %1 without saving?\n" 00110 "The following events will be deleted:") 00111 .arg(KGlobal::locale()->formatDate(limitDate)),eventStrs, 00112 i18n("Delete Old Events"),i18n("&Delete")); 00113 if (result != KMessageBox::Continue) 00114 return; 00115 } 00116 for( it = events.begin(); it != events.end(); ++it ) { 00117 calendar->deleteEvent( *it ); 00118 } 00119 emit eventsDeleted(); 00120 } 00121 00122 void EventArchiver::archiveEvents( Calendar* calendar, const QDate& limitDate, QWidget* widget, const Event::List& events, bool /*withGUI*/) 00123 { 00124 FileStorage storage( calendar ); 00125 00126 // Save current calendar to disk 00127 KTempFile tmpFile; 00128 tmpFile.setAutoDelete(true); 00129 storage.setFileName( tmpFile.name() ); 00130 if ( !storage.save() ) { 00131 kdDebug(5850) << "EventArchiver::archiveEvents(): Can't save calendar to temp file" << endl; 00132 return; 00133 } 00134 00135 // Duplicate current calendar by loading in new calendar object 00136 CalendarLocal archiveCalendar( KOPrefs::instance()->mTimeZoneId ); 00137 00138 FileStorage archiveStore( &archiveCalendar ); 00139 archiveStore.setFileName( tmpFile.name() ); 00140 if (!archiveStore.load()) { 00141 kdDebug(5850) << "EventArchiver::archiveEvents(): Can't load calendar from temp file" << endl; 00142 return; 00143 } 00144 00145 // Strip active events from calendar so that only events to be archived 00146 // remain. 00147 Event::List activeEvents = archiveCalendar.events( limitDate, 00148 QDate( 3000, 1, 1 ), 00149 false ); 00150 Event::List::ConstIterator it; 00151 for( it = activeEvents.begin(); it != activeEvents.end(); ++it ) { 00152 archiveCalendar.deleteEvent( *it ); 00153 } 00154 00155 // Get or create the archive file 00156 KURL archiveURL( KOPrefs::instance()->mArchiveFile ); 00157 QString archiveFile; 00158 00159 if ( KIO::NetAccess::exists( archiveURL, true, widget ) ) { 00160 if( !KIO::NetAccess::download( archiveURL, archiveFile, widget ) ) { 00161 kdDebug(5850) << "EventArchiver::archiveEvents(): Can't download archive file" << endl; 00162 return; 00163 } 00164 // Merge with events to be archived. 00165 archiveStore.setFileName( archiveFile ); 00166 if ( !archiveStore.load() ) { 00167 kdDebug(5850) << "EventArchiver::archiveEvents(): Can't merge with archive file" << endl; 00168 return; 00169 } 00170 /* 00171 QPtrList<Event> es = archiveCalendar.events(QDate(1800,1,1), 00172 QDate(3000,1,1), 00173 false); 00174 kdDebug(5850) << "--Following events in archive calendar:" << endl; 00175 Event *e; 00176 for(e=es.first();e;e=es.next()) { 00177 kdDebug(5850) << "-----Event: " << e->getSummary() << endl; 00178 } 00179 */ 00180 } else { 00181 archiveFile = tmpFile.name(); 00182 } 00183 00184 // Save archive calendar 00185 if ( !archiveStore.save() ) { 00186 KMessageBox::error(widget,i18n("Cannot write archive file %1.").arg( archiveStore.fileName() )); 00187 return; 00188 } 00189 00190 // Upload if necessary 00191 KURL srcUrl; 00192 srcUrl.setPath(archiveFile); 00193 if (srcUrl != archiveURL) { 00194 if ( !KIO::NetAccess::upload( archiveFile, archiveURL, widget ) ) { 00195 KMessageBox::error(widget,i18n("Cannot write archive to final destination.")); 00196 return; 00197 } 00198 } 00199 00200 KIO::NetAccess::removeTempFile(archiveFile); 00201 00202 // Delete archived events from calendar 00203 for( it = events.begin(); it != events.end(); ++it ) { 00204 calendar->deleteEvent( *it ); 00205 } 00206 emit eventsDeleted(); 00207 } 00208 00209 #include "eventarchiver.moc"
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Oct 1 15:19:30 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003