2 * GtkNSSKeyPairDialogs.cpp
4 * Copyright © 2003 Crispin Flowerday <gnome@flowerday.cx>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * $Id: GtkNSSKeyPairDialogs.cpp 6952 2007-03-11 19:42:02Z chpe $
24 * This file provides Gtk implementations of the mozilla Generating Key Pair
28 #include "mozilla-config.h"
31 #include <glib/gi18n.h>
32 #include <gtk/gtkdialog.h>
33 #include <gtk/gtkhbox.h>
34 #include <gtk/gtkimage.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkmain.h>
37 #include <gtk/gtkprogressbar.h>
38 #include <gtk/gtkstock.h>
39 #include <gtk/gtkvbox.h>
41 #include <nsIDOMWindow.h>
42 #include <nsIInterfaceRequestor.h>
43 #include <nsIInterfaceRequestorUtils.h>
44 #include <nsIKeygenThread.h>
45 #include <nsIObserver.h>
46 #include <nsIServiceManager.h>
48 #include "ephy-debug.h"
50 #include "ephy-stock-icons.h"
52 #include "AutoJSContextStack.h"
53 #include "AutoWindowModalState.h"
54 #include "EphyUtils.h"
56 #include "GtkNSSKeyPairDialogs.h"
58 GtkNSSKeyPairDialogs::GtkNSSKeyPairDialogs ()
60 LOG ("GtkNSSKeyPairDialogs ctor (%p)", this);
63 GtkNSSKeyPairDialogs::~GtkNSSKeyPairDialogs ()
65 LOG ("GtkNSSKeyPairDialogs dtor (%p)", this);
68 NS_IMPL_THREADSAFE_ISUPPORTS1 (GtkNSSKeyPairDialogs
,
69 nsIGeneratingKeypairInfoDialogs
)
71 class KeyPairObserver
: public nsIObserver
77 KeyPairObserver() : close_called (FALSE
) {};
78 virtual ~KeyPairObserver() {};
80 gboolean close_called
;
83 NS_IMPL_ISUPPORTS1 (KeyPairObserver
, nsIObserver
);
85 NS_IMETHODIMP
KeyPairObserver::Observe (nsISupports
*aSubject
, const char *aTopic
,
86 const PRUnichar
*aData
)
92 /* ------------------------------------------------------------ */
94 begin_busy (GtkWidget
*widget
)
96 static GdkCursor
*cursor
= NULL
;
98 if (cursor
== NULL
) cursor
= gdk_cursor_new (GDK_WATCH
);
100 if (!GTK_WIDGET_REALIZED (widget
)) gtk_widget_realize (GTK_WIDGET(widget
));
102 gdk_window_set_cursor (GTK_WIDGET (widget
)->window
, cursor
);
104 /* Eek! FIXME: AutoJSContextStack! */
105 while (gtk_events_pending ()) gtk_main_iteration ();
109 end_busy (GtkWidget
*widget
)
111 gdk_window_set_cursor (GTK_WIDGET(widget
)->window
, NULL
);
119 KeyPairObserver
*helper
;
124 generating_timeout_cb (KeyPairInfo
*info
)
126 gtk_progress_bar_pulse (GTK_PROGRESS_BAR (info
->progress
));
128 if (info
->helper
->close_called
)
130 gtk_dialog_response (GTK_DIALOG (info
->dialog
), GTK_RESPONSE_OK
);
136 /* void displayGeneratingKeypairInfo (in nsIInterfaceRequestor ctx,
137 in nsIKeygenThread runnable); */
139 GtkNSSKeyPairDialogs::DisplayGeneratingKeypairInfo (nsIInterfaceRequestor
*ctx
,
140 nsIKeygenThread
*runnable
)
142 GtkWidget
*dialog
, *progress
, *label
, *vbox
;
146 AutoJSContextStack stack
;
148 if (NS_FAILED (rv
)) return rv
;
150 nsCOMPtr
<nsIDOMWindow
> parent
= do_GetInterface (ctx
);
151 GtkWindow
*gparent
= GTK_WINDOW (EphyUtils::FindGtkParent (parent
));
153 AutoWindowModalState
modalState (parent
);
155 dialog
= gtk_dialog_new_with_buttons ("", gparent
,
156 GTK_DIALOG_DESTROY_WITH_PARENT
, (char *) NULL
);
160 gtk_window_group_add_window (ephy_gui_ensure_window_group (gparent
),
161 GTK_WINDOW (dialog
));
164 gtk_window_set_icon_name (GTK_WINDOW (dialog
), EPHY_STOCK_EPHY
);
166 gtk_dialog_set_has_separator (GTK_DIALOG (dialog
), FALSE
);
167 gtk_window_set_resizable (GTK_WINDOW (dialog
), FALSE
);
168 gtk_container_set_border_width (GTK_CONTAINER (dialog
), 5);
170 vbox
= gtk_vbox_new (FALSE
, 12);
171 gtk_container_set_border_width (GTK_CONTAINER (vbox
), 5);
172 gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dialog
)->vbox
), vbox
, TRUE
, TRUE
, 0);
174 label
= gtk_label_new (NULL
);
175 gtk_label_set_use_markup (GTK_LABEL (label
), TRUE
);
176 gtk_label_set_line_wrap (GTK_LABEL (label
), TRUE
);
177 gtk_misc_set_alignment (GTK_MISC (label
), 0.0, 0.0);
178 gtk_box_pack_start (GTK_BOX (vbox
), label
, TRUE
, TRUE
, 0);
180 char *msg
= g_strdup_printf ("<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s",
181 _("Generating Private Key."),
182 _("Please wait while a new private key is "
183 "generated. This process could take a few minutes." ));
184 gtk_label_set_markup (GTK_LABEL(label
), msg
);
187 progress
= gtk_progress_bar_new ();
188 gtk_box_pack_start (GTK_BOX (vbox
), progress
, TRUE
, TRUE
, 0);
190 /* Create a helper class that just waits for close events
191 * from the other thread */
192 nsCOMPtr
<KeyPairObserver
> helper
= new KeyPairObserver
;
194 KeyPairInfo callback_data
= { progress
, dialog
, helper
};
195 timeout_id
= g_timeout_add (100, (GSourceFunc
)generating_timeout_cb
, &callback_data
);
197 gtk_widget_show_all (dialog
);
198 gtk_widget_hide (GTK_DIALOG (dialog
)->action_area
);
201 runnable
->StartKeyGeneration (helper
);
202 int res
= gtk_dialog_run (GTK_DIALOG (dialog
));
203 if (res
!= GTK_RESPONSE_OK
&& helper
->close_called
== FALSE
)
205 /* Ignore the already_closed flag, our nsIDOMWindowInterna::Close
206 * function just sets a flag, it doesn't close the window, so we
207 * dont have a race condition */
208 PRBool already_closed
= FALSE
;
209 runnable
->UserCanceled (&already_closed
);
212 g_source_remove (timeout_id
);
214 gtk_widget_destroy (dialog
);