summaryrefslogtreecommitdiff
path: root/noncore/unsupported/libopie/orecurrancewidget.cpp
blob: 33be2692f3b2e306c3a1fa71f862302bccb01c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#include <qapplication.h>
#include <qlabel.h>
#include <qspinbox.h>


#include "orecurrancewidget.h"

// Global Templates for use in setting up the repeat label...
// the problem is these strings get initialized before QPEApplication can install the translator -zecke
namespace {
QString strDayTemplate;
QString strYearTemplate;
QString strMonthDateTemplate;
QString strMonthDayTemplate;
QString strWeekTemplate;
QString dayLabel[7];
}

/*
 * static linkage to not polute the symbol table...
 * The problem is that const and static linkage are resolved prior to installing a translator
 * leading to that the above strings are translted but to the original we delay the init of these strings...
 * -zecke
 */
static void fillStrings() {
    strDayTemplate = QObject::tr("Every");
    strYearTemplate = QObject::tr("%1 %2 every ");
    strMonthDateTemplate = QObject::tr("The %1 every ");
    strMonthDayTemplate = QObject::tr("The %1 %2 of every");
    strWeekTemplate = QObject::tr("Every ");
    dayLabel[0] = QObject::tr("Monday");
    dayLabel[1] = QObject::tr("Tuesday");
    dayLabel[2] = QObject::tr("Wednesday");
    dayLabel[3] = QObject::tr("Thursday");
    dayLabel[4] = QObject::tr("Friday");
    dayLabel[5] = QObject::tr("Saturday");
    dayLabel[6] = QObject::tr("Sunday");
}

static QString numberPlacing( int x );	// return the proper word format for
                                        // x (1st, 2nd, etc)
static int week( const QDate &dt );    // what week in the month is dt?

/**
 * Constructs the Widget
 * @param startOnMonday Does the week start on monday
 * @param newStart The start date of the recurrence
 * @param parent The parent widget
 * @param name the name of object
 * @param modal if the dialog should be modal
 * @param fl Additional window flags
 */
ORecurranceWidget::ORecurranceWidget( bool startOnMonday,
                                      const QDate& newStart,
                                      QWidget* parent,
                                      const char* name,
                                      bool modal,
                                      WFlags fl )
    : ORecurranceBase( parent, name, modal, fl ),
      start( newStart ),
      currInterval( None ),
      startWeekOnMonday( startOnMonday )
{
     if (strDayTemplate.isEmpty() )
        fillStrings();

    init();
    fraType->setButton( currInterval );
    chkNoEnd->setChecked( TRUE );
    setupNone();
}

/**
 * Different constructor
 * @param startOnMonday Does the week start on monday?
 * @param rp Already set ORecur object
 * @param startDate The start date
 * @param parent The parent widget
 * @param name The name of the object
 * @param modal
 * @param fl The flags for window
 */
ORecurranceWidget::ORecurranceWidget( bool startOnMonday,
                                      const ORecur& rp, const QDate& startDate,
                                      QWidget* parent, const char* name,
                                      bool modal, WFlags fl)
    : ORecurranceBase( parent, name, modal, fl ),
      start( startDate ),
      end( rp.endDate() ),
      startWeekOnMonday( startOnMonday )
{
     if (strDayTemplate.isEmpty() )
        fillStrings();
    // do some stuff with the repeat pattern
    init();
    setRecurrence( rp );
}

ORecurranceWidget::~ORecurranceWidget() {
}

/**
 * set the start date
 * @param date the new start date
 */
void ORecurranceWidget::setStartDate( const QDate& date ) {
    setRecurrence( recurrence(), date );
}
/**
 * set the recurrence
 * @param rp  The ORecur object with the new recurrence rules
 */
void ORecurranceWidget::setRecurrence( const ORecur& rp ) {
    setRecurrence( rp, start );
}

/**
 * overloaded method taking ORecur and a new start date
 * @param rp Recurrence rule
 * @param date The new start date
 */
void ORecurranceWidget::setRecurrence( const ORecur& rp, const QDate& date ) {
    start = date;
    end = rp.endDate();
    switch ( rp.type() ) {
    default:
    case ORecur::NoRepeat:
        currInterval = None;
        setupNone();
        break;
    case ORecur::Daily:
        currInterval = Day;
        setupDaily();
        break;
    case ORecur::Weekly:
        currInterval = Week;
        setupWeekly();
        int day, buttons;
        for ( day = 0x01, buttons = 0; buttons < 7;
              day = day << 1, buttons++ ) {
            if ( rp.days() & day ) {
                if ( startWeekOnMonday )
                    fraExtra->setButton( buttons );
                else {
                    if ( buttons == 7 )
                        fraExtra->setButton( 0 );
                    else
                        fraExtra->setButton( buttons + 1 );
 		    }
		}
	    }
        slotWeekLabel();
        break;
    case ORecur::MonthlyDay:
        currInterval = Month;
        setupMonthly();
        fraExtra->setButton( 0 );
        slotMonthLabel( 0 );
        break;
    case ORecur::MonthlyDate:
        currInterval = Month;
        setupMonthly();
        fraExtra->setButton( 1 );
        slotMonthLabel( 1 );
        break;
    case ORecur::Yearly:
        currInterval = Year;
        setupYearly();
        break;
    }
    fraType->setButton( currInterval );
    spinFreq->setValue( rp.frequency() );
    if ( !rp.hasEndDate() ) {
	cmdEnd->setText( tr("No End Date") );
	chkNoEnd->setChecked( TRUE );
    } else
	cmdEnd->setText( TimeString::shortDate( end ) );
}

/**
 * the user selected recurrence rule.
 * @return The recurrence rule.
 */
ORecur ORecurranceWidget::recurrence()const {
    QListIterator<QToolButton> it( listRTypeButtons );
    QListIterator<QToolButton> itExtra( listExtra );
    ORecur rpTmp;
    int i;
    for ( i = 0; *it; ++it, i++ ) {
	if ( (*it)->isOn() ) {
	    switch ( i ) {
            case None:
                rpTmp.setType( ORecur::NoRepeat );
                break;
            case Day:
                rpTmp.setType( ORecur::Daily );
                break;
            case Week:{
                rpTmp.setType( ORecur::Weekly );
                int day;
                int day2 = 0;
                for ( day = 1; *itExtra; ++itExtra, day = day << 1 ) {
                    if ( (*itExtra)->isOn() ) {
                        if ( startWeekOnMonday )
                            day2 |= day;
                        else {
                            if ( day == 1 )
                                day2 |= Event::SUN;
                            else
                                day2 |= day >> 1;
                        }
                    }
                }
                rpTmp.setDays( day2 );
            }
                break;
            case Month:
                if ( cmdExtra1->isOn() )
                    rpTmp.setType( ORecur::MonthlyDay );
                else if ( cmdExtra2->isOn() )
                    rpTmp.setType( ORecur::MonthlyDate );
                // figure out the montly day...
                rpTmp.setPosition(  week( start ) );
                break;
            case Year:
                rpTmp.setType( ORecur::Yearly );
                break;
	    }
	    break;  // no need to keep looking!
	}
    }
    rpTmp.setFrequency(spinFreq->value() );
    rpTmp.setHasEndDate( !chkNoEnd->isChecked() );
    if ( rpTmp.hasEndDate() ) {
	rpTmp.setEndDate( end );
    }
    // timestamp it...
//    rpTmp.setCreateTime(  ); current DateTime is already set -zecke
    return rpTmp;
}

/**
 * Return the end date of the recurrence. This is only
 * valid if the recurrence rule does contain an enddate
 */
QDate ORecurranceWidget::endDate()const {
    return end;
}
void ORecurranceWidget::slotSetRType(int rtype) {
   // now call the right function based on the type...
    currInterval = static_cast<repeatButtons>(rtype);
    switch ( currInterval ) {
    case None:
        setupNone();
        break;
    case Day:
        setupDaily();
        break;
    case Week:
        setupWeekly();
        slotWeekLabel();
        break;
    case Month:
        setupMonthly();
        cmdExtra2->setOn( TRUE );
        slotMonthLabel( 1 );
        break;
    case Year:
        setupYearly();
        break;
    }
}
void ORecurranceWidget::endDateChanged(int y, int m, int d) {
    end.setYMD( y, m, d );
    if ( end < start )
	end = start;
    cmdEnd->setText(  TimeString::shortDate( end ) );
    repeatPicker->setDate( end.year(), end.month(), end.day() );
}
void ORecurranceWidget::slotNoEnd( bool unused) {
    // if the item was toggled, then go ahead and set it to the maximum date
    if ( unused ) {
	end.setYMD( 3000, 12, 31 );
	cmdEnd->setText( tr("No End Date") );
    } else {
	end = start;
	cmdEnd->setText( TimeString::shortDate(end) );
    }
}
void ORecurranceWidget::setupRepeatLabel( const QString& s) {
    lblVar1->setText( s );
}
void ORecurranceWidget::setupRepeatLabel( int x) {
    // change the spelling based on the value of x
    QString strVar2;

    if ( x > 1 )
	lblVar1->show();
    else
	lblVar1->hide();

    switch ( currInterval ) {
	case None:
	    break;
	case Day:
	    if ( x > 1 )
		strVar2 = tr( "days" );
	    else
		strVar2 = tr( "day" );
	    break;
	case Week:
	    if ( x > 1 )
		strVar2 = tr( "weeks" );
	    else
		strVar2 = tr( "week" );
	    break;
	case Month:
	    if ( x > 1 )
		strVar2 = tr( "months" );
	    else
		strVar2 = tr( "month" );
	    break;
	case Year:
	    if ( x > 1 )
		strVar2 = tr( "years" );
	    else
		strVar2 = tr( "year" );
	    break;
    }
    if ( !strVar2.isNull() )
	lblVar2->setText( strVar2 );
}
void ORecurranceWidget::slotWeekLabel() {
    QString str;
    QListIterator<QToolButton> it( listExtra );
    unsigned int i;
    unsigned int keepMe;
    bool bNeedCarriage = FALSE;
    // don't do something we'll regret!!!
    if ( currInterval != Week )
	return;

    if ( startWeekOnMonday )
	keepMe = start.dayOfWeek() - 1;
    else
	keepMe = start.dayOfWeek() % 7;

    QStringList list;
    for ( i = 0; *it; ++it, i++ ) {
	// a crazy check, if you are repeating weekly, the current day
	// must be selected!!!
	if ( i == keepMe && !( (*it)->isOn() ) )
	    (*it)->setOn( TRUE );
	if ( (*it)->isOn() ) {
	    if ( startWeekOnMonday )
		list.append( dayLabel[i] );
	    else {
		if ( i == 0 )
		    list.append( dayLabel[6] );
		else
		    list.append( dayLabel[i - 1] );
	    }
	}
    }
    QStringList::Iterator itStr;
    for ( i = 0, itStr = list.begin(); itStr != list.end(); ++itStr, i++ ) {
	if ( i == 3 )
	    bNeedCarriage = TRUE;
	else
	    bNeedCarriage = FALSE;
	if ( str.isNull() )
	    str = *itStr;
	else if ( i == list.count() - 1 ) {
	    if ( i < 2 )
		str += tr(" and ") + *itStr;
	    else {
		if ( bNeedCarriage )
		    str += tr( ",\nand " ) + *itStr;
		else
		    str += tr( ", and " ) + *itStr;
	    }
	} else {
	    if ( bNeedCarriage )
		str += ",\n" + *itStr;
	    else
		str += ", " + *itStr;
	}
    }
    str = str.prepend( tr("on ") );

    lblWeekVar->setText( str );
}
void ORecurranceWidget::slotMonthLabel(int type) {
    QString str;
    if ( currInterval != Month || type > 1 )
	return;
    if ( type == 1 )
		str = strMonthDateTemplate.arg( numberPlacing(start.day()) );
    else
		str = strMonthDayTemplate.arg( numberPlacing(week(start)))
			  .arg( dayLabel[start.dayOfWeek() - 1] );
    lblRepeat->setText( str );
}
void ORecurranceWidget::slotChangeStartOfWeek( bool onMonday ) {
 startWeekOnMonday = onMonday;
    // we need to make this unintrusive as possible...
    int saveSpin = spinFreq->value();
    char days = 0;
    int day;
    QListIterator<QToolButton> itExtra( listExtra );
    for ( day = 1; *itExtra; ++itExtra, day = day << 1 ) {
	if ( (*itExtra)->isOn() ) {
	    if ( !startWeekOnMonday )
		days |= day;
	    else {
		if ( day == 1 )
		    days |= ORecur::SUN;
		else
		    days |= day >> 1;
	    }
	}
    }
    setupWeekly();
    spinFreq->setValue( saveSpin );
    int buttons;
    for ( day = 0x01, buttons = 0; buttons < 7;
	  day = day << 1, buttons++ ) {
	if ( days & day ) {
	    if ( startWeekOnMonday )
		fraExtra->setButton( buttons );
	    else {
		if ( buttons == 7 )
		    fraExtra->setButton( 0 );
		else
		    fraExtra->setButton( buttons + 1 );
	    }
	}
    }
    slotWeekLabel();
}
void ORecurranceWidget::setupNone() {
   lblRepeat->setText( tr("No Repeat") );
    lblVar1->hide();
    lblVar2->hide();
    hideExtras();
    cmdEnd->hide();
    lblFreq->hide();
    lblEvery->hide();
    lblFreq->hide();
    spinFreq->hide();
    lblEnd->hide();
    lblWeekVar->hide();
}
void ORecurranceWidget::setupDaily() {
  hideExtras();
    lblWeekVar->hide();
    spinFreq->setValue( 1 );
    lblFreq->setText( tr("day(s)") );
    lblVar2->show();
    showRepeatStuff();
    lblRepeat->setText( strDayTemplate );
    setupRepeatLabel( 1 );
}
void ORecurranceWidget::setupWeekly() {
// reshow the buttons...
    fraExtra->setTitle( tr("Repeat On") );
    fraExtra->setExclusive( FALSE );
    fraExtra->show();
    if ( startWeekOnMonday ) {
	cmdExtra1->setText( tr("Mon") );
	cmdExtra2->setText( tr("Tue") );
	cmdExtra3->setText( tr("Wed") );
	cmdExtra4->setText( tr("Thu") );
	cmdExtra5->setText( tr("Fri") );
	cmdExtra6->setText( tr("Sat") );
	cmdExtra7->setText( tr("Sun") );
    } else {
	cmdExtra1->setText( tr("Sun") );
	cmdExtra2->setText( tr("Mon") );
	cmdExtra3->setText( tr("Tue") );
	cmdExtra4->setText( tr("Wed") );
	cmdExtra5->setText( tr("Thu") );
	cmdExtra6->setText( tr("Fri") );
	cmdExtra7->setText( tr("Sat") );
    }
    // I hope clustering these improve performance....
    cmdExtra1->setOn( FALSE );
    cmdExtra2->setOn( FALSE );
    cmdExtra3->setOn( FALSE );
    cmdExtra4->setOn( FALSE );
    cmdExtra5->setOn( FALSE );
    cmdExtra6->setOn( FALSE );
    cmdExtra7->setOn( FALSE );

    cmdExtra1->show();
    cmdExtra2->show();
    cmdExtra3->show();
    cmdExtra4->show();
    cmdExtra5->show();
    cmdExtra6->show();
    cmdExtra7->show();

    lblWeekVar->show();
    spinFreq->setValue( 1 );
    // might as well set the day too...
    if ( startWeekOnMonday ) {
	fraExtra->setButton( start.dayOfWeek() - 1 );
    } else {
	fraExtra->setButton( start.dayOfWeek() % 7 );
    }
    lblFreq->setText( tr("week(s)") );
    lblVar2->show();
    showRepeatStuff();
    setupRepeatLabel( 1 );
}
void ORecurranceWidget::setupMonthly() {
    hideExtras();
    lblWeekVar->hide();
    fraExtra->setTitle( tr("Repeat By") );
    fraExtra->setExclusive( TRUE );
    fraExtra->show();
    cmdExtra1->setText( tr("Day") );
    cmdExtra1->show();
    cmdExtra2->setText( tr("Date") );
    cmdExtra2->show();
    spinFreq->setValue( 1 );
    lblFreq->setText( tr("month(s)") );
    lblVar2->show();
    showRepeatStuff();
    setupRepeatLabel( 1 );
}
void ORecurranceWidget::setupYearly() {
hideExtras();
    lblWeekVar->hide();
    spinFreq->setValue( 1 );
    lblFreq->setText( tr("year(s)") );
    lblFreq->show();
    lblFreq->show();
    showRepeatStuff();
    lblVar2->show();
    QString strEvery = strYearTemplate.arg( start.monthName(start.month()) ).arg( numberPlacing(start.day()) );
    lblRepeat->setText( strEvery );
    setupRepeatLabel( 1 );

}
void ORecurranceWidget::init() {
 QPopupMenu *m1 = new QPopupMenu( this );
    repeatPicker = new DateBookMonth( m1, 0, TRUE );
    m1->insertItem( repeatPicker );
    cmdEnd->setPopup( m1 );
    cmdEnd->setPopupDelay( 0 );

    QObject::connect( repeatPicker, SIGNAL(dateClicked(int,int,int)),
                      this, SLOT(endDateChanged(int,int,int)) );
    QObject::connect( qApp, SIGNAL(weekChanged(bool)),
		      this, SLOT(slotChangeStartOfWeek(bool)) );

    listRTypeButtons.setAutoDelete( TRUE );
    listRTypeButtons.append( cmdNone );
    listRTypeButtons.append( cmdDay );
    listRTypeButtons.append( cmdWeek );
    listRTypeButtons.append( cmdMonth );
    listRTypeButtons.append( cmdYear );

    listExtra.setAutoDelete( TRUE );
    listExtra.append( cmdExtra1 );
    listExtra.append( cmdExtra2 );
    listExtra.append( cmdExtra3 );
    listExtra.append( cmdExtra4 );
    listExtra.append( cmdExtra5 );
    listExtra.append( cmdExtra6 );
    listExtra.append( cmdExtra7 );
}
void ORecurranceWidget::hideExtras() {
      // hide the extra buttons...
    fraExtra->hide();
    chkNoEnd->hide();
    QListIterator<QToolButton> it( listExtra );
    for ( ; *it; ++it ) {
	(*it)->hide();
	(*it)->setOn( FALSE );
    }
}
void ORecurranceWidget::showRepeatStuff() {
    cmdEnd->show();
    chkNoEnd->show();
    lblFreq->show();
    lblEvery->show();
    lblFreq->show();
    spinFreq->show();
    lblEnd->show();
    lblRepeat->setText( tr("Every") );
}


static int week( const QDate &start )
{
    // figure out the week...
    int stop = start.day(),
        sentinel = start.dayOfWeek(),
        dayOfWeek = QDate( start.year(), start.month(), 1 ).dayOfWeek(),
        week = 1,
	i;
    for ( i = 1; i < stop; i++ ) {
	if ( dayOfWeek++ == sentinel )
	    week++;
	if ( dayOfWeek > 7 )
	    dayOfWeek = 0;
    }
    return week;
}

static QString numberPlacing( int x )
{
    // I hope this works in other languages besides english...
    QString str = QString::number( x );
    switch ( x % 10 ) {
	case 1:
	    str += QWidget::tr( "st" );
	    break;
	case 2:
	    str += QWidget::tr( "nd" );
	    break;
	case 3:
	    str += QWidget::tr( "rd" );
	    break;
	default:
	    str += QWidget::tr( "th" );
	    break;
    }
    return str;
}