SVN_SILENT made messages (.desktop file)
[kdepim.git] / kalarm / birthdaydlg.cpp
blob1dcf30f55ddc6c45af77d2426bfbdea0cc54abc9
1 /*
2 * birthdaydlg.cpp - dialog to pick birthdays from address book
3 * Program: kalarm
4 * Copyright © 2002-2012 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 "birthdaydlg.h"
23 #include "kalarm.h"
24 #include "alarmcalendar.h"
25 #include "birthdaymodel.h"
26 #include "checkbox.h"
27 #include "editdlgtypes.h"
28 #include "fontcolourbutton.h"
29 #include "kalarmapp.h"
30 #include "latecancel.h"
31 #include "preferences.h"
32 #include "reminder.h"
33 #include "repetitionbutton.h"
34 #include "shellprocess.h"
35 #include "soundpicker.h"
36 #include "specialactions.h"
38 #include <AkonadiCore/control.h>
39 #include <AkonadiCore/entitymimetypefiltermodel.h>
41 #include <KLocalizedString>
42 #include <kconfiggroup.h>
43 #include <kmessagebox.h>
44 #include <kstandardaction.h>
45 #include <kactioncollection.h>
46 #include <kdescendantsproxymodel.h>
47 #include <khbox.h>
48 #include <KSharedConfig>
50 #include <QAction>
51 #include <QGroupBox>
52 #include <QLabel>
53 #include <QTreeView>
54 #include <QHeaderView>
55 #include <QHBoxLayout>
56 #include <QVBoxLayout>
57 #include "kalarm_debug.h"
59 using namespace KCal;
62 BirthdayDlg::BirthdayDlg(QWidget* parent)
63 : KDialog(parent),
64 mSpecialActionsButton(0)
66 setObjectName(QStringLiteral("BirthdayDlg")); // used by LikeBack
67 setCaption(i18nc("@title:window", "Import Birthdays From KAddressBook"));
68 setButtons(Ok | Cancel);
69 setDefaultButton(Ok);
71 connect(this, &BirthdayDlg::okClicked, this, &BirthdayDlg::slotOk);
73 QWidget* topWidget = new QWidget(this);
74 setMainWidget(topWidget);
75 QVBoxLayout* topLayout = new QVBoxLayout(topWidget);
76 topLayout->setMargin(0);
77 topLayout->setSpacing(spacingHint());
79 // Prefix and suffix to the name in the alarm text
80 // Get default prefix and suffix texts from config file
81 KConfigGroup config(KSharedConfig::openConfig(), "General");
82 mPrefixText = config.readEntry("BirthdayPrefix", i18nc("@info", "Birthday: "));
83 mSuffixText = config.readEntry("BirthdaySuffix");
85 QGroupBox* textGroup = new QGroupBox(i18nc("@title:group", "Alarm Text"), topWidget);
86 topLayout->addWidget(textGroup);
87 QGridLayout* grid = new QGridLayout(textGroup);
88 grid->setMargin(marginHint());
89 grid->setSpacing(spacingHint());
90 QLabel* label = new QLabel(i18nc("@label:textbox", "Prefix:"), textGroup);
91 label->setFixedSize(label->sizeHint());
92 grid->addWidget(label, 0, 0);
93 mPrefix = new BLineEdit(mPrefixText, textGroup);
94 mPrefix->setMinimumSize(mPrefix->sizeHint());
95 label->setBuddy(mPrefix);
96 connect(mPrefix, &BLineEdit::focusLost, this, &BirthdayDlg::slotTextLostFocus);
97 mPrefix->setWhatsThis(i18nc("@info:whatsthis",
98 "Enter text to appear before the person's name in the alarm message, "
99 "including any necessary trailing spaces."));
100 grid->addWidget(mPrefix, 0, 1);
102 label = new QLabel(i18nc("@label:textbox", "Suffix:"), textGroup);
103 label->setFixedSize(label->sizeHint());
104 grid->addWidget(label, 1, 0);
105 mSuffix = new BLineEdit(mSuffixText, textGroup);
106 mSuffix->setMinimumSize(mSuffix->sizeHint());
107 label->setBuddy(mSuffix);
108 connect(mSuffix, &BLineEdit::focusLost, this, &BirthdayDlg::slotTextLostFocus);
109 mSuffix->setWhatsThis(i18nc("@info:whatsthis",
110 "Enter text to appear after the person's name in the alarm message, "
111 "including any necessary leading spaces."));
112 grid->addWidget(mSuffix, 1, 1);
114 QGroupBox* group = new QGroupBox(i18nc("@title:group", "Select Birthdays"), topWidget);
115 topLayout->addWidget(group);
116 QVBoxLayout* layout = new QVBoxLayout(group);
117 layout->setMargin(0);
119 // Start Akonadi server as we need it for the birthday model to access contacts information
120 Akonadi::Control::start();
122 BirthdayModel* model = BirthdayModel::instance();
123 connect(model, &BirthdayModel::dataChanged, this, &BirthdayDlg::resizeViewColumns);
125 KDescendantsProxyModel* descendantsModel = new KDescendantsProxyModel(this);
126 descendantsModel->setSourceModel(model);
128 Akonadi::EntityMimeTypeFilterModel* mimeTypeFilter = new Akonadi::EntityMimeTypeFilterModel(this);
129 mimeTypeFilter->setSourceModel(descendantsModel);
130 mimeTypeFilter->addMimeTypeExclusionFilter(Akonadi::Collection::mimeType());
131 mimeTypeFilter->setHeaderGroup(Akonadi::EntityTreeModel::ItemListHeaders);
133 mBirthdaySortModel = new BirthdaySortModel(this);
134 mBirthdaySortModel->setSourceModel(mimeTypeFilter);
135 mBirthdaySortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
136 mBirthdaySortModel->setPrefixSuffix(mPrefixText, mSuffixText);
137 mListView = new QTreeView(group);
138 mListView->setEditTriggers(QAbstractItemView::NoEditTriggers);
139 mListView->setModel(mBirthdaySortModel);
140 mListView->setRootIsDecorated(false); // don't show expander icons
141 mListView->setSortingEnabled(true);
142 mListView->sortByColumn(BirthdayModel::NameColumn);
143 mListView->setAllColumnsShowFocus(true);
144 mListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
145 mListView->setSelectionBehavior(QAbstractItemView::SelectRows);
146 mListView->setTextElideMode(Qt::ElideRight);
147 mListView->header()->setResizeMode(BirthdayModel::NameColumn, QHeaderView::Stretch);
148 mListView->header()->setResizeMode(BirthdayModel::DateColumn, QHeaderView::ResizeToContents);
149 connect(mListView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(slotSelectionChanged()));
150 mListView->setWhatsThis(xi18nc("@info:whatsthis",
151 "<para>Select birthdays to set alarms for.<nl/>"
152 "This list shows all birthdays in <application>KAddressBook</application> except those for which alarms already exist.</para>"
153 "<para>You can select multiple birthdays at one time by dragging the mouse over the list, "
154 "or by clicking the mouse while pressing Ctrl or Shift.</para>"));
155 layout->addWidget(mListView);
157 group = new QGroupBox(i18nc("@title:group", "Alarm Configuration"), topWidget);
158 topLayout->addWidget(group);
159 QVBoxLayout* groupLayout = new QVBoxLayout(group);
160 groupLayout->setMargin(marginHint());
161 groupLayout->setSpacing(spacingHint());
163 // Sound checkbox and file selector
164 QHBoxLayout* hlayout = new QHBoxLayout();
165 hlayout->setMargin(0);
166 groupLayout->addLayout(hlayout);
167 mSoundPicker = new SoundPicker(group);
168 mSoundPicker->setFixedSize(mSoundPicker->sizeHint());
169 hlayout->addWidget(mSoundPicker);
170 hlayout->addSpacing(2*spacingHint());
171 hlayout->addStretch();
173 // Font and colour choice button and sample text
174 mFontColourButton = new FontColourButton(group);
175 mFontColourButton->setMaximumHeight(mFontColourButton->sizeHint().height() * 3/2);
176 hlayout->addWidget(mFontColourButton);
177 connect(mFontColourButton, &FontColourButton::selected, this, &BirthdayDlg::setColours);
179 // How much advance warning to give
180 mReminder = new Reminder(i18nc("@info:whatsthis", "Check to display a reminder in advance of or after the birthday."),
181 i18nc("@info:whatsthis", "Enter the number of days before or after each birthday to display a reminder. "
182 "This is in addition to the alarm which is displayed on the birthday."),
183 i18nc("@info:whatsthis", "Select whether the reminder should be triggered before or after the birthday."),
184 false, false, group);
185 mReminder->setFixedSize(mReminder->sizeHint());
186 mReminder->setMaximum(0, 364);
187 mReminder->setMinutes(0, true);
188 groupLayout->addWidget(mReminder, 0, Qt::AlignLeft);
190 // Acknowledgement confirmation required - default = no confirmation
191 hlayout = new QHBoxLayout();
192 hlayout->setMargin(0);
193 hlayout->setSpacing(2*spacingHint());
194 groupLayout->addLayout(hlayout);
195 mConfirmAck = EditDisplayAlarmDlg::createConfirmAckCheckbox(group);
196 mConfirmAck->setFixedSize(mConfirmAck->sizeHint());
197 hlayout->addWidget(mConfirmAck);
198 hlayout->addSpacing(2*spacingHint());
199 hlayout->addStretch();
201 if (ShellProcess::authorised()) // don't display if shell commands not allowed (e.g. kiosk mode)
203 // Special actions button
204 mSpecialActionsButton = new SpecialActionsButton(false, group);
205 mSpecialActionsButton->setFixedSize(mSpecialActionsButton->sizeHint());
206 hlayout->addWidget(mSpecialActionsButton);
209 // Late display checkbox - default = allow late display
210 hlayout = new QHBoxLayout();
211 hlayout->setMargin(0);
212 hlayout->setSpacing(2*spacingHint());
213 groupLayout->addLayout(hlayout);
214 mLateCancel = new LateCancelSelector(false, group);
215 mLateCancel->setFixedSize(mLateCancel->sizeHint());
216 hlayout->addWidget(mLateCancel);
217 hlayout->addStretch();
219 // Sub-repetition button
220 mSubRepetition = new RepetitionButton(i18nc("@action:button", "Sub-Repetition"), false, group);
221 mSubRepetition->setFixedSize(mSubRepetition->sizeHint());
222 mSubRepetition->set(Repetition(), true, 364*24*60);
223 mSubRepetition->setWhatsThis(i18nc("@info:whatsthis", "Set up an additional alarm repetition"));
224 hlayout->addWidget(mSubRepetition);
226 // Set the values to their defaults
227 setColours(Preferences::defaultFgColour(), Preferences::defaultBgColour());
228 mFontColourButton->setDefaultFont();
229 mFontColourButton->setBgColour(Preferences::defaultBgColour());
230 mFontColourButton->setFgColour(Preferences::defaultFgColour());
231 mLateCancel->setMinutes(Preferences::defaultLateCancel(), true, TimePeriod::Days);
232 mConfirmAck->setChecked(Preferences::defaultConfirmAck());
233 mSoundPicker->set(Preferences::defaultSoundType(), Preferences::defaultSoundFile(),
234 Preferences::defaultSoundVolume(), -1, 0, Preferences::defaultSoundRepeat());
235 if (mSpecialActionsButton)
237 KAEvent::ExtraActionOptions opts(0);
238 if (Preferences::defaultExecPreActionOnDeferral())
239 opts |= KAEvent::ExecPreActOnDeferral;
240 if (Preferences::defaultCancelOnPreActionError())
241 opts |= KAEvent::CancelOnPreActError;
242 if (Preferences::defaultDontShowPreActionError())
243 opts |= KAEvent::DontShowPreActError;
244 mSpecialActionsButton->setActions(Preferences::defaultPreAction(), Preferences::defaultPostAction(), opts);
247 KActionCollection* actions = new KActionCollection(this);
248 KStandardAction::selectAll(mListView, SLOT(selectAll()), actions);
249 KStandardAction::deselect(mListView, SLOT(clearSelection()), actions);
250 actions->addAssociatedWidget(mListView);
251 foreach (QAction* action, actions->actions())
252 action->setShortcutContext(Qt::WidgetWithChildrenShortcut);
254 enableButtonOk(false); // only enable OK button when something is selected
257 /******************************************************************************
258 * Return a list of events for birthdays chosen.
260 QVector<KAEvent> BirthdayDlg::events() const
262 QVector<KAEvent> list;
263 QModelIndexList indexes = mListView->selectionModel()->selectedRows();
264 int count = indexes.count();
265 if (!count)
266 return list;
267 QDate today = KDateTime::currentLocalDate();
268 KDateTime todayStart(today, KDateTime::ClockTime);
269 int thisYear = today.year();
270 int reminder = mReminder->minutes();
271 for (int i = 0; i < count; ++i)
273 const QModelIndex nameIndex = indexes.at(i).model()->index(indexes.at(i).row(), 0);
274 const QModelIndex birthdayIndex = indexes.at(i).model()->index(indexes.at(i).row(), 1);
275 const QString name = nameIndex.data(Qt::DisplayRole).toString();
276 QDate date = birthdayIndex.data(BirthdayModel::DateRole).toDate();
277 date.setYMD(thisYear, date.month(), date.day());
278 if (date <= today)
279 date.setYMD(thisYear + 1, date.month(), date.day());
280 KAEvent event(KDateTime(date, KDateTime::ClockTime),
281 mPrefix->text() + name + mSuffix->text(),
282 mFontColourButton->bgColour(), mFontColourButton->fgColour(),
283 mFontColourButton->font(), KAEvent::MESSAGE, mLateCancel->minutes(),
284 mFlags, true);
285 float fadeVolume;
286 int fadeSecs;
287 float volume = mSoundPicker->volume(fadeVolume, fadeSecs);
288 int repeatPause = mSoundPicker->repeatPause();
289 event.setAudioFile(mSoundPicker->file().prettyUrl(), volume, fadeVolume, fadeSecs, repeatPause);
290 QVector<int> months(1, date.month());
291 event.setRecurAnnualByDate(1, months, 0, KARecurrence::defaultFeb29Type(), -1, QDate());
292 event.setRepetition(mSubRepetition->repetition());
293 event.setNextOccurrence(todayStart);
294 if (reminder)
295 event.setReminder(reminder, false);
296 if (mSpecialActionsButton)
297 event.setActions(mSpecialActionsButton->preAction(),
298 mSpecialActionsButton->postAction(),
299 mSpecialActionsButton->options());
300 event.endChanges();
301 list.append(event);
303 return list;
306 /******************************************************************************
307 * Called when the OK button is selected to import the selected birthdays.
309 void BirthdayDlg::slotOk()
311 // Save prefix and suffix texts to use as future defaults
312 KConfigGroup config(KSharedConfig::openConfig(), "General");
313 config.writeEntry("BirthdayPrefix", mPrefix->text());
314 config.writeEntry("BirthdaySuffix", mSuffix->text());
315 config.sync();
317 mFlags = KAEvent::ANY_TIME;
318 if (mSoundPicker->sound() == Preferences::Sound_Beep) mFlags |= KAEvent::BEEP;
319 if (mSoundPicker->repeatPause() >= 0) mFlags |= KAEvent::REPEAT_SOUND;
320 if (mConfirmAck->isChecked()) mFlags |= KAEvent::CONFIRM_ACK;
321 if (mFontColourButton->defaultFont()) mFlags |= KAEvent::DEFAULT_FONT;
322 KDialog::accept();
325 /******************************************************************************
326 * Called when the group of items selected changes.
327 * Enable/disable the OK button depending on whether anything is selected.
329 void BirthdayDlg::slotSelectionChanged()
331 enableButtonOk(mListView->selectionModel()->hasSelection());
334 /******************************************************************************
335 * Called when the font/color button has been clicked.
336 * Set the colors in the message text entry control.
338 void BirthdayDlg::setColours(const QColor& fgColour, const QColor& bgColour)
340 QPalette pal = mPrefix->palette();
341 pal.setColor(mPrefix->backgroundRole(), bgColour);
342 pal.setColor(mPrefix->foregroundRole(), fgColour);
343 mPrefix->setPalette(pal);
344 mSuffix->setPalette(pal);
347 /******************************************************************************
348 * Called when the data has changed in the birthday list.
349 * Resize the date column.
351 void BirthdayDlg::resizeViewColumns()
353 mListView->resizeColumnToContents(BirthdayModel::DateColumn);
356 /******************************************************************************
357 * Called when the prefix or suffix text has lost keyboard focus.
358 * If the text has changed, re-evaluates the selection list according to the new
359 * birthday alarm text format.
361 void BirthdayDlg::slotTextLostFocus()
363 QString prefix = mPrefix->text();
364 QString suffix = mSuffix->text();
365 if (prefix != mPrefixText || suffix != mSuffixText)
367 // Text has changed - re-evaluate the selection list
368 mPrefixText = prefix;
369 mSuffixText = suffix;
370 mBirthdaySortModel->setPrefixSuffix(mPrefixText, mSuffixText);
374 // vim: et sw=4: