dialog-password.c 2.44 KB
Newer Older
1
#include <gnumeric-config.h>
2
#include <gtk/gtk.h>
3
#include <glib/gi18n-lib.h>
4
#include "dialogs.h"
5
#include "gui-util.h"
6 7

static void
8
cb_accept_password (G_GNUC_UNUSED GtkWidget *IGNORED, GtkDialog *d)
9 10 11 12
{
	gtk_dialog_response (d, GTK_RESPONSE_ACCEPT);
}

13 14 15
/*
 * Note: filename is fs encoded, not UTF-8.
 */
16 17 18 19 20
char *
dialog_get_password (GtkWindow *parent, const char *filename)
{
	char *res = NULL;
	char *str;
Morten Welinder's avatar
Morten Welinder committed
21
	char *dispname;
22 23 24 25
	char *primary;
	char *secondary;
	GtkWidget *d, *hb, *vb, *pwb, *image, *label, *entry;

Morten Welinder's avatar
Morten Welinder committed
26 27 28
	dispname  = g_filename_display_name (filename);
	primary   = g_strdup_printf (_("%s is encrypted"), dispname);
	g_free (dispname);
29 30 31 32 33 34 35 36 37
	secondary = _("Encrypted files require a password\nbefore they can be opened.");
	label = gtk_label_new (NULL);
	str = g_strdup_printf ("<span weight=\"bold\" size=\"larger\">"
			       "%s</span>\n\n%s", primary, secondary);
	gtk_label_set_markup (GTK_LABEL (label), str);
	g_free (primary);
	g_free (str);

	gtk_label_set_selectable (GTK_LABEL (label), TRUE);
Morten Welinder's avatar
Morten Welinder committed
38

39
	d = gtk_dialog_new_with_buttons ("", parent,
40
					 GTK_DIALOG_DESTROY_WITH_PARENT,
41 42
					 GNM_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					 GNM_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
43 44
					 NULL);
	gtk_window_set_resizable (GTK_WINDOW (d), FALSE);
45
	hb = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
46
	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (d))), hb,
47
			    TRUE, TRUE, 8);
48 49
	image = gtk_image_new_from_icon_name ("gnumeric-protection-yes-dialog",
					      GTK_ICON_SIZE_DIALOG);
50 51
	gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
	gtk_box_pack_start (GTK_BOX (hb), image, FALSE, FALSE, 0);
52
	vb = gtk_box_new (GTK_ORIENTATION_VERTICAL, 8);
53
	gtk_box_pack_start (GTK_BOX (hb), vb, TRUE, TRUE, 6);
54
	gtk_box_pack_start (GTK_BOX (vb), label, TRUE, TRUE, 0);
55
	pwb = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
56 57 58
	/* Strange width so that width of primary/secondary text will win. */
	entry = g_object_new (GTK_TYPE_ENTRY,
			      "visibility", FALSE,
59
			      "width-request", 1, NULL);
60
	gtk_box_pack_start (GTK_BOX (pwb), gtk_label_new (_("Password:")),
61
			    FALSE, FALSE, 0);
62
	gtk_box_pack_start (GTK_BOX (pwb), entry, TRUE, TRUE, 0);
63 64
	gtk_box_pack_start (GTK_BOX (vb), pwb, FALSE, FALSE, 0);
	gtk_widget_show_all (d);
Morten Welinder's avatar
Morten Welinder committed
65

66 67 68 69 70 71 72 73 74
	g_signal_connect (G_OBJECT (entry),
		"activate",
		G_CALLBACK (cb_accept_password), d);

	if (gtk_dialog_run (GTK_DIALOG (d)) == GTK_RESPONSE_ACCEPT)
		res = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
	gtk_widget_destroy (d);
	return res;
}