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.h>
22 #include <glib/gstdio.h>
23 #include <libgnomevfs/gnome-vfs.h>
24 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
25 #include <libgnomevfs/gnome-vfs-mime-utils.h>
26 #include "ev-file-helpers.h"
27 #include "ev-attachment.h"
40 struct _EvAttachmentPrivate {
49 GnomeVFSMimeApplication *app;
53 #define EV_ATTACHMENT_GET_PRIVATE(object) \
54 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_ATTACHMENT, EvAttachmentPrivate))
56 G_DEFINE_TYPE (EvAttachment, ev_attachment, G_TYPE_OBJECT)
59 ev_attachment_error_quark (void)
61 static GQuark error_quark = 0;
65 g_quark_from_static_string ("ev-attachment-error-quark");
71 ev_attachment_finalize (GObject *object)
73 EvAttachment *attachment = EV_ATTACHMENT (object);
75 if (attachment->priv->name) {
76 g_free (attachment->priv->name);
77 attachment->priv->name = NULL;
80 if (attachment->priv->description) {
81 g_free (attachment->priv->description);
82 attachment->priv->description = NULL;
85 if (attachment->priv->data) {
86 g_free (attachment->priv->data);
87 attachment->priv->data = NULL;
90 if (attachment->priv->mime_type) {
91 g_free (attachment->priv->mime_type);
92 attachment->priv->mime_type = NULL;
95 if (attachment->priv->app) {
96 gnome_vfs_mime_application_free (attachment->priv->app);
97 attachment->priv->app = NULL;
100 if (attachment->priv->tmp_uri) {
101 ev_tmp_filename_unlink (attachment->priv->tmp_uri);
102 g_free (attachment->priv->tmp_uri);
103 attachment->priv->tmp_uri = NULL;
106 (* G_OBJECT_CLASS (ev_attachment_parent_class)->finalize) (object);
110 ev_attachment_set_property (GObject *object,
113 GParamSpec *param_spec)
115 EvAttachment *attachment = EV_ATTACHMENT (object);
119 attachment->priv->name = g_value_dup_string (value);
121 case PROP_DESCRIPTION:
122 attachment->priv->description = g_value_dup_string (value);
125 attachment->priv->mtime = g_value_get_ulong (value);
128 attachment->priv->ctime = g_value_get_ulong (value);
131 attachment->priv->size = g_value_get_uint (value);
134 attachment->priv->data = g_value_get_pointer (value);
135 attachment->priv->mime_type =
136 g_strdup (gnome_vfs_get_mime_type_for_data (attachment->priv->data,
137 attachment->priv->size));
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
148 ev_attachment_class_init (EvAttachmentClass *klass)
150 GObjectClass *g_object_class;
152 g_object_class = G_OBJECT_CLASS (klass);
154 g_object_class->set_property = ev_attachment_set_property;
156 g_type_class_add_private (g_object_class, sizeof (EvAttachmentPrivate));
159 g_object_class_install_property (g_object_class,
161 g_param_spec_string ("name",
163 "The attachment name",
166 G_PARAM_CONSTRUCT_ONLY));
167 g_object_class_install_property (g_object_class,
169 g_param_spec_string ("description",
171 "The attachment description",
174 G_PARAM_CONSTRUCT_ONLY));
175 g_object_class_install_property (g_object_class,
177 g_param_spec_ulong ("mtime",
179 "The attachment modification date",
182 G_PARAM_CONSTRUCT_ONLY));
183 g_object_class_install_property (g_object_class,
185 g_param_spec_ulong ("ctime",
187 "The attachment creation date",
190 G_PARAM_CONSTRUCT_ONLY));
191 g_object_class_install_property (g_object_class,
193 g_param_spec_uint ("size",
195 "The attachment size",
198 G_PARAM_CONSTRUCT_ONLY));
199 g_object_class_install_property (g_object_class,
201 g_param_spec_pointer ("data",
203 "The attachment data",
205 G_PARAM_CONSTRUCT_ONLY));
207 g_object_class->finalize = ev_attachment_finalize;
211 ev_attachment_init (EvAttachment *attachment)
213 attachment->priv = EV_ATTACHMENT_GET_PRIVATE (attachment);
215 attachment->priv->name = NULL;
216 attachment->priv->description = NULL;
217 attachment->priv->data = NULL;
218 attachment->priv->mime_type = NULL;
220 attachment->priv->tmp_uri = NULL;
224 ev_attachment_new (const gchar *name,
225 const gchar *description,
231 EvAttachment *attachment;
233 attachment = g_object_new (EV_TYPE_ATTACHMENT,
235 "description", description,
246 ev_attachment_get_name (EvAttachment *attachment)
248 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
250 return attachment->priv->name;
254 ev_attachment_get_description (EvAttachment *attachment)
256 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
258 return attachment->priv->description;
262 ev_attachment_get_modification_date (EvAttachment *attachment)
264 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
266 return attachment->priv->mtime;
270 ev_attachment_get_creation_date (EvAttachment *attachment)
272 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
274 return attachment->priv->ctime;
278 ev_attachment_get_mime_type (EvAttachment *attachment)
280 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
282 return attachment->priv->mime_type;
286 ev_attachment_save (EvAttachment *attachment,
290 GnomeVFSHandle *handle = NULL;
291 GnomeVFSFileSize written;
292 GnomeVFSResult result;
294 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
295 g_return_val_if_fail (uri != NULL, FALSE);
297 result = gnome_vfs_create (&handle, uri,
298 GNOME_VFS_OPEN_WRITE,
300 if (result != GNOME_VFS_OK) {
304 _("Couldn't save attachment “%s”: %s"),
306 gnome_vfs_result_to_string (result));
311 result = gnome_vfs_write (handle, attachment->priv->data,
312 attachment->priv->size, &written);
313 if (result != GNOME_VFS_OK || written < attachment->priv->size){
317 _("Couldn't save attachment “%s”: %s"),
319 gnome_vfs_result_to_string (result));
321 gnome_vfs_close (handle);
326 gnome_vfs_close (handle);
332 ev_attachment_launch_app (EvAttachment *attachment,
335 GnomeVFSResult result;
338 g_assert (attachment->priv->tmp_uri != NULL);
339 g_assert (attachment->priv->app != NULL);
341 uris = g_list_prepend (uris, attachment->priv->tmp_uri);
342 result = gnome_vfs_mime_application_launch (attachment->priv->app,
345 if (result != GNOME_VFS_OK) {
349 _("Couldn't open attachment “%s”: %s"),
350 attachment->priv->name,
351 gnome_vfs_result_to_string (result));
364 ev_attachment_open (EvAttachment *attachment,
368 gboolean retval = FALSE;
369 GnomeVFSMimeApplication *default_app = NULL;
371 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
373 if (!attachment->priv->app) {
374 default_app = gnome_vfs_mime_get_default_application (attachment->priv->mime_type);
375 attachment->priv->app = default_app;
378 if (!attachment->priv->app) {
382 _("Couldn't open attachment “%s”"),
383 attachment->priv->name);
388 if (attachment->priv->tmp_uri &&
389 g_file_test (attachment->priv->tmp_uri, G_FILE_TEST_EXISTS)) {
390 retval = ev_attachment_launch_app (attachment, error);
392 gchar *uri, *filename;
394 filename = g_build_filename (ev_tmp_dir (), attachment->priv->name, NULL);
395 uri = g_filename_to_uri (filename, NULL, NULL);
397 if (ev_attachment_save (attachment, uri, error)) {
398 if (attachment->priv->tmp_uri)
399 g_free (attachment->priv->tmp_uri);
400 attachment->priv->tmp_uri = g_strdup (filename);
402 retval = ev_attachment_launch_app (attachment, error);