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.
20 #include <glib/gi18n.h>
21 #include <glib/gstdio.h>
22 #include <libgnomevfs/gnome-vfs.h>
23 #include <libgnomevfs/gnome-vfs-mime-handlers.h>
24 #include <libgnomevfs/gnome-vfs-mime-utils.h>
25 #include "ev-file-helpers.h"
26 #include "ev-attachment.h"
39 struct _EvAttachmentPrivate {
48 GnomeVFSMimeApplication *app;
52 #define EV_ATTACHMENT_GET_PRIVATE(object) \
53 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_ATTACHMENT, EvAttachmentPrivate))
55 G_DEFINE_TYPE (EvAttachment, ev_attachment, G_TYPE_OBJECT)
58 ev_attachment_error_quark (void)
60 static GQuark error_quark = 0;
64 g_quark_from_static_string ("ev-attachment-error-quark");
70 ev_attachment_finalize (GObject *object)
72 EvAttachment *attachment = EV_ATTACHMENT (object);
74 if (attachment->priv->name) {
75 g_free (attachment->priv->name);
76 attachment->priv->name = NULL;
79 if (attachment->priv->description) {
80 g_free (attachment->priv->description);
81 attachment->priv->description = NULL;
84 if (attachment->priv->data) {
85 g_free (attachment->priv->data);
86 attachment->priv->data = NULL;
89 if (attachment->priv->mime_type) {
90 g_free (attachment->priv->mime_type);
91 attachment->priv->mime_type = NULL;
94 if (attachment->priv->app) {
95 gnome_vfs_mime_application_free (attachment->priv->app);
96 attachment->priv->app = NULL;
99 if (attachment->priv->tmp_uri) {
100 g_unlink (attachment->priv->tmp_uri);
101 g_free (attachment->priv->tmp_uri);
102 attachment->priv->tmp_uri = NULL;
105 (* G_OBJECT_CLASS (ev_attachment_parent_class)->finalize) (object);
109 ev_attachment_set_property (GObject *object,
112 GParamSpec *param_spec)
114 EvAttachment *attachment = EV_ATTACHMENT (object);
118 attachment->priv->name = g_value_dup_string (value);
120 case PROP_DESCRIPTION:
121 attachment->priv->description = g_value_dup_string (value);
124 attachment->priv->mtime = g_value_get_ulong (value);
127 attachment->priv->ctime = g_value_get_ulong (value);
130 attachment->priv->size = g_value_get_uint (value);
133 attachment->priv->data = g_value_get_pointer (value);
134 attachment->priv->mime_type =
135 g_strdup (gnome_vfs_get_mime_type_for_data (attachment->priv->data,
136 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_uri = 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 GnomeVFSHandle *handle = NULL;
290 GnomeVFSFileSize written;
291 GnomeVFSResult result;
293 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
294 g_return_val_if_fail (uri != NULL, FALSE);
296 result = gnome_vfs_create (&handle, uri,
297 GNOME_VFS_OPEN_WRITE,
299 if (result != GNOME_VFS_OK) {
303 _("Couldn't save attachment “%s”: %s"),
305 gnome_vfs_result_to_string (result));
310 result = gnome_vfs_write (handle, attachment->priv->data,
311 attachment->priv->size, &written);
312 if (result != GNOME_VFS_OK || written < attachment->priv->size){
316 _("Couldn't save attachment “%s”: %s"),
318 gnome_vfs_result_to_string (result));
320 gnome_vfs_close (handle);
325 gnome_vfs_close (handle);
331 ev_attachment_launch_app (EvAttachment *attachment,
334 GnomeVFSResult result;
337 g_assert (attachment->priv->tmp_uri != NULL);
338 g_assert (attachment->priv->app != NULL);
340 uris = g_list_prepend (uris, attachment->priv->tmp_uri);
341 result = gnome_vfs_mime_application_launch (attachment->priv->app,
344 if (result != GNOME_VFS_OK) {
348 _("Couldn't open attachment “%s”: %s"),
349 attachment->priv->name,
350 gnome_vfs_result_to_string (result));
363 ev_attachment_open (EvAttachment *attachment,
367 gboolean retval = FALSE;
368 GnomeVFSMimeApplication *default_app = NULL;
370 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
372 if (!attachment->priv->app) {
373 default_app = gnome_vfs_mime_get_default_application (attachment->priv->mime_type);
374 attachment->priv->app = default_app;
377 if (!attachment->priv->app) {
381 _("Couldn't open attachment “%s”"),
382 attachment->priv->name);
387 if (attachment->priv->tmp_uri &&
388 g_file_test (attachment->priv->tmp_uri, G_FILE_TEST_EXISTS)) {
389 retval = ev_attachment_launch_app (attachment, error);
391 gchar *uri, *filename;
393 filename = g_build_filename (ev_tmp_dir (), attachment->priv->name, NULL);
394 uri = g_filename_to_uri (filename, NULL, NULL);
396 if (ev_attachment_save (attachment, uri, error)) {
397 if (attachment->priv->tmp_uri)
398 g_free (attachment->priv->tmp_uri);
399 attachment->priv->tmp_uri = g_strdup (filename);
401 retval = ev_attachment_launch_app (attachment, error);