webpage.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 "launcherwindow.h"
  34. #include <QAction>
  35. #include <QApplication>
  36. #include <QAuthenticator>
  37. #ifndef QT_NO_DESKTOPSERVICES
  38. #include <QDesktopServices>
  39. #endif
  40. #include <QDialog>
  41. #include <QDialogButtonBox>
  42. #include <QLabel>
  43. #include <QLayout>
  44. #ifndef QT_NO_LINEEDIT
  45. #include <QLineEdit>
  46. #endif
  47. #include <QProgressBar>
  48. #include <QtNetwork/QNetworkReply>
  49. #include <QtNetwork/QNetworkRequest>
  50. #include <QtNetwork/QNetworkProxy>
  51. WebPage::WebPage(QObject* parent)
  52. : QWebPage(parent)
  53. , m_userAgent()
  54. , m_interruptingJavaScriptEnabled(false)
  55. {
  56. applyProxy();
  57. connect(networkAccessManager(), SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),
  58. this, SLOT(authenticationRequired(QNetworkReply*, QAuthenticator*)));
  59. connect(this, SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)), this, SLOT(requestPermission(QWebFrame*, QWebPage::Feature)));
  60. connect(this, SIGNAL(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)), this, SLOT(featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)));
  61. }
  62. void WebPage::applyProxy()
  63. {
  64. QUrl proxyUrl(qgetenv("http_proxy"));
  65. if (proxyUrl.isValid() && !proxyUrl.host().isEmpty()) {
  66. int proxyPort = (proxyUrl.port() > 0) ? proxyUrl.port() : 8080;
  67. networkAccessManager()->setProxy(QNetworkProxy(QNetworkProxy::HttpProxy, proxyUrl.host(), proxyPort));
  68. }
  69. }
  70. bool WebPage::supportsExtension(QWebPage::Extension extension) const
  71. {
  72. if (extension == QWebPage::ErrorPageExtension)
  73. return true;
  74. return false;
  75. }
  76. bool WebPage::extension(Extension extension, const ExtensionOption* option, ExtensionReturn* output)
  77. {
  78. const QWebPage::ErrorPageExtensionOption* info = static_cast<const QWebPage::ErrorPageExtensionOption*>(option);
  79. QWebPage::ErrorPageExtensionReturn* errorPage = static_cast<QWebPage::ErrorPageExtensionReturn*>(output);
  80. errorPage->content = QString("<html><head><title>Failed loading page</title></head><body>%1</body></html>")
  81. .arg(info->errorString).toUtf8();
  82. return true;
  83. }
  84. bool WebPage::acceptNavigationRequest(QWebFrame* frame, const QNetworkRequest& request, NavigationType type)
  85. {
  86. QObject* view = parent();
  87. QVariant value = view->property("keyboardModifiers");
  88. if (!value.isNull()) {
  89. Qt::KeyboardModifiers modifiers = Qt::KeyboardModifiers(value.toInt());
  90. if (modifiers & Qt::ShiftModifier) {
  91. QWebPage* page = createWindow(QWebPage::WebBrowserWindow);
  92. page->mainFrame()->load(request);
  93. return false;
  94. }
  95. if (modifiers & Qt::AltModifier) {
  96. openUrlInDefaultBrowser(request.url());
  97. return false;
  98. }
  99. }
  100. return QWebPage::acceptNavigationRequest(frame, request, type);
  101. }
  102. void WebPage::openUrlInDefaultBrowser(const QUrl& url)
  103. {
  104. #ifndef QT_NO_DESKTOPSERVICES
  105. if (QAction* action = qobject_cast<QAction*>(sender()))
  106. QDesktopServices::openUrl(action->data().toUrl());
  107. else
  108. QDesktopServices::openUrl(url);
  109. #endif
  110. }
  111. QString WebPage::userAgentForUrl(const QUrl& url) const
  112. {
  113. if (!m_userAgent.isEmpty())
  114. return m_userAgent;
  115. return QWebPage::userAgentForUrl(url);
  116. }
  117. bool WebPage::shouldInterruptJavaScript()
  118. {
  119. if (!m_interruptingJavaScriptEnabled)
  120. return false;
  121. return QWebPage::shouldInterruptJavaScript();
  122. }
  123. void WebPage::authenticationRequired(QNetworkReply* reply, QAuthenticator* authenticator)
  124. {
  125. QDialog* dialog = new QDialog(QApplication::activeWindow());
  126. dialog->setWindowTitle("HTTP Authentication");
  127. QGridLayout* layout = new QGridLayout(dialog);
  128. dialog->setLayout(layout);
  129. QLabel* messageLabel = new QLabel(dialog);
  130. messageLabel->setWordWrap(true);
  131. QString messageStr = QString("Enter with username and password for: %1");
  132. messageLabel->setText(messageStr.arg(reply->url().toString()));
  133. layout->addWidget(messageLabel, 0, 1);
  134. #ifndef QT_NO_LINEEDIT
  135. QLabel* userLabel = new QLabel("Username:", dialog);
  136. layout->addWidget(userLabel, 1, 0);
  137. QLineEdit* userInput = new QLineEdit(dialog);
  138. layout->addWidget(userInput, 1, 1);
  139. QLabel* passLabel = new QLabel("Password:", dialog);
  140. layout->addWidget(passLabel, 2, 0);
  141. QLineEdit* passInput = new QLineEdit(dialog);
  142. passInput->setEchoMode(QLineEdit::Password);
  143. layout->addWidget(passInput, 2, 1);
  144. #endif
  145. QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
  146. | QDialogButtonBox::Cancel, Qt::Horizontal, dialog);
  147. connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
  148. connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
  149. layout->addWidget(buttonBox, 3, 1);
  150. if (dialog->exec() == QDialog::Accepted) {
  151. #ifndef QT_NO_LINEEDIT
  152. authenticator->setUser(userInput->text());
  153. authenticator->setPassword(passInput->text());
  154. #endif
  155. }
  156. delete dialog;
  157. }
  158. void WebPage::requestPermission(QWebFrame* frame, QWebPage::Feature feature)
  159. {
  160. setFeaturePermission(frame, feature, PermissionGrantedByUser);
  161. }
  162. void WebPage::featurePermissionRequestCanceled(QWebFrame*, QWebPage::Feature)
  163. {
  164. }
  165. QWebPage* WebPage::createWindow(QWebPage::WebWindowType type)
  166. {
  167. LauncherWindow* mw = new LauncherWindow;
  168. if (type == WebModalDialog)
  169. mw->setWindowModality(Qt::ApplicationModal);
  170. mw->show();
  171. return mw->page();
  172. }
  173. QObject* WebPage::createPlugin(const QString &classId, const QUrl&, const QStringList&, const QStringList&)
  174. {
  175. if (classId == "alien_QLabel") {
  176. QLabel* l = new QLabel;
  177. l->winId();
  178. return l;
  179. }
  180. if (classId == QLatin1String("QProgressBar"))
  181. return new QProgressBar(view());
  182. if (classId == QLatin1String("QLabel"))
  183. return new QLabel(view());
  184. return 0;
  185. }