HelpTextBrowser.cpp

Go to the documentation of this file.
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 ** \version $Id: HelpTextBrowser.cpp 3735 2009-04-28 20:28:01Z edmanm $
00014 ** \brief Displays an HTML-based help document
00015 */
00016 
00017 #include "HelpTextBrowser.h"
00018 #include "VMessageBox.h"
00019 #include "Vidalia.h"
00020 
00021 #include "html.h"
00022 
00023 #include <QDir>
00024 #include <QFile>
00025 #include <QDesktopServices>
00026 
00027 
00028 /** Default constructor. */
00029 HelpTextBrowser::HelpTextBrowser(QWidget *parent)
00030   : QTextBrowser(parent)
00031 {
00032   setOpenExternalLinks(false);
00033 }
00034 
00035 /** Loads a resource into the browser. If it is an HTML resource, we'll load
00036  * it as UTF-8, so the special characters in our translations appear properly. */
00037 QVariant
00038 HelpTextBrowser::loadResource(int type, const QUrl &name)
00039 {
00040   /* If it's an HTML file, we'll handle it ourselves */
00041   if (type == QTextDocument::HtmlResource) {
00042     QString helpPath = ":/help/";
00043     
00044     /* Fall back to English if there is no translation of the specified help
00045      * page in the current language. */
00046     if (!name.path().contains("/")) {
00047       QString language = Vidalia::language();
00048       if (!QDir(":/help/" + language).exists())
00049         language = "en";
00050       helpPath += language + "/";
00051     }
00052     
00053     QFile file(helpPath + name.path());
00054     if (!file.open(QIODevice::ReadOnly)) {
00055       return tr("Error opening help file: ") + name.path();
00056     }
00057     return QString::fromUtf8(file.readAll());
00058   }
00059   /* Everything else, just let QTextBrowser take care of it. */
00060   return QTextBrowser::loadResource(type, name);
00061 }
00062 
00063 
00064 /** Called when the displayed document is changed. If <b>url</b> specifies
00065  * an external link, then the user will be prompted for whether they want to
00066  * open the link in their default browser or not. */
00067 void
00068 HelpTextBrowser::setSource(const QUrl &url)
00069 {
00070   if (url.scheme() != "qrc" && !url.isRelative()) {
00071     /* External link. Prompt the user for a response. */
00072     int ret = VMessageBox::question(this,
00073                 tr("Opening External Link"),
00074                 p(tr("Vidalia can open the link you selected in your default "
00075                      "Web browser. If your browser is not currently "
00076                      "configured to use Tor then the request will not be "
00077                      "anonymous.")) +
00078                 p(tr("Do you want Vidalia to open the link in your Web "
00079                      "browser?")),
00080                 VMessageBox::Yes|VMessageBox::Default, 
00081                 VMessageBox::Cancel|VMessageBox::Cancel);
00082     
00083     if (ret == VMessageBox::Cancel)
00084       return;
00085     
00086     bool ok = QDesktopServices::openUrl(url);
00087     if (!ok) {
00088       VMessageBox::information(this,
00089         tr("Unable to Open Link"),
00090         tr("Vidalia was unable to open the selected link in your Web browser. "
00091            "You can still copy the URL and paste it into your browser."),
00092         VMessageBox::Ok);
00093     }
00094   } else {
00095     /* Internal link. Just load it like normal. */
00096     QTextBrowser::setSource(url);
00097   }
00098 }
00099 

Generated on Mon Aug 30 19:09:59 2010 for Vidalia by  doxygen 1.5.9