No issues found
1 /*
2 * e-attachment-handler-image.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 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
19 *
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "e-attachment-handler-image.h"
27
28 #include <glib/gi18n.h>
29 #include <gdesktop-enums.h>
30
31 #include <e-util/e-util.h>
32
33 #define E_ATTACHMENT_HANDLER_IMAGE_GET_PRIVATE(obj) \
34 (G_TYPE_INSTANCE_GET_PRIVATE \
35 ((obj), E_TYPE_ATTACHMENT_HANDLER_IMAGE, EAttachmentHandlerImagePrivate))
36
37 struct _EAttachmentHandlerImagePrivate {
38 gint placeholder;
39 };
40
41 static const gchar *ui =
42 "<ui>"
43 " <popup name='context'>"
44 " <placeholder name='custom-actions'>"
45 " <menuitem action='image-set-as-background'/>"
46 " </placeholder>"
47 " </popup>"
48 "</ui>";
49
50 G_DEFINE_TYPE (
51 EAttachmentHandlerImage,
52 e_attachment_handler_image,
53 E_TYPE_ATTACHMENT_HANDLER)
54
55 static void
56 action_image_set_as_background_saved_cb (EAttachment *attachment,
57 GAsyncResult *result,
58 EAttachmentHandler *handler)
59 {
60 GDesktopBackgroundStyle style;
61 EAttachmentView *view;
62 GSettings *settings;
63 GtkWidget *dialog;
64 GFile *file;
65 gpointer parent;
66 gchar *uri;
67 GError *error = NULL;
68
69 view = e_attachment_handler_get_view (handler);
70 settings = g_settings_new ("org.gnome.desktop.background");
71
72 file = e_attachment_save_finish (attachment, result, &error);
73
74 if (error != NULL)
75 goto error;
76
77 uri = g_file_get_uri (file);
78 g_settings_set_string (settings, "picture-uri", uri);
79 g_free (uri);
80
81 style = g_settings_get_enum (settings, "picture-options");
82 if (style == G_DESKTOP_BACKGROUND_STYLE_NONE)
83 g_settings_set_enum (
84 settings, "picture-options",
85 G_DESKTOP_BACKGROUND_STYLE_WALLPAPER);
86
87 g_object_unref (file);
88
89 goto exit;
90
91 error:
92 parent = gtk_widget_get_toplevel (GTK_WIDGET (view));
93 parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
94
95 dialog = gtk_message_dialog_new_with_markup (
96 parent, GTK_DIALOG_DESTROY_WITH_PARENT,
97 GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
98 "<big><b>%s</b></big>",
99 _("Could not set as background"));
100
101 gtk_message_dialog_format_secondary_text (
102 GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
103
104 gtk_dialog_run (GTK_DIALOG (dialog));
105
106 gtk_widget_destroy (dialog);
107 g_error_free (error);
108
109 exit:
110 g_object_unref (settings);
111 g_object_unref (handler);
112 }
113
114 static void
115 action_image_set_as_background_cb (GtkAction *action,
116 EAttachmentHandler *handler)
117 {
118 EAttachmentView *view;
119 EAttachment *attachment;
120 GFile *destination;
121 GList *selected;
122 const gchar *path;
123
124 view = e_attachment_handler_get_view (handler);
125 selected = e_attachment_view_get_selected_attachments (view);
126 g_return_if_fail (g_list_length (selected) == 1);
127 attachment = E_ATTACHMENT (selected->data);
128
129 /* Save the image under the user's Pictures directory. */
130 path = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
131 destination = g_file_new_for_path (path);
132 g_mkdir_with_parents (path, 0755);
133
134 e_attachment_save_async (
135 attachment, destination, (GAsyncReadyCallback)
136 action_image_set_as_background_saved_cb,
137 g_object_ref (handler));
138
139 g_object_unref (destination);
140
141 g_list_foreach (selected, (GFunc) g_object_unref, NULL);
142 g_list_free (selected);
143 }
144
145 static GtkActionEntry standard_entries[] = {
146
147 { "image-set-as-background",
148 NULL,
149 N_("Set as _Background"),
150 NULL,
151 NULL, /* XXX Add a tooltip! */
152 G_CALLBACK (action_image_set_as_background_cb) }
153 };
154
155 static void
156 attachment_handler_image_update_actions_cb (EAttachmentView *view,
157 EAttachmentHandler *handler)
158 {
159 EAttachment *attachment;
160 GFileInfo *file_info;
161 GtkActionGroup *action_group;
162 const gchar *content_type;
163 gchar *mime_type;
164 GList *selected;
165 gboolean visible = FALSE;
166
167 selected = e_attachment_view_get_selected_attachments (view);
168
169 if (g_list_length (selected) != 1)
170 goto exit;
171
172 attachment = E_ATTACHMENT (selected->data);
173 file_info = e_attachment_get_file_info (attachment);
174
175 if (file_info == NULL)
176 goto exit;
177
178 if (e_attachment_get_loading (attachment))
179 goto exit;
180
181 if (e_attachment_get_saving (attachment))
182 goto exit;
183
184 content_type = g_file_info_get_content_type (file_info);
185
186 mime_type = g_content_type_get_mime_type (content_type);
187 visible = (g_ascii_strncasecmp (mime_type, "image/", 6) == 0);
188 g_free (mime_type);
189
190 exit:
191 action_group = e_attachment_view_get_action_group (view, "image");
192 gtk_action_group_set_visible (action_group, visible);
193
194 g_list_foreach (selected, (GFunc) g_object_unref, NULL);
195 g_list_free (selected);
196 }
197
198 static void
199 attachment_handler_image_constructed (GObject *object)
200 {
201 EAttachmentHandler *handler;
202 EAttachmentView *view;
203 GtkActionGroup *action_group;
204 GtkUIManager *ui_manager;
205 GError *error = NULL;
206
207 handler = E_ATTACHMENT_HANDLER (object);
208
209 /* Chain up to parent's constructed() method. */
210 G_OBJECT_CLASS (e_attachment_handler_image_parent_class)->constructed (object);
211
212 view = e_attachment_handler_get_view (handler);
213
214 action_group = e_attachment_view_add_action_group (view, "image");
215 gtk_action_group_add_actions (
216 action_group, standard_entries,
217 G_N_ELEMENTS (standard_entries), object);
218
219 ui_manager = e_attachment_view_get_ui_manager (view);
220 gtk_ui_manager_add_ui_from_string (ui_manager, ui, -1, &error);
221
222 if (error != NULL) {
223 g_warning ("%s", error->message);
224 g_error_free (error);
225 }
226
227 g_signal_connect (
228 view, "update-actions",
229 G_CALLBACK (attachment_handler_image_update_actions_cb),
230 object);
231 }
232
233 static void
234 e_attachment_handler_image_class_init (EAttachmentHandlerImageClass *class)
235 {
236 GObjectClass *object_class;
237
238 g_type_class_add_private (class, sizeof (EAttachmentHandlerImagePrivate));
239
240 object_class = G_OBJECT_CLASS (class);
241 object_class->constructed = attachment_handler_image_constructed;
242 }
243
244 static void
245 e_attachment_handler_image_init (EAttachmentHandlerImage *handler)
246 {
247 handler->priv = E_ATTACHMENT_HANDLER_IMAGE_GET_PRIVATE (handler);
248 }