/*! @file

    @id $Id$
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef __SWISSWEBPAGE_HXX__
#define __SWISSWEBPAGE_HXX__

#include <qbrowserlib/pluginfactory.hxx>

#include <QtWebKit/QWebPage>
#include <QtCore/QProcessEnvironment>

#include <QtCore/QDebug>
#ifndef LOG
#define LOG qDebug()<<__PRETTY_FUNCTION__
#endif

//! @addtogroup qbrowserlib
//! @{

//! QWebPage with additional features and better default behaviour.
/*! SwissWebPage is designed to be used by SwissWebView.

    This QWebPage supports the folloing additional features:
     - Handling of plugins through PluginFactory
     - Processing of unsupportedContent
     - Delegation of all links
     - Signals @ref newPage if a new window should be created
     - Set useragent from environment variable @c SWISS_USERAGENT */
class SwissWebPage: public QWebPage {
    Q_OBJECT;
  signals:
    void newPage(SwissWebPage*);
  public:
    SwissWebPage(QObject *parent = 0): QWebPage(parent) {
      setPluginFactory(new PluginFactory);
      setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
      setForwardUnsupportedContent(true);
    }
  protected:
    virtual QWebPage* createWindow(WebWindowType type) {
      switch (type) {
        case QWebPage::WebBrowserWindow:
        case QWebPage::WebModalDialog: {
          SwissWebPage *page(new SwissWebPage);
          newPage(page);
          return page;
        } break;
      }
      return 0;
    }
    virtual QString userAgentForUrl(const QUrl& url) const {
      QString add(QProcessEnvironment::systemEnvironment()
                  .value("SWISS_USERAGENT"));
      return QWebPage::userAgentForUrl(url)+(add.size()?" "+add:QString());
    }
    QObject* createPlugin(const QString& classid, const QUrl& url,
                          const QStringList& paramNames,
                          const QStringList& paramValues) {
      LOG<<"classid:"<<classid
         <<"url:"<<url
         <<"paramNames:"<<paramNames.join(", ")
         <<"paramValues:"<<paramValues.join(", ");
      return QWebPage::createPlugin(classid, url, paramNames, paramValues);
    }
};

//! @}
#endif
