ginebra2/GWebPage.h
changeset 0 1450b09d0cfd
child 3 0954f5dd2cd0
child 5 0f2326c2a325
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #ifndef __GINEBRA_GWEBPAGE_H__
       
    20 #define __GINEBRA_GWEBPAGE_H__
       
    21 
       
    22 #include <QDebug>
       
    23 #include <QWebPage>
       
    24 #include <QWebFrame>
       
    25 #include "ChromeWidget.h"
       
    26 
       
    27 namespace GVA {
       
    28 
       
    29   // ------------------------------
       
    30   // Simple wrapper class for QWebPage to allow interception of javascript errors.
       
    31   class WebPageWrapper : public QWebPage {
       
    32     public:
       
    33       WebPageWrapper(QObject *parent, const QString &prefix)
       
    34         : QWebPage(parent),
       
    35           m_prefix(prefix) {
       
    36         qDebug() << "WebPageWrapper::WebPageWrapper";
       
    37       }
       
    38 
       
    39       // Called when javascript errors are hit in the chrome page.
       
    40       virtual void javaScriptConsoleMessage(const QString & message, int lineNumber, const QString & sourceID) {
       
    41         qDebug() << m_prefix << ":";
       
    42         qDebug() << (const char*)QString("===\t%2:%3 %4")
       
    43               .arg(sourceID)
       
    44               .arg(lineNumber)
       
    45               .arg(message).toAscii();
       
    46       }
       
    47       QString m_prefix;
       
    48   };
       
    49   // ------------------------------
       
    50 
       
    51   class GWebPage : public QObject {
       
    52       Q_OBJECT
       
    53     public:
       
    54       GWebPage(QWebPage *page) {
       
    55         m_page = page;
       
    56       }
       
    57 
       
    58       Q_PROPERTY(QString name READ objectName)  // JS API
       
    59       Q_PROPERTY(QString title READ getTitle)  // JS API
       
    60       QString getTitle() {
       
    61         return m_page->mainFrame()->title();
       
    62       }
       
    63 
       
    64       QWebPage *page() { return m_page; }
       
    65       operator QWebPage *() { return m_page; }
       
    66 
       
    67       void dump() {
       
    68         qDebug() << "GWebPage::dump: " << this;
       
    69         qDebug() << "   page=" << (m_page ? m_page : 0);
       
    70       }
       
    71 
       
    72     protected:
       
    73       QWebPage *m_page;
       
    74   };
       
    75 
       
    76   // ------------------------------
       
    77   /*! \ingroup JavascriptAPI
       
    78    * \brief A content view that has full access to the Javascript APIs.
       
    79    *
       
    80    * Example code to load an HTML file into a super page:
       
    81    * \code
       
    82    * window.views.WebView.createSuperPage("BookmarkView", true);
       
    83    * window.views.WebView.BookmarkView.load("./chrome/BookmarkView.html");
       
    84    * \endcode
       
    85    */
       
    86   class GSuperWebPage : public GWebPage {
       
    87       Q_OBJECT
       
    88     public:
       
    89       GSuperWebPage(WebPageWrapper *page, ChromeWidget *chromeWidget)
       
    90         : GWebPage(page),
       
    91           m_chromeWidget(chromeWidget)
       
    92       {
       
    93         if(!m_page) {
       
    94           m_page = new WebPageWrapper(this, "Superpage javascript error");
       
    95         }
       
    96         qDebug() << "GSuperWebPage::GSuperWebPage: page=" << GWebPage::page();
       
    97         connect(GWebPage::page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(onJavaScriptWindowObjectCleared()));
       
    98       }
       
    99     public slots:
       
   100       void load(const QString &url) {   // JS API
       
   101         qDebug() << "GSuperWebPage::load: " << url;
       
   102         page()->mainFrame()->load(url);
       
   103       }
       
   104     private slots:
       
   105       void onJavaScriptWindowObjectCleared() {
       
   106         qDebug() << "GSuperWebPage::onJavaScriptWindowObjectCleared: " << objectName();
       
   107         if(m_chromeWidget)
       
   108           m_chromeWidget->exportJSObjectsToPage(m_page);
       
   109       }
       
   110 
       
   111     private:
       
   112       ChromeWidget *m_chromeWidget;  // not owned
       
   113   };
       
   114 }
       
   115 
       
   116 #endif // __GINEBRA_GWEBPAGE_H__