Vidalia 0.3.1
HelpTextBrowser.cpp
Go to the documentation of this file.
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/*
12** \file HelpTextBrowser.cpp
13** \brief Displays an HTML-based help document
14*/
15
16#include "HelpTextBrowser.h"
17#include "VMessageBox.h"
18#include "Vidalia.h"
19
20#include "html.h"
21
22#include <QDir>
23#include <QFile>
24#include <QDesktopServices>
25
26
27/** Default constructor. */
29 : QTextBrowser(parent)
30{
31 setOpenExternalLinks(false);
32}
33
34/** Loads a resource into the browser. If it is an HTML resource, we'll load
35 * it as UTF-8, so the special characters in our translations appear properly. */
37HelpTextBrowser::loadResource(int type, const QUrl &name)
38{
39 /* If it's an HTML file, we'll handle it ourselves */
40 if (type == QTextDocument::HtmlResource) {
41 QString helpPath = ":/help/";
42
43 /* Fall back to English if there is no translation of the specified help
44 * page in the current language. */
45 if (!name.path().contains("/")) {
46 QString language = Vidalia::language();
47 if (!QDir(":/help/" + language).exists())
48 language = "en";
49 helpPath += language + "/";
50 }
51
52 QFile file(helpPath + name.path());
53 if (!file.open(QIODevice::ReadOnly)) {
54 return tr("Error opening help file: ") + name.path();
55 }
56 return QString::fromUtf8(file.readAll());
57 }
58 /* Everything else, just let QTextBrowser take care of it. */
59 return QTextBrowser::loadResource(type, name);
60}
61
62
63/** Called when the displayed document is changed. If <b>url</b> specifies
64 * an external link, then the user will be prompted for whether they want to
65 * open the link in their default browser or not. */
66void
68{
69 if (url.scheme() != "qrc" && !url.isRelative()) {
70 /* External link. Prompt the user for a response. */
71 int ret = VMessageBox::question(this,
72 tr("Opening External Link"),
73 p(tr("Vidalia can open the link you selected in your default "
74 "Web browser. If your browser is not currently "
75 "configured to use Tor then the request will not be "
76 "anonymous.")) +
77 p(tr("Do you want Vidalia to open the link in your Web "
78 "browser?")),
79 VMessageBox::Yes|VMessageBox::Default,
81
82 if (ret == VMessageBox::Cancel)
83 return;
84
85 bool ok = QDesktopServices::openUrl(url);
86 if (!ok) {
88 tr("Unable to Open Link"),
89 tr("Vidalia was unable to open the selected link in your Web browser. "
90 "You can still copy the URL and paste it into your browser."),
92 }
93 } else {
94 /* Internal link. Just load it like normal. */
95 QTextBrowser::setSource(url);
96 }
97}
98
stop errmsg QVariant
HelpTextBrowser(QWidget *parent=0)
QVariant loadResource(int type, const QUrl &name)
virtual void setSource(const QUrl &url)
static int information(QWidget *parent, QString caption, QString text, int button0, int button1=NoButton, int button2=NoButton)
static int question(QWidget *parent, QString caption, QString text, int button0, int button1=NoButton, int button2=NoButton, QString remember=QString(), VSettings *settings=0, QString key=QString())
static QString language()
Definition: Vidalia.h:69
QString p(QString str)
Definition: html.cpp:22