1/*
2 This file is part of the kcal library.
3
4 Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
5 Author: Kevin Krammer, krake@kdab.com
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library 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 GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include "recurrenceactions.h"
24
25#include "ui_recurrenceactionsscopewidget.h"
26
27#include <KDialog>
28#include <KLocale>
29#include <KLocalizedString>
30#include <KMessageBox>
31#include <KGlobal>
32
33#include <QPointer>
34
35#include <boost/shared_ptr.hpp>
36
37using namespace KCalUtils;
38using namespace KCalUtils::RecurrenceActions;
39using namespace KCalCore;
40
41class ScopeWidget : public QWidget
42{
43public:
44 ScopeWidget(int availableChoices, const KDateTime &dateTime, QWidget *parent)
45 : QWidget(parent), mAvailableChoices(availableChoices)
46 {
47 mUi.setupUi(this);
48
49 if ((mAvailableChoices & PastOccurrences) == 0) {
50 mUi.checkBoxPast->hide();
51 } else {
52 mUi.checkBoxPast->setText(i18nc("@option:check calendar items before a certain date",
53 "Items before %1",
54 KGlobal::locale()->formatDateTime(dateTime)));
55 }
56 if ((mAvailableChoices & SelectedOccurrence) == 0) {
57 mUi.checkBoxSelected->hide();
58 } else {
59 mUi.checkBoxSelected->setText(i18nc("@option:check currently selected calendar item",
60 "Selected item"));
61 }
62 if ((mAvailableChoices & FutureOccurrences) == 0) {
63 mUi.checkBoxFuture->hide();
64 } else {
65 mUi.checkBoxFuture->setText(i18nc("@option:check calendar items after a certain date",
66 "Items after %1",
67 KGlobal::locale()->formatDateTime(dateTime)));
68 }
69 }
70
71 void setMessage(const QString &message);
72 void setIcon(const QIcon &icon);
73
74 void setCheckedChoices(int choices);
75 int checkedChoices() const;
76
77private:
78 const int mAvailableChoices;
79 Ui_RecurrenceActionsScopeWidget mUi;
80};
81
82void ScopeWidget::setMessage(const QString &message)
83{
84 mUi.messageLabel->setText(message);
85}
86
87void ScopeWidget::setIcon(const QIcon &icon)
88{
89 QStyleOption option;
90 option.initFrom(this);
91 mUi.iconLabel->setPixmap(
92 icon.pixmap(style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option, this)));
93}
94
95void ScopeWidget::setCheckedChoices(int choices)
96{
97 // mask with available ones
98 choices &= mAvailableChoices;
99
100 mUi.checkBoxPast->setChecked((choices & PastOccurrences) != 0);
101 mUi.checkBoxSelected->setChecked((choices & SelectedOccurrence) != 0);
102 mUi.checkBoxFuture->setChecked((choices & FutureOccurrences) != 0);
103}
104
105int ScopeWidget::checkedChoices() const
106{
107 int result = NoOccurrence;
108
109 if (mUi.checkBoxPast->isChecked()) {
110 result |= PastOccurrences;
111 }
112 if (mUi.checkBoxSelected->isChecked()) {
113 result |= SelectedOccurrence;
114 }
115 if (mUi.checkBoxFuture->isChecked()) {
116 result |= FutureOccurrences;
117 }
118
119 return result;
120}
121
122int RecurrenceActions::availableOccurrences(const Incidence::Ptr &incidence,
123 const KDateTime &selectedOccurrence)
124{
125 int result = NoOccurrence;
126
127 if (incidence->recurrence()->recursOn(selectedOccurrence.date(),
128 selectedOccurrence.timeSpec())) {
129 result |= SelectedOccurrence;
130 }
131
132 if (incidence->recurrence()->getPreviousDateTime(selectedOccurrence).isValid()) {
133 result |= PastOccurrences;
134 }
135
136 if (incidence->recurrence()->getNextDateTime(selectedOccurrence).isValid()) {
137 result |= FutureOccurrences;
138 }
139
140 return result;
141}
142
143int RecurrenceActions::questionMultipleChoice(const KDateTime &selectedOccurrence,
144 const QString &message, const QString &caption,
145 const KGuiItem &action, int availableChoices,
146 int preselectedChoices, QWidget *parent)
147{
148 QPointer<KDialog> dialog = new KDialog(parent);
149 dialog->setCaption(caption);
150 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
151 dialog->setDefaultButton(KDialog::Ok);
152 dialog->setButtonGuiItem(KDialog::Ok, action);
153
154 ScopeWidget *widget = new ScopeWidget(availableChoices, selectedOccurrence, dialog);
155 dialog->setMainWidget(widget);
156
157 widget->setMessage(message);
158 widget->setIcon(widget->style()->standardIcon(QStyle::SP_MessageBoxQuestion));
159 widget->setCheckedChoices(preselectedChoices);
160
161 const int result = dialog->exec();
162 if (dialog) {
163 dialog->deleteLater();
164 }
165
166 if (result == QDialog::Rejected) {
167 return NoOccurrence;
168 }
169
170 return widget->checkedChoices();
171}
172
173int RecurrenceActions::questionSelectedAllCancel(const QString &message, const QString &caption,
174 const KGuiItem &actionSelected,
175 const KGuiItem &actionAll, QWidget *parent)
176{
177 KDialog *dialog = new KDialog(parent);
178 dialog->setCaption(caption);
179 dialog->setButtons(KDialog::Yes | KDialog::Ok | KDialog::Cancel);
180 dialog->setObjectName(QLatin1String("RecurrenceActions::questionSelectedAllCancel"));
181 dialog->setDefaultButton(KDialog::Yes);
182 dialog->setButtonGuiItem(KDialog::Yes, actionSelected);
183 dialog->setButtonGuiItem(KDialog::Ok, actionAll);
184
185 bool checkboxResult = false;
186 int result = KMessageBox::createKMessageBox(
187 dialog,
188 QMessageBox::Question,
189 message,
190 QStringList(),
191 QString(),
192 &checkboxResult,
193 KMessageBox::Notify);
194
195 switch (result) {
196 case KDialog::Yes:
197 return SelectedOccurrence;
198 case QDialog::Accepted:
199 // See kdialog.h, 'Ok' doesn't return KDialog:Ok
200 return AllOccurrences;
201 default:
202 return NoOccurrence;
203 }
204
205 return NoOccurrence;
206}
207
208int RecurrenceActions::questionSelectedFutureAllCancel(const QString &message,
209 const QString &caption,
210 const KGuiItem &actionSelected,
211 const KGuiItem &actionFuture,
212 const KGuiItem &actionAll,
213 QWidget *parent)
214{
215 KDialog *dialog = new KDialog(parent);
216 dialog->setCaption(caption);
217 dialog->setButtons(KDialog::Yes | KDialog::No | KDialog::Ok | KDialog::Cancel);
218 dialog->setObjectName(QLatin1String("RecurrenceActions::questionSelectedFutureAllCancel"));
219 dialog->setDefaultButton(KDialog::Yes);
220 dialog->setButtonGuiItem(KDialog::Yes, actionSelected);
221 dialog->setButtonGuiItem(KDialog::No, actionFuture);
222 dialog->setButtonGuiItem(KDialog::Ok, actionAll);
223
224 bool checkboxResult = false;
225 int result = KMessageBox::createKMessageBox(
226 dialog,
227 QMessageBox::Question,
228 message,
229 QStringList(),
230 QString(),
231 &checkboxResult,
232 KMessageBox::Notify);
233
234 switch (result) {
235 case KDialog::Yes:
236 return SelectedOccurrence;
237 case KDialog::No:
238 return FutureOccurrences;
239 case QDialog::Accepted:
240 return AllOccurrences;
241 default:
242 return NoOccurrence;
243 }
244
245 return NoOccurrence;
246}
247
248// kate: space-indent on; indent-width 2; replace-tabs on;
249