korganizer

archivedialog.cpp
1 /*
2  This file is part of KOrganizer.
3  Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
4  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 // ArchiveDialog -- archive/delete past events.
26 
27 #include <tqlabel.h>
28 #include <tqlayout.h>
29 #include <tqdatetime.h>
30 #include <tqcheckbox.h>
31 #include <tqwhatsthis.h>
32 #include <tqhgroupbox.h>
33 
34 #include <kdebug.h>
35 #include <tdelocale.h>
36 #include <kurlrequester.h>
37 #include <tdemessagebox.h>
38 #include <tdefiledialog.h>
39 #include <kurl.h>
40 #include <klineedit.h>
41 #include <kactivelabel.h>
42 
43 #include <libtdepim/kdateedit.h>
44 
45 #include "koprefs.h"
46 
47 #include "archivedialog.h"
48 #include "eventarchiver.h"
49 #include <knuminput.h>
50 #include <tqbuttongroup.h>
51 #include <tqradiobutton.h>
52 #include "archivedialog.moc"
53 
54 ArchiveDialog::ArchiveDialog(Calendar *cal,TQWidget *parent, const char *name)
55  : KDialogBase (Plain,i18n("Archive/Delete Past Events and To-dos"),
56  User1|Cancel,User1,parent,name,false,true,
57  i18n("&Archive"))
58 {
59  mCalendar = cal;
60 
61  TQFrame *topFrame = plainPage();
62  TQVBoxLayout *topLayout = new TQVBoxLayout(topFrame);
63  topLayout->setSpacing(spacingHint());
64 
65  KActiveLabel *descLabel = new KActiveLabel(
66  i18n("Archiving saves old items into the given file and "
67  "then deletes them in the current calendar. If the archive file "
68  "already exists they will be added. "
69  "(<a href=\"whatsthis:In order to add an archive "
70  "to your calendar, use the &quot;Merge Calendar&quot; function. "
71  "You can view an archive by opening it in KOrganizer like any "
72  "other calendar. It is not saved in a special format, but as "
73  "vCalendar.\">How to restore</a>)"),
74  topFrame);
75  topLayout->addWidget(descLabel);
76 
77  TQButtonGroup* radioBG = new TQButtonGroup( this );
78  radioBG->hide(); // just for the exclusive behavior
79  connect( radioBG, TQ_SIGNAL( clicked( int ) ), TQ_SLOT( slotActionChanged() ) );
80 
81  TQHBoxLayout *dateLayout = new TQHBoxLayout(0);
82  mArchiveOnceRB = new TQRadioButton(i18n("Archive now items older than:"),topFrame);
83  dateLayout->addWidget(mArchiveOnceRB);
84  radioBG->insert(mArchiveOnceRB);
85  mDateEdit = new KDateEdit(topFrame);
86  TQWhatsThis::add(mDateEdit,
87  i18n("The date before which items should be archived. All older events and to-dos will "
88  "be saved and deleted, the newer (and events exactly on that date) will be kept."));
89  dateLayout->addWidget(mDateEdit);
90  topLayout->addLayout(dateLayout);
91 
92  // Checkbox, numinput and combo for auto-archiving
93  // (similar to kmail's mExpireFolderCheckBox/mReadExpiryTimeNumInput in kmfolderdia.cpp)
94  TQHBox* autoArchiveHBox = new TQHBox(topFrame);
95  topLayout->addWidget(autoArchiveHBox);
96  mAutoArchiveRB = new TQRadioButton(i18n("Automaticall&y archive items older than:"), autoArchiveHBox);
97  radioBG->insert(mAutoArchiveRB);
98  TQWhatsThis::add(mAutoArchiveRB,
99  i18n("If this feature is enabled, KOrganizer will regularly check if events and to-dos have to be archived; "
100  "this means you will not need to use this dialog box again, except to change the settings."));
101 
102  mExpiryTimeNumInput = new KIntNumInput(autoArchiveHBox);
103  mExpiryTimeNumInput->setRange(1, 500, 1, false);
104  mExpiryTimeNumInput->setEnabled(false);
105  mExpiryTimeNumInput->setValue(7);
106  TQWhatsThis::add(mExpiryTimeNumInput,
107  i18n("The age of the events and to-dos to archive. All older items "
108  "will be saved and deleted, the newer will be kept."));
109 
110  mExpiryUnitsComboBox = new TQComboBox(autoArchiveHBox);
111  // Those items must match the "Expiry Unit" enum in the kcfg file!
112  mExpiryUnitsComboBox->insertItem(i18n("Day(s)"));
113  mExpiryUnitsComboBox->insertItem(i18n("Week(s)"));
114  mExpiryUnitsComboBox->insertItem(i18n("Month(s)"));
115  mExpiryUnitsComboBox->setEnabled(false);
116 
117  TQHBoxLayout *fileLayout = new TQHBoxLayout(0);
118  fileLayout->setSpacing(spacingHint());
119  TQLabel *l = new TQLabel(i18n("Archive &file:"),topFrame);
120  fileLayout->addWidget(l);
121  mArchiveFile = new KURLRequester(KOPrefs::instance()->mArchiveFile,topFrame);
122  mArchiveFile->setMode(KFile::File);
123  mArchiveFile->setFilter(i18n("*.ics|iCalendar Files"));
124  TQWhatsThis::add(mArchiveFile,
125  i18n("The path of the archive. The events and to-dos will be added to the "
126  "archive file, so any events that are already in the file "
127  "will not be modified or deleted. You can later load or merge the "
128  "file like any other calendar. It is not saved in a special "
129  "format, it uses the iCalendar format. "));
130  l->setBuddy(mArchiveFile->lineEdit());
131  fileLayout->addWidget(mArchiveFile);
132  topLayout->addLayout(fileLayout);
133 
134  TQHGroupBox *typeBox = new TQHGroupBox( i18n("Type of Items to Archive"),
135  topFrame);
136  mEvents = new TQCheckBox( i18n("&Events"), typeBox );
137  mTodos = new TQCheckBox( i18n("Completed &To-dos"), typeBox );
138  topLayout->addWidget( typeBox );
139  TQWhatsThis::add( typeBox, i18n("Here you can select which items "
140  "should be archived. Events are archived if they "
141  "ended before the date given above; to-dos are archived if "
142  "they were finished before the date.") );
143 
144  mDeleteCb = new TQCheckBox(i18n("&Delete only, do not save"),
145  topFrame);
146  TQWhatsThis::add(mDeleteCb,
147  i18n("Select this option to delete old events and to-dos without saving them. "
148  "It is not possible to recover the events later."));
149  topLayout->addWidget(mDeleteCb);
150  connect(mDeleteCb, TQ_SIGNAL(toggled(bool)), mArchiveFile, TQ_SLOT(setDisabled(bool)));
151  connect(mDeleteCb, TQ_SIGNAL(toggled(bool)), this, TQ_SLOT(slotEnableUser1()));
152  connect(mArchiveFile->lineEdit(),TQ_SIGNAL(textChanged ( const TQString & )),
153  this,TQ_SLOT(slotEnableUser1()));
154 
155  // Load settings from KOPrefs
156  mExpiryTimeNumInput->setValue( KOPrefs::instance()->mExpiryTime );
157  mExpiryUnitsComboBox->setCurrentItem( KOPrefs::instance()->mExpiryUnit );
158  mDeleteCb->setChecked( KOPrefs::instance()->mArchiveAction == KOPrefs::actionDelete );
159  mEvents->setChecked( KOPrefs::instance()->mArchiveEvents );
160  mTodos->setChecked( KOPrefs::instance()->mArchiveTodos );
161 
162  slotEnableUser1();
163 
164  // The focus should go to a useful field by default, not to the top richtext-label
165  if ( KOPrefs::instance()->mAutoArchive ) {
166  mAutoArchiveRB->setChecked( true );
167  mAutoArchiveRB->setFocus();
168  } else {
169  mArchiveOnceRB->setChecked( true );
170  mArchiveOnceRB->setFocus();
171  }
172  slotActionChanged();
173 }
174 
175 ArchiveDialog::~ArchiveDialog()
176 {
177 }
178 
179 void ArchiveDialog::slotEnableUser1()
180 {
181  bool state = ( mDeleteCb->isChecked() ||
182  !mArchiveFile->lineEdit()->text().isEmpty() );
183  enableButton(KDialogBase::User1,state);
184 }
185 
186 void ArchiveDialog::slotActionChanged()
187 {
188  mDateEdit->setEnabled( mArchiveOnceRB->isChecked() );
189  mExpiryTimeNumInput->setEnabled( mAutoArchiveRB->isChecked() );
190  mExpiryUnitsComboBox->setEnabled( mAutoArchiveRB->isChecked() );
191 }
192 
193 // Archive old events
194 void ArchiveDialog::slotUser1()
195 {
196  EventArchiver archiver;
197  connect( &archiver, TQ_SIGNAL( eventsDeleted() ), this, TQ_SLOT( slotEventsDeleted() ) );
198 
199  KOPrefs::instance()->mAutoArchive = mAutoArchiveRB->isChecked();
200  KOPrefs::instance()->mExpiryTime = mExpiryTimeNumInput->value();
201  KOPrefs::instance()->mExpiryUnit = mExpiryUnitsComboBox->currentItem();
202 
203  if (mDeleteCb->isChecked()) {
204  KOPrefs::instance()->mArchiveAction = KOPrefs::actionDelete;
205  } else {
206  KOPrefs::instance()->mArchiveAction = KOPrefs::actionArchive;
207 
208  // Get destination URL
209  KURL destUrl( mArchiveFile->url() );
210  if ( !destUrl.isValid() ) {
211  KMessageBox::sorry(this,i18n("The archive file name is not valid.\n"));
212  return;
213  }
214  // Force filename to be ending with vCalendar extension
215  TQString filename = destUrl.fileName();
216  if (!filename.endsWith(".vcs") && !filename.endsWith(".ics")) {
217  filename.append(".ics");
218  destUrl.setFileName(filename);
219  }
220 
221  KOPrefs::instance()->mArchiveFile = destUrl.url();
222  }
223  if ( KOPrefs::instance()->mAutoArchive ) {
224  archiver.runAuto( mCalendar, this, true /*with gui*/ );
225  emit autoArchivingSettingsModified();
226  accept();
227  }
228  else
229  archiver.runOnce( mCalendar, mDateEdit->date(), this );
230 }
231 
232 void ArchiveDialog::slotEventsDeleted()
233 {
234  emit eventsDeleted();
235  if ( !KOPrefs::instance()->mAutoArchive )
236  accept();
237 }
This class handles expiring and archiving of events.
Definition: eventarchiver.h:48
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.