1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/* this file is part of atril, a mate document viewer
*
* Copyright (C) 2006 Carlos Garcia Campos <carlosgc@gnome.org>
*
* Atril is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Atril is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <glib/gi18n-lib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "ev-file-helpers.h"
#include "ev-attachment.h"
enum
{
PROP_0,
PROP_NAME,
PROP_DESCRIPTION,
PROP_MTIME,
PROP_CTIME,
PROP_SIZE,
PROP_DATA
};
struct _EvAttachmentPrivate {
gchar *name;
gchar *description;
GTime mtime;
GTime ctime;
gsize size;
gchar *data;
gchar *mime_type;
GAppInfo *app;
GFile *tmp_file;
};
#define EV_ATTACHMENT_GET_PRIVATE(object) \
(G_TYPE_INSTANCE_GET_PRIVATE ((object), EV_TYPE_ATTACHMENT, EvAttachmentPrivate))
G_DEFINE_TYPE (EvAttachment, ev_attachment, G_TYPE_OBJECT)
GQuark
ev_attachment_error_quark (void)
{
static GQuark error_quark = 0;
if (error_quark == 0)
error_quark =
g_quark_from_static_string ("ev-attachment-error-quark");
return error_quark;
}
static void
ev_attachment_finalize (GObject *object)
{
EvAttachment *attachment = EV_ATTACHMENT (object);
if (attachment->priv->name) {
g_free (attachment->priv->name);
attachment->priv->name = NULL;
}
if (attachment->priv->description) {
g_free (attachment->priv->description);
attachment->priv->description = NULL;
}
if (attachment->priv->data) {
g_free (attachment->priv->data);
attachment->priv->data = NULL;
}
if (attachment->priv->mime_type) {
g_free (attachment->priv->mime_type);
attachment->priv->mime_type = NULL;
}
if (attachment->priv->app) {
g_object_unref (attachment->priv->app);
attachment->priv->app = NULL;
}
if (attachment->priv->tmp_file) {
ev_tmp_file_unlink (attachment->priv->tmp_file);
g_object_unref (attachment->priv->tmp_file);
attachment->priv->tmp_file = NULL;
}
G_OBJECT_CLASS (ev_attachment_parent_class)->finalize (object);
}
static void
ev_attachment_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *param_spec)
{
EvAttachment *attachment = EV_ATTACHMENT (object);
switch (prop_id) {
case PROP_NAME:
attachment->priv->name = g_value_dup_string (value);
break;
case PROP_DESCRIPTION:
attachment->priv->description = g_value_dup_string (value);
break;
case PROP_MTIME:
attachment->priv->mtime = g_value_get_ulong (value);
break;
case PROP_CTIME:
attachment->priv->ctime = g_value_get_ulong (value);
break;
case PROP_SIZE:
attachment->priv->size = g_value_get_uint (value);
break;
case PROP_DATA:
attachment->priv->data = g_value_get_pointer (value);
attachment->priv->mime_type = g_content_type_guess (attachment->priv->name,
(guchar *) attachment->priv->data,
attachment->priv->size,
NULL);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
prop_id,
param_spec);
break;
}
}
static void
ev_attachment_class_init (EvAttachmentClass *klass)
{
GObjectClass *g_object_class;
g_object_class = G_OBJECT_CLASS (klass);
g_object_class->set_property = ev_attachment_set_property;
g_type_class_add_private (g_object_class, sizeof (EvAttachmentPrivate));
/* Properties */
g_object_class_install_property (g_object_class,
PROP_NAME,
g_param_spec_string ("name",
"Name",
"The attachment name",
NULL,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (g_object_class,
PROP_DESCRIPTION,
g_param_spec_string ("description",
"Description",
"The attachment description",
NULL,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (g_object_class,
PROP_MTIME,
g_param_spec_ulong ("mtime",
"ModifiedTime",
"The attachment modification date",
0, G_MAXULONG, 0,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (g_object_class,
PROP_CTIME,
g_param_spec_ulong ("ctime",
"CreationTime",
"The attachment creation date",
0, G_MAXULONG, 0,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (g_object_class,
PROP_SIZE,
g_param_spec_uint ("size",
"Size",
"The attachment size",
0, G_MAXUINT, 0,
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class_install_property (g_object_class,
PROP_DATA,
g_param_spec_pointer ("data",
"Data",
"The attachment data",
G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY));
g_object_class->finalize = ev_attachment_finalize;
}
static void
ev_attachment_init (EvAttachment *attachment)
{
attachment->priv = EV_ATTACHMENT_GET_PRIVATE (attachment);
attachment->priv->name = NULL;
attachment->priv->description = NULL;
attachment->priv->data = NULL;
attachment->priv->mime_type = NULL;
attachment->priv->tmp_file = NULL;
}
EvAttachment *
ev_attachment_new (const gchar *name,
const gchar *description,
GTime mtime,
GTime ctime,
gsize size,
gpointer data)
{
EvAttachment *attachment;
attachment = g_object_new (EV_TYPE_ATTACHMENT,
"name", name,
"description", description,
"mtime", mtime,
"ctime", ctime,
"size", size,
"data", data,
NULL);
return attachment;
}
const gchar *
ev_attachment_get_name (EvAttachment *attachment)
{
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
return attachment->priv->name;
}
const gchar *
ev_attachment_get_description (EvAttachment *attachment)
{
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
return attachment->priv->description;
}
GTime
ev_attachment_get_modification_date (EvAttachment *attachment)
{
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
return attachment->priv->mtime;
}
GTime
ev_attachment_get_creation_date (EvAttachment *attachment)
{
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), 0);
return attachment->priv->ctime;
}
const gchar *
ev_attachment_get_mime_type (EvAttachment *attachment)
{
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), NULL);
return attachment->priv->mime_type;
}
gboolean
ev_attachment_save (EvAttachment *attachment,
GFile *file,
GError **error)
{
GFileOutputStream *output_stream;
GError *ioerror = NULL;
gssize written_bytes;
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
g_return_val_if_fail (G_IS_FILE (file), FALSE);
output_stream = g_file_replace (file, NULL, FALSE, 0, NULL, &ioerror);
if (output_stream == NULL) {
char *uri;
uri = g_file_get_uri (file);
g_set_error (error,
EV_ATTACHMENT_ERROR,
ioerror->code,
_("Couldn't save attachment “%s”: %s"),
uri,
ioerror->message);
g_error_free (ioerror);
g_free (uri);
return FALSE;
}
written_bytes = g_output_stream_write (G_OUTPUT_STREAM (output_stream),
attachment->priv->data,
attachment->priv->size,
NULL, &ioerror);
if (written_bytes == -1) {
char *uri;
uri = g_file_get_uri (file);
g_set_error (error,
EV_ATTACHMENT_ERROR,
ioerror->code,
_("Couldn't save attachment “%s”: %s"),
uri,
ioerror->message);
g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);
g_error_free (ioerror);
g_free (uri);
return FALSE;
}
g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);
return TRUE;
}
static gboolean
ev_attachment_launch_app (EvAttachment *attachment,
GdkScreen *screen,
guint32 timestamp,
GError **error)
{
gboolean result;
GList *files = NULL;
GAppLaunchContext *context = NULL;
GError *ioerror = NULL;
g_assert (G_IS_FILE (attachment->priv->tmp_file));
g_assert (G_IS_APP_INFO (attachment->priv->app));
files = g_list_prepend (files, attachment->priv->tmp_file);
context = G_APP_LAUNCH_CONTEXT (gdk_app_launch_context_new ());
gdk_app_launch_context_set_screen (GDK_APP_LAUNCH_CONTEXT (context), screen);
gdk_app_launch_context_set_timestamp (GDK_APP_LAUNCH_CONTEXT (context), timestamp);
result = g_app_info_launch (attachment->priv->app, files,
context, &ioerror);
if (context)
g_object_unref (context);
if (!result) {
g_set_error (error,
EV_ATTACHMENT_ERROR,
(gint) result,
_("Couldn't open attachment “%s”: %s"),
attachment->priv->name,
ioerror->message);
g_list_free (files);
g_error_free (ioerror);
return FALSE;
}
g_list_free (files);
return TRUE;
}
gboolean
ev_attachment_open (EvAttachment *attachment,
GdkScreen *screen,
guint32 timestamp,
GError **error)
{
GAppInfo *app_info;
gboolean retval = FALSE;
g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
if (!attachment->priv->app) {
app_info = g_app_info_get_default_for_type (attachment->priv->mime_type, FALSE);
attachment->priv->app = app_info;
}
if (!attachment->priv->app) {
g_set_error (error,
EV_ATTACHMENT_ERROR,
0,
_("Couldn't open attachment “%s”"),
attachment->priv->name);
return FALSE;
}
if (attachment->priv->tmp_file) {
retval = ev_attachment_launch_app (attachment, screen,
timestamp, error);
} else {
char *template;
GFile *file;
/* FIXMEchpe: convert to filename encoding first! */
template = g_strdup_printf ("%s.XXXXXX", ev_attachment_get_name (attachment));
file = ev_mkstemp_file (template, error);
g_free (template);
if (file != NULL && ev_attachment_save (attachment, file, error)) {
if (attachment->priv->tmp_file)
g_object_unref (attachment->priv->tmp_file);
attachment->priv->tmp_file = g_object_ref (file);
retval = ev_attachment_launch_app (attachment, screen,
timestamp, error);
}
g_object_unref (file);
}
return retval;
}
|