[poppler] 2 commits - glib/demo glib/poppler-attachment.cc

Carlos Garcia Campos carlosgc at kemper.freedesktop.org
Thu Aug 21 10:05:18 PDT 2008


 glib/demo/Makefile.am      |    2 
 glib/demo/attachments.c    |  242 +++++++++++++++++++++++++++++++++++++++++++++
 glib/demo/attachments.h    |   31 +++++
 glib/demo/main.c           |    4 
 glib/demo/utils.c          |   23 ++++
 glib/demo/utils.h          |    1 
 glib/poppler-attachment.cc |    4 
 7 files changed, 304 insertions(+), 3 deletions(-)

New commits:
commit e233325e6f13d8b232bf68a2812fe755e7fccd4d
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Thu Aug 21 19:03:32 2008 +0200

    [glib-demo] Add demo for attachments

diff --git a/glib/demo/Makefile.am b/glib/demo/Makefile.am
index f147830..0eb6455 100644
--- a/glib/demo/Makefile.am
+++ b/glib/demo/Makefile.am
@@ -11,6 +11,8 @@ poppler_glib_demo_SOURCES = 			\
 	main.c					\
 	annots.h				\
 	annots.c				\
+	attachments.c				\
+	attachments.h				\
 	fonts.h					\
 	fonts.c					\
 	forms.h					\
diff --git a/glib/demo/attachments.c b/glib/demo/attachments.c
new file mode 100644
index 0000000..c31a4e0
--- /dev/null
+++ b/glib/demo/attachments.c
@@ -0,0 +1,242 @@
+/*
+ * Copyright (C) 2008 Carlos Garcia Campos  <carlosgc at gnome.org>
+ *
+ * This program 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, or (at your option)
+ * any later version.
+ *
+ * This program 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 <gtk/gtk.h>
+
+#include "attachments.h"
+#include "utils.h"
+
+enum {
+	ATTACHMENTS_NAME_COLUMN,
+	ATTACHMENTS_DESCRIPTION_COLUMN,
+	ATTACHMENTS_SIZE_COLUMN,
+	ATTACHMENTS_CTIME_COLUMN,
+	ATTACHMENTS_MTIME_COLUMN,
+	ATTACHMENTS_ATTACHMENT_COLUMN,
+	N_COLUMNS
+};
+
+static void
+pgd_attachments_fill_model (GtkListStore    *model,
+			    PopplerDocument *document)
+{
+	GList *list, *l;
+
+	list = poppler_document_get_attachments (document);
+
+	for (l = list; l && l->data; l = g_list_next (l)) {
+		PopplerAttachment *attachment = POPPLER_ATTACHMENT (l->data);
+		GtkTreeIter        iter;
+		gchar             *size;
+		gchar             *ctime, *mtime;
+
+		size = g_strdup_printf ("%d", attachment->size);
+		ctime = pgd_format_date (attachment->ctime);
+		mtime = pgd_format_date (attachment->mtime);
+
+		gtk_list_store_append (model, &iter);
+		gtk_list_store_set (model, &iter,
+				    ATTACHMENTS_NAME_COLUMN,
+				    attachment->name ? attachment->name : "Unknown", 
+				    ATTACHMENTS_DESCRIPTION_COLUMN,
+				    attachment->description ? attachment->description : "Unknown", 
+				    ATTACHMENTS_SIZE_COLUMN,
+				    size ? size : "Unknown",
+				    ATTACHMENTS_CTIME_COLUMN,
+				    ctime ? ctime : "Unknown", 
+				    ATTACHMENTS_MTIME_COLUMN,
+				    mtime ? mtime : "Unknown", 
+				    ATTACHMENTS_ATTACHMENT_COLUMN, attachment,
+				    -1);
+
+		g_free (size);
+		g_free (ctime);
+		g_free (mtime);
+
+		g_object_unref (attachment);
+	}
+
+	g_list_free (list);
+}
+
+static GtkWidget *
+pgd_attachments_create_list (GtkTreeModel *model)
+{
+	GtkWidget       *treeview;
+	GtkCellRenderer *renderer;
+	
+	treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
+	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), TRUE);
+	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
+
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
+						     0, "Name",
+						     renderer,
+						     "text", ATTACHMENTS_NAME_COLUMN,
+						     NULL);
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
+						     1, "Description",
+						     renderer,
+						     "text", ATTACHMENTS_DESCRIPTION_COLUMN,
+						     NULL);
+	g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
+	g_object_set (G_OBJECT (gtk_tree_view_get_column (GTK_TREE_VIEW (treeview), 1)),
+		      "expand", TRUE, NULL);
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
+						     2, "Size",
+						     renderer,
+						     "text", ATTACHMENTS_SIZE_COLUMN,
+						     NULL);
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
+						     3, "Creation Date",
+						     renderer,
+						     "text", ATTACHMENTS_CTIME_COLUMN,
+						     NULL);
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
+						     4, "Modification Date",
+						     renderer,
+						     "text", ATTACHMENTS_MTIME_COLUMN,
+						     NULL);
+	return treeview;
+}
+
+static void
+pgd_attachments_save_dialog_response (GtkFileChooser    *file_chooser,
+				      gint               response,
+				      PopplerAttachment *attachment)
+{
+	gchar  *filename;
+	GError *error = NULL;
+	
+	if (response != GTK_RESPONSE_ACCEPT) {
+		g_object_unref (attachment);
+		gtk_widget_destroy (GTK_WIDGET (file_chooser));
+		return;
+	}
+
+	filename = gtk_file_chooser_get_filename (file_chooser);
+	if (!poppler_attachment_save (attachment, filename, &error)) {
+		g_warning (error->message);
+		g_error_free (error);
+	}
+	g_free (filename);
+	g_object_unref (attachment);
+	gtk_widget_destroy (GTK_WIDGET (file_chooser));
+}
+
+
+static void
+pgd_attachments_save_button_clicked (GtkButton   *button,
+				     GtkTreeView *treeview)
+{
+	GtkTreeSelection  *selection;
+	GtkTreeModel      *model;
+	GtkTreeIter        iter;
+	GtkWidget         *file_chooser;
+	PopplerAttachment *attachment;
+
+	selection = gtk_tree_view_get_selection (treeview);
+	if (!gtk_tree_selection_get_selected (selection, &model, &iter)) 
+		return;
+
+	gtk_tree_model_get (model, &iter,
+			    ATTACHMENTS_ATTACHMENT_COLUMN, &attachment,
+			    -1);
+
+	if (!attachment)
+		return;
+
+	file_chooser = gtk_file_chooser_dialog_new ("Save attachment",
+						    GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (treeview))),
+						    GTK_FILE_CHOOSER_ACTION_SAVE,
+						    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+						    GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
+						    NULL);
+	gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_chooser), attachment->name);
+	g_signal_connect (G_OBJECT (file_chooser), "response",
+			  G_CALLBACK (pgd_attachments_save_dialog_response),
+			  (gpointer) attachment);
+	gtk_widget_show (file_chooser);
+		
+}
+
+GtkWidget *
+pgd_attachments_create_widget (PopplerDocument *document)
+{
+	GtkWidget    *vbox;
+	GtkWidget    *treeview;
+	GtkListStore *model;
+	GtkWidget    *swindow;
+	GtkWidget    *hbox, *button;
+
+	vbox = gtk_vbox_new (FALSE, 12);
+
+	swindow = gtk_scrolled_window_new (NULL, NULL);
+	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swindow),
+					GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+
+	if (poppler_document_has_attachments (document)) {
+		model = gtk_list_store_new (N_COLUMNS,
+					    G_TYPE_STRING, G_TYPE_STRING,
+					    G_TYPE_STRING,
+					    G_TYPE_STRING, G_TYPE_STRING,
+					    G_TYPE_OBJECT);
+		pgd_attachments_fill_model (model, document);
+	} else {
+		GtkTreeIter iter;
+		gchar      *markup;
+
+		model = gtk_list_store_new (1, G_TYPE_STRING);
+		gtk_list_store_append (model, &iter);
+		markup = g_strdup_printf ("<span size=\"larger\" style=\"italic\">%s</span>",
+					  "The document doesn't contain outline");
+		gtk_list_store_set (model, &iter, 0, markup, -1);
+		g_free (markup);
+	}
+	
+	treeview = pgd_attachments_create_list (GTK_TREE_MODEL (model));
+	g_object_unref (model);
+
+	gtk_container_add (GTK_CONTAINER (swindow), treeview);
+	gtk_widget_show (treeview);
+
+	gtk_box_pack_start (GTK_BOX (vbox), swindow, TRUE, TRUE, 0);
+	gtk_widget_show (swindow);
+
+	hbox = gtk_hbutton_box_new ();
+	gtk_button_box_set_layout (GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_SPREAD);
+
+	button = gtk_button_new_with_label ("Save");
+	g_signal_connect (G_OBJECT (button), "clicked",
+			  G_CALLBACK (pgd_attachments_save_button_clicked),
+			  (gpointer)treeview);
+
+	gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
+	gtk_widget_show (button);
+	
+
+	gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, TRUE, 6);
+	gtk_widget_show (hbox);
+
+	return vbox;
+}
diff --git a/glib/demo/attachments.h b/glib/demo/attachments.h
new file mode 100644
index 0000000..330b3c1
--- /dev/null
+++ b/glib/demo/attachments.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2008 Carlos Garcia Campos  <carlosgc at gnome.org>
+ *
+ * This program 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, or (at your option)
+ * any later version.
+ *
+ * This program 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 <gtk/gtkwidget.h>
+#include <poppler.h>
+
+#ifndef _ATTACHMENTS_H_
+#define _ATTACHMENTS_H_
+
+G_BEGIN_DECLS
+
+GtkWidget *pgd_attachments_create_widget (PopplerDocument *document);
+
+G_END_DECLS
+
+#endif /* _ATTACHMENTS_H_ */
diff --git a/glib/demo/main.c b/glib/demo/main.c
index 26e4c32..3c26e61 100644
--- a/glib/demo/main.c
+++ b/glib/demo/main.c
@@ -30,6 +30,7 @@
 #include "transitions.h"
 #include "images.h"
 #include "annots.h"
+#include "attachments.h"
 
 enum {
 	PGD_TITLE_COLUMN,
@@ -53,7 +54,8 @@ static const PopplerGlibDemo demo_list[] = {
 	{ "Forms",            pgd_forms_create_widget },
 	{ "Page Transitions", pgd_transitions_create_widget },
 	{ "Images",           pgd_images_create_widget },
-	{ "Annots",           pgd_annots_create_widget }
+	{ "Annots",           pgd_annots_create_widget },
+	{ "Attachments",      pgd_attachments_create_widget }
 };
 
 static void
diff --git a/glib/demo/utils.c b/glib/demo/utils.c
index 0fa5074..e6d06c8 100644
--- a/glib/demo/utils.c
+++ b/glib/demo/utils.c
@@ -17,6 +17,7 @@
  */
 
 #include <gtk/gtk.h>
+#include <time.h>
 
 #include "utils.h"
 
@@ -234,3 +235,25 @@ pgd_action_view_set_action (GtkWidget     *action_view,
 	gtk_container_add (GTK_CONTAINER (alignment), table);
 	gtk_widget_show (table);
 }
+
+gchar *
+pgd_format_date (GTime utime)
+{
+	time_t time = (time_t) utime;
+	char s[256];
+	const char *fmt_hack = "%c";
+	size_t len;
+#ifdef HAVE_LOCALTIME_R
+	struct tm t;
+	if (time == 0 || !localtime_r (&time, &t)) return NULL;
+	len = strftime (s, sizeof (s), fmt_hack, &t);
+#else
+	struct tm *t;
+	if (time == 0 || !(t = localtime (&time)) ) return NULL;
+	len = strftime (s, sizeof (s), fmt_hack, t);
+#endif
+
+	if (len == 0 || s[0] == '\0') return NULL;
+
+	return g_locale_to_utf8 (s, -1, NULL, NULL, NULL);
+}
diff --git a/glib/demo/utils.h b/glib/demo/utils.h
index 5f62d8d..d2d46d9 100644
--- a/glib/demo/utils.h
+++ b/glib/demo/utils.h
@@ -36,6 +36,7 @@ void       pgd_table_add_property_with_value_widget (GtkTable        *table,
 GtkWidget *pgd_action_view_new                      (PopplerDocument *document);
 void       pgd_action_view_set_action               (GtkWidget       *action_view,
 						     PopplerAction   *action);
+gchar     *pgd_format_date                          (GTime            utime);
 
 G_END_DECLS
 
commit 0e2efa0fce5bf059ce85d3e3bec2293b405ec3c9
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Thu Aug 21 19:02:24 2008 +0200

    [glib] Make sure name and descripction are valid utf8 strings

diff --git a/glib/poppler-attachment.cc b/glib/poppler-attachment.cc
index fcdc47f..db77f11 100644
--- a/glib/poppler-attachment.cc
+++ b/glib/poppler-attachment.cc
@@ -88,9 +88,9 @@ _poppler_attachment_new (PopplerDocument *document,
   attachment = (PopplerAttachment *) g_object_new (POPPLER_TYPE_ATTACHMENT, NULL);
   
   if (emb_file->name ())
-    attachment->name = g_strdup (emb_file->name ()->getCString ());
+    attachment->name = _poppler_goo_string_to_utf8 (emb_file->name ());
   if (emb_file->description ())
-    attachment->description = g_strdup (emb_file->description ()->getCString ());
+    attachment->description = _poppler_goo_string_to_utf8 (emb_file->description ());
 
   attachment->size = emb_file->size ();
   


More information about the poppler mailing list