Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / embed / mozilla / GeckoFormSigningDialog.cpp
blobb33e2b3c9b21b2dfab58eb174bba8ce1967b2cbc
1 /*
2 * Copyright © 2006 Christian Persch
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1, or (at your option)
7 * any later version.
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 Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
18 * $Id: GeckoFormSigningDialog.cpp 6952 2007-03-11 19:42:02Z chpe $
21 #include "mozilla-config.h"
22 #include "config.h"
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <glade/glade-xml.h>
29 #include <nsStringAPI.h>
31 #include <nsCOMPtr.h>
32 #include <nsIDOMWindow.h>
33 #include <nsIInterfaceRequestor.h>
34 #include <nsIInterfaceRequestorUtils.h>
36 #include "eel-gconf-extensions.h"
37 #include "ephy-debug.h"
38 #include "ephy-embed-shell.h"
39 #include "ephy-file-helpers.h"
40 #include "ephy-prefs.h"
42 #include "AutoJSContextStack.h"
43 #include "AutoWindowModalState.h"
44 #include "EphyUtils.h"
46 #include "GeckoFormSigningDialog.h"
48 #define LITERAL(s) NS_REINTERPRET_CAST(const nsAString::char_type*, NS_L(s))
50 NS_IMPL_ISUPPORTS1 (GeckoFormSigningDialog,
51 nsIFormSigningDialog)
53 GeckoFormSigningDialog::GeckoFormSigningDialog()
55 LOG ("GeckoFormSigningDialog ctor [%p]", this);
58 GeckoFormSigningDialog::~GeckoFormSigningDialog()
60 LOG ("GeckoFormSigningDialog dtor [%p]", this);
63 /* nsIFormSigningDialog implementation */
65 /* boolean confirmSignText (in nsIInterfaceRequestor ctxt,
66 in AString host,
67 in AString signText,
68 [array, size_is (count)] in wstring certNickList,
69 [array, size_is (count)] in wstring certDetailsList,
70 in PRUint32 count,
71 out PRInt32 selectedIndex,
72 out AString password); */
73 NS_IMETHODIMP
74 GeckoFormSigningDialog::ConfirmSignText (nsIInterfaceRequestor *ctx,
75 const nsAString & host,
76 const nsAString & signText,
77 const PRUnichar **certNickList,
78 const PRUnichar **certDetailsList,
79 PRUint32 count,
80 PRInt32 *selectedIndex,
81 nsAString &_password,
82 PRBool *_cancelled)
84 /* FIXME: limit |signText| to a sensitlbe length (maybe 100k)? */
86 nsresult rv;
87 AutoJSContextStack stack;
88 rv = stack.Init ();
89 if (NS_FAILED (rv)) return rv;
91 nsCOMPtr<nsIDOMWindow> parent (do_GetInterface (ctx));
92 if (!parent) {
93 parent = EphyJSUtils::GetDOMWindowFromCallContext ();
94 g_print ("Fallback window %p\n", (void*)parent.get());
96 GtkWidget *gparent = EphyUtils::FindGtkParent (parent);
98 AutoWindowModalState modalState (parent);
100 GladeXML *gxml = glade_xml_new (ephy_file ("form-signing-dialog.glade"),
101 "form_signing_dialog", NULL);
102 g_return_val_if_fail (gxml, NS_ERROR_FAILURE);
104 GtkWidget *dialog = glade_xml_get_widget (gxml, "form_signing_dialog");
105 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (gparent));
107 GtkLabel *primary_label = GTK_LABEL (glade_xml_get_widget (gxml, "primary_label"));
108 char *primary = g_strdup_printf (_("The web site “%s” requests that you sign the following text:"),
109 NS_ConvertUTF16toUTF8 (host).get ());
110 gtk_label_set_text (primary_label, primary);
111 g_free (primary);
113 GtkTextView *textview = GTK_TEXT_VIEW (glade_xml_get_widget (gxml, "textview"));
114 NS_ConvertUTF16toUTF8 text (signText);
115 gtk_text_buffer_set_text (gtk_text_view_get_buffer (textview),
116 text.get (), text.Length ());
118 GtkTable *table = GTK_TABLE (glade_xml_get_widget (gxml, "table"));
119 GtkComboBox *combo = GTK_COMBO_BOX (gtk_combo_box_new_text ());
120 for (PRUint32 i = 0; i < count; ++i) {
121 gtk_combo_box_append_text (combo, NS_ConvertUTF16toUTF8 (certNickList[i]).get ());
124 gtk_combo_box_set_active (combo, 0);
125 gtk_table_attach (table, GTK_WIDGET (combo), 1, 2, 0, 1,
126 GtkAttachOptions (0), GtkAttachOptions (0), 0, 0);
127 gtk_widget_show (GTK_WIDGET (combo));
129 /* FIXME: Add "View Certificate" button */
131 GtkEntry *password_entry = GTK_ENTRY (glade_xml_get_widget (gxml, "password_entry"));
133 GtkWidget *button = gtk_dialog_add_button (GTK_DIALOG (dialog),
134 GTK_STOCK_CANCEL,
135 GTK_RESPONSE_CANCEL);
136 gtk_dialog_add_button (GTK_DIALOG (dialog),
137 _("_Sign text"),
138 GTK_RESPONSE_ACCEPT);
139 gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_REJECT);
140 gtk_widget_grab_focus (button);
142 /* FIXME: make Sign insensitive for some time (proportional to text length, with maximum?) */
144 g_object_unref (gxml);
146 int response = gtk_dialog_run (GTK_DIALOG (dialog));
148 *_cancelled = response != GTK_RESPONSE_ACCEPT;
150 if (response == GTK_RESPONSE_ACCEPT) {
151 _password = NS_ConvertUTF8toUTF16 (gtk_entry_get_text (password_entry));
152 *selectedIndex = gtk_combo_box_get_active (combo);
155 gtk_widget_destroy (dialog);
157 return NS_OK;