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  *
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) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Adam Lock <adamlock@eircom.net>
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 #include "StdAfx.h"
00040 
00041 #include "MozillaBrowser.h"
00042 #include "PromptService.h"
00043 
00044 #include "nsCOMPtr.h"
00045 
00046 #include "nsIPromptService.h"
00047 #include "nsIFactory.h"
00048 #include "nsIDOMWindow.h"
00049 #include "nsReadableUtils.h"
00050 
00051 class PromptDlg
00052 {
00053 public:
00054     PromptDlg();
00055     virtual ~PromptDlg();
00056 
00057     nsresult ConfirmCheck(
00058         HWND hwndParent,
00059         const PRUnichar *dialogTitle,
00060         const PRUnichar *text,
00061         const PRUnichar *checkMsg, PRBool *checkValue,
00062         PRBool *_retval);
00063 
00064     nsresult ConfirmEx(
00065         HWND hwndParent,
00066         const PRUnichar *dialogTitle,
00067         const PRUnichar *text,
00068         PRUint32 buttonFlags,
00069         const PRUnichar *button0Title,
00070         const PRUnichar *button1Title,
00071         const PRUnichar *button2Title,
00072         const PRUnichar *checkMsg, PRBool *checkValue,
00073         PRInt32 *buttonPressed);
00074 
00075     nsresult Prompt(HWND hwndParent, const PRUnichar *dialogTitle,
00076         const PRUnichar *text, PRUnichar **value,
00077         const PRUnichar *checkMsg, PRBool *checkValue,
00078         PRBool *_retval);
00079 
00080     nsresult PromptUsernameAndPassword(HWND hwndParent,
00081         const PRUnichar *dialogTitle,
00082         const PRUnichar *text,
00083         PRUnichar **username, PRUnichar **password,
00084         const PRUnichar *checkMsg, PRBool *checkValue,
00085         PRBool *_retval);
00086 
00087     nsresult PromptPassword(HWND hwndParent,
00088         const PRUnichar *dialogTitle,
00089         const PRUnichar *text,
00090         PRUnichar **password,
00091         const PRUnichar *checkMsg, PRBool *checkValue,
00092         PRBool *_retval);
00093 
00094 protected:
00095     static BOOL CALLBACK ConfirmProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
00096     static BOOL CALLBACK PromptProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
00097 
00098     TCHAR *mTitle;
00099     TCHAR *mMessage;
00100     TCHAR *mCheckMessage;
00101     BOOL   mCheckValue;
00102     TCHAR *mButtonTitles[3];
00103     TCHAR *mUsername;
00104     TCHAR *mPassword;
00105     TCHAR *mValue;
00106 
00107     enum {
00108         PROMPT_USERPASS,
00109         PROMPT_PASS,
00110         PROMPT_VALUE
00111     } mPromptMode;
00112 };
00113 
00114 PromptDlg::PromptDlg() :
00115     mMessage(NULL),
00116     mCheckMessage(NULL),
00117     mCheckValue(FALSE),
00118     mUsername(NULL),
00119     mPassword(NULL),
00120     mValue(NULL),
00121     mTitle(NULL),
00122     mPromptMode(PROMPT_USERPASS)
00123 {
00124     for (int i = 0; i < 3; i++)
00125     {
00126         mButtonTitles[i] = NULL;
00127     }
00128 }
00129 
00130 PromptDlg::~PromptDlg()
00131 {
00132     if (mTitle)
00133         free(mTitle);
00134     if (mMessage)
00135         free(mMessage);
00136     if (mCheckMessage)
00137         free(mCheckMessage);
00138     if (mUsername)
00139         free(mUsername);
00140     if (mPassword)
00141         free(mPassword);
00142     if (mValue)
00143         free(mValue);
00144     for (int i = 0; i < 3; i++)
00145     {
00146         if (mButtonTitles[i])
00147         {
00148             free(mButtonTitles[i]);
00149         }
00150     }
00151 }
00152 
00153 nsresult PromptDlg::ConfirmCheck(
00154     HWND hwndParent,
00155     const PRUnichar *dialogTitle,
00156     const PRUnichar *text,
00157     const PRUnichar *checkMsg, PRBool *checkValue,
00158     PRBool *_retval)
00159 {
00160     NS_ENSURE_ARG_POINTER(_retval);
00161     
00162     PRInt32 buttonPressed = 0;
00163     PRUint32 buttonFlags = nsIPromptService::BUTTON_TITLE_YES +
00164                           (nsIPromptService::BUTTON_TITLE_NO << 8);
00165 
00166     // Use the extended confirmation dialog with Yes & No buttons
00167     nsresult rv = ConfirmEx(hwndParent, dialogTitle, text,
00168         buttonFlags, NULL, NULL, NULL,
00169         checkMsg, checkValue, &buttonPressed);
00170 
00171     if (NS_SUCCEEDED(rv))
00172     {
00173         *_retval = (buttonPressed == 0) ? PR_TRUE : PR_FALSE;
00174     }
00175 
00176     return rv;
00177 }
00178 
00179 nsresult PromptDlg::ConfirmEx(
00180     HWND hwndParent,
00181     const PRUnichar *dialogTitle,
00182     const PRUnichar *text,
00183     PRUint32 buttonFlags,
00184     const PRUnichar *button0Title,
00185     const PRUnichar *button1Title,
00186     const PRUnichar *button2Title,
00187     const PRUnichar *checkMsg, PRBool *checkValue,
00188     PRInt32 *buttonPressed)
00189 {
00190     NS_ENSURE_ARG_POINTER(dialogTitle);
00191     NS_ENSURE_ARG_POINTER(text);
00192     NS_ENSURE_ARG_POINTER(buttonPressed);
00193 
00194     HINSTANCE hInstResource = _Module.m_hInstResource;
00195 
00196     USES_CONVERSION;
00197 
00198     // Duplicate all strings, turning them into TCHARs
00199 
00200     mTitle = _tcsdup(W2T(dialogTitle));
00201     mMessage = _tcsdup(W2T(text));
00202     if (checkMsg)
00203     {
00204         NS_ENSURE_ARG_POINTER(checkValue);
00205         mCheckMessage = _tcsdup(W2T(checkMsg));
00206         mCheckValue = (*checkValue == PR_TRUE) ? TRUE : FALSE;
00207     }
00208 
00209     // Turn the button flags into strings. The nsIPromptService flags
00210     // are scary bit shifted values which explains the masks and bit
00211     // shifting happening in this loop.
00212 
00213     for (int i = 0; i < 3; i++)
00214     {
00215         PRUint32 titleId = buttonFlags & 255;
00216         TCHAR **title = &mButtonTitles[i];
00217         switch (titleId)
00218         {
00219         case nsIPromptService::BUTTON_TITLE_OK:
00220         case nsIPromptService::BUTTON_TITLE_CANCEL:
00221         case nsIPromptService::BUTTON_TITLE_YES:
00222         case nsIPromptService::BUTTON_TITLE_NO:
00223         case nsIPromptService::BUTTON_TITLE_SAVE:
00224         case nsIPromptService::BUTTON_TITLE_DONT_SAVE:
00225         case nsIPromptService::BUTTON_TITLE_REVERT:
00226         {
00227             const int kTitleSize = 256;
00228             int stringId = IDS_CONFIRMEX_OK + titleId - nsIPromptService::BUTTON_TITLE_OK;
00229             *title = (TCHAR *) malloc(sizeof(TCHAR) * kTitleSize);
00230             ::LoadString(hInstResource, stringId, *title, kTitleSize - 1);
00231             break;
00232         }
00233         case nsIPromptService::BUTTON_TITLE_IS_STRING:
00234         {
00235             const PRUnichar *srcTitle =
00236                 (i == 0) ? button0Title : 
00237                 (i == 1) ? button1Title : button2Title;
00238             if (srcTitle)
00239             {
00240                 *title = _tcsdup(W2T(srcTitle));
00241             }
00242             break;
00243         }
00244         default:
00245             // ANYTHING ELSE GETS IGNORED
00246             break;
00247         }
00248         buttonFlags >>= 8;
00249     }
00250 
00251     // Must have at least one button the user can click on!
00252     NS_ENSURE_ARG_POINTER(mButtonTitles[0]);
00253 
00254     INT result = DialogBoxParam(hInstResource,
00255         MAKEINTRESOURCE(IDD_CONFIRMEX), hwndParent, ConfirmProc, (LPARAM) this);
00256 
00257     if (checkValue)
00258         *checkValue = mCheckValue;
00259 
00260     if (buttonPressed)
00261         *buttonPressed = result;
00262 
00263     return NS_OK;
00264 }
00265 
00266 nsresult
00267 PromptDlg::Prompt(HWND hwndParent, const PRUnichar *dialogTitle,
00268     const PRUnichar *text, PRUnichar **value,
00269     const PRUnichar *checkMsg, PRBool *checkValue,
00270     PRBool *_retval)
00271 {
00272     NS_ENSURE_ARG_POINTER(text);
00273     NS_ENSURE_ARG_POINTER(_retval);
00274 
00275     HINSTANCE hInstResource = _Module.m_hInstResource;
00276 
00277     USES_CONVERSION;
00278 
00279     // Duplicate all strings, turning them into TCHARs
00280 
00281     if (dialogTitle)
00282         mTitle = _tcsdup(W2T(dialogTitle));
00283     mMessage = _tcsdup(W2T(text));
00284     if (checkMsg)
00285     {
00286         NS_ENSURE_ARG_POINTER(checkValue);
00287         mCheckMessage = _tcsdup(W2T(checkMsg));
00288         mCheckValue = (*checkValue == PR_TRUE) ? TRUE : FALSE;
00289     }
00290     if (value)
00291     {
00292         mValue = _tcsdup(W2T(*value));
00293     }
00294 
00295     mPromptMode = PROMPT_VALUE;
00296     INT result = DialogBoxParam(hInstResource,
00297         MAKEINTRESOURCE(IDD_PROMPT), hwndParent, PromptProc, (LPARAM) this);
00298 
00299     if (result == IDOK)
00300     {
00301         if (value)
00302         {
00303             if (*value)
00304                 nsMemory::Free(*value);
00305             nsAutoString v(T2W(mValue));
00306             *value = ToNewUnicode(v);
00307         }
00308 
00309         if (checkValue)
00310             *checkValue = mCheckValue;
00311 
00312         *_retval = TRUE;
00313     }
00314     else
00315     {
00316         *_retval = FALSE;
00317     }
00318 
00319     return NS_OK;
00320 }
00321 
00322 nsresult
00323 PromptDlg::PromptUsernameAndPassword(HWND hwndParent,
00324     const PRUnichar *dialogTitle,
00325     const PRUnichar *text,
00326     PRUnichar **username, PRUnichar **password,
00327     const PRUnichar *checkMsg, PRBool *checkValue,
00328     PRBool *_retval)
00329 {
00330     NS_ENSURE_ARG_POINTER(text);
00331     NS_ENSURE_ARG_POINTER(_retval);
00332 
00333     HINSTANCE hInstResource = _Module.m_hInstResource;
00334 
00335     USES_CONVERSION;
00336 
00337     // Duplicate all strings, turning them into TCHARs
00338 
00339     if (dialogTitle)
00340         mTitle = _tcsdup(W2T(dialogTitle));
00341     mMessage = _tcsdup(W2T(text));
00342     if (checkMsg)
00343     {
00344         NS_ENSURE_ARG_POINTER(checkValue);
00345         mCheckMessage = _tcsdup(W2T(checkMsg));
00346         mCheckValue = (*checkValue == PR_TRUE) ? TRUE : FALSE;
00347     }
00348     if (username)
00349     {
00350         mUsername = _tcsdup(W2T(*username));
00351     }
00352     if (password)
00353     {
00354         mPassword = _tcsdup(W2T(*password));
00355     }
00356 
00357     mPromptMode = PROMPT_USERPASS;
00358     INT result = DialogBoxParam(hInstResource,
00359         MAKEINTRESOURCE(IDD_PROMPTUSERPASS), hwndParent, PromptProc, (LPARAM) this);
00360 
00361     if (result == IDOK)
00362     {
00363         if (username)
00364         {
00365             if (*username)
00366                 nsMemory::Free(*username);
00367             nsAutoString user(T2W(mUsername));
00368             *username = ToNewUnicode(user);
00369         }
00370         if (password)
00371         {
00372             if (*password)
00373                 nsMemory::Free(*password);
00374             nsAutoString pass(T2W(mPassword));
00375             *password = ToNewUnicode(pass);
00376         }
00377 
00378         if (checkValue)
00379             *checkValue = mCheckValue;
00380 
00381         *_retval = TRUE;
00382     }
00383     else
00384     {
00385         *_retval = FALSE;
00386     }
00387 
00388     return NS_OK;
00389 }
00390 
00391 nsresult
00392 PromptDlg::PromptPassword(HWND hwndParent,
00393     const PRUnichar *dialogTitle,
00394     const PRUnichar *text,
00395     PRUnichar **password,
00396     const PRUnichar *checkMsg, PRBool *checkValue,
00397     PRBool *_retval)
00398 {
00399     NS_ENSURE_ARG_POINTER(text);
00400     NS_ENSURE_ARG_POINTER(_retval);
00401 
00402     HINSTANCE hInstResource = _Module.m_hInstResource;
00403 
00404     USES_CONVERSION;
00405 
00406     // Duplicate all strings, turning them into TCHARs
00407 
00408     if (dialogTitle)
00409         mTitle = _tcsdup(W2T(dialogTitle));
00410     mMessage = _tcsdup(W2T(text));
00411     if (checkMsg)
00412     {
00413         NS_ENSURE_ARG_POINTER(checkValue);
00414         mCheckMessage = _tcsdup(W2T(checkMsg));
00415         mCheckValue = (*checkValue == PR_TRUE) ? TRUE : FALSE;
00416     }
00417     if (password)
00418     {
00419         mPassword = _tcsdup(W2T(*password));
00420     }
00421 
00422     mPromptMode = PROMPT_PASS;
00423     INT result = DialogBoxParam(hInstResource,
00424         MAKEINTRESOURCE(IDD_PROMPTUSERPASS), hwndParent, PromptProc, (LPARAM) this);
00425 
00426     if (result == IDOK)
00427     {
00428         if (password)
00429         {
00430             if (*password)
00431                 nsMemory::Free(*password);
00432             nsAutoString pass(T2W(mPassword));
00433             *password = ToNewUnicode(pass);
00434         }
00435 
00436         if (checkValue)
00437             *checkValue = mCheckValue;
00438 
00439         *_retval = TRUE;
00440     }
00441     else
00442     {
00443         *_retval = FALSE;
00444     }
00445 
00446     return NS_OK;
00447 }
00448 
00449 
00450 BOOL CALLBACK
00451 PromptDlg::PromptProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
00452 {
00453     PromptDlg *pThis = (PromptDlg *) GetWindowLong(hwndDlg, DWL_USER);
00454     switch (uMsg)
00455     {
00456     case WM_INITDIALOG:
00457         // Initialise pThis
00458         SetWindowLong(hwndDlg, DWL_USER, lParam);
00459         pThis = (PromptDlg *) lParam;
00460 
00461         // Set dialog title & message text
00462         if (pThis->mTitle)
00463             SetWindowText(hwndDlg, pThis->mTitle);
00464 
00465         SetDlgItemText(hwndDlg, IDC_MESSAGE, pThis->mMessage);
00466 
00467         // Set check message text
00468         if (pThis->mCheckMessage)
00469         {
00470             SetDlgItemText(hwndDlg, IDC_CHECKMSG, pThis->mCheckMessage);
00471             CheckDlgButton(hwndDlg, IDC_CHECKMSG, pThis->mCheckValue ? BST_CHECKED : BST_UNCHECKED); 
00472         }
00473         else
00474         {
00475             ShowWindow(GetDlgItem(hwndDlg, IDC_CHECKMSG), SW_HIDE);
00476         }
00477 
00478         switch (pThis->mPromptMode)
00479         {
00480         case PROMPT_PASS:
00481             EnableWindow(GetDlgItem(hwndDlg, IDC_USERNAME), FALSE);
00482             if (pThis->mPassword)
00483             {
00484                 SetDlgItemText(hwndDlg, IDC_PASSWORD, pThis->mPassword);
00485             }
00486             break;
00487         case PROMPT_USERPASS:
00488             if (pThis->mUsername)
00489             {
00490                 SetDlgItemText(hwndDlg, IDC_USERNAME, pThis->mUsername);
00491             }
00492             if (pThis->mPassword)
00493             {
00494                 SetDlgItemText(hwndDlg, IDC_PASSWORD, pThis->mPassword);
00495             }
00496             break;
00497         case PROMPT_VALUE:
00498             if (pThis->mValue)
00499             {
00500                 SetDlgItemText(hwndDlg, IDC_VALUE, pThis->mValue);
00501             }
00502             break;
00503         }
00504 
00505         return TRUE;
00506 
00507     case WM_COMMAND:
00508         if (HIWORD(wParam) == BN_CLICKED)
00509         {
00510             int id = LOWORD(wParam);
00511             if (id == IDOK)
00512             {
00513                 if (pThis->mCheckMessage)
00514                 {
00515                     pThis->mCheckValue =
00516                         (IsDlgButtonChecked(hwndDlg, IDC_CHECKMSG) == BST_CHECKED) ?
00517                             TRUE : FALSE;
00518                 }
00519 
00520                 const int bufferSize = 256;
00521                 TCHAR buffer[bufferSize];
00522 
00523                 switch (pThis->mPromptMode)
00524                 {
00525                 case PROMPT_USERPASS:
00526                     if (pThis->mUsername)
00527                     {
00528                         free(pThis->mUsername);
00529                     }
00530                     memset(buffer, 0, sizeof(buffer));
00531                     GetDlgItemText(hwndDlg, IDC_USERNAME, buffer, bufferSize);
00532                     pThis->mUsername = _tcsdup(buffer);
00533 
00534                     // DROP THROUGH !!!!
00535 
00536                 case PROMPT_PASS:
00537                     if (pThis->mPassword)
00538                     {
00539                         free(pThis->mPassword);
00540                     }
00541                     memset(buffer, 0, sizeof(buffer));
00542                     GetDlgItemText(hwndDlg, IDC_PASSWORD, buffer, bufferSize);
00543                     pThis->mPassword = _tcsdup(buffer);
00544                     break;
00545 
00546                 case PROMPT_VALUE:
00547                     if (pThis->mValue)
00548                     {
00549                         free(pThis->mValue);
00550                     }
00551                     memset(buffer, 0, sizeof(buffer));
00552                     GetDlgItemText(hwndDlg, IDC_VALUE, buffer, bufferSize);
00553                     pThis->mValue = _tcsdup(buffer);
00554                     break;
00555                 }
00556                 EndDialog(hwndDlg, IDOK);
00557             }
00558             else if (id == IDCANCEL)
00559             {
00560                 EndDialog(hwndDlg, IDCANCEL);
00561             }
00562         }
00563         return TRUE;
00564     }
00565     return FALSE;
00566 }
00567 
00568 BOOL CALLBACK
00569 PromptDlg::ConfirmProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
00570 {
00571     PromptDlg *pThis = (PromptDlg *) GetWindowLong(hwndDlg, DWL_USER);
00572     int i;
00573 
00574     switch (uMsg)
00575     {
00576     case WM_INITDIALOG:
00577 
00578         // Initialise pThis
00579         SetWindowLong(hwndDlg, DWL_USER, lParam);
00580         pThis = (PromptDlg *) lParam;
00581 
00582         // Set dialog title & message text
00583         SetWindowText(hwndDlg, pThis->mTitle);
00584         SetDlgItemText(hwndDlg, IDC_MESSAGE, pThis->mMessage);
00585 
00586         // Set check message text
00587         if (pThis->mCheckMessage)
00588         {
00589             SetDlgItemText(hwndDlg, IDC_CHECKMSG, pThis->mCheckMessage);
00590             CheckDlgButton(hwndDlg, IDC_CHECKMSG, pThis->mCheckValue ? BST_CHECKED : BST_UNCHECKED); 
00591         }
00592         else
00593         {
00594             ShowWindow(GetDlgItem(hwndDlg, IDC_CHECKMSG), SW_HIDE);
00595         }
00596 
00597         // Set the button text or hide them
00598         for (i = 0; i < 3; i++)
00599         {
00600             int id = IDC_BUTTON0 + i;
00601             if (pThis->mButtonTitles[i])
00602             {
00603                 SetDlgItemText(hwndDlg, id, pThis->mButtonTitles[i]);
00604             }
00605             else
00606             {
00607                 ShowWindow(GetDlgItem(hwndDlg, id), SW_HIDE);
00608             }
00609         }
00610         return TRUE;
00611 
00612     case WM_COMMAND:
00613         if (HIWORD(wParam) == BN_CLICKED)
00614         {
00615             int id = LOWORD(wParam);
00616             if (id == IDC_BUTTON0 ||
00617                 id == IDC_BUTTON1 ||
00618                 id == IDC_BUTTON2)
00619             {
00620                 if (pThis->mCheckMessage)
00621                 {
00622                     pThis->mCheckValue =
00623                         (IsDlgButtonChecked(hwndDlg, IDC_CHECKMSG) == BST_CHECKED) ?
00624                             TRUE : FALSE;
00625                 }
00626                 EndDialog(hwndDlg, id - IDC_BUTTON0);
00627             }
00628         }
00629         return FALSE;
00630     }
00631 
00632     return FALSE;
00633 }
00634 
00635 
00636 //*****************************************************************************
00637 // CPromptService
00638 //*****************************************************************************   
00639 
00640 class CPromptService: public nsIPromptService
00641 {
00642 public:
00643     CPromptService();
00644     virtual ~CPromptService();
00645 
00646     CMozillaBrowser *GetOwningBrowser(nsIDOMWindow *parent);
00647 
00648     NS_DECL_ISUPPORTS
00649     NS_DECL_NSIPROMPTSERVICE
00650 };
00651 
00652 //*****************************************************************************   
00653 
00654 NS_IMPL_ISUPPORTS1(CPromptService, nsIPromptService)
00655 
00656 CPromptService::CPromptService()
00657 {
00658 }
00659 
00660 CPromptService::~CPromptService()
00661 {
00662 }
00663 
00664 
00665 CMozillaBrowser *CPromptService::GetOwningBrowser(nsIDOMWindow *parent)
00666 {
00667     if (parent == nsnull)
00668     {
00669         // return the first element from the list if there is one
00670         if (CMozillaBrowser::sBrowserList.Count() > 0)
00671         {
00672             return (CMozillaBrowser *) CMozillaBrowser::sBrowserList[0];
00673         }
00674         return NULL;
00675     }
00676 
00677     // Search for the browser with a content window matching the one provided
00678     PRInt32 i;
00679     for (i = 0; i < CMozillaBrowser::sBrowserList.Count(); i++)
00680     {
00681         CMozillaBrowser *p = (CMozillaBrowser *) CMozillaBrowser::sBrowserList[i];
00682         if (p->mWebBrowser)
00683         {
00684             nsCOMPtr<nsIDOMWindow> domWindow;
00685             p->mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00686             if (domWindow.get() == parent)
00687             {
00688                 return p;
00689             }
00690         }
00691     }
00692 
00693     return NULL;
00694 }
00695 
00697 // nsIPrompt
00698 
00699 NS_IMETHODIMP CPromptService::Alert(nsIDOMWindow *parent, const PRUnichar *dialogTitle,
00700     const PRUnichar *text)
00701 {
00702     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00703     if (pOwner)
00704     {
00705         USES_CONVERSION;
00706         pOwner->MessageBox(W2T(text), W2T(dialogTitle), MB_OK | MB_ICONEXCLAMATION);
00707     }
00708     return NS_OK;
00709 }
00710 
00711 NS_IMETHODIMP CPromptService::AlertCheck(nsIDOMWindow *parent, const PRUnichar *dialogTitle,
00712     const PRUnichar *text,
00713     const PRUnichar *checkMsg, PRBool *checkValue)
00714 {
00715     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00716     if (pOwner)
00717     {
00718         // TODO show dialog with check box
00719         USES_CONVERSION;
00720         pOwner->MessageBox(W2T(text), W2T(dialogTitle), MB_OK | MB_ICONEXCLAMATION);
00721     }
00722     return NS_OK;
00723 }
00724 
00725 NS_IMETHODIMP CPromptService::Confirm(nsIDOMWindow *parent, const PRUnichar *dialogTitle,
00726     const PRUnichar *text,
00727     PRBool *_retval)
00728 {
00729     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00730     if (pOwner)
00731     {
00732         USES_CONVERSION;
00733         int nAnswer = pOwner->MessageBox(W2T(text), W2T(dialogTitle),
00734             MB_YESNO | MB_ICONQUESTION);
00735         *_retval = (nAnswer == IDYES) ? PR_TRUE : PR_FALSE;
00736     }
00737     return NS_OK;
00738 }
00739 
00740 
00741 NS_IMETHODIMP CPromptService::ConfirmCheck(nsIDOMWindow *parent, const PRUnichar *dialogTitle,
00742     const PRUnichar *text,
00743     const PRUnichar *checkMsg, PRBool *checkValue,
00744     PRBool *_retval)
00745 {
00746     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00747     if (pOwner)
00748     {
00749         PromptDlg dlg;
00750         return dlg.ConfirmCheck(pOwner->m_hWnd, dialogTitle,
00751             text, checkMsg, checkValue, _retval);
00752     }
00753     return NS_OK;
00754 }
00755 
00756 
00757 NS_IMETHODIMP CPromptService::ConfirmEx(nsIDOMWindow *parent, const PRUnichar *dialogTitle,
00758     const PRUnichar *text,
00759     PRUint32 buttonFlags,
00760     const PRUnichar *button0Title,
00761     const PRUnichar *button1Title,
00762     const PRUnichar *button2Title,
00763     const PRUnichar *checkMsg, PRBool *checkValue,
00764     PRInt32 *buttonPressed)
00765 {
00766     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00767     if (pOwner)
00768     {
00769         PromptDlg dlg;
00770         return dlg.ConfirmEx(pOwner->m_hWnd, dialogTitle,
00771             text, buttonFlags, button0Title, button1Title, button2Title,
00772             checkMsg, checkValue, buttonPressed);
00773     }
00774     return NS_ERROR_FAILURE;
00775 }
00776 
00777 NS_IMETHODIMP CPromptService::Prompt(nsIDOMWindow *parent, const PRUnichar *dialogTitle,
00778     const PRUnichar *text, PRUnichar **value,
00779     const PRUnichar *checkMsg, PRBool *checkValue,
00780     PRBool *_retval)
00781 {
00782     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00783     if (pOwner)
00784     {
00785         PromptDlg dlg;
00786         return dlg.Prompt(pOwner->m_hWnd,
00787             dialogTitle, text, value, checkMsg, checkValue, _retval);
00788     }
00789     return NS_ERROR_FAILURE;
00790 }
00791 
00792 NS_IMETHODIMP CPromptService::PromptUsernameAndPassword(nsIDOMWindow *parent,
00793     const PRUnichar *dialogTitle,
00794     const PRUnichar *text,
00795     PRUnichar **username, PRUnichar **password,
00796     const PRUnichar *checkMsg, PRBool *checkValue,
00797     PRBool *_retval)
00798 {
00799     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00800     if (pOwner)
00801     {
00802         PromptDlg dlg;
00803         return dlg.PromptUsernameAndPassword(pOwner->m_hWnd,
00804             dialogTitle, text, username, password, checkMsg, checkValue, _retval);
00805     }
00806     return NS_ERROR_FAILURE;
00807 }
00808 
00809 NS_IMETHODIMP CPromptService::PromptPassword(nsIDOMWindow *parent,
00810     const PRUnichar *dialogTitle,
00811     const PRUnichar *text,
00812     PRUnichar **password,
00813     const PRUnichar *checkMsg, PRBool *checkValue,
00814     PRBool *_retval)
00815 {
00816     CMozillaBrowser *pOwner = GetOwningBrowser(parent);
00817     if (pOwner)
00818     {
00819         PromptDlg dlg;
00820         return dlg.PromptPassword(pOwner->m_hWnd,
00821             dialogTitle, text, password, checkMsg, checkValue, _retval);
00822     }
00823     return NS_ERROR_FAILURE;
00824 }
00825 
00826 NS_IMETHODIMP CPromptService::Select(nsIDOMWindow *parent,
00827     const PRUnichar *dialogTitle,
00828     const PRUnichar *text, PRUint32 count,
00829     const PRUnichar **selectList, PRInt32 *outSelection,
00830     PRBool *_retval)
00831 {
00832     return NS_ERROR_NOT_IMPLEMENTED;
00833 }
00834 
00835 
00836 //*****************************************************************************
00837 // CPromptServiceFactory
00838 //*****************************************************************************   
00839 
00840 class CPromptServiceFactory : public nsIFactory
00841 {
00842 public:
00843     NS_DECL_ISUPPORTS
00844     NS_DECL_NSIFACTORY
00845 
00846     CPromptServiceFactory();
00847     virtual ~CPromptServiceFactory();
00848 };
00849 
00850 //*****************************************************************************   
00851 
00852 NS_IMPL_ISUPPORTS1(CPromptServiceFactory, nsIFactory)
00853 
00854 CPromptServiceFactory::CPromptServiceFactory()
00855 {
00856 }
00857 
00858 CPromptServiceFactory::~CPromptServiceFactory()
00859 {
00860 }
00861 
00862 NS_IMETHODIMP CPromptServiceFactory::CreateInstance(nsISupports *aOuter, const nsIID & aIID, void **aResult)
00863 {
00864     NS_ENSURE_ARG_POINTER(aResult);
00865 
00866     *aResult = NULL;  
00867     CPromptService *inst = new CPromptService;    
00868     if (!inst)
00869     return NS_ERROR_OUT_OF_MEMORY;
00870 
00871     nsresult rv = inst->QueryInterface(aIID, aResult);
00872     if (rv != NS_OK) {  
00873         // We didn't get the right interface, so clean up  
00874         delete inst;  
00875     }  
00876 
00877     return rv;
00878 }
00879 
00880 NS_IMETHODIMP CPromptServiceFactory::LockFactory(PRBool lock)
00881 {
00882     return NS_OK;
00883 }
00884 
00885 //*****************************************************************************   
00886 
00887 nsresult NS_NewPromptServiceFactory(nsIFactory** aFactory)
00888 {
00889     NS_ENSURE_ARG_POINTER(aFactory);
00890     *aFactory = nsnull;
00891 
00892     CPromptServiceFactory *result = new CPromptServiceFactory;
00893     if (!result)
00894         return NS_ERROR_OUT_OF_MEMORY;
00895 
00896     NS_ADDREF(result);
00897     *aFactory = result;
00898 
00899     return NS_OK;
00900 }