kalarm

deferdlg.cpp
1 /*
2  * deferdlg.cpp - dialogue to defer an alarm
3  * Program: kalarm
4  * Copyright © 2002-2006,2008 by David Jarvie <djarvie@kde.org>
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 
25 #include <tdeglobal.h>
26 #include <tdelocale.h>
27 #include <tdemessagebox.h>
28 #include <kdebug.h>
29 
30 #include <libkcal/event.h>
31 #include <libkcal/recurrence.h>
32 
33 #include "alarmcalendar.h"
34 #include "alarmevent.h"
35 #include "alarmtimewidget.h"
36 #include "datetime.h"
37 #include "functions.h"
38 #include "kalarmapp.h"
39 #include "deferdlg.moc"
40 
41 
42 DeferAlarmDlg::DeferAlarmDlg(const TQString& caption, const DateTime& initialDT,
43  bool cancelButton, TQWidget* parent, const char* name)
44  : KDialogBase(parent, name, true, caption, Ok|Cancel|User1, Ok, false, i18n("Cancel &Deferral"))
45 {
46  if (!cancelButton)
47  showButton(User1, false);
48 
49  TQWidget* page = new TQWidget(this);
50  setMainWidget(page);
51  TQVBoxLayout* layout = new TQVBoxLayout(page, 0, spacingHint());
52 
53  mTimeWidget = new AlarmTimeWidget(AlarmTimeWidget::DEFER_TIME, page, "timeGroup");
54  mTimeWidget->setDateTime(initialDT);
55  mTimeWidget->setMinDateTimeIsCurrent();
56  connect(mTimeWidget, TQ_SIGNAL(pastMax()), TQ_SLOT(slotPastLimit()));
57  layout->addWidget(mTimeWidget);
58  layout->addSpacing(spacingHint());
59 
60  setButtonWhatsThis(Ok, i18n("Defer the alarm until the specified time."));
61  setButtonWhatsThis(User1, i18n("Cancel the deferred alarm. This does not affect future recurrences."));
62 }
63 
64 
65 /******************************************************************************
66 * Called when the OK button is clicked.
67 */
68 void DeferAlarmDlg::slotOk()
69 {
70  mAlarmDateTime = mTimeWidget->getDateTime(&mDeferMinutes);
71  if (!mAlarmDateTime.isValid())
72  return;
73  KAEvent::DeferLimitType limitType;
74  DateTime endTime;
75  if (!mLimitEventID.isEmpty())
76  {
77  // Get the event being deferred
78  const KCal::Event* kcalEvent = AlarmCalendar::getEvent(mLimitEventID);
79  if (kcalEvent)
80  {
81  KAEvent event(*kcalEvent);
82  endTime = event.deferralLimit(&limitType);
83  }
84  }
85  else
86  {
87  endTime = mLimitDateTime;
88  limitType = mLimitDateTime.isValid() ? KAEvent::LIMIT_MAIN : KAEvent::LIMIT_NONE;
89  }
90  if (endTime.isValid() && mAlarmDateTime > endTime)
91  {
92  TQString text;
93  switch (limitType)
94  {
95  case KAEvent::LIMIT_REPETITION:
96  text = i18n("Cannot defer past the alarm's next sub-repetition (currently %1)");
97  break;
98  case KAEvent::LIMIT_RECURRENCE:
99  text = i18n("Cannot defer past the alarm's next recurrence (currently %1)");
100  break;
101  case KAEvent::LIMIT_REMINDER:
102  text = i18n("Cannot defer past the alarm's next reminder (currently %1)");
103  break;
104  case KAEvent::LIMIT_MAIN:
105  text = i18n("Cannot defer reminder past the main alarm time (%1)");
106  break;
107  case KAEvent::LIMIT_NONE:
108  break; // can't happen with a valid endTime
109  }
110  KMessageBox::sorry(this, text.arg(endTime.formatLocale()));
111  }
112  else
113  accept();
114 }
115 
116 /******************************************************************************
117 * Select the 'Time from now' radio button and preset its value.
118 */
119 void DeferAlarmDlg::setDeferMinutes(int minutes)
120 {
121  mTimeWidget->selectTimeFromNow(minutes);
122 }
123 
124 /******************************************************************************
125 * Called the maximum date/time for the date/time edit widget has been passed.
126 */
127 void DeferAlarmDlg::slotPastLimit()
128 {
129  enableButtonOK(false);
130 }
131 
132 /******************************************************************************
133 * Set the time limit for deferral based on the next occurrence of the alarm
134 * with the specified ID.
135 */
136 void DeferAlarmDlg::setLimit(const DateTime& limit)
137 {
138  mLimitEventID = TQString();
139  mLimitDateTime = limit;
140  mTimeWidget->setMaxDateTime(mLimitDateTime);
141 }
142 
143 /******************************************************************************
144 * Set the time limit for deferral based on the next occurrence of the alarm
145 * with the specified ID.
146 */
147 DateTime DeferAlarmDlg::setLimit(const TQString& eventID)
148 {
149  mLimitEventID = eventID;
150  const KCal::Event* kcalEvent = AlarmCalendar::getEvent(mLimitEventID);
151  if (kcalEvent)
152  {
153  KAEvent event(*kcalEvent);
154  mLimitDateTime = event.deferralLimit();
155  }
156  else
157  mLimitDateTime = DateTime();
158  mTimeWidget->setMaxDateTime(mLimitDateTime);
159  return mLimitDateTime;
160 }
161 
162 /******************************************************************************
163 * Called when the Cancel Deferral button is clicked.
164 */
165 void DeferAlarmDlg::slotUser1()
166 {
167  mAlarmDateTime = DateTime();
168  accept();
169 }
170 
171 /******************************************************************************
172 * Called when the Cancel button is clicked.
173 */
174 void DeferAlarmDlg::slotCancel()
175 {
176  reject();
177 }
represents calendar alarms and events
KAEvent corresponds to a KCal::Event instance.
Definition: alarmevent.h:232
miscellaneous functions
the KAlarm application object