-rw-r--r-- | libopie2/opiepim/core/orecur.cpp | 25 | ||||
-rw-r--r-- | libopie2/opiepim/core/orecur.h | 12 | ||||
-rw-r--r-- | libopie2/opiepim/core/otimezone.cpp | 104 | ||||
-rw-r--r-- | libopie2/opiepim/core/otimezone.h | 71 |
4 files changed, 193 insertions, 19 deletions
diff --git a/libopie2/opiepim/core/orecur.cpp b/libopie2/opiepim/core/orecur.cpp index daf3506..e6a4787 100644 --- a/libopie2/opiepim/core/orecur.cpp +++ b/libopie2/opiepim/core/orecur.cpp | |||
@@ -1,78 +1,80 @@ | |||
1 | #include <qshared.h> | 1 | #include <qshared.h> |
2 | 2 | ||
3 | #include <qtopia/timeconversion.h> | 3 | #include <qtopia/timeconversion.h> |
4 | 4 | ||
5 | #include "orecur.h" | 5 | #include "orecur.h" |
6 | 6 | ||
7 | struct ORecur::Data : public QShared { | 7 | struct ORecur::Data : public QShared { |
8 | Data() : QShared() { | 8 | Data() : QShared() { |
9 | type = ORecur::NoRepeat; | 9 | type = ORecur::NoRepeat; |
10 | freq = -1; | 10 | freq = -1; |
11 | days = 0; | 11 | days = 0; |
12 | pos = 0; | 12 | pos = 0; |
13 | create = -1; | 13 | create = QDateTime::currentDateTime(); |
14 | hasEnd = FALSE; | 14 | hasEnd = FALSE; |
15 | end = 0; | 15 | end = QDate::currentDate(); |
16 | } | 16 | } |
17 | char days; // Q_UINT8 for 8 seven days;) | 17 | char days; // Q_UINT8 for 8 seven days;) |
18 | ORecur::RepeatType type; | 18 | ORecur::RepeatType type; |
19 | int freq; | 19 | int freq; |
20 | int pos; | 20 | int pos; |
21 | bool hasEnd : 1; | 21 | bool hasEnd : 1; |
22 | time_t end; | 22 | QDate end; |
23 | time_t create; | 23 | QDateTime create; |
24 | int rep; | 24 | int rep; |
25 | QString app; | 25 | QString app; |
26 | ExceptionList list; | 26 | ExceptionList list; |
27 | QDate start; | 27 | QDate start; |
28 | }; | 28 | }; |
29 | 29 | ||
30 | 30 | ||
31 | ORecur::ORecur() { | 31 | ORecur::ORecur() { |
32 | data = new Data; | 32 | data = new Data; |
33 | } | 33 | } |
34 | ORecur::ORecur( const ORecur& rec) | 34 | ORecur::ORecur( const ORecur& rec) |
35 | : data( rec.data ) | 35 | : data( rec.data ) |
36 | { | 36 | { |
37 | data->ref(); | 37 | data->ref(); |
38 | } | 38 | } |
39 | ORecur::~ORecur() { | 39 | ORecur::~ORecur() { |
40 | if ( data->deref() ) { | 40 | if ( data->deref() ) { |
41 | delete data; | 41 | delete data; |
42 | data = 0l; | 42 | data = 0l; |
43 | } | 43 | } |
44 | } | 44 | } |
45 | void ORecur::deref() { | 45 | void ORecur::deref() { |
46 | if ( data->deref() ) { | 46 | if ( data->deref() ) { |
47 | delete data; | 47 | delete data; |
48 | data = 0l; | 48 | data = 0l; |
49 | } | 49 | } |
50 | } | 50 | } |
51 | bool ORecur::operator==( const ORecur& )const { | 51 | bool ORecur::operator==( const ORecur& )const { |
52 | return false; | 52 | return false; |
53 | } | 53 | } |
54 | ORecur &ORecur::operator=( const ORecur& re) { | 54 | ORecur &ORecur::operator=( const ORecur& re) { |
55 | if ( *this == re ) return *this; | ||
56 | |||
55 | re.data->ref(); | 57 | re.data->ref(); |
56 | deref(); | 58 | deref(); |
57 | data = re.data; | 59 | data = re.data; |
58 | 60 | ||
59 | return *this; | 61 | return *this; |
60 | } | 62 | } |
61 | bool ORecur::doesRecur()const { | 63 | bool ORecur::doesRecur()const { |
62 | return !( type() == NoRepeat ); | 64 | return !( type() == NoRepeat ); |
63 | } | 65 | } |
64 | /* | 66 | /* |
65 | * we try to be smart here | 67 | * we try to be smart here |
66 | * | 68 | * |
67 | */ | 69 | */ |
68 | bool ORecur::doesRecur( const QDate& date ) { | 70 | bool ORecur::doesRecur( const QDate& date ) { |
69 | /* the day before the recurrance */ | 71 | /* the day before the recurrance */ |
70 | QDate da = date.addDays(-1); | 72 | QDate da = date.addDays(-1); |
71 | 73 | ||
72 | QDate recur; | 74 | QDate recur; |
73 | if (!nextOcurrence( da, recur ) ) | 75 | if (!nextOcurrence( da, recur ) ) |
74 | return false; | 76 | return false; |
75 | 77 | ||
76 | return (recur == date); | 78 | return (recur == date); |
77 | } | 79 | } |
78 | // FIXME unuglify! | 80 | // FIXME unuglify! |
@@ -345,93 +347,86 @@ bool ORecur::nextOcurrence( const QDate& from, QDate& next ) { | |||
345 | next = QDate(iyear, imonth, iday); | 347 | next = QDate(iyear, imonth, iday); |
346 | if ((next > endDate()) && hasEndDate() ) | 348 | if ((next > endDate()) && hasEndDate() ) |
347 | return FALSE; | 349 | return FALSE; |
348 | return TRUE; | 350 | return TRUE; |
349 | default: | 351 | default: |
350 | return FALSE; | 352 | return FALSE; |
351 | } | 353 | } |
352 | } | 354 | } |
353 | ORecur::RepeatType ORecur::type()const{ | 355 | ORecur::RepeatType ORecur::type()const{ |
354 | return data->type; | 356 | return data->type; |
355 | } | 357 | } |
356 | int ORecur::frequency()const { | 358 | int ORecur::frequency()const { |
357 | return data->freq; | 359 | return data->freq; |
358 | } | 360 | } |
359 | int ORecur::position()const { | 361 | int ORecur::position()const { |
360 | return data->pos; | 362 | return data->pos; |
361 | } | 363 | } |
362 | char ORecur::days() const{ | 364 | char ORecur::days() const{ |
363 | return data->days; | 365 | return data->days; |
364 | } | 366 | } |
365 | bool ORecur::hasEndDate()const { | 367 | bool ORecur::hasEndDate()const { |
366 | return data->hasEnd; | 368 | return data->hasEnd; |
367 | } | 369 | } |
368 | QDate ORecur::endDate()const { | 370 | QDate ORecur::endDate()const { |
369 | return TimeConversion::fromUTC( data->end ).date(); | 371 | return data->end; |
370 | } | 372 | } |
371 | QDate ORecur::start()const{ | 373 | QDate ORecur::start()const{ |
372 | return data->start; | 374 | return data->start; |
373 | } | 375 | } |
374 | time_t ORecur::endDateUTC()const { | 376 | QDateTime ORecur::createdDateTime()const { |
375 | return data->end; | ||
376 | } | ||
377 | time_t ORecur::createTime()const { | ||
378 | return data->create; | 377 | return data->create; |
379 | } | 378 | } |
380 | int ORecur::repetition()const { | 379 | int ORecur::repetition()const { |
381 | return data->rep; | 380 | return data->rep; |
382 | } | 381 | } |
383 | QString ORecur::service()const { | 382 | QString ORecur::service()const { |
384 | return data->app; | 383 | return data->app; |
385 | } | 384 | } |
386 | ORecur::ExceptionList& ORecur::exceptions() { | 385 | ORecur::ExceptionList& ORecur::exceptions() { |
387 | return data->list; | 386 | return data->list; |
388 | } | 387 | } |
389 | void ORecur::setType( const RepeatType& z) { | 388 | void ORecur::setType( const RepeatType& z) { |
390 | checkOrModify(); | 389 | checkOrModify(); |
391 | data->type = z; | 390 | data->type = z; |
392 | } | 391 | } |
393 | void ORecur::setFrequency( int freq ) { | 392 | void ORecur::setFrequency( int freq ) { |
394 | checkOrModify(); | 393 | checkOrModify(); |
395 | data->freq = freq; | 394 | data->freq = freq; |
396 | } | 395 | } |
397 | void ORecur::setPosition( int pos ) { | 396 | void ORecur::setPosition( int pos ) { |
398 | checkOrModify(); | 397 | checkOrModify(); |
399 | data->pos = pos; | 398 | data->pos = pos; |
400 | } | 399 | } |
401 | void ORecur::setDays( char c ) { | 400 | void ORecur::setDays( char c ) { |
402 | checkOrModify(); | 401 | checkOrModify(); |
403 | data->days = c; | 402 | data->days = c; |
404 | } | 403 | } |
405 | void ORecur::setEndDate( const QDate& dt) { | 404 | void ORecur::setEndDate( const QDate& dt) { |
406 | checkOrModify(); | 405 | checkOrModify(); |
407 | data->end = TimeConversion::toUTC( dt ); | 406 | data->end = dt; |
408 | } | ||
409 | void ORecur::setEndDateUTC( time_t t) { | ||
410 | checkOrModify(); | ||
411 | data->end = t; | ||
412 | } | 407 | } |
413 | void ORecur::setCreateTime( time_t t) { | 408 | void ORecur::setCreatedDateTime( const QDateTime& t) { |
414 | checkOrModify(); | 409 | checkOrModify(); |
415 | data->create = t; | 410 | data->create = t; |
416 | } | 411 | } |
417 | void ORecur::setHasEndDate( bool b) { | 412 | void ORecur::setHasEndDate( bool b) { |
418 | checkOrModify(); | 413 | checkOrModify(); |
419 | data->hasEnd = b; | 414 | data->hasEnd = b; |
420 | } | 415 | } |
421 | void ORecur::setRepitition( int rep ) { | 416 | void ORecur::setRepitition( int rep ) { |
422 | checkOrModify(); | 417 | checkOrModify(); |
423 | data->rep = rep; | 418 | data->rep = rep; |
424 | } | 419 | } |
425 | void ORecur::setService( const QString& app ) { | 420 | void ORecur::setService( const QString& app ) { |
426 | checkOrModify(); | 421 | checkOrModify(); |
427 | data->app = app; | 422 | data->app = app; |
428 | } | 423 | } |
429 | void ORecur::setStart( const QDate& dt ) { | 424 | void ORecur::setStart( const QDate& dt ) { |
430 | checkOrModify(); | 425 | checkOrModify(); |
431 | data->start = dt; | 426 | data->start = dt; |
432 | } | 427 | } |
433 | void ORecur::checkOrModify() { | 428 | void ORecur::checkOrModify() { |
434 | if ( data->count != 1 ) { | 429 | if ( data->count != 1 ) { |
435 | data->deref(); | 430 | data->deref(); |
436 | Data* d2 = new Data; | 431 | Data* d2 = new Data; |
437 | d2->days = data->days; | 432 | d2->days = data->days; |
diff --git a/libopie2/opiepim/core/orecur.h b/libopie2/opiepim/core/orecur.h index 8713d97..1e0014b 100644 --- a/libopie2/opiepim/core/orecur.h +++ b/libopie2/opiepim/core/orecur.h | |||
@@ -14,70 +14,74 @@ | |||
14 | class ORecur { | 14 | class ORecur { |
15 | public: | 15 | public: |
16 | typedef QValueList<QDate> ExceptionList; | 16 | typedef QValueList<QDate> ExceptionList; |
17 | enum RepeatType{ NoRepeat = -1, Daily, Weekly, MonthlyDay, | 17 | enum RepeatType{ NoRepeat = -1, Daily, Weekly, MonthlyDay, |
18 | MonthlyDate, Yearly }; | 18 | MonthlyDate, Yearly }; |
19 | enum Days { MON = 0x01, TUE = 0x02, WED = 0x04, THU = 0x08, | 19 | enum Days { MON = 0x01, TUE = 0x02, WED = 0x04, THU = 0x08, |
20 | FRI = 0x10, SAT = 0x20, SUN = 0x40 }; | 20 | FRI = 0x10, SAT = 0x20, SUN = 0x40 }; |
21 | ORecur(); | 21 | ORecur(); |
22 | ORecur( const ORecur& ); | 22 | ORecur( const ORecur& ); |
23 | ~ORecur(); | 23 | ~ORecur(); |
24 | 24 | ||
25 | ORecur &operator=( const ORecur& ); | 25 | ORecur &operator=( const ORecur& ); |
26 | bool operator==(const ORecur& )const; | 26 | bool operator==(const ORecur& )const; |
27 | 27 | ||
28 | bool doesRecur()const; | 28 | bool doesRecur()const; |
29 | /* if it recurrs on that day */ | 29 | /* if it recurrs on that day */ |
30 | bool doesRecur( const QDate& ); | 30 | bool doesRecur( const QDate& ); |
31 | RepeatType type()const; | 31 | RepeatType type()const; |
32 | int frequency()const; | 32 | int frequency()const; |
33 | int position()const; | 33 | int position()const; |
34 | char days()const; | 34 | char days()const; |
35 | bool hasEndDate()const; | 35 | bool hasEndDate()const; |
36 | QDate start()const; | 36 | QDate start()const; |
37 | QDate endDate()const; | 37 | QDate endDate()const; |
38 | time_t endDateUTC()const; | 38 | QDateTime createdDateTime()const; |
39 | time_t createTime()const; | 39 | /** |
40 | * starting on monday=0, sunday=6 | ||
41 | * for convience | ||
42 | */ | ||
43 | bool repeatOnWeekDay( int day )const; | ||
40 | 44 | ||
41 | /** | 45 | /** |
42 | * FromWhereToStart is not included!!! | 46 | * FromWhereToStart is not included!!! |
43 | */ | 47 | */ |
44 | bool nextOcurrence( const QDate& FromWhereToStart, QDate &recurDate ); | 48 | bool nextOcurrence( const QDate& FromWhereToStart, QDate &recurDate ); |
49 | |||
45 | /** | 50 | /** |
46 | * The module this ORecur belongs to | 51 | * The module this ORecur belongs to |
47 | */ | 52 | */ |
48 | QString service()const; | 53 | QString service()const; |
49 | 54 | ||
50 | /* | 55 | /* |
51 | * reference to the exception list | 56 | * reference to the exception list |
52 | */ | 57 | */ |
53 | ExceptionList &exceptions(); | 58 | ExceptionList &exceptions(); |
54 | 59 | ||
55 | /** | 60 | /** |
56 | * the current repetition | 61 | * the current repetition |
57 | */ | 62 | */ |
58 | int repetition()const; | 63 | int repetition()const; |
59 | 64 | ||
60 | void setType( const RepeatType& ); | 65 | void setType( const RepeatType& ); |
61 | void setFrequency( int freq ); | 66 | void setFrequency( int freq ); |
62 | void setPosition( int pos ); | 67 | void setPosition( int pos ); |
63 | void setDays( char c); | 68 | void setDays( char c); |
64 | void setEndDate( const QDate& dt ); | 69 | void setEndDate( const QDate& dt ); |
65 | void setStart( const QDate& dt ); | 70 | void setStart( const QDate& dt ); |
66 | void setEndDateUTC( time_t ); | 71 | void setCreatedDateTime( const QDateTime& ); |
67 | void setCreateTime( time_t ); | ||
68 | void setHasEndDate( bool b ); | 72 | void setHasEndDate( bool b ); |
69 | void setRepitition(int ); | 73 | void setRepitition(int ); |
70 | 74 | ||
71 | void setService( const QString& ser ); | 75 | void setService( const QString& ser ); |
72 | private: | 76 | private: |
73 | void deref(); | 77 | void deref(); |
74 | inline void checkOrModify(); | 78 | inline void checkOrModify(); |
75 | 79 | ||
76 | 80 | ||
77 | class Data; | 81 | class Data; |
78 | Data* data; | 82 | Data* data; |
79 | class ORecurPrivate; | 83 | class ORecurPrivate; |
80 | ORecurPrivate *d; | 84 | ORecurPrivate *d; |
81 | }; | 85 | }; |
82 | 86 | ||
83 | #endif | 87 | #endif |
diff --git a/libopie2/opiepim/core/otimezone.cpp b/libopie2/opiepim/core/otimezone.cpp new file mode 100644 index 0000000..b2bd3aa --- a/dev/null +++ b/libopie2/opiepim/core/otimezone.cpp | |||
@@ -0,0 +1,104 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdlib.h> | ||
3 | |||
4 | #include <sys/types.h> | ||
5 | |||
6 | #include "otimezone.h" | ||
7 | |||
8 | namespace { | ||
9 | |||
10 | QDateTime utcTime( time_t t) { | ||
11 | tm* broken = ::gmtime( &t ); | ||
12 | QDateTime ret; | ||
13 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | ||
14 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | ||
15 | return ret; | ||
16 | } | ||
17 | QDateTime utcTime( time_t t, const QString& zone) { | ||
18 | QCString org = ::getenv( "TZ" ); | ||
19 | ::setenv( "TZ", zone.latin1(), true ); | ||
20 | ::tzset(); | ||
21 | |||
22 | tm* broken = ::localtime( &t ); | ||
23 | ::setenv( "TZ", org, true ); | ||
24 | |||
25 | QDateTime ret; | ||
26 | ret.setDate( QDate( broken->tm_year + 1900, broken->tm_mon +1, broken->tm_mday ) ); | ||
27 | ret.setTime( QTime( broken->tm_hour, broken->tm_min, broken->tm_sec ) ); | ||
28 | |||
29 | return ret; | ||
30 | } | ||
31 | time_t to_Time_t( const QDateTime& utc, const QString& str ) { | ||
32 | QDate d = utc.date(); | ||
33 | QTime t = utc.time(); | ||
34 | |||
35 | tm broken; | ||
36 | broken.tm_year = d.year() - 1900; | ||
37 | broken.tm_mon = d.month() - 1; | ||
38 | broken.tm_mday = d.day(); | ||
39 | broken.tm_hour = t.hour(); | ||
40 | broken.tm_min = t.minute(); | ||
41 | broken.tm_sec = t.second(); | ||
42 | |||
43 | QCString org = ::getenv( "TZ" ); | ||
44 | ::setenv( "TZ", str.latin1(), true ); | ||
45 | ::tzset(); | ||
46 | |||
47 | time_t ti = ::mktime( &broken ); | ||
48 | ::setenv( "TZ", org, true ); | ||
49 | |||
50 | return ti; | ||
51 | } | ||
52 | } | ||
53 | OTimeZone::OTimeZone( const ZoneName& zone ) | ||
54 | : m_name(zone) { | ||
55 | } | ||
56 | OTimeZone::~OTimeZone() { | ||
57 | } | ||
58 | |||
59 | bool OTimeZone::isValid()const { | ||
60 | return !m_name.isEmpty(); | ||
61 | } | ||
62 | |||
63 | /* | ||
64 | * we will get the current timezone | ||
65 | * and ask it to convert to the timezone date | ||
66 | */ | ||
67 | QDateTime OTimeZone::toLocalDateTime( const QDateTime& dt) { | ||
68 | return OTimeZone::current().toDateTime( dt, *this ); | ||
69 | } | ||
70 | QDateTime OTimeZone::toUTCDateTime( const QDateTime& dt ) { | ||
71 | return OTimeZone::utc().toDateTime( dt, *this ); | ||
72 | } | ||
73 | QDateTime OTimeZone::fromUTCDateTime( time_t t) { | ||
74 | return utcTime( t ); | ||
75 | } | ||
76 | QDateTime OTimeZone::toDateTime( time_t t) { | ||
77 | return utcTime( t, m_name ); | ||
78 | } | ||
79 | /* | ||
80 | * convert dt to utc using zone.m_name | ||
81 | * convert utc -> timeZoneDT using this->m_name | ||
82 | */ | ||
83 | QDateTime OTimeZone::toDateTime( const QDateTime& dt, const OTimeZone& zone ) { | ||
84 | time_t utc = to_Time_t( dt, zone.m_name ); | ||
85 | qWarning("%d %s", utc, zone.m_name.latin1() ); | ||
86 | return utcTime( utc, m_name ); | ||
87 | } | ||
88 | time_t OTimeZone::fromDateTime( const QDateTime& time ) { | ||
89 | return to_Time_t( time, m_name ); | ||
90 | } | ||
91 | time_t OTimeZone::fromUTCDateTime( const QDateTime& time ) { | ||
92 | return to_Time_t( time, "UTC" ); | ||
93 | } | ||
94 | OTimeZone OTimeZone::current() { | ||
95 | QCString str = ::getenv("TZ"); | ||
96 | OTimeZone zone( str ); | ||
97 | return zone; | ||
98 | } | ||
99 | OTimeZone OTimeZone::utc() { | ||
100 | return OTimeZone("UTC"); | ||
101 | } | ||
102 | QString OTimeZone::timeZone()const { | ||
103 | return m_name; | ||
104 | } | ||
diff --git a/libopie2/opiepim/core/otimezone.h b/libopie2/opiepim/core/otimezone.h new file mode 100644 index 0000000..bb08349 --- a/dev/null +++ b/libopie2/opiepim/core/otimezone.h | |||
@@ -0,0 +1,71 @@ | |||
1 | #ifndef OPIE_TIME_ZONE_H | ||
2 | #define OPIE_TIME_ZONE_H | ||
3 | |||
4 | #include <time.h> | ||
5 | #include <qdatetime.h> | ||
6 | |||
7 | /** | ||
8 | * A very primitive class to convert time | ||
9 | * from one timezone to another | ||
10 | * and to localtime | ||
11 | * and time_t | ||
12 | */ | ||
13 | class OTimeZone { | ||
14 | public: | ||
15 | typedef QString ZoneName; | ||
16 | OTimeZone( const ZoneName& = ZoneName::null ); | ||
17 | virtual ~OTimeZone(); // just in case. | ||
18 | |||
19 | bool isValid()const; | ||
20 | |||
21 | /** | ||
22 | * converts the QDateTime to a DateTime | ||
23 | * in the local timezone | ||
24 | * if QDateTime is 25th Jan and takes place in Europe/Berlin at 12h | ||
25 | * and the current timezone is Europe/London the returned | ||
26 | * time will be 11h. | ||
27 | */ | ||
28 | QDateTime toLocalDateTime( const QDateTime& dt ); | ||
29 | |||
30 | /** | ||
31 | * converts the QDateTime to UTC time | ||
32 | */ | ||
33 | QDateTime toUTCDateTime( const QDateTime& dt ); | ||
34 | |||
35 | /** | ||
36 | * reads the time_t into a QDateTime using UTC as timezone! | ||
37 | */ | ||
38 | QDateTime fromUTCDateTime( time_t ); | ||
39 | |||
40 | /** | ||
41 | * converts the time_t to the time in the timezone | ||
42 | */ | ||
43 | QDateTime toDateTime( time_t ); | ||
44 | |||
45 | /** | ||
46 | * converts the QDateTime from one timezone to this timeZone | ||
47 | */ | ||
48 | QDateTime toDateTime( const QDateTime&, const OTimeZone& timeZone ); | ||
49 | |||
50 | /** | ||
51 | * converts the date time into a time_t. It takes the timezone into account | ||
52 | */ | ||
53 | time_t fromDateTime( const QDateTime& ); | ||
54 | |||
55 | /** | ||
56 | * converts the datetime with timezone UTC | ||
57 | */ | ||
58 | time_t fromUTCDateTime( const QDateTime& ); | ||
59 | |||
60 | static OTimeZone current(); | ||
61 | static OTimeZone utc(); | ||
62 | |||
63 | QString timeZone()const; | ||
64 | private: | ||
65 | ZoneName m_name; | ||
66 | class Private; | ||
67 | Private* d; | ||
68 | }; | ||
69 | |||
70 | |||
71 | #endif | ||