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
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/>.
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"
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))
43 e_cal_component_preview
,
46 struct _ECalComponentPreviewPrivate
{
47 /* information about currently showing component in a preview;
48 * if it didn't change then the preview is not updated */
51 struct icaltimetype comp_last_modified
;
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" \
64 ".description { font-family: monospace; font-size: 1em; }\n" \
69 clear_comp_info (ECalComponentPreview
*preview
)
71 ECalComponentPreviewPrivate
*priv
;
73 g_return_if_fail (E_IS_CAL_COMPONENT_PREVIEW (preview
));
77 g_free (priv
->cal_uid
);
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
);
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 */
95 update_comp_info (ECalComponentPreview
*preview
,
99 gboolean use_24_hour_format
)
101 ECalComponentPreviewPrivate
*priv
;
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
);
117 struct icaltimetype comp_last_modified
, *itm
= NULL
;
118 gint
*sequence
= NULL
;
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
);
127 comp_last_modified
= *itm
;
128 e_cal_component_free_icaltimetype (itm
);
130 comp_last_modified
= icaltime_null_time ();
131 e_cal_component_get_sequence (comp
, &sequence
);
133 comp_sequence
= *sequence
;
134 e_cal_component_free_sequence (sequence
);
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
;
160 /* Converts a time_t to a string, relative to the specified timezone */
162 timet_to_str_with_zone (ECalComponentDateTime
*dt
,
164 icaltimezone
*default_zone
)
166 struct icaltimetype itt
;
167 icaltimezone
*zone
= NULL
;
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 ();
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
);
186 cal_component_preview_write_html (ECalComponentPreview
*preview
,
191 icaltimezone
*default_zone
;
192 ECalComponentText text
;
193 ECalComponentDateTime dt
;
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\">");
214 g_string_append_printf (buffer
, "<h2>%s</h2>", text
.value
);
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
);
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
;
229 icon_file
= e_categories_dup_icon_file_for (category
);
230 if (icon_file
&& g_file_test (icon_file
, G_FILE_TEST_EXISTS
)) {
233 uri
= g_filename_to_uri (icon_file
, NULL
, NULL
);
234 g_string_append_printf (
235 buffer
, "<img alt=\"%s\" src=\"evo-%s\">",
240 g_string_append_len (string
, ", ", 2);
241 g_string_append (string
, category
);
247 g_string_append_printf (buffer
, "%s", string
->str
);
249 g_string_append (buffer
, "</td></tr>");
250 e_cal_component_free_categories_list (list
);
251 g_string_free (string
, TRUE
);
254 e_cal_component_get_location (comp
, &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
);
269 e_cal_component_free_datetime (&dt
);
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
);
280 e_cal_component_free_datetime (&dt
);
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
);
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
);
298 g_string_append_printf (
299 buffer
, "<tr><th>%s</th><td>%s</td></tr>",
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>",
313 e_cal_component_get_status (comp
, &status
);
315 case ICAL_STATUS_INPROCESS
:
316 str
= g_strdup (_("In Progress"));
318 case ICAL_STATUS_COMPLETED
:
319 str
= g_strdup (_("Completed"));
321 case ICAL_STATUS_CANCELLED
:
322 str
= g_strdup (_("Cancelled"));
324 case ICAL_STATUS_NONE
:
326 str
= g_strdup (_("Not Started"));
330 g_string_append_printf (buffer
, "<td>%s</td></tr>", str
);
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>",
340 if (*priority_value
<= 4)
341 str
= g_strdup (_("High"));
342 else if (*priority_value
== 5)
343 str
= g_strdup (_("Normal"));
345 str
= g_strdup (_("Low"));
347 g_string_append_printf (buffer
, "<td>%s</td></tr>", str
);
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
);
362 g_string_append_printf (
363 buffer
, "<tr><th>%s</th>",
366 g_string_append (buffer
, "<td class=\"description\">");
368 for (node
= list
; node
!= NULL
; node
= node
->next
) {
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);
380 g_string_append_printf (buffer
, "%s", html
);
385 g_string_append (buffer
, "</td></tr>");
387 e_cal_component_free_text_list (list
);
391 e_cal_component_get_url (comp
, (const gchar
**) &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>");
401 g_string_append (buffer
, "</body></html>");
405 load_comp (ECalComponentPreview
*preview
)
409 if (!preview
->priv
->comp
) {
410 e_cal_component_preview_clear (preview
);
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
);
421 cal_component_preview_web_process_crashed_cb (ECalComponentPreview
*preview
)
423 EAlertSink
*alert_sink
;
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
));
445 e_alert_submit (alert_sink
, tagid
, NULL
);
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
);
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
;
469 e_cal_component_preview_init (ECalComponentPreview
*preview
)
471 preview
->priv
= E_CAL_COMPONENT_PREVIEW_GET_PRIVATE (preview
);
474 preview
, "web-process-crashed",
475 G_CALLBACK (cal_component_preview_web_process_crashed_cb
), NULL
);
479 e_cal_component_preview_new (void)
481 return g_object_new (E_TYPE_CAL_COMPONENT_PREVIEW
, NULL
);
485 e_cal_component_preview_display (ECalComponentPreview
*preview
,
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
))
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
));