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
27using namespace KCalCore;
28#else
29using namespace KCal;
30#endif
31
32namespace KAlarmCal
33{
34
35class 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
53Repetition::Repetition()
54 : d(new Private)
55{
56}
57
58Repetition::Repetition(const Duration& interval, int count)
59 : d(new Private(interval, count))
60{
61}
62
63Repetition::Repetition(const Repetition& other)
64 : d(new Private(*other.d))
65{
66}
67
68Repetition::~Repetition()
69{
70 delete d;
71}
72
73Repetition& Repetition::operator=(const Repetition& other)
74{
75 if (&other != this)
76 *d = *other.d;
77 return *this;
78}
79
80void 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
94void 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
104Repetition::operator bool() const
105{
106 return d->mCount;
107}
108
109bool Repetition::operator==(const Repetition& r) const
110{
111 return d->mInterval == r.d->mInterval && d->mCount == r.d->mCount;
112}
113
114int Repetition::count() const
115{
116 return d->mCount;
117}
118
119Duration Repetition::interval() const
120{
121 return d->mInterval;
122}
123
124Duration Repetition::duration() const
125{
126 return d->mInterval * d->mCount;
127}
128
129Duration Repetition::duration(int count) const
130{
131 return d->mInterval * count;
132}
133
134bool Repetition::isDaily() const
135{
136 return d->mInterval.isDaily();
137}
138
139int Repetition::intervalDays() const
140{
141 return d->mInterval.asDays();
142}
143
144int Repetition::intervalMinutes() const
145{
146 return d->mInterval.asSeconds() / 60;
147}
148
149int Repetition::intervalSeconds() const
150{
151 return d->mInterval.asSeconds();
152}
153
154int 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
161int 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