korganizer Library API Documentation

archivedialog.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 // ArchiveDialog -- archive/delete past appointments.
00025 
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qdatetime.h>
00029 
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <kurlrequester.h>
00033 #include <kmessagebox.h>
00034 #include <kglobal.h>
00035 #include <kfiledialog.h>
00036 #include <kurl.h>
00037 #include <ktempfile.h>
00038 #include <kio/netaccess.h>
00039 #include <klineedit.h>
00040 
00041 #include <libkcal/event.h>
00042 #include <libkcal/calendar.h>
00043 #include <libkcal/calendarlocal.h>
00044 #include <libkcal/filestorage.h>
00045 
00046 #include <libkdepim/kdateedit.h>
00047 
00048 #include "koprefs.h"
00049 
00050 #include "archivedialog.h"
00051 #include "archivedialog.moc"
00052 
00053 ArchiveDialog::ArchiveDialog(Calendar *cal,QWidget *parent, const char *name)
00054   : KDialogBase (Plain,i18n("Archive/Delete Past Appointments"),
00055                  User1|User2|Cancel,User1,parent,name,false,true,
00056                  i18n("Archive"),i18n("Delete"))
00057 {
00058   mCalendar = cal;
00059 
00060   QFrame *topFrame = plainPage();
00061   QGridLayout *topLayout = new QGridLayout(topFrame);
00062   topLayout->setSpacing(spacingHint());
00063 
00064   QLabel *dateLabel = new QLabel(i18n("Appointments older than:"),topFrame);
00065   topLayout->addWidget(dateLabel,0,0);
00066 
00067   mDateEdit = new KDateEdit(topFrame);
00068   topLayout->addWidget(mDateEdit,0,1);
00069 
00070   QHBox *fileBox = new QHBox(topFrame);
00071   fileBox->setSpacing(spacingHint());
00072   topLayout->addMultiCellWidget(fileBox,1,1,0,1);
00073   (void)new QLabel(i18n("Archive file:"),fileBox);
00074   mArchiveFile = new KURLRequester (KOPrefs::instance()->mArchiveFile,fileBox);
00075   mArchiveFile->setMode(KFile::File);
00076   mArchiveFile->setFilter(i18n("*.vcs|vCalendar Files"));
00077   connect(mArchiveFile->lineEdit(),SIGNAL(textChanged ( const QString & )),this,SLOT(slotArchiveFileChanged(const QString &)));
00078   enableButton(KDialogBase::User1,!mArchiveFile->lineEdit()->text().isEmpty());
00079 }
00080 
00081 ArchiveDialog::~ArchiveDialog()
00082 {
00083 }
00084 
00085 void ArchiveDialog::slotArchiveFileChanged(const QString &text)
00086 {
00087     enableButton(KDialogBase::User1,!text.isEmpty());
00088 }
00089 
00090 // Archive old events
00091 void ArchiveDialog::slotUser1()
00092 {
00093   // Get destination URL
00094   KURL destUrl = mArchiveFile->url();
00095   if (destUrl.isMalformed()) {
00096     KMessageBox::sorry(this,i18n("The archive file name is not valid.\n"));
00097     return;
00098   }
00099   // Force filename to be ending with vCalendar extension
00100   QString filename = destUrl.fileName();
00101   if (filename.right(4) != ".vcs") {
00102     filename.append(".vcs");
00103     destUrl.setFileName(filename);
00104   }
00105 
00106   // Get events to be archived
00107   QPtrList<Event> events = mCalendar->events(QDate(1800,1,1),
00108                                              mDateEdit->date().addDays(-1),true);
00109   if (events.count() == 0) {
00110     KMessageBox::sorry(this,i18n("There are no events before %1")
00111         .arg(KGlobal::locale()->formatDate(mDateEdit->date())));
00112     return;
00113   }
00114 
00115   FileStorage storage( mCalendar );
00116 
00117   // Save current calendar to disk
00118   KTempFile tmpFile;
00119   tmpFile.setAutoDelete(true);
00120   storage.setFileName( tmpFile.name() );
00121   if ( !storage.save() ) {
00122     kdDebug() << "ArchiveDialog::slotUser1(): Can't save calendar to temp file" << endl;
00123     return;
00124   }
00125 
00126   // Duplicate current calendar by loading in new calendar object
00127   CalendarLocal archiveCalendar( KOPrefs::instance()->mTimeZoneId.local8Bit() );
00128 
00129   FileStorage archiveStore( &archiveCalendar );
00130   archiveStore.setFileName( tmpFile.name() );
00131   if (!archiveStore.load()) {
00132     kdDebug() << "ArchiveDialog::slotUser1(): Can't load calendar from temp file" << endl;
00133     return;
00134   }
00135 
00136   // Strip active events from calendar so that only events to be archived
00137   // remain.
00138   QPtrList<Event> activeEvents = archiveCalendar.events(mDateEdit->date(),
00139                                                         QDate(3000,1,1),
00140                                                         false);
00141   Event *ev;
00142   for(ev=activeEvents.first();ev;ev=activeEvents.next()) {
00143     archiveCalendar.deleteEvent(ev);
00144   }
00145 
00146   // Get or create the archive file
00147   QString archiveFile;
00148 
00149   if (KIO::NetAccess::exists(destUrl)) {
00150     if(!KIO::NetAccess::download(destUrl,archiveFile)) {
00151       kdDebug() << "ArchiveDialog::slotUser1(): Can't download archive file" << endl;
00152       return;
00153     }
00154     // Merge with events to be archived.
00155     archiveStore.setFileName( archiveFile );
00156     if ( !archiveStore.load() ) {
00157       kdDebug() << "ArchiveDialog::slotUser1(): Can't merge with archive file" << endl;
00158       return;
00159     }
00160 /*
00161     QPtrList<Event> es = archiveCalendar.events(QDate(1800,1,1),
00162                                                 QDate(3000,1,1),
00163                                                 false);
00164     kdDebug() << "--Following events in archive calendar:" << endl;
00165     Event *e;
00166     for(e=es.first();e;e=es.next()) {
00167       kdDebug() << "-----Event: " << e->getSummary() << endl;
00168     }
00169 */
00170   } else {
00171     archiveFile = tmpFile.name();
00172   }
00173 
00174   // Save archive calendar
00175   if ( !archiveStore.save() ) {
00176     KMessageBox::error(this,i18n("Cannot write archive file."));
00177     return;
00178   }
00179 
00180   // Upload if necessary
00181   KURL srcUrl;
00182   srcUrl.setPath(archiveFile);
00183   if (srcUrl != destUrl) {
00184     if (!KIO::NetAccess::upload(archiveFile,destUrl)) {
00185       KMessageBox::error(this,i18n("Cannot write archive to final destination."));
00186       return;
00187     }
00188   }
00189 
00190   KOPrefs::instance()->mArchiveFile = destUrl.url();
00191 
00192   KIO::NetAccess::removeTempFile(archiveFile);
00193 
00194   // Delete archived events from calendar
00195   for(ev=events.first();ev;ev=events.next()) {
00196     mCalendar->deleteEvent(ev);
00197   }
00198   emit eventsDeleted();
00199 
00200   accept();
00201 }
00202 
00203 // Delete old events
00204 void ArchiveDialog::slotUser2()
00205 {
00206   QPtrList<Event> events = mCalendar->events(QDate(1769,12,1),
00207                                              mDateEdit->date().addDays(-1),true);
00208 
00209   if (events.count() == 0) {
00210     KMessageBox::sorry(this,i18n("There are no events before %1")
00211         .arg(KGlobal::locale()->formatDate(mDateEdit->date())));
00212     return;
00213   }
00214 
00215   QStringList eventStrs;
00216   Event *ev;
00217   for(ev=events.first();ev;ev=events.next()) {
00218     eventStrs.append(ev->summary());
00219   }
00220 
00221   int result = KMessageBox::warningContinueCancelList(this,
00222       i18n("Delete all events before %1?\nThe following events will be deleted:")
00223       .arg(KGlobal::locale()->formatDate(mDateEdit->date())),eventStrs,
00224       i18n("Delete old events"),i18n("Delete"));
00225   if (result == KMessageBox::Continue) {
00226     for(ev=events.first();ev;ev=events.next()) {
00227       mCalendar->deleteEvent(ev);
00228     }
00229     emit eventsDeleted();
00230     accept();
00231   }
00232 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:41:08 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001