/*! @file

    @id $Id: browser.hxx 15 2010-06-03 14:17:27Z marc $

    This product includes software developed by the OpenSSL Project
    for use in the OpenSSL Toolkit (http://www.openssl.org/)

    This product includes cryptographic software written by
    Eric Young (eay@cryptsoft.com)
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#ifndef BROWSER_HXX
#define BROWSER_HXX

#include <qbrowserlib/log.hxx>
#include <smartcardauth.hxx>
#include <logincertificate.hxx>
#include <qbrowserlib/errorlog.hxx>
#include <qbrowserlib/downloadmanager.hxx>
#include <authentication.hxx>
#include <editbookmarks.hxx>
#include <proxyface/proxy.hxx>
#include <sslclientnetworkmanager.hxx>

#include <ui_browser.hxx>

#include <qbrowserlib/buttonlineedit.hxx>
#include <qbrowserlib/filestorage.hxx>
#include <qbrowserlib/swisswebview.hxx>
#include <qbrowserlib/settings.hxx>
#include <qbrowserlib/executor.hxx>

#include <QMainWindow>
#include <QProgressBar>
#include <QComboBox>
#include <QSlider>
#include <QMessageBox>
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QWebFrame>
#include <QWebHistory>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QSslError>
#include <QtNetwork/QNetworkProxy>
#include <QtCore/QDir>
#include <QFileDialog>

#include <QDesktopServices>

#include <stdexcept>
#include <cassert>
#include <memory>

extern QString TMP;
QString helptext(); // from main.cxx

//! @addtogroup browser
//! @{

class Browser: public QMainWindow, protected Ui::Browser {

    Q_OBJECT;

  public:

    Browser(const QString& actlib, const QStringList& urls = QStringList(),
            QSettings* settings=0,
            qbrowserlib::Settings::MimeTypes mimeTypes =
            qbrowserlib::Settings::MimeTypes(),
            bool kiosk = false, bool login = true, bool quirks=true,
            QString bookmarkfile=QString()):
        _url(0), _find(new qbrowserlib::ButtonLineEdit),
        _kiosk(kiosk),
        _downloadManager(new qbrowserlib::DownloadManager),
        _logincertificate(this),
        _proxy("http://swisssign.com", this),
        _showErrorLog(0),
        _startUrl(urls.size()),
        _quirks(quirks), _search(new qbrowserlib::ButtonLineEdit),
        _searchEngines(new QComboBox),
        _bookmarkfile(bookmarkfile) {
      TRC; LOG<<urls;
      qbrowserlib::Settings::instance(mimeTypes, this, settings, !kiosk);
      qbrowserlib::ErrorLog::instance(this);
      _home = "about:blank";
      if (urls.size()) _home = urls.at(0);
      setupUi(this);
      actionDebugLog->setVisible(qbrowserlib::Log::DEBUG);
      actionStop->setVisible(false);
      actionReload->setVisible(false);
      show();
      try {
        _networkManager.scAuth
          (new SmartCardAuth(suisseid::Scanner(actlib.toStdString()).scan(),
                             this, login));
      } catch (const std::exception& x) {
        LOG<<"No SmartCard Support:"<<x.what();
      }
      statusBar()->addPermanentWidget(_find);
      _find->add(actionUnFind);
      _find->add(actionClearFind);
      _find->hide();
      statusBar()->addPermanentWidget(_progress = new QProgressBar());
      QSizePolicy sp(_progress->sizePolicy());
      sp.setHorizontalPolicy(QSizePolicy::Minimum);
      _progress->setSizePolicy(sp);
      QPushButton* btn(0);
      statusBar()->addPermanentWidget
        (btn=new QPushButton(QIcon(":/icons/16x16/butt16_09.png"), ""));
      assert(connect(btn, SIGNAL(clicked(bool)), SLOT(smaller())));
      statusBar()->addPermanentWidget(_zoom = new QSlider(Qt::Horizontal));
      statusBar()->addPermanentWidget
        (btn=new QPushButton(QIcon(":/icons/16x16/butt16_10.png"), ""));
      assert(connect(btn, SIGNAL(clicked(bool)), SLOT(larger())));
      sp = _zoom->sizePolicy();
      sp.setHorizontalPolicy(QSizePolicy::Minimum);
      _zoom->setSizePolicy(sp);
      _zoom->setMinimum(10);
      _zoom->setMaximum(190);
      _zoom->setValue(100);
      _zoom->setSingleStep(1);
      _zoom->setPageStep(10);
      _zoom->setTickPosition(QSlider::TicksAbove);
      assert(connect(_zoom, SIGNAL(valueChanged(int)), SLOT(zoom(int))));
      if (_kiosk||!_quirks) {
        _toolbar->addAction(actionReload);
        _toolbar->addAction(actionStop);
      }
      if (!_kiosk) {
        QComboBox* combo(new QComboBox(_toolbar));
        _url = combo;
        QFrame* urlFrame(new QFrame);
        QHBoxLayout* layout(new QHBoxLayout);
        urlFrame->setLayout(layout);
        _toolbar->addWidget(urlFrame);
        layout->addWidget(_url);
        combo->setInsertPolicy(QComboBox::NoInsert);
        combo->setLineEdit(new qbrowserlib::ButtonLineEdit(combo));
        combo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);
        sp=combo->sizePolicy();
        sp.setHorizontalPolicy(QSizePolicy::Expanding);
        combo->setSizePolicy(sp);
        combo->setEditable(!_kiosk);
        assert(connect(combo, SIGNAL(currentIndexChanged(const QString&)),
                       SLOT(load(QString))));
        assert(connect(combo->lineEdit(), SIGNAL(returnPressed()),
                       SLOT(load())));
        assert(connect(combo->lineEdit(), SIGNAL(textChanged(QString)),
                       SLOT(goodUrl())));
        if (_quirks) {
          dynamic_cast<qbrowserlib::ButtonLineEdit*>(combo->lineEdit())->add
            (actionStop);
          dynamic_cast<qbrowserlib::ButtonLineEdit*>(combo->lineEdit())->add
            (actionReload);
        }
        if (!_quirks) {
          dynamic_cast<qbrowserlib::ButtonLineEdit*>(combo->lineEdit())->add
            (actionAddBookmark);
          dynamic_cast<qbrowserlib::ButtonLineEdit*>(combo->lineEdit())->add
            (actionClearLocation);
        }
        _editbookmarks =
          std::auto_ptr<EditBookmarks>(new EditBookmarks(combo, this));
        assert(connect(_editbookmarks.get(), SIGNAL(startEdit(bool)),
                       actionAddBookmark, SLOT(setDisabled(bool))));
        assert(connect(_editbookmarks.get(), SIGNAL(endEdit(bool)),
                       actionAddBookmark, SLOT(setEnabled(bool))));
        assert(connect(_editbookmarks.get(), SIGNAL(accepted()),
                       SLOT(saveBookmarks())));
      } else {
        QLineEdit* label(new QLineEdit(_toolbar));
        _url = label;
        label->setReadOnly(true);
        label->setEnabled(false);
        _toolbar->addWidget(_url);
      }
      if (!_kiosk) {
        QFrame* searchFrame(new QFrame);
        QHBoxLayout* layout(new QHBoxLayout);
        layout->addWidget(_searchEngines);
        layout->addWidget(_search);
        searchFrame->setLayout(layout);
        _toolbar->addWidget(searchFrame);
        _search->add(actionSearch);
      }
      if (!_quirks) _search->add(actionClearSearch);
      sp=_search->sizePolicy();
      sp.setHorizontalPolicy(QSizePolicy::Minimum);
      _search->setSizePolicy(sp);
      assert(connect(_search, SIGNAL(returnPressed()),
                     SLOT(on_actionSearch_triggered())));
      if (_quirks) {
        _toolbar->setMovable(false);
        _toolbar->setFloatable(false);
        if (!_kiosk) _toolbar->addAction(actionAddBookmark);
        _toolbar->addAction(actionHome);
      }
      (*_downloadManager)+=&_networkManager;
      assert(connect(&qbrowserlib::ErrorLog::instance(),
                     SIGNAL(reset()), SLOT(errorReset())));
      assert(connect(&_networkManager, SIGNAL(finished(QNetworkReply*)),
                     SLOT(finished(QNetworkReply*))));
      assert(connect(_downloadManager.data(), SIGNAL(progress(qint64, qint64)),
                     SLOT(progress(qint64, qint64))));
      assert(connect(_downloadManager.data(), SIGNAL(started()),
                     SLOT(started())));
      assert(connect(_downloadManager.data(), SIGNAL(finished()),
                     SLOT(finished())));
      assert(connect(_downloadManager.data(), SIGNAL(error(QString)),
                     SLOT(downloadError(QString))));
      assert(connect(_downloadManager.data(), SIGNAL(metaDataChanged(QNetworkReply*)),
                     SLOT(metaDataChanged(QNetworkReply*))));
      assert(connect(&qbrowserlib::Settings::instance(),
                     SIGNAL(newSettings()), SLOT(newSettings())));
      newSettings();
      on_actionNewTab_triggered();

      if (!settings || _kiosk) {
        actionSettings->setVisible(false);
        actionSaveWindowState->setVisible(false);
        actionLoadWindowState->setVisible(false);
      }
      if (_kiosk) {
        actionNew->setEnabled(false);
        actionNew->setVisible(false);
        actionNewTab->setEnabled(false);
        actionNewTab->setVisible(false);
        actionCloseTab->setVisible(false);
        actionEditBookmarks->setVisible(false);
        delete _bookmarks; _bookmarks=0;
      }
      if (!_kiosk &&
          ((!_startUrl &&
            qbrowserlib::Settings::instance().flag("SaveWindowState") &&
            qbrowserlib::Settings::instance()()) ||
           _bookmarkfile.readable()))
        loadWin();
      if (urls.size()) load(urls.at(0));
      for (int i(1); i<urls.size(); ++i) load(urls.at(i), newTab());
      if (!QSslSocket::supportsSsl())
        QMessageBox::critical(this, tr("SSL Not Supported"),
                              tr("SSL is not supported on your system"));
      // temporary caching of favicons
      QWebSettings::setIconDatabasePath(TMP);
    }

    ~Browser() {
      TRC;
      QWebSettings::clearIconDatabase();
      qbrowserlib::Settings::purge(); // free singleton
      qbrowserlib::ErrorLog::purge(); // free singleton
    }

    //! Whitelisting
    bool check(QUrl page) {
      return true;
    }

    //! Create new empty tab.
    qbrowserlib::SwissWebView* newTab() {
      qbrowserlib::SwissWebView* browser
        (new qbrowserlib::SwissWebView(0, &_networkManager));
      newTab(browser);
      return browser;
    }

    
  protected:
    
    void closeEvent(QCloseEvent *event) {
      TRC;
      if (!_kiosk &&
          ((!_startUrl &&
            qbrowserlib::Settings::instance().flag("SaveWindowState") &&
            qbrowserlib::Settings::instance()()) ||
           _bookmarkfile.writeable()))
        saveWin();
      QMainWindow::closeEvent(event);
      QApplication::exit(0);
    }


  public Q_SLOTS:

    void on_actionEditBookmarks_triggered() {
      _editbookmarks->show();
    }


  private Q_SLOTS:
      
    void load() {
      TRC;
      if (qobject_cast<QComboBox*>(_url))
        load(qobject_cast<QComboBox*>(_url)->currentText());
      else
        load(qobject_cast<QLineEdit*>(_url)->text());
    }
    
    void load(QString page) {
      qbrowserlib::Settings::instance().replaceSearchEngine(page);
      if (QUrl(page).scheme()=="") page = "http://"+page;
      if (qobject_cast<QComboBox*>(_url))
        qobject_cast<QComboBox*>(_url)->lineEdit()->setText(page);
      else
        qobject_cast<QLineEdit*>(_url)->setText(page);
      load(QUrl(page));
    }

    void load(QUrl page, QWebView* view=0) {
      TRC; LOG<<page.toString();
      statusBar()->showMessage(tr("Checking: %1").arg(page.toString()));
      if (!check(page)) {
        LOG<<"########## BLACK LISTED IGNORED ##########";
        statusBar()->showMessage(tr("Forbidden: %1").arg(page.toString()));
        QMessageBox::warning(this, tr("Access Denied"),
                             tr("<p>Access denied due to security"
                                " considerations.</p><p>You are not"
                                " allowed to connect to %1.")
                             .arg(page.toString()));
        return;
      }
      statusBar()->showMessage(tr("Reading: %1").arg(page.toString()));
      if (!page.isValid()) {
        statusBar()->showMessage(tr("Illegal URL: %1").arg(page.errorString()));
        return;
      }
      if (!view) view=qobject_cast<QWebView*>(_tabs->currentWidget());
      view->load(page);
    }

    //! Slot that receives and initializes a new SwissWebView
    void newTab(qbrowserlib::SwissWebView* browser) {
      _url->setFocus();
      // SwissWebView
      assert(connect(browser, SIGNAL(newView(qbrowserlib::SwissWebView*)),
                     SLOT(newTab(qbrowserlib::SwissWebView*))));
      // QWebView
      assert(connect(browser, SIGNAL(urlChanged(const QUrl&)),
                     SLOT(urlChanged(const QUrl&))));
      assert(connect(browser, SIGNAL(linkClicked(const QUrl&)),
                     SLOT(linkClicked(const QUrl&))));
      assert(connect(browser, SIGNAL(iconChanged()),
                     SLOT(iconChanged())));
      assert(connect(browser, SIGNAL(titleChanged(const QString&)),
                     SLOT(titleChanged(const QString&))));
      assert(connect(browser, SIGNAL(statusBarMessage(const QString&)),
                     SLOT(statusBarMessage(const QString&))));
      assert(connect(browser, SIGNAL(loadProgress(int)),
                     SLOT(loadProgress(int))));
      assert(connect(browser, SIGNAL(loadStarted()),
                     SLOT(loadStarted())));
      assert(connect(browser, SIGNAL(loadFinished(bool)),
                     SLOT(loadFinished(bool))));
      // QWebPage WebAction
      assert(connect(browser->pageAction(QWebPage::OpenLink),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredOpenLink(bool))));
      assert(connect(browser->pageAction(QWebPage::OpenLinkInNewWindow),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredOpenLinkInNewWindow(bool))));
      assert(connect(browser->pageAction(QWebPage::OpenFrameInNewWindow),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredOpenFrameInNewWindow(bool))));
      assert(connect(browser->pageAction(QWebPage::DownloadLinkToDisk),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredDownloadLinkToDisk(bool))));
      assert(connect(browser->pageAction(QWebPage::CopyLinkToClipboard),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredCopyLinkToClipboard(bool))));
      assert(connect(browser->pageAction(QWebPage::OpenImageInNewWindow),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredOpenImageInNewWindow(bool))));
      assert(connect(browser->pageAction(QWebPage::DownloadImageToDisk),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredDownloadImageToDisk(bool))));
      assert(connect(browser->pageAction(QWebPage::CopyImageToClipboard),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredCopyImageToClipboard(bool))));
      assert(connect(browser->pageAction(QWebPage::Back),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredBack(bool))));
      assert(connect(browser->pageAction(QWebPage::Forward),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredForward(bool))));
      assert(connect(browser->pageAction(QWebPage::Stop),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredStop(bool))));
      assert(connect(browser->pageAction(QWebPage::Reload),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredReload(bool))));
      assert(connect(browser->pageAction(QWebPage::Cut),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredCut(bool))));
      assert(connect(browser->pageAction(QWebPage::Copy),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredCopy(bool))));
      assert(connect(browser->pageAction(QWebPage::Paste),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredPaste(bool))));
      assert(connect(browser->pageAction(QWebPage::Undo),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredUndo(bool))));
      assert(connect(browser->pageAction(QWebPage::Redo),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredRedo(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToNextChar),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToNextChar(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToPreviousChar),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToPreviousChar(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToNextWord),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToNextWord(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToPreviousWord),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToPreviousWord(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToNextLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToNextLine(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToPreviousLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToPreviousLine(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToStartOfLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToStartOfLine(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToEndOfLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToEndOfLine(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToStartOfBlock),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToStartOfBlock(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToEndOfBlock),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToEndOfBlock(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToStartOfDocument),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToStartOfDocument(bool))));
      assert(connect(browser->pageAction(QWebPage::MoveToEndOfDocument),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredMoveToEndOfDocument(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectNextChar),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectNextChar(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectPreviousChar),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectPreviousChar(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectNextWord),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectNextWord(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectPreviousWord),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectPreviousWord(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectNextLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectNextLine(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectPreviousLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectPreviousLine(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectStartOfLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectStartOfLine(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectEndOfLine),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectEndOfLine(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectStartOfBlock),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectStartOfBlock(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectEndOfBlock),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectEndOfBlock(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectStartOfDocument),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectStartOfDocument(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectEndOfDocument),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectEndOfDocument(bool))));
      assert(connect(browser->pageAction(QWebPage::DeleteStartOfWord),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredDeleteStartOfWord(bool))));
      assert(connect(browser->pageAction(QWebPage::DeleteEndOfWord),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredDeleteEndOfWord(bool))));
      assert(connect(browser->pageAction(QWebPage::SetTextDirectionDefault),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSetTextDirectionDefault(bool))));
      assert(connect(browser->pageAction(QWebPage::SetTextDirectionLeftToRight),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSetTextDirectionLeftToRight(bool))));
      assert(connect(browser->pageAction(QWebPage::SetTextDirectionRightToLeft),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSetTextDirectionRightToLeft(bool))));
      assert(connect(browser->pageAction(QWebPage::ToggleBold),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredToggleBold(bool))));
      assert(connect(browser->pageAction(QWebPage::ToggleItalic),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredToggleItalic(bool))));
      assert(connect(browser->pageAction(QWebPage::ToggleUnderline),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredToggleUnderline(bool))));
      assert(connect(browser->pageAction(QWebPage::InspectElement),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredInspectElement(bool))));
      assert(connect(browser->pageAction(QWebPage::InsertParagraphSeparator),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredInsertParagraphSeparator(bool))));
      assert(connect(browser->pageAction(QWebPage::InsertLineSeparator),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredInsertLineSeparator(bool))));
      assert(connect(browser->pageAction(QWebPage::SelectAll),
                     SIGNAL(triggered(bool)),
                     SLOT(triggeredSelectAll(bool))));
      // QWebPage
      assert(connect(browser->page(), SIGNAL(contentsChanged()),
                     SLOT(contentsChanged())));
      assert(connect(browser->page(),
                     SIGNAL(databaseQuotaExceeded(QWebFrame*, QString)),
                     SLOT(databaseQuotaExceeded(QWebFrame*, QString))));
      assert(connect(browser->page(),
                     SIGNAL(downloadRequested(const QNetworkRequest&)),
                     SLOT(downloadRequested(const QNetworkRequest&))));
      assert(connect(browser->page(), SIGNAL(frameCreated(QWebFrame*)),
                     SLOT(frameCreated(QWebFrame*))));
      assert(connect(browser->page(),
                     SIGNAL(geometryChangeRequested(const QRect&)),
                     SLOT(geometryChangeRequested(const QRect&))));
//       assert(connect(browser->page(), SIGNAL(linkClicked(const QUrl&)),
//                      SLOT(linkClicked(const QUrl&))));
      assert(connect(browser->page(),
                     SIGNAL(linkHovered(const QString&, const QString&,
                                        const QString&)),
                     SLOT(linkHovered(const QString&, const QString&,
                                      const QString&))));
//       assert(connect(browser->page(), SIGNAL(loadFinished(bool)),
//                      SLOT(loadFinished(bool))));
//       assert(connect(browser->page(), SIGNAL(loadProgress(int)),
//                      SLOT(loadProgress(int))));
//       assert(connect(browser->page(), SIGNAL(loadStarted()),
//                      SLOT(loadStarted())));
      assert(connect(browser->page(),
                     SIGNAL(menuBarVisibilityChangeRequested(bool)),
                     SLOT(menuBarVisibilityChangeRequested(bool))));
      assert(connect(browser->page(), SIGNAL(microFocusChanged()),
                     SLOT(microFocusChanged())));
      assert(connect(browser->page(), SIGNAL(printRequested(QWebFrame*)),
                     SLOT(printRequested(QWebFrame*))));
      assert(connect(browser->page(), SIGNAL(repaintRequested(const QRect&)),
                     SLOT(repaintRequested(const QRect&))));
      assert(connect(browser->page(),
                     SIGNAL(restoreFrameStateRequested(QWebFrame*)),
                     SLOT(restoreFrameStateRequested(QWebFrame*))));
      assert(connect(browser->page(),
                     SIGNAL(saveFrameStateRequested(QWebFrame*,
                                                    QWebHistoryItem*)),
                     SLOT(saveFrameStateRequested(QWebFrame*,
                                                  QWebHistoryItem*))));
      assert(connect(browser->page(),
                     SIGNAL(scrollRequested(int, int, const QRect&)),
                     SLOT(scrollRequested(int, int, const QRect&))));
      assert(connect(browser->page(), SIGNAL(selectionChanged()),
                     SLOT(selectionChanged())));
//       assert(connect(browser->page(), SIGNAL(statusBarMessage(const QString&)),
//                      SLOT(statusBarMessage(const QString&))));
      assert(connect(browser->page(),
                     SIGNAL(statusBarVisibilityChangeRequested(bool)),
                     SLOT(statusBarVisibilityChangeRequested(bool))));
      assert(connect(browser->page(),
                     SIGNAL(toolBarVisibilityChangeRequested(bool)),
                     SLOT(toolBarVisibilityChangeRequested(bool))));
      assert(connect(browser->page(),
                     SIGNAL(unsupportedContent(QNetworkReply*)),
                     SLOT(unsupportedContent(QNetworkReply*))));
      assert(connect(browser->page(), SIGNAL(windowCloseRequested()),
                     SLOT(windowCloseRequested())));
      // QNetworkAccessManager
      assert(connect(browser->page()->networkAccessManager(),
                     SIGNAL(authenticationRequired(QNetworkReply*,
                                                   QAuthenticator*)),
                     SLOT(authenticationRequired(QNetworkReply*,
                                                 QAuthenticator*))));
      assert(connect(browser->page()->networkAccessManager(),
                     SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&,
                                                 QAuthenticator*)),
                     &_proxy,
                     SLOT(proxyAuthenticationRequired(const QNetworkProxy&,
                                                      QAuthenticator*))));
      assert(connect(browser->page()->networkAccessManager(),
                     SIGNAL(sslErrors(QNetworkReply*, const QList<QSslError>&)),
                     SLOT(sslErrors(QNetworkReply*, const QList<QSslError>&))));
      _tabs->setCurrentIndex(_tabs->addTab(browser, tr("New Tab")));
      _tabs->setTabsClosable(_tabs->count()>1);
      //_tabs->tabBar()->setShown(_tabs->count()>1);
    }

    void newSettings() {
      _searchEngines->clear();
      for (qbrowserlib::Settings::SearchEngines::const_iterator
             it(qbrowserlib::Settings::instance().searchEngines().begin());
           it!=qbrowserlib::Settings::instance().searchEngines().end(); it++)
        if (QFile(":/icons/"+it.key()).exists())
          _searchEngines->addItem(QIcon(":/icons/"+it.key()), "", it.value());
        else
          _searchEngines->addItem(it.key(), it.value());
      if (_searchEngines->count()==0)
        _searchEngines->addItem(QIcon(":/icons/gg"), tr("gg"),
                                tr("http://www.google.com/search?hl=%2&q=%1"));
      _searchEngines->setVisible(_searchEngines->count()>1);
    }

    void on_actionAddBookmark_triggered() {
      QComboBox* url(qobject_cast<QComboBox*>(_url));
      if (!_bookmarks) return;
      if (_url && !url->currentText().isEmpty()) {
        url->addItem(url->currentText());
        QAction* a(_bookmarks->addAction(url->currentText()));
        a->setData(url->currentText());
        connect(a, SIGNAL(triggered(bool)), SLOT(loadFromHistory()));
        saveBookmarks();
      }
    }
    
    void smaller() {
      _zoom->setValue(_zoom->value()-_zoom->pageStep());
    }
    
    void larger() {
      _zoom->setValue(_zoom->value()+_zoom->pageStep());
    }
    
    void zoom(int i) {
      TRC; LOG<<i;
      statusBar()->showMessage(tr("Zoom: %1%").arg(i));
      qobject_cast<QWebView*>(_tabs->currentWidget())->setZoomFactor(i/100.0);
    }

    void  loadFromHistory() {
      load(qobject_cast<QAction*>(sender())->data().toString());
    }
    
    void on_actionHome_triggered() {
      TRC;
      load(_home);
    }
    
    void on_actionNew_triggered() {
      TRC;
      QStringList args(QCoreApplication::arguments());
      QString prg(args.takeFirst());
      statusBar()->showMessage(tr("opening new window"), 5000);
      QProcess::startDetached(prg, args);
    }

    void on_actionNewTab_triggered() {
      newTab();
    }

    void on_actionCloseTab_triggered() {
      on__tabs_tabCloseRequested(_tabs->currentIndex());
    }

    void on__tabs_currentChanged(int) {
      if (qobject_cast<QComboBox*>(_url)) {
        qobject_cast<QComboBox*>(_url)->setEditText
          (qobject_cast<QWebView*>(_tabs->currentWidget())
           ->url().toString());
        qobject_cast<QComboBox*>(_url)->lineEdit()->setCursorPosition(0);
      } else {
        qobject_cast<QLineEdit*>(_url)->setText
          (qobject_cast<QWebView*>(_tabs->currentWidget())
           ->url().toString());
        qobject_cast<QLineEdit*>(_url)->setCursorPosition(0);
      }
      resetWindowTitle();
      activateTab();
    }
    
    void on__tabs_tabCloseRequested(int index) {
      _tabs->removeTab(index);
      _tabs->setTabsClosable(_tabs->count()>1);
      //_tabs->tabBar()->setShown(_tabs->count()>1);
    }

    void on_actionPrintPreview_triggered() {
      QPrintPreviewDialog preview(&_printer, this);
      connect(&preview, SIGNAL(paintRequested(QPrinter*)),
              qobject_cast<QWebView*>(_tabs->currentWidget()),
              SLOT(print(QPrinter*)));
      preview.exec();
    }
    
    void on_actionInstantPrint_triggered() {
      qobject_cast<QWebView*>(_tabs->currentWidget())->print(&_printer);
    }
    
    void on_actionPrint_triggered() {
      QPrintDialog dialog(&_printer, this);
      dialog.setWindowTitle(tr("Print Document"));
      if (dialog.exec()!=QDialog::Accepted) return;
      on_actionInstantPrint_triggered();
    }

    void on_actionClose_triggered() {
      TRC;
      close();
    }

    void on_actionBack_triggered() {
      TRC;
      qobject_cast<QWebView*>(_tabs->currentWidget())->history()->back();
    }

    void on_actionBack_hovered() {
      TRC;
      if (!qobject_cast<QWebView*>(_tabs->currentWidget())->history()
          ->backItem().isValid())
        return;
      actionBack->setStatusTip
        (tr("%1 - %2", "statusbar actionBack_hovered %1=url %2=title")
         .arg(qobject_cast<QWebView*>(_tabs->currentWidget())->history()
              ->backItem().url().toString())
         .arg(qobject_cast<QWebView*>(_tabs->currentWidget())->history()
              ->backItem().title()));
      actionBack->showStatusText(this);
    }

    void on_actionForward_triggered() {
      TRC;
      qobject_cast<QWebView*>(_tabs->currentWidget())->history()->forward();
    }

    void on_actionForward_hovered() {
      TRC;
      if (!qobject_cast<QWebView*>(_tabs->currentWidget())->history()
          ->forwardItem().isValid())
        return;
      actionForward->setStatusTip
        (tr("%1 - %2", "statusbar actionForward_hovered %1=url %2=title")
         .arg(qobject_cast<QWebView*>(_tabs->currentWidget())->history()
              ->forwardItem().url().toString())
         .arg(qobject_cast<QWebView*>(_tabs->currentWidget())->history()
              ->forwardItem().title()));
      actionForward->showStatusText(this);
    }

    void on_actionReload_triggered() {
      TRC;
      qobject_cast<QWebView*>(_tabs->currentWidget())->reload();
    }

    void on_actionStop_triggered() {
      TRC;
      for (int i(0); i<_tabs->count(); ++i)
        qobject_cast<QWebView*>(_tabs->widget(i))->stop();
      _downloadManager->abort();
    }

    void on_actionClearLocation_triggered() {
      if (!qobject_cast<QComboBox*>(_url)) return;
      qobject_cast<QComboBox*>(_url)->clearEditText();
      qobject_cast<QComboBox*>(_url)->setFocus();
    }

    void on_actionClearFind_triggered() {
      _find->clear();
      _find->setFocus();
    }

    void on_actionFind_triggered() {
      if (_find->isHidden()) {
        _find->setText(qobject_cast<QWebView*>(_tabs->currentWidget())
                       ->selectedText());
        _find->show();
      }
      disconnect(_find, SIGNAL(returnPressed()), this, SLOT(rfind()));
      disconnect(_find, SIGNAL(textEdited(QString)), this, SLOT(rfind()));
      assert(connect(_find, SIGNAL(returnPressed()), SLOT(find())));
      assert(connect(_find, SIGNAL(textEdited(QString)), SLOT(find())));
      _find->changeStyleSheet(tr("background-color: white", "neutral find"));
      _find->setFocus();
    }

    void on_actionReverseFind_triggered() {
      if (_find->isHidden()) {
        _find->setText(qobject_cast<QWebView*>(_tabs->currentWidget())
                       ->selectedText());
        _find->show();
      }
      disconnect(_find, SIGNAL(returnPressed()), this, SLOT(find()));
      disconnect(_find, SIGNAL(textEdited(QString)), this, SLOT(find()));
      assert(connect(_find, SIGNAL(returnPressed()), SLOT(rfind())));
      assert(connect(_find, SIGNAL(textEdited(QString)), SLOT(rfind())));
      _find->changeStyleSheet(tr("background-color: white", "neutral find"));
      _find->setFocus();
    }

    void find(const QString& txt=QString()) {
      if (qobject_cast<QWebView*>(_tabs->currentWidget())->findText
          (_find->text(),
           QWebPage::FindWrapsAroundDocument))
        _find->changeStyleSheet(tr("background-color: #ADA", "text found"));
      else
        _find->changeStyleSheet(tr("background-color: #F77", "text not found"));
    }

    void rfind(const QString& txt=QString()) {
      if(qobject_cast<QWebView*>(_tabs->currentWidget())->findText
         (_find->text(),
          QWebPage::FindWrapsAroundDocument
          |QWebPage::FindBackward))
        _find->changeStyleSheet(tr("background-color: #ADA", "text found"));
      else
        _find->changeStyleSheet(tr("background-color: #F77", "text not found"));
    }

    void on_actionUnFind_triggered() {
      _find->hide();
    }
    
    void on_actionSearch_triggered() {
      load(_searchEngines->itemData(_searchEngines->currentIndex()).toString()
           .arg(_search->text())
           .arg(QLocale::system().name().left(2)));
    }

    void on_actionClearSearch_triggered() {
      _search->clear();
    }

    void on_actionProxy_triggered() {
      _proxy.show();
    }

    void on_actionLoginCertificate_triggered() {
      _logincertificate.show();
    }

    void on_actionErrorLog_triggered() {
      qbrowserlib::ErrorLog::instance().show();
    }

    void on_actionCommandline_triggered() {
      QMessageBox::information(this, tr("Commandline Arguments"), helptext());
    }
    
    void on_actionAbout_triggered() {
      QMessageBox::about(this, tr("About"),
                         tr("%8\n"
                            "Version: %1\n"
                            "Builddate: %2\n"
                            "Libraries:\n"
                            "%3\n"
                            "%4\n"
                            "qt-%5 (%6)\n"
                            "openssl-%7 (%9) %10\n\n"
                            "This product includes software"
                            " developed by the OpenSSL Project"
                            " for use in the OpenSSL Toolkit"
                            " (http://www.openssl.org/)\n\n"
                            "This product includes cryptographic"
                            " software written by Eric Young"
                            " (eay@cryptsoft.com)")
                         .arg(PACKAGE_VERSION)
                         .arg(MAKE_STRING(BUILD_DATE))
                         .arg(QString::fromStdString(proxy::version()))
                         .arg(QString::fromStdString(pcsc::version()))
                         .arg(qVersion())
                         .arg(QT_VERSION_STR)
                         .arg(SHLIB_VERSION_NUMBER)
                         .arg(QApplication::applicationName())
                         .arg(OPENSSL_VERSION_TEXT)
                         .arg(QSslSocket::supportsSsl()?"ok":"failed"));
    }

    void on_actionDebugLog_triggered() {
      qbrowserlib::Log::show(this);
    }
    
    void on_actionSettings_triggered() {
      qbrowserlib::Settings::instance().show();
    }

    void on_actionSaveWindowState_triggered() {
      saveWin();
    }

    void on_actionLoadWindowState_triggered() {
      loadWin();
    }

    void saveWin() {
      TRC; LOG<<"Save Window State";
      saveBookmarks();
      if (!qbrowserlib::Settings::instance()()) return;
      QStringList tabs;
      for (int i(0); i<_tabs->count(); ++i)
        tabs<<qobject_cast<QWebView*>(_tabs->widget(i))->url().toString();
      qbrowserlib::Settings::instance()()->setValue("Window/Tabs", tabs);
      qbrowserlib::Settings::instance()()
        ->setValue("Window/CurrentTab", _tabs->currentIndex());
      qbrowserlib::Settings::instance()()
        ->setValue("Window/Geometry", saveGeometry());
      qbrowserlib::Settings::instance()()
        ->setValue("Window/WindowState", saveState());
    }

    void saveBookmarks() {
      TRC; LOG<<"Saving Bookmarks ...";
      QStringList urls;
      if (qobject_cast<QComboBox*>(_url))
        for (int i(0); i<qobject_cast<QComboBox*>(_url)->count(); ++i)
          urls<<qobject_cast<QComboBox*>(_url)->itemText(i);
      urls.removeAll("");
      urls.removeDuplicates();
      if (_bookmarkfile.writeable()) {
        LOG<<"write to bookmark file"<<_bookmarkfile.name();
        _bookmarkfile.write(urls);
      } else if (qbrowserlib::Settings::instance()()) {
        LOG<<"write to settings";
        qbrowserlib::Settings::instance()()->setValue("Window/Urls", urls);
      } else LOG<<"bookmarks not saved";
    }

    void loadWin() {
      TRC;
      loadBookmarks();
      if (!qbrowserlib::Settings::instance()()) return;
      QStringList tabs(qbrowserlib::Settings::instance()()->value("Window/Tabs").toStringList());
      int oldCnt(_tabs->count());
      for (QStringList::iterator it(tabs.begin()); it!=tabs.end(); ++it) {
        LOG<<"-------------------------------------------------------------";
        load(*it, newTab());
        LOG<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
      }
      for (int i(0); i<oldCnt && _tabs->count()>1; ++i) _tabs->removeTab(0);
      _tabs->setTabsClosable(_tabs->count()>1);
      //_tabs->tabBar()->setShown(_tabs->count()>1);
      _tabs->setCurrentIndex(qbrowserlib::Settings::instance()()
                             ->value("Window/CurrentTab").toInt());
      restoreGeometry(qbrowserlib::Settings::instance()()
                      ->value("Window/Geometry").toByteArray());
      restoreState(qbrowserlib::Settings::instance()()
                   ->value("Window/WindowState").toByteArray());
    }

    void loadBookmarks() {
      TRC; LOG<<"Loading Bookmarks ...";
      QStringList urls;
      if (_bookmarkfile.readable()) {
        LOG<<"load from file"<<_bookmarkfile.name();
        urls = _bookmarkfile.read();
      } else if (qbrowserlib::Settings::instance()()) {
        LOG<<"load from settings";
        urls = qbrowserlib::Settings::instance()()
          ->value("Window/Urls").toStringList();
      }
      urls.removeAll("");
      urls.removeDuplicates();
      if (qobject_cast<QComboBox*>(_url)) {
        QString ci(qobject_cast<QComboBox*>(_url)->currentText());
        qobject_cast<QComboBox*>(_url)->clear();
        qobject_cast<QComboBox*>(_url)->addItems(urls);
        qobject_cast<QComboBox*>(_url)->lineEdit()->setText(ci);
      }
      if (_bookmarks) {
        _bookmarks->clear();
        for (QStringList::iterator it(urls.begin()); it!=urls.end(); ++it) {
          QAction* a(_bookmarks->addAction(*it));
          a->setData(*it);
          connect(a, SIGNAL(triggered(bool)), SLOT(loadFromHistory()));
        }
      }
    }
    
    //@name                                                       QWebView slots
    //@{
    
    void urlChanged(const QUrl& url) {
      TRC; LOG<<url.toString();
      if (sender()!=_tabs->currentWidget()) return;
      LOG<<"signal on current tab";
      if (qobject_cast<QComboBox*>(_url)) {
        qobject_cast<QComboBox*>(_url)->setEditText(url.toString());
        qobject_cast<QComboBox*>(_url)->lineEdit()->setCursorPosition(0);
      } else {
        qobject_cast<QLineEdit*>(_url)->setText(url.toString());
        qobject_cast<QLineEdit*>(_url)->setCursorPosition(0);
      }
    }
    
    void linkClicked(const QUrl& url) {
      TRC; LOG<<url.toString();
      load(url, qobject_cast<QWebView*>(sender()));
    }
    
    void iconChanged() {
      TRC;
      QWebView* view(qobject_cast<QWebView*>(sender()));
      int index = _tabs->indexOf(view);
      if (index<0) return;
      LOG<<"get icon for "<<view->url();
      QIcon icon(QWebSettings::iconForUrl(view->url()));
      if (!icon.isNull()) return _tabs->setTabIcon(index, icon);
      LOG<<"icon for url is null"<<view->url();
      _tabs->setTabIcon(index, QWebSettings::webGraphic
                        (QWebSettings::DefaultFrameIconGraphic));
    }
    
    void titleChanged(const QString& text) {
      TRC; LOG<<text;
      _tabs->setTabText(_tabs->indexOf(qobject_cast<QWidget*>(sender())),
                        trUtf8("%1").arg(text));
      resetWindowTitle();
    }

    void resetWindowTitle() {
      if (qobject_cast<QWebView*>(_tabs->currentWidget()))
        setWindowTitle(trUtf8("[%1] - %2 - SwissSurfer (%3)",
                              "main window title "
                              "%1 = host name "
                              "%2 = page title "
                              "%3 = number of open tabs")
                       .arg((qobject_cast<QWebView*>(_tabs->currentWidget())
                            ->url().host()))
                       .arg((qobject_cast<QWebView*>(_tabs->currentWidget())
                            ->title()))
                       .arg(_tabs->count()));
      else
        setWindowTitle(trUtf8("SwissSurfer", "window title with no tab"));
    }
    
    void statusBarMessage(const QString& text) {
      TRC; LOG<<text;
      if (text.size()) statusBar()->showMessage(tr("Info: %1").arg(text));
    }
    
    void loadProgress(int i) {
      TRC; LOG<<i;
    }
    
    void loadStarted() {
      TRC;
    }
    
    void loadFinished(bool ok) {
      TRC; LOG<<(ok?"success":"error");
      QSize sz(qobject_cast<QWebView*>(_tabs->currentWidget())->size());
      qobject_cast<QWebView*>(_tabs->currentWidget())->resize(0,0);
      qobject_cast<QWebView*>(_tabs->currentWidget())->resize(sz);
      if (ok) {
        statusBar()->showMessage(tr("done."));
        goodUrl();
      }
      activateTab();
    }

    //@}

    //@name                                             QWebPage WebAction slots
    //@{
    
    void triggeredOpenLink(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredOpenLinkInNewWindow(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredOpenFrameInNewWindow(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredDownloadLinkToDisk(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredCopyLinkToClipboard(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredOpenImageInNewWindow(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredDownloadImageToDisk(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredCopyImageToClipboard(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredBack(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredForward(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredStop(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredReload(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredCut(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredCopy(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredPaste(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredUndo(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredRedo(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToNextChar(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToPreviousChar(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToNextWord(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToPreviousWord(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToNextLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToPreviousLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToStartOfLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToEndOfLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToStartOfBlock(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToEndOfBlock(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToStartOfDocument(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredMoveToEndOfDocument(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectNextChar(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectPreviousChar(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectNextWord(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectPreviousWord(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectNextLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectPreviousLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectStartOfLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectEndOfLine(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectStartOfBlock(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectEndOfBlock(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectStartOfDocument(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectEndOfDocument(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredDeleteStartOfWord(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredDeleteEndOfWord(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSetTextDirectionDefault(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSetTextDirectionLeftToRight(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSetTextDirectionRightToLeft(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredToggleBold(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredToggleItalic(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredToggleUnderline(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredInspectElement(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredInsertParagraphSeparator(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredInsertLineSeparator(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    void triggeredSelectAll(bool) {
      TRC; LOG<<qobject_cast<QAction*>(sender())->data().toString();
    }
    
    //@}

    //@name                                                       QWebPage slots
    //@{
    
    void contentsChanged() {
      TRC;
    }
    
    void databaseQuotaExceeded(QWebFrame* frame, QString databaseName) {
      TRC; LOG<<databaseName;
    }
    
    void downloadRequested(const QNetworkRequest& request) {
      TRC;
    }
    
    void frameCreated(QWebFrame* frame) {
      TRC;
    }
    
    void geometryChangeRequested(const QRect& geom) {
      TRC;
    }
    
//     void linkClicked(const QUrl& url) {
//       TRC; LOG<<url.toString();
//     }
    
    void linkHovered(const QString& link, const QString& title,
                     const QString& textContent) {
      TRC; LOG<<link<<title<<textContent;
      statusBar()->showMessage(tr("%1", "statusbar for hovered link %1=url")
                               .arg(link));
    }
    
//     void loadFinished(bool ok) {
//       TRC; LOG<<(ok?"succeess":"error");
//     }
    
//     void loadProgress(int progress) {
//       TRC; LOG<<progress;
//     }
    
//     void loadStarted() {
//       TRC;
//     }
    
    void menuBarVisibilityChangeRequested(bool visible) {
      TRC;
    }
    
    void microFocusChanged() {
      TRC;
    }
    
    void printRequested(QWebFrame* frame) {
      TRC;
    }
    
    void repaintRequested(const QRect& dirtyRect) {
      //TRC;
    }
    
    void restoreFrameStateRequested(QWebFrame* frame) {
      TRC;
    }
    
    void saveFrameStateRequested(QWebFrame* frame, QWebHistoryItem* item) {
      TRC;
    }
    
    void scrollRequested(int dx, int dy, const QRect& rectToScroll) {
      TRC;
    }
    
    void selectionChanged() {
      TRC;
    }
    
//     void statusBarMessage(const QString& text) {
//       TRC;
//     }
    
    void statusBarVisibilityChangeRequested(bool visible) {
      TRC;
    }
    
    void toolBarVisibilityChangeRequested(bool visible) {
      TRC;
    }

    //@name                                                     handle downloads
    //@{
    
    void unsupportedContent(QNetworkReply* reply) {
      TRC; LOG<<reply->header(QNetworkRequest::ContentTypeHeader).toString();
      LOG<<"Status:"<<_downloadManager->networkError(reply->error());
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
      QList<QNetworkReply::RawHeaderPair> rh(reply->rawHeaderPairs());
      for(QList<QNetworkReply::RawHeaderPair>::iterator it(rh.begin());
          it!=rh.end(); ++it) {
        LOG<<"RawHeader:"<<it->first<<it->second;
      }
#endif
      handleContent(reply);
    }

    //! check and handle content that is defined in settings
    void metaDataChanged(QNetworkReply* reply) {
      TRC;
      QString filename
        (QString::fromUtf8(reply->rawHeader("Content-Disposition")));
      if (filename.contains
          (QRegExp("^\\s*attachment\\s*;\\s*filename\\s*=\\s*\"[^\"]+\""))) {
        LOG<<"From Content-Disposition";
        filename = filename.replace
          (QRegExp("^\\s*attachment\\s*;\\s*filename\\s*=\\s*\"([^\"]+)\".*"),
           "\\1");
      } else {
        LOG<<"From path";
        filename =
          QFileInfo(!reply->url().toLocalFile().isEmpty()
                    ?reply->url().toLocalFile()
                    :reply->url().path()).fileName();
      }
      LOG<<"Filename:"<<filename;
      QStringList type
        (qbrowserlib::Settings::instance().mimetype
         (reply->header(QNetworkRequest::ContentTypeHeader).toString(),
          filename));
    }

    void handleContent(QNetworkReply* reply) {
      TRC; LOG<<"Location:"<<reply->header(QNetworkRequest::LocationHeader)
        .toString();
      LOG<<"Content-Type:"<<reply->header(QNetworkRequest::ContentTypeHeader)
        .toString();
      LOG<<"Content-Disposition:"<<reply->rawHeader("Content-Disposition");
      LOG<<"Status:"<<_downloadManager->networkError(reply->error());
      LOG<<"URL:"<<reply->url().toString();
      LOG<<"File:"<<reply->url().toLocalFile();
      LOG<<"Path:"<<reply->url().path();
      if (reply->error()!=QNetworkReply::NoError) {
        LOG<<"Error:"<<_downloadManager->networkError(reply->error());
        return;
      }
      statusBar()->showMessage(tr("handling content ..."), 5000);
      LOG<<"Show SaveOrRunDialog";
      qbrowserlib::SaveOrRunDialog dlg
        (reply, reply->header(QNetworkRequest::ContentTypeHeader).toString(),
         reply->url().host(), _kiosk, this);
      if (!dlg.handlePreconfigured()) dlg.exec();
    }

    //@}
    
    void windowCloseRequested() {
      TRC;
    }
    
    //@}

    //@name                                              DownloadManager signals
    //@{

    void progress(qint64 done, qint64 total) {
      _progress->setMaximum(total);
      _progress->setValue(done);
    }

    void started() {
      actionStop->setVisible(true);
      actionReload->setVisible(false);
      _progress->setRange(0, 0);
      _progress->setValue(0);
      _progress->setEnabled(true);
      _progress->show();
    }
    
    void finished() {
      TRC;
      actionStop->setVisible(false);
      actionReload->setVisible(true);
      _progress->setRange(0, 1);
      _progress->setValue(1);
      _progress->setEnabled(false);
    }

    void finished(QNetworkReply *r) {
      TRC;
      if (r->error()!=QNetworkReply::NoError &&
          r->error()!=QNetworkReply::OperationCanceledError) {
        statusBar()->showMessage
          (qbrowserlib::DownloadManager::networkError(r->error()));
        badUrl();
        if (!_showErrorLog) {
          statusBar()->addPermanentWidget
            ((_showErrorLog = new QPushButton(QIcon(":/icons/error"),
                                              tr("errors", "show error log"))));
          assert(connect(_showErrorLog, SIGNAL(clicked(bool)),
                         actionErrorLog, SLOT(trigger())));
        }
        _showErrorLog->show();
      }
    }

    void errorReset() {
      TRC;
      _showErrorLog->hide();
    }

    void badUrl() {
      if (qobject_cast<QComboBox*>(_url))
        dynamic_cast<qbrowserlib::ButtonLineEdit*>
          (qobject_cast<QComboBox*>(_url)->lineEdit())
          ->changeStyleSheet(tr("background-color: #F77", "invalid url"));
      else
        qobject_cast<QLineEdit*>(_url)
          ->setStyleSheet(tr("background-color: #F77", "invalid url"));
    }

    void goodUrl() {
      if (qobject_cast<QComboBox*>(_url))
        dynamic_cast<qbrowserlib::ButtonLineEdit*>
          (qobject_cast<QComboBox*>(_url)->lineEdit())
          ->changeStyleSheet(tr("background-color: white", "valid url"));
      else
        qobject_cast<QLineEdit*>(_url)
          ->setStyleSheet(tr("background-color: white", "valid url"));
    }

    void downloadError(QString error) {
      qbrowserlib::ErrorLog::instance().append(error);
    }

    //@}
    
    //@name                                        QNetworkAccessManager signals
    //@{

    void authenticationRequired(QNetworkReply* reply,
                                QAuthenticator* authenticator) {
      TRC; LOG<<reply->url().toString()<<authenticator->realm();
      statusBar()->showMessage(tr("authentication required"));
      Authentication auth(authenticator, this);
      if (auth.exec()==QDialog::Rejected) reply->abort();
    }
    
    void sslErrors(QNetworkReply* reply, const QList<QSslError>& errors) {
      TRC;
      statusBar()->showMessage(tr("ssl error"));
    }
    
    //@}

  private:

    void activateTab() {
      iconChanged();
      QWebHistory* history(qobject_cast<QWebView*>(_tabs->currentWidget())
                          ->history());
      actionForward->setEnabled(history->canGoForward());
      actionBack->setEnabled(history->canGoBack());
      _history->clear();
      QList<QWebHistoryItem> items(history->items());
      for (int i = 0; i < items.size(); ++i) {
        QAction* a(_history->addAction(items[i].title()));
        a->setData(items[i].url());
        connect(a, SIGNAL(triggered(bool)), SLOT(loadFromHistory()));
      }
    }

  private:
    
    QWidget* _url;
    qbrowserlib::ButtonLineEdit* _find;
    QSlider* _zoom;
    QProgressBar* _progress;
    QString _home;
    bool _kiosk;
    QPrinter _printer;
    SslClientAuthNetworkAccessManager _networkManager;
    QSharedPointer<qbrowserlib::DownloadManager> _downloadManager;
    LoginCertificate _logincertificate;
    gui::Proxy _proxy;
    QPushButton* _showErrorLog;
    std::auto_ptr<EditBookmarks> _editbookmarks;
    bool _startUrl;
    bool _quirks;
    qbrowserlib::ButtonLineEdit* _search;
    QComboBox* _searchEngines;
    FileStorage _bookmarkfile;
};

//! @}
#endif
