summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/core/orecur.cpp
Unidiff
Diffstat (limited to 'libopie2/opiepim/core/orecur.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/core/orecur.cpp66
1 files changed, 65 insertions, 1 deletions
diff --git a/libopie2/opiepim/core/orecur.cpp b/libopie2/opiepim/core/orecur.cpp
index e6a4787..e3b45b4 100644
--- a/libopie2/opiepim/core/orecur.cpp
+++ b/libopie2/opiepim/core/orecur.cpp
@@ -1,28 +1,31 @@
1#include <time.h>
2
1#include <qshared.h> 3#include <qshared.h>
2 4
3#include <qtopia/timeconversion.h> 5#include <qtopia/timeconversion.h>
4 6
7#include "otimezone.h"
5#include "orecur.h" 8#include "orecur.h"
6 9
7struct ORecur::Data : public QShared { 10struct ORecur::Data : public QShared {
8 Data() : QShared() { 11 Data() : QShared() {
9 type = ORecur::NoRepeat; 12 type = ORecur::NoRepeat;
10 freq = -1; 13 freq = -1;
11 days = 0; 14 days = 0;
12 pos = 0; 15 pos = 0;
13 create = QDateTime::currentDateTime(); 16 create = QDateTime::currentDateTime();
14 hasEnd = FALSE; 17 hasEnd = FALSE;
15 end = QDate::currentDate(); 18 end = QDate::currentDate();
16 } 19 }
17 char days; // Q_UINT8 for 8 seven days;) 20 char days; // Q_UINT8 for 8 seven days;)
18 ORecur::RepeatType type; 21 ORecur::RepeatType type;
19 int freq; 22 int freq;
20 int pos; 23 int pos;
21 bool hasEnd : 1; 24 bool hasEnd : 1;
22 QDate end; 25 QDate end;
23 QDateTime create; 26 QDateTime create;
24 int rep; 27 int rep;
25 QString app; 28 QString app;
26 ExceptionList list; 29 ExceptionList list;
27 QDate start; 30 QDate start;
28}; 31};
@@ -60,63 +63,71 @@ ORecur &ORecur::operator=( const ORecur& re) {
60 63
61 return *this; 64 return *this;
62} 65}
63bool ORecur::doesRecur()const { 66bool ORecur::doesRecur()const {
64 return !( type() == NoRepeat ); 67 return !( type() == NoRepeat );
65} 68}
66/* 69/*
67 * we try to be smart here 70 * we try to be smart here
68 * 71 *
69 */ 72 */
70bool ORecur::doesRecur( const QDate& date ) { 73bool ORecur::doesRecur( const QDate& date ) {
71 /* the day before the recurrance */ 74 /* the day before the recurrance */
72 QDate da = date.addDays(-1); 75 QDate da = date.addDays(-1);
73 76
74 QDate recur; 77 QDate recur;
75 if (!nextOcurrence( da, recur ) ) 78 if (!nextOcurrence( da, recur ) )
76 return false; 79 return false;
77 80
78 return (recur == date); 81 return (recur == date);
79} 82}
80// FIXME unuglify! 83// FIXME unuglify!
81// GPL from Datebookdb.cpp 84// GPL from Datebookdb.cpp
82// FIXME exception list! 85// FIXME exception list!
83bool ORecur::nextOcurrence( const QDate& from, QDate& next ) { 86bool ORecur::nextOcurrence( const QDate& from, QDate& next ) {
87 bool stillLooking;
88 stillLooking = p_nextOccurrence( from, next );
89 while ( stillLooking && data->list.contains(next) )
90 stillLooking = p_nextOccurrence( next.addDays(1), next );
91
92 return stillLooking;
93}
94bool ORecur::p_nextOccurrence( const QDate& from, QDate& next ) {
84 95
85 // easy checks, first are we too far in the future or too far in the past? 96 // easy checks, first are we too far in the future or too far in the past?
86 QDate tmpDate; 97 QDate tmpDate;
87 int freq = frequency(); 98 int freq = frequency();
88 int diff, diff2, a; 99 int diff, diff2, a;
89 int iday, imonth, iyear; 100 int iday, imonth, iyear;
90 int dayOfWeek = 0; 101 int dayOfWeek = 0;
91 int firstOfWeek = 0; 102 int firstOfWeek = 0;
92 int weekOfMonth; 103 int weekOfMonth;
93 104
94 105
95 if (hasEndDate() && endDate() < from) 106 if (hasEndDate() && endDate() < from)
96 return FALSE; 107 return FALSE;
97 108
98 if (start() >= from) { 109 if (start() >= from ) {
99 next = start(); 110 next = start();
100 return TRUE; 111 return TRUE;
101 } 112 }
102 113
103 switch ( type() ) { 114 switch ( type() ) {
104 case Weekly: 115 case Weekly:
105 /* weekly is just daily by 7 */ 116 /* weekly is just daily by 7 */
106 /* first convert the repeatPattern.Days() mask to the next 117 /* first convert the repeatPattern.Days() mask to the next
107 day of week valid after from */ 118 day of week valid after from */
108 dayOfWeek = from.dayOfWeek(); 119 dayOfWeek = from.dayOfWeek();
109 dayOfWeek--; /* we want 0-6, doco for above specs 1-7 */ 120 dayOfWeek--; /* we want 0-6, doco for above specs 1-7 */
110 121
111 /* this is done in case freq > 1 and from in week not 122 /* this is done in case freq > 1 and from in week not
112 for this round */ 123 for this round */
113 // firstOfWeek = 0; this is already done at decl. 124 // firstOfWeek = 0; this is already done at decl.
114 while(!((1 << firstOfWeek) & days() )) 125 while(!((1 << firstOfWeek) & days() ))
115 firstOfWeek++; 126 firstOfWeek++;
116 127
117 /* there is at least one 'day', or there would be no event */ 128 /* there is at least one 'day', or there would be no event */
118 while(!((1 << (dayOfWeek % 7)) & days() )) 129 while(!((1 << (dayOfWeek % 7)) & days() ))
119 dayOfWeek++; 130 dayOfWeek++;
120 131
121 dayOfWeek = dayOfWeek % 7; /* the actual day of week */ 132 dayOfWeek = dayOfWeek % 7; /* the actual day of week */
122 dayOfWeek -= start().dayOfWeek() -1; 133 dayOfWeek -= start().dayOfWeek() -1;
@@ -422,25 +433,78 @@ void ORecur::setService( const QString& app ) {
422 data->app = app; 433 data->app = app;
423} 434}
424void ORecur::setStart( const QDate& dt ) { 435void ORecur::setStart( const QDate& dt ) {
425 checkOrModify(); 436 checkOrModify();
426 data->start = dt; 437 data->start = dt;
427} 438}
428void ORecur::checkOrModify() { 439void ORecur::checkOrModify() {
429 if ( data->count != 1 ) { 440 if ( data->count != 1 ) {
430 data->deref(); 441 data->deref();
431 Data* d2 = new Data; 442 Data* d2 = new Data;
432 d2->days = data->days; 443 d2->days = data->days;
433 d2->type = data->type; 444 d2->type = data->type;
434 d2->freq = data->freq; 445 d2->freq = data->freq;
435 d2->pos = data->pos; 446 d2->pos = data->pos;
436 d2->hasEnd = data->hasEnd; 447 d2->hasEnd = data->hasEnd;
437 d2->end = data->end; 448 d2->end = data->end;
438 d2->create = data->create; 449 d2->create = data->create;
439 d2->rep = data->rep; 450 d2->rep = data->rep;
440 d2->app = data->app; 451 d2->app = data->app;
441 d2->list = data->list; 452 d2->list = data->list;
442 d2->start = data->start; 453 d2->start = data->start;
443 data = d2; 454 data = d2;
444 } 455 }
445} 456}
457QString ORecur::toString()const {
458 QString buf;
459
460 buf += " rtype=\"";
461 switch ( data->type ) {
462 case ORecur::Daily:
463 buf += "Daily";
464 break;
465 case ORecur::Weekly:
466 buf += "Weekly";
467 break;
468 case ORecur::MonthlyDay:
469 buf += "MonthlyDay";
470 break;
471 case ORecur::MonthlyDate:
472 buf += "MonthlyDate";
473 break;
474 case ORecur::Yearly:
475 buf += "Yearly";
476 break;
477 default:
478 buf += "NoRepeat";
479 break;
480 }
481 buf += "\"";
482 if (data->days > 0 )
483 buf += " rweekdays=\"" + QString::number( static_cast<int>( data->days ) ) + "\"";
484 if ( data->pos != 0 )
485 buf += " rposition=\"" + QString::number(data->pos ) + "\"";
486
487 buf += " rfreq=\"" + QString::number( data->freq ) + "\"";
488 buf += " rhasenddate=\"" + QString::number( static_cast<int>( data->hasEnd ) ) + "\"";
489 if ( data->hasEnd )
490 buf += " enddt=\""
491 + QString::number( OTimeZone::utc().fromUTCDateTime( QDateTime( data->end, QTime(12,0,0) ) ) )
492 + "\"";
493 buf += " created=\"" + QString::number( OTimeZone::utc().fromUTCDateTime( data->create ) ) + "\"";
494
495 if ( data->list.isEmpty() ) return buf;
496 // save exceptions list here!!
497 ExceptionList::ConstIterator it;
498 ExceptionList list = data->list;
499 buf += " exceptions=\"";
500 QDate date;
501 for ( it = list.begin(); it != list.end(); ++it ) {
502 date = (*it);
503 if ( it != list.begin() ) buf += " ";
504
505 buf += QCString().sprintf("%04d%02d%02d", date.year(), date.month(), date.day() );
506 }
507 buf += "\"";
446 508
509 return buf;
510}