PageRenderTime 1840ms CodeModel.GetById 50ms RepoModel.GetById 19ms app.codeStats 16ms

/vidalia-0.2.15/src/vidalia/help/browser/HelpTextBrowser.cpp

#
C++ | 98 lines | 59 code | 14 blank | 25 comment | 12 complexity | c6f668e08f739dbe65fe5895bd6b7e9d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, GPL-3.0, LGPL-3.0
  1. /*
  2. ** This file is part of Vidalia, and is subject to the license terms in the
  3. ** LICENSE file, found in the top level directory of this distribution. If you
  4. ** did not receive the LICENSE file with this file, you may obtain it from the
  5. ** Vidalia source package distributed by the Vidalia Project at
  6. ** http://www.torproject.org/projects/vidalia.html. No part of Vidalia,
  7. ** including this file, may be copied, modified, propagated, or distributed
  8. ** except according to the terms described in the LICENSE file.
  9. */
  10. /*
  11. ** \file HelpTextBrowser.cpp
  12. ** \brief Displays an HTML-based help document
  13. */
  14. #include "HelpTextBrowser.h"
  15. #include "VMessageBox.h"
  16. #include "Vidalia.h"
  17. #include "html.h"
  18. #include <QDir>
  19. #include <QFile>
  20. #include <QDesktopServices>
  21. /** Default constructor. */
  22. HelpTextBrowser::HelpTextBrowser(QWidget *parent)
  23. : QTextBrowser(parent)
  24. {
  25. setOpenExternalLinks(false);
  26. }
  27. /** Loads a resource into the browser. If it is an HTML resource, we'll load
  28. * it as UTF-8, so the special characters in our translations appear properly. */
  29. QVariant
  30. HelpTextBrowser::loadResource(int type, const QUrl &name)
  31. {
  32. /* If it's an HTML file, we'll handle it ourselves */
  33. if (type == QTextDocument::HtmlResource) {
  34. QString helpPath = ":/help/";
  35. /* Fall back to English if there is no translation of the specified help
  36. * page in the current language. */
  37. if (!name.path().contains("/")) {
  38. QString language = Vidalia::language();
  39. if (!QDir(":/help/" + language).exists())
  40. language = "en";
  41. helpPath += language + "/";
  42. }
  43. QFile file(helpPath + name.path());
  44. if (!file.open(QIODevice::ReadOnly)) {
  45. return tr("Error opening help file: ") + name.path();
  46. }
  47. return QString::fromUtf8(file.readAll());
  48. }
  49. /* Everything else, just let QTextBrowser take care of it. */
  50. return QTextBrowser::loadResource(type, name);
  51. }
  52. /** Called when the displayed document is changed. If <b>url</b> specifies
  53. * an external link, then the user will be prompted for whether they want to
  54. * open the link in their default browser or not. */
  55. void
  56. HelpTextBrowser::setSource(const QUrl &url)
  57. {
  58. if (url.scheme() != "qrc" && !url.isRelative()) {
  59. /* External link. Prompt the user for a response. */
  60. int ret = VMessageBox::question(this,
  61. tr("Opening External Link"),
  62. p(tr("Vidalia can open the link you selected in your default "
  63. "Web browser. If your browser is not currently "
  64. "configured to use Tor then the request will not be "
  65. "anonymous.")) +
  66. p(tr("Do you want Vidalia to open the link in your Web "
  67. "browser?")),
  68. VMessageBox::Yes|VMessageBox::Default,
  69. VMessageBox::Cancel|VMessageBox::Cancel);
  70. if (ret == VMessageBox::Cancel)
  71. return;
  72. bool ok = QDesktopServices::openUrl(url);
  73. if (!ok) {
  74. VMessageBox::information(this,
  75. tr("Unable to Open Link"),
  76. tr("Vidalia was unable to open the selected link in your Web browser. "
  77. "You can still copy the URL and paste it into your browser."),
  78. VMessageBox::Ok);
  79. }
  80. } else {
  81. /* Internal link. Just load it like normal. */
  82. QTextBrowser::setSource(url);
  83. }
  84. }