Diffstat (limited to 'libopie2/opiepim/core/opimoccurrence.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/core/opimoccurrence.cpp | 319 |
1 files changed, 319 insertions, 0 deletions
diff --git a/libopie2/opiepim/core/opimoccurrence.cpp b/libopie2/opiepim/core/opimoccurrence.cpp new file mode 100644 index 0000000..14ab5cf --- a/dev/null +++ b/libopie2/opiepim/core/opimoccurrence.cpp | |||
@@ -0,0 +1,319 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | Copyright (C) 2003, 2004 Holger Freyther <zecke@handhelds.org> | ||
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
5 | .=l. | ||
6 | .>+-= | ||
7 | _;:, .> :=|. This program is free software; you can | ||
8 | .> <`_, > . <= redistribute it and/or modify it under | ||
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
10 | .="- .-=="i, .._ License as published by the Free Software | ||
11 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
12 | ._= =} : or (at your option) any later version. | ||
13 | .%`+i> _;_. | ||
14 | .i_,=:_. -<s. This program is distributed in the hope that | ||
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
16 | : .. .:, . . . without even the implied warranty of | ||
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
19 | ..}^=.= = ; Library General Public License for more | ||
20 | ++= -. .` .: details. | ||
21 | : = ...= . :.=- | ||
22 | -. .:....=;==+<; You should have received a copy of the GNU | ||
23 | -_. . . )=. = Library General Public License along with | ||
24 | -- :-=` this library; see the file COPYING.LIB. | ||
25 | If not, write to the Free Software Foundation, | ||
26 | Inc., 59 Temple Place - Suite 330, | ||
27 | Boston, MA 02111-1307, USA. | ||
28 | */ | ||
29 | |||
30 | #include "opimoccurrence.h" | ||
31 | #include <opie2/opimtemplatebase.h> | ||
32 | #include <opie2/private/opimoccurrence_p.h> | ||
33 | |||
34 | |||
35 | /* QT */ | ||
36 | #include <qshared.h> | ||
37 | |||
38 | |||
39 | namespace Opie { | ||
40 | |||
41 | OPimOccurrence::OPimOccurrence( OPimOccurrence::Data* _data, | ||
42 | enum OPimOccurrence::Position pos ) | ||
43 | : m_pos( pos ), data( _data ) | ||
44 | {} | ||
45 | |||
46 | /** | ||
47 | * \brief Copy constructor | ||
48 | */ | ||
49 | OPimOccurrence::OPimOccurrence( const OPimOccurrence& oc ) | ||
50 | : data( oc.data ) | ||
51 | { | ||
52 | /* | ||
53 | * Increment the reference count | ||
54 | */ | ||
55 | data->ref(); | ||
56 | |||
57 | /* | ||
58 | * copy the other information | ||
59 | */ | ||
60 | m_start = oc.m_start; | ||
61 | m_end = oc.m_end; | ||
62 | m_occurrence = oc.m_occurrence; | ||
63 | m_isAllDay = oc.m_isAllDay; | ||
64 | m_pos = oc.m_pos; | ||
65 | } | ||
66 | |||
67 | OPimOccurrence::OPimOccurrence() | ||
68 | : m_isAllDay( false ), m_pos( StartEnd ) | ||
69 | { | ||
70 | /* simple convient c'tor */ | ||
71 | data = new OPimOccurrence::Data(); | ||
72 | } | ||
73 | |||
74 | OPimOccurrence::~OPimOccurrence() { | ||
75 | deref(); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * \brief Copy Operator | ||
80 | */ | ||
81 | OPimOccurrence& OPimOccurrence::operator=( const OPimOccurrence& oc ) { | ||
82 | /* guard against self assignment */ | ||
83 | if ( this == &oc ) return *this; | ||
84 | |||
85 | oc.data->ref(); | ||
86 | deref(); | ||
87 | data = oc.data; | ||
88 | |||
89 | |||
90 | /* | ||
91 | * copy the other information | ||
92 | */ | ||
93 | m_start = oc.m_start; | ||
94 | m_end = oc.m_end; | ||
95 | m_occurrence = oc.m_occurrence; | ||
96 | m_isAllDay = oc.m_isAllDay; | ||
97 | m_pos = oc.m_pos; | ||
98 | |||
99 | return *this; | ||
100 | } | ||
101 | |||
102 | |||
103 | /** | ||
104 | * @internal | ||
105 | */ | ||
106 | void OPimOccurrence::deref() { | ||
107 | if ( data->deref() ) { | ||
108 | delete data; | ||
109 | data = 0; | ||
110 | } | ||
111 | } | ||
112 | |||
113 | /** | ||
114 | * \brief Set the Occurrence to be All Day on a specified QDate | ||
115 | * | ||
116 | * If no QTime is associated to a OPimOccurrence use this Method | ||
117 | * to set the Period of this Occurrence. When using this Method | ||
118 | * later calls to \sa isAllDay() will return true. | ||
119 | * The Occurrence will be set to occurr on \par from. | ||
120 | * | ||
121 | * @param from The Day this OPimOccurrence occurs | ||
122 | * | ||
123 | */ | ||
124 | void OPimOccurrence::setPeriod( const QDate& from ) { | ||
125 | m_occurrence = from; | ||
126 | m_start = m_end = QTime(); // assign invalid value just in case | ||
127 | m_isAllDay = true; | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * \brief Set the period of this Occurrence with a QTime associated (overloaded) | ||
132 | * | ||
133 | * Set the period of time for this Occurrence. Each Ocurrence is limited | ||
134 | * to one day. Using this Method will make \sa isAllDay() return false. | ||
135 | * If \par from and \par to are on two different days the QDate of the | ||
136 | * \par from QDateTime will be used. | ||
137 | * | ||
138 | * @param from The Start Date Time of the Occurrence | ||
139 | * @param to The End Date Time of the Occurrence | ||
140 | */ | ||
141 | void OPimOccurrence::setPeriod( const QDateTime& from, const QDateTime& to ) { | ||
142 | m_occurrence = from.date(); | ||
143 | m_start = from.time(); | ||
144 | m_end = to.time(); | ||
145 | m_isAllDay = false; | ||
146 | } | ||
147 | |||
148 | /** | ||
149 | * \brief Set the period of this Occurrence with a QTime associated | ||
150 | * | ||
151 | * @param from The QDate of the Occurrence | ||
152 | * @param start The Start QTime of the Occurrence | ||
153 | * @param end The End QTime of the Occurrence | ||
154 | */ | ||
155 | void OPimOccurrence::setPeriod( const QDate& from, const QTime& start, | ||
156 | const QTime& end ) { | ||
157 | m_occurrence = from; | ||
158 | m_start = start; | ||
159 | m_end = end; | ||
160 | m_isAllDay = false; | ||
161 | } | ||
162 | |||
163 | |||
164 | /** | ||
165 | * \brief Is a QTime associated to the OPimOccurrence | ||
166 | * | ||
167 | * @return Return true if no QTime is associated | ||
168 | */ | ||
169 | bool OPimOccurrence::isAllDay()const { | ||
170 | return m_isAllDay; | ||
171 | } | ||
172 | |||
173 | |||
174 | /** | ||
175 | * \brief Return the QDate where this OPimOccurrence takes place | ||
176 | * @return the QDate where this OPimOccurrence occurrs. | ||
177 | */ | ||
178 | QDate OPimOccurrence::date()const { | ||
179 | return m_occurrence; | ||
180 | } | ||
181 | |||
182 | |||
183 | /** | ||
184 | * \brief Return the start time of the OPimOccurrence | ||
185 | * | ||
186 | * @return Return the Start Time of the OPimOccurrence. It is | ||
187 | * invalid if \sa isAllDay() returns true. | ||
188 | */ | ||
189 | QTime OPimOccurrence::startTime()const { | ||
190 | return m_start; | ||
191 | } | ||
192 | |||
193 | QTime OPimOccurrence::endTime()const { | ||
194 | return m_end; | ||
195 | } | ||
196 | |||
197 | QDateTime OPimOccurrence::startDateTime()const { | ||
198 | return QDateTime( m_occurrence, m_start ); | ||
199 | } | ||
200 | |||
201 | QDateTime OPimOccurrence::endDateTime()const { | ||
202 | return QDateTime( m_occurrence, m_end ); | ||
203 | } | ||
204 | |||
205 | |||
206 | QString OPimOccurrence::summary()const { | ||
207 | return data->summary; | ||
208 | } | ||
209 | |||
210 | QString OPimOccurrence::location()const { | ||
211 | return data->location; | ||
212 | } | ||
213 | |||
214 | QString OPimOccurrence::note()const { | ||
215 | return data->note; | ||
216 | } | ||
217 | |||
218 | |||
219 | /** | ||
220 | * -1 if no time is associated | ||
221 | * otherwise the length of the occurrence in hours | ||
222 | */ | ||
223 | int OPimOccurrence::length()const { | ||
224 | if ( m_isAllDay ) | ||
225 | return -1; | ||
226 | else | ||
227 | return ( m_end.hour() * 60 - m_start.hour() * 60 ) | ||
228 | + QABS( m_start.minute() - m_end.minute() ); | ||
229 | } | ||
230 | |||
231 | enum OPimOccurrence::Position OPimOccurrence::position()const { | ||
232 | return m_pos; | ||
233 | } | ||
234 | |||
235 | void OPimOccurrence::setPosition( enum OPimOccurrence::Position& pos ) { | ||
236 | m_pos = pos; | ||
237 | } | ||
238 | |||
239 | |||
240 | Opie::Core::OSharedPointer<OPimRecord> OPimOccurrence::record()const { | ||
241 | if ( !data->record && data->backend ) | ||
242 | data->record = data->backend->record( data->uid ); | ||
243 | return data->record; | ||
244 | } | ||
245 | |||
246 | template<class Record> Record OPimOccurrence::internalToRecord()const { | ||
247 | Record target; | ||
248 | |||
249 | /* If it is not loaded, try to load it using OPimBase */ | ||
250 | if ( !data->record && data->backend ) | ||
251 | data->record = data->backend->record( data->uid ); | ||
252 | |||
253 | Record *ta = Record::safeCast( data->record ); | ||
254 | if ( ta ) | ||
255 | target = *ta; | ||
256 | |||
257 | |||
258 | return target; | ||
259 | } | ||
260 | |||
261 | OPimEvent OPimOccurrence::toEvent()const { | ||
262 | return internalToRecord<OPimEvent>(); | ||
263 | } | ||
264 | |||
265 | OPimTodo OPimOccurrence::toTodo()const { | ||
266 | return internalToRecord<OPimTodo>(); | ||
267 | } | ||
268 | |||
269 | OPimContact OPimOccurrence::toContact()const { | ||
270 | return internalToRecord<OPimContact>(); | ||
271 | } | ||
272 | |||
273 | bool OPimOccurrence::operator<( const OPimOccurrence& oc )const { | ||
274 | if ( m_occurrence < oc.m_occurrence ) | ||
275 | return true; | ||
276 | if ( m_occurrence == oc.m_occurrence ) | ||
277 | return m_start < oc.m_start; | ||
278 | else | ||
279 | return false; | ||
280 | } | ||
281 | |||
282 | bool OPimOccurrence::operator<=( const OPimOccurrence& oc )const { | ||
283 | return ( m_occurrence <= oc.m_occurrence ); | ||
284 | } | ||
285 | |||
286 | bool OPimOccurrence::operator==( const OPimOccurrence& oc )const { | ||
287 | if ( data->uid != oc.data->uid ) | ||
288 | return false; | ||
289 | if ( m_occurrence != oc.m_occurrence ) | ||
290 | return false; | ||
291 | if ( m_isAllDay != oc.m_isAllDay ) | ||
292 | return false; | ||
293 | if ( m_isAllDay && oc.m_isAllDay ) | ||
294 | if ( m_start != oc.m_start || | ||
295 | m_end != oc.m_end ) | ||
296 | return false; | ||
297 | if ( data->summary != oc.data->summary ) | ||
298 | return false; | ||
299 | if ( data->note != oc.data->note ) | ||
300 | return false; | ||
301 | if ( data->location != oc.data->location ) | ||
302 | return false; | ||
303 | |||
304 | return true; | ||
305 | } | ||
306 | |||
307 | bool OPimOccurrence::operator!=( const OPimOccurrence& oc )const { | ||
308 | return !( *this == oc ); | ||
309 | } | ||
310 | |||
311 | bool OPimOccurrence::operator>( const OPimOccurrence& oc )const { | ||
312 | return !( *this <= oc ); | ||
313 | } | ||
314 | |||
315 | bool OPimOccurrence::operator>=( const OPimOccurrence& oc )const { | ||
316 | return !( *this < oc ); | ||
317 | } | ||
318 | |||
319 | } | ||