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 "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:
00061 limitDate = limitDate.addDays( -expiryTime );
00062 break;
00063 case KOPrefs::UnitWeeks:
00064 limitDate = limitDate.addDays( -expiryTime * 7 );
00065 break;
00066 case KOPrefs::UnitMonths:
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
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
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
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
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
00182
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
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
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
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
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
00241 for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00242 calendar->deleteIncidence( *it );
00243 }
00244 emit eventsDeleted();
00245 }
00246
00247 #include "eventarchiver.moc"