Build with clang.
[kdepim.git] / kalarm / templatedlg.cpp
blob407fac214fe037004c67166ddab8d34e7484a760
1 /*
2 * templatedlg.cpp - dialog to create, edit and delete alarm templates
3 * Program: kalarm
4 * Copyright © 2004-2011 by David Jarvie <djarvie@kde.org>
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.
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.
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.
21 #include "kalarm.h"
22 #include "templatedlg.moc"
24 #include "editdlg.h"
25 #include "alarmcalendar.h"
26 #ifndef USE_AKONADI
27 #include "alarmresources.h"
28 #include "eventlistmodel.h"
29 #include "templatelistfiltermodel.h"
30 #endif
31 #include "functions.h"
32 #include "messagebox.h"
33 #include "newalarmaction.h"
34 #include "shellprocess.h"
35 #include "templatelistview.h"
36 #include "undo.h"
38 #include <klocale.h>
39 #include <kguiitem.h>
40 #include <kdebug.h>
41 #include <kstandardaction.h>
42 #include <kactioncollection.h>
43 #include <kaction.h>
44 #include <kmenu.h>
46 #include <QPushButton>
47 #include <QList>
48 #include <QVBoxLayout>
49 #include <QHBoxLayout>
50 #include <QBoxLayout>
51 #include <QResizeEvent>
53 using namespace KCal;
55 static const char TMPL_DIALOG_NAME[] = "TemplateDialog";
58 TemplateDlg* TemplateDlg::mInstance = 0;
61 TemplateDlg::TemplateDlg(QWidget* parent)
62 : KDialog(parent)
64 QWidget* topWidget = new QWidget(this);
65 setMainWidget(topWidget);
66 setButtons(Close);
67 setDefaultButton(Close);
68 setModal(false);
69 setCaption(i18nc("@title:window", "Alarm Templates"));
70 showButtonSeparator(true);
71 QBoxLayout* topLayout = new QHBoxLayout(topWidget);
72 topLayout->setMargin(0);
73 topLayout->setSpacing(spacingHint());
75 QBoxLayout* layout = new QVBoxLayout();
76 layout->setMargin(0);
77 topLayout->addLayout(layout);
78 #ifdef USE_AKONADI
79 mListFilterModel = new TemplateListModel(this);
80 if (!ShellProcess::authorised())
81 mListFilterModel->setAlarmActionFilter(static_cast<KAEvent::Actions>(KAEvent::ACT_ALL & ~KAEvent::ACT_COMMAND));
82 #else
83 mListFilterModel = new TemplateListFilterModel(EventListModel::templates());
84 if (!ShellProcess::authorised())
85 mListFilterModel->setTypeFilter(static_cast<KAEvent::Actions>(KAEvent::ACT_ALL & ~KAEvent::ACT_COMMAND));
86 #endif
87 mListView = new TemplateListView(topWidget);
88 mListView->setModel(mListFilterModel);
89 #ifdef USE_AKONADI
90 mListView->sortByColumn(TemplateListModel::TemplateNameColumn, Qt::AscendingOrder);
91 #else
92 mListView->sortByColumn(TemplateListFilterModel::TemplateNameColumn, Qt::AscendingOrder);
93 #endif
94 mListView->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
95 mListView->setWhatsThis(i18nc("@info:whatsthis", "The list of alarm templates"));
96 mListView->setItemDelegate(new TemplateListDelegate(mListView));
97 connect(mListView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(slotSelectionChanged()));
98 layout->addWidget(mListView);
100 layout = new QVBoxLayout();
101 layout->setMargin(0);
102 topLayout->addLayout(layout);
103 QPushButton* button = new QPushButton(i18nc("@action:button", "New"), topWidget);
104 mNewAction = new NewAlarmAction(true, i18nc("@action", "New"), this);
105 button->setMenu(mNewAction->menu());
106 connect(mNewAction, SIGNAL(selected(EditAlarmDlg::Type)), SLOT(slotNew(EditAlarmDlg::Type)));
107 button->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template"));
108 layout->addWidget(button);
110 mEditButton = new QPushButton(i18nc("@action:button", "Edit..."), topWidget);
111 connect(mEditButton, SIGNAL(clicked()), SLOT(slotEdit()));
112 mEditButton->setWhatsThis(i18nc("@info:whatsthis", "Edit the currently highlighted alarm template"));
113 layout->addWidget(mEditButton);
115 mCopyButton = new QPushButton(i18nc("@action:button", "Copy"), topWidget);
116 connect(mCopyButton, SIGNAL(clicked()), SLOT(slotCopy()));
117 mCopyButton->setWhatsThis(i18nc("@info:whatsthis", "Create a new alarm template based on a copy of the currently highlighted template"));
118 layout->addWidget(mCopyButton);
120 mDeleteButton = new QPushButton(i18nc("@action:button", "Delete"), topWidget);
121 connect(mDeleteButton, SIGNAL(clicked()), SLOT(slotDelete()));
122 mDeleteButton->setWhatsThis(i18nc("@info:whatsthis", "Delete the currently highlighted alarm template"));
123 layout->addWidget(mDeleteButton);
125 KActionCollection* actions = new KActionCollection(this);
126 KAction* act = KStandardAction::selectAll(mListView, SLOT(selectAll()), actions);
127 topLevelWidget()->addAction(act);
128 act = KStandardAction::deselect(mListView, SLOT(clearSelection()), actions);
129 topLevelWidget()->addAction(act);
131 slotSelectionChanged(); // enable/disable buttons as appropriate
133 QSize s;
134 if (KAlarm::readConfigWindowSize(TMPL_DIALOG_NAME, s))
135 resize(s);
138 /******************************************************************************
139 * Destructor.
141 TemplateDlg::~TemplateDlg()
143 mInstance = 0;
146 /******************************************************************************
147 * Create an instance, if none already exists.
149 TemplateDlg* TemplateDlg::create(QWidget* parent)
151 if (mInstance)
152 return 0;
153 mInstance = new TemplateDlg(parent);
154 return mInstance;
157 /******************************************************************************
158 * Called when the New Template button is clicked to create a new template.
160 void TemplateDlg::slotNew(EditAlarmDlg::Type type)
162 KAlarm::editNewTemplate(type, mListView);
165 /******************************************************************************
166 * Called when the Copy button is clicked to edit a copy of an existing alarm,
167 * to add to the list.
169 void TemplateDlg::slotCopy()
171 #ifdef USE_AKONADI
172 KAEvent event = mListView->selectedEvent();
173 if (event.isValid())
174 KAlarm::editNewTemplate(&event, mListView);
175 #else
176 KAEvent* event = mListView->selectedEvent();
177 if (event)
178 KAlarm::editNewTemplate(event, mListView);
179 #endif
182 /******************************************************************************
183 * Called when the Modify button is clicked to edit the currently highlighted
184 * alarm in the list.
186 void TemplateDlg::slotEdit()
188 #ifdef USE_AKONADI
189 KAEvent event = mListView->selectedEvent();
190 if (event.isValid())
191 KAlarm::editTemplate(&event, mListView);
192 #else
193 KAEvent* event = mListView->selectedEvent();
194 if (event)
195 KAlarm::editTemplate(event, mListView);
196 #endif
199 /******************************************************************************
200 * Called when the Delete button is clicked to delete the currently highlighted
201 * alarms in the list.
203 void TemplateDlg::slotDelete()
205 #ifdef USE_AKONADI
206 QVector<KAEvent> events = mListView->selectedEvents();
207 #else
208 KAEvent::List events = mListView->selectedEvents();
209 #endif
210 int n = events.count();
211 if (KAMessageBox::warningContinueCancel(this, i18ncp("@info", "Do you really want to delete the selected alarm template?",
212 "Do you really want to delete the %1 selected alarm templates?", n),
213 i18ncp("@title:window", "Delete Alarm Template", "Delete Alarm Templates", n),
214 KGuiItem(i18nc("@action:button", "&Delete"), "edit-delete"))
215 != KMessageBox::Continue)
216 return;
218 #ifdef USE_AKONADI
219 KAEvent::List delEvents;
220 #else
221 QStringList delEvents;
222 #endif
223 Undo::EventList undos;
224 AlarmCalendar* resources = AlarmCalendar::resources();
225 for (int i = 0; i < n; ++i)
227 #ifdef USE_AKONADI
228 KAEvent* event = &events[i];
229 delEvents.append(event);
230 Akonadi::Collection c = resources->collectionForEvent(event->itemId());
231 undos.append(*event, c);
232 #else
233 const KAEvent* event = events[i];
234 delEvents.append(event->id());
235 undos.append(*event, resources->resourceForEvent(event->id()));
236 #endif
238 KAlarm::deleteTemplates(delEvents, this);
239 Undo::saveDeletes(undos);
242 /******************************************************************************
243 * Called when the group of items selected changes.
244 * Enable/disable the buttons depending on whether/how many templates are
245 * currently highlighted.
247 void TemplateDlg::slotSelectionChanged()
249 AlarmCalendar* resources = AlarmCalendar::resources();
250 #ifdef USE_AKONADI
251 QVector<KAEvent> events = mListView->selectedEvents();
252 #else
253 KAEvent::List events = mListView->selectedEvents();
254 #endif
255 int count = events.count();
256 bool readOnly = false;
257 for (int i = 0; i < count; ++i)
259 #ifdef USE_AKONADI
260 const KAEvent* event = &events[i];
261 if (resources->eventReadOnly(event->itemId()))
262 #else
263 const KAEvent* event = events[i];
264 if (resources->eventReadOnly(event->id()))
265 #endif
267 readOnly = true;
268 break;
271 mEditButton->setEnabled(count == 1);
272 mCopyButton->setEnabled(count == 1);
273 mDeleteButton->setEnabled(count && !readOnly);
276 /******************************************************************************
277 * Called when the dialog's size has changed.
278 * Records the new size in the config file.
280 void TemplateDlg::resizeEvent(QResizeEvent* re)
282 if (isVisible())
283 KAlarm::writeConfigWindowSize(TMPL_DIALOG_NAME, re->size());
284 KDialog::resizeEvent(re);
287 // vim: et sw=4: