26 #include "eventarchiver.h"
27 #include <tdeglobal.h>
28 #include <tdelocale.h>
29 #include <tdetempfile.h>
30 #include <tdeio/netaccess.h>
31 #include <tdeglobal.h>
32 #include <libkcal/filestorage.h>
33 #include <libkcal/calendarlocal.h>
35 #include <tdemessagebox.h>
39 EventArchiver::EventArchiver( TQObject* parent,
const char* name )
40 : TQObject( parent, name )
44 EventArchiver::~EventArchiver()
50 run( calendar, limitDate, widget,
true,
true );
55 TQDate limitDate( TQDate::currentDate() );
56 int expiryTime = KOPrefs::instance()->mExpiryTime;
57 switch (KOPrefs::instance()->mExpiryUnit) {
58 case KOPrefs::UnitDays:
59 limitDate = limitDate.addDays( -expiryTime );
61 case KOPrefs::UnitWeeks:
62 limitDate = limitDate.addDays( -expiryTime*7 );
64 case KOPrefs::UnitMonths:
65 limitDate = limitDate.addMonths( -expiryTime );
70 run( calendar, limitDate, widget, withGUI,
false );
73 void EventArchiver::run(
Calendar* calendar,
const TQDate& limitDate, TQWidget* widget,
bool withGUI,
77 Incidence::List incidences;
80 Journal::List journals;
82 if ( KOPrefs::instance()->mArchiveEvents ) {
84 TQDate( 1769, 12, 1 ),
86 limitDate.addDays( -1 ),
89 if ( KOPrefs::instance()->mArchiveTodos ) {
91 Todo::List::ConstIterator it;
92 for( it = t.begin(); it != t.end(); ++it ) {
93 const bool todoComplete = (*it) &&
94 (*it)->isCompleted() &&
95 ( (*it)->completed().date() < limitDate );
97 if ( todoComplete && !isSubTreeComplete( *it, limitDate ) ) {
99 KMessageBox::information(
101 i18n(
"Unable to archive to-do \"%1\" because at least one of its "
102 "sub-to-dos does not meet the archival requirements." ).arg( (*it)->summary() ),
103 i18n(
"Archive To-do" ),
104 "UncompletedChildrenArchiveTodos" );
105 }
else if ( todoComplete ) {
111 incidences = Calendar::mergeIncidenceList( events, todos, journals );
114 kdDebug(5850) <<
"EventArchiver: archiving incidences before " << limitDate <<
" -> "
115 << incidences.count() <<
" incidences found." << endl;
116 if ( incidences.isEmpty() ) {
117 if ( withGUI && errorIfNone ) {
118 KMessageBox::information(
120 i18n(
"There are no incidences available to archive before the specified cut-off date %1. "
121 "Archiving will not be performed." ).arg( TDEGlobal::locale()->formatDate( limitDate ) ),
122 "ArchiverNoIncidences" );
128 switch ( KOPrefs::instance()->mArchiveAction ) {
129 case KOPrefs::actionDelete:
130 deleteIncidences( calendar, limitDate, widget, incidences, withGUI );
132 case KOPrefs::actionArchive:
133 archiveIncidences( calendar, limitDate, widget, incidences, withGUI );
138 void EventArchiver::deleteIncidences(
Calendar* calendar,
const TQDate& limitDate, TQWidget* widget,
const Incidence::List& incidences,
bool withGUI )
140 TQStringList incidenceStrs;
141 Incidence::List::ConstIterator it;
142 for( it = incidences.begin(); it != incidences.end(); ++it ) {
143 incidenceStrs.append( (*it)->summary() );
147 int result = KMessageBox::warningContinueCancelList(
148 widget, i18n(
"Delete all items before %1 without saving?\n"
149 "The following items will be deleted:")
150 .arg(TDEGlobal::locale()->formatDate(limitDate)), incidenceStrs,
151 i18n(
"Delete Old Items"),KStdGuiItem::del());
152 if (result != KMessageBox::Continue)
155 for( it = incidences.begin(); it != incidences.end(); ++it ) {
158 emit eventsDeleted();
161 void EventArchiver::archiveIncidences(
Calendar* calendar,
const TQDate& , TQWidget* widget,
const Incidence::List& incidences,
bool )
167 tmpFile.setAutoDelete(
true);
168 storage.setFileName( tmpFile.name() );
169 if ( !storage.save() ) {
170 kdDebug(5850) <<
"EventArchiver::archiveEvents(): Can't save calendar to temp file" << endl;
175 CalendarLocal archiveCalendar( KOPrefs::instance()->mTimeZoneId );
178 archiveStore.setFileName( tmpFile.name() );
179 if (!archiveStore.load()) {
180 kdDebug(5850) <<
"EventArchiver::archiveEvents(): Can't load calendar from temp file" << endl;
187 Incidence::List allIncidences = archiveCalendar.rawIncidences();
188 Incidence::List::ConstIterator it;
189 for( it = incidences.begin(); it != incidences.end(); ++it ) {
190 uids << (*it)->uid();
192 for( it = allIncidences.begin(); it != allIncidences.end(); ++it ) {
193 if ( !uids.contains( (*it)->uid() ) ) {
194 archiveCalendar.deleteIncidence( *it );
199 KURL archiveURL( KOPrefs::instance()->mArchiveFile );
200 TQString archiveFile;
202 if ( TDEIO::NetAccess::exists( archiveURL,
true, widget ) ) {
203 if( !TDEIO::NetAccess::download( archiveURL, archiveFile, widget ) ) {
204 kdDebug(5850) <<
"EventArchiver::archiveEvents(): Can't download archive file" << endl;
208 archiveStore.setFileName( archiveFile );
209 if ( !archiveStore.load() ) {
210 kdDebug(5850) <<
"EventArchiver::archiveEvents(): Can't merge with archive file" << endl;
214 archiveFile = tmpFile.name();
218 if ( !archiveStore.save() ) {
219 KMessageBox::error(widget,i18n(
"Cannot write archive file %1.").arg( archiveStore.fileName() ));
225 srcUrl.setPath(archiveFile);
226 if (srcUrl != archiveURL) {
227 if ( !TDEIO::NetAccess::upload( archiveFile, archiveURL, widget ) ) {
228 KMessageBox::error(widget,i18n(
"Cannot write archive to final destination."));
233 TDEIO::NetAccess::removeTempFile(archiveFile);
236 for( it = incidences.begin(); it != incidences.end(); ++it ) {
239 emit eventsDeleted();
242 bool EventArchiver::isSubTreeComplete(
const Todo *todo,
const TQDate &limitDate,
243 TQStringList checkedUids )
const
250 if ( checkedUids.contains( todo->
uid() ) ) {
252 kdWarning() <<
"To-do hierarchy loop detected!";
256 checkedUids.append( todo->
uid() );
258 Incidence::List::ConstIterator it;
259 const Incidence::List relations = todo->
relations();
261 for( it = relations.begin(); it != relations.end(); ++it ) {
262 if ( (*it)->type() ==
"Todo" ) {
263 const Todo *t =
static_cast<const Todo*
>( *it );
264 if ( !isSubTreeComplete( t, limitDate, checkedUids ) ) {
273 #include "eventarchiver.moc"
void runOnce(Calendar *calendar, const TQDate &limitDate, TQWidget *widget)
Delete or archive events once.
void runAuto(Calendar *calendar, TQWidget *widget, bool withGUI)
Delete or archive events.
virtual bool deleteIncidence(Incidence *incidence)
virtual Event::List rawEvents(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)=0
virtual Todo::List rawTodos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)=0
Incidence::List relations() const
TQDateTime completed() const