krop's commit fixes my problem in a better way, reverting
[kdepim.git] / korganizer / archivedialog.cpp
blob5235bedb2d55605009d39df88cb5c9f8a48070ad
1 /*
2 This file is part of KOrganizer.
4 Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 As a special exception, permission is given to link this program
22 with any edition of Qt, and distribute the resulting executable,
23 without including the source code for Qt in the source distribution.
26 // ArchiveDialog -- archive/delete past events.
28 #include "archivedialog.h"
29 #include "koprefs.h"
30 #include "eventarchiver.h"
32 #include <libkdepim/kdateedit.h>
34 #include <kdebug.h>
35 #include <klocale.h>
36 #include <knuminput.h>
37 #include <kurlrequester.h>
38 #include <kmessagebox.h>
39 #include <kfiledialog.h>
40 #include <kurl.h>
41 #include <klineedit.h>
42 #include <kvbox.h>
43 #include <KComboBox>
44 #include <KTextBrowser>
46 #include <QLabel>
47 #include <QLayout>
48 #include <QDateTime>
49 #include <QCheckBox>
50 #include <QVBoxLayout>
51 #include <QFrame>
52 #include <QHBoxLayout>
53 #include <QButtonGroup>
54 #include <QRadioButton>
56 #include "archivedialog.moc"
58 ArchiveDialog::ArchiveDialog( Calendar *cal, QWidget *parent )
59 : KDialog (parent)
61 setCaption( i18nc( "@title:window", "Archive/Delete Past Events and To-dos" ) );
62 setButtons( User1|Cancel );
63 setDefaultButton( User1 );
64 setModal( false );
65 showButtonSeparator( true );
66 setButtonText( User1, i18nc( "@action:button", "&Archive" ) );
67 mCalendar = cal;
69 QFrame *topFrame = new QFrame( this );
70 setMainWidget( topFrame );
71 QVBoxLayout *topLayout = new QVBoxLayout( topFrame );
72 topLayout->setSpacing( spacingHint() );
74 KTextBrowser *descLabel = new KTextBrowser( topFrame );
75 descLabel->setText(
76 i18nc( "@info:whatsthis",
77 "Archiving saves old items into the given file and "
78 "then deletes them in the current calendar. If the archive file "
79 "already exists they will be added. "
80 "(<link url=\"whatsthis:In order to add an archive "
81 "to your calendar, use the Merge Calendar function. "
82 "You can view an archive by opening it in KOrganizer like any "
83 "other calendar. It is not saved in a special format, but as "
84 "vCalendar.\">How to restore</link>)" ) );
85 descLabel->setTextInteractionFlags(
86 Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard |
87 Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard );
88 topLayout->addWidget( descLabel );
90 QButtonGroup *radioBG = new QButtonGroup( this );
91 connect( radioBG, SIGNAL(buttonClicked(int)), SLOT(slotActionChanged()) );
93 QHBoxLayout *dateLayout = new QHBoxLayout();
94 dateLayout->setMargin( 0 );
95 mArchiveOnceRB =
96 new QRadioButton( i18nc( "@option:radio", "Archive now items older than:" ),
97 topFrame );
98 dateLayout->addWidget( mArchiveOnceRB );
99 radioBG->addButton( mArchiveOnceRB );
100 mDateEdit = new KPIM::KDateEdit( topFrame );
101 mDateEdit->setWhatsThis(
102 i18nc( "@info:whatsthis",
103 "The date before which items should be archived. All older events "
104 "and to-dos will be saved and deleted, the newer (and events "
105 "exactly on that date) will be kept." ) );
106 dateLayout->addWidget( mDateEdit );
107 topLayout->addLayout( dateLayout );
109 // Checkbox, numinput and combo for auto-archiving (similar to kmail's
110 // mExpireFolderCheckBox/mReadExpiryTimeNumInput in kmfolderdia.cpp)
111 KHBox *autoArchiveHBox = new KHBox( topFrame );
112 topLayout->addWidget( autoArchiveHBox );
113 mAutoArchiveRB =
114 new QRadioButton( i18nc( "@option:radio", "Automaticall&y archive items older than:" ),
115 autoArchiveHBox );
116 radioBG->addButton( mAutoArchiveRB );
117 mAutoArchiveRB->setWhatsThis(
118 i18nc( "@info:whatsthis",
119 "If this feature is enabled, KOrganizer will regularly check if "
120 "events and to-dos have to be archived; this means you will not "
121 "need to use this dialog box again, except to change the settings." ) );
123 mExpiryTimeNumInput = new KIntNumInput( autoArchiveHBox );
124 mExpiryTimeNumInput->setRange( 1, 500, 1 );
125 mExpiryTimeNumInput->setSliderEnabled( false );
126 mExpiryTimeNumInput->setEnabled( false );
127 mExpiryTimeNumInput->setValue( 7 );
128 mExpiryTimeNumInput->setWhatsThis(
129 i18nc( "@info:whatsthis",
130 "The age of the events and to-dos to archive. All older items "
131 "will be saved and deleted, the newer will be kept." ) );
133 mExpiryUnitsComboBox = new KComboBox( autoArchiveHBox );
134 // Those items must match the "Expiry Unit" enum in the kcfg file!
135 mExpiryUnitsComboBox->addItem(
136 i18nc( "@item:inlistbox expires in daily units", "Day(s)" ) );
137 mExpiryUnitsComboBox->addItem(
138 i18nc( "@item:inlistbox expiration in weekly units", "Week(s)" ) );
139 mExpiryUnitsComboBox->addItem(
140 i18nc( "@item:inlistbox expiration in monthly units", "Month(s)" ) );
141 mExpiryUnitsComboBox->setEnabled( false );
143 QHBoxLayout *fileLayout = new QHBoxLayout();
144 fileLayout->setMargin( 0 );
145 fileLayout->setSpacing( spacingHint() );
146 QLabel *l = new QLabel( i18nc( "@label", "Archive &file:" ), topFrame );
147 fileLayout->addWidget(l);
148 mArchiveFile = new KUrlRequester( KOPrefs::instance()->mArchiveFile, topFrame );
149 mArchiveFile->setMode( KFile::File );
150 mArchiveFile->setFilter( i18nc( "@label filter for KUrlRequester", "*.ics|iCalendar Files" ) );
151 mArchiveFile->setWhatsThis(
152 i18nc( "@info:whatsthis",
153 "The path of the archive. The events and to-dos will be added to "
154 "the archive file, so any events that are already in the file "
155 "will not be modified or deleted. You can later load or merge the "
156 "file like any other calendar. It is not saved in a special "
157 "format, it uses the iCalendar format." ) );
158 l->setBuddy( mArchiveFile->lineEdit() );
159 fileLayout->addWidget( mArchiveFile );
160 topLayout->addLayout( fileLayout );
162 QGroupBox *typeBox = new QGroupBox( i18nc( "@title:group", "Type of Items to Archive" ) );
163 topLayout->addWidget( typeBox );
165 QBoxLayout *typeLayout = new QVBoxLayout( typeBox );
167 mEvents = new QCheckBox( i18nc( "@option:check", "&Events" ) );
168 typeLayout->addWidget( mEvents );
169 mTodos = new QCheckBox( i18nc( "@option:check", "&To-dos" ) );
170 typeLayout->addWidget( mTodos );
171 typeBox->setWhatsThis(
172 i18nc( "@info:whatsthis",
173 "Here you can select which items "
174 "should be archived. Events are archived if they "
175 "ended before the date given above; to-dos are archived if "
176 "they were finished before the date." ) );
178 mDeleteCb = new QCheckBox( i18nc( "@option:check", "&Delete only, do not save" ), topFrame );
179 mDeleteCb->setWhatsThis(
180 i18nc( "@info:whatsthis",
181 "Select this option to delete old events and to-dos without saving "
182 "them. It is not possible to recover the events later." ) );
183 topLayout->addWidget(mDeleteCb);
184 connect( mDeleteCb, SIGNAL(toggled(bool)), mArchiveFile, SLOT(setDisabled(bool)) );
185 connect( mDeleteCb, SIGNAL(toggled(bool)), this, SLOT(slotEnableUser1()) );
186 connect( mArchiveFile->lineEdit(), SIGNAL(textChanged(const QString &)),
187 this, SLOT(slotEnableUser1()) );
189 // Load settings from KOPrefs
190 mExpiryTimeNumInput->setValue( KOPrefs::instance()->mExpiryTime );
191 mExpiryUnitsComboBox->setCurrentIndex( KOPrefs::instance()->mExpiryUnit );
192 mDeleteCb->setChecked( KOPrefs::instance()->mArchiveAction == KOPrefs::actionDelete );
193 mEvents->setChecked( KOPrefs::instance()->mArchiveEvents );
194 mTodos->setChecked( KOPrefs::instance()->mArchiveTodos );
196 slotEnableUser1();
198 // The focus should go to a useful field by default, not to the top richtext-label
199 if ( KOPrefs::instance()->mAutoArchive ) {
200 mAutoArchiveRB->setChecked( true );
201 mAutoArchiveRB->setFocus();
202 } else {
203 mArchiveOnceRB->setChecked( true );
204 mArchiveOnceRB->setFocus();
206 slotActionChanged();
207 connect( this, SIGNAL(user1Clicked()), this, SLOT(slotUser1()) );
210 ArchiveDialog::~ArchiveDialog()
214 void ArchiveDialog::slotEnableUser1()
216 bool state = ( mDeleteCb->isChecked() || !mArchiveFile->lineEdit()->text().isEmpty() );
217 enableButton( KDialog::User1, state );
220 void ArchiveDialog::slotActionChanged()
222 mDateEdit->setEnabled( mArchiveOnceRB->isChecked() );
223 mExpiryTimeNumInput->setEnabled( mAutoArchiveRB->isChecked() );
224 mExpiryUnitsComboBox->setEnabled( mAutoArchiveRB->isChecked() );
227 // Archive old events
228 void ArchiveDialog::slotUser1()
230 EventArchiver archiver;
231 connect( &archiver, SIGNAL(eventsDeleted()), this, SLOT(slotEventsDeleted()) );
233 KOPrefs::instance()->mAutoArchive = mAutoArchiveRB->isChecked();
234 KOPrefs::instance()->mExpiryTime = mExpiryTimeNumInput->value();
235 KOPrefs::instance()->mExpiryUnit = mExpiryUnitsComboBox->currentIndex();
237 if ( mDeleteCb->isChecked() ) {
238 KOPrefs::instance()->mArchiveAction = KOPrefs::actionDelete;
239 } else {
240 KOPrefs::instance()->mArchiveAction = KOPrefs::actionArchive;
242 // Get destination URL
243 KUrl destUrl( mArchiveFile->url() );
244 if ( !destUrl.isValid() ) {
245 KMessageBox::sorry( this, i18nc( "@info", "The archive file name is not valid." ) );
246 return;
248 // Force filename to be ending with vCalendar extension
249 QString filename = destUrl.fileName();
250 if ( !filename.endsWith( QLatin1String( ".vcs" ) ) &&
251 !filename.endsWith( QLatin1String( ".ics" ) ) ) {
252 filename.append( QLatin1String( ".ics" ) );
253 destUrl.setFileName( filename );
256 KOPrefs::instance()->mArchiveFile = destUrl.url();
258 if ( KOPrefs::instance()->mAutoArchive ) {
259 archiver.runAuto( mCalendar, this, true /*with gui*/);
260 emit autoArchivingSettingsModified();
261 accept();
262 } else {
263 archiver.runOnce( mCalendar, mDateEdit->date(), this );
267 void ArchiveDialog::slotEventsDeleted()
269 emit eventsDeleted();
270 if ( !KOPrefs::instance()->mAutoArchive ) {
271 accept();