Title: [100767] trunk/Source/WebKit2
Revision
100767
Author
[email protected]
Date
2011-11-18 06:33:01 -0800 (Fri, 18 Nov 2011)

Log Message

[Qt] Support customizing JS alert/confirm/prompt dialogs using QML
https://bugs.webkit.org/show_bug.cgi?id=72319

Reviewed by Simon Hausmann.

Adds alertDialog, confirmDialog and promptDialog properties to QQuickWebView's
privateObject. These are QML components that are created by the webview when the
corresponding function is called in JS.

The dialogs are created in a context that contains a model object, similar to
ListView delegates. The "message" and "defaultValue" parameters are available
in the model object, as well as slots expected to be called by the dialog.

This commit removes the old code for supporting QWidget builtin dialogs. The code
wasn't working properly (closing any dialog was closing the app after QWindow
refactoring in Qt5).

* Target.pri:
* UIProcess/API/qt/qquickwebview.cpp:
(QQuickWebViewPrivate::runJavaScriptAlert):
(QQuickWebViewPrivate::runJavaScriptConfirm):
(QQuickWebViewPrivate::runJavaScriptPrompt):
(QQuickWebViewExperimental::alertDialog):
(QQuickWebViewExperimental::setAlertDialog):
(QQuickWebViewExperimental::confirmDialog):
(QQuickWebViewExperimental::setConfirmDialog):
(QQuickWebViewExperimental::promptDialog):
(QQuickWebViewExperimental::setPromptDialog):
* UIProcess/API/qt/qquickwebview_p.h:
* UIProcess/API/qt/qquickwebview_p_p.h:
* UIProcess/API/qt/tests/qmltests/WebView/tst_javaScriptDialogs.qml: Added.
* UIProcess/API/qt/tests/qmltests/common/alert.html: Added.
* UIProcess/API/qt/tests/qmltests/common/confirm.html: Added.
* UIProcess/API/qt/tests/qmltests/common/prompt.html: Added.
* UIProcess/API/qt/tests/qmltests/qmltests.pro:
* UIProcess/qt/QtDialogRunner.cpp: Added.
(QtDialogRunner::QtDialogRunner):
(QtDialogRunner::~QtDialogRunner):
(DialogContextObject::DialogContextObject):
(DialogContextObject::message):
(DialogContextObject::defaultValue):
(DialogContextObject::dismiss):
(DialogContextObject::accept):
(DialogContextObject::reject):
(QtDialogRunner::initForAlert):
(QtDialogRunner::initForConfirm):
(QtDialogRunner::initForPrompt):
(QtDialogRunner::createDialog):
* UIProcess/qt/QtDialogRunner.h: Added.
(QtDialogRunner::wasAccepted):
(QtDialogRunner::result):
(QtDialogRunner::onAccepted):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit2/ChangeLog (100766 => 100767)


--- trunk/Source/WebKit2/ChangeLog	2011-11-18 14:23:01 UTC (rev 100766)
+++ trunk/Source/WebKit2/ChangeLog	2011-11-18 14:33:01 UTC (rev 100767)
@@ -1,3 +1,58 @@
+2011-11-17  Caio Marcelo de Oliveira Filho  <[email protected]>
+
+        [Qt] Support customizing JS alert/confirm/prompt dialogs using QML
+        https://bugs.webkit.org/show_bug.cgi?id=72319
+
+        Reviewed by Simon Hausmann.
+
+        Adds alertDialog, confirmDialog and promptDialog properties to QQuickWebView's
+        privateObject. These are QML components that are created by the webview when the
+        corresponding function is called in JS.
+
+        The dialogs are created in a context that contains a model object, similar to
+        ListView delegates. The "message" and "defaultValue" parameters are available
+        in the model object, as well as slots expected to be called by the dialog.
+
+        This commit removes the old code for supporting QWidget builtin dialogs. The code
+        wasn't working properly (closing any dialog was closing the app after QWindow
+        refactoring in Qt5).
+
+        * Target.pri:
+        * UIProcess/API/qt/qquickwebview.cpp:
+        (QQuickWebViewPrivate::runJavaScriptAlert):
+        (QQuickWebViewPrivate::runJavaScriptConfirm):
+        (QQuickWebViewPrivate::runJavaScriptPrompt):
+        (QQuickWebViewExperimental::alertDialog):
+        (QQuickWebViewExperimental::setAlertDialog):
+        (QQuickWebViewExperimental::confirmDialog):
+        (QQuickWebViewExperimental::setConfirmDialog):
+        (QQuickWebViewExperimental::promptDialog):
+        (QQuickWebViewExperimental::setPromptDialog):
+        * UIProcess/API/qt/qquickwebview_p.h:
+        * UIProcess/API/qt/qquickwebview_p_p.h:
+        * UIProcess/API/qt/tests/qmltests/WebView/tst_javaScriptDialogs.qml: Added.
+        * UIProcess/API/qt/tests/qmltests/common/alert.html: Added.
+        * UIProcess/API/qt/tests/qmltests/common/confirm.html: Added.
+        * UIProcess/API/qt/tests/qmltests/common/prompt.html: Added.
+        * UIProcess/API/qt/tests/qmltests/qmltests.pro:
+        * UIProcess/qt/QtDialogRunner.cpp: Added.
+        (QtDialogRunner::QtDialogRunner):
+        (QtDialogRunner::~QtDialogRunner):
+        (DialogContextObject::DialogContextObject):
+        (DialogContextObject::message):
+        (DialogContextObject::defaultValue):
+        (DialogContextObject::dismiss):
+        (DialogContextObject::accept):
+        (DialogContextObject::reject):
+        (QtDialogRunner::initForAlert):
+        (QtDialogRunner::initForConfirm):
+        (QtDialogRunner::initForPrompt):
+        (QtDialogRunner::createDialog):
+        * UIProcess/qt/QtDialogRunner.h: Added.
+        (QtDialogRunner::wasAccepted):
+        (QtDialogRunner::result):
+        (QtDialogRunner::onAccepted):
+
 2011-11-18  Kenneth Rohde Christiansen  <[email protected]>
 
         [Qt] Support wheel event together with resizesToContents

Modified: trunk/Source/WebKit2/Target.pri (100766 => 100767)


--- trunk/Source/WebKit2/Target.pri	2011-11-18 14:23:01 UTC (rev 100766)
+++ trunk/Source/WebKit2/Target.pri	2011-11-18 14:33:01 UTC (rev 100767)
@@ -318,6 +318,7 @@
     UIProcess/qt/QtPinchGestureRecognizer.h \
     UIProcess/qt/QtTapGestureRecognizer.h \
     UIProcess/qt/QtWebError.h \
+    UIProcess/qt/QtDialogRunner.h \
     UIProcess/qt/QtDownloadManager.h \
     UIProcess/qt/QtWebPageProxy.h \
     UIProcess/qt/qwkhistory.h \
@@ -622,6 +623,7 @@
     UIProcess/qt/QtPinchGestureRecognizer.cpp \
     UIProcess/qt/QtTapGestureRecognizer.cpp \
     UIProcess/qt/QtWebError.cpp \
+    UIProcess/qt/QtDialogRunner.cpp \
     UIProcess/qt/QtDownloadManager.cpp \
     UIProcess/qt/QtWebPageProxy.cpp \
     UIProcess/qt/qwkhistory.cpp \

Modified: trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp (100766 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp	2011-11-18 14:23:01 UTC (rev 100766)
+++ trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp	2011-11-18 14:33:01 UTC (rev 100767)
@@ -21,6 +21,7 @@
 #include "config.h"
 #include "qquickwebview_p.h"
 
+#include "QtDialogRunner.h"
 #include "QtWebPageProxy.h"
 #include "UtilsQt.h"
 #include "WebPageGroup.h"
@@ -33,11 +34,13 @@
 #include <QtDeclarative/QQuickCanvas>
 #include <QtWidgets/QFileDialog>
 #include <QtWidgets/QInputDialog>
-#include <QtWidgets/QMessageBox>
 #include <WKOpenPanelResultListener.h>
 
 QQuickWebViewPrivate::QQuickWebViewPrivate()
     : q_ptr(0)
+    , alertDialog(0)
+    , confirmDialog(0)
+    , promptDialog(0)
     , postTransitionState(adoptPtr(new PostTransitionState(this)))
     , transitioningToNewPage(false)
     , useTraditionalDesktopBehaviour(false)
@@ -221,45 +224,56 @@
 
 void QQuickWebViewPrivate::runJavaScriptAlert(const QString& alertText)
 {
-#ifndef QT_NO_MESSAGEBOX
+    if (!alertDialog)
+        return;
+
     Q_Q(QQuickWebView);
-    const QString title = QObject::tr("_javascript_ Alert - %1").arg(q->url().host());
+    QtDialogRunner dialogRunner;
+    if (!dialogRunner.initForAlert(alertDialog, q, alertText))
+        return;
+
     disableMouseEvents();
-    QMessageBox::information(0, title, escapeHtml(alertText), QMessageBox::Ok);
+    dialogRunner.exec();
     enableMouseEvents();
-#else
-    Q_UNUSED(alertText);
-#endif
 }
 
 bool QQuickWebViewPrivate::runJavaScriptConfirm(const QString& message)
 {
-    bool result = true;
-#ifndef QT_NO_MESSAGEBOX
+    if (!confirmDialog)
+        return true;
+
     Q_Q(QQuickWebView);
-    const QString title = QObject::tr("_javascript_ Confirm - %1").arg(q->url().host());
+    QtDialogRunner dialogRunner;
+    if (!dialogRunner.initForConfirm(confirmDialog, q, message))
+        return true;
+
     disableMouseEvents();
-    result = QMessageBox::Yes == QMessageBox::information(0, title, escapeHtml(message), QMessageBox::Yes, QMessageBox::No);
+    dialogRunner.exec();
     enableMouseEvents();
-#else
-    Q_UNUSED(message);
-#endif
-    return result;
+
+    return dialogRunner.wasAccepted();
 }
 
 QString QQuickWebViewPrivate::runJavaScriptPrompt(const QString& message, const QString& defaultValue, bool& ok)
 {
-#ifndef QT_NO_INPUTDIALOG
+    if (!promptDialog) {
+        ok = true;
+        return defaultValue;
+    }
+
     Q_Q(QQuickWebView);
-    const QString title = QObject::tr("_javascript_ Prompt - %1").arg(q->url().host());
+    QtDialogRunner dialogRunner;
+    if (!dialogRunner.initForPrompt(promptDialog, q, message, defaultValue)) {
+        ok = true;
+        return defaultValue;
+    }
+
     disableMouseEvents();
-    QString result = QInputDialog::getText(0, title, escapeHtml(message), QLineEdit::Normal, defaultValue, &ok);
+    dialogRunner.exec();
     enableMouseEvents();
-    return result;
-#else
-    Q_UNUSED(message);
-    return defaultValue;
-#endif
+
+    ok = dialogRunner.wasAccepted();
+    return dialogRunner.result();
 }
 
 void QQuickWebViewPrivate::chooseFiles(WKOpenPanelResultListenerRef listenerRef, const QStringList& selectedFileNames, QtWebPageProxy::FileChooserType type)
@@ -388,6 +402,51 @@
     d->setUseTraditionalDesktopBehaviour(enable);
 }
 
+QDeclarativeComponent* QQuickWebViewExperimental::alertDialog() const
+{
+    Q_D(const QQuickWebView);
+    return d->alertDialog;
+}
+
+void QQuickWebViewExperimental::setAlertDialog(QDeclarativeComponent* alertDialog)
+{
+    Q_D(QQuickWebView);
+    if (d->alertDialog == alertDialog)
+        return;
+    d->alertDialog = alertDialog;
+    emit alertDialogChanged();
+}
+
+QDeclarativeComponent* QQuickWebViewExperimental::confirmDialog() const
+{
+    Q_D(const QQuickWebView);
+    return d->confirmDialog;
+}
+
+void QQuickWebViewExperimental::setConfirmDialog(QDeclarativeComponent* confirmDialog)
+{
+    Q_D(QQuickWebView);
+    if (d->confirmDialog == confirmDialog)
+        return;
+    d->confirmDialog = confirmDialog;
+    emit confirmDialogChanged();
+}
+
+QDeclarativeComponent* QQuickWebViewExperimental::promptDialog() const
+{
+    Q_D(const QQuickWebView);
+    return d->promptDialog;
+}
+
+void QQuickWebViewExperimental::setPromptDialog(QDeclarativeComponent* promptDialog)
+{
+    Q_D(QQuickWebView);
+    if (d->promptDialog == promptDialog)
+        return;
+    d->promptDialog = promptDialog;
+    emit promptDialogChanged();
+}
+
 QQuickWebView::QQuickWebView(QQuickItem* parent)
     : QQuickItem(parent)
     , d_ptr(new QQuickWebViewPrivate)

Modified: trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h (100766 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h	2011-11-18 14:23:01 UTC (rev 100766)
+++ trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h	2011-11-18 14:33:01 UTC (rev 100767)
@@ -24,6 +24,7 @@
 #include "qwebkitglobal.h"
 #include <QtDeclarative/qquickitem.h>
 
+class QDeclarativeComponent;
 class QQuickWebPage;
 class QQuickWebViewPrivate;
 class QQuickWebViewExperimental;
@@ -137,13 +138,29 @@
 
 class QWEBKIT_EXPORT QQuickWebViewExperimental : public QObject {
     Q_OBJECT
+    Q_PROPERTY(QDeclarativeComponent* alertDialog READ alertDialog WRITE setAlertDialog NOTIFY alertDialogChanged)
+    Q_PROPERTY(QDeclarativeComponent* confirmDialog READ confirmDialog WRITE setConfirmDialog NOTIFY confirmDialogChanged)
+    Q_PROPERTY(QDeclarativeComponent* promptDialog READ promptDialog WRITE setPromptDialog NOTIFY promptDialogChanged)
+
 public:
     QQuickWebViewExperimental(QQuickWebView* webView);
     virtual ~QQuickWebViewExperimental();
 
+    QDeclarativeComponent* alertDialog() const;
+    void setAlertDialog(QDeclarativeComponent*);
+    QDeclarativeComponent* confirmDialog() const;
+    void setConfirmDialog(QDeclarativeComponent*);
+    QDeclarativeComponent* promptDialog() const;
+    void setPromptDialog(QDeclarativeComponent*);
+
 public Q_SLOTS:
     void setUseTraditionalDesktopBehaviour(bool enable);
 
+Q_SIGNALS:
+    void alertDialogChanged();
+    void confirmDialogChanged();
+    void promptDialogChanged();
+
 private:
     QQuickWebView* q_ptr;
     QQuickWebViewPrivate* d_ptr;

Modified: trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h (100766 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h	2011-11-18 14:23:01 UTC (rev 100766)
+++ trunk/Source/WebKit2/UIProcess/API/qt/qquickwebview_p_p.h	2011-11-18 14:33:01 UTC (rev 100767)
@@ -35,12 +35,15 @@
 class QtWebPageProxy;
 
 QT_BEGIN_NAMESPACE
+class QDeclarativeComponent;
 class QFileDialog;
 QT_END_NAMESPACE
 
 class QQuickWebViewPrivate : public WebKit::QtPolicyInterface {
 
     Q_DECLARE_PUBLIC(QQuickWebView)
+    friend class QQuickWebViewExperimental;
+
 public:
     QQuickWebViewPrivate();
     virtual ~QQuickWebViewPrivate() { }
@@ -112,6 +115,10 @@
     QQuickWebView* q_ptr;
     QScopedPointer<QtWebPageProxy> pageProxy;
 
+    QDeclarativeComponent* alertDialog;
+    QDeclarativeComponent* confirmDialog;
+    QDeclarativeComponent* promptDialog;
+
     WebCore::ViewportArguments viewportArguments;
     OwnPtr<PostTransitionState> postTransitionState;
     bool transitioningToNewPage;

Added: trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_javaScriptDialogs.qml (0 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_javaScriptDialogs.qml	                        (rev 0)
+++ trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/WebView/tst_javaScriptDialogs.qml	2011-11-18 14:33:01 UTC (rev 100767)
@@ -0,0 +1,127 @@
+import QtQuick 2.0
+import QtTest 1.0
+import QtWebKit 3.0
+import QtWebKit.experimental 3.0
+
+WebView {
+    id: webView
+
+    property bool modelMessageEqualsMessage: false
+    property string messageFromAlertDialog: ""
+    property int confirmCount: 0
+    property int promptCount: 0
+
+    experimental.alertDialog: Item {
+        Timer {
+            running: true
+            interval: 1
+            onTriggered: {
+                webView.messageFromAlertDialog = message
+                webView.modelMessageEqualsMessage = Boolean(model.message == message)
+                model.dismiss()
+            }
+        }
+    }
+
+    experimental.confirmDialog: Item {
+        Timer {
+            running: true
+            interval: 1
+            onTriggered: {
+                webView.confirmCount += 1
+                if (message == "ACCEPT")
+                    model.accept()
+                else
+                    model.reject()
+            }
+        }
+    }
+
+    experimental.promptDialog: Item {
+        Timer {
+            running: true
+            interval: 1
+            onTriggered: {
+                webView.promptCount += 1
+                if (message == "REJECT")
+                    model.reject()
+                else {
+                    var reversedDefaultValue = defaultValue.split("").reverse().join("")
+                    model.accept(reversedDefaultValue)
+                }
+            }
+        }
+    }
+
+    SignalSpy {
+        id: loadSpy
+        target: webView
+        signalName: "loadSucceeded"
+    }
+
+    TestCase {
+        id: test
+        name: "WebViewJavaScriptDialogs"
+
+        function init() {
+            webView.modelMessageEqualsMessage = false
+            webView.messageFromAlertDialog = ""
+            webView.confirmCount = 0
+            webView.promptCount = 0
+            loadSpy.clear()
+        }
+
+        function test_alert() {
+            webView.load(Qt.resolvedUrl("../common/alert.html"))
+            loadSpy.wait()
+            compare(loadSpy.count, 1)
+            compare(webView.messageFromAlertDialog, "Hello Qt")
+            verify(webView.modelMessageEqualsMessage)
+        }
+
+        function test_alertWithoutDialog() {
+            skip("Setting experimental properties from JS code isn't working")
+            webView.experimental.alertDialog = null
+            webView.load(Qt.resolvedUrl("../common/alert.html"))
+            loadSpy.wait()
+            compare(loadSpy.count, 1)
+            compare(webView.messageFromAlertDialog, "")
+        }
+
+        function test_confirm() {
+            webView.load(Qt.resolvedUrl("../common/confirm.html"))
+            loadSpy.wait()
+            compare(loadSpy.count, 1)
+            compare(webView.confirmCount, 2)
+            compare(webView.title, "ACCEPTED REJECTED")
+        }
+
+        function test_confirmWithoutDialog() {
+            skip("Setting experimental properties from JS code isn't working")
+            webView.experimental.confirmDialog = null
+            webView.load(Qt.resolvedUrl("../common/confirm.html"))
+            loadSpy.wait()
+            compare(loadSpy.count, 1)
+            compare(webView.confirmCount, 0)
+            compare(webView.title, "ACCEPTED ACCEPTED")
+        }
+
+        function test_prompt() {
+            webView.load(Qt.resolvedUrl("../common/prompt.html"))
+            loadSpy.wait()
+            compare(loadSpy.count, 1)
+            compare(webView.promptCount, 2)
+            compare(webView.title, "tQ olleH")
+        }
+
+        function test_promptWithoutDialog() {
+            skip("Setting experimental properties from JS code isn't working")
+            webView.experimental.promptDialog = null
+            webView.load(Qt.resolvedUrl("../common/prompt.html"))
+            loadSpy.wait()
+            compare(loadSpy.count, 1)
+            compare(webView.promptCount, 0)
+            compare(webView.title, "FAIL")
+        }
+    }
+}

Added: trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/alert.html (0 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/alert.html	                        (rev 0)
+++ trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/alert.html	2011-11-18 14:33:01 UTC (rev 100767)
@@ -0,0 +1,8 @@
+<!doctype html>
+<html>
+<head>
+<script>alert("Hello Qt");</script>
+</head>
+<body>
+</body>
+</html>

Added: trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/confirm.html (0 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/confirm.html	                        (rev 0)
+++ trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/confirm.html	2011-11-18 14:33:01 UTC (rev 100767)
@@ -0,0 +1,19 @@
+<!doctype html>
+<html>
+<head>
+<script>
+document.title = "";
+function updateTitle(accepted) {
+    if (accepted)
+        document.title += " ACCEPTED";
+    else
+        document.title += " REJECTED";
+}
+
+updateTitle(confirm("ACCEPT"));
+updateTitle(confirm("REJECT"));
+</script>
+</head>
+<body>
+</body>
+</html>

Added: trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/prompt.html (0 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/prompt.html	                        (rev 0)
+++ trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/common/prompt.html	2011-11-18 14:33:01 UTC (rev 100767)
@@ -0,0 +1,13 @@
+<!doctype html>
+<html>
+<head>
+<script>
+document.title = prompt("Please, reverse the default value", "Hello Qt");
+if (prompt("REJECT") !== null) {
+    document.title = "FAIL";
+}
+</script>
+</head>
+<body>
+</body>
+</html>

Modified: trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/qmltests.pro (100766 => 100767)


--- trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/qmltests.pro	2011-11-18 14:23:01 UTC (rev 100766)
+++ trunk/Source/WebKit2/UIProcess/API/qt/tests/qmltests/qmltests.pro	2011-11-18 14:33:01 UTC (rev 100767)
@@ -16,6 +16,7 @@
 DEFINES += IMPORT_DIR=\"\\\"$${ROOT_BUILD_DIR}$${QMAKE_DIR_SEP}imports\\\"\"
 
 OTHER_FILES += \
+    WebView/tst_javaScriptDialogs.qml \
     WebView/tst_properties.qml \
     WebView/tst_loadFail.qml \
     WebView/tst_loadProgress.qml \

Added: trunk/Source/WebKit2/UIProcess/qt/QtDialogRunner.cpp (0 => 100767)


--- trunk/Source/WebKit2/UIProcess/qt/QtDialogRunner.cpp	                        (rev 0)
+++ trunk/Source/WebKit2/UIProcess/qt/QtDialogRunner.cpp	2011-11-18 14:33:01 UTC (rev 100767)
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "config.h"
+#include "QtDialogRunner.h"
+
+#include <QtDeclarative/QDeclarativeComponent>
+#include <QtDeclarative/QDeclarativeContext>
+#include <QtDeclarative/QDeclarativeEngine>
+#include <QtDeclarative/QQuickItem>
+#include <wtf/PassOwnPtr.h>
+
+QtDialogRunner::QtDialogRunner()
+    : QEventLoop()
+    , m_wasAccepted(false)
+{
+}
+
+QtDialogRunner::~QtDialogRunner()
+{
+}
+
+class DialogContextObject : public QObject {
+    Q_OBJECT
+    Q_PROPERTY(QString message READ message CONSTANT)
+    Q_PROPERTY(QString defaultValue READ defaultValue CONSTANT)
+
+public:
+    DialogContextObject(const QString& message, const QString& defaultValue = QString())
+        : QObject()
+        , m_message(message)
+        , m_defaultValue(defaultValue)
+    {
+    }
+    QString message() const { return m_message; }
+    QString defaultValue() const { return m_defaultValue; }
+
+public slots:
+    void dismiss() { emit dismissed(); }
+    void accept() { emit accepted(); }
+    void accept(const QString& result) { emit accepted(result); }
+    void reject() { emit rejected(); }
+
+signals:
+    void dismissed();
+    void accepted();
+    void accepted(const QString& result);
+    void rejected();
+
+private:
+    QString m_message;
+    QString m_defaultValue;
+};
+
+bool QtDialogRunner::initForAlert(QDeclarativeComponent* component, QQuickItem* dialogParent, const QString& message)
+{
+    DialogContextObject* contextObject = new DialogContextObject(message);
+    if (!createDialog(component, dialogParent, contextObject))
+        return false;
+
+    connect(contextObject, SIGNAL(dismissed()), SLOT(quit()));
+    return true;
+}
+
+bool QtDialogRunner::initForConfirm(QDeclarativeComponent* component, QQuickItem* dialogParent, const QString& message)
+{
+    DialogContextObject* contextObject = new DialogContextObject(message);
+    if (!createDialog(component, dialogParent, contextObject))
+        return false;
+
+    connect(contextObject, SIGNAL(accepted()), SLOT(onAccepted()));
+    connect(contextObject, SIGNAL(accepted()), SLOT(quit()));
+    connect(contextObject, SIGNAL(rejected()), SLOT(quit()));
+    return true;
+}
+
+bool QtDialogRunner::initForPrompt(QDeclarativeComponent* component, QQuickItem* dialogParent, const QString& message, const QString& defaultValue)
+{
+    DialogContextObject* contextObject = new DialogContextObject(message, defaultValue);
+    if (!createDialog(component, dialogParent, contextObject))
+        return false;
+
+    connect(contextObject, SIGNAL(accepted(QString)), SLOT(onAccepted(QString)));
+    connect(contextObject, SIGNAL(accepted(QString)), SLOT(quit()));
+    connect(contextObject, SIGNAL(rejected()), SLOT(quit()));
+    return true;
+}
+
+bool QtDialogRunner::createDialog(QDeclarativeComponent* component, QQuickItem* dialogParent, QObject* contextObject)
+{
+    QDeclarativeContext* baseContext = component->creationContext();
+    if (!baseContext)
+        baseContext = QDeclarativeEngine::contextForObject(dialogParent);
+    m_dialogContext = adoptPtr(new QDeclarativeContext(baseContext));
+
+    // This makes both "message" and "model.message" work for the dialog, just like QtQuick's ListView delegates.
+    contextObject->setParent(m_dialogContext.get());
+    m_dialogContext->setContextProperty(QLatin1String("model"), contextObject);
+    m_dialogContext->setContextObject(contextObject);
+
+    QObject* object = component->create(m_dialogContext.get());
+    if (!object) {
+        m_dialogContext.clear();
+        return false;
+    }
+
+    m_dialog = adoptPtr(qobject_cast<QQuickItem*>(object));
+    if (!m_dialog) {
+        m_dialogContext.clear();
+        m_dialog.clear();
+        return false;
+    }
+
+    m_dialog->setParentItem(dialogParent);
+    return true;
+}
+
+#include "QtDialogRunner.moc"
+#include "moc_QtDialogRunner.cpp"

Added: trunk/Source/WebKit2/UIProcess/qt/QtDialogRunner.h (0 => 100767)


--- trunk/Source/WebKit2/UIProcess/qt/QtDialogRunner.h	                        (rev 0)
+++ trunk/Source/WebKit2/UIProcess/qt/QtDialogRunner.h	2011-11-18 14:33:01 UTC (rev 100767)
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#ifndef QtDialogRunner_h
+#define QtDialogRunner_h
+
+#include <QtCore/QEventLoop>
+#include <wtf/OwnPtr.h>
+
+class QDeclarativeComponent;
+class QDeclarativeContext;
+class QQuickItem;
+
+class QtDialogRunner : public QEventLoop {
+    Q_OBJECT
+
+public:
+    QtDialogRunner();
+    virtual ~QtDialogRunner();
+
+    bool initForAlert(QDeclarativeComponent*, QQuickItem* dialogParent, const QString& message);
+    bool initForConfirm(QDeclarativeComponent*, QQuickItem* dialogParent, const QString& message);
+    bool initForPrompt(QDeclarativeComponent*, QQuickItem* dialogParent, const QString& message, const QString& defaultValue);
+
+    bool wasAccepted() const { return m_wasAccepted; }
+    QString result() const { return m_result; }
+
+public slots:
+    void onAccepted(const QString& result = QString())
+    {
+        m_wasAccepted = true;
+        m_result = result;
+    }
+
+private:
+    bool createDialog(QDeclarativeComponent*, QQuickItem* dialogParent, QObject* contextObject);
+
+    OwnPtr<QDeclarativeContext> m_dialogContext;
+    OwnPtr<QQuickItem> m_dialog;
+    QString m_result;
+    bool m_wasAccepted;
+};
+
+#endif // QtDialogRunner_h
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to