1 | /* |
2 | * repetition.cpp - represents a sub-repetition: interval and count |
3 | * This file is part of kalarmcal library, which provides access to KAlarm |
4 | * calendar data. |
5 | * Copyright © 2009-2012 by David Jarvie <djarvie@kde.org> |
6 | * |
7 | * This library is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU Library General Public License as published |
9 | * by the Free Software Foundation; either version 2 of the License, or (at |
10 | * your option) any later version. |
11 | * |
12 | * This library is distributed in the hope that it will be useful, but WITHOUT |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
15 | * License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Library General Public License |
18 | * along with this library; see the file COPYING.LIB. If not, write to the |
19 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
20 | * MA 02110-1301, USA. |
21 | */ |
22 | |
23 | #include "repetition.h" |
24 | #include <kdatetime.h> |
25 | |
26 | #ifndef KALARMCAL_USE_KRESOURCES |
27 | using namespace KCalCore; |
28 | #else |
29 | using namespace KCal; |
30 | #endif |
31 | |
32 | namespace KAlarmCal |
33 | { |
34 | |
35 | class Repetition::Private |
36 | { |
37 | public: |
38 | Private() : mInterval(0), mCount(0) {} |
39 | Private(const Duration& interval, int count) |
40 | : mInterval(interval), mCount(count) |
41 | { |
42 | if ((!count && interval) || (count && !interval)) |
43 | { |
44 | mCount = 0; |
45 | mInterval = 0; |
46 | } |
47 | } |
48 | |
49 | Duration mInterval; // sub-repetition interval |
50 | int mCount; // sub-repetition count (excluding the first time) |
51 | }; |
52 | |
53 | Repetition::Repetition() |
54 | : d(new Private) |
55 | { |
56 | } |
57 | |
58 | Repetition::Repetition(const Duration& interval, int count) |
59 | : d(new Private(interval, count)) |
60 | { |
61 | } |
62 | |
63 | Repetition::Repetition(const Repetition& other) |
64 | : d(new Private(*other.d)) |
65 | { |
66 | } |
67 | |
68 | Repetition::~Repetition() |
69 | { |
70 | delete d; |
71 | } |
72 | |
73 | Repetition& Repetition::operator=(const Repetition& other) |
74 | { |
75 | if (&other != this) |
76 | *d = *other.d; |
77 | return *this; |
78 | } |
79 | |
80 | void Repetition::set(const Duration& interval, int count) |
81 | { |
82 | if (!count || !interval) |
83 | { |
84 | d->mCount = 0; |
85 | d->mInterval = 0; |
86 | } |
87 | else |
88 | { |
89 | d->mCount = count; |
90 | d->mInterval = interval; |
91 | } |
92 | } |
93 | |
94 | void Repetition::set(const Duration& interval) |
95 | { |
96 | if (d->mCount) |
97 | { |
98 | d->mInterval = interval; |
99 | if (!interval) |
100 | d->mCount = 0; |
101 | } |
102 | } |
103 | |
104 | Repetition::operator bool() const |
105 | { |
106 | return d->mCount; |
107 | } |
108 | |
109 | bool Repetition::operator==(const Repetition& r) const |
110 | { |
111 | return d->mInterval == r.d->mInterval && d->mCount == r.d->mCount; |
112 | } |
113 | |
114 | int Repetition::count() const |
115 | { |
116 | return d->mCount; |
117 | } |
118 | |
119 | Duration Repetition::interval() const |
120 | { |
121 | return d->mInterval; |
122 | } |
123 | |
124 | Duration Repetition::duration() const |
125 | { |
126 | return d->mInterval * d->mCount; |
127 | } |
128 | |
129 | Duration Repetition::duration(int count) const |
130 | { |
131 | return d->mInterval * count; |
132 | } |
133 | |
134 | bool Repetition::isDaily() const |
135 | { |
136 | return d->mInterval.isDaily(); |
137 | } |
138 | |
139 | int Repetition::intervalDays() const |
140 | { |
141 | return d->mInterval.asDays(); |
142 | } |
143 | |
144 | int Repetition::intervalMinutes() const |
145 | { |
146 | return d->mInterval.asSeconds() / 60; |
147 | } |
148 | |
149 | int Repetition::intervalSeconds() const |
150 | { |
151 | return d->mInterval.asSeconds(); |
152 | } |
153 | |
154 | int Repetition::nextRepeatCount(const KDateTime& from, const KDateTime& preDateTime) const |
155 | { |
156 | return d->mInterval.isDaily() |
157 | ? from.daysTo(preDateTime) / d->mInterval.asDays() + 1 |
158 | : static_cast<int>(from.secsTo_long(preDateTime) / d->mInterval.asSeconds()) + 1; |
159 | } |
160 | |
161 | int Repetition::previousRepeatCount(const KDateTime& from, const KDateTime& afterDateTime) const |
162 | { |
163 | return d->mInterval.isDaily() |
164 | ? from.daysTo(afterDateTime.addSecs(-1)) / d->mInterval.asDays() |
165 | : static_cast<int>((from.secsTo_long(afterDateTime) - 1) / d->mInterval.asSeconds()); |
166 | } |
167 | |
168 | } // namespace KAlarmCal |
169 | |
170 | // vim: et sw=4: |
171 | |