Back to index

lightning-sunbird  0.9+nobinonly
QtPromptService.cpp
Go to the documentation of this file.
00001 /*
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
00006  * License Version 1.1 (the "License"); you may not use this file
00007  * except in compliance with the License. You may obtain a copy of
00008  * the License at http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS
00011  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00012  * implied. See the License for the specific language governing
00013  * rights and limitations under the License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Lixto GmbH.
00019  * Portions created by Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Michal Ceresna <ceresna@amos.sturak.sk>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "QtPromptService.h"
00040 #include <nsString.h>
00041 #include <nsIWindowWatcher.h>
00042 #include <nsIWebBrowserChrome.h>
00043 #include <nsIEmbeddingSiteWindow.h>
00044 #include <nsCOMPtr.h>
00045 #include <nsIServiceManager.h>
00046 #include <nsReadableUtils.h>
00047 
00048 #include <qmessagebox.h>
00049 #include <qlabel.h>
00050 #include <qcheckbox.h>
00051 #include <qcombobox.h>
00052 #include <qapplication.h>
00053 #include <qstyle.h>
00054 #include <qpixmap.h>
00055 #include <qpushbutton.h>
00056 #include <qlineedit.h>
00057 #include "ui_alert.h"
00058 #include "ui_confirm.h"
00059 #include "ui_prompt.h"
00060 #include "ui_select.h"
00061 #include "ui_userpass.h"
00062 
00063 #if (QT_VERSION < 0x030200)
00064 //constant not defined in older qt version
00065 #define SP_MessageBoxQuestion SP_MessageBoxInformation
00066 #endif
00067 
00068 QtPromptService::QtPromptService()
00069 {
00070 }
00071 
00072 QtPromptService::~QtPromptService()
00073 {
00074 }
00075 
00076 NS_IMPL_ISUPPORTS1(QtPromptService, nsIPromptService)
00077 
00078 
00081 NS_IMETHODIMP
00082 QtPromptService::Alert(nsIDOMWindow* aParent,
00083                        const PRUnichar* aDialogTitle,
00084                        const PRUnichar* aDialogText)
00085 {
00086     return
00087         AlertCheck(aParent,
00088                    aDialogTitle, aDialogText,
00089                    NULL, NULL);
00090 }
00091 
00096 NS_IMETHODIMP
00097 QtPromptService::AlertCheck(nsIDOMWindow* aParent,
00098                             const PRUnichar* aDialogTitle,
00099                             const PRUnichar* aDialogText,
00100                             const PRUnichar* aCheckMsg,
00101                             PRBool* aCheckValue)
00102 {
00103     AlertDialog d(GetQWidgetForDOMWindow(aParent));
00104     d.icon->setPixmap(QApplication::style().
00105                       stylePixmap(QStyle::SP_MessageBoxWarning));
00106     if (aDialogTitle) {
00107         d.setCaption(QString::fromUcs2(aDialogTitle));
00108     }
00109     d.message->setText(QString::fromUcs2(aDialogText));
00110     if (aCheckMsg) {
00111         d.check->setText(QString::fromUcs2(aCheckMsg));
00112         d.check->setChecked(*aCheckValue);
00113     }
00114     else {
00115         d.check->hide();
00116     }
00117     d.adjustSize();
00118     d.exec();
00119 
00120     if (aCheckMsg) {
00121         *aCheckValue = d.check->isChecked();
00122     }
00123 
00124     return NS_OK;
00125 }
00126 
00131 NS_IMETHODIMP
00132 QtPromptService::Confirm(nsIDOMWindow* aParent,
00133                          const PRUnichar* aDialogTitle,
00134                          const PRUnichar* aDialogText,
00135                          PRBool* aConfirm)
00136 {
00137     return
00138         ConfirmCheck(aParent,
00139                      aDialogTitle, aDialogText,
00140                      NULL, NULL,
00141                      aConfirm);
00142 }
00143 
00149 NS_IMETHODIMP
00150 QtPromptService::ConfirmCheck(nsIDOMWindow* aParent,
00151                               const PRUnichar* aDialogTitle,
00152                               const PRUnichar* aDialogText,
00153                               const PRUnichar* aCheckMsg,
00154                               PRBool* aCheckValue,
00155                               PRBool* aConfirm)
00156 {
00157     PRInt32 ret;
00158     ConfirmEx(aParent,
00159               aDialogTitle, aDialogText,
00160               STD_OK_CANCEL_BUTTONS,
00161               NULL, NULL, NULL,
00162               aCheckMsg,
00163               aCheckValue,
00164               &ret);
00165     *aConfirm = (ret==0);
00166 
00167     return NS_OK;
00168 }
00169 
00195 NS_IMETHODIMP
00196 QtPromptService::ConfirmEx(nsIDOMWindow* aParent,
00197                            const PRUnichar* aDialogTitle,
00198                            const PRUnichar* aDialogText,
00199                            PRUint32 aButtonFlags,
00200                            const PRUnichar* aButton0Title,
00201                            const PRUnichar* aButton1Title,
00202                            const PRUnichar* aButton2Title,
00203                            const PRUnichar* aCheckMsg,
00204                            PRBool* aCheckValue,
00205                            PRInt32* aRetVal)
00206 {
00207     ConfirmDialog d(GetQWidgetForDOMWindow(aParent));
00208     d.icon->setPixmap(QApplication::style().
00209                       stylePixmap(QStyle::SP_MessageBoxQuestion));
00210     if (aDialogTitle) {
00211         d.setCaption(QString::fromUcs2(aDialogTitle));
00212     }
00213     d.message->setText(QString::fromUcs2(aDialogText));
00214 
00215     QString l = GetButtonLabel(aButtonFlags, BUTTON_POS_0, aButton0Title);
00216     if (!l.isNull()) d.but1->setText(l); else d.but1->hide();
00217     l = GetButtonLabel(aButtonFlags, BUTTON_POS_1, aButton1Title);
00218     if (!l.isNull()) d.but2->setText(l); else d.but2->hide();
00219     l = GetButtonLabel(aButtonFlags, BUTTON_POS_2, aButton2Title);
00220     if (!l.isNull()) d.but3->setText(l); else d.but3->hide();
00221 
00222     if (aCheckMsg) {
00223         d.check->setText(QString::fromUcs2(aCheckMsg));
00224         d.check->setChecked(*aCheckValue);
00225     }
00226     else {
00227         d.check->hide();
00228     }
00229     d.adjustSize();
00230     int ret = d.exec();
00231 
00232     *aRetVal = ret;
00233 
00234     return NS_OK;
00235 }
00236 
00250 NS_IMETHODIMP
00251 QtPromptService::Prompt(nsIDOMWindow* aParent,
00252                         const PRUnichar* aDialogTitle,
00253                         const PRUnichar* aDialogText,
00254                         PRUnichar** aValue,
00255                         const PRUnichar* aCheckMsg,
00256                         PRBool* aCheckValue,
00257                         PRBool* aConfirm)
00258 {
00259     PromptDialog d(GetQWidgetForDOMWindow(aParent));
00260     d.icon->setPixmap(QApplication::style().
00261                       stylePixmap(QStyle::SP_MessageBoxQuestion));
00262     if (aDialogTitle) {
00263         d.setCaption(QString::fromUcs2(aDialogTitle));
00264     }
00265     d.message->setText(QString::fromUcs2(aDialogText));
00266     if (aValue && *aValue) {
00267         d.input->setText(QString::fromUcs2(*aValue));
00268     }
00269     if (aCheckMsg) {
00270         d.check->setText(QString::fromUcs2(aCheckMsg));
00271         d.check->setChecked(*aCheckValue);
00272     }
00273     else {
00274         d.check->hide();
00275     }
00276     d.adjustSize();
00277     int ret = d.exec();
00278 
00279     if (aCheckMsg) {
00280         *aCheckValue = d.check->isChecked();
00281     }
00282     *aConfirm = (ret & QMessageBox::Ok);
00283     if (*aConfirm) {
00284         if (*aValue) nsMemory::Free(*aValue);
00285         *aValue =
00286             ToNewUnicode(NS_ConvertUTF8toUCS2(d.input->text().utf8()));
00287     }
00288 
00289     return NS_OK;
00290 }
00291 
00309 NS_IMETHODIMP
00310 QtPromptService::PromptUsernameAndPassword(nsIDOMWindow* aParent,
00311                                            const PRUnichar* aDialogTitle,
00312                                            const PRUnichar* aDialogText,
00313                                            PRUnichar** aUsername,
00314                                            PRUnichar** aPassword,
00315                                            const PRUnichar* aCheckMsg,
00316                                            PRBool* aCheckValue,
00317                                            PRBool* aConfirm)
00318 {
00319     UserpassDialog d(GetQWidgetForDOMWindow(aParent));
00320     d.icon->setPixmap(QApplication::style().
00321                       stylePixmap(QStyle::SP_MessageBoxQuestion));
00322     if (aDialogTitle) {
00323         d.setCaption(QString::fromUcs2(aDialogTitle));
00324     }
00325     d.message->setText(QString::fromUcs2(aDialogText));
00326     if (aUsername && *aUsername) {
00327         d.username->setText(QString::fromUcs2(*aUsername));
00328     }
00329     if (aPassword && *aPassword) {
00330         d.password->setText(QString::fromUcs2(*aPassword));
00331     }
00332     if (aCheckMsg) {
00333         d.check->setText(QString::fromUcs2(aCheckMsg));
00334         d.check->setChecked(*aCheckValue);
00335     }
00336     else {
00337         d.check->hide();
00338     }
00339     d.adjustSize();
00340     int ret = d.exec();
00341 
00342     if (aCheckMsg) {
00343         *aCheckValue = d.check->isChecked();
00344     }
00345     *aConfirm = (ret & QMessageBox::Ok);
00346     if (*aConfirm) {
00347         if (*aUsername) nsMemory::Free(*aUsername);
00348         *aUsername =
00349             ToNewUnicode(NS_ConvertUTF8toUCS2(d.username->text().utf8()));
00350         if (*aPassword) nsMemory::Free(*aPassword);
00351         *aPassword =
00352             ToNewUnicode(NS_ConvertUTF8toUCS2(d.password->text().utf8()));
00353     }
00354 
00355     return NS_OK;
00356 }
00357 
00371 NS_IMETHODIMP
00372 QtPromptService::PromptPassword(nsIDOMWindow* aParent,
00373                                 const PRUnichar* aDialogTitle,
00374                                 const PRUnichar* aDialogText,
00375                                 PRUnichar** aPassword,
00376                                 const PRUnichar* aCheckMsg,
00377                                 PRBool* aCheckValue,
00378                                 PRBool* aConfirm)
00379 {
00380     UserpassDialog d(GetQWidgetForDOMWindow(aParent));
00381     d.icon->setPixmap(QApplication::style().
00382                       stylePixmap(QStyle::SP_MessageBoxQuestion));
00383     if (aDialogTitle) {
00384         d.setCaption(QString::fromUcs2(aDialogTitle));
00385     }
00386     d.message->setText(QString::fromUcs2(aDialogText));
00387     d.lb_username->hide();
00388     d.username->hide();
00389     if (aPassword && *aPassword) {
00390         d.password->setText(QString::fromUcs2(*aPassword));
00391     }
00392     if (aCheckMsg) {
00393         d.check->setText(QString::fromUcs2(aCheckMsg));
00394         d.check->setChecked(*aCheckValue);
00395     }
00396     else {
00397         d.check->hide();
00398     }
00399     d.adjustSize();
00400     int ret = d.exec();
00401 
00402     if (aCheckMsg) {
00403         *aCheckValue = d.check->isChecked();
00404     }
00405     *aConfirm = (ret & QMessageBox::Ok);
00406     if (*aConfirm) {
00407         if (*aPassword) nsMemory::Free(*aPassword);
00408         *aPassword =
00409             ToNewUnicode(NS_ConvertUTF8toUCS2(d.password->text().utf8()));
00410     }
00411 
00412     return NS_OK;
00413 }
00414 
00418 NS_IMETHODIMP
00419 QtPromptService::Select(nsIDOMWindow* aParent,
00420                         const PRUnichar* aDialogTitle,
00421                         const PRUnichar* aDialogText,
00422                         PRUint32 aCount,
00423                         const PRUnichar** aSelectList,
00424                         PRInt32* outSelection,
00425                         PRBool* aConfirm)
00426 {
00427     SelectDialog d(GetQWidgetForDOMWindow(aParent));
00428     d.icon->setPixmap(QApplication::style().
00429                       stylePixmap(QStyle::SP_MessageBoxQuestion));
00430     if (aDialogTitle) {
00431         d.setCaption(QString::fromUcs2(aDialogTitle));
00432     }
00433     d.message->setText(QString::fromUcs2(aDialogText));
00434     if (aSelectList) {
00435         QStringList l;
00436         for (PRUint32 i = 0; i < aCount; ++i) {
00437             l.append(QString::fromUcs2(aSelectList[i]));
00438         }
00439         d.select->clear();
00440         d.select->insertStringList(l);
00441     }
00442     d.adjustSize();
00443     int ret = d.exec();
00444 
00445     *aConfirm = (ret & QMessageBox::Ok);
00446     if (*aConfirm) {
00447         *outSelection = d.select->currentItem();
00448     }
00449 
00450     return NS_OK;
00451 }
00452 
00453 QWidget*
00454 QtPromptService::GetQWidgetForDOMWindow(nsIDOMWindow* aDOMWindow)
00455 {
00456     nsCOMPtr<nsIWindowWatcher> wwatch = do_GetService("@mozilla.org/embedcomp/window-watcher;1");
00457 
00458     nsCOMPtr<nsIWebBrowserChrome> chrome;
00459     wwatch->GetChromeForWindow(aDOMWindow, getter_AddRefs(chrome));
00460     nsCOMPtr<nsIEmbeddingSiteWindow> siteWindow = do_QueryInterface(chrome);
00461     QWidget* parentWidget;
00462     siteWindow->GetSiteWindow((void**)&parentWidget);
00463 
00464     return parentWidget;
00465 }
00466 
00467 QString
00468 QtPromptService::GetButtonLabel(PRUint32 aFlags,
00469                                 PRUint32 aPos,
00470                                 const PRUnichar* aStringValue)
00471 {
00472     PRUint32 posFlag = (aFlags & (255 * aPos)) / aPos;
00473     switch (posFlag) {
00474     case BUTTON_TITLE_OK:
00475         return qApp->translate("QtPromptService", "&OK");
00476     case BUTTON_TITLE_CANCEL:
00477         return qApp->translate("QtPromptService", "&Cancel");
00478     case BUTTON_TITLE_YES:
00479         return qApp->translate("QtPromptService", "&Yes");
00480     case BUTTON_TITLE_NO:
00481         return qApp->translate("QtPromptService", "&No");
00482     case BUTTON_TITLE_SAVE:
00483         return qApp->translate("QtPromptService", "&Save");
00484     case BUTTON_TITLE_DONT_SAVE:
00485         return qApp->translate("QtPromptService", "&Don't Save");
00486     case BUTTON_TITLE_REVERT:
00487         return qApp->translate("QtPromptService", "&Revert");
00488     case BUTTON_TITLE_IS_STRING:
00489         return qApp->translate("QtPromptService",
00490                                QString::fromUcs2(aStringValue));
00491     case 0:
00492         return QString::null;
00493     default:
00494         NS_WARNING("Unexpected button flags");
00495         return QString::null;
00496     }
00497 }