evolution-3.6.4/modules/text-highlight/e-mail-display-popup-text-highlight.c

No issues found

Incomplete coverage

Tool Failure ID Location Function Message Data
clang-analyzer no-output-found e-mail-display-popup-text-highlight.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
clang-analyzer no-output-found e-mail-display-popup-text-highlight.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  */
 15 
 16 #ifdef HAVE_CONFIG_H
 17 #include <config.h>
 18 #endif
 19 
 20 #include "e-mail-display-popup-text-highlight.h"
 21 #include "mail/e-mail-display-popup-extension.h"
 22 #include "mail/e-mail-display.h"
 23 #include <shell/e-shell.h>
 24 #include <shell/e-shell-window.h>
 25 #include "mail/e-mail-browser.h"
 26 
 27 #include <libebackend/libebackend.h>
 28 
 29 #include <glib/gi18n-lib.h>
 30 
 31 #include "languages.h"
 32 
 33 #define d(x)
 34 
 35 typedef struct _EMailDisplayPopupTextHighlight {
 36 	EExtension parent;
 37 
 38 	GtkActionGroup *action_group;
 39 
 40 	WebKitDOMDocument *document;
 41 } EMailDisplayPopupTextHighlight;
 42 
 43 typedef struct _EMailDisplayPopupTextHighlightClass {
 44 	EExtensionClass parent_class;
 45 } EMailDisplayPopupTextHighlightClass;
 46 
 47 #define E_MAIL_DISPLAY_POPUP_TEXT_HIGHLIGHT(obj) \
 48 	(G_TYPE_CHECK_INSTANCE_CAST \
 49 	((obj), e_mail_display_popup_text_highlight_get_type (), EMailDisplayPopupTextHighlight))
 50 
 51 GType e_mail_display_popup_text_highlight_get_type (void);
 52 static void e_mail_display_popup_extension_interface_init (EMailDisplayPopupExtensionInterface *iface);
 53 
 54 G_DEFINE_DYNAMIC_TYPE_EXTENDED (
 55 	EMailDisplayPopupTextHighlight,
 56 	e_mail_display_popup_text_highlight,
 57 	E_TYPE_EXTENSION,
 58 	0,
 59 	G_IMPLEMENT_INTERFACE_DYNAMIC (
 60 		E_TYPE_MAIL_DISPLAY_POPUP_EXTENSION,
 61 		e_mail_display_popup_extension_interface_init));
 62 
 63 static const gchar *ui =
 64 "<ui>"
 65 "  <popup name='context'>"
 66 "    <placeholder name='custom-actions-2'>"
 67 "      <separator />"
 68 "      <menu action='format-as-menu'>"
 69 "	 <placeholder name='format-as-actions' />"
 70 "	 <menu action='format-as-other-menu'>"
 71 "	 </menu>"
 72 "      </menu>"
 73 "    </placeholder>"
 74 "  </popup>"
 75 "</ui>";
 76 
 77 static const gchar *ui_reader =
 78 "<ui>"
 79 "  <popup name='mail-preview-popup'>"
 80 "    <placeholder name='mail-preview-popup-actions'>"
 81 "      <separator />"
 82 "      <menu action='format-as-menu'>"
 83 "	 <placeholder name='format-as-actions' />"
 84 "	 <menu action='format-as-other-menu'>"
 85 "	 </menu>"
 86 "      </menu>"
 87 "    </placeholder>"
 88 "  </popup>"
 89 "</ui>";
 90 
 91 static GtkActionEntry entries[] = {
 92 
 93 	{ "format-as-menu",
 94 	  NULL,
 95 	  N_("_Format as..."),
 96 	  NULL,
 97 	  NULL,
 98 	  NULL
 99 	},
100 
101 	{ "format-as-other-menu",
102 	  NULL,
103 	  N_("_Other languages"),
104 	  NULL,
105 	  NULL,
106 	  NULL
107 	}
108 };
109 
110 static void
111 reformat (GtkAction *old,
112           GtkAction *action,
113           gpointer user_data)
114 {
115 	EMailDisplayPopupTextHighlight *th_extension;
116 	WebKitDOMDocument *doc;
117 	WebKitDOMDOMWindow *window;
118 	WebKitDOMElement *frame_element;
119 	SoupURI *soup_uri;
120 	GHashTable *query;
121 	gchar *uri;
122 
123 	th_extension = E_MAIL_DISPLAY_POPUP_TEXT_HIGHLIGHT (user_data);
124 	doc = th_extension->document;
125 	if (!doc)
126 		return;
127 
128 	uri = webkit_dom_document_get_document_uri (doc);
129 	soup_uri = soup_uri_new (uri);
130 	g_free (uri);
131 
132 	if (!soup_uri)
133 		goto exit;
134 
135 	if (!soup_uri->query) {
136 		soup_uri_free (soup_uri);
137 		goto exit;
138 	}
139 
140 	query = soup_form_decode (soup_uri->query);
141 	g_hash_table_replace (
142 		query, g_strdup ("__formatas"), (gpointer) gtk_action_get_name (action));
143 	g_hash_table_replace (
144 		query, g_strdup ("mime_type"), (gpointer) "text/plain");
145 
146 	soup_uri_set_query_from_form (soup_uri, query);
147 	g_hash_table_destroy (query);
148 
149 	uri = soup_uri_to_string (soup_uri, FALSE);
150 	soup_uri_free (soup_uri);
151 
152 	/* Get frame's window and from the window the actual <iframe> element */
153 	window = webkit_dom_document_get_default_view (doc);
154 	frame_element = webkit_dom_dom_window_get_frame_element (window);
155 	webkit_dom_html_iframe_element_set_src (
156 		WEBKIT_DOM_HTML_IFRAME_ELEMENT (frame_element), uri);
157 
158 	g_free (uri);
159 
160 	/* The frame has been reloaded, the document pointer is invalid now */
161 exit:
162 	th_extension->document = NULL;
163 }
164 
165 static GtkActionGroup *
166 create_group (EMailDisplayPopupExtension *extension)
167 {
168 	EExtensible *extensible;
169 	EWebView *web_view;
170 	GtkUIManager *ui_manager, *shell_ui_manager;
171 	GtkActionGroup *group;
172 	EShell *shell;
173 	GtkWindow *shell_window;
174 	gint i;
175 	gsize len;
176 	guint merge_id, shell_merge_id;
177 	Language *languages;
178 	GSList *radio_group;
179 	gint action_index;
180 
181 	extensible = e_extension_get_extensible (E_EXTENSION (extension));
182 	web_view = E_WEB_VIEW (extensible);
183 
184 	ui_manager = e_web_view_get_ui_manager (web_view);
185 	shell = e_shell_get_default ();
186 	shell_window = e_shell_get_active_window (shell);
187 	if (E_IS_SHELL_WINDOW (shell_window)) {
188 		shell_ui_manager = e_shell_window_get_ui_manager (E_SHELL_WINDOW (shell_window));
189 	} else if (E_IS_MAIL_BROWSER (shell_window)) {
190 		shell_ui_manager = e_mail_browser_get_ui_manager (E_MAIL_BROWSER (shell_window));
191 	} else {
192 		return NULL;
193 	}
194 
195 	group = gtk_action_group_new ("format-as");
196 	gtk_action_group_add_actions (group, entries, G_N_ELEMENTS (entries), NULL);
197 
198 	gtk_ui_manager_insert_action_group (ui_manager, group, 0);
199 	gtk_ui_manager_add_ui_from_string (ui_manager, ui, -1, NULL);
200 
201 	gtk_ui_manager_insert_action_group (shell_ui_manager, group, 0);
202 	gtk_ui_manager_add_ui_from_string (shell_ui_manager, ui_reader, -1, NULL);
203 
204 	merge_id = gtk_ui_manager_new_merge_id (ui_manager);
205 	shell_merge_id = gtk_ui_manager_new_merge_id (shell_ui_manager);
206 
207 	languages = get_default_langauges (&len);
208 	radio_group = NULL;
209 	action_index = 0;
210 	for (i = 0; i < len; i++) {
211 
212 		GtkRadioAction *action;
213 		action = gtk_radio_action_new (
214 				languages[i].action_name,
215 				languages[i].action_label,
216 				NULL, NULL, action_index);
217 		action_index++;
218 		gtk_action_group_add_action (group, GTK_ACTION (action));
219 		g_signal_connect (
220 			action, "changed",
221 			G_CALLBACK (reformat), extension);
222 		gtk_radio_action_set_group (action, radio_group);
223 		radio_group = gtk_radio_action_get_group (action);
224 
225 		g_object_unref (action);
226 
227 		gtk_ui_manager_add_ui (
228 			ui_manager, merge_id,
229 			"/context/custom-actions-2/format-as-menu/format-as-actions",
230 			languages[i].action_name, languages[i].action_name,
231 			GTK_UI_MANAGER_AUTO, FALSE);
232 
233 		gtk_ui_manager_add_ui (
234 			shell_ui_manager, shell_merge_id,
235 			"/mail-preview-popup/mail-preview-popup-actions/format-as-menu/format-as-actions",
236 			languages[i].action_name, languages[i].action_name,
237 			GTK_UI_MANAGER_AUTO, FALSE);
238 	}
239 
240 	languages = get_additinal_languages (&len);
241 	for (i = 0; i < len; i++) {
242 		GtkRadioAction *action;
243 
244 		action = gtk_radio_action_new (
245 				languages[i].action_name,
246 				languages[i].action_label,
247 				NULL, NULL, action_index);
248 		action_index++;
249 		gtk_action_group_add_action (group, GTK_ACTION (action));
250 		g_signal_connect (
251 			action, "changed",
252 			G_CALLBACK (reformat), extension);
253 
254 		gtk_radio_action_set_group (action, radio_group);
255 		radio_group = gtk_radio_action_get_group (action);
256 
257 		g_object_unref (action);
258 
259 		gtk_ui_manager_add_ui (
260 			ui_manager, merge_id,
261 			"/context/custom-actions-2/format-as-menu/format-as-other-menu",
262 			languages[i].action_name, languages[i].action_name,
263 			GTK_UI_MANAGER_AUTO, FALSE);
264 
265 		gtk_ui_manager_add_ui (
266 			shell_ui_manager, shell_merge_id,
267 			"/mail-preview-popup/mail-preview-popup-actions/format-as-menu/format-as-other-menu",
268 			languages[i].action_name, languages[i].action_name,
269 			GTK_UI_MANAGER_AUTO, FALSE);
270 	}
271 
272 	return group;
273 }
274 
275 static void
276 update_actions (EMailDisplayPopupExtension *extension,
277                 WebKitHitTestResult *context)
278 {
279 	EMailDisplayPopupTextHighlight *th_extension;
280 	WebKitDOMNode *node;
281 	WebKitDOMDocument *document;
282 	gchar *uri;
283 
284 	th_extension = E_MAIL_DISPLAY_POPUP_TEXT_HIGHLIGHT (extension);
285 
286 	if (th_extension->action_group == NULL) {
287 		th_extension->action_group = create_group (extension);
288 	}
289 
290 	th_extension->document = NULL;
291 	g_object_get (G_OBJECT (context), "inner-node", &node, NULL);
292 	document = webkit_dom_node_get_owner_document (node);
293 	uri = webkit_dom_document_get_document_uri (document);
294 
295 	/* If the part below context menu was made by text-highlight formatter,
296 	 * then try to check what formatter it's using at the moment and set
297 	 * it as active in the popup menu */
298 	if (uri && strstr (uri, ".text-highlight") != NULL) {
299 		SoupURI *soup_uri;
300 		gtk_action_group_set_visible (
301 			th_extension->action_group, TRUE);
302 
303 		soup_uri = soup_uri_new (uri);
304 		if (soup_uri && soup_uri->query) {
305 			GHashTable *query = soup_form_decode (soup_uri->query);
306 			gchar *highlighter;
307 
308 			highlighter = g_hash_table_lookup (query, "__formatas");
309 			if (highlighter && *highlighter) {
310 				GtkAction *action = gtk_action_group_get_action (
311 					th_extension->action_group, highlighter);
312 				if (action) {
313 					gint value;
314 					g_object_get (
315 						G_OBJECT (action), "value",
316 						&value, NULL);
317 					gtk_radio_action_set_current_value (
318 						GTK_RADIO_ACTION (action), value);
319 				}
320 			}
321 			g_hash_table_destroy (query);
322 		}
323 
324 		if (soup_uri) {
325 			soup_uri_free (soup_uri);
326 		}
327 
328 	} else {
329 		gtk_action_group_set_visible (
330 			th_extension->action_group, FALSE);
331 	}
332 
333 	/* Set the th_extension->document AFTER changing the active action to
334 	 * prevent the reformat() from doing some crazy reformatting
335 	 * (reformat() returns immediatelly when th_extension->document is NULL) */
336 	th_extension->document = document;
337 
338 	g_free (uri);
339 }
340 
341 void
342 e_mail_display_popup_text_highlight_type_register (GTypeModule *type_module)
343 {
344 	e_mail_display_popup_text_highlight_register_type (type_module);
345 }
346 
347 static void
348 e_mail_display_popup_text_highlight_class_init (EMailDisplayPopupTextHighlightClass *klass)
349 {
350 	EExtensionClass *extension_class;
351 
352 	e_mail_display_popup_text_highlight_parent_class = g_type_class_peek_parent (klass);
353 
354 	extension_class = E_EXTENSION_CLASS (klass);
355 	extension_class->extensible_type = E_TYPE_MAIL_DISPLAY;
356 }
357 
358 static void
359 e_mail_display_popup_extension_interface_init (EMailDisplayPopupExtensionInterface *iface)
360 {
361 	iface->update_actions = update_actions;
362 }
363 
364 void
365 e_mail_display_popup_text_highlight_class_finalize (EMailDisplayPopupTextHighlightClass *klass)
366 {
367 
368 }
369 
370 static void
371 e_mail_display_popup_text_highlight_init (EMailDisplayPopupTextHighlight *extension)
372 {
373 	extension->action_group = NULL;
374 }