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
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:
00059 limitDate = limitDate.addDays( -expiryTime );
00060 break;
00061 case KOPrefs::UnitWeeks:
00062 limitDate = limitDate.addDays( -expiryTime*7 );
00063 break;
00064 case KOPrefs::UnitMonths:
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
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
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& , QWidget* widget, const Incidence::List& incidences, bool )
00145 {
00146 FileStorage storage( calendar );
00147
00148
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
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
00168
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
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
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
00201 if ( !archiveStore.save() ) {
00202 KMessageBox::error(widget,i18n("Cannot write archive file %1.").arg( archiveStore.fileName() ));
00203 return;
00204 }
00205
00206
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
00219 for( it = incidences.begin(); it != incidences.end(); ++it ) {
00220 calendar->deleteIncidence( *it );
00221 }
00222 emit eventsDeleted();
00223 }
00224
00225 #include "eventarchiver.moc"