evolution-3.6.4/widgets/misc/e-attachment-handler-sendto.c

No issues found

  1 /*
  2  * e-attachment-handler-sendto.c
  3  *
  4  * Copyright (C) 2009 Matthew Barnes
  5  *
  6  * This program is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU Lesser General Public
  8  * License as published by the Free Software Foundation; either
  9  * version 2 of the License, or (at your option) any later version.
 10  *
 11  * This program is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Lesser General Public
 17  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 18  *
 19  */
 20 
 21 #ifdef HAVE_CONFIG_H
 22 #include <config.h>
 23 #endif
 24 
 25 #include "e-attachment-handler-sendto.h"
 26 
 27 #include <errno.h>
 28 
 29 #include <glib/gi18n-lib.h>
 30 
 31 static const gchar *ui =
 32 "<ui>"
 33 "  <popup name='context'>"
 34 "    <placeholder name='custom-actions'>"
 35 "      <menuitem action='sendto'/>"
 36 "    </placeholder>"
 37 "  </popup>"
 38 "</ui>";
 39 
 40 G_DEFINE_TYPE (
 41 	EAttachmentHandlerSendto,
 42 	e_attachment_handler_sendto,
 43 	E_TYPE_ATTACHMENT_HANDLER)
 44 
 45 static void
 46 sendto_save_finished_cb (EAttachment *attachment,
 47                          GAsyncResult *result,
 48                          EAttachmentHandler *handler)
 49 {
 50 	EAttachmentView *view;
 51 	EAttachmentStore *store;
 52 	GtkWidget *dialog;
 53 	gchar **uris;
 54 	gpointer parent;
 55 	gchar *arguments;
 56 	gchar *command_line;
 57 	guint n_uris = 1;
 58 	GError *error = NULL;
 59 
 60 	view = e_attachment_handler_get_view (handler);
 61 	store = e_attachment_view_get_store (view);
 62 
 63 	uris = e_attachment_store_get_uris_finish (store, result, &error);
 64 
 65 	if (uris != NULL)
 66 		n_uris = g_strv_length (uris);
 67 
 68 	if (error != NULL)
 69 		goto error;
 70 
 71 	arguments = g_strjoinv (" ", uris);
 72 	command_line = g_strdup_printf ("nautilus-sendto %s", arguments);
 73 
 74 	g_message ("Command: %s", command_line);
 75 	g_spawn_command_line_async (command_line, &error);
 76 
 77 	g_free (command_line);
 78 	g_free (arguments);
 79 
 80 	if (error != NULL)
 81 		goto error;
 82 
 83 	goto exit;
 84 
 85 error:
 86 	parent = gtk_widget_get_toplevel (GTK_WIDGET (view));
 87 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
 88 
 89 	dialog = gtk_message_dialog_new_with_markup (
 90 		parent, GTK_DIALOG_DESTROY_WITH_PARENT,
 91 		GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
 92 		"<big><b>%s</b></big>",
 93 		ngettext ("Could not send attachment",
 94 		"Could not send attachments", n_uris));
 95 
 96 	gtk_message_dialog_format_secondary_text (
 97 		GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
 98 
 99 	gtk_dialog_run (GTK_DIALOG (dialog));
100 
101 	gtk_widget_destroy (dialog);
102 	g_error_free (error);
103 
104 exit:
105 	g_object_unref (handler);
106 	g_strfreev (uris);
107 }
108 
109 static void
110 action_sendto_cb (GtkAction *action,
111                   EAttachmentHandler *handler)
112 {
113 	EAttachmentView *view;
114 	EAttachmentStore *store;
115 	GList *selected;
116 
117 	view = e_attachment_handler_get_view (handler);
118 	store = e_attachment_view_get_store (view);
119 
120 	selected = e_attachment_view_get_selected_attachments (view);
121 	g_return_if_fail (selected != NULL);
122 
123 	e_attachment_store_get_uris_async (
124 		store, selected, (GAsyncReadyCallback)
125 		sendto_save_finished_cb, g_object_ref (handler));
126 
127 	g_list_foreach (selected, (GFunc) g_object_unref, NULL);
128 	g_list_free (selected);
129 }
130 
131 static GtkActionEntry standard_entries[] = {
132 
133 	{ "sendto",
134 	  "document-send",
135 	  N_("_Send To..."),
136 	  NULL,
137 	  N_("Send the selected attachments somewhere"),
138 	  G_CALLBACK (action_sendto_cb) }
139 };
140 
141 static void
142 attachment_handler_sendto_update_actions_cb (EAttachmentView *view,
143                                              EAttachmentHandler *handler)
144 {
145 	GtkActionGroup *action_group;
146 	GList *selected, *iter;
147 	gboolean visible = FALSE;
148 	gchar *program;
149 
150 	program = g_find_program_in_path ("nautilus-sendto");
151 	selected = e_attachment_view_get_selected_attachments (view);
152 
153 	if (program == NULL || selected == NULL)
154 		goto exit;
155 
156 	/* Make sure no file transfers are in progress. */
157 	for (iter = selected; iter != NULL; iter = iter->next) {
158 		EAttachment *attachment = iter->data;
159 
160 		if (e_attachment_get_loading (attachment))
161 			goto exit;
162 
163 		if (e_attachment_get_saving (attachment))
164 			goto exit;
165 	}
166 
167 	visible = TRUE;
168 
169 exit:
170 	action_group = e_attachment_view_get_action_group (view, "sendto");
171 	gtk_action_group_set_visible (action_group, visible);
172 
173 	g_list_foreach (selected, (GFunc) g_object_unref, NULL);
174 	g_list_free (selected);
175 
176 	g_free (program);
177 }
178 
179 static void
180 attachment_handler_sendto_constructed (GObject *object)
181 {
182 	EAttachmentHandler *handler;
183 	EAttachmentView *view;
184 	GtkActionGroup *action_group;
185 	GtkUIManager *ui_manager;
186 	GError *error = NULL;
187 
188 	handler = E_ATTACHMENT_HANDLER (object);
189 
190 	/* Chain up to parent's constructed() method. */
191 	G_OBJECT_CLASS (e_attachment_handler_sendto_parent_class)->constructed (object);
192 
193 	view = e_attachment_handler_get_view (handler);
194 	ui_manager = e_attachment_view_get_ui_manager (view);
195 
196 	action_group = gtk_action_group_new ("sendto");
197 	gtk_action_group_set_translation_domain (
198 		action_group, GETTEXT_PACKAGE);
199 	gtk_action_group_add_actions (
200 		action_group, standard_entries,
201 		G_N_ELEMENTS (standard_entries), object);
202 	gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
203 
204 	gtk_ui_manager_add_ui_from_string (ui_manager, ui, -1, &error);
205 
206 	if (error != NULL) {
207 		g_warning ("%s", error->message);
208 		g_error_free (error);
209 	}
210 
211 	g_signal_connect (
212 		view, "update-actions",
213 		G_CALLBACK (attachment_handler_sendto_update_actions_cb),
214 		object);
215 }
216 
217 static void
218 e_attachment_handler_sendto_class_init (EAttachmentHandlerSendtoClass *class)
219 {
220 	GObjectClass *object_class;
221 
222 	object_class = G_OBJECT_CLASS (class);
223 	object_class->constructed = attachment_handler_sendto_constructed;
224 }
225 
226 static void
227 e_attachment_handler_sendto_init (EAttachmentHandlerSendto *handler)
228 {
229 }