Update Romanian translation
[evolution.git] / src / calendar / gui / e-cal-component-preview.c
blobbb47e36fa0f22c530ca5e28a1fb1d96091998508
1 /*
2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU Lesser General Public License as published by
4 * the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful, but
7 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9 * for more details.
11 * You should have received a copy of the GNU Lesser General Public License
12 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 * Authors:
16 * Federico Mena Quintero <federico@ximian.com>
17 * Damon Chaplin <damon@ximian.com>
18 * Rodrigo Moya <rodrigo@ximian.com>
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
24 #include "evolution-config.h"
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
29 #include <camel/camel.h>
31 #include "shell/e-shell-utils.h"
33 #include "calendar-config.h"
35 #include "e-cal-component-preview.h"
37 #define E_CAL_COMPONENT_PREVIEW_GET_PRIVATE(obj) \
38 (G_TYPE_INSTANCE_GET_PRIVATE \
39 ((obj), E_TYPE_CAL_COMPONENT_PREVIEW, ECalComponentPreviewPrivate))
41 G_DEFINE_TYPE (
42 ECalComponentPreview,
43 e_cal_component_preview,
44 E_TYPE_WEB_VIEW)
46 struct _ECalComponentPreviewPrivate {
47 /* information about currently showing component in a preview;
48 * if it didn't change then the preview is not updated */
49 gchar *cal_uid;
50 gchar *comp_uid;
51 struct icaltimetype comp_last_modified;
52 gint comp_sequence;
54 ECalClient *client;
55 ECalComponent *comp;
56 icaltimezone *timezone;
57 gboolean use_24_hour_format;
60 #define HTML_HEADER "<!doctype html public \"-//W3C//DTD HTML 4.0 TRANSITIONAL//EN\">\n<html>\n" \
61 "<head>\n<meta name=\"generator\" content=\"Evolution Calendar Component\">\n" \
62 "<link type=\"text/css\" rel=\"stylesheet\" href=\"evo-file://" EVOLUTION_PRIVDATADIR "/theme/webview.css\">\n" \
63 "<style>\n" \
64 ".description { font-family: monospace; font-size: 1em; }\n" \
65 "</style>\n" \
66 "</head>"
68 static void
69 clear_comp_info (ECalComponentPreview *preview)
71 ECalComponentPreviewPrivate *priv;
73 g_return_if_fail (E_IS_CAL_COMPONENT_PREVIEW (preview));
75 priv = preview->priv;
77 g_free (priv->cal_uid);
78 priv->cal_uid = NULL;
79 g_free (priv->comp_uid);
80 priv->comp_uid = NULL;
81 priv->comp_last_modified = icaltime_null_time ();
82 priv->comp_sequence = -1;
84 g_clear_object (&priv->client);
85 g_clear_object (&priv->comp);
86 if (priv->timezone) {
87 icaltimezone_free (priv->timezone, 1);
88 priv->timezone = NULL;
92 /* Stores information about actually shown component and
93 * returns whether component in the preview changed */
94 static gboolean
95 update_comp_info (ECalComponentPreview *preview,
96 ECalClient *client,
97 ECalComponent *comp,
98 icaltimezone *zone,
99 gboolean use_24_hour_format)
101 ECalComponentPreviewPrivate *priv;
102 gboolean changed;
104 g_return_val_if_fail (preview != NULL, TRUE);
105 g_return_val_if_fail (E_IS_CAL_COMPONENT_PREVIEW (preview), TRUE);
107 priv = preview->priv;
109 if (!E_IS_CAL_COMPONENT (comp) || !E_IS_CAL_CLIENT (client)) {
110 changed = !priv->cal_uid;
111 clear_comp_info (preview);
112 } else {
113 ESource *source;
114 const gchar *uid;
115 gchar *cal_uid;
116 gchar *comp_uid;
117 struct icaltimetype comp_last_modified, *itm = NULL;
118 gint *sequence = NULL;
119 gint comp_sequence;
121 source = e_client_get_source (E_CLIENT (client));
122 cal_uid = g_strdup (e_source_get_uid (source));
123 e_cal_component_get_uid (comp, &uid);
124 comp_uid = g_strdup (uid);
125 e_cal_component_get_last_modified (comp, &itm);
126 if (itm) {
127 comp_last_modified = *itm;
128 e_cal_component_free_icaltimetype (itm);
129 } else
130 comp_last_modified = icaltime_null_time ();
131 e_cal_component_get_sequence (comp, &sequence);
132 if (sequence) {
133 comp_sequence = *sequence;
134 e_cal_component_free_sequence (sequence);
135 } else
136 comp_sequence = 0;
138 changed = !priv->cal_uid || !priv->comp_uid || !cal_uid || !comp_uid ||
139 !g_str_equal (priv->cal_uid, cal_uid) ||
140 !g_str_equal (priv->comp_uid, comp_uid) ||
141 priv->comp_sequence != comp_sequence ||
142 icaltime_compare (priv->comp_last_modified, comp_last_modified) != 0;
144 clear_comp_info (preview);
146 priv->cal_uid = cal_uid;
147 priv->comp_uid = comp_uid;
148 priv->comp_sequence = comp_sequence;
149 priv->comp_last_modified = comp_last_modified;
151 priv->comp = g_object_ref (comp);
152 priv->client = g_object_ref (client);
153 priv->timezone = icaltimezone_copy (zone);
154 priv->use_24_hour_format = use_24_hour_format;
157 return changed;
160 /* Converts a time_t to a string, relative to the specified timezone */
161 static gchar *
162 timet_to_str_with_zone (ECalComponentDateTime *dt,
163 ECalClient *client,
164 icaltimezone *default_zone)
166 struct icaltimetype itt;
167 icaltimezone *zone = NULL;
168 struct tm tm;
170 if (dt->tzid != NULL) {
171 e_cal_client_get_timezone_sync (
172 client, dt->tzid, &zone, NULL, NULL);
173 } else if (icaltime_is_utc (*dt->value)) {
174 zone = icaltimezone_get_utc_timezone ();
177 itt = *dt->value;
178 if (zone != NULL)
179 icaltimezone_convert_time (&itt, zone, default_zone);
180 tm = icaltimetype_to_tm (&itt);
182 return e_datetime_format_format_tm ("calendar", "table", itt.is_date ? DTFormatKindDate : DTFormatKindDateTime, &tm);
185 static void
186 cal_component_preview_write_html (ECalComponentPreview *preview,
187 GString *buffer)
189 ECalClient *client;
190 ECalComponent *comp;
191 icaltimezone *default_zone;
192 ECalComponentText text;
193 ECalComponentDateTime dt;
194 gchar *str;
195 GString *string;
196 GSList *list, *iter;
197 icalcomponent *icalcomp;
198 icalproperty *icalprop;
199 icalproperty_status status;
200 const gchar *location;
201 gint *priority_value;
203 client = preview->priv->client;
204 comp = preview->priv->comp;
205 default_zone = preview->priv->timezone;
207 /* write document header */
208 e_cal_component_get_summary (comp, &text);
210 g_string_append (buffer, HTML_HEADER);
211 g_string_append (buffer, "<body class=\"-e-web-view-background-color -e-web-view-text-color\">");
213 if (text.value)
214 g_string_append_printf (buffer, "<h2>%s</h2>", text.value);
215 else
216 g_string_append_printf (buffer, "<h2><i>%s</i></h2>",_("Untitled"));
218 g_string_append (buffer, "<table border=\"0\" cellspacing=\"5\">");
220 /* write icons for the categories */
221 string = g_string_new (NULL);
222 e_cal_component_get_categories_list (comp, &list);
223 if (list != NULL)
224 g_string_append_printf (buffer, "<tr><th>%s</th><td>", _("Categories:"));
225 for (iter = list; iter != NULL; iter = iter->next) {
226 const gchar *category = iter->data;
227 gchar *icon_file;
229 icon_file = e_categories_dup_icon_file_for (category);
230 if (icon_file && g_file_test (icon_file, G_FILE_TEST_EXISTS)) {
231 gchar *uri;
233 uri = g_filename_to_uri (icon_file, NULL, NULL);
234 g_string_append_printf (
235 buffer, "<img alt=\"%s\" src=\"evo-%s\">",
236 category, uri);
237 g_free (uri);
238 } else {
239 if (iter != list)
240 g_string_append_len (string, ", ", 2);
241 g_string_append (string, category);
244 g_free (icon_file);
246 if (string->len > 0)
247 g_string_append_printf (buffer, "%s", string->str);
248 if (list != NULL)
249 g_string_append (buffer, "</td></tr>");
250 e_cal_component_free_categories_list (list);
251 g_string_free (string, TRUE);
253 /* write location */
254 e_cal_component_get_location (comp, &location);
255 if (location)
256 g_string_append_printf (
257 buffer, "<tr><th>%s</th><td>%s</td></tr>",
258 _("Summary:"), text.value);
260 /* write start date */
261 e_cal_component_get_dtstart (comp, &dt);
262 if (dt.value != NULL) {
263 str = timet_to_str_with_zone (&dt, client, default_zone);
264 g_string_append_printf (
265 buffer, "<tr><th>%s</th><td>%s</td></tr>",
266 _("Start Date:"), str);
267 g_free (str);
269 e_cal_component_free_datetime (&dt);
271 /* write end date */
272 e_cal_component_get_dtend (comp, &dt);
273 if (dt.value != NULL) {
274 str = timet_to_str_with_zone (&dt, client, default_zone);
275 g_string_append_printf (
276 buffer,"<tr><th>%s</th><td>%s</td></tr>",
277 _("End Date:"), str);
278 g_free (str);
280 e_cal_component_free_datetime (&dt);
282 /* write Due Date */
283 e_cal_component_get_due (comp, &dt);
284 if (dt.value != NULL) {
285 str = timet_to_str_with_zone (&dt, client, default_zone);
286 g_string_append_printf (
287 buffer, "<tr><th>%s</th><td>%s</td></tr>",
288 _("Due Date:"), str);
289 g_free (str);
291 e_cal_component_free_datetime (&dt);
293 if (e_cal_util_component_has_recurrences (e_cal_component_get_icalcomponent (comp))) {
294 str = e_cal_recur_describe_recurrence (e_cal_component_get_icalcomponent (comp),
295 calendar_config_get_week_start_day (), E_CAL_RECUR_DESCRIBE_RECURRENCE_FLAG_NONE);
297 if (str) {
298 g_string_append_printf (
299 buffer, "<tr><th>%s</th><td>%s</td></tr>",
300 _("Recurs:"), str);
301 g_free (str);
305 /* write status */
306 icalcomp = e_cal_component_get_icalcomponent (comp);
307 icalprop = icalcomponent_get_first_property (
308 icalcomp, ICAL_STATUS_PROPERTY);
309 if (icalprop != NULL) {
310 g_string_append_printf (
311 buffer, "<tr><th>%s</th>",
312 _("Status:"));
313 e_cal_component_get_status (comp, &status);
314 switch (status) {
315 case ICAL_STATUS_INPROCESS :
316 str = g_strdup (_("In Progress"));
317 break;
318 case ICAL_STATUS_COMPLETED :
319 str = g_strdup (_("Completed"));
320 break;
321 case ICAL_STATUS_CANCELLED :
322 str = g_strdup (_("Cancelled"));
323 break;
324 case ICAL_STATUS_NONE :
325 default :
326 str = g_strdup (_("Not Started"));
327 break;
330 g_string_append_printf (buffer, "<td>%s</td></tr>", str);
331 g_free (str);
334 /* write priority */
335 e_cal_component_get_priority (comp, &priority_value);
336 if (priority_value && *priority_value != 0) {
337 g_string_append_printf (
338 buffer, "<tr><th>%s</th>",
339 _("Priority:"));
340 if (*priority_value <= 4)
341 str = g_strdup (_("High"));
342 else if (*priority_value == 5)
343 str = g_strdup (_("Normal"));
344 else
345 str = g_strdup (_("Low"));
347 g_string_append_printf (buffer, "<td>%s</td></tr>", str);
349 g_free (str);
352 if (priority_value)
353 e_cal_component_free_priority (priority_value);
355 /* write description and URL */
356 g_string_append (buffer, "<tr><td colspan=\"2\"><hr></td></tr>");
358 e_cal_component_get_description_list (comp, &list);
359 if (list) {
360 GSList *node;
362 g_string_append_printf (
363 buffer, "<tr><th>%s</th>",
364 _("Description:"));
366 g_string_append (buffer, "<td class=\"description\">");
368 for (node = list; node != NULL; node = node->next) {
369 gchar *html;
371 text = * (ECalComponentText *) node->data;
372 html = camel_text_to_html (
373 text.value ? text.value : "",
374 CAMEL_MIME_FILTER_TOHTML_CONVERT_NL |
375 CAMEL_MIME_FILTER_TOHTML_CONVERT_SPACES |
376 CAMEL_MIME_FILTER_TOHTML_CONVERT_URLS |
377 CAMEL_MIME_FILTER_TOHTML_CONVERT_ADDRESSES, 0);
379 if (html)
380 g_string_append_printf (buffer, "%s", html);
382 g_free (html);
385 g_string_append (buffer, "</td></tr>");
387 e_cal_component_free_text_list (list);
390 /* URL */
391 e_cal_component_get_url (comp, (const gchar **) &str);
392 if (str) {
393 g_string_append_printf (
394 buffer, "<tr><th>%s</th><td><a href=\"%s\">%s</a></td></tr>",
395 _("Web Page:"), str, str);
398 g_string_append (buffer, "</table>");
400 /* close document */
401 g_string_append (buffer, "</body></html>");
404 static void
405 load_comp (ECalComponentPreview *preview)
407 GString *buffer;
409 if (!preview->priv->comp) {
410 e_cal_component_preview_clear (preview);
411 return;
414 buffer = g_string_sized_new (4096);
415 cal_component_preview_write_html (preview, buffer);
416 e_web_view_load_string (E_WEB_VIEW (preview), buffer->str);
417 g_string_free (buffer, TRUE);
420 static void
421 cal_component_preview_web_process_crashed_cb (ECalComponentPreview *preview)
423 EAlertSink *alert_sink;
424 const gchar *tagid;
426 g_return_if_fail (E_IS_CAL_COMPONENT_PREVIEW (preview));
428 tagid = "system:webkit-web-process-crashed";
430 if (preview->priv->comp) {
431 ECalComponentVType vtype;
433 vtype = e_cal_component_get_vtype (preview->priv->comp);
434 if (vtype == E_CAL_COMPONENT_EVENT)
435 tagid = "calendar:webkit-web-process-crashed-event";
436 else if (vtype == E_CAL_COMPONENT_TODO)
437 tagid = "calendar:webkit-web-process-crashed-task";
438 else if (vtype == E_CAL_COMPONENT_JOURNAL)
439 tagid = "calendar:webkit-web-process-crashed-memo";
442 /* Cannot use the EWebView, because it places the alerts inside itself */
443 alert_sink = e_shell_utils_find_alternate_alert_sink (GTK_WIDGET (preview));
444 if (alert_sink)
445 e_alert_submit (alert_sink, tagid, NULL);
448 static void
449 cal_component_preview_finalize (GObject *object)
451 clear_comp_info (E_CAL_COMPONENT_PREVIEW (object));
453 /* Chain up to parent's finalize() method. */
454 G_OBJECT_CLASS (e_cal_component_preview_parent_class)->finalize (object);
457 static void
458 e_cal_component_preview_class_init (ECalComponentPreviewClass *class)
460 GObjectClass *object_class;
462 g_type_class_add_private (class, sizeof (ECalComponentPreviewPrivate));
464 object_class = G_OBJECT_CLASS (class);
465 object_class->finalize = cal_component_preview_finalize;
468 static void
469 e_cal_component_preview_init (ECalComponentPreview *preview)
471 preview->priv = E_CAL_COMPONENT_PREVIEW_GET_PRIVATE (preview);
473 g_signal_connect (
474 preview, "web-process-crashed",
475 G_CALLBACK (cal_component_preview_web_process_crashed_cb), NULL);
478 GtkWidget *
479 e_cal_component_preview_new (void)
481 return g_object_new (E_TYPE_CAL_COMPONENT_PREVIEW, NULL);
484 void
485 e_cal_component_preview_display (ECalComponentPreview *preview,
486 ECalClient *client,
487 ECalComponent *comp,
488 icaltimezone *zone,
489 gboolean use_24_hour_format)
491 g_return_if_fail (E_IS_CAL_COMPONENT_PREVIEW (preview));
492 g_return_if_fail (E_IS_CAL_COMPONENT (comp));
494 /* do not update preview when setting the same component as last time,
495 * which even didn't change */
496 if (!update_comp_info (preview, client, comp, zone, use_24_hour_format))
497 return;
499 load_comp (preview);
502 void
503 e_cal_component_preview_clear (ECalComponentPreview *preview)
505 g_return_if_fail (E_IS_CAL_COMPONENT_PREVIEW (preview));
507 clear_comp_info (preview);
508 e_web_view_clear (E_WEB_VIEW (preview));