Vidalia 0.2.12
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.vidalia-project.net/. No part of Vidalia, including this file, 00007 ** may be copied, modified, propagated, or distributed except according to the 00008 ** terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file HelpTextBrowser.cpp 00013 ** \brief Displays an HTML-based help document 00014 */ 00015 00016 #include "HelpTextBrowser.h" 00017 #include "VMessageBox.h" 00018 #include "Vidalia.h" 00019 00020 #include "html.h" 00021 00022 #include <QDir> 00023 #include <QFile> 00024 #include <QDesktopServices> 00025 00026 00027 /** Default constructor. */ 00028 HelpTextBrowser::HelpTextBrowser(QWidget *parent) 00029 : QTextBrowser(parent) 00030 { 00031 setOpenExternalLinks(false); 00032 } 00033 00034 /** Loads a resource into the browser. If it is an HTML resource, we'll load 00035 * it as UTF-8, so the special characters in our translations appear properly. */ 00036 QVariant 00037 HelpTextBrowser::loadResource(int type, const QUrl &name) 00038 { 00039 /* If it's an HTML file, we'll handle it ourselves */ 00040 if (type == QTextDocument::HtmlResource) { 00041 QString helpPath = ":/help/"; 00042 00043 /* Fall back to English if there is no translation of the specified help 00044 * page in the current language. */ 00045 if (!name.path().contains("/")) { 00046 QString language = Vidalia::language(); 00047 if (!QDir(":/help/" + language).exists()) 00048 language = "en"; 00049 helpPath += language + "/"; 00050 } 00051 00052 QFile file(helpPath + name.path()); 00053 if (!file.open(QIODevice::ReadOnly)) { 00054 return tr("Error opening help file: ") + name.path(); 00055 } 00056 return QString::fromUtf8(file.readAll()); 00057 } 00058 /* Everything else, just let QTextBrowser take care of it. */ 00059 return QTextBrowser::loadResource(type, name); 00060 } 00061 00062 00063 /** Called when the displayed document is changed. If <b>url</b> specifies 00064 * an external link, then the user will be prompted for whether they want to 00065 * open the link in their default browser or not. */ 00066 void 00067 HelpTextBrowser::setSource(const QUrl &url) 00068 { 00069 if (url.scheme() != "qrc" && !url.isRelative()) { 00070 /* External link. Prompt the user for a response. */ 00071 int ret = VMessageBox::question(this, 00072 tr("Opening External Link"), 00073 p(tr("Vidalia can open the link you selected in your default " 00074 "Web browser. If your browser is not currently " 00075 "configured to use Tor then the request will not be " 00076 "anonymous.")) + 00077 p(tr("Do you want Vidalia to open the link in your Web " 00078 "browser?")), 00079 VMessageBox::Yes|VMessageBox::Default, 00080 VMessageBox::Cancel|VMessageBox::Cancel); 00081 00082 if (ret == VMessageBox::Cancel) 00083 return; 00084 00085 bool ok = QDesktopServices::openUrl(url); 00086 if (!ok) { 00087 VMessageBox::information(this, 00088 tr("Unable to Open Link"), 00089 tr("Vidalia was unable to open the selected link in your Web browser. " 00090 "You can still copy the URL and paste it into your browser."), 00091 VMessageBox::Ok); 00092 } 00093 } else { 00094 /* Internal link. Just load it like normal. */ 00095 QTextBrowser::setSource(url); 00096 } 00097 } 00098