1 /*
2  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
3  * Copyright (C) 2019 Red Hat, Inc. (www.redhat.com)
4  *
5  * This library is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library. If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #include "evolution-data-server-config.h"
20 
21 /**
22  * SECTION:e-cal-component-alarm-repeat
23  * @short_description: An ECalComponentAlarmRepeat structure
24  * @include: libecal/libecal.h
25  *
26  * Contains functions to work with the #ECalComponentAlarmRepeat structure.
27  **/
28 
29 #include "e-cal-component-alarm-repeat.h"
30 
31 G_DEFINE_BOXED_TYPE (ECalComponentAlarmRepeat, e_cal_component_alarm_repeat, e_cal_component_alarm_repeat_copy, e_cal_component_alarm_repeat_free)
32 
33 struct _ECalComponentAlarmRepeat {
34 	gint repetitions;
35 	ICalDuration *interval;
36 };
37 
38 /**
39  * e_cal_component_alarm_repeat_new:
40  * @repetitions: number of extra repetitions, zero for none
41  * @interval: (not nullable): interval between repetitions
42  *
43  * Creates a new #ECalComponentAlarmRepeat describing alarm repetitions.
44  * The returned structure should be freed with e_cal_component_alarm_repeat_free(),
45  * when no longer needed.
46  *
47  * Returns: (transfer full): a newly allocated #ECalComponentAlarmRepeat
48  *
49  * Since: 3.34
50  **/
51 ECalComponentAlarmRepeat *
e_cal_component_alarm_repeat_new(gint repetitions,const ICalDuration * interval)52 e_cal_component_alarm_repeat_new (gint repetitions,
53 				  const ICalDuration *interval)
54 {
55 	g_return_val_if_fail (I_CAL_IS_DURATION ((ICalDuration *) interval), NULL);
56 
57 	return e_cal_component_alarm_repeat_new_seconds (repetitions,
58 		i_cal_duration_as_int ((ICalDuration *) interval));
59 }
60 
61 /**
62  * e_cal_component_alarm_repeat_new_seconds:
63  * @repetitions: number of extra repetitions, zero for none
64  * @interval_seconds: interval between repetitions, in seconds
65  *
66  * Creates a new #ECalComponentAlarmRepeat describing alarm repetitions.
67  * The returned structure should be freed with e_cal_component_alarm_repeat_free(),
68  * when no longer needed.
69  *
70  * Returns: (transfer full): a newly allocated #ECalComponentAlarmRepeat
71  *
72  * Since: 3.34
73  **/
74 ECalComponentAlarmRepeat *
e_cal_component_alarm_repeat_new_seconds(gint repetitions,gint interval_seconds)75 e_cal_component_alarm_repeat_new_seconds (gint repetitions,
76 					  gint interval_seconds)
77 {
78 	ECalComponentAlarmRepeat *repeat;
79 
80 	repeat = g_slice_new0 (ECalComponentAlarmRepeat);
81 	repeat->repetitions = repetitions;
82 	repeat->interval = i_cal_duration_new_from_int (interval_seconds);
83 
84 	return repeat;
85 }
86 
87 /**
88  * e_cal_component_alarm_repeat_copy:
89  * @repeat: (not nullable): an #ECalComponentAlarmRepeat to copy
90  *
91  * Returns: (transfer full): a newly allocated #ECalComponentAlarmRepeat, copy of @repeat.
92  *    The returned structure should be freed with e_cal_component_alarm_repeat_free(),
93  *    when no longer needed.
94  *
95  * Since: 3.34
96  **/
97 ECalComponentAlarmRepeat *
e_cal_component_alarm_repeat_copy(const ECalComponentAlarmRepeat * repeat)98 e_cal_component_alarm_repeat_copy (const ECalComponentAlarmRepeat *repeat)
99 {
100 	g_return_val_if_fail (repeat != NULL, NULL);
101 
102 	return e_cal_component_alarm_repeat_new_seconds (repeat->repetitions,
103 		i_cal_duration_as_int (repeat->interval));
104 }
105 
106 /**
107  * e_cal_component_alarm_repeat_free: (skip)
108  * @repeat: (type ECalComponentAlarmRepeat) (nullable): an #ECalComponentAlarmRepeat to free
109  *
110  * Free the @repeat, previously allocated by e_cal_component_alarm_repeat_new(),
111  * e_cal_component_alarm_repeat_new_seconds() or e_cal_component_alarm_repeat_copy().
112  *
113  * Since: 3.34
114  **/
115 void
e_cal_component_alarm_repeat_free(gpointer repeat)116 e_cal_component_alarm_repeat_free (gpointer repeat)
117 {
118 	ECalComponentAlarmRepeat *rpt = repeat;
119 
120 	if (rpt) {
121 		g_clear_object (&rpt->interval);
122 		g_slice_free (ECalComponentAlarmRepeat, rpt);
123 	}
124 }
125 
126 /**
127  * e_cal_component_alarm_repeat_get_repetitions:
128  * @repeat: an #ECalComponentAlarmRepeat
129  *
130  * Returns: the repetitions count of the @repeat
131  *
132  * Since: 3.34
133  **/
134 gint
e_cal_component_alarm_repeat_get_repetitions(const ECalComponentAlarmRepeat * repeat)135 e_cal_component_alarm_repeat_get_repetitions (const ECalComponentAlarmRepeat *repeat)
136 {
137 	g_return_val_if_fail (repeat != NULL, 0);
138 
139 	return repeat->repetitions;
140 }
141 
142 /**
143  * e_cal_component_alarm_repeat_set_repetitions:
144  * @repeat: an #ECalComponentAlarmRepeat
145  * @repetitions: number of repetitions, zero for none
146  *
147  * Set the @repetitions count of the @repeat.
148  *
149  * Since: 3.34
150  **/
151 void
e_cal_component_alarm_repeat_set_repetitions(ECalComponentAlarmRepeat * repeat,gint repetitions)152 e_cal_component_alarm_repeat_set_repetitions (ECalComponentAlarmRepeat *repeat,
153 					      gint repetitions)
154 {
155 	g_return_if_fail (repeat != NULL);
156 
157 	if (repeat->repetitions != repetitions) {
158 		repeat->repetitions = repetitions;
159 	}
160 }
161 
162 /**
163  * e_cal_component_alarm_repeat_get_interval:
164  * @repeat: an #ECalComponentAlarmRepeat
165  *
166  * Returns the interval between repetitions of the @repeat, as an #ICalDuration
167  * object. This object is owned by @repeat and should not be freed. It's valid until
168  * the @repeat is not freed or its interval changed with either e_cal_component_alarm_repeat_set_interval()
169  * or e_cal_component_alarm_repeat_set_interval_seconds().
170  *
171  * Returns: (transfer none): the interval between repetitions of the @repeat
172  *
173  * Since: 3.34
174  **/
175 ICalDuration *
e_cal_component_alarm_repeat_get_interval(const ECalComponentAlarmRepeat * repeat)176 e_cal_component_alarm_repeat_get_interval (const ECalComponentAlarmRepeat *repeat)
177 {
178 	g_return_val_if_fail (repeat != NULL, NULL);
179 
180 	return repeat->interval;
181 }
182 
183 /**
184  * e_cal_component_alarm_repeat_set_interval:
185  * @repeat: an #ECalComponentAlarmRepeat
186  * @interval: (not nullable): interval between repetitions, as an #ICalDuration
187  *
188  * Set the @interval between repetitions of the @repeat.
189  *
190  * Since: 3.34
191  **/
192 void
e_cal_component_alarm_repeat_set_interval(ECalComponentAlarmRepeat * repeat,const ICalDuration * interval)193 e_cal_component_alarm_repeat_set_interval (ECalComponentAlarmRepeat *repeat,
194 					   const ICalDuration *interval)
195 {
196 	g_return_if_fail (repeat != NULL);
197 	g_return_if_fail (interval != NULL);
198 
199 	if (repeat->interval != interval) {
200 		e_cal_component_alarm_repeat_set_interval_seconds (repeat,
201 			i_cal_duration_as_int ((ICalDuration *) interval));
202 	}
203 }
204 
205 /**
206  * e_cal_component_alarm_repeat_get_interval_seconds:
207  * @repeat: an #ECalComponentAlarmRepeat
208  *
209  * Returns the interval between repetitions of the @repeat in seconds.
210  *
211  * Returns: the interval between repetitions of the @repeat
212  *
213  * Since: 3.34
214  **/
215 gint
e_cal_component_alarm_repeat_get_interval_seconds(const ECalComponentAlarmRepeat * repeat)216 e_cal_component_alarm_repeat_get_interval_seconds (const ECalComponentAlarmRepeat *repeat)
217 {
218 	g_return_val_if_fail (repeat != NULL, 0);
219 
220 	return i_cal_duration_as_int (repeat->interval);
221 }
222 
223 /**
224  * e_cal_component_alarm_repeat_set_interval_seconds:
225  * @repeat: an #ECalComponentAlarmRepeat
226  * @interval_seconds: interval between repetitions, in seconds
227  *
228  * Set the @interval_seconds between repetitions of the @repeat.
229  *
230  * Since: 3.34
231  **/
232 void
e_cal_component_alarm_repeat_set_interval_seconds(ECalComponentAlarmRepeat * repeat,gint interval_seconds)233 e_cal_component_alarm_repeat_set_interval_seconds (ECalComponentAlarmRepeat *repeat,
234 						   gint interval_seconds)
235 {
236 	g_return_if_fail (repeat != NULL);
237 
238 	if (i_cal_duration_as_int (repeat->interval) != interval_seconds) {
239 		g_clear_object (&repeat->interval);
240 		repeat->interval = i_cal_duration_new_from_int (interval_seconds);
241 	}
242 }
243