Vidalia  0.2.15
BridgeDownloader.h
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.torproject.org/projects/vidalia.html. No part of Vidalia, 
00007 **  including this file, may be copied, modified, propagated, or distributed 
00008 **  except according to the terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file BridgeDownloader.h
00013 ** \brief Downloads a list of new bridge addresses via HTTPS
00014 */
00015 
00016 #ifndef _BRIDGEDOWNLOADER_H
00017 #define _BRIDGEDOWNLOADER_H
00018 
00019 #include <QtNetwork>
00020 
00021 class BridgeDownloader : public QObject
00022 {
00023   Q_OBJECT
00024 
00025 public:
00026   /** Available bridge download methods. */
00027   enum BridgeDownloadMethod {
00028     DownloadMethodHttps, /** Download via an HTTPS connection. */
00029   };
00030 
00031   /** Default constructor.
00032    */
00033   BridgeDownloader(QObject *parent = 0);
00034 
00035   /** Initiates a request for a set of bridges using the specified
00036    * download <b>method</b>. Returns true if the request was initiated
00037    * successfully, or false on error.
00038    */
00039   bool downloadBridges(BridgeDownloadMethod method);
00040 
00041   /** Enables HTTPS proxy support, using the proxy server <b>host</b> on
00042    * port <b>port</b>. A <b>username</b> and <b>password</b> can also
00043    * optionally be supplied, if required by the proxy.
00044    */
00045   void setProxy(const QString &host, int port,
00046                 const QString &username = QString(),
00047                 const QString &password = QString());
00048 
00049   /** Returns true if <b>method</b> is supported by the currently
00050    * available Qt libraries.
00051    */
00052   static bool isMethodSupported(BridgeDownloadMethod method);
00053 
00054 public slots:
00055   /** Cancels any pending bridge download requests.
00056    */
00057   void cancelBridgeRequest();
00058 
00059 signals:
00060   /** Emitted when the underlying QHttp object reads data from an HTTPS
00061    * response. <b>done</b> indicates how many bytes out of <b>total</b>
00062    * have been read so far. Note that <b>total</b> may be 0 if the expected
00063    * total size of the response is not known.
00064    */
00065   void downloadProgress(qint64 done, qint64 total);
00066 
00067   /** Emitted when the status of the bridge request changes. <b>status</b>
00068    * describes the new current state of the request.
00069    */
00070   void statusChanged(const QString &status);
00071 
00072   /** Emitted when the previous request for bridge addresses completes
00073    * successfully. The QStringList <b>bridges</b> contains a (possibly empty)
00074    * list of bridge addresses parsed from the received response.
00075    */
00076   void bridgeRequestFinished(const QStringList &bridges);
00077 
00078   /** Emitted when the previous request for bridge addresses fails. The
00079    * QString <b>error</b> is a human-readable string describing the error
00080    * encountered.
00081    */
00082   void bridgeRequestFailed(const QString &error);
00083 
00084 private slots:
00085   /** Called when the state of the underlying QHttp object changes. A
00086    * statusChanged() signal is emitted with the appropriate text
00087    * describing the new state of the request.
00088    */
00089   void httpsStateChanged(int state); 
00090 
00091   /** Called when the underlying QHttp object used to make the bridge
00092    * request completes. <b>error</b> is set to false if the request was
00093    * successful, or true if the request failed. If <b>id</b> does not
00094    * match the request ID previously returned by QHttp::get(), then the
00095    * signal is ignored since it is the result of a close() or abort()
00096    * request.
00097    */
00098   void httpsRequestFinished(QNetworkReply *reply);
00099 
00100   /** Called when the HTTPS connection encounters one or more
00101    * <b>sslErrors</b>. Currently the errors are just logged and
00102    * bridgeRequestFailed() is <i>not</i> emitted, since QHttp will also
00103    * emit 
00104    */
00105   void sslErrors(QNetworkReply *, const QList<QSslError> &sslErrors);
00106 
00107 private:
00108   /** Initiates an HTTPS connection to bridges.torproject.org to start
00109    * downloading a set of bridges.
00110    */
00111   void startHttpsDownload();
00112 
00113   /** Used to connect to the bridge database, send an HTTPS request for
00114    * new bridge addresses and then read the response. */
00115   QNetworkAccessManager* _https;
00116   
00117   /** Identifier of the current bridge request */
00118   QNetworkReply *_reply;
00119 };
00120 
00121 #endif
00122