Back to index

lightning-sunbird  0.9+nobinonly
GtkPromptService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:expandtab:shiftwidth=4:tabstop=4: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2003
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *  Brian Ryner <bryner@brianryner.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "GtkPromptService.h"
00041 #include "EmbedPrompter.h"
00042 #include <nsString.h>
00043 #include <nsIWindowWatcher.h>
00044 #include <nsIWebBrowserChrome.h>
00045 #include <nsIEmbeddingSiteWindow.h>
00046 #include <nsCOMPtr.h>
00047 #include <nsIServiceManager.h>
00048 
00049 GtkPromptService::GtkPromptService()
00050 {
00051 }
00052 
00053 GtkPromptService::~GtkPromptService()
00054 {
00055 }
00056 
00057 NS_IMPL_ISUPPORTS1(GtkPromptService, nsIPromptService)
00058 
00059 NS_IMETHODIMP
00060 GtkPromptService::Alert(nsIDOMWindow* aParent, const PRUnichar* aDialogTitle, 
00061                         const PRUnichar* aDialogText)
00062 {
00063     EmbedPrompter prompter;
00064     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Alert").get());
00065     prompter.SetMessageText(aDialogText);
00066     prompter.Create(EmbedPrompter::TYPE_ALERT, 
00067                     GetGtkWindowForDOMWindow(aParent));
00068     prompter.Run();
00069     return NS_OK;
00070 }
00071 
00072 NS_IMETHODIMP
00073 GtkPromptService::AlertCheck(nsIDOMWindow* aParent,
00074                              const PRUnichar* aDialogTitle,
00075                              const PRUnichar* aDialogText,
00076                              const PRUnichar* aCheckMsg, PRBool* aCheckValue)
00077 {
00078     NS_ENSURE_ARG_POINTER(aCheckValue);
00079 
00080     EmbedPrompter prompter;
00081     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Alert").get());
00082     prompter.SetMessageText(aDialogText);
00083     prompter.SetCheckMessage(aCheckMsg);
00084     prompter.SetCheckValue(*aCheckValue);
00085     prompter.Create(EmbedPrompter::TYPE_ALERT_CHECK,
00086                     GetGtkWindowForDOMWindow(aParent));
00087     prompter.Run();
00088     prompter.GetCheckValue(aCheckValue);
00089     return NS_OK;
00090 }
00091 
00092 NS_IMETHODIMP
00093 GtkPromptService::Confirm(nsIDOMWindow* aParent,
00094                           const PRUnichar* aDialogTitle,
00095                           const PRUnichar* aDialogText, PRBool* aConfirm)
00096 {
00097     EmbedPrompter prompter;
00098     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Confirm").get());
00099     prompter.SetMessageText(aDialogText);
00100     prompter.Create(EmbedPrompter::TYPE_CONFIRM,
00101                     GetGtkWindowForDOMWindow(aParent));
00102     prompter.Run();
00103     prompter.GetConfirmValue(aConfirm);
00104     return NS_OK;
00105 }
00106 
00107 NS_IMETHODIMP
00108 GtkPromptService::ConfirmCheck(nsIDOMWindow* aParent,
00109                                const PRUnichar* aDialogTitle,
00110                                const PRUnichar* aDialogText,
00111                                const PRUnichar* aCheckMsg,
00112                                PRBool* aCheckValue, PRBool* aConfirm)
00113 {
00114     NS_ENSURE_ARG_POINTER(aCheckValue);
00115 
00116     EmbedPrompter prompter;
00117     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Confirm").get());
00118     prompter.SetMessageText(aDialogText);
00119     prompter.SetCheckMessage(aCheckMsg);
00120     prompter.SetCheckValue(*aCheckValue);
00121     prompter.Create(EmbedPrompter::TYPE_CONFIRM_CHECK,
00122                     GetGtkWindowForDOMWindow(aParent));
00123     prompter.Run();
00124     prompter.GetCheckValue(aCheckValue);
00125     prompter.GetConfirmValue(aConfirm);
00126     return NS_OK;
00127 }
00128 
00129 NS_IMETHODIMP
00130 GtkPromptService::ConfirmEx(nsIDOMWindow* aParent,
00131                             const PRUnichar* aDialogTitle,
00132                             const PRUnichar* aDialogText,
00133                             PRUint32 aButtonFlags,
00134                             const PRUnichar* aButton0Title,
00135                             const PRUnichar* aButton1Title,
00136                             const PRUnichar* aButton2Title,
00137                             const PRUnichar* aCheckMsg, PRBool* aCheckValue,
00138                             PRInt32* aRetVal)
00139 {
00140     EmbedPrompter prompter;
00141     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Confirm").get());
00142     prompter.SetMessageText(aDialogText);
00143 
00144     nsAutoString button0Label, button1Label, button2Label;
00145     GetButtonLabel(aButtonFlags, BUTTON_POS_0, aButton0Title, button0Label);
00146     GetButtonLabel(aButtonFlags, BUTTON_POS_1, aButton1Title, button1Label);
00147     GetButtonLabel(aButtonFlags, BUTTON_POS_2, aButton2Title, button2Label);
00148     prompter.SetButtons(button0Label.get(), button1Label.get(),
00149                         button2Label.get());
00150 
00151     if (aCheckMsg)
00152         prompter.SetCheckMessage(aCheckMsg);
00153     if (aCheckValue)
00154         prompter.SetCheckValue(*aCheckValue);
00155 
00156     prompter.Create(EmbedPrompter::TYPE_UNIVERSAL,
00157                     GetGtkWindowForDOMWindow(aParent));
00158     prompter.Run();
00159 
00160     if (aCheckValue)
00161         prompter.GetCheckValue(aCheckValue);
00162 
00163     prompter.GetButtonPressed(aRetVal);
00164 
00165     return NS_OK;
00166 }
00167 
00168 NS_IMETHODIMP
00169 GtkPromptService::Prompt(nsIDOMWindow* aParent, const PRUnichar* aDialogTitle,
00170                          const PRUnichar* aDialogText, PRUnichar** aValue,
00171                          const PRUnichar* aCheckMsg, PRBool* aCheckValue,
00172                          PRBool* aConfirm)
00173 {
00174     EmbedPrompter prompter;
00175     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Prompt").get());
00176     prompter.SetMessageText(aDialogText);
00177     prompter.SetTextValue(*aValue);
00178     if (aCheckMsg)
00179         prompter.SetCheckMessage(aCheckMsg);
00180     if (aCheckValue)
00181         prompter.SetCheckValue(*aCheckValue);
00182 
00183     prompter.Create(EmbedPrompter::TYPE_PROMPT,
00184                     GetGtkWindowForDOMWindow(aParent));
00185     prompter.Run();
00186     if (aCheckValue)
00187         prompter.GetCheckValue(aCheckValue);
00188     prompter.GetConfirmValue(aConfirm);
00189     if (*aConfirm) {
00190         if (*aValue)
00191             nsMemory::Free(*aValue);
00192         prompter.GetTextValue(aValue);
00193     }
00194     return NS_OK;
00195 }
00196 
00197 NS_IMETHODIMP
00198 GtkPromptService::PromptUsernameAndPassword(nsIDOMWindow* aParent,
00199                                             const PRUnichar* aDialogTitle,
00200                                             const PRUnichar* aDialogText,
00201                                             PRUnichar** aUsername,
00202                                             PRUnichar** aPassword,
00203                                             const PRUnichar* aCheckMsg,
00204                                             PRBool* aCheckValue,
00205                                             PRBool* aConfirm)
00206 {
00207     EmbedPrompter prompter;
00208     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Prompt").get());
00209     prompter.SetMessageText(aDialogText);
00210     prompter.SetUser(*aUsername);
00211     prompter.SetPassword(*aPassword);
00212     if (aCheckMsg)
00213         prompter.SetCheckMessage(aCheckMsg);
00214     if (aCheckValue)
00215         prompter.SetCheckValue(*aCheckValue);
00216 
00217     prompter.Create(EmbedPrompter::TYPE_PROMPT_USER_PASS,
00218                     GetGtkWindowForDOMWindow(aParent));
00219     prompter.Run();
00220     if (aCheckValue)
00221         prompter.GetCheckValue(aCheckValue);
00222     prompter.GetConfirmValue(aConfirm);
00223     if (*aConfirm) {
00224         if (*aUsername)
00225             nsMemory::Free(*aUsername);
00226         prompter.GetUser(aUsername);
00227 
00228         if (*aPassword)
00229             nsMemory::Free(*aPassword);
00230         prompter.GetPassword(aPassword);
00231     }
00232     return NS_OK;
00233 }
00234 
00235 NS_IMETHODIMP
00236 GtkPromptService::PromptPassword(nsIDOMWindow* aParent,
00237                                  const PRUnichar* aDialogTitle,
00238                                  const PRUnichar* aDialogText,
00239                                  PRUnichar** aPassword,
00240                                  const PRUnichar* aCheckMsg,
00241                                  PRBool* aCheckValue, PRBool* aConfirm)
00242 {
00243     EmbedPrompter prompter;
00244     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Prompt").get());
00245     prompter.SetMessageText(aDialogText);
00246     prompter.SetPassword(*aPassword);
00247     if (aCheckMsg)
00248         prompter.SetCheckMessage(aCheckMsg);
00249     if (aCheckValue)
00250         prompter.SetCheckValue(*aCheckValue);
00251 
00252     prompter.Create(EmbedPrompter::TYPE_PROMPT_PASS,
00253                     GetGtkWindowForDOMWindow(aParent));
00254     prompter.Run();
00255     if (aCheckValue)
00256         prompter.GetCheckValue(aCheckValue);
00257     prompter.GetConfirmValue(aConfirm);
00258     if (*aConfirm) {
00259         if (*aPassword)
00260             nsMemory::Free(*aPassword);
00261         prompter.GetPassword(aPassword);
00262     }
00263     return NS_OK;
00264 }
00265 
00266 NS_IMETHODIMP
00267 GtkPromptService::Select(nsIDOMWindow* aParent, const PRUnichar* aDialogTitle,
00268                          const PRUnichar* aDialogText, PRUint32 aCount,
00269                          const PRUnichar** aSelectList, PRInt32* outSelection,
00270                          PRBool* aConfirm)
00271 {
00272     EmbedPrompter prompter;
00273     prompter.SetTitle(aDialogTitle ? aDialogTitle : NS_LITERAL_STRING("Select").get());
00274     prompter.SetMessageText(aDialogText);
00275     prompter.SetItems(aSelectList, aCount);
00276     prompter.Create(EmbedPrompter::TYPE_SELECT,
00277                     GetGtkWindowForDOMWindow(aParent));
00278     prompter.Run();
00279     prompter.GetSelectedItem(outSelection);
00280     prompter.GetConfirmValue(aConfirm);
00281     return NS_OK;
00282 }
00283 
00284 GtkWindow*
00285 GtkPromptService::GetGtkWindowForDOMWindow(nsIDOMWindow* aDOMWindow)
00286 {
00287     nsCOMPtr<nsIWindowWatcher> wwatch = do_GetService("@mozilla.org/embedcomp/window-watcher;1");
00288 
00289     if (!aDOMWindow)
00290         return NULL;
00291 
00292     nsCOMPtr<nsIWebBrowserChrome> chrome;
00293     wwatch->GetChromeForWindow(aDOMWindow, getter_AddRefs(chrome));
00294     nsCOMPtr<nsIEmbeddingSiteWindow> siteWindow = do_QueryInterface(chrome);
00295 
00296     if (!siteWindow)
00297         return NULL;
00298 
00299     GtkWidget* parentWidget;
00300     siteWindow->GetSiteWindow((void**)&parentWidget);
00301 
00302     if (!parentWidget)
00303         return NULL;
00304 
00305     GtkWidget* gtkWin = gtk_widget_get_toplevel(parentWidget);
00306     if (GTK_WIDGET_TOPLEVEL(gtkWin))
00307         return GTK_WINDOW(gtkWin);
00308 
00309     return NULL;
00310 }
00311 
00312 void
00313 GtkPromptService::GetButtonLabel(PRUint32 aFlags, PRUint32 aPos,
00314                                  const PRUnichar* aStringValue,
00315                                  nsAString& aLabel)
00316 {
00317     PRUint32 posFlag = (aFlags & (255 * aPos)) / aPos;
00318     switch (posFlag) {
00319     case 0:
00320         break;
00321     case BUTTON_TITLE_OK:
00322         aLabel.AssignLiteral(GTK_STOCK_OK);
00323         break;
00324     case BUTTON_TITLE_CANCEL:
00325         aLabel.AssignLiteral(GTK_STOCK_CANCEL);
00326         break;
00327     case BUTTON_TITLE_YES:
00328         aLabel.AssignLiteral(GTK_STOCK_YES);
00329         break;
00330     case BUTTON_TITLE_NO:
00331         aLabel.AssignLiteral(GTK_STOCK_NO);
00332         break;
00333     case BUTTON_TITLE_SAVE:
00334         aLabel.AssignLiteral(GTK_STOCK_SAVE);
00335         break;
00336     case BUTTON_TITLE_DONT_SAVE:
00337         aLabel.AssignLiteral("Don't Save");
00338         break;
00339     case BUTTON_TITLE_REVERT:
00340         aLabel.AssignLiteral("Revert");
00341         break;
00342     case BUTTON_TITLE_IS_STRING:
00343         aLabel = aStringValue;
00344         break;
00345     default:
00346         NS_WARNING("Unexpected button flags");
00347     }
00348 }