Vidalia 0.2.12

Vidalia.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.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 vidalia.h
00013 ** \brief Main Vidalia QApplication object
00014 */
00015 
00016 #ifndef _VIDALIA_H
00017 #define _VIDALIA_H
00018 
00019 #include "config.h"
00020 #include "VidaliaSettings.h"
00021 #include "TorSettings.h"
00022 #include "Log.h"
00023 #include "TorControl.h"
00024 
00025 #include <QApplication>
00026 #include <QMap>
00027 #include <QList>
00028 #include <QString>
00029 #include <QKeySequence>
00030 
00031 #if defined(Q_OS_WIN)
00032 #include <windows.h>
00033 #include "win32.h"
00034 #endif
00035 
00036 /** Pointer to this Vidalia application instance. */
00037 #define vApp  ((Vidalia *)qApp)
00038 
00039 #define vDebug(fmt)   (vApp->log(Log::Debug, (fmt)))
00040 #define vInfo(fmt)    (vApp->log(Log::Info, (fmt)))
00041 #define vNotice(fmt)  (vApp->log(Log::Notice, (fmt)))
00042 #define vWarn(fmt)    (vApp->log(Log::Warn, (fmt)))
00043 #define vError(fmt)   (vApp->log(Log::Error, (fmt)))
00044 
00045 
00046 class Vidalia : public QApplication
00047 {
00048   Q_OBJECT
00049 
00050 public:
00051   /** Constructor. */
00052   Vidalia(QStringList args, int &argc, char **argv);
00053   /** Destructor. */
00054   ~Vidalia();
00055 
00056   /** Validates that all arguments were well-formed. */
00057   static bool validateArguments(QString &errmsg);
00058   /** Displays usage information for command-line args. */
00059   static void showUsageMessageBox();
00060   /** Returns true if the user wants to see usage information. */
00061   static bool showUsage();
00062   
00063   /** Sets the current language. */
00064   static bool setLanguage(QString languageCode = QString());
00065   /** Sets the current GUI style. */
00066   static bool setStyle(QString styleKey = QString());
00067   
00068   /** Returns the current language. */
00069   static QString language() { return _language; }
00070   /** Returns the current GUI style. */
00071   static QString style() { return _style; }
00072   /** Returns Vidalia's application version. */
00073   static QString version() { return VIDALIA_VERSION; }
00074 
00075   /** Returns Vidalia's main TorControl object. */
00076   static TorControl* torControl() { return _torControl; }
00077   
00078   /** Returns the location Vidalia uses for its data files. */
00079   static QString dataDirectory();
00080   /** Returns the default location of Vidalia's data directory. */
00081   static QString defaultDataDirectory();
00082   
00083   /** Returns the location of Vidalia's pid file. */
00084   static QString pidFile();
00085 
00086   /** Returns true if Vidalia should read the control password from stdin.
00087    */
00088   static bool readPasswordFromStdin();
00089 
00090   /** Writes <b>msg</b> with severity <b>level</b> to Vidalia's log. */
00091   static Log::LogMessage log(Log::LogLevel level, QString msg);
00092  
00093   /** Enters the main event loop and waits until exit() is called. The signal
00094    * running() will be emitted when the event loop has started. */
00095   static int run();
00096 
00097   /** Creates and binds a shortcut such that when <b>key</b> is pressed in
00098    * <b>sender</b>'s context, <b>receiver</b>'s <b>slot</b> will be called. */
00099   static void createShortcut(const QKeySequence &key, QWidget *sender,
00100                              QObject *receiver, const char *slot);
00101 
00102   /** Creates and binds a shortcut such that when <b>key</b> is pressed in
00103    * <b>sender</b>'s context, <b>receiver</b>'s <b>slot</b> will be called. */
00104   static void createShortcut(const QString &key, QWidget *sender,
00105                              QObject *receiver, const char *slot);
00106 
00107   /** Loads and installs all available translators for the specified
00108    * <b>languageCode</b>. All currently installed QTranslator objects will be
00109    * removed. Returns true if at least Vidalia's language file can be loaded
00110    * for the given language. Otherwise, returns false and no change is made
00111    * to the current translators.
00112    */
00113   static bool retranslateUi(const QString &languageCode);
00114 
00115 signals:
00116   /** Emitted when the application is running and the main event loop has
00117    * started. */ 
00118   void running();
00119 
00120 protected:
00121 #if defined(Q_OS_WIN)
00122   /** Filters Windows events, looking for events of interest */
00123   bool winEventFilter(MSG *msg, long *result);
00124 #endif
00125 
00126   /** Removes all currently installed QTranslators. */
00127   static void removeAllTranslators();
00128 
00129 private slots:
00130   /** Called when the application's main event loop has started. This method
00131    * will emit the running() signal to indicate that the application's event
00132    * loop is running. */
00133   void onEventLoopStarted();
00134  
00135 private:
00136   /** Catches debugging messages from Qt and sends them to 
00137    * Vidalia's logs. */
00138   static void qt_msg_handler(QtMsgType type, const char *msg);
00139 
00140   /** Parse the list of command-line arguments. */
00141   void parseArguments(QStringList args);
00142   /** Returns true if the specified arguments wants a value. */
00143   bool argNeedsValue(QString argName);
00144 
00145   /** Copies a default settings file (if one exists) to Vidalia's data
00146    * directory.
00147    */
00148   void copyDefaultSettingsFile() const;
00149 
00150   /** Clears the list of default CA certificates and adds only the ones
00151    * Vidalia is interested in.
00152    */
00153   void loadDefaultCaCertificates() const;
00154 
00155   static QMap<QString, QString> _args; /**< List of command-line arguments.  */
00156   static QString _style;               /**< The current GUI style.           */
00157   static QString _language;            /**< The current language.            */
00158   static TorControl* _torControl;      /**< Vidalia's main TorControl object.*/
00159   static Log _log; /**< Logs debugging messages to file or stdout. */
00160   static QList<QTranslator *> _translators; /**< List of installed translators. */
00161 };
00162 
00163 #endif
00164