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 <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
00091 void ArchiveDialog::slotUser1()
00092 {
00093
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
00100 QString filename = destUrl.fileName();
00101 if (filename.right(4) != ".vcs") {
00102 filename.append(".vcs");
00103 destUrl.setFileName(filename);
00104 }
00105
00106
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
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
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
00137
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
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
00155 archiveStore.setFileName( archiveFile );
00156 if ( !archiveStore.load() ) {
00157 kdDebug() << "ArchiveDialog::slotUser1(): Can't merge with archive file" << endl;
00158 return;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 } else {
00171 archiveFile = tmpFile.name();
00172 }
00173
00174
00175 if ( !archiveStore.save() ) {
00176 KMessageBox::error(this,i18n("Cannot write archive file."));
00177 return;
00178 }
00179
00180
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
00195 for(ev=events.first();ev;ev=events.next()) {
00196 mCalendar->deleteEvent(ev);
00197 }
00198 emit eventsDeleted();
00199
00200 accept();
00201 }
00202
00203
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 }