kalarm

templatedlg.cpp
1/*
2 * templatedlg.cpp - dialogue to create, edit and delete alarm templates
3 * Program: kalarm
4 * Copyright © 2004-2006 by David Jarvie <software@astrojar.org.uk>
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include "kalarm.h"
22
23#include <tqlayout.h>
24#include <tqpushbutton.h>
25#include <tqwhatsthis.h>
26
27#include <tdelocale.h>
28#include <kguiitem.h>
29#include <tdemessagebox.h>
30#include <tdeaccel.h>
31#include <kdebug.h>
32
33#include "editdlg.h"
34#include "alarmcalendar.h"
35#include "functions.h"
36#include "templatelistview.h"
37#include "undo.h"
38#include "templatedlg.moc"
39
40static const char TMPL_DIALOG_NAME[] = "TemplateDialog";
41
42
43TemplateDlg* TemplateDlg::mInstance = 0;
44
45
46TemplateDlg::TemplateDlg(TQWidget* parent, const char* name)
47 : KDialogBase(KDialogBase::Plain, i18n("Alarm Templates"), Close, Ok, parent, name, false, true)
48{
49 TQWidget* topWidget = plainPage();
50 TQBoxLayout* topLayout = new TQHBoxLayout(topWidget);
51 topLayout->setSpacing(spacingHint());
52
53 TQBoxLayout* layout = new TQVBoxLayout(topLayout);
54 mTemplateList = new TemplateListView(true, i18n("The list of alarm templates"), topWidget);
55 mTemplateList->setSelectionMode(TQListView::Extended);
56 mTemplateList->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding));
57 connect(mTemplateList, TQ_SIGNAL(selectionChanged()), TQ_SLOT(slotSelectionChanged()));
58 layout->addWidget(mTemplateList);
59
60 layout = new TQVBoxLayout(topLayout);
61 TQPushButton* button = new TQPushButton(i18n("&New..."), topWidget);
62 connect(button, TQ_SIGNAL(clicked()), TQ_SLOT(slotNew()));
63 TQWhatsThis::add(button, i18n("Create a new alarm template"));
64 layout->addWidget(button);
65
66 mEditButton = new TQPushButton(i18n("&Edit..."), topWidget);
67 connect(mEditButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotEdit()));
68 TQWhatsThis::add(mEditButton, i18n("Edit the currently highlighted alarm template"));
69 layout->addWidget(mEditButton);
70
71 mCopyButton = new TQPushButton(i18n("Co&py"), topWidget);
72 connect(mCopyButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotCopy()));
73 TQWhatsThis::add(mCopyButton,
74 i18n("Create a new alarm template based on a copy of the currently highlighted template"));
75 layout->addWidget(mCopyButton);
76
77 mDeleteButton = new TQPushButton(i18n("&Delete"), topWidget);
78 connect(mDeleteButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotDelete()));
79 TQWhatsThis::add(mDeleteButton, i18n("Delete the currently highlighted alarm template"));
80 layout->addWidget(mDeleteButton);
81
82 TDEAccel* accel = new TDEAccel(this);
83 accel->insert(TDEStdAccel::SelectAll, mTemplateList, TQ_SLOT(slotSelectAll()));
84 accel->insert(TDEStdAccel::Deselect, mTemplateList, TQ_SLOT(slotDeselect()));
85 accel->readSettings();
86
87 mTemplateList->refresh();
88 slotSelectionChanged(); // enable/disable buttons as appropriate
89
90 TQSize s;
91 if (KAlarm::readConfigWindowSize(TMPL_DIALOG_NAME, s))
92 resize(s);
93}
94
95/******************************************************************************
96* Destructor.
97*/
98TemplateDlg::~TemplateDlg()
99{
100 mInstance = 0;
101}
102
103/******************************************************************************
104* Create an instance, if none already exists.
105*/
106TemplateDlg* TemplateDlg::create(TQWidget* parent, const char* name)
107{
108 if (mInstance)
109 return 0;
110 mInstance = new TemplateDlg(parent, name);
111 return mInstance;
112}
113
114/******************************************************************************
115* Called when the New Template button is clicked to create a new template
116* based on the currently selected alarm.
117*/
118void TemplateDlg::slotNew()
119{
120 createTemplate(0, this, mTemplateList);
121}
122
123/******************************************************************************
124* Called when the Copy button is clicked to edit a copy of an existing alarm,
125* to add to the list.
126*/
127void TemplateDlg::slotCopy()
128{
129 TemplateListViewItem* item = mTemplateList->selectedItem();
130 if (item)
131 {
132 KAEvent event = item->event();
133 createTemplate(&event, mTemplateList);
134 }
135}
136
137/******************************************************************************
138* Create a new template.
139* If 'event' is non-zero, base the new template on an existing event or template.
140*/
141void TemplateDlg::createTemplate(const KAEvent* event, TQWidget* parent, TemplateListView* view)
142{
143 EditAlarmDlg editDlg(true, i18n("New Alarm Template"), parent, 0, event);
144 if (editDlg.exec() == TQDialog::Accepted)
145 {
146 KAEvent event;
147 editDlg.getEvent(event);
148
149 // Add the template to the displayed lists and to the calendar file
150 KAlarm::addTemplate(event, view, &editDlg);
151 Undo::saveAdd(event);
152 }
153}
154
155/******************************************************************************
156* Called when the Modify button is clicked to edit the currently highlighted
157* alarm in the list.
158*/
159void TemplateDlg::slotEdit()
160{
161 TemplateListViewItem* item = mTemplateList->selectedItem();
162 if (item)
163 {
164 KAEvent event = item->event();
165 EditAlarmDlg editDlg(true, i18n("Edit Alarm Template"), this, 0, &event);
166 if (editDlg.exec() == TQDialog::Accepted)
167 {
168 KAEvent newEvent;
169 editDlg.getEvent(newEvent);
170 TQString id = event.id();
171 newEvent.setEventID(id);
172
173 // Update the event in the displays and in the calendar file
174 KAlarm::updateTemplate(newEvent, mTemplateList, &editDlg);
175 Undo::saveEdit(event, newEvent);
176 }
177 }
178}
179
180/******************************************************************************
181* Called when the Delete button is clicked to delete the currently highlighted
182* alarms in the list.
183*/
184void TemplateDlg::slotDelete()
185{
186 TQValueList<EventListViewItemBase*> items = mTemplateList->selectedItems();
187 int n = items.count();
188 if (KMessageBox::warningContinueCancel(this, i18n("Do you really want to delete the selected alarm template?",
189 "Do you really want to delete the %n selected alarm templates?", n),
190 i18n("Delete Alarm Template", "Delete Alarm Templates", n), KGuiItem(i18n("&Delete"), "edit-delete"))
191 != KMessageBox::Continue)
192 return;
193
194 int warnErr = 0;
195 KAlarm::UpdateStatus status = KAlarm::UPDATE_OK;
196 TQValueList<KAEvent> events;
197 AlarmCalendar::templateCalendar()->startUpdate(); // prevent multiple saves of the calendar until we're finished
198 for (TQValueList<EventListViewItemBase*>::Iterator it = items.begin(); it != items.end(); ++it)
199 {
200 TemplateListViewItem* item = (TemplateListViewItem*)(*it);
201 events.append(item->event());
202 KAlarm::UpdateStatus st = KAlarm::deleteTemplate(item->event());
203 if (st != KAlarm::UPDATE_OK)
204 {
205 status = st;
206 ++warnErr;
207 }
208 }
209 if (!AlarmCalendar::templateCalendar()->endUpdate()) // save the calendar now
210 {
211 status = KAlarm::SAVE_FAILED;
212 warnErr = items.count();
213 }
214 Undo::saveDeletes(events);
215 if (warnErr)
216 displayUpdateError(this, status, KAlarm::ERR_TEMPLATE, warnErr);
217}
218
219/******************************************************************************
220* Called when the group of items selected changes.
221* Enable/disable the buttons depending on whether/how many templates are
222* currently highlighted.
223*/
224void TemplateDlg::slotSelectionChanged()
225{
226 int count = mTemplateList->selectedCount();
227 mEditButton->setEnabled(count == 1);
228 mCopyButton->setEnabled(count == 1);
229 mDeleteButton->setEnabled(count);
230}
231
232/******************************************************************************
233* Called when the dialog's size has changed.
234* Records the new size in the config file.
235*/
236void TemplateDlg::resizeEvent(TQResizeEvent* re)
237{
238 if (isVisible())
239 KAlarm::writeConfigWindowSize(TMPL_DIALOG_NAME, re->size());
240 KDialog::resizeEvent(re);
241}
KAEvent corresponds to a KCal::Event instance.
Definition: alarmevent.h:232
miscellaneous functions
UpdateStatus
Return codes from calendar update functions.
Definition: functions.h:51
undo/redo facility