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-datetime
23  * @short_description: An ECalComponentDateTime structure
24  * @include: libecal/libecal.h
25  *
26  * Contains functions to work with the #ECalComponentDateTime structure.
27  **/
28 
29 #include "e-cal-component-datetime.h"
30 
31 G_DEFINE_BOXED_TYPE (ECalComponentDateTime, e_cal_component_datetime, e_cal_component_datetime_copy, e_cal_component_datetime_free)
32 
33 struct _ECalComponentDateTime {
34 	/* Actual date/time value */
35 	ICalTime *value;
36 
37 	/* Timezone ID */
38 	gchar *tzid;
39 };
40 
41 /**
42  * e_cal_component_datetime_new:
43  * @value: (not nullable): an #ICalTime as a value
44  * @tzid: (nullable): timezone ID for the @value, or %NULL
45  *
46  * Creates a new #ECalComponentDateTime instance, which holds
47  * the @value and @tzid. The returned structure should be freed
48  * with e_cal_component_datetime_free(), when no longer needed.
49  *
50  * Returns: (transfer full): a new #ECalComponentDateTime
51  *
52  * Since: 3.34
53  **/
54 ECalComponentDateTime *
e_cal_component_datetime_new(const ICalTime * value,const gchar * tzid)55 e_cal_component_datetime_new (const ICalTime *value,
56 			      const gchar *tzid)
57 {
58 	ECalComponentDateTime *dt;
59 
60 	g_return_val_if_fail (I_CAL_IS_TIME ((ICalTime *) value), NULL);
61 
62 	dt = g_slice_new0 (ECalComponentDateTime);
63 	e_cal_component_datetime_set (dt, value, tzid);
64 
65 	return dt;
66 }
67 
68 /**
69  * e_cal_component_datetime_new_take:
70  * @value: (transfer full) (not nullable): an #ICalTime as a value
71  * @tzid: (transfer full) (nullable): timezone ID for the @value, or %NULL
72  *
73  * Creates a new #ECalComponentDateTime instance, which holds
74  * the @value and @tzid. It is similar to e_cal_component_datetime_new(),
75  * except this function assumes ownership of the @value and @tzid.
76  * The returned structure should be freed with e_cal_component_datetime_free(),
77  * when no longer needed.
78  *
79  * Returns: (transfer full): a new #ECalComponentDateTime
80  *
81  * Since: 3.34
82  **/
83 ECalComponentDateTime *
e_cal_component_datetime_new_take(ICalTime * value,gchar * tzid)84 e_cal_component_datetime_new_take (ICalTime *value,
85 				   gchar *tzid)
86 {
87 	ECalComponentDateTime *dt;
88 
89 	g_return_val_if_fail (I_CAL_IS_TIME (value), NULL);
90 
91 	dt = g_slice_new0 (ECalComponentDateTime);
92 	dt->value = value;
93 	dt->tzid = tzid;
94 
95 	return dt;
96 }
97 
98 /**
99  * e_cal_component_datetime_copy:
100  * @dt: (not nullable): an #ECalComponentDateTime
101  *
102  * Creates a new copy of @dt. The returned structure should be freed
103  * with e_cal_component_datetime_free() when no longer needed.
104  *
105  * Returns: (transfer full): a new #ECalComponentDateTime, copy of @dt
106  *
107  * Since: 3.34
108  **/
109 ECalComponentDateTime *
e_cal_component_datetime_copy(const ECalComponentDateTime * dt)110 e_cal_component_datetime_copy (const ECalComponentDateTime *dt)
111 {
112 	g_return_val_if_fail (dt != NULL, NULL);
113 
114 	return e_cal_component_datetime_new (
115 		e_cal_component_datetime_get_value (dt),
116 		e_cal_component_datetime_get_tzid (dt));
117 }
118 
119 /**
120  * e_cal_component_datetime_free: (skip)
121  * @dt: (type ECalComponentDateTime) (nullable): an #ECalComponentDateTime to free
122  *
123  * Free @dt, previously created by e_cal_component_datetime_new(),
124  * e_cal_component_datetime_new_take() or e_cal_component_datetime_copy().
125  * The function does nothing, if @dt is %NULL.
126  *
127  * Since: 3.34
128  **/
129 void
e_cal_component_datetime_free(gpointer dt)130 e_cal_component_datetime_free (gpointer dt)
131 {
132 	ECalComponentDateTime *pdt = dt;
133 
134 	if (pdt) {
135 		g_clear_object (&pdt->value);
136 		g_free (pdt->tzid);
137 		g_slice_free (ECalComponentDateTime, pdt);
138 	}
139 }
140 
141 /**
142  * e_cal_component_datetime_set:
143  * @dt: an #ECalComponentDateTime
144  * @value: (not nullable): an #ICalTime as a value
145  * @tzid: (nullable): timezone ID for the @value, or %NULL
146  *
147  * Sets both @value and @tzid in one call. Use e_cal_component_datetime_set_value()
148  * and e_cal_component_datetime_set_tzid() to set them separately.
149  *
150  * Since: 3.34
151  **/
152 void
e_cal_component_datetime_set(ECalComponentDateTime * dt,const ICalTime * value,const gchar * tzid)153 e_cal_component_datetime_set (ECalComponentDateTime *dt,
154 			      const ICalTime *value,
155 			      const gchar *tzid)
156 {
157 	g_return_if_fail (dt != NULL);
158 	g_return_if_fail (I_CAL_IS_TIME ((ICalTime *) value));
159 
160 	e_cal_component_datetime_set_value (dt, value);
161 	e_cal_component_datetime_set_tzid (dt, tzid);
162 }
163 
164 /**
165  * e_cal_component_datetime_get_value:
166  * @dt: an #ECalComponentDateTime
167  *
168  * Returns the value stored with the @dt. The object is owned by @dt and
169  * it's valid until the @dt is freed or its value overwritten.
170  *
171  * Returns: (transfer none): a value of @dt, as an #ICalTime
172  *
173  * Since: 3.34
174  **/
175 ICalTime *
e_cal_component_datetime_get_value(const ECalComponentDateTime * dt)176 e_cal_component_datetime_get_value (const ECalComponentDateTime *dt)
177 {
178 	g_return_val_if_fail (dt != NULL, NULL);
179 
180 	return dt->value;
181 }
182 
183 /**
184  * e_cal_component_datetime_set_value:
185  * @dt: an #ECalComponentDateTime
186  * @value: (not nullable): the value to set, as an #ICalTime
187  *
188  * Sets the @value of the @dt. Any previously set value is freed.
189  *
190  * Since: 3.34
191  **/
192 void
e_cal_component_datetime_set_value(ECalComponentDateTime * dt,const ICalTime * value)193 e_cal_component_datetime_set_value (ECalComponentDateTime *dt,
194 				    const ICalTime *value)
195 {
196 	g_return_if_fail (dt != NULL);
197 	g_return_if_fail (I_CAL_IS_TIME ((ICalTime *) value));
198 
199 	if (dt->value != value) {
200 		g_clear_object (&dt->value);
201 		dt->value = i_cal_time_clone (value);
202 	}
203 }
204 
205 /**
206  * e_cal_component_datetime_take_value:
207  * @dt: an #ECalComponentDateTime
208  * @value: (not nullable) (transfer full): the value to take, as an #ICalTime
209  *
210  * Sets the @value of the @dt and assumes ownership of the @value.
211  * Any previously set value is freed.
212  *
213  * Since: 3.34
214  **/
215 void
e_cal_component_datetime_take_value(ECalComponentDateTime * dt,ICalTime * value)216 e_cal_component_datetime_take_value (ECalComponentDateTime *dt,
217 				     ICalTime *value)
218 {
219 	g_return_if_fail (dt != NULL);
220 	g_return_if_fail (I_CAL_IS_TIME (value));
221 
222 	if (dt->value != value) {
223 		g_clear_object (&dt->value);
224 		dt->value = value;
225 	}
226 }
227 
228 /**
229  * e_cal_component_datetime_get_tzid:
230  * @dt: an #ECalComponentDateTime
231  *
232  * Returns the TZID stored with the @dt. The string is owned by @dt and
233  * it's valid until the @dt is freed or its TZID overwritten. It never
234  * returns an empty string, it returns either set TZID parameter value
235  * or %NULL, when none is set.
236  *
237  * Returns: (transfer none) (nullable): a TZID of @dt, or %NULL
238  *
239  * Since: 3.34
240  **/
241 const gchar *
e_cal_component_datetime_get_tzid(const ECalComponentDateTime * dt)242 e_cal_component_datetime_get_tzid (const ECalComponentDateTime *dt)
243 {
244 	g_return_val_if_fail (dt != NULL, NULL);
245 
246 	return dt->tzid;
247 }
248 
249 /**
250  * e_cal_component_datetime_set_tzid:
251  * @dt: an #ECalComponentDateTime
252  * @tzid: (nullable): the TZID to set, or %NULL
253  *
254  * Sets the @tzid of the @dt. Any previously set TZID is freed.
255  * An empty string or a %NULL as @tzid is treated as none TZID.
256  *
257  * Since: 3.34
258  **/
259 void
e_cal_component_datetime_set_tzid(ECalComponentDateTime * dt,const gchar * tzid)260 e_cal_component_datetime_set_tzid (ECalComponentDateTime *dt,
261 				   const gchar *tzid)
262 {
263 	g_return_if_fail (dt != NULL);
264 
265 	if (tzid && !*tzid)
266 		tzid = NULL;
267 
268 	if (tzid != dt->tzid) {
269 		g_free (dt->tzid);
270 		dt->tzid = g_strdup (tzid);
271 	}
272 }
273 
274 /**
275  * e_cal_component_datetime_take_tzid:
276  * @dt: an #ECalComponentDateTime
277  * @tzid: (nullable) (transfer full): the TZID to take, or %NULL
278  *
279  * Sets the @tzid of the @dt and assumes ownership of @tzid. Any previously
280  * set TZID is freed. An empty string or a %NULL as @tzid is treated as none TZID.
281  *
282  * Since: 3.34
283  **/
284 void
e_cal_component_datetime_take_tzid(ECalComponentDateTime * dt,gchar * tzid)285 e_cal_component_datetime_take_tzid (ECalComponentDateTime *dt,
286 				    gchar *tzid)
287 {
288 	g_return_if_fail (dt != NULL);
289 
290 	if (tzid && !*tzid) {
291 		g_free (tzid);
292 		tzid = NULL;
293 	}
294 
295 	if (tzid != dt->tzid) {
296 		g_free (dt->tzid);
297 		dt->tzid = tzid;
298 	}
299 }
300