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 "ev-file-helpers.h"
24 #include "ev-attachment.h"
37 struct _EvAttachmentPrivate {
50 #define EV_ATTACHMENT_GET_PRIVATE(object) \
51 (G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_ATTACHMENT, EvAttachmentPrivate))
53 G_DEFINE_TYPE (EvAttachment, ev_attachment, G_TYPE_OBJECT)
56 ev_attachment_error_quark (void)
58 static GQuark error_quark = 0;
62 g_quark_from_static_string ("ev-attachment-error-quark");
68 ev_attachment_finalize (GObject *object)
70 EvAttachment *attachment = EV_ATTACHMENT (object);
72 if (attachment->priv->name) {
73 g_free (attachment->priv->name);
74 attachment->priv->name = NULL;
77 if (attachment->priv->description) {
78 g_free (attachment->priv->description);
79 attachment->priv->description = NULL;
82 if (attachment->priv->data) {
83 g_free (attachment->priv->data);
84 attachment->priv->data = NULL;
87 if (attachment->priv->mime_type) {
88 g_free (attachment->priv->mime_type);
89 attachment->priv->mime_type = NULL;
92 if (attachment->priv->app) {
93 g_object_unref (attachment->priv->app);
94 attachment->priv->app = NULL;
97 if (attachment->priv->tmp_file) {
98 ev_tmp_file_unlink (attachment->priv->tmp_file);
99 g_object_unref (attachment->priv->tmp_file);
100 attachment->priv->tmp_file = NULL;
103 (* G_OBJECT_CLASS (ev_attachment_parent_class)->finalize) (object);
107 ev_attachment_set_property (GObject *object,
110 GParamSpec *param_spec)
112 EvAttachment *attachment = EV_ATTACHMENT (object);
116 attachment->priv->name = g_value_dup_string (value);
118 case PROP_DESCRIPTION:
119 attachment->priv->description = g_value_dup_string (value);
122 attachment->priv->mtime = g_value_get_ulong (value);
125 attachment->priv->ctime = g_value_get_ulong (value);
128 attachment->priv->size = g_value_get_uint (value);
131 attachment->priv->data = g_value_get_pointer (value);
132 attachment->priv->mime_type = g_content_type_guess (attachment->priv->name,
133 (guchar *) attachment->priv->data,
134 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_file = 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 GFileOutputStream *output_stream;
289 GError *ioerror = NULL;
290 gssize written_bytes;
292 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
293 g_return_val_if_fail (G_IS_FILE (file), FALSE);
295 output_stream = g_file_create (file, 0, NULL, &ioerror);
296 if (output_stream == NULL) {
299 uri = g_file_get_uri (file);
303 _("Couldn't save attachment “%s”: %s"),
307 g_error_free (ioerror);
313 written_bytes = g_output_stream_write (G_OUTPUT_STREAM (output_stream),
314 attachment->priv->data,
315 attachment->priv->size,
317 if (written_bytes == -1) {
320 uri = g_file_get_uri (file);
324 _("Couldn't save attachment “%s”: %s"),
328 g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);
329 g_error_free (ioerror);
335 g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);
342 ev_attachment_launch_app (EvAttachment *attachment,
347 GError *ioerror = NULL;
349 g_assert (G_IS_FILE (attachment->priv->tmp_file));
350 g_assert (G_IS_APP_INFO (attachment->priv->app));
352 files = g_list_prepend (files, attachment->priv->tmp_file);
353 result = g_app_info_launch (attachment->priv->app, files,
360 _("Couldn't open attachment “%s”: %s"),
361 attachment->priv->name,
365 g_error_free (ioerror);
376 ev_attachment_open (EvAttachment *attachment,
380 gboolean retval = FALSE;
382 g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
384 if (!attachment->priv->app) {
385 app_info = g_app_info_get_default_for_type (attachment->priv->mime_type, TRUE);
386 attachment->priv->app = app_info;
389 if (!attachment->priv->app) {
393 _("Couldn't open attachment “%s”"),
394 attachment->priv->name);
399 if (attachment->priv->tmp_file &&
400 g_file_query_exists (attachment->priv->tmp_file, NULL)) {
401 retval = ev_attachment_launch_app (attachment, error);
406 tmpdir = g_file_new_for_path (ev_tmp_dir ());
407 file = g_file_get_child (tmpdir, attachment->priv->name);
409 if (ev_attachment_save (attachment, file, error)) {
410 if (attachment->priv->tmp_file)
411 g_object_unref (attachment->priv->tmp_file);
412 attachment->priv->tmp_file = g_object_ref (file);
414 retval = ev_attachment_launch_app (attachment, error);
417 g_object_unref (file);
418 g_object_unref (tmpdir);