No issues found
Tool | Failure ID | Location | Function | Message | Data |
---|---|---|---|---|---|
clang-analyzer | no-output-found | e-mail-signature-preview.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None | |
clang-analyzer | no-output-found | e-mail-signature-preview.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None |
1 /*
2 * e-mail-signature-preview.c
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
19 #include "e-mail-signature-preview.h"
20
21 #include <fcntl.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <glib/gstdio.h>
25
26 #include <libevolution-utils/e-alert-sink.h>
27
28 #define E_MAIL_SIGNATURE_PREVIEW_GET_PRIVATE(obj) \
29 (G_TYPE_INSTANCE_GET_PRIVATE \
30 ((obj), E_TYPE_MAIL_SIGNATURE_PREVIEW, EMailSignaturePreviewPrivate))
31
32 #define SOURCE_IS_MAIL_SIGNATURE(source) \
33 (e_source_has_extension ((source), E_SOURCE_EXTENSION_MAIL_SIGNATURE))
34
35 struct _EMailSignaturePreviewPrivate {
36 ESourceRegistry *registry;
37 GCancellable *cancellable;
38 gchar *source_uid;
39 };
40
41 enum {
42 PROP_0,
43 PROP_REGISTRY,
44 PROP_SOURCE_UID
45 };
46
47 enum {
48 REFRESH,
49 LAST_SIGNAL
50 };
51
52 static guint signals[LAST_SIGNAL];
53
54 G_DEFINE_TYPE (
55 EMailSignaturePreview,
56 e_mail_signature_preview,
57 E_TYPE_WEB_VIEW)
58
59 static void
60 mail_signature_preview_load_cb (ESource *source,
61 GAsyncResult *result,
62 EMailSignaturePreview *preview)
63 {
64 ESourceMailSignature *extension;
65 const gchar *extension_name;
66 const gchar *mime_type;
67 gchar *contents = NULL;
68 GError *error = NULL;
69
70 e_source_mail_signature_load_finish (
71 source, result, &contents, NULL, &error);
72
73 /* Ignore cancellations. */
74 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
75 g_warn_if_fail (contents == NULL);
76 g_object_unref (preview);
77 g_error_free (error);
78 return;
79
80 } else if (error != NULL) {
81 g_warn_if_fail (contents == NULL);
82 e_alert_submit (
83 E_ALERT_SINK (preview),
84 "widgets:no-load-signature",
85 error->message, NULL);
86 g_object_unref (preview);
87 g_error_free (error);
88 return;
89 }
90
91 g_return_if_fail (contents != NULL);
92
93 extension_name = E_SOURCE_EXTENSION_MAIL_SIGNATURE;
94 extension = e_source_get_extension (source, extension_name);
95 mime_type = e_source_mail_signature_get_mime_type (extension);
96
97 if (g_strcmp0 (mime_type, "text/html") == 0)
98 e_web_view_load_string (E_WEB_VIEW (preview), contents);
99 else {
100 gchar *string;
101
102 string = g_markup_printf_escaped ("<pre>%s</pre>", contents);
103 e_web_view_load_string (E_WEB_VIEW (preview), string);
104 g_free (string);
105 }
106
107 g_free (contents);
108
109 g_object_unref (preview);
110 }
111
112 static void
113 mail_signature_preview_set_registry (EMailSignaturePreview *preview,
114 ESourceRegistry *registry)
115 {
116 g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
117 g_return_if_fail (preview->priv->registry == NULL);
118
119 preview->priv->registry = g_object_ref (registry);
120 }
121
122 static void
123 mail_signature_preview_set_property (GObject *object,
124 guint property_id,
125 const GValue *value,
126 GParamSpec *pspec)
127 {
128 switch (property_id) {
129 case PROP_REGISTRY:
130 mail_signature_preview_set_registry (
131 E_MAIL_SIGNATURE_PREVIEW (object),
132 g_value_get_object (value));
133 return;
134
135 case PROP_SOURCE_UID:
136 e_mail_signature_preview_set_source_uid (
137 E_MAIL_SIGNATURE_PREVIEW (object),
138 g_value_get_string (value));
139 return;
140 }
141
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
143 }
144
145 static void
146 mail_signature_preview_get_property (GObject *object,
147 guint property_id,
148 GValue *value,
149 GParamSpec *pspec)
150 {
151 switch (property_id) {
152 case PROP_REGISTRY:
153 g_value_set_object (
154 value,
155 e_mail_signature_preview_get_registry (
156 E_MAIL_SIGNATURE_PREVIEW (object)));
157 return;
158
159 case PROP_SOURCE_UID:
160 g_value_set_string (
161 value,
162 e_mail_signature_preview_get_source_uid (
163 E_MAIL_SIGNATURE_PREVIEW (object)));
164 return;
165 }
166
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
168 }
169
170 static void
171 mail_signature_preview_dispose (GObject *object)
172 {
173 EMailSignaturePreviewPrivate *priv;
174
175 priv = E_MAIL_SIGNATURE_PREVIEW_GET_PRIVATE (object);
176
177 if (priv->registry != NULL) {
178 g_object_unref (priv->registry);
179 priv->registry = NULL;
180 }
181
182 if (priv->cancellable != NULL) {
183 g_cancellable_cancel (priv->cancellable);
184 g_object_unref (priv->cancellable);
185 priv->cancellable = NULL;
186 }
187
188 /* Chain up to parent's dispose() method. */
189 G_OBJECT_CLASS (e_mail_signature_preview_parent_class)->
190 dispose (object);
191 }
192
193 static void
194 mail_signature_preview_finalize (GObject *object)
195 {
196 EMailSignaturePreviewPrivate *priv;
197
198 priv = E_MAIL_SIGNATURE_PREVIEW_GET_PRIVATE (object);
199
200 g_free (priv->source_uid);
201
202 /* Chain up to parent's finalize() method. */
203 G_OBJECT_CLASS (e_mail_signature_preview_parent_class)->
204 finalize (object);
205 }
206
207 static void
208 mail_signature_preview_refresh (EMailSignaturePreview *preview)
209 {
210 ESourceRegistry *registry;
211 ESource *source;
212 const gchar *extension_name;
213 const gchar *source_uid;
214
215 /* Cancel any unfinished refreshes. */
216 if (preview->priv->cancellable != NULL) {
217 g_cancellable_cancel (preview->priv->cancellable);
218 g_object_unref (preview->priv->cancellable);
219 preview->priv->cancellable = NULL;
220 }
221
222 source_uid = e_mail_signature_preview_get_source_uid (preview);
223
224 if (source_uid == NULL)
225 goto fail;
226
227 registry = e_mail_signature_preview_get_registry (preview);
228 source = e_source_registry_ref_source (registry, source_uid);
229
230 if (source == NULL)
231 goto fail;
232
233 extension_name = E_SOURCE_EXTENSION_MAIL_SIGNATURE;
234 if (!e_source_has_extension (source, extension_name)) {
235 g_object_unref (source);
236 goto fail;
237 }
238
239 preview->priv->cancellable = g_cancellable_new ();
240
241 e_source_mail_signature_load (
242 source, G_PRIORITY_DEFAULT,
243 preview->priv->cancellable, (GAsyncReadyCallback)
244 mail_signature_preview_load_cb, g_object_ref (preview));
245
246 g_object_unref (source);
247
248 return;
249
250 fail:
251 e_web_view_clear (E_WEB_VIEW (preview));
252 }
253
254 static void
255 e_mail_signature_preview_class_init (EMailSignaturePreviewClass *class)
256 {
257 GObjectClass *object_class;
258
259 g_type_class_add_private (class, sizeof (EMailSignaturePreviewPrivate));
260
261 object_class = G_OBJECT_CLASS (class);
262 object_class->set_property = mail_signature_preview_set_property;
263 object_class->get_property = mail_signature_preview_get_property;
264 object_class->dispose = mail_signature_preview_dispose;
265 object_class->finalize = mail_signature_preview_finalize;
266
267 class->refresh = mail_signature_preview_refresh;
268
269 g_object_class_install_property (
270 object_class,
271 PROP_REGISTRY,
272 g_param_spec_object (
273 "registry",
274 "Registry",
275 NULL,
276 E_TYPE_SOURCE_REGISTRY,
277 G_PARAM_READWRITE |
278 G_PARAM_CONSTRUCT_ONLY |
279 G_PARAM_STATIC_STRINGS));
280
281 g_object_class_install_property (
282 object_class,
283 PROP_SOURCE_UID,
284 g_param_spec_string (
285 "source-uid",
286 "Source UID",
287 NULL,
288 NULL,
289 G_PARAM_READWRITE |
290 G_PARAM_STATIC_STRINGS));
291
292 signals[REFRESH] = g_signal_new (
293 "refresh",
294 G_TYPE_FROM_CLASS (class),
295 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
296 G_STRUCT_OFFSET (EMailSignaturePreviewClass, refresh),
297 NULL, NULL,
298 g_cclosure_marshal_VOID__VOID,
299 G_TYPE_NONE, 0);
300 }
301
302 static void
303 e_mail_signature_preview_init (EMailSignaturePreview *preview)
304 {
305 preview->priv = E_MAIL_SIGNATURE_PREVIEW_GET_PRIVATE (preview);
306 }
307
308 GtkWidget *
309 e_mail_signature_preview_new (ESourceRegistry *registry)
310 {
311 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
312
313 return g_object_new (
314 E_TYPE_MAIL_SIGNATURE_PREVIEW,
315 "registry", registry, NULL);
316 }
317
318 void
319 e_mail_signature_preview_refresh (EMailSignaturePreview *preview)
320 {
321 g_return_if_fail (E_IS_MAIL_SIGNATURE_PREVIEW (preview));
322
323 g_signal_emit (preview, signals[REFRESH], 0);
324 }
325
326 ESourceRegistry *
327 e_mail_signature_preview_get_registry (EMailSignaturePreview *preview)
328 {
329 g_return_val_if_fail (E_IS_MAIL_SIGNATURE_PREVIEW (preview), NULL);
330
331 return preview->priv->registry;
332 }
333
334 const gchar *
335 e_mail_signature_preview_get_source_uid (EMailSignaturePreview *preview)
336 {
337 g_return_val_if_fail (E_IS_MAIL_SIGNATURE_PREVIEW (preview), NULL);
338
339 return preview->priv->source_uid;
340 }
341
342 void
343 e_mail_signature_preview_set_source_uid (EMailSignaturePreview *preview,
344 const gchar *source_uid)
345 {
346 g_return_if_fail (E_IS_MAIL_SIGNATURE_PREVIEW (preview));
347
348 /* Avoid repeatedly loading the same signature file. */
349 if (g_strcmp0 (source_uid, preview->priv->source_uid) == 0)
350 return;
351
352 g_free (preview->priv->source_uid);
353 preview->priv->source_uid = g_strdup (source_uid);
354
355 g_object_notify (G_OBJECT (preview), "source-uid");
356
357 e_mail_signature_preview_refresh (preview);
358 }