korganizer

archivedialog.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
00004     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 // ArchiveDialog -- archive/delete past events.
00026 
00027 #include <qlabel.h>
00028 #include <qlayout.h>
00029 #include <qdatetime.h>
00030 #include <qcheckbox.h>
00031 #include <qwhatsthis.h>
00032 #include <qhgroupbox.h>
00033 
00034 #include <kdebug.h>
00035 #include <klocale.h>
00036 #include <kurlrequester.h>
00037 #include <kmessagebox.h>
00038 #include <kfiledialog.h>
00039 #include <kurl.h>
00040 #include <klineedit.h>
00041 #include <kactivelabel.h>
00042 
00043 #include <libkdepim/kdateedit.h>
00044 
00045 #include "koprefs.h"
00046 
00047 #include "archivedialog.h"
00048 #include "eventarchiver.h"
00049 #include <knuminput.h>
00050 #include <qbuttongroup.h>
00051 #include <qradiobutton.h>
00052 #include "archivedialog.moc"
00053 
00054 ArchiveDialog::ArchiveDialog(Calendar *cal,QWidget *parent, const char *name)
00055   : KDialogBase (Plain,i18n("Archive/Delete Past Events and To-dos"),
00056                  User1|Cancel,User1,parent,name,false,true,
00057                  i18n("&Archive"))
00058 {
00059   mCalendar = cal;
00060 
00061   QFrame *topFrame = plainPage();
00062   QVBoxLayout *topLayout = new QVBoxLayout(topFrame);
00063   topLayout->setSpacing(spacingHint());
00064 
00065   KActiveLabel *descLabel = new KActiveLabel(
00066     i18n("Archiving saves old items into the given file and "
00067          "then deletes them in the current calendar. If the archive file "
00068          "already exists they will be added. "
00069          "(<a href=\"whatsthis:In order to add an archive "
00070          "to your calendar, use the &quot;Merge Calendar&quot; function. "
00071          "You can view an archive by opening it in KOrganizer like any "
00072          "other calendar. It is not saved in a special format, but as "
00073          "vCalendar.\">How to restore</a>)"),
00074     topFrame);
00075   topLayout->addWidget(descLabel);
00076 
00077   QButtonGroup* radioBG = new QButtonGroup( this );
00078   radioBG->hide(); // just for the exclusive behavior
00079   connect( radioBG, SIGNAL( clicked( int ) ), SLOT( slotActionChanged() ) );
00080 
00081   QHBoxLayout *dateLayout = new QHBoxLayout(0);
00082   mArchiveOnceRB = new QRadioButton(i18n("Archive now items older than:"),topFrame);
00083   dateLayout->addWidget(mArchiveOnceRB);
00084   radioBG->insert(mArchiveOnceRB);
00085   mDateEdit = new KDateEdit(topFrame);
00086   QWhatsThis::add(mDateEdit,
00087     i18n("The date before which items should be archived. All older events and to-dos will "
00088          "be saved and deleted, the newer (and events exactly on that date) will be kept."));
00089   dateLayout->addWidget(mDateEdit);
00090   topLayout->addLayout(dateLayout);
00091 
00092   // Checkbox, numinput and combo for auto-archiving
00093   // (similar to kmail's mExpireFolderCheckBox/mReadExpiryTimeNumInput in kmfolderdia.cpp)
00094   QHBox* autoArchiveHBox = new QHBox(topFrame);
00095   topLayout->addWidget(autoArchiveHBox);
00096   mAutoArchiveRB = new QRadioButton(i18n("Automaticall&y archive items older than:"), autoArchiveHBox);
00097   radioBG->insert(mAutoArchiveRB);
00098   QWhatsThis::add(mAutoArchiveRB,
00099     i18n("If this feature is enabled, KOrganizer will regularly check if events and to-dos have to be archived; "
00100          "this means you will not need to use this dialog box again, except to change the settings."));
00101 
00102   mExpiryTimeNumInput = new KIntNumInput(autoArchiveHBox);
00103   mExpiryTimeNumInput->setRange(1, 500, 1, false);
00104   mExpiryTimeNumInput->setEnabled(false);
00105   mExpiryTimeNumInput->setValue(7);
00106   QWhatsThis::add(mExpiryTimeNumInput,
00107     i18n("The age of the events and to-dos to archive. All older items "
00108          "will be saved and deleted, the newer will be kept."));
00109 
00110   mExpiryUnitsComboBox = new QComboBox(autoArchiveHBox);
00111   // Those items must match the "Expiry Unit" enum in the kcfg file!
00112   mExpiryUnitsComboBox->insertItem(i18n("Day(s)"));
00113   mExpiryUnitsComboBox->insertItem(i18n("Week(s)"));
00114   mExpiryUnitsComboBox->insertItem(i18n("Month(s)"));
00115   mExpiryUnitsComboBox->setEnabled(false);
00116 
00117   QHBoxLayout *fileLayout = new QHBoxLayout(0);
00118   fileLayout->setSpacing(spacingHint());
00119   QLabel *l = new QLabel(i18n("Archive &file:"),topFrame);
00120   fileLayout->addWidget(l);
00121   mArchiveFile = new KURLRequester(KOPrefs::instance()->mArchiveFile,topFrame);
00122   mArchiveFile->setMode(KFile::File);
00123   mArchiveFile->setFilter(i18n("*.ics|iCalendar Files"));
00124   QWhatsThis::add(mArchiveFile,
00125     i18n("The path of the archive. The events and to-dos will be added to the "
00126          "archive file, so any events that are already in the file "
00127          "will not be modified or deleted. You can later load or merge the "
00128          "file like any other calendar. It is not saved in a special "
00129          "format, it uses the iCalendar format. "));
00130   l->setBuddy(mArchiveFile->lineEdit());
00131   fileLayout->addWidget(mArchiveFile);
00132   topLayout->addLayout(fileLayout);
00133 
00134   QHGroupBox *typeBox = new QHGroupBox( i18n("Type of Items to Archive"),
00135                                         topFrame);
00136   mEvents = new QCheckBox( i18n("&Events"), typeBox );
00137   mTodos = new QCheckBox( i18n("Completed &To-dos"), typeBox );
00138   topLayout->addWidget( typeBox );
00139   QWhatsThis::add( typeBox, i18n("Here you can select which items "
00140                    "should be archived. Events are archived if they "
00141                    "ended before the date given above; to-dos are archived if "
00142                    "they were finished before the date.") );
00143 
00144   mDeleteCb = new QCheckBox(i18n("&Delete only, do not save"),
00145                             topFrame);
00146   QWhatsThis::add(mDeleteCb,
00147     i18n("Select this option to delete old events and to-dos without saving them. "
00148          "It is not possible to recover the events later."));
00149   topLayout->addWidget(mDeleteCb);
00150   connect(mDeleteCb, SIGNAL(toggled(bool)), mArchiveFile, SLOT(setDisabled(bool)));
00151   connect(mDeleteCb, SIGNAL(toggled(bool)), this, SLOT(slotEnableUser1()));
00152   connect(mArchiveFile->lineEdit(),SIGNAL(textChanged ( const QString & )),
00153           this,SLOT(slotEnableUser1()));
00154 
00155   QVBoxLayout *archiveModeLayout = new QVBoxLayout(0);
00156   archiveModeLayout->setMargin(marginHint());
00157   mArchiveOwnFolders = new QRadioButton( i18n("Archive own folders only (no shared folders)"), this );
00158   mArchiveAllFolders = new QRadioButton( i18n("Archive all folders with write access (including shared folders)"), this );
00159   archiveModeLayout->addWidget( new QLabel( i18n("<b>Warning:</b> Archiving all folders will archive/delete the events and to-dos from <b>all</b> calendars you have write access to, including shared folders!"), this ) );
00160   archiveModeLayout->addWidget( mArchiveOwnFolders );
00161   archiveModeLayout->addWidget( mArchiveAllFolders );
00162   topLayout->addLayout( archiveModeLayout );
00163 
00164   QButtonGroup *archiveModeGroup = new QButtonGroup(this);
00165   archiveModeGroup->hide();
00166   archiveModeGroup->insert(mArchiveOwnFolders);
00167   archiveModeGroup->insert(mArchiveAllFolders);
00168 
00169   // Load settings from KOPrefs
00170   mExpiryTimeNumInput->setValue( KOPrefs::instance()->mExpiryTime );
00171   mExpiryUnitsComboBox->setCurrentItem( KOPrefs::instance()->mExpiryUnit );
00172   mDeleteCb->setChecked( KOPrefs::instance()->mArchiveAction == KOPrefs::actionDelete );
00173   mEvents->setChecked( KOPrefs::instance()->mArchiveEvents );
00174   mTodos->setChecked( KOPrefs::instance()->mArchiveTodos );
00175   if ( KOPrefs::instance()->mArchiveOwnFoldersOnly ) {
00176     mArchiveOwnFolders->setChecked( true );
00177   } else {
00178     mArchiveAllFolders->setChecked( true );
00179   }
00180 
00181   slotEnableUser1();
00182 
00183   // The focus should go to a useful field by default, not to the top richtext-label
00184   if ( KOPrefs::instance()->mAutoArchive ) {
00185     mAutoArchiveRB->setChecked( true );
00186     mAutoArchiveRB->setFocus();
00187   } else {
00188     mArchiveOnceRB->setChecked( true );
00189     mArchiveOnceRB->setFocus();
00190   }
00191   slotActionChanged();
00192 }
00193 
00194 ArchiveDialog::~ArchiveDialog()
00195 {
00196 }
00197 
00198 void ArchiveDialog::slotEnableUser1()
00199 {
00200   bool state = ( mDeleteCb->isChecked() ||
00201                  !mArchiveFile->lineEdit()->text().isEmpty() );
00202   enableButton(KDialogBase::User1,state);
00203 }
00204 
00205 void ArchiveDialog::slotActionChanged()
00206 {
00207   mDateEdit->setEnabled( mArchiveOnceRB->isChecked() );
00208   mExpiryTimeNumInput->setEnabled( mAutoArchiveRB->isChecked() );
00209   mExpiryUnitsComboBox->setEnabled( mAutoArchiveRB->isChecked() );
00210 }
00211 
00212 // Archive old events
00213 void ArchiveDialog::slotUser1()
00214 {
00215   EventArchiver archiver;
00216   connect( &archiver, SIGNAL( eventsDeleted() ), this, SLOT( slotEventsDeleted() ) );
00217 
00218   KOPrefs::instance()->mAutoArchive = mAutoArchiveRB->isChecked();
00219   KOPrefs::instance()->mExpiryTime = mExpiryTimeNumInput->value();
00220   KOPrefs::instance()->mExpiryUnit = mExpiryUnitsComboBox->currentItem();
00221 
00222   KOPrefs::instance()->mArchiveOwnFoldersOnly = mArchiveOwnFolders->isChecked();
00223 
00224   if (mDeleteCb->isChecked()) {
00225     KOPrefs::instance()->mArchiveAction = KOPrefs::actionDelete;
00226   } else {
00227     KOPrefs::instance()->mArchiveAction = KOPrefs::actionArchive;
00228 
00229     // Get destination URL
00230     KURL destUrl( mArchiveFile->url() );
00231     if ( !destUrl.isValid() ) {
00232       KMessageBox::sorry(this,i18n("The archive file name is not valid.\n"));
00233       return;
00234     }
00235     // Force filename to be ending with vCalendar extension
00236     QString filename = destUrl.fileName();
00237     if (!filename.endsWith(".vcs") && !filename.endsWith(".ics")) {
00238       filename.append(".ics");
00239       destUrl.setFileName(filename);
00240     }
00241 
00242     KOPrefs::instance()->mArchiveFile = destUrl.url();
00243   }
00244   if ( KOPrefs::instance()->mAutoArchive ) {
00245     archiver.runAuto( mCalendar, this, true /*with gui*/ );
00246     emit autoArchivingSettingsModified();
00247     accept();
00248   }
00249   else
00250     archiver.runOnce( mCalendar, mDateEdit->date(), this );
00251 }
00252 
00253 void ArchiveDialog::slotEventsDeleted()
00254 {
00255   emit eventsDeleted();
00256   if ( !KOPrefs::instance()->mAutoArchive )
00257     accept();
00258 }
KDE Home | KDE Accessibility Home | Description of Access Keys