[poppler] glib/demo

Carlos Garcia Campos carlosgc at kemper.freedesktop.org
Mon Jun 14 09:17:39 PDT 2010


 glib/demo/main.c |  175 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 170 insertions(+), 5 deletions(-)

New commits:
commit cc2e5f190e19ee8169f67df2541302a2816873d3
Author: Carlos Garcia Campos <carlosgc at gnome.org>
Date:   Mon Jun 14 18:17:01 2010 +0200

    [glib-demo] Support password protected documents

diff --git a/glib/demo/main.c b/glib/demo/main.c
index 9137a25..206d29b 100644
--- a/glib/demo/main.c
+++ b/glib/demo/main.c
@@ -141,6 +141,145 @@ pdg_demo_notebook_create (PopplerDocument *document)
 	return notebook;
 }
 
+static void
+pgd_demo_auth_dialog_entry_changed (GtkEditable *editable,
+				    GtkDialog   *dialog)
+{
+	const char *text;
+
+	text = gtk_entry_get_text (GTK_ENTRY (editable));
+
+	gtk_dialog_set_response_sensitive (dialog, GTK_RESPONSE_OK,
+					   (text != NULL && *text != '\0'));
+	g_object_set_data (G_OBJECT (dialog), "pgd-password", (gpointer)text);
+}
+
+static void
+pgd_demo_auth_dialog_entry_activated (GtkEntry  *entry,
+				      GtkDialog *dialog)
+{
+	gtk_dialog_response (dialog, GTK_RESPONSE_OK);
+}
+
+static GtkDialog *
+pgd_demo_get_auth_dialog (GFile *uri_file)
+{
+	GtkDialog *dialog;
+	GtkWidget *content_area, *action_area;
+	GtkWidget *entry_container;
+	GtkWidget *password_entry;
+	GtkWidget *hbox, *main_vbox, *vbox, *icon;
+	GtkWidget *table;
+	GtkWidget *label;
+	gchar     *format, *markup, *file_name;
+
+	dialog = GTK_DIALOG (gtk_dialog_new ());
+	content_area = gtk_dialog_get_content_area (dialog);
+	action_area = gtk_dialog_get_action_area (dialog);
+
+	/* Set the dialog up with HIG properties */
+	gtk_dialog_set_has_separator (dialog, FALSE);
+	gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
+	gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
+	gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
+	gtk_box_set_spacing (GTK_BOX (action_area), 6);
+
+	gtk_window_set_title (GTK_WINDOW (dialog), "Enter password");
+	gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
+	gtk_window_set_icon_name (GTK_WINDOW (dialog), GTK_STOCK_DIALOG_AUTHENTICATION);
+	gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
+
+	gtk_dialog_add_buttons (dialog,
+				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+				"_Unlock Document", GTK_RESPONSE_OK,
+				NULL);
+	gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
+	gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
+					   GTK_RESPONSE_OK, FALSE);
+	gtk_dialog_set_alternative_button_order (dialog,
+						 GTK_RESPONSE_OK,
+						 GTK_RESPONSE_CANCEL,
+						 -1);
+
+	/* Build contents */
+	hbox = gtk_hbox_new (FALSE, 12);
+	gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
+	gtk_box_pack_start (GTK_BOX (content_area), hbox, TRUE, TRUE, 0);
+	gtk_widget_show (hbox);
+
+	icon = gtk_image_new_from_stock (GTK_STOCK_DIALOG_AUTHENTICATION,
+					 GTK_ICON_SIZE_DIALOG);
+
+	gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
+	gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
+	gtk_widget_show (icon);
+
+	main_vbox = gtk_vbox_new (FALSE, 18);
+	gtk_box_pack_start (GTK_BOX (hbox), main_vbox, TRUE, TRUE, 0);
+	gtk_widget_show (main_vbox);
+
+	label = gtk_label_new (NULL);
+	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+	file_name = g_file_get_basename (uri_file);
+	format = g_strdup_printf ("<span size=\"larger\" weight=\"bold\">%s</span>\n\n%s",
+				  "Password required",
+				  "The document “%s” is locked and requires a password before it can be opened.");
+	markup = g_markup_printf_escaped (format, file_name);
+	gtk_label_set_markup (GTK_LABEL (label), markup);
+	g_free (format);
+	g_free (markup);
+	g_free (file_name);
+	gtk_box_pack_start (GTK_BOX (main_vbox), label,
+			    FALSE, FALSE, 0);
+	gtk_widget_show (label);
+
+	vbox = gtk_vbox_new (FALSE, 6);
+	gtk_box_pack_start (GTK_BOX (main_vbox), vbox, FALSE, FALSE, 0);
+	gtk_widget_show (vbox);
+
+	/* The table that holds the entries */
+	entry_container = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
+
+	gtk_alignment_set_padding (GTK_ALIGNMENT (entry_container),
+				   0, 0, 0, 0);
+
+	gtk_box_pack_start (GTK_BOX (vbox), entry_container,
+			    FALSE, FALSE, 0);
+	gtk_widget_show (entry_container);
+
+	table = gtk_table_new (1, 2, FALSE);
+	gtk_table_set_col_spacings (GTK_TABLE (table), 12);
+	gtk_table_set_row_spacings (GTK_TABLE (table), 6);
+	gtk_container_add (GTK_CONTAINER (entry_container), table);
+	gtk_widget_show (table);
+
+	label = gtk_label_new_with_mnemonic ("_Password:");
+	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+
+	password_entry = gtk_entry_new ();
+	gtk_entry_set_visibility (GTK_ENTRY (password_entry), FALSE);
+	g_signal_connect (password_entry, "changed",
+			  G_CALLBACK (pgd_demo_auth_dialog_entry_changed),
+			  dialog);
+	g_signal_connect (password_entry, "activate",
+			  G_CALLBACK (pgd_demo_auth_dialog_entry_activated),
+			  dialog);
+
+	gtk_table_attach (GTK_TABLE (table), label,
+			  0, 1, 0, 1,
+			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
+	gtk_widget_show (label);
+
+	gtk_table_attach_defaults (GTK_TABLE (table), password_entry,
+				   1, 2, 0, 1);
+	gtk_widget_show (password_entry);
+
+	gtk_label_set_mnemonic_widget (GTK_LABEL (label), password_entry);
+
+	return dialog;
+}
+
 gint main (gint argc, gchar **argv)
 {
 	PopplerDocument  *document;
@@ -166,19 +305,45 @@ gint main (gint argc, gchar **argv)
 
 	file = g_file_new_for_commandline_arg (argv[1]);
 	uri = g_file_get_uri (file);
-	g_object_unref (file);
 
 	timer = g_timer_new ();
 	document = poppler_document_new_from_file (uri, NULL, &error);
 	g_timer_stop (timer);
 	if (error) {
-		g_print ("Error: %s\n", error->message);
-		g_error_free (error);
-		g_free (uri);
+		while (g_error_matches (error, POPPLER_ERROR, POPPLER_ERROR_ENCRYPTED)) {
+			GtkDialog   *dialog;
+			const gchar *password;
 
-		return 1;
+			dialog = pgd_demo_get_auth_dialog (file);
+			if (gtk_dialog_run (dialog) != GTK_RESPONSE_OK) {
+				g_print ("Error: no password provided\n");
+				g_object_unref (file);
+				g_free (uri);
+
+				return 1;
+			}
+
+			g_clear_error (&error);
+			password = g_object_get_data (G_OBJECT (dialog), "pgd-password");
+
+			g_timer_start (timer);
+			document = poppler_document_new_from_file (uri, password, &error);
+			g_timer_stop (timer);
+
+			gtk_widget_destroy (GTK_WIDGET (dialog));
+		}
+
+		if (error) {
+			g_print ("Error: %s\n", error->message);
+			g_error_free (error);
+			g_object_unref (file);
+			g_free (uri);
+
+			return 1;
+		}
 	}
 
+	g_object_unref (file);
 	g_free (uri);
 
 	g_print ("Document successfully loaded in %.4f seconds\n",


More information about the poppler mailing list