Back to index

lightning-sunbird  0.9+nobinonly
PromptService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* PromptService is intended to override the default Mozilla PromptService,
00039    giving nsIPrompt implementations of our own design, rather than using
00040    Mozilla's. Do this by building this into a component and registering the
00041    factory with the same CID/ContractID as Mozilla's (see MfcEmbed.cpp).
00042 */
00043 
00044 #include "PromptService.h"
00045 #include "nsCOMPtr.h"
00046 #include "nsMemory.h"
00047 #include "nsString.h"
00048 #include "nsReadableUtils.h"
00049 #include "nsIDOMWindow.h"
00050 #include "nsIEmbeddingSiteWindow.h"
00051 #include "nsIFactory.h"
00052 #include "nsIPromptService.h"
00053 #include "nsPIPromptService.h"
00054 #include "nsIServiceManager.h"
00055 #include "nsIWebBrowserChrome.h"
00056 #include "nsIWindowWatcher.h"
00057 #include "PtMozilla.h"
00058 
00059 
00060 //*****************************************************************************
00061 // CPromptService
00062 //*****************************************************************************
00063 
00064 class CPromptService: public nsIPromptService, public nsPIPromptService {
00065 public:
00066                  CPromptService();
00067   virtual       ~CPromptService();
00068 
00069   NS_DECL_ISUPPORTS
00070   NS_DECL_NSIPROMPTSERVICE
00071   NS_DECL_NSPIPROMPTSERVICE
00072 
00073 private:
00074        nsCOMPtr<nsIWindowWatcher> mWWatch;
00075        PtWidget_t *GetWebBrowser(nsIDOMWindow *aWindow);
00076        int InvokeDialogCallback(PtWidget_t *w, int type, char *title, char *text, char *checkbox_msg, int *value);
00077 };
00078 
00079 //*****************************************************************************
00080 
00081 //NS_IMPL_ISUPPORTS1(CPromptService, nsIPromptService)
00082 NS_IMPL_ISUPPORTS2(CPromptService, nsIPromptService, nsPIPromptService)
00083 
00084 CPromptService::CPromptService() :
00085        mWWatch(do_GetService("@mozilla.org/embedcomp/window-watcher;1")) {
00086        NS_INIT_ISUPPORTS();
00087        }
00088 
00089 CPromptService::~CPromptService() {
00090        }
00091 
00092 PtWidget_t *CPromptService::GetWebBrowser(nsIDOMWindow *aWindow)
00093 {
00094   nsCOMPtr<nsIWebBrowserChrome> chrome;
00095   PtWidget_t *val = 0;
00096 
00097        nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService("@mozilla.org/embedcomp/window-watcher;1"));
00098        if (!wwatch) return nsnull;
00099 
00100   if( wwatch ) {
00101     nsCOMPtr<nsIDOMWindow> fosterParent;
00102     if (!aWindow) { // it will be a dependent window. try to find a foster parent.
00103       wwatch->GetActiveWindow(getter_AddRefs(fosterParent));
00104       aWindow = fosterParent;
00105     }
00106     wwatch->GetChromeForWindow(aWindow, getter_AddRefs(chrome));
00107   }
00108 
00109   if (chrome) {
00110     nsCOMPtr<nsIEmbeddingSiteWindow> site(do_QueryInterface(chrome));
00111     if (site) {
00112       site->GetSiteWindow(reinterpret_cast<void **>(&val));
00113     }
00114   }
00115 
00116   return val;
00117 }
00118 
00119 int 
00120 CPromptService::InvokeDialogCallback(PtWidget_t *w, int type, char *title, char *text, char *checkbox_msg, int *value)
00121 {
00122     PtMozillaWidget_t   *moz = (PtMozillaWidget_t *) w;
00123     PtCallbackList_t    *cb;
00124     PtCallbackInfo_t    cbinfo;
00125     PtMozillaDialogCb_t dlg;
00126               PtWidget_t *parent;
00127     int ret;
00128 
00129     if (!moz->dialog_cb)
00130         return NS_OK;
00131 
00132     cb = moz->dialog_cb;
00133     memset(&cbinfo, 0, sizeof(cbinfo));
00134     cbinfo.reason = Pt_CB_MOZ_DIALOG;
00135     cbinfo.cbdata = &dlg;
00136 
00137     memset(&dlg, 0, sizeof(PtMozillaDialogCb_t));
00138     dlg.type = type;
00139     dlg.title = title;
00140     dlg.text = text;
00141        dlg.checkbox_message = checkbox_msg;
00142               parent = PtFindDisjoint( w );
00143               dlg.parent = parent ? parent : w;  
00144 
00145     ret = PtInvokeCallbackList(cb, (PtWidget_t *)moz, &cbinfo);
00146     if (value)
00147         *value = dlg.ret_value;
00148     return (ret);
00149 }
00150 
00151 NS_IMETHODIMP CPromptService::Alert(nsIDOMWindow *parent,
00152                                                                const PRUnichar *dialogTitle,
00153                                     const PRUnichar *text)
00154 {
00155        nsString                    mTitle(dialogTitle);
00156        nsString                    mText(text);
00157        PtWidget_t *w = GetWebBrowser( parent );
00158        char *title = ToNewCString(mTitle), *ptext = ToNewCString(mText);
00159 
00160        if( w ) InvokeDialogCallback(w, Pt_MOZ_DIALOG_ALERT, title, ptext, nsnull, nsnull);
00161        else {
00162               char const *btns[] = { "&Ok" };
00163               PtAlert( NULL, NULL, title, NULL, ptext, NULL, 1, btns, NULL, 1, 1, 0 );
00164               }
00165 
00166        if( title ) nsMemory::Free( (void*)title );
00167        if( ptext ) nsMemory::Free( (void*)ptext );
00168 
00169        return NS_OK;
00170        }
00171 
00172 NS_IMETHODIMP CPromptService::AlertCheck(nsIDOMWindow *parent,
00173                                          const PRUnichar *dialogTitle,
00174                                          const PRUnichar *text,
00175                                          const PRUnichar *checkboxMsg,
00176                                          PRBool *checkValue)
00177 {
00178        nsString      mTitle(dialogTitle);
00179        nsString      mText(text);
00180        nsString      mMsg(checkboxMsg);
00181        int           ret = 0;
00182        PtWidget_t *w = GetWebBrowser( parent );
00183        char *title = ToNewCString(mTitle), *ptext = ToNewCString(mText), *msg = ToNewCString(mMsg);
00184 
00185        InvokeDialogCallback(w, Pt_MOZ_DIALOG_ALERT, title, ptext, msg, &ret);
00186        *checkValue = ret;
00187 
00188        if( title ) nsMemory::Free( (void*)title );
00189        if( ptext ) nsMemory::Free( (void*)ptext );
00190        if( msg ) nsMemory::Free( (void*)msg );
00191 
00192        return NS_OK;
00193        }
00194 
00195 
00196 NS_IMETHODIMP CPromptService::Confirm(nsIDOMWindow *parent,
00197                                       const PRUnichar *dialogTitle,
00198                                       const PRUnichar *text,
00199                                       PRBool *_retval)
00200 {
00201        nsString                    mTitle(dialogTitle);
00202        nsString                    mText(text);
00203        PtWidget_t *w = GetWebBrowser( parent );
00204        char *title = ToNewCString(mTitle), *ptext = ToNewCString(mText);
00205 
00206        if(InvokeDialogCallback(w, Pt_MOZ_DIALOG_CONFIRM, title, ptext, nsnull, nsnull) == Pt_CONTINUE)
00207               *_retval = PR_TRUE;
00208        else
00209               *_retval = PR_FALSE;
00210 
00211        if( title ) nsMemory::Free( (void*)title );
00212        if( ptext ) nsMemory::Free( (void*)ptext );
00213 
00214        return NS_OK;
00215 }
00216 
00217 NS_IMETHODIMP CPromptService::ConfirmCheck(nsIDOMWindow *parent,
00218                                            const PRUnichar *dialogTitle,
00219                                            const PRUnichar *text,
00220                                            const PRUnichar *checkboxMsg,
00221                                            PRBool *checkValue,
00222                                            PRBool *_retval)
00223 {
00224   nsString  mTitle(dialogTitle);
00225   nsString  mText(text);
00226   nsString  mMsg(checkboxMsg);
00227   int     ret = 0;
00228   PtWidget_t *w = GetWebBrowser( parent );
00229        char *title = ToNewCString(mTitle), *ptext = ToNewCString(mText), *msg = ToNewCString(mMsg);
00230 
00231   if (InvokeDialogCallback(w, Pt_MOZ_DIALOG_CONFIRM_CHECK, title, ptext, msg, &ret) == Pt_CONTINUE)
00232     *_retval = PR_TRUE;
00233   else
00234     *_retval = PR_FALSE;
00235   if (checkValue)
00236     *checkValue = ret;
00237 
00238        if( title ) nsMemory::Free( (void*)title );
00239        if( ptext ) nsMemory::Free( (void*)ptext );
00240        if( msg ) nsMemory::Free( (void*)msg );
00241 
00242   return NS_OK;
00243 
00244 }
00245 
00246 NS_IMETHODIMP CPromptService::Prompt(nsIDOMWindow *parent,
00247                                      const PRUnichar *dialogTitle,
00248                                      const PRUnichar *text,
00249                                      PRUnichar **value,
00250                                      const PRUnichar *checkboxMsg,
00251                                      PRBool *checkValue,
00252                                      PRBool *_retval)
00253 {
00254        nsString      mTitle(dialogTitle);
00255        nsString      mText(text);
00256        nsString      mDefaultResponse(*value);
00257        nsString      mMsg(checkboxMsg);
00258        PtCallbackList_t *cb;
00259        PtCallbackInfo_t cbinfo;
00260        PtMozillaPromptCb_t prompt;
00261        PtWidget_t *w = GetWebBrowser( parent );
00262        PtMozillaWidget_t *moz = (PtMozillaWidget_t *) w;
00263 
00264        if (!moz->prompt_cb)
00265            return NS_OK;
00266 
00267        cb = moz->prompt_cb;
00268        memset(&cbinfo, 0, sizeof(cbinfo));
00269        cbinfo.reason = Pt_CB_MOZ_PROMPT;
00270        cbinfo.cbdata = &prompt;
00271 
00272        memset(&prompt, 0, sizeof(PtMozillaPromptCb_t));
00273        prompt.title = ToNewCString(mTitle);
00274        prompt.text = ToNewCString(mText);
00275        prompt.dflt_resp = ToNewCString(mDefaultResponse);
00276 
00277        if (PtInvokeCallbackList(cb, (PtWidget_t *)moz, &cbinfo) == Pt_CONTINUE)
00278        {
00279               nsCString   mResponse(prompt.response);
00280               *value = ToNewUnicode(mResponse);
00281               *_retval = PR_TRUE;
00282        }
00283        else
00284            *_retval = PR_FALSE;
00285 
00286        free( prompt.title );
00287        free( prompt.text );
00288        free( prompt.dflt_resp );
00289 
00290        return NS_OK;
00291 }
00292 
00293 NS_IMETHODIMP CPromptService::PromptUsernameAndPassword(nsIDOMWindow *parent,
00294                                                         const PRUnichar *dialogTitle,
00295                                                         const PRUnichar *text,
00296                                                         PRUnichar **username,
00297                                                         PRUnichar **password,
00298                                                         const PRUnichar *checkboxMsg,
00299                                                         PRBool *checkValue,
00300                                                         PRBool *_retval)
00301 {
00302        PtWidget_t *w = GetWebBrowser( parent );
00303        PtMozillaWidget_t                  *moz = (PtMozillaWidget_t *) w;
00304        PtCallbackList_t                   *cb;
00305        PtCallbackInfo_t                   cbinfo;
00306        PtMozillaAuthenticateCb_t   auth;
00307        nsString                                  mTitle(dialogTitle);
00308        nsString                                  mRealm(checkboxMsg);
00309 
00310        if (!moz->auth_cb)
00311            return NS_OK;
00312 
00313        cb = moz->auth_cb;
00314        memset(&cbinfo, 0, sizeof(cbinfo));
00315        cbinfo.reason = Pt_CB_MOZ_AUTHENTICATE;
00316        cbinfo.cbdata = &auth;
00317 
00318        memset(&auth, 0, sizeof(PtMozillaAuthenticateCb_t));
00319        auth.title = ToNewCString(mTitle);
00320        auth.realm = ToNewCString(mRealm);
00321 
00322        if (PtInvokeCallbackList(cb, (PtWidget_t *)moz, &cbinfo) == Pt_CONTINUE)
00323     {
00324               nsCString     mUser(auth.user);
00325               nsCString     mPass(auth.pass);
00326               *username = ToNewUnicode(mUser);
00327               *password = ToNewUnicode(mPass);
00328        *_retval = PR_TRUE;
00329     }
00330     else
00331        *_retval = PR_FALSE;
00332 
00333        free( auth.title );
00334        free( auth.realm );
00335 
00336        return NS_OK;
00337 }
00338 
00339 NS_IMETHODIMP CPromptService::PromptPassword(nsIDOMWindow *parent,
00340                                              const PRUnichar *dialogTitle,
00341                                              const PRUnichar *text,
00342                                              PRUnichar **password,
00343                                              const PRUnichar *checkboxMsg,
00344                                              PRBool *checkValue,
00345                                              PRBool *_retval)
00346 {
00347        return NS_ERROR_NOT_IMPLEMENTED;
00348 }
00349 
00350 NS_IMETHODIMP CPromptService::Select(nsIDOMWindow *parent,
00351                                      const PRUnichar *dialogTitle,
00352                                      const PRUnichar *text, PRUint32 count,
00353                                      const PRUnichar **selectList,
00354                                      PRInt32 *outSelection,
00355                                      PRBool *_retval)
00356 {
00357   return NS_ERROR_NOT_IMPLEMENTED;
00358 }
00359 
00360 NS_IMETHODIMP CPromptService::ConfirmEx(nsIDOMWindow *parent,
00361                                         const PRUnichar *dialogTitle,
00362                                         const PRUnichar *text,
00363                                         PRUint32 buttonFlags,
00364                                         const PRUnichar *button0Title,
00365                                         const PRUnichar *button1Title,
00366                                         const PRUnichar *button2Title,
00367                                         const PRUnichar *checkMsg,
00368                                         PRBool *checkValue,
00369                                         PRInt32 *buttonPressed)
00370 {
00371        if (checkValue)
00372               *checkValue = PR_FALSE;
00373        if (buttonPressed)
00374               *buttonPressed = 0;
00375        return NS_OK;
00376 }
00377 
00378 nsresult
00379 CPromptService::DoDialog(nsIDOMWindow *aParent,
00380                    nsIDialogParamBlock *aParamBlock, const char *aChromeURL)
00381 {
00382 #if 0
00383   NS_ENSURE_ARG(aParamBlock);
00384   NS_ENSURE_ARG(aChromeURL);
00385   if (!mWatcher)
00386     return NS_ERROR_FAILURE;
00387 
00388   nsresult rv = NS_OK;
00389 
00390   // get a parent, if at all possible
00391   // (though we'd rather this didn't fail, it's OK if it does. so there's
00392   // no failure or null check.)
00393   nsCOMPtr<nsIDOMWindow> activeParent; // retain ownership for method lifetime
00394   if (!aParent) {
00395     mWatcher->GetActiveWindow(getter_AddRefs(activeParent));
00396     aParent = activeParent;
00397   }
00398 
00399   nsCOMPtr<nsISupports> arguments(do_QueryInterface(aParamBlock));
00400   nsCOMPtr<nsIDOMWindow> dialog;
00401   rv = mWatcher->OpenWindow(aParent, aChromeURL, "_blank",
00402                             "centerscreen,chrome,modal,titlebar", arguments,
00403                             getter_AddRefs(dialog));
00404 
00405   return rv;
00406 #else
00407        return NS_OK;
00408 #endif
00409 }
00410  
00411 //*****************************************************************************
00412 // CPromptServiceFactory
00413 //*****************************************************************************   
00414 
00415 class CPromptServiceFactory : public nsIFactory {
00416 public:
00417   NS_DECL_ISUPPORTS
00418   NS_DECL_NSIFACTORY
00419 
00420   CPromptServiceFactory();
00421   virtual ~CPromptServiceFactory();
00422 };
00423 
00424 //*****************************************************************************
00425 
00426 NS_IMPL_ISUPPORTS1(CPromptServiceFactory, nsIFactory)
00427 
00428 CPromptServiceFactory::CPromptServiceFactory() {
00429 
00430   NS_INIT_ISUPPORTS();
00431 }
00432 
00433 CPromptServiceFactory::~CPromptServiceFactory() {
00434 }
00435 
00436 NS_IMETHODIMP CPromptServiceFactory::CreateInstance(nsISupports *aOuter, const nsIID & aIID, void **aResult)
00437 {
00438   NS_ENSURE_ARG_POINTER(aResult);
00439   
00440   *aResult = NULL;  
00441   CPromptService *inst = new CPromptService;    
00442   if (!inst)
00443     return NS_ERROR_OUT_OF_MEMORY;
00444     
00445   nsresult rv = inst->QueryInterface(aIID, aResult);
00446   if (rv != NS_OK) {  
00447     // We didn't get the right interface, so clean up  
00448     delete inst;  
00449   }  
00450     
00451   return rv;
00452 }
00453 
00454 NS_IMETHODIMP CPromptServiceFactory::LockFactory(PRBool lock)
00455 {
00456   return NS_OK;
00457 }
00458 
00459 //*****************************************************************************
00460 
00461 void InitPromptService( ) {
00462 }
00463 
00464 nsresult NS_NewPromptServiceFactory(nsIFactory** aFactory)
00465 {
00466   NS_ENSURE_ARG_POINTER(aFactory);
00467   *aFactory = nsnull;
00468   
00469   CPromptServiceFactory *result = new CPromptServiceFactory;
00470   if (!result)
00471     return NS_ERROR_OUT_OF_MEMORY;
00472     
00473   NS_ADDREF(result);
00474   *aFactory = result;
00475   
00476   return NS_OK;
00477 }