~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/glib/demo/attachments.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Carlos Garcia Campos  <carlosgc@gnome.org>
 
3
 *
 
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)
 
7
 * any later version.
 
8
 *
 
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.
 
13
 *
 
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.
 
17
 */
 
18
 
 
19
#include <gtk/gtk.h>
 
20
 
 
21
#include "attachments.h"
 
22
#include "utils.h"
 
23
 
 
24
enum {
 
25
        ATTACHMENTS_NAME_COLUMN,
 
26
        ATTACHMENTS_DESCRIPTION_COLUMN,
 
27
        ATTACHMENTS_SIZE_COLUMN,
 
28
        ATTACHMENTS_CTIME_COLUMN,
 
29
        ATTACHMENTS_MTIME_COLUMN,
 
30
        ATTACHMENTS_ATTACHMENT_COLUMN,
 
31
        N_COLUMNS
 
32
};
 
33
 
 
34
static void
 
35
pgd_attachments_fill_model (GtkListStore    *model,
 
36
                            PopplerDocument *document)
 
37
{
 
38
        GList *list, *l;
 
39
 
 
40
        list = poppler_document_get_attachments (document);
 
41
 
 
42
        for (l = list; l && l->data; l = g_list_next (l)) {
 
43
                PopplerAttachment *attachment = POPPLER_ATTACHMENT (l->data);
 
44
                GtkTreeIter        iter;
 
45
                gchar             *size;
 
46
                gchar             *ctime, *mtime;
 
47
 
 
48
                size = g_strdup_printf ("%" G_GSIZE_FORMAT, attachment->size);
 
49
                ctime = pgd_format_date (attachment->ctime);
 
50
                mtime = pgd_format_date (attachment->mtime);
 
51
 
 
52
                gtk_list_store_append (model, &iter);
 
53
                gtk_list_store_set (model, &iter,
 
54
                                    ATTACHMENTS_NAME_COLUMN,
 
55
                                    attachment->name ? attachment->name : "Unknown", 
 
56
                                    ATTACHMENTS_DESCRIPTION_COLUMN,
 
57
                                    attachment->description ? attachment->description : "Unknown", 
 
58
                                    ATTACHMENTS_SIZE_COLUMN,
 
59
                                    size ? size : "Unknown",
 
60
                                    ATTACHMENTS_CTIME_COLUMN,
 
61
                                    ctime ? ctime : "Unknown", 
 
62
                                    ATTACHMENTS_MTIME_COLUMN,
 
63
                                    mtime ? mtime : "Unknown", 
 
64
                                    ATTACHMENTS_ATTACHMENT_COLUMN, attachment,
 
65
                                    -1);
 
66
 
 
67
                g_free (size);
 
68
                g_free (ctime);
 
69
                g_free (mtime);
 
70
 
 
71
                g_object_unref (attachment);
 
72
        }
 
73
 
 
74
        g_list_free (list);
 
75
}
 
76
 
 
77
static GtkWidget *
 
78
pgd_attachments_create_list (GtkTreeModel *model)
 
79
{
 
80
        GtkWidget       *treeview;
 
81
        GtkCellRenderer *renderer;
 
82
        
 
83
        treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
 
84
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), TRUE);
 
85
        gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
 
86
 
 
87
        renderer = gtk_cell_renderer_text_new ();
 
88
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
89
                                                     0, "Name",
 
90
                                                     renderer,
 
91
                                                     "text", ATTACHMENTS_NAME_COLUMN,
 
92
                                                     NULL);
 
93
        renderer = gtk_cell_renderer_text_new ();
 
94
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
95
                                                     1, "Description",
 
96
                                                     renderer,
 
97
                                                     "text", ATTACHMENTS_DESCRIPTION_COLUMN,
 
98
                                                     NULL);
 
99
        g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
 
100
        g_object_set (G_OBJECT (gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 1)),
 
101
                      "expand", TRUE, NULL);
 
102
        renderer = gtk_cell_renderer_text_new ();
 
103
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
104
                                                     2, "Size",
 
105
                                                     renderer,
 
106
                                                     "text", ATTACHMENTS_SIZE_COLUMN,
 
107
                                                     NULL);
 
108
        renderer = gtk_cell_renderer_text_new ();
 
109
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
110
                                                     3, "Creation Date",
 
111
                                                     renderer,
 
112
                                                     "text", ATTACHMENTS_CTIME_COLUMN,
 
113
                                                     NULL);
 
114
        renderer = gtk_cell_renderer_text_new ();
 
115
        gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
116
                                                     4, "Modification Date",
 
117
                                                     renderer,
 
118
                                                     "text", ATTACHMENTS_MTIME_COLUMN,
 
119
                                                     NULL);
 
120
        return treeview;
 
121
}
 
122
 
 
123
static void
 
124
pgd_attachments_save_dialog_response (GtkFileChooser    *file_chooser,
 
125
                                      gint               response,
 
126
                                      PopplerAttachment *attachment)
 
127
{
 
128
        gchar  *filename;
 
129
        GError *error = NULL;
 
130
        
 
131
        if (response != GTK_RESPONSE_ACCEPT) {
 
132
                g_object_unref (attachment);
 
133
                gtk_widget_destroy (GTK_WIDGET (file_chooser));
 
134
                return;
 
135
        }
 
136
 
 
137
        filename = gtk_file_chooser_get_filename (file_chooser);
 
138
        if (!poppler_attachment_save (attachment, filename, &error)) {
 
139
                g_warning ("%s", error->message);
 
140
                g_error_free (error);
 
141
        }
 
142
        g_free (filename);
 
143
        g_object_unref (attachment);
 
144
        gtk_widget_destroy (GTK_WIDGET (file_chooser));
 
145
}
 
146
 
 
147
static void
 
148
pgd_attachments_save_button_clicked (GtkButton   *button,
 
149
                                     GtkTreeView *treeview)
 
150
{
 
151
        GtkTreeSelection  *selection;
 
152
        GtkTreeModel      *model;
 
153
        GtkTreeIter        iter;
 
154
        GtkWidget         *file_chooser;
 
155
        PopplerAttachment *attachment;
 
156
 
 
157
        selection = gtk_tree_view_get_selection (treeview);
 
158
        if (!gtk_tree_selection_get_selected (selection, &model, &iter)) 
 
159
                return;
 
160
 
 
161
        gtk_tree_model_get (model, &iter,
 
162
                            ATTACHMENTS_ATTACHMENT_COLUMN, &attachment,
 
163
                            -1);
 
164
 
 
165
        if (!attachment)
 
166
                return;
 
167
 
 
168
        file_chooser = gtk_file_chooser_dialog_new ("Save attachment",
 
169
                                                    GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (treeview))),
 
170
                                                    GTK_FILE_CHOOSER_ACTION_SAVE,
 
171
                                                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
172
                                                    GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
 
173
                                                    NULL);
 
174
        gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_chooser), attachment->name);
 
175
        g_signal_connect (G_OBJECT (file_chooser), "response",
 
176
                          G_CALLBACK (pgd_attachments_save_dialog_response),
 
177
                          (gpointer) attachment);
 
178
        gtk_widget_show (file_chooser);
 
179
                
 
180
}
 
181
 
 
182
static gboolean
 
183
attachment_save_callback (const gchar  *buf,
 
184
                          gsize         count,
 
185
                          gpointer      data,
 
186
                          GError      **error)
 
187
{
 
188
        GChecksum *cs = (GChecksum *)data;
 
189
 
 
190
        g_checksum_update (cs, buf, count);
 
191
 
 
192
        return TRUE;
 
193
}
 
194
 
 
195
static void
 
196
message_dialog_run (GtkWindow   *parent,
 
197
                    const gchar *message)
 
198
{
 
199
        GtkWidget *dialog;
 
200
 
 
201
        dialog = gtk_message_dialog_new (parent,
 
202
                                         GTK_DIALOG_DESTROY_WITH_PARENT,
 
203
                                         GTK_MESSAGE_INFO,
 
204
                                         GTK_BUTTONS_CLOSE,
 
205
                                         "%s", message);
 
206
        gtk_dialog_run (GTK_DIALOG (dialog));
 
207
        gtk_widget_destroy (dialog);
 
208
}
 
209
 
 
210
static void
 
211
pgd_attachments_validate_button_clicked (GtkButton   *button,
 
212
                                         GtkTreeView *treeview)
 
213
{
 
214
        GtkTreeSelection  *selection;
 
215
        GtkTreeModel      *model;
 
216
        GtkTreeIter        iter;
 
217
        GChecksum         *cs;
 
218
        guint8            *digest;
 
219
        gsize              digest_len;
 
220
        PopplerAttachment *attachment;
 
221
        gboolean           valid = TRUE;
 
222
 
 
223
        selection = gtk_tree_view_get_selection (treeview);
 
224
        if (!gtk_tree_selection_get_selected (selection, &model, &iter))
 
225
                return;
 
226
 
 
227
        gtk_tree_model_get (model, &iter,
 
228
                            ATTACHMENTS_ATTACHMENT_COLUMN, &attachment,
 
229
                            -1);
 
230
 
 
231
        if (!attachment)
 
232
                return;
 
233
 
 
234
        if (attachment->checksum->len == 0) {
 
235
                message_dialog_run (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (treeview))),
 
236
                                    "Impossible to validate attachment: checksum is not available");
 
237
                g_object_unref (attachment);
 
238
 
 
239
                return;
 
240
        }
 
241
        
 
242
        cs = g_checksum_new (G_CHECKSUM_MD5);
 
243
        poppler_attachment_save_to_callback (attachment, attachment_save_callback,
 
244
                                             (gpointer)cs, NULL);
 
245
        digest_len = g_checksum_type_get_length (G_CHECKSUM_MD5);
 
246
        digest = (guint8 *) g_malloc (digest_len);
 
247
        g_checksum_get_digest (cs, digest, &digest_len);
 
248
        g_checksum_free (cs);
 
249
 
 
250
        if (attachment->checksum->len == digest_len) {
 
251
                gint i;
 
252
        
 
253
                for (i = 0; i < digest_len; i++) {
 
254
                        if ((guint8)attachment->checksum->str[i] != digest[i]) {
 
255
                                valid = FALSE;
 
256
                                break;
 
257
                        }
 
258
                }
 
259
        }
 
260
 
 
261
        if (valid) {
 
262
                message_dialog_run (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (treeview))),
 
263
                                    "Attachment is valid");
 
264
        } else {
 
265
                message_dialog_run (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (treeview))),
 
266
                                    "Attachment is not valid: the checksum does not match");
 
267
        }
 
268
 
 
269
        g_free (digest);
 
270
        g_object_unref (attachment);
 
271
}
 
272
 
 
273
GtkWidget *
 
274
pgd_attachments_create_widget (PopplerDocument *document)
 
275
{
 
276
        GtkWidget    *vbox;
 
277
        GtkWidget    *treeview;
 
278
        GtkListStore *model;
 
279
        GtkWidget    *swindow;
 
280
        GtkWidget    *hbox, *button;
 
281
        gboolean      has_attachments;
 
282
 
 
283
        vbox = gtk_vbox_new (FALSE, 12);
 
284
 
 
285
        swindow = gtk_scrolled_window_new (NULL, NULL);
 
286
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
 
287
                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 
288
 
 
289
        has_attachments = poppler_document_has_attachments (document);
 
290
        if (has_attachments) {
 
291
                model = gtk_list_store_new (N_COLUMNS,
 
292
                                            G_TYPE_STRING, G_TYPE_STRING,
 
293
                                            G_TYPE_STRING,
 
294
                                            G_TYPE_STRING, G_TYPE_STRING,
 
295
                                            G_TYPE_OBJECT);
 
296
                pgd_attachments_fill_model (model, document);
 
297
                treeview = pgd_attachments_create_list (GTK_TREE_MODEL (model));
 
298
        } else {
 
299
                GtkCellRenderer *renderer;
 
300
                GtkTreeIter      iter;
 
301
                gchar           *markup;
 
302
 
 
303
                model = gtk_list_store_new (1, G_TYPE_STRING);
 
304
                gtk_list_store_append (model, &iter);
 
305
                markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>",
 
306
                                          "The document doesn't contain attachments");
 
307
                gtk_list_store_set (model, &iter, 0, markup, -1);
 
308
                g_free (markup);
 
309
 
 
310
                treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
 
311
 
 
312
                renderer = gtk_cell_renderer_text_new ();
 
313
                gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
 
314
                                                             0, "Name",
 
315
                                                             renderer,
 
316
                                                             "markup", 0,
 
317
                                                             NULL);
 
318
        }
 
319
        g_object_unref (model);
 
320
 
 
321
        gtk_container_add (GTK_CONTAINER (swindow), treeview);
 
322
        gtk_widget_show (treeview);
 
323
 
 
324
        gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0);
 
325
        gtk_widget_show (swindow);
 
326
 
 
327
        if (!has_attachments)
 
328
                return vbox;
 
329
        
 
330
        hbox = gtk_hbutton_box_new ();
 
331
        gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_SPREAD);
 
332
 
 
333
        button = gtk_button_new_with_label ("Save");
 
334
        g_signal_connect (G_OBJECT (button), "clicked",
 
335
                          G_CALLBACK (pgd_attachments_save_button_clicked),
 
336
                          (gpointer)treeview);
 
337
 
 
338
        gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
 
339
        gtk_widget_show (button);
 
340
 
 
341
        button = gtk_button_new_with_label ("Validate");
 
342
        g_signal_connect (G_OBJECT (button), "clicked",
 
343
                          G_CALLBACK (pgd_attachments_validate_button_clicked),
 
344
                          (gpointer)treeview);
 
345
 
 
346
        gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
 
347
        gtk_widget_show (button);
 
348
 
 
349
        gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 6);
 
350
        gtk_widget_show (hbox);
 
351
 
 
352
        return vbox;
 
353
}