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-attachment.h"
38
struct _EvAttachmentPrivate {
47
GnomeVFSMimeApplication *app;
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
gnome_vfs_mime_application_free (attachment->priv->app);
95
attachment->priv->app = NULL;
98
if (attachment->priv->tmp_uri) {
99
g_unlink (attachment->priv->tmp_uri);
100
g_free (attachment->priv->tmp_uri);
101
attachment->priv->tmp_uri = 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 =
134
g_strdup (gnome_vfs_get_mime_type_for_data (attachment->priv->data,
135
attachment->priv->size));
138
G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
146
ev_attachment_class_init (EvAttachmentClass *klass)
148
GObjectClass *g_object_class;
150
g_object_class = G_OBJECT_CLASS (klass);
152
g_object_class->set_property = ev_attachment_set_property;
154
g_type_class_add_private (g_object_class, sizeof (EvAttachmentPrivate));
157
g_object_class_install_property (g_object_class,
159
g_param_spec_string ("name",
161
"The attachment name",
164
G_PARAM_CONSTRUCT_ONLY));
165
g_object_class_install_property (g_object_class,
167
g_param_spec_string ("description",
169
"The attachment description",
172
G_PARAM_CONSTRUCT_ONLY));
173
g_object_class_install_property (g_object_class,
175
g_param_spec_ulong ("mtime",
177
"The attachment modification date",
180
G_PARAM_CONSTRUCT_ONLY));
181
g_object_class_install_property (g_object_class,
183
g_param_spec_ulong ("ctime",
185
"The attachment creation date",
188
G_PARAM_CONSTRUCT_ONLY));
189
g_object_class_install_property (g_object_class,
191
g_param_spec_uint ("size",
193
"The attachment size",
196
G_PARAM_CONSTRUCT_ONLY));
197
g_object_class_install_property (g_object_class,
199
g_param_spec_pointer ("data",
201
"The attachment data",
203
G_PARAM_CONSTRUCT_ONLY));
205
g_object_class->finalize = ev_attachment_finalize;
209
ev_attachment_init (EvAttachment *attachment)
211
attachment->priv = EV_ATTACHMENT_GET_PRIVATE (attachment);
213
attachment->priv->name = NULL;
214
attachment->priv->description = NULL;
215
attachment->priv->data = NULL;
216
attachment->priv->mime_type = NULL;
218
attachment->priv->tmp_uri = NULL;
222
ev_attachment_new (const gchar *name,
223
const gchar *description,
229
EvAttachment *attachment;
231
attachment = g_object_new (EV_TYPE_ATTACHMENT,
233
"description", description,
244
ev_attachment_get_name (EvAttachment *attachment)
246
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
248
return attachment->priv->name;
252
ev_attachment_get_description (EvAttachment *attachment)
254
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
256
return attachment->priv->description;
260
ev_attachment_get_modification_date (EvAttachment *attachment)
262
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
264
return attachment->priv->mtime;
268
ev_attachment_get_creation_date (EvAttachment *attachment)
270
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
272
return attachment->priv->ctime;
276
ev_attachment_get_mime_type (EvAttachment *attachment)
278
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
280
return attachment->priv->mime_type;
284
ev_attachment_save (EvAttachment *attachment,
288
GnomeVFSHandle *handle = NULL;
289
GnomeVFSFileSize written;
290
GnomeVFSResult result;
292
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
293
g_return_val_if_fail (uri != NULL, FALSE);
295
result = gnome_vfs_create (&handle, uri,
296
GNOME_VFS_OPEN_WRITE |
297
GNOME_VFS_OPEN_TRUNCATE,
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 (g_get_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);