1 /* this file is part of evince, a gnome document viewer
3 * Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
5 * Evince is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * Evince is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
21 #include <glib/gi18n-lib.h>
22 #include <glib/gstdio.h>
24 #include "ev-file-helpers.h"
25 #include "ev-attachment.h"
38 struct _EvAttachmentPrivate {
51 #define EV_ATTACHMENT_GET_PRIVATE(object) \
52 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_ATTACHMENT, EvAttachmentPrivate))
54 G_DEFINE_TYPE (EvAttachment, ev_attachment, G_TYPE_OBJECT)
57 ev_attachment_error_quark (void)
59 static GQuark error_quark = 0;
63 g_quark_from_static_string ("ev-attachment-error-quark");
69 ev_attachment_finalize (GObject *object)
71 EvAttachment *attachment = EV_ATTACHMENT (object);
73 if (attachment->priv->name) {
74 g_free (attachment->priv->name);
75 attachment->priv->name = NULL;
78 if (attachment->priv->description) {
79 g_free (attachment->priv->description);
80 attachment->priv->description = NULL;
83 if (attachment->priv->data) {
84 g_free (attachment->priv->data);
85 attachment->priv->data = NULL;
88 if (attachment->priv->mime_type) {
89 g_free (attachment->priv->mime_type);
90 attachment->priv->mime_type = NULL;
93 if (attachment->priv->app) {
94 g_object_unref (attachment->priv->app);
95 attachment->priv->app = NULL;
98 if (attachment->priv->tmp_file) {
99 ev_tmp_file_unlink (attachment->priv->tmp_file);
100 g_object_unref (attachment->priv->tmp_file);
101 attachment->priv->tmp_file = NULL;
104 (* G_OBJECT_CLASS (ev_attachment_parent_class)->finalize) (object);
108 ev_attachment_set_property (GObject *object,
111 GParamSpec *param_spec)
113 EvAttachment *attachment = EV_ATTACHMENT (object);
117 attachment->priv->name = g_value_dup_string (value);
119 case PROP_DESCRIPTION:
120 attachment->priv->description = g_value_dup_string (value);
123 attachment->priv->mtime = g_value_get_ulong (value);
126 attachment->priv->ctime = g_value_get_ulong (value);
129 attachment->priv->size = g_value_get_uint (value);
132 attachment->priv->data = g_value_get_pointer (value);
133 attachment->priv->mime_type = g_content_type_guess (attachment->priv->name,
134 (guchar *) attachment->priv->data,
135 attachment->priv->size,
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
147 ev_attachment_class_init (EvAttachmentClass *klass)
149 GObjectClass *g_object_class;
151 g_object_class = G_OBJECT_CLASS (klass);
153 g_object_class->set_property = ev_attachment_set_property;
155 g_type_class_add_private (g_object_class, sizeof (EvAttachmentPrivate));
158 g_object_class_install_property (g_object_class,
160 g_param_spec_string ("name",
162 "The attachment name",
165 G_PARAM_CONSTRUCT_ONLY));
166 g_object_class_install_property (g_object_class,
168 g_param_spec_string ("description",
170 "The attachment description",
173 G_PARAM_CONSTRUCT_ONLY));
174 g_object_class_install_property (g_object_class,
176 g_param_spec_ulong ("mtime",
178 "The attachment modification date",
181 G_PARAM_CONSTRUCT_ONLY));
182 g_object_class_install_property (g_object_class,
184 g_param_spec_ulong ("ctime",
186 "The attachment creation date",
189 G_PARAM_CONSTRUCT_ONLY));
190 g_object_class_install_property (g_object_class,
192 g_param_spec_uint ("size",
194 "The attachment size",
197 G_PARAM_CONSTRUCT_ONLY));
198 g_object_class_install_property (g_object_class,
200 g_param_spec_pointer ("data",
202 "The attachment data",
204 G_PARAM_CONSTRUCT_ONLY));
206 g_object_class->finalize = ev_attachment_finalize;
210 ev_attachment_init (EvAttachment *attachment)
212 attachment->priv = EV_ATTACHMENT_GET_PRIVATE (attachment);
214 attachment->priv->name = NULL;
215 attachment->priv->description = NULL;
216 attachment->priv->data = NULL;
217 attachment->priv->mime_type = NULL;
219 attachment->priv->tmp_file = NULL;
223 ev_attachment_new (const gchar *name,
224 const gchar *description,
230 EvAttachment *attachment;
232 attachment = g_object_new (EV_TYPE_ATTACHMENT,
234 "description", description,
245 ev_attachment_get_name (EvAttachment *attachment)
247 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
249 return attachment->priv->name;
253 ev_attachment_get_description (EvAttachment *attachment)
255 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
257 return attachment->priv->description;
261 ev_attachment_get_modification_date (EvAttachment *attachment)
263 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
265 return attachment->priv->mtime;
269 ev_attachment_get_creation_date (EvAttachment *attachment)
271 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
273 return attachment->priv->ctime;
277 ev_attachment_get_mime_type (EvAttachment *attachment)
279 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
281 return attachment->priv->mime_type;
285 ev_attachment_save (EvAttachment *attachment,
289 GFileOutputStream *output_stream;
290 GError *ioerror = NULL;
291 gssize written_bytes;
293 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
294 g_return_val_if_fail (G_IS_FILE (file), FALSE);
296 output_stream = g_file_create (file, 0, NULL, &ioerror);
297 if (output_stream == NULL) {
300 uri = g_file_get_uri (file);
304 _("Couldn't save attachment “%s”: %s"),
308 g_error_free (ioerror);
314 written_bytes = g_output_stream_write (G_OUTPUT_STREAM (output_stream),
315 attachment->priv->data,
316 attachment->priv->size,
318 if (written_bytes == -1) {
321 uri = g_file_get_uri (file);
325 _("Couldn't save attachment “%s”: %s"),
329 g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);
330 g_error_free (ioerror);
336 g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);
343 ev_attachment_launch_app (EvAttachment *attachment,
350 GAppLaunchContext *context = NULL;
351 GError *ioerror = NULL;
353 g_assert (G_IS_FILE (attachment->priv->tmp_file));
354 g_assert (G_IS_APP_INFO (attachment->priv->app));
356 files = g_list_prepend (files, attachment->priv->tmp_file);
358 #if GTK_CHECK_VERSION (2, 14, 0)
359 context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
360 gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context), screen);
361 gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context), timestamp);
363 result = g_app_info_launch (attachment->priv->app, files,
367 g_object_unref (context);
373 _("Couldn't open attachment “%s”: %s"),
374 attachment->priv->name,
378 g_error_free (ioerror);
389 ev_attachment_open (EvAttachment *attachment,
395 gboolean retval = FALSE;
397 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
399 if (!attachment->priv->app) {
400 app_info = g_app_info_get_default_for_type (attachment->priv->mime_type, TRUE);
401 attachment->priv->app = app_info;
404 if (!attachment->priv->app) {
408 _("Couldn't open attachment “%s”"),
409 attachment->priv->name);
414 if (attachment->priv->tmp_file &&
415 g_file_query_exists (attachment->priv->tmp_file, NULL)) {
416 retval = ev_attachment_launch_app (attachment, screen,
422 tmpdir = g_file_new_for_path (ev_tmp_dir ());
423 file = g_file_get_child (tmpdir, attachment->priv->name);
425 if (ev_attachment_save (attachment, file, error)) {
426 if (attachment->priv->tmp_file)
427 g_object_unref (attachment->priv->tmp_file);
428 attachment->priv->tmp_file = g_object_ref (file);
430 retval = ev_attachment_launch_app (attachment, screen,
434 g_object_unref (file);
435 g_object_unref (tmpdir);