1 /*
2  * e-mail-formatter-itip.c
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-config.h"
19 
20 #include <glib/gi18n-lib.h>
21 
22 #include <em-format/e-mail-formatter-extension.h>
23 #include <em-format/e-mail-formatter.h>
24 #include <em-format/e-mail-part-utils.h>
25 #include <libebackend/libebackend.h>
26 
27 #include "itip-view.h"
28 #include "e-mail-part-itip.h"
29 
30 #include "e-mail-formatter-itip.h"
31 
32 #define d(x)
33 
34 typedef EMailFormatterExtension EMailFormatterItip;
35 typedef EMailFormatterExtensionClass EMailFormatterItipClass;
36 
37 GType e_mail_formatter_itip_get_type (void);
38 GType e_mail_formatter_itip_loader_get_type (void);
39 
40 G_DEFINE_DYNAMIC_TYPE (
41 	EMailFormatterItip,
42 	e_mail_formatter_itip,
43 	E_TYPE_MAIL_FORMATTER_EXTENSION)
44 
45 static const gchar *formatter_mime_types[] = {
46 	"text/calendar",
47 	"application/ics",
48 	NULL
49 };
50 
51 static gboolean
emfe_itip_format(EMailFormatterExtension * extension,EMailFormatter * formatter,EMailFormatterContext * context,EMailPart * part,GOutputStream * stream,GCancellable * cancellable)52 emfe_itip_format (EMailFormatterExtension *extension,
53                   EMailFormatter *formatter,
54                   EMailFormatterContext *context,
55                   EMailPart *part,
56                   GOutputStream *stream,
57                   GCancellable *cancellable)
58 {
59 	GString *buffer;
60 	EMailPartItip *itip_part;
61 
62 	/* This can be called with attachment parts too, thus
63 	   return silently in that case */
64 	if (!E_IS_MAIL_PART_ITIP (part))
65 		return FALSE;
66 
67 	itip_part = (EMailPartItip *) part;
68 
69 	if (context->mode == E_MAIL_FORMATTER_MODE_PRINTING) {
70 		ItipView *itip_view;
71 
72 		buffer = g_string_sized_new (1024);
73 
74 		itip_view = itip_view_new (e_mail_part_get_id (part),
75 			itip_part,
76 			itip_part->folder,
77 			itip_part->message_uid,
78 			itip_part->message,
79 			itip_part->itip_mime_part,
80 			itip_part->vcalendar,
81 			itip_part->cancellable);
82 		itip_view_init_view (itip_view);
83 		itip_view_write_for_printing (itip_view, buffer);
84 
85 	} else if (context->mode == E_MAIL_FORMATTER_MODE_RAW) {
86 		buffer = g_string_sized_new (2048);
87 
88 		itip_view_write (itip_part, formatter, buffer);
89 	} else {
90 		CamelFolder *folder, *old_folder;
91 		CamelMimeMessage *message, *old_message;
92 		const gchar *message_uid;
93 		const gchar *default_charset, *charset;
94 		gchar *uri, *old_message_uid;
95 
96 		folder = e_mail_part_list_get_folder (context->part_list);
97 		message = e_mail_part_list_get_message (context->part_list);
98 		message_uid = e_mail_part_list_get_message_uid (context->part_list);
99 
100 		/* mark message as containing calendar, thus it will show the
101 		 * icon in message list now on */
102 		if (message_uid != NULL && folder != NULL &&
103 			!camel_folder_get_message_user_flag (
104 				folder, message_uid, "$has_cal")) {
105 
106 			camel_folder_set_message_user_flag (
107 				folder, message_uid, "$has_cal", TRUE);
108 		}
109 
110 		old_folder = itip_part->folder;
111 		old_message = itip_part->message;
112 		old_message_uid = itip_part->message_uid;
113 
114 		itip_part->folder = folder ? g_object_ref (folder) : NULL;
115 		itip_part->message = g_object_ref (message);
116 		itip_part->message_uid = g_strdup (message_uid);
117 
118 		g_clear_object (&old_folder);
119 		g_clear_object (&old_message);
120 		g_free (old_message_uid);
121 
122 		default_charset = e_mail_formatter_get_default_charset (formatter);
123 		charset = e_mail_formatter_get_charset (formatter);
124 
125 		if (!default_charset)
126 			default_charset = "";
127 		if (!charset)
128 			charset = "";
129 
130 		uri = e_mail_part_build_uri (
131 			folder, message_uid,
132 			"part_id", G_TYPE_STRING, e_mail_part_get_id (part),
133 			"mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_RAW,
134 			"formatter_default_charset", G_TYPE_STRING, default_charset,
135 			"formatter_charset", G_TYPE_STRING, charset,
136 			NULL);
137 
138 		buffer = g_string_sized_new (256);
139 		g_string_append_printf (
140 			buffer,
141 			"<div class=\"part-container\" "
142 			"style=\"border: none; background: none;\">"
143 			"<iframe width=\"100%%\" height=\"auto\""
144 			" frameborder=\"0\" src=\"%s\" name=\"%s\" id=\"%s\"></iframe>"
145 			"</div>",
146 			uri,
147 			e_mail_part_get_id (part),
148 			e_mail_part_get_id (part));
149 
150 		g_free (uri);
151 	}
152 
153 	g_output_stream_write_all (
154 		stream, buffer->str, buffer->len, NULL, cancellable, NULL);
155 
156 	g_string_free (buffer, TRUE);
157 
158 	return TRUE;
159 }
160 
161 static void
e_mail_formatter_itip_class_init(EMailFormatterExtensionClass * class)162 e_mail_formatter_itip_class_init (EMailFormatterExtensionClass *class)
163 {
164 	class->display_name = _("ITIP");
165 	class->description = _("Display part as an invitation");
166 	class->mime_types = formatter_mime_types;
167 	class->format = emfe_itip_format;
168 }
169 
170 static void
e_mail_formatter_itip_class_finalize(EMailFormatterExtensionClass * class)171 e_mail_formatter_itip_class_finalize (EMailFormatterExtensionClass *class)
172 {
173 }
174 
175 static void
e_mail_formatter_itip_init(EMailFormatterExtension * extension)176 e_mail_formatter_itip_init (EMailFormatterExtension *extension)
177 {
178 }
179 
180 void
e_mail_formatter_itip_type_register(GTypeModule * type_module)181 e_mail_formatter_itip_type_register (GTypeModule *type_module)
182 {
183 	e_mail_formatter_itip_register_type (type_module);
184 }
185