00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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:
00058 limitDate = limitDate.addDays( -expiryTime );
00059 break;
00060 case KOPrefs::UnitWeeks:
00061 limitDate = limitDate.addDays( -expiryTime*7 );
00062 break;
00063 case KOPrefs::UnitMonths:
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
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 )
00123 {
00124 FileStorage storage( calendar );
00125
00126
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
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
00146
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
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
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
00172
00173
00174
00175
00176
00177
00178
00179
00180 } else {
00181 archiveFile = tmpFile.name();
00182 }
00183
00184
00185 if ( !archiveStore.save() ) {
00186 KMessageBox::error(widget,i18n("Cannot write archive file %1.").arg( archiveStore.fileName() ));
00187 return;
00188 }
00189
00190
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
00203 for( it = events.begin(); it != events.end(); ++it ) {
00204 calendar->deleteEvent( *it );
00205 }
00206 emit eventsDeleted();
00207 }
00208
00209 #include "eventarchiver.moc"