Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / embed / mozilla / EphyPromptService.cpp
blob8e16d36b16bdf23a503f154d5226412aca1c50cf
1 /*
2 * Copyright © 2005, 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: EphyPromptService.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-object.h>
26 #include <glib/gi18n.h>
27 #include <gtk/gtk.h>
29 #include <nsStringAPI.h>
31 #include <nsCOMPtr.h>
32 #include <nsIDOMWindow.h>
33 #include <nsServiceManagerUtils.h>
35 #include "ephy-embed-shell.h"
36 #include "ephy-gui.h"
37 #include "ephy-debug.h"
39 #include "AutoJSContextStack.h"
40 #include "AutoWindowModalState.h"
41 #include "EphyUtils.h"
43 #include "EphyPromptService.h"
45 #define TIMEOUT 1000 /* ms */
46 #define TIMEOUT_DATA_KEY "timeout"
48 #define MAX_MESSAGE_LENGTH 512
49 #define MAX_TITLE_LENGTH 256
50 #define MAX_BUTTON_TEXT_LENGTH 128
52 enum
54 RESPONSE_ABORT_SCRIPT = 42
57 class Prompter
59 public:
60 Prompter (const char*, nsIDOMWindow*, const PRUnichar*, const PRUnichar*);
61 ~Prompter();
63 void AddStockButton (const char*, int);
64 void AddButtonWithFlags (PRInt32, PRUint32, const PRUnichar*, PRUint32);
65 void AddButtonsWithFlags (PRUint32, const PRUnichar*, const PRUnichar*, const PRUnichar*);
66 void AddCheckbox (const PRUnichar*, PRBool*);
67 void GetCheckboxState (PRBool *);
68 void AddEntry (const char *, const PRUnichar *, PRBool);
69 void GetText (PRUint32, PRUnichar **);
70 void AddSelect (PRUint32, const PRUnichar **, PRInt32);
71 void GetSelected (PRInt32*);
73 PRInt32 Run (PRBool * = nsnull);
74 void Show ();
76 PRBool IsCalledFromScript ();
77 void PerformScriptAbortion ();
79 char *ConvertAndTruncateString (const PRUnichar *, PRInt32 = -1);
80 char* ConvertAndEscapeButtonText (const PRUnichar *, PRInt32 = -1);
82 private:
83 nsCOMPtr<nsIDOMWindow> mWindow;
84 GtkDialog *mDialog;
85 GtkWidget *mVBox;
86 GtkWidget *mCheck;
87 GtkSizeGroup *mSizeGroup;
88 GtkWidget *mEntries[2];
89 GtkWidget *mCombo;
90 PRInt32 mNumButtons;
91 PRInt32 mNumEntries;
92 PRInt32 mDefaultResponse;
93 PRInt32 mUnaffirmativeResponse;
94 PRInt32 mResponse;
95 PRBool mSuccess;
96 PRBool mDelay;
99 Prompter::Prompter (const char *aStock,
100 nsIDOMWindow *aParent,
101 const PRUnichar *aTitle,
102 const PRUnichar *aText)
103 : mWindow (aParent)
104 , mDialog(nsnull)
105 , mVBox(nsnull)
106 , mCheck(nsnull)
107 , mSizeGroup(nsnull)
108 , mCombo(nsnull)
109 , mNumButtons(0)
110 , mNumEntries(0)
111 , mDefaultResponse(GTK_RESPONSE_ACCEPT)
112 , mUnaffirmativeResponse(0)
113 , mResponse(GTK_RESPONSE_CANCEL)
114 , mSuccess(PR_FALSE)
115 , mDelay(PR_FALSE)
117 GtkWidget *parent, *hbox, *label, *image;
119 g_object_ref (ephy_embed_shell_get_default ());
121 mEntries[0] = mEntries[1] = nsnull;
123 mDialog = GTK_DIALOG (gtk_dialog_new ());
124 g_object_ref_sink (mDialog);
126 char *title = NULL;
127 if (aTitle)
129 title = ConvertAndTruncateString (aTitle, MAX_TITLE_LENGTH);
132 gtk_window_set_title (GTK_WINDOW (mDialog), title ? title : "");
133 g_free (title);
135 gtk_window_set_modal (GTK_WINDOW (mDialog), TRUE);
137 parent = EphyUtils::FindGtkParent (aParent);
138 if (GTK_IS_WINDOW (parent))
140 gtk_window_set_transient_for (GTK_WINDOW (mDialog),
141 GTK_WINDOW (parent));
143 gtk_window_group_add_window (ephy_gui_ensure_window_group (GTK_WINDOW (parent)),
144 GTK_WINDOW (mDialog));
147 gtk_dialog_set_has_separator (mDialog, FALSE);
148 gtk_window_set_resizable (GTK_WINDOW (mDialog), FALSE);
149 gtk_container_set_border_width (GTK_CONTAINER (mDialog), 5);
150 gtk_box_set_spacing (GTK_BOX (mDialog->vbox), 14); /* 2 * 5 + 14 = 24 */
152 hbox = gtk_hbox_new (FALSE, 12);
153 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
154 gtk_container_add (GTK_CONTAINER (GTK_DIALOG (mDialog)->vbox), hbox);
156 image = gtk_image_new_from_stock (aStock, GTK_ICON_SIZE_DIALOG);
157 gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
158 gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
160 mVBox = gtk_vbox_new (FALSE, 12);
161 gtk_box_pack_start (GTK_BOX (hbox), mVBox, TRUE, TRUE, 0);
163 char *text = NULL;
164 if (aText)
166 text = ConvertAndTruncateString (aText, MAX_MESSAGE_LENGTH);
169 label = gtk_label_new (text);
170 g_free (text);
172 gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
173 /* Guard against overlong nonbreakable text (exploit) */
174 gtk_label_set_line_wrap_mode (GTK_LABEL (label), PANGO_WRAP_WORD_CHAR);
175 gtk_label_set_selectable (GTK_LABEL (label), TRUE);
176 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.0);
178 gtk_box_pack_start (GTK_BOX (mVBox), label, FALSE, FALSE, 0);
179 gtk_widget_show (label);
181 if (IsCalledFromScript ())
183 gtk_dialog_add_button (GTK_DIALOG (mDialog),
184 _("_Abort Script"),
185 RESPONSE_ABORT_SCRIPT);
188 gtk_widget_show (image);
189 gtk_widget_show (mVBox);
190 gtk_widget_show (hbox);
193 Prompter::~Prompter ()
195 if (mSizeGroup)
197 g_object_unref (mSizeGroup);
200 gtk_widget_destroy (GTK_WIDGET (mDialog));
201 g_object_unref (mDialog);
203 g_object_unref (ephy_embed_shell_get_default ());
206 void
207 Prompter::AddStockButton (const char *aStock,
208 int aResponse)
210 gtk_dialog_add_button (GTK_DIALOG (mDialog),
211 aStock, aResponse);
212 ++mNumButtons;
215 void
216 Prompter::AddButtonWithFlags (PRInt32 aNum,
217 PRUint32 aFlags,
218 const PRUnichar *aText,
219 PRUint32 aDefault)
221 if (aFlags == 0) return;
223 const char *label = NULL;
224 char *freeme = NULL;
225 gboolean isAffirmative = FALSE;
226 switch (aFlags)
228 case nsIPromptService::BUTTON_TITLE_OK:
229 label = GTK_STOCK_OK;
230 isAffirmative = TRUE;
231 break;
233 case nsIPromptService::BUTTON_TITLE_CANCEL:
234 label = GTK_STOCK_CANCEL;
235 break;
237 case nsIPromptService::BUTTON_TITLE_YES:
238 label = GTK_STOCK_YES;
239 isAffirmative = TRUE;
240 break;
242 case nsIPromptService::BUTTON_TITLE_NO:
243 label = GTK_STOCK_NO;
244 break;
246 case nsIPromptService::BUTTON_TITLE_SAVE:
247 label = GTK_STOCK_SAVE;
248 isAffirmative = TRUE;
249 break;
251 case nsIPromptService::BUTTON_TITLE_DONT_SAVE:
252 label = _("Don't Save");
253 break;
255 case nsIPromptService::BUTTON_TITLE_REVERT:
256 label = GTK_STOCK_REVERT_TO_SAVED;
257 break;
259 case nsIPromptService::BUTTON_TITLE_IS_STRING:
260 default:
261 label = freeme = ConvertAndEscapeButtonText (aText, MAX_BUTTON_TEXT_LENGTH);
262 /* We can't tell, so assume it's affirmative */
263 isAffirmative = TRUE;
264 break;
267 if (label == NULL) return;
269 gtk_dialog_add_button (mDialog, label, aNum);
270 ++mNumButtons;
272 if (isAffirmative && mDelay)
274 gtk_dialog_set_response_sensitive (mDialog, aNum, FALSE);
277 if (!isAffirmative)
279 mUnaffirmativeResponse = aNum;
282 if (aDefault)
284 mDefaultResponse = aNum;
287 g_free (freeme);
290 void
291 Prompter::AddButtonsWithFlags (PRUint32 aFlags,
292 const PRUnichar *aText0,
293 const PRUnichar *aText1,
294 const PRUnichar *aText2)
296 mDelay = (aFlags & nsIPromptService::BUTTON_DELAY_ENABLE) != 0;
297 mDefaultResponse = -1;
299 /* Reverse the order, on the assumption that what we passed is the
300 * 'windows' button order, and we want HIG order.
302 AddButtonWithFlags (2, ((aFlags / nsIPromptService::BUTTON_POS_2) & 0xff), aText2,
303 aFlags & nsIPromptService::BUTTON_POS_2_DEFAULT);
304 AddButtonWithFlags (1, ((aFlags / nsIPromptService::BUTTON_POS_1) & 0xff), aText1,
305 aFlags & nsIPromptService::BUTTON_POS_1_DEFAULT);
306 AddButtonWithFlags (0, ((aFlags / nsIPromptService::BUTTON_POS_0) & 0xff), aText0,
307 aFlags & nsIPromptService::BUTTON_POS_0_DEFAULT);
309 /* If no default was set, use the 'rightmost' unaffirmative response.
310 * This happens with the suite's password manager prompt.
312 if (mDefaultResponse == -1)
314 mDefaultResponse = mUnaffirmativeResponse;
318 void
319 Prompter::AddCheckbox (const PRUnichar *aText,
320 PRBool *aState)
322 if (!aState || !aText) return;
324 char *label = ConvertAndEscapeButtonText (aText, 2 * MAX_BUTTON_TEXT_LENGTH);
325 mCheck = gtk_check_button_new_with_mnemonic (label);
326 g_free (label);
328 gtk_label_set_line_wrap (GTK_LABEL (GTK_BIN (mCheck)->child), TRUE);
329 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (mCheck), *aState);
330 gtk_box_pack_start (GTK_BOX (mVBox), mCheck, FALSE, FALSE, 0);
331 gtk_widget_show (mCheck);
334 void
335 Prompter::GetCheckboxState (PRBool *aState)
337 if (!aState || !mCheck) return;
339 *aState = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (mCheck));
342 void
343 Prompter::AddEntry (const char *aLabel,
344 const PRUnichar *aValue,
345 PRBool aIsPassword)
347 if (!mSizeGroup)
349 mSizeGroup = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
352 GtkWidget *hbox = gtk_hbox_new (FALSE, 12);
353 gtk_box_pack_start (GTK_BOX (mVBox), hbox, FALSE, FALSE, 0);
355 GtkWidget *label = nsnull;
356 if (aLabel)
358 label = gtk_label_new_with_mnemonic (aLabel);
359 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
360 gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
361 gtk_size_group_add_widget (mSizeGroup, label);
364 GtkWidget *entry = mEntries[mNumEntries++] = gtk_entry_new ();
365 gtk_entry_set_visibility (GTK_ENTRY (entry), !aIsPassword);
366 gtk_entry_set_activates_default(GTK_ENTRY (entry), TRUE);
368 if (aValue)
370 nsCString cValue;
371 NS_UTF16ToCString (nsDependentString(aValue),
372 NS_CSTRING_ENCODING_UTF8, cValue);
374 gtk_entry_set_text (GTK_ENTRY (entry), cValue.get());
377 if (label)
379 gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);
382 gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
383 gtk_widget_show_all (hbox);
386 void
387 Prompter::GetText (PRUint32 aNum,
388 PRUnichar **aValue)
390 if (!aValue || !mEntries[aNum]) return;
392 const char *text = gtk_entry_get_text (GTK_ENTRY (mEntries[aNum]));
393 if (!text) return;
395 nsString value;
396 NS_CStringToUTF16 (nsDependentCString (text),
397 NS_CSTRING_ENCODING_UTF8, value);
399 *aValue = NS_StringCloneData (value);
402 void
403 Prompter::AddSelect (PRUint32 aCount,
404 const PRUnichar **aList,
405 PRInt32 aDefault)
407 mCombo = gtk_combo_box_new_text ();
409 for (PRUint32 i = 0; i < aCount; i++)
411 /* FIXME: use "" instead in this case? */
412 if (!aList[i] || !aList[i][0]) continue;
414 nsCString cData;
415 NS_UTF16ToCString (nsDependentString(aList[i]), NS_CSTRING_ENCODING_UTF8, cData);
417 gtk_combo_box_append_text (GTK_COMBO_BOX (mCombo), cData.get());
420 gtk_combo_box_set_active (GTK_COMBO_BOX (mCombo), aDefault);
422 gtk_box_pack_start (GTK_BOX (mVBox), mCombo, FALSE, FALSE, 0);
423 gtk_widget_show (mCombo);
426 void
427 Prompter::GetSelected (PRInt32 *aSelected)
429 if (!aSelected || !mCombo) return;
431 *aSelected = gtk_combo_box_get_active (GTK_COMBO_BOX (mCombo));
434 static gboolean
435 EnableResponse (GtkDialog *aDialog)
437 g_object_steal_data (G_OBJECT (aDialog), TIMEOUT_DATA_KEY);
439 gtk_dialog_set_response_sensitive (aDialog, 0, TRUE);
440 gtk_dialog_set_response_sensitive (aDialog, 1, TRUE);
441 gtk_dialog_set_response_sensitive (aDialog, 2, TRUE);
443 return FALSE;
446 static void
447 RemoveTimeout (gpointer idptr)
449 guint timeout = GPOINTER_TO_UINT (idptr);
451 g_return_if_fail (timeout != 0);
453 g_source_remove (timeout);
456 PRInt32
457 Prompter::Run (PRBool *aSuccess)
459 #if 0
460 AutoEventQueue queue;
461 if (NS_FAILED (queue.Init()))
463 if (aSuccess)
465 *aSuccess = PR_FALSE;
467 mSuccess = PR_FALSE;
469 return GTK_RESPONSE_CANCEL;
471 #endif
473 nsresult rv;
474 AutoJSContextStack stack;
475 rv = stack.Init ();
476 if (NS_FAILED (rv)) return rv;
478 AutoWindowModalState modalState (mWindow);
480 if (mDelay)
482 guint timeout = g_timeout_add (TIMEOUT,
483 (GSourceFunc) EnableResponse,
484 mDialog);
485 g_object_set_data_full (G_OBJECT (mDialog), TIMEOUT_DATA_KEY,
486 GUINT_TO_POINTER (timeout),
487 (GDestroyNotify) RemoveTimeout);
490 gtk_dialog_set_default_response (GTK_DIALOG (mDialog), mDefaultResponse);
492 GtkWidget *widget = GTK_WIDGET (mDialog);
493 gtk_widget_show (widget);
494 mResponse = gtk_dialog_run (mDialog);
495 gtk_widget_hide (widget);
497 g_object_set_data (G_OBJECT (mDialog), TIMEOUT_DATA_KEY, NULL);
499 mSuccess = (GTK_RESPONSE_ACCEPT == mResponse);
500 if (aSuccess)
502 *aSuccess = mSuccess;
505 if (mResponse == RESPONSE_ABORT_SCRIPT)
507 PerformScriptAbortion ();
510 return mResponse;
513 static void
514 DeletePrompter (gpointer aPromptPtr,
515 GObject *aZombie)
517 Prompter *prompt = NS_STATIC_CAST (Prompter*, aPromptPtr);
519 delete prompt;
522 void
523 Prompter::Show ()
525 /* We don't need it anymore */
526 mWindow = nsnull;
528 gtk_window_set_modal (GTK_WINDOW (mDialog), FALSE);
530 g_signal_connect (mDialog, "response",
531 G_CALLBACK (gtk_widget_destroy), NULL);
532 g_object_weak_ref (G_OBJECT (mDialog),
533 (GWeakNotify) DeletePrompter,
534 NS_STATIC_CAST (gpointer, this));
536 gtk_widget_show (GTK_WIDGET (mDialog));
539 PRBool
540 Prompter::IsCalledFromScript()
542 #if 0
543 nsCOMPtr<nsIXPConnect> xpconnect (do_GetService (nsIXPConnect::GetCID()));
544 NS_ENSURE_TRUE (xpconnect, PR_FALSE);
546 nsresult rv;
547 nsCOMPtr<nsIXPCNativeCallContext> ncc;
548 rv = xpconnect->GetCurrentNativeCallContext (getter_AddRefs (ncc));
549 NS_ENSURE_SUCCESS (rv, PR_FALSE);
551 if (!ncc) return PR_FALSE;
553 JSContext *cx = nsnull;
554 rv = ncc->GetJSContext (&cx);
555 g_print ("GetJSContext rv=%x, cx=%p\n", rv, cx);
557 NS_ENSURE_SUCCESS (rv, PR_FALSE);
559 return cx != nsnull;
560 #endif
561 return PR_FALSE;
564 void
565 Prompter::PerformScriptAbortion()
567 #if 0
568 /* FIXME: can we only stop the calling script, not all scripts in the context? */
570 nsCOMPtr<nsIXPConnect> xpconnect (do_GetService (nsIXPConnect::GetCID()));
571 NS_ENSURE_TRUE (xpconnect, );
573 nsresult rv;
574 nsCOMPtr<nsIXPCNativeCallContext> ncc;
575 rv = xpconnect->GetCurrentNativeCallContext (getter_AddRefs (ncc));
576 NS_ENSURE_SUCCESS (rv, );
577 NS_ENSURE_TRUE (ncc, );
579 JSContext *cx = nsnull;
580 rv = ncc->GetJSContext (&cx);
581 g_print ("GetJSContext rv=%x, cx=%p\n", rv, cx);
582 NS_ENSURE_SUCCESS (rv, );
583 NS_ENSURE_TRUE (cx, );
585 g_print ("Would now disable scripts\n");
586 // MozillaPrivate::SetScriptsEnabled (cx, PR_FALSE, PR_FALSE);
587 #endif
590 char *
591 Prompter::ConvertAndTruncateString (const PRUnichar *aText,
592 PRInt32 aMaxLength)
594 if (aText == nsnull) return NULL;
596 /* This depends on the assumption that
597 * typeof(PRUnichar) == typeof (gunichar2) == uint16,
598 * which should be pretty safe.
600 glong n_read = 0, n_written = 0;
601 char *converted = g_utf16_to_utf8 ((gunichar2*) aText, aMaxLength,
602 &n_read, &n_written, NULL);
603 /* FIXME loop from the end while !g_unichar_isspace (char)? */
605 return converted;
608 char *
609 Prompter::ConvertAndEscapeButtonText(const PRUnichar *aText,
610 PRInt32 aMaxLength)
612 char *converted = ConvertAndTruncateString (aText, aMaxLength);
613 if (converted == NULL) return NULL;
615 char *escaped = (char*) g_malloc (strlen (converted) + 1);
616 char *q = escaped;
617 for (const char *p = converted; *p; ++p, ++q)
619 if (*p == '&')
621 if (*(p+1) == '&')
623 *q = '&';
624 ++p;
626 else
628 *q = '_';
631 else
633 *q = *p;
637 /* Null termination */
638 *q = '\0';
640 g_free (converted);
642 return escaped;
645 /* FIXME: needs THREADSAFE? */
646 #if HAVE_NSINONBLOCKINGALERTSERVICE_H
647 NS_IMPL_ISUPPORTS2 (EphyPromptService,
648 nsIPromptService,
649 nsINonBlockingAlertService)
650 #else
651 NS_IMPL_ISUPPORTS1 (EphyPromptService,
652 nsIPromptService)
653 #endif
655 EphyPromptService::EphyPromptService()
657 LOG ("EphyPromptService ctor (%p)", this);
660 EphyPromptService::~EphyPromptService()
662 LOG ("EphyPromptService dtor (%p)", this);
665 /* nsIPromptService implementation */
667 /* void alert (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText); */
668 NS_IMETHODIMP
669 EphyPromptService::Alert (nsIDOMWindow *aParent,
670 const PRUnichar *aDialogTitle,
671 const PRUnichar *aText)
673 Prompter prompt (GTK_STOCK_DIALOG_INFO, aParent, aDialogTitle, aText);
674 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
675 prompt.Run ();
677 return NS_OK;
680 /* void alertCheck (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, in wstring aCheckMsg, inout boolean aCheckState); */
681 NS_IMETHODIMP
682 EphyPromptService::AlertCheck (nsIDOMWindow *aParent,
683 const PRUnichar *aDialogTitle,
684 const PRUnichar *aText,
685 const PRUnichar *aCheckMsg,
686 PRBool *aCheckState)
688 Prompter prompt (GTK_STOCK_DIALOG_INFO, aParent, aDialogTitle, aText);
689 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
690 prompt.AddCheckbox (aCheckMsg, aCheckState);
692 prompt.Run ();
693 prompt.GetCheckboxState (aCheckState);
695 return NS_OK;
698 /* boolean confirm (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText); */
699 NS_IMETHODIMP
700 EphyPromptService::Confirm (nsIDOMWindow *aParent,
701 const PRUnichar *aDialogTitle,
702 const PRUnichar *aText,
703 PRBool *_retval)
705 NS_ENSURE_ARG_POINTER (_retval);
707 Prompter prompt (GTK_STOCK_DIALOG_QUESTION, aParent, aDialogTitle, aText);
708 prompt.AddStockButton (GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
709 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
710 prompt.Run (_retval);
712 return NS_OK;
715 /* boolean confirmCheck (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, in wstring aCheckMsg, inout boolean aCheckState); */
716 NS_IMETHODIMP
717 EphyPromptService::ConfirmCheck (nsIDOMWindow *aParent,
718 const PRUnichar *aDialogTitle,
719 const PRUnichar *aText,
720 const PRUnichar *aCheckMsg,
721 PRBool *aCheckState,
722 PRBool *_retval)
724 NS_ENSURE_ARG_POINTER (_retval);
726 Prompter prompt (GTK_STOCK_DIALOG_QUESTION, aParent, aDialogTitle, aText);
727 prompt.AddStockButton (GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
728 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
729 prompt.AddCheckbox (aCheckMsg, aCheckState);
731 prompt.Run (_retval);
732 prompt.GetCheckboxState (aCheckState);
734 return NS_OK;
737 /* PRInt32 confirmEx (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, in unsigned long aButtonFlags, in wstring aButton0Title, in wstring aButton1Title, in wstring aButton2Title, in wstring aCheckMsg, inout boolean aCheckState); */
738 NS_IMETHODIMP
739 EphyPromptService::ConfirmEx (nsIDOMWindow *aParent,
740 const PRUnichar *aDialogTitle,
741 const PRUnichar *aText,
742 PRUint32 aButtonFlags,
743 const PRUnichar *aButton0Title,
744 const PRUnichar *aButton1Title,
745 const PRUnichar *aButton2Title,
746 const PRUnichar *aCheckMsg,
747 PRBool *aCheckState,
748 PRInt32 *_retval)
750 NS_ENSURE_ARG_POINTER (_retval);
752 Prompter prompt (GTK_STOCK_DIALOG_QUESTION, aParent, aDialogTitle, aText);
753 prompt.AddButtonsWithFlags (aButtonFlags, aButton0Title,
754 aButton1Title, aButton2Title);
755 prompt.AddCheckbox (aCheckMsg, aCheckState);
757 *_retval = prompt.Run (nsnull);
758 prompt.GetCheckboxState (aCheckState);
760 return NS_OK;
763 /* boolean prompt (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, inout wstring aValue, in wstring aCheckMsg, inout boolean aCheckState); */
764 NS_IMETHODIMP
765 EphyPromptService::Prompt (nsIDOMWindow *aParent,
766 const PRUnichar *aDialogTitle,
767 const PRUnichar *aText,
768 PRUnichar **aValue,
769 const PRUnichar *aCheckMsg,
770 PRBool *aCheckState,
771 PRBool *_retval)
773 NS_ENSURE_ARG_POINTER (_retval);
774 NS_ENSURE_ARG_POINTER (aValue);
776 Prompter prompt (GTK_STOCK_DIALOG_QUESTION, aParent, aDialogTitle, aText);
777 prompt.AddStockButton (GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
778 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
779 prompt.AddEntry (nsnull, *aValue, PR_FALSE);
780 prompt.AddCheckbox (aCheckMsg, aCheckState);
782 prompt.Run (_retval);
783 prompt.GetText (0, aValue);
784 prompt.GetCheckboxState (aCheckState);
786 return NS_OK;
789 /* boolean promptUsernameAndPassword (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, inout wstring aUsername, inout wstring aPassword, in wstring aCheckMsg, inout boolean aCheckState); */
790 NS_IMETHODIMP
791 EphyPromptService::PromptUsernameAndPassword (nsIDOMWindow *aParent,
792 const PRUnichar *aDialogTitle,
793 const PRUnichar *aText,
794 PRUnichar **aUsername,
795 PRUnichar **aPassword,
796 const PRUnichar *aCheckMsg,
797 PRBool *aCheckState,
798 PRBool *_retval)
800 NS_ENSURE_ARG_POINTER (_retval);
801 NS_ENSURE_ARG_POINTER (aUsername);
802 NS_ENSURE_ARG_POINTER (aPassword);
804 Prompter prompt (GTK_STOCK_DIALOG_AUTHENTICATION, aParent, aDialogTitle, aText);
805 prompt.AddStockButton (GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
806 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
807 prompt.AddEntry (_("_Username:"), *aUsername, PR_FALSE);
808 prompt.AddEntry (_("_Password:"), *aPassword, PR_TRUE);
809 prompt.AddCheckbox (aCheckMsg, aCheckState);
811 prompt.Run (_retval);
812 prompt.GetText (0, aUsername);
813 prompt.GetText (1, aPassword);
814 prompt.GetCheckboxState (aCheckState);
816 return NS_OK;
819 /* boolean promptPassword (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, inout wstring aPassword, in wstring aCheckMsg, inout boolean aCheckState); */
820 NS_IMETHODIMP
821 EphyPromptService::PromptPassword (nsIDOMWindow *aParent,
822 const PRUnichar *aDialogTitle,
823 const PRUnichar *aText,
824 PRUnichar **aPassword,
825 const PRUnichar *aCheckMsg,
826 PRBool *aCheckState,
827 PRBool *_retval)
829 NS_ENSURE_ARG_POINTER (_retval);
830 NS_ENSURE_ARG_POINTER (aPassword);
832 Prompter prompt (GTK_STOCK_DIALOG_AUTHENTICATION, aParent, aDialogTitle, aText);
833 prompt.AddStockButton (GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
834 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
835 prompt.AddEntry (_("_Password:"), *aPassword, PR_TRUE);
836 prompt.AddCheckbox (aCheckMsg, aCheckState);
838 // FIXME: Add a CAPSLOCK indicator?
840 prompt.Run (_retval);
841 prompt.GetText (0, aPassword);
842 prompt.GetCheckboxState (aCheckState);
844 return NS_OK;
847 /* boolean select (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText, in PRUint32 aCount, [array, size_is (aCount)] in wstring aSelectList, out long aOutSelection); */
848 NS_IMETHODIMP
849 EphyPromptService::Select (nsIDOMWindow *aParent,
850 const PRUnichar *aDialogTitle,
851 const PRUnichar *aText,
852 PRUint32 aCount,
853 const PRUnichar **aSelectList,
854 PRInt32 *aOutSelection,
855 PRBool *_retval)
857 NS_ENSURE_ARG_POINTER (_retval);
858 NS_ENSURE_ARG_POINTER (aOutSelection);
860 Prompter prompt (GTK_STOCK_DIALOG_QUESTION, aParent, aDialogTitle, aText);
861 prompt.AddStockButton (GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
862 prompt.AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
863 prompt.AddSelect (aCount, aSelectList, *aOutSelection);
865 prompt.Run (_retval);
866 prompt.GetSelected (aOutSelection);
868 return NS_OK;
871 #if HAVE_NSINONBLOCKINGALERTSERVICE_H
873 /* showNonBlockingAlert (in nsIDOMWindow aParent, in wstring aDialogTitle, in wstring aText); */
874 NS_IMETHODIMP
875 EphyPromptService::ShowNonBlockingAlert (nsIDOMWindow *aParent,
876 const PRUnichar *aDialogTitle,
877 const PRUnichar *aText)
879 Prompter *prompt = new Prompter (GTK_STOCK_DIALOG_INFO, aParent, aDialogTitle, aText);
880 if (!prompt) return NS_ERROR_OUT_OF_MEMORY;
882 prompt->AddStockButton (GTK_STOCK_OK, GTK_RESPONSE_ACCEPT);
883 prompt->Show ();
885 return NS_OK;
888 #endif /* HAVE_NSINONBLOCKINGALERTSERVICE_H */