PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/WebKitTools/QtLauncher/webpage.cpp

https://github.com/mikezit/Webkit_Code
C++ | 111 lines | 63 code | 17 blank | 31 comment | 9 complexity | 0441ac1f4da00d2b6a60bb8acf795202 MD5 | raw file
  1. /*
  2. * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies)
  3. * Copyright (C) 2009 Girish Ramakrishnan <girish@forwardbias.in>
  4. * Copyright (C) 2006 George Staikos <staikos@kde.org>
  5. * Copyright (C) 2006 Dirk Mueller <mueller@kde.org>
  6. * Copyright (C) 2006 Zack Rusin <zack@kde.org>
  7. * Copyright (C) 2006 Simon Hausmann <hausmann@kde.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  28. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "webpage.h"
  33. #include <QDesktopServices>
  34. #include <QtGui>
  35. #include <QtNetwork/QNetworkRequest>
  36. #include <QtNetwork/QNetworkProxy>
  37. WebPage::WebPage(QObject* parent)
  38. : QWebPage(parent)
  39. {
  40. applyProxy();
  41. }
  42. void WebPage::applyProxy()
  43. {
  44. QUrl proxyUrl(qgetenv("http_proxy"));
  45. if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
  46. int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
  47. networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort));
  48. }
  49. }
  50. bool WebPage::supportsExtension(QWebPage::Extension extension) const
  51. {
  52. if (extension == QWebPage::ErrorPageExtension)
  53. return true;
  54. return false;
  55. }
  56. bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
  57. {
  58. const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
  59. QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
  60. errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
  61. .arg(info->errorString).toUtf8();
  62. return true;
  63. }
  64. bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
  65. {
  66. QObject* view = parent();
  67. QVariant value = view->property("keyboardModifiers");
  68. if (!value.isNull()) {
  69. Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
  70. if (modifiers & Qt::ShiftModifier) {
  71. QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
  72. page->mainFrame()->load(request);
  73. return false;
  74. }
  75. if (modifiers & Qt::AltModifier) {
  76. openUrlInDefaultBrowser(request.url());
  77. return false;
  78. }
  79. }
  80. return QWebPage::acceptNavigationRequest(frame, request, type);
  81. }
  82. void WebPage::openUrlInDefaultBrowser(const QUrl& url)
  83. {
  84. if (QAction* action = qobject_cast<QAction*>(sender()))
  85. QDesktopServices::openUrl(action->data().toUrl());
  86. else
  87. QDesktopServices::openUrl(url);
  88. }
  89. QString WebPage::userAgentForUrl(const QUrl& url) const
  90. {
  91. if (!m_userAgent.isEmpty())
  92. return m_userAgent;
  93. return QWebPage::userAgentForUrl(url);
  94. }