1
/* poppler-attachment.cc: glib wrapper for poppler
2
* Copyright (C) 2006, Red Hat, Inc.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2, or (at your option)
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <glib/gstdio.h>
24
#include "poppler-private.h"
27
* SECTION:poppler-attachment
28
* @short_description: Attachments
29
* @title: PopplerAttachment
32
/* FIXME: We need to add gettext support sometime */
35
typedef struct _PopplerAttachmentPrivate PopplerAttachmentPrivate;
36
struct _PopplerAttachmentPrivate
41
#define POPPLER_ATTACHMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), POPPLER_TYPE_ATTACHMENT, PopplerAttachmentPrivate))
43
static void poppler_attachment_dispose (GObject *obj);
44
static void poppler_attachment_finalize (GObject *obj);
46
G_DEFINE_TYPE (PopplerAttachment, poppler_attachment, G_TYPE_OBJECT)
49
poppler_attachment_init (PopplerAttachment *attachment)
54
poppler_attachment_class_init (PopplerAttachmentClass *klass)
56
G_OBJECT_CLASS (klass)->dispose = poppler_attachment_dispose;
57
G_OBJECT_CLASS (klass)->finalize = poppler_attachment_finalize;
58
g_type_class_add_private (klass, sizeof (PopplerAttachmentPrivate));
62
poppler_attachment_dispose (GObject *obj)
64
PopplerAttachmentPrivate *priv;
66
priv = POPPLER_ATTACHMENT_GET_PRIVATE (obj);
70
priv->obj_stream->free();
71
delete priv->obj_stream;
72
priv->obj_stream = NULL;
75
G_OBJECT_CLASS (poppler_attachment_parent_class)->dispose (obj);
79
poppler_attachment_finalize (GObject *obj)
81
PopplerAttachment *attachment;
83
attachment = (PopplerAttachment *) obj;
86
g_free (attachment->name);
87
attachment->name = NULL;
89
if (attachment->description)
90
g_free (attachment->description);
91
attachment->description = NULL;
93
if (attachment->checksum)
94
g_string_free (attachment->checksum, TRUE);
95
attachment->checksum = NULL;
97
G_OBJECT_CLASS (poppler_attachment_parent_class)->finalize (obj);
100
/* Public functions */
103
_poppler_attachment_new (EmbFile *emb_file)
105
PopplerAttachment *attachment;
106
PopplerAttachmentPrivate *priv;
108
g_assert (emb_file != NULL);
110
attachment = (PopplerAttachment *) g_object_new (POPPLER_TYPE_ATTACHMENT, NULL);
111
priv = POPPLER_ATTACHMENT_GET_PRIVATE (attachment);
113
if (emb_file->name ())
114
attachment->name = _poppler_goo_string_to_utf8 (emb_file->name ());
115
if (emb_file->description ())
116
attachment->description = _poppler_goo_string_to_utf8 (emb_file->description ());
118
attachment->size = emb_file->size ();
120
_poppler_convert_pdf_date_to_gtime (emb_file->createDate (), (time_t *)&attachment->ctime);
121
_poppler_convert_pdf_date_to_gtime (emb_file->modDate (), (time_t *)&attachment->mtime);
123
if (emb_file->checksum ()->getLength () > 0)
124
attachment->checksum = g_string_new_len (emb_file->checksum ()->getCString (),
125
emb_file->checksum ()->getLength ());
127
priv->obj_stream = new Object();
128
emb_file->streamObject().copy(priv->obj_stream);
134
save_helper (const gchar *buf,
139
FILE *f = (FILE *) data;
142
n = fwrite (buf, 1, count, f);
147
g_file_error_from_errno (errno),
148
_("Error writing to image file: %s"),
157
* poppler_attachment_save:
158
* @attachment: A #PopplerAttachment.
159
* @filename: name of file to save
160
* @error: (allow-none): return location for error, or %NULL.
162
* Saves @attachment to a file indicated by @filename. If @error is set, %FALSE
163
* will be returned. Possible errors include those in the #G_FILE_ERROR domain
164
* and whatever the save function generates.
166
* Return value: %TRUE, if the file successfully saved
169
poppler_attachment_save (PopplerAttachment *attachment,
170
const char *filename,
176
g_return_val_if_fail (POPPLER_IS_ATTACHMENT (attachment), FALSE);
178
f = g_fopen (filename, "wb");
182
gchar *display_name = g_filename_display_name (filename);
185
g_file_error_from_errno (errno),
186
_("Failed to open '%s' for writing: %s"),
189
g_free (display_name);
193
result = poppler_attachment_save_to_callback (attachment, save_helper, f, error);
197
gchar *display_name = g_filename_display_name (filename);
200
g_file_error_from_errno (errno),
201
_("Failed to close '%s', all data may not have been saved: %s"),
204
g_free (display_name);
211
#define BUF_SIZE 1024
214
* poppler_attachment_save_to_callback:
215
* @attachment: A #PopplerAttachment.
216
* @save_func: (scope call): a function that is called to save each block of data that the save routine generates.
217
* @user_data: user data to pass to the save function.
218
* @error: (allow-none): return location for error, or %NULL.
220
* Saves @attachment by feeding the produced data to @save_func. Can be used
221
* when you want to store the attachment to something other than a file, such as
222
* an in-memory buffer or a socket. If @error is set, %FALSE will be
223
* returned. Possible errors include those in the #G_FILE_ERROR domain and
224
* whatever the save function generates.
226
* Return value: %TRUE, if the save successfully completed
229
poppler_attachment_save_to_callback (PopplerAttachment *attachment,
230
PopplerAttachmentSaveFunc save_func,
237
gboolean eof_reached = FALSE;
239
g_return_val_if_fail (POPPLER_IS_ATTACHMENT (attachment), FALSE);
241
stream = POPPLER_ATTACHMENT_GET_PRIVATE (attachment)->obj_stream->getStream();
248
for (i = 0; i < BUF_SIZE; i++)
250
data = stream->getChar ();
261
if (! (save_func) (buf, i, user_data, error))
265
while (! eof_reached);