diff options
Diffstat (limited to 'WebKit/qt/QtLauncher/main.cpp')
| -rw-r--r-- | WebKit/qt/QtLauncher/main.cpp | 353 |
1 files changed, 267 insertions, 86 deletions
diff --git a/WebKit/qt/QtLauncher/main.cpp b/WebKit/qt/QtLauncher/main.cpp index 9cbab53..2286712 100644 --- a/WebKit/qt/QtLauncher/main.cpp +++ b/WebKit/qt/QtLauncher/main.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) + * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in> * Copyright (C) 2006 George Staikos <staikos@kde.org> * Copyright (C) 2006 Dirk Mueller <mueller@kde.org> * Copyright (C) 2006 Zack Rusin <zack@kde.org> @@ -29,16 +30,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include <qwebpage.h> -#include <qwebview.h> -#include <qwebframe.h> -#include <qwebsettings.h> -#include <qwebelement.h> - #include <QtGui> -#include <QDebug> #include <QtNetwork/QNetworkProxy> -#if QT_VERSION >= 0x040400 && !defined(QT_NO_PRINTER) +#include <QtNetwork/QNetworkRequest> +#if !defined(QT_NO_PRINTER) #include <QPrintPreviewDialog> #endif @@ -46,37 +41,143 @@ #include <QtUiTools/QUiLoader> #endif -#include <QVector> -#include <QTextStream> +#include <QDebug> #include <QFile> +#include <QTextStream> +#include <QVector> + #include <cstdio> +#include <qwebelement.h> +#include <qwebframe.h> +#include <qwebinspector.h> +#include <qwebpage.h> +#include <qwebsettings.h> +#include <qwebview.h> + #ifndef NDEBUG void QWEBKIT_EXPORT qt_drt_garbageCollector_collect(); #endif -class WebPage : public QWebPage +static QUrl urlFromUserInput(const QString& input) { +#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0) + return QUrl::fromUserInput(input); +#else + return QUrl(input); +#endif +} + +class WebView : public QWebView { + Q_OBJECT +public: + WebView(QWidget* parent) : QWebView(parent) {} + +protected: + virtual void contextMenuEvent(QContextMenuEvent* event) + { + QMenu* menu = page()->createStandardContextMenu(); + + QWebHitTestResult r = page()->mainFrame()->hitTestContent(event->pos()); + + if (!r.linkUrl().isEmpty()) { + QAction* newTabAction = menu->addAction(tr("Open in Default &Browser"), this, SLOT(openUrlInDefaultBrowser())); + newTabAction->setData(r.linkUrl()); + menu->insertAction(menu->actions().at(2), newTabAction); + } + + menu->exec(mapToGlobal(event->pos())); + delete menu; + } + + virtual void mousePressEvent(QMouseEvent* event) + { + mouseButtons = event->buttons(); + keyboardModifiers = event->modifiers(); + + QWebView::mousePressEvent(event); + } + +public slots: + void openUrlInDefaultBrowser(const QUrl &url = QUrl()) + { + if (QAction* action = qobject_cast<QAction*>(sender())) + QDesktopServices::openUrl(action->data().toUrl()); + else + QDesktopServices::openUrl(url); + } + +public: + Qt::MouseButtons mouseButtons; + Qt::KeyboardModifiers keyboardModifiers; +}; + +class WebPage : public QWebPage { public: WebPage(QWidget *parent) : QWebPage(parent) {} virtual QWebPage *createWindow(QWebPage::WebWindowType); virtual QObject* createPlugin(const QString&, const QUrl&, const QStringList&, const QStringList&); + virtual bool supportsExtension(QWebPage::Extension extension) const + { + if (extension == QWebPage::ErrorPageExtension) + return true; + return false; + } + virtual bool extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output); + + + virtual bool acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest &request, NavigationType type) + { + WebView* webView = static_cast<WebView*>(view()); + if (webView->keyboardModifiers & Qt::ShiftModifier) { + QWebPage* page = createWindow(QWebPage::WebBrowserWindow); + page->mainFrame()->load(request); + return false; + } + if (webView->keyboardModifiers & Qt::AltModifier) { + webView->openUrlInDefaultBrowser(request.url()); + return false; + } + + return QWebPage::acceptNavigationRequest(frame, request, type); + } }; -class MainWindow : public QMainWindow -{ +class WebInspector : public QWebInspector { Q_OBJECT public: - MainWindow(QString url = QString()): currentZoom(100) { + WebInspector(QWidget* parent) : QWebInspector(parent) {} +signals: + void visibleChanged(bool nowVisible); +protected: + void showEvent(QShowEvent* event) + { + QWebInspector::showEvent(event); + emit visibleChanged(true); + } + void hideEvent(QHideEvent* event) + { + QWebInspector::hideEvent(event); + emit visibleChanged(false); + } +}; + +class MainWindow : public QMainWindow { + Q_OBJECT +public: + MainWindow(QString url = QString()): currentZoom(100) + { setAttribute(Qt::WA_DeleteOnClose); + if (qgetenv("QTLAUNCHER_USE_ARGB_VISUALS").toInt() == 1) + setAttribute(Qt::WA_TranslucentBackground); - view = new QWebView(this); - setCentralWidget(view); + QSplitter* splitter = new QSplitter(Qt::Vertical, this); + setCentralWidget(splitter); + view = new WebView(splitter); WebPage* page = new WebPage(view); view->setPage(page); - connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished())); connect(view, SIGNAL(titleChanged(const QString&)), @@ -85,10 +186,16 @@ public: this, SLOT(showLinkHover(const QString&, const QString&))); connect(view->page(), SIGNAL(windowCloseRequested()), this, SLOT(close())); + inspector = new WebInspector(splitter); + inspector->setPage(page); + inspector->hide(); + connect(this, SIGNAL(destroyed()), inspector, SLOT(deleteLater())); + setupUI(); - // set the proxy to the http_proxy env variable - if present - QUrl proxyUrl = view->guessUrlFromString(qgetenv("http_proxy")); + // set the proxy to the http_proxy env variable - if present + QUrl proxyUrl = urlFromUserInput(qgetenv("http_proxy")); + if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) { int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080; page->networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort)); @@ -98,39 +205,47 @@ public: if (fi.exists() && fi.isRelative()) url = fi.absoluteFilePath(); - QUrl qurl = view->guessUrlFromString(url); + QUrl qurl = urlFromUserInput(url); + if (qurl.scheme().isEmpty()) + qurl = QUrl("http://" + url + "/"); if (qurl.isValid()) { urlEdit->setText(qurl.toEncoded()); view->load(qurl); - - // the zoom values are chosen to be like in Mozilla Firefox 3 - zoomLevels << 30 << 50 << 67 << 80 << 90; - zoomLevels << 100; - zoomLevels << 110 << 120 << 133 << 150 << 170 << 200 << 240 << 300; } + + // the zoom values are chosen to be like in Mozilla Firefox 3 + zoomLevels << 30 << 50 << 67 << 80 << 90; + zoomLevels << 100; + zoomLevels << 110 << 120 << 133 << 150 << 170 << 200 << 240 << 300; } - QWebPage* webPage() const { + QWebPage* webPage() const + { return view->page(); } - QWebView* webView() const { + QWebView* webView() const + { return view; } protected slots: - void changeLocation() { + void changeLocation() + { QString string = urlEdit->text(); - QUrl url = view->guessUrlFromString(string); - if (!url.isValid()) + QUrl url = urlFromUserInput(string); + if (url.scheme().isEmpty()) url = QUrl("http://" + string + "/"); - urlEdit->setText(url.toEncoded()); - view->load(url); - view->setFocus(Qt::OtherFocusReason); + if (url.isValid()) { + urlEdit->setText(url.toEncoded()); + view->load(url); + view->setFocus(Qt::OtherFocusReason); + } } - void loadFinished() { + void loadFinished() + { urlEdit->setText(view->url().toString()); QUrl::FormattingOptions opts; @@ -147,7 +262,8 @@ protected slots: urlModel.setStringList(urlList); } - void showLinkHover(const QString &link, const QString &toolTip) { + void showLinkHover(const QString &link, const QString &toolTip) + { statusBar()->showMessage(link); #ifndef QT_NO_TOOLTIP if (!toolTip.isEmpty()) @@ -155,22 +271,24 @@ protected slots: #endif } - void zoomIn() { + void zoomIn() + { int i = zoomLevels.indexOf(currentZoom); Q_ASSERT(i >= 0); if (i < zoomLevels.count() - 1) currentZoom = zoomLevels[i + 1]; - view->setZoomFactor(qreal(currentZoom)/100.0); + view->setZoomFactor(qreal(currentZoom) / 100.0); } - void zoomOut() { + void zoomOut() + { int i = zoomLevels.indexOf(currentZoom); Q_ASSERT(i >= 0); if (i > 0) currentZoom = zoomLevels[i - 1]; - view->setZoomFactor(qreal(currentZoom)/100.0); + view->setZoomFactor(qreal(currentZoom) / 100.0); } void resetZoom() @@ -184,8 +302,9 @@ protected slots: view->page()->settings()->setAttribute(QWebSettings::ZoomTextOnly, b); } - void print() { -#if QT_VERSION >= 0x040400 && !defined(QT_NO_PRINTER) + void print() + { +#if !defined(QT_NO_PRINTER) QPrintPreviewDialog dlg(this); connect(&dlg, SIGNAL(paintRequested(QPrinter *)), view, SLOT(print(QPrinter *))); @@ -193,21 +312,53 @@ protected slots: #endif } - void setEditable(bool on) { + void screenshot() + { + QPixmap pixmap = QPixmap::grabWidget(view); + QLabel* label = new QLabel; + label->setAttribute(Qt::WA_DeleteOnClose); + label->setWindowTitle("Screenshot - Preview"); + label->setPixmap(pixmap); + label->show(); + + QString fileName = QFileDialog::getSaveFileName(label, "Screenshot"); + if (!fileName.isEmpty()) { + pixmap.save(fileName, "png"); + label->setWindowTitle(QString("Screenshot - Saved at %1").arg(fileName)); + } + } + + void setEditable(bool on) + { view->page()->setContentEditable(on); formatMenuAction->setVisible(on); } - void dumpHtml() { + /* + void dumpPlugins() { + QList<QWebPluginInfo> plugins = QWebSettings::pluginDatabase()->plugins(); + foreach (const QWebPluginInfo plugin, plugins) { + qDebug() << "Plugin:" << plugin.name(); + foreach (const QWebPluginInfo::MimeType mime, plugin.mimeTypes()) { + qDebug() << " " << mime.name; + } + } + } + */ + + void dumpHtml() + { qDebug() << "HTML: " << view->page()->mainFrame()->toHtml(); } - void selectElements() { + void selectElements() + { bool ok; QString str = QInputDialog::getText(this, "Select elements", "Choose elements", QLineEdit::Normal, "a", &ok); + if (ok && !str.isEmpty()) { - QList<QWebElement> result = view->page()->mainFrame()->findAllElements(str); + QWebElementCollection result = view->page()->mainFrame()->findAllElements(str); foreach (QWebElement e, result) e.setStyleProperty("background-color", "yellow"); statusBar()->showMessage(QString("%1 element(s) selected").arg(result.count()), 5000); @@ -216,8 +367,9 @@ protected slots: public slots: - void newWindow(const QString &url = QString()) { - MainWindow *mw = new MainWindow(url); + void newWindow(const QString &url = QString()) + { + MainWindow* mw = new MainWindow(url); mw->show(); } @@ -227,7 +379,8 @@ private: int currentZoom; // create the status bar, tool bar & menu - void setupUI() { + void setupUI() + { progress = new QProgressBar(this); progress->setRange(0, 100); progress->setMinimumSize(100, 20); @@ -243,25 +396,24 @@ private: urlEdit->setSizePolicy(QSizePolicy::Expanding, urlEdit->sizePolicy().verticalPolicy()); connect(urlEdit, SIGNAL(returnPressed()), SLOT(changeLocation())); - QCompleter *completer = new QCompleter(this); + QCompleter* completer = new QCompleter(this); urlEdit->setCompleter(completer); completer->setModel(&urlModel); - QToolBar *bar = addToolBar("Navigation"); + QToolBar* bar = addToolBar("Navigation"); bar->addAction(view->pageAction(QWebPage::Back)); bar->addAction(view->pageAction(QWebPage::Forward)); bar->addAction(view->pageAction(QWebPage::Reload)); bar->addAction(view->pageAction(QWebPage::Stop)); bar->addWidget(urlEdit); - QMenu *fileMenu = menuBar()->addMenu("&File"); - QAction *newWindow = fileMenu->addAction("New Window", this, SLOT(newWindow())); -#if QT_VERSION >= 0x040400 - fileMenu->addAction(tr("Print"), this, SLOT(print())); -#endif + QMenu* fileMenu = menuBar()->addMenu("&File"); + QAction* newWindow = fileMenu->addAction("New Window", this, SLOT(newWindow())); + fileMenu->addAction(tr("Print"), this, SLOT(print()), QKeySequence::Print); + QAction* screenshot = fileMenu->addAction("Screenshot", this, SLOT(screenshot())); fileMenu->addAction("Close", this, SLOT(close())); - QMenu *editMenu = menuBar()->addMenu("&Edit"); + QMenu* editMenu = menuBar()->addMenu("&Edit"); editMenu->addAction(view->pageAction(QWebPage::Undo)); editMenu->addAction(view->pageAction(QWebPage::Redo)); editMenu->addSeparator(); @@ -269,34 +421,36 @@ private: editMenu->addAction(view->pageAction(QWebPage::Copy)); editMenu->addAction(view->pageAction(QWebPage::Paste)); editMenu->addSeparator(); - QAction *setEditable = editMenu->addAction("Set Editable", this, SLOT(setEditable(bool))); + QAction* setEditable = editMenu->addAction("Set Editable", this, SLOT(setEditable(bool))); setEditable->setCheckable(true); - QMenu *viewMenu = menuBar()->addMenu("&View"); + QMenu* viewMenu = menuBar()->addMenu("&View"); viewMenu->addAction(view->pageAction(QWebPage::Stop)); viewMenu->addAction(view->pageAction(QWebPage::Reload)); viewMenu->addSeparator(); - QAction *zoomIn = viewMenu->addAction("Zoom &In", this, SLOT(zoomIn())); - QAction *zoomOut = viewMenu->addAction("Zoom &Out", this, SLOT(zoomOut())); - QAction *resetZoom = viewMenu->addAction("Reset Zoom", this, SLOT(resetZoom())); - QAction *zoomTextOnly = viewMenu->addAction("Zoom Text Only", this, SLOT(toggleZoomTextOnly(bool))); + QAction* zoomIn = viewMenu->addAction("Zoom &In", this, SLOT(zoomIn())); + QAction* zoomOut = viewMenu->addAction("Zoom &Out", this, SLOT(zoomOut())); + QAction* resetZoom = viewMenu->addAction("Reset Zoom", this, SLOT(resetZoom())); + QAction* zoomTextOnly = viewMenu->addAction("Zoom Text Only", this, SLOT(toggleZoomTextOnly(bool))); zoomTextOnly->setCheckable(true); zoomTextOnly->setChecked(false); viewMenu->addSeparator(); viewMenu->addAction("Dump HTML", this, SLOT(dumpHtml())); + //viewMenu->addAction("Dump plugins", this, SLOT(dumpPlugins())); - QMenu *formatMenu = new QMenu("F&ormat"); + QMenu* formatMenu = new QMenu("F&ormat", this); formatMenuAction = menuBar()->addMenu(formatMenu); formatMenuAction->setVisible(false); formatMenu->addAction(view->pageAction(QWebPage::ToggleBold)); formatMenu->addAction(view->pageAction(QWebPage::ToggleItalic)); formatMenu->addAction(view->pageAction(QWebPage::ToggleUnderline)); - QMenu *writingMenu = formatMenu->addMenu(tr("Writing Direction")); + QMenu* writingMenu = formatMenu->addMenu(tr("Writing Direction")); writingMenu->addAction(view->pageAction(QWebPage::SetTextDirectionDefault)); writingMenu->addAction(view->pageAction(QWebPage::SetTextDirectionLeftToRight)); writingMenu->addAction(view->pageAction(QWebPage::SetTextDirectionRightToLeft)); newWindow->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_N)); + screenshot->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S)); view->pageAction(QWebPage::Back)->setShortcut(QKeySequence::Back); view->pageAction(QWebPage::Stop)->setShortcut(Qt::Key_Escape); view->pageAction(QWebPage::Forward)->setShortcut(QKeySequence::Forward); @@ -313,29 +467,45 @@ private: view->pageAction(QWebPage::ToggleItalic)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I)); view->pageAction(QWebPage::ToggleUnderline)->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_U)); - QMenu *toolsMenu = menuBar()->addMenu("&Tools"); + QMenu* toolsMenu = menuBar()->addMenu("&Tools"); toolsMenu->addAction("Select elements...", this, SLOT(selectElements())); + QAction* showInspectorAction = toolsMenu->addAction("Show inspector", inspector, SLOT(setVisible(bool))); + showInspectorAction->setCheckable(true); + showInspectorAction->setShortcuts(QList<QKeySequence>() << QKeySequence(tr("F12"))); + showInspectorAction->connect(inspector, SIGNAL(visibleChanged(bool)), SLOT(setChecked(bool))); } - QWebView *view; - QLineEdit *urlEdit; - QProgressBar *progress; + QWebView* view; + QLineEdit* urlEdit; + QProgressBar* progress; + WebInspector* inspector; - QAction *formatMenuAction; + QAction* formatMenuAction; QStringList urlList; QStringListModel urlModel; }; -QWebPage *WebPage::createWindow(QWebPage::WebWindowType) +bool WebPage::extension(Extension extension, const ExtensionOption *option, ExtensionReturn *output) +{ + const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option); + QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output); + + errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>") + .arg(info->errorString).toUtf8(); + + return true; +} + +QWebPage* WebPage::createWindow(QWebPage::WebWindowType) { - MainWindow *mw = new MainWindow; + MainWindow* mw = new MainWindow; mw->show(); return mw->webPage(); } -QObject *WebPage::createPlugin(const QString &classId, const QUrl &url, const QStringList ¶mNames, const QStringList ¶mValues) +QObject* WebPage::createPlugin(const QString &classId, const QUrl &url, const QStringList ¶mNames, const QStringList ¶mValues) { Q_UNUSED(url); Q_UNUSED(paramNames); @@ -349,13 +519,13 @@ QObject *WebPage::createPlugin(const QString &classId, const QUrl &url, const QS #endif } -class URLLoader : public QObject -{ +class URLLoader : public QObject { Q_OBJECT public: URLLoader(QWebView* view, const QString& inputFileName) : m_view(view) , m_stdOut(stdout) + , m_loaded(0) { init(inputFileName); } @@ -367,7 +537,7 @@ public slots: if (getUrl(qstr)) { QUrl url(qstr, QUrl::StrictMode); if (url.isValid()) { - m_stdOut << "Loading " << qstr << " ......" << endl; + m_stdOut << "Loading " << qstr << " ......" << ++m_loaded << endl; m_view->load(url); } else loadNext(); @@ -410,6 +580,7 @@ private: int m_index; QWebView* m_view; QTextStream m_stdOut; + int m_loaded; }; #include "main.moc" @@ -429,19 +600,22 @@ int launcherMain(const QApplication& app) int main(int argc, char **argv) { QApplication app(argc, argv); - QString url = QString("%1/%2").arg(QDir::homePath()).arg(QLatin1String("index.html")); + QString defaultUrl = QString("file://%1/%2").arg(QDir::homePath()).arg(QLatin1String("index.html")); QWebSettings::setMaximumPagesInCache(4); app.setApplicationName("QtLauncher"); -#if QT_VERSION >= 0x040400 app.setApplicationVersion("0.1"); -#endif QWebSettings::setObjectCacheCapacities((16*1024*1024) / 8, (16*1024*1024) / 8, 16*1024*1024); QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true); QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); + QWebSettings::enablePersistentStorage(); + + // To allow QWebInspector's configuration persistence + QCoreApplication::setOrganizationName("Nokia"); + QCoreApplication::setApplicationName("QtLauncher"); const QStringList args = app.arguments(); @@ -453,21 +627,28 @@ int main(int argc, char **argv) exit(0); } MainWindow* window = new MainWindow; - QWebView *view = window->webView(); + QWebView* view = window->webView(); URLLoader loader(view, listFile); QObject::connect(view, SIGNAL(loadFinished(bool)), &loader, SLOT(loadNext())); loader.loadNext(); window->show(); launcherMain(app); } else { - if (args.count() > 1) - url = args.at(1); - - MainWindow* window = new MainWindow(url); + MainWindow* window = 0; + + // Look though the args for something we can open + for (int i = 1; i < args.count(); i++) { + if (!args.at(i).startsWith("-")) { + if (!window) + window = new MainWindow(args.at(i)); + else + window->newWindow(args.at(i)); + } + } - // Opens every given urls in new windows - for (int i = 2; i < args.count(); i++) - window->newWindow(args.at(i)); + // If not, just open the default URL + if (!window) + window = new MainWindow(defaultUrl); window->show(); launcherMain(app); |
