summaryrefslogtreecommitdiff
path: root/core/pim/datebook/dateentryimpl.cpp
Unidiff
Diffstat (limited to 'core/pim/datebook/dateentryimpl.cpp') (more/less context) (show whitespace changes)
-rw-r--r--core/pim/datebook/dateentryimpl.cpp474
1 files changed, 474 insertions, 0 deletions
diff --git a/core/pim/datebook/dateentryimpl.cpp b/core/pim/datebook/dateentryimpl.cpp
new file mode 100644
index 0000000..1122f79
--- a/dev/null
+++ b/core/pim/datebook/dateentryimpl.cpp
@@ -0,0 +1,474 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "dateentryimpl.h"
22#include "repeatentry.h"
23
24#include <qpe/qpeapplication.h>
25#include <qpe/categoryselect.h>
26#include <qpe/datebookmonth.h>
27#include <qpe/global.h>
28#include <qpe/timeconversion.h>
29#include <qpe/timestring.h>
30#include <qpe/tzselect.h>
31
32#include <qcheckbox.h>
33#include <qcombobox.h>
34#include <qlayout.h>
35#include <qlineedit.h>
36#include <qmultilineedit.h>
37#include <qpopupmenu.h>
38#include <qscrollview.h>
39#include <qspinbox.h>
40#include <qtoolbutton.h>
41
42#include <stdlib.h>
43
44/*
45 * Constructs a DateEntry which is a child of 'parent', with the
46 * name 'name' and widget flags set to 'f'
47 *
48 * The dialog will by default be modeless, unless you set 'modal' to
49 * TRUE to construct a modal dialog.
50 */
51
52DateEntry::DateEntry( bool startOnMonday, const QDateTime &start,
53 const QDateTime &end, bool whichClock, QWidget* parent,
54 const char* name )
55 : DateEntryBase( parent, name ),
56 ampm( whichClock ),
57 startWeekOnMonday( startOnMonday )
58{
59 init();
60 setDates(start,end);
61}
62
63static void addOrPick( QComboBox* combo, const QString& t )
64{
65 for (int i=0; i<combo->count(); i++) {
66 if ( combo->text(i) == t ) {
67 combo->setCurrentItem(i);
68 return;
69 }
70 }
71 combo->setEditText(t);
72}
73
74DateEntry::DateEntry( bool startOnMonday, const Event &event, bool whichClock,
75 QWidget* parent, const char* name )
76 : DateEntryBase( parent, name ),
77 ampm( whichClock ),
78 startWeekOnMonday( startOnMonday )
79{
80 init();
81 setDates(event.start(),event.end());
82 comboCategory->setCategories( event.categories(), "Calendar", tr("Calendar") );
83 if(!event.description().isEmpty())
84 addOrPick( comboDescription, event.description() );
85 if(!event.location().isEmpty())
86 addOrPick( comboLocation, event.location() );
87 checkAlarm->setChecked( event.hasAlarm() );
88 checkAllDay->setChecked( event.type() == Event::AllDay );
89 if(!event.notes().isEmpty())
90 editNote->setText(event.notes());
91 spinAlarm->setValue(event.alarmTime());
92 if ( event.alarmSound() != Event::Silent )
93 comboSound->setCurrentItem( 1 );
94 if ( event.hasRepeat() ) {
95 rp = event.repeatPattern();
96 cmdRepeat->setText( tr("Repeat...") );
97 }
98 setRepeatLabel();
99}
100
101void DateEntry::setDates( const QDateTime& s, const QDateTime& e )
102{
103 int shour,
104 ehour;
105 QString strStart,
106 strEnd;
107 startDate = s.date();
108 endDate = e.date();
109 startTime = s.time();
110 endTime = e.time();
111 startDateChanged( s.date().year(), s.date().month(), s.date().day() );
112 if ( ampm ) {
113 shour = s.time().hour();
114 ehour = e.time().hour();
115 if ( shour >= 12 ) {
116 if ( shour > 12 )
117 shour -= 12;
118 strStart.sprintf( "%d:%02d PM", shour, s.time().minute() );
119 } else {
120 if ( shour == 0 )
121 shour = 12;
122 strStart.sprintf( "%d:%02d AM", shour, s.time().minute() );
123 }
124 if ( ehour == 24 && e.time().minute() == 0 ) {
125 strEnd = "11:59 PM"; // or "midnight"
126 } else if ( ehour >= 12 ) {
127 if ( ehour > 12 )
128 ehour -= 12;
129 strEnd.sprintf( "%d:%02d PM", ehour, e.time().minute() );
130 } else {
131 if ( ehour == 0 )
132 ehour = 12;
133 strEnd.sprintf( "%d:%02d AM", ehour, e.time().minute() );
134 }
135 } else {
136 strStart.sprintf( "%02d:%02d", s.time().hour(), s.time().minute() );
137 strEnd.sprintf( "%02d:%02d", e.time().hour(), e.time().minute() );
138 }
139 addOrPick(comboStart, strStart );
140 endDateChanged( e.date().year(), e.date().month(), e.date().day() );
141 addOrPick(comboEnd, strEnd );
142}
143
144void DateEntry::init()
145{
146 comboDescription->setInsertionPolicy(QComboBox::AtCurrent);
147 comboLocation->setInsertionPolicy(QComboBox::AtCurrent);
148
149 initCombos();
150 QPopupMenu *m1 = new QPopupMenu( this );
151 startPicker = new DateBookMonth( m1, 0, TRUE );
152 m1->insertItem( startPicker );
153 buttonStart->setPopup( m1 );
154 connect( startPicker, SIGNAL( dateClicked( int, int, int ) ),
155 this, SLOT( startDateChanged( int, int, int ) ) );
156
157 //Let start button change both start and end dates
158 connect( startPicker, SIGNAL( dateClicked( int, int, int ) ),
159 this, SLOT( endDateChanged( int, int, int ) ) );
160 connect( qApp, SIGNAL( clockChanged( bool ) ),
161 this, SLOT( slotChangeClock( bool ) ) );
162 connect( qApp, SIGNAL(weekChanged(bool)),
163 this, SLOT(slotChangeStartOfWeek(bool)) );
164
165 QPopupMenu *m2 = new QPopupMenu( this );
166 endPicker = new DateBookMonth( m2, 0, TRUE );
167 m2->insertItem( endPicker );
168 buttonEnd->setPopup( m2 );
169 connect( endPicker, SIGNAL( dateClicked( int, int, int ) ),
170 this, SLOT( endDateChanged( int, int, int ) ) );
171}
172
173/*
174 * Destroys the object and frees any allocated resources
175 */
176DateEntry::~DateEntry()
177{
178 // no need to delete child widgets, Qt does it all for us
179}
180
181/*
182 * public slot
183 */
184void DateEntry::endDateChanged( int y, int m, int d )
185{
186 endDate.setYMD( y, m, d );
187 if ( endDate < startDate ) {
188 endDate = startDate;
189 }
190
191 buttonEnd->setText( TimeString::shortDate( endDate ) );
192
193 endPicker->setDate( endDate.year(), endDate.month(), endDate.day() );
194}
195
196static QTime parseTime( const QString& s, bool ampm )
197{
198 QTime tmpTime;
199 QStringList l = QStringList::split( ':', s );
200 int hour = l[0].toInt();
201 if ( ampm ) {
202 int i=0;
203 while (i<int(l[1].length()) && l[1][i]>='0' && l[1][i]<='9')
204 i++;
205 QString digits = l[1].left(i);
206 if ( l[1].contains( "PM", FALSE ) ) {
207 if ( hour != 12 )
208 hour += 12;
209 } else {
210 if ( hour == 12 )
211 hour = 0;
212 }
213 l[1] = digits;
214 }
215 int minute = l[1].toInt();
216 if ( minute > 59 )
217 minute = 59;
218 else if ( minute < 0 )
219 minute = 0;
220 if ( hour > 23 ) {
221 hour = 23;
222 minute = 59;
223 } else if ( hour < 0 )
224 hour = 0;
225 tmpTime.setHMS( hour, minute, 0 );
226 return tmpTime;
227}
228
229/*
230 * public slot
231 */
232void DateEntry::endTimeChanged( const QString &s )
233{
234 QTime tmpTime = parseTime(s,ampm);
235 if ( endDate > startDate || tmpTime >= startTime ) {
236 endTime = tmpTime;
237 } else {
238 endTime = startTime;
239 comboEnd->setCurrentItem( comboStart->currentItem() );
240 }
241}
242
243/*
244 * public slot
245 */
246void DateEntry::startDateChanged( int y, int m, int d )
247{
248 QDate prev = startDate;
249 startDate.setYMD( y, m, d );
250 if ( rp.type == Event::Weekly &&
251 startDate.dayOfWeek() != prev.dayOfWeek() ) {
252 // if we change the start of a weekly repeating event
253 // set the repeating day appropriately
254 char mask = 1 << (prev.dayOfWeek()-1);
255 rp.days &= (~mask);
256 rp.days |= 1 << (startDate.dayOfWeek()-1);
257 }
258
259 buttonStart->setText( TimeString::shortDate( startDate ) );
260
261 // our pickers must be reset...
262 startPicker->setDate( y, m, d );
263 endPicker->setDate( y, m, d );
264}
265
266/*
267 * public slot
268 */
269void DateEntry::startTimeChanged( int index )
270{
271 startTime = parseTime(comboStart->text(index),ampm);
272 changeEndCombo( index );
273}
274/*
275 * public slot
276 */
277void DateEntry::typeChanged( const QString &s )
278{
279 bool b = s != "All Day";
280 buttonStart->setEnabled( b );
281 comboStart->setEnabled( b );
282 comboEnd->setEnabled( b );
283}
284/*
285 * public slot
286 */
287void DateEntry::changeEndCombo( int change )
288{
289 if ( change + 2 < comboEnd->count() )
290 change += 2;
291 comboEnd->setCurrentItem( change );
292 endTimeChanged( comboEnd->currentText() );
293}
294
295void DateEntry::slotRepeat()
296{
297 // Work around for compiler Bug..
298 RepeatEntry *e;
299
300 // it is better in my opinion to just grab this from the mother,
301 // since, this dialog doesn't need to keep track of it...
302 if ( rp.type != Event::NoRepeat )
303 e = new RepeatEntry( startWeekOnMonday, rp, startDate, this);
304 else
305 e = new RepeatEntry( startWeekOnMonday, startDate, this );
306
307#if defined(Q_WS_QWS) || defined(_WS_QWS_)
308 e->showMaximized();
309#endif
310 if ( e->exec() ) {
311 rp = e->repeatPattern();
312 setRepeatLabel();
313 }
314}
315
316void DateEntry::slotChangeStartOfWeek( bool onMonday )
317{
318 startWeekOnMonday = onMonday;
319}
320
321Event DateEntry::event()
322{
323 Event ev;
324 Event::SoundTypeChoice st;
325 ev.setDescription( comboDescription->currentText() );
326 ev.setLocation( comboLocation->currentText() );
327 ev.setCategories( comboCategory->currentCategories() );
328 ev.setType( checkAllDay->isChecked() ? Event::AllDay : Event::Normal );
329 if ( startDate > endDate ) {
330 QDate tmp = endDate;
331 endDate = startDate;
332 startDate = tmp;
333 }
334 startTime = parseTime( comboStart->currentText(), ampm );
335 endTime = parseTime( comboEnd->currentText(), ampm );
336 if ( startTime > endTime && endDate == startDate ) {
337 QTime tmp = endTime;
338 endTime = startTime;
339 startTime = tmp;
340 }
341 // don't set the time if theres no need too
342 if ( ev.type() == Event::AllDay ) {
343 startTime.setHMS( 0, 0, 0 );
344 endTime.setHMS( 23, 59, 59 );
345 }
346
347 // adjust start and end times based on timezone
348 QDateTime start( startDate, startTime );
349 QDateTime end( endDate, endTime );
350 time_t start_utc, end_utc;
351
352// qDebug( "tz: %s", timezone->currentZone().latin1() );
353
354 // get real timezone
355 QString realTZ;
356 realTZ = QString::fromLocal8Bit( getenv("TZ") );
357
358 // set timezone
359 if ( setenv( "TZ", timezone->currentZone(), true ) != 0 )
360 qWarning( "There was a problem setting the timezone." );
361
362 // convert to UTC based on selected TZ (calling tzset internally)
363 start_utc = TimeConversion::toUTC( start );
364 end_utc = TimeConversion::toUTC( end );
365
366 // done playing around... put it all back
367 unsetenv( "TZ" );
368 if ( !realTZ.isNull() )
369 if ( setenv( "TZ", realTZ, true ) != 0 )
370 qWarning( "There was a problem setting the timezone." );
371
372 // convert UTC to local time (calling tzset internally)
373 ev.setStart( TimeConversion::fromUTC( start_utc ) );
374 ev.setEnd( TimeConversion::fromUTC( end_utc ) );
375
376 // we only have one type of sound at the moment... LOUD!!!
377 if ( comboSound->currentItem() != 0 )
378 st = Event::Loud;
379 else
380 st = Event::Silent;
381 ev.setAlarm( checkAlarm->isChecked(), spinAlarm->value(), st );
382 if ( rp.type != Event::NoRepeat )
383 ev.setRepeat( TRUE, rp );
384 ev.setNotes( editNote->text() );
385 return ev;
386}
387
388void DateEntry::setRepeatLabel()
389{
390
391 switch( rp.type ) {
392 case Event::Daily:
393 cmdRepeat->setText( tr("Daily...") );
394 break;
395 case Event::Weekly:
396 cmdRepeat->setText( tr("Weekly...") );
397 break;
398 case Event::MonthlyDay:
399 case Event::MonthlyDate:
400 cmdRepeat->setText( tr("Monthly...") );
401 break;
402 case Event::Yearly:
403 cmdRepeat->setText( tr("Yearly...") );
404 break;
405 default:
406 cmdRepeat->setText( tr("No Repeat...") );
407 }
408}
409
410void DateEntry::setAlarmEnabled( bool alarmPreset, int presetTime, Event::SoundTypeChoice sound )
411{
412 checkAlarm->setChecked( alarmPreset );
413 spinAlarm->setValue( presetTime );
414 if ( sound != Event::Silent )
415 comboSound->setCurrentItem( 1 );
416 else
417 comboSound->setCurrentItem( 0 );
418}
419
420void DateEntry::initCombos()
421{
422 comboStart->clear();
423 comboEnd->clear();
424 if ( ampm ) {
425 for ( int i = 0; i < 24; i++ ) {
426 if ( i == 0 ) {
427 comboStart->insertItem( "12:00 AM" );
428 comboStart->insertItem( "12:30 AM" );
429 comboEnd->insertItem( "12:00 AM" );
430 comboEnd->insertItem( "12:30 AM" );
431 } else if ( i == 12 ) {
432 comboStart->insertItem( "12:00 PM" );
433 comboStart->insertItem( "12:30 PM" );
434 comboEnd->insertItem( "12:00 PM" );
435 comboEnd->insertItem( "12:30 PM" );
436 } else if ( i > 12 ) {
437 comboStart->insertItem( QString::number( i - 12 ) + ":00 PM" );
438 comboStart->insertItem( QString::number( i - 12 ) + ":30 PM" );
439 comboEnd->insertItem( QString::number( i - 12 ) + ":00 PM" );
440 comboEnd->insertItem( QString::number( i - 12 ) + ":30 PM" );
441 } else {
442 comboStart->insertItem( QString::number( i) + ":00 AM" );
443 comboStart->insertItem( QString::number( i ) + ":30 AM" );
444 comboEnd->insertItem( QString::number( i ) + ":00 AM" );
445 comboEnd->insertItem( QString::number( i ) + ":30 AM" );
446 }
447 }
448 } else {
449 for ( int i = 0; i < 24; i++ ) {
450 if ( i < 10 ) {
451 comboStart->insertItem( QString("0")
452 + QString::number(i) + ":00" );
453 comboStart->insertItem( QString("0")
454 + QString::number(i) + ":30" );
455 comboEnd->insertItem( QString("0")
456 + QString::number(i) + ":00" );
457 comboEnd->insertItem( QString("0")
458 + QString::number(i) + ":30" );
459 } else {
460 comboStart->insertItem( QString::number(i) + ":00" );
461 comboStart->insertItem( QString::number(i) + ":30" );
462 comboEnd->insertItem( QString::number(i) + ":00" );
463 comboEnd->insertItem( QString::number(i) + ":30" );
464 }
465 }
466 }
467}
468
469void DateEntry::slotChangeClock( bool whichClock )
470{
471 ampm = whichClock;
472 initCombos();
473 setDates( QDateTime( startDate, startTime ), QDateTime( endDate, endTime ) );
474}