No issues found
1 /*
2 * Evolution calendar - Widget utilities
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
16 *
17 *
18 * Authors:
19 * Federico Mena-Quintero <federico@ximian.com>
20 *
21 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
22 *
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <ctype.h>
30 #include <string.h>
31 #include <libical/ical.h>
32 #include <glib/gi18n.h>
33
34 #include "widgets/misc/e-dateedit.h"
35 #include "../itip-utils.h"
36 #include <shell/e-shell.h>
37 #include "comp-editor-util.h"
38
39 /**
40 * comp_editor_dates:
41 * @dates: A structure to be filled out with dates of a component
42 * @comp: The component to extract the dates from
43 *
44 * Extracts the dates from the calendar component into the
45 * CompEditorPageDates structure. Call comp_editor_free_dates() to free the
46 * results.
47 **/
48 void
49 comp_editor_dates (CompEditorPageDates *dates,
50 ECalComponent *comp)
51 {
52 ECalComponentDateTime dt;
53
54 dates->start = NULL;
55 dates->end = NULL;
56 dates->due = NULL;
57 dates->complete = NULL;
58
59 /* Note that the ECalComponentDateTime's returned contain allocated
60 * icaltimetype and tzid values, so we just take over ownership of
61 * those. */
62 e_cal_component_get_dtstart (comp, &dt);
63 if (dt.value) {
64 dates->start = g_new (ECalComponentDateTime, 1);
65 *dates->start = dt;
66 }
67
68 e_cal_component_get_dtend (comp, &dt);
69 if (dt.value) {
70 dates->end = g_new (ECalComponentDateTime, 1);
71 *dates->end = dt;
72 }
73
74 e_cal_component_get_due (comp, &dt);
75 if (dt.value) {
76 dates->due = g_new (ECalComponentDateTime, 1);
77 *dates->due = dt;
78 }
79
80 e_cal_component_get_completed (comp, &dates->complete);
81 }
82
83 /* This frees the dates in the CompEditorPageDates struct. But it doesn't free
84 * the struct (as that is usually static).
85 */
86 void
87 comp_editor_free_dates (CompEditorPageDates *dates)
88 {
89 /* Note that e_cal_component_free_datetime() only frees the fields in
90 * the struct. It doesn't free the struct itself, so we do that. */
91 if (dates->start) {
92 e_cal_component_free_datetime (dates->start);
93 g_free (dates->start);
94 }
95
96 if (dates->end) {
97 e_cal_component_free_datetime (dates->end);
98 g_free (dates->end);
99 }
100
101 if (dates->due) {
102 e_cal_component_free_datetime (dates->due);
103 g_free (dates->due);
104 }
105
106 if (dates->complete)
107 e_cal_component_free_icaltimetype (dates->complete);
108 }
109
110 /**
111 * comp_editor_new_date_edit:
112 * @show_date: Whether to show a date picker in the widget.
113 * @show_time: Whether to show a time picker in the widget.
114 * @make_time_insensitive: Whether the time field is made insensitive rather
115 * than hiding it. This is useful if you want to preserve the layout of the
116 * widgets.
117 *
118 * Creates a new #EDateEdit widget, configured using the calendar's preferences.
119 *
120 * Return value: A newly-created #EDateEdit widget.
121 **/
122 GtkWidget *
123 comp_editor_new_date_edit (gboolean show_date,
124 gboolean show_time,
125 gboolean make_time_insensitive)
126 {
127 EDateEdit *dedit;
128
129 dedit = E_DATE_EDIT (e_date_edit_new ());
130
131 e_date_edit_set_show_date (dedit, show_date);
132 e_date_edit_set_show_time (dedit, show_time);
133 #if 0
134 e_date_edit_set_make_time_insensitive (dedit, make_time_insensitive);
135 #else
136 e_date_edit_set_make_time_insensitive (dedit, FALSE);
137 #endif
138
139 return GTK_WIDGET (dedit);
140 }
141
142 /* Returns the current time, for EDateEdit widgets and ECalendar items in the
143 * dialogs.
144 * FIXME: Should probably use the timezone from somewhere in the component
145 * rather than the current timezone. */
146 struct tm
147 comp_editor_get_current_time (EDateEdit *date_edit,
148 CompEditor *editor)
149 {
150 icaltimezone *zone;
151 struct icaltimetype tt;
152 struct tm tmp_tm = { 0 };
153
154 /* Get the current timezone. */
155 zone = comp_editor_get_timezone (editor);
156
157 tt = icaltime_from_timet_with_zone (time (NULL), FALSE, zone);
158
159 /* Now copy it to the struct tm and return it. */
160 tmp_tm.tm_year = tt.year - 1900;
161 tmp_tm.tm_mon = tt.month - 1;
162 tmp_tm.tm_mday = tt.day;
163 tmp_tm.tm_hour = tt.hour;
164 tmp_tm.tm_min = tt.minute;
165 tmp_tm.tm_sec = tt.second;
166 tmp_tm.tm_isdst = -1;
167
168 return tmp_tm;
169 }
170
171 /**
172 * comp_editor_strip_categories:
173 * @categories: A string of category names entered by the user.
174 *
175 * Takes a string of the form "categ, categ, categ, ..." and removes the
176 * whitespace between categories to result in "categ,categ,categ,..."
177 *
178 * Return value: The category names stripped of surrounding whitespace
179 * and separated with commas.
180 **/
181 gchar *
182 comp_editor_strip_categories (const gchar *categories)
183 {
184 gchar *new_categories;
185 const gchar *start, *end;
186 const gchar *p;
187 gchar *new_p;
188
189 if (!categories)
190 return NULL;
191
192 new_categories = g_new (char, strlen (categories) + 1);
193
194 start = end = NULL;
195 new_p = new_categories;
196
197 for (p = categories; *p; p = g_utf8_next_char (p)) {
198 gunichar c;
199
200 c = g_utf8_get_char (p);
201
202 if (g_unichar_isspace (c))
203 continue;
204 else if (c == ',') {
205 gint len;
206
207 if (!start)
208 continue;
209
210 g_return_val_if_fail (start <= end, NULL);
211
212 len = end - start + 1;
213 strncpy (new_p, start, len);
214 new_p[len] = ',';
215 new_p += len + 1;
216
217 start = end = NULL;
218 } else {
219 if (!start) {
220 start = p;
221 end = p;
222 } else
223 end = g_utf8_next_char (p) - 1;
224 }
225 }
226
227 if (start) {
228 gint len;
229
230 g_return_val_if_fail (start <= end, NULL);
231
232 len = end - start + 1;
233 strncpy (new_p, start, len);
234 new_p += len;
235 }
236
237 *new_p = '\0';
238
239 return new_categories;
240 }
241
242 static GSList *
243 manage_new_attendees (const GSList *lst,
244 const gchar *eml,
245 gboolean add)
246 {
247 GSList *copy = NULL;
248 const GSList *l;
249 gboolean found = FALSE;
250
251 g_return_val_if_fail (eml != NULL, NULL);
252
253 for (l = lst; l; l = l->next) {
254 const gchar *eml2 = l->data;
255
256 if (!eml2)
257 continue;
258
259 if (g_ascii_strcasecmp (eml, eml2) == 0) {
260 found = TRUE;
261 if (add)
262 copy = g_slist_append (copy, g_strdup (eml2));
263 } else {
264 copy = g_slist_append (copy, g_strdup (eml2));
265 }
266 }
267
268 if (!found && add) {
269 copy = g_slist_append (copy, g_strdup (eml));
270 }
271
272 return copy;
273 }
274
275 static void
276 free_slist_strs (gpointer data)
277 {
278 GSList *lst = data;
279
280 if (lst) {
281 g_slist_foreach (lst, (GFunc) g_free, NULL);
282 g_slist_free (lst);
283 }
284 }
285
286 /**
287 * comp_editor_manage_new_attendees:
288 * @comp: The component.
289 * @ma: An attendee.
290 * @add: %TRUE to add attendee's email to new-attendees, %FALSE to remove
291 * from it.
292 *
293 * Manages the 'new-attendees' string of new attendees of the component.
294 *
295 * <note>
296 * <para>
297 * The list is just string of emails separated by ';'
298 * </para>
299 * </note>
300 **/
301 void
302 comp_editor_manage_new_attendees (ECalComponent *comp,
303 EMeetingAttendee *ma,
304 gboolean add)
305 {
306 const gchar *eml;
307
308 g_return_if_fail (comp != NULL);
309 g_return_if_fail (ma != NULL);
310
311 eml = e_meeting_attendee_get_address (ma);
312 if (eml)
313 eml = itip_strip_mailto (eml);
314 g_return_if_fail (eml != NULL);
315
316 g_object_set_data_full (
317 G_OBJECT (comp), "new-attendees",
318 manage_new_attendees (
319 g_object_get_data (G_OBJECT (comp), "new-attendees"),
320 eml, add), free_slist_strs);
321 }
322
323 /**
324 * comp_editor_copy_new_attendees:
325 * @des: Component, to copy to.
326 * @src: Component, to copy from.
327 *
328 * Copies "new-attendees" information from @src to @des component.
329 **/
330 void
331 comp_editor_copy_new_attendees (ECalComponent *des,
332 ECalComponent *src)
333 {
334 GSList *copy = NULL, *l;
335
336 g_return_if_fail (src != NULL);
337 g_return_if_fail (des != NULL);
338
339 for (l = g_object_get_data (G_OBJECT (src), "new-attendees"); l; l = l->next) {
340 copy = g_slist_append (copy, g_strdup (l->data));
341 }
342
343 g_object_set_data_full (G_OBJECT (des), "new-attendees", copy, free_slist_strs);
344 }
345
346 /**
347 * comp_editor_have_in_new_attendees:
348 * @comp: Component with the "new-attendees" possibly set.
349 * @ma: Meeting attendee to check.
350 *
351 * Returns: Whether @ma is present in the list of new attendees of the comp.
352 **/
353 gboolean
354 comp_editor_have_in_new_attendees (ECalComponent *comp,
355 EMeetingAttendee *ma)
356 {
357 const gchar *eml;
358
359 g_return_val_if_fail (comp != NULL, FALSE);
360 g_return_val_if_fail (ma != NULL, FALSE);
361
362 eml = e_meeting_attendee_get_address (ma);
363 if (eml)
364 eml = itip_strip_mailto (eml);
365 g_return_val_if_fail (eml != NULL, FALSE);
366
367 return comp_editor_have_in_new_attendees_lst (
368 g_object_get_data (G_OBJECT (comp), "new-attendees"), eml);
369 }
370
371 /**
372 * comp_editor_have_in_new_attendees_lst:
373 *
374 * Same as comp_editor_have_in_new_attendees() only parameters are
375 * direct GSList and string.
376 **/
377 gboolean
378 comp_editor_have_in_new_attendees_lst (const GSList *new_attendees,
379 const gchar *eml)
380 {
381 const GSList *l;
382
383 if (!eml)
384 return FALSE;
385
386 for (l = new_attendees; l; l = l->next) {
387 if (l->data && g_ascii_strcasecmp (eml, l->data) == 0)
388 return TRUE;
389 }
390
391 return FALSE;
392 }
393
394 /**
395 * comp_editor_test_time_in_the_past:
396 * @time_tt: Time to check.
397 * @parent: Parent window for a question dialog.
398 * @tag: Question message tag to use.
399 * Returns whether given time is in the past.
400 *
401 * Tests the given @time_tt whether occurs in the past,
402 * and if so, returns TRUE.
403 **/
404 gboolean
405 comp_editor_test_time_in_the_past (const struct icaltimetype time_tt)
406 {
407 struct icaltimetype now_tt;
408 gboolean is_past;
409
410 if (icaltime_is_null_time (time_tt))
411 return FALSE;
412
413 if (time_tt.is_date) {
414 now_tt = icaltime_today ();
415 is_past = icaltime_compare_date_only (time_tt, now_tt) < 0;
416 } else {
417 now_tt = icaltime_current_time_with_zone (time_tt.zone);
418 now_tt.zone = time_tt.zone;
419 is_past = icaltime_compare (time_tt, now_tt) < 0;
420 }
421
422 return is_past;
423 }