Vidalia  0.3.1
Vidalia.h
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 vidalia.h
13 ** \brief Main Vidalia QApplication object
14 */
15 
16 #ifndef _VIDALIA_H
17 #define _VIDALIA_H
18 
19 #include "config.h"
20 #include "VidaliaSettings.h"
21 #include "TorSettings.h"
22 #include "Log.h"
23 #include "TorControl.h"
24 
25 #include <QApplication>
26 #include <QMap>
27 #include <QList>
28 #include <QString>
29 #include <QKeySequence>
30 
31 #if defined(Q_OS_WIN)
32 #include <windows.h>
33 #include "win32.h"
34 #endif
35 
36 /** Pointer to this Vidalia application instance. */
37 #define vApp ((Vidalia *)qApp)
38 
39 #define vDebug(fmt) (vApp->log(Log::Debug, (fmt)))
40 #define vInfo(fmt) (vApp->log(Log::Info, (fmt)))
41 #define vNotice(fmt) (vApp->log(Log::Notice, (fmt)))
42 #define vWarn(fmt) (vApp->log(Log::Warn, (fmt)))
43 #define vError(fmt) (vApp->log(Log::Error, (fmt)))
44 
45 
46 class Vidalia : public QApplication
47 {
48  Q_OBJECT
49 
50 public:
51  /** Constructor. */
52  Vidalia(QStringList args, int &argc, char **argv);
53  /** Destructor. */
54  ~Vidalia();
55 
56  /** Validates that all arguments were well-formed. */
57  bool validateArguments(QString &errmsg);
58  /** Displays usage information for command-line args. */
59  static void showUsageMessageBox();
60  /** Returns true if the user wants to see usage information. */
61  static bool showUsage();
62 
63  /** Sets the current language. */
64  static bool setLanguage(QString languageCode = QString());
65  /** Sets the current GUI style. */
66  static bool setStyle(QString styleKey = QString());
67 
68  /** Returns the current language. */
69  static QString language() { return _language; }
70  /** Returns the current GUI style. */
71  static QString style() { return _style; }
72  /** Returns Vidalia's application version. */
73  static QString version() { return VIDALIA_VERSION; }
74 
75  /** Returns Vidalia's main TorControl object. */
76  static TorControl* torControl() { return _torControl; }
77 
78  /** Returns the location Vidalia uses for its data files. */
79  static QString dataDirectory();
80  /** Returns the default location of Vidalia's data directory. */
81  static QString defaultDataDirectory();
82 
83  /** Returns the location of Vidalia's pid file. */
84  static QString pidFile();
85 
86  /** Returns true if Vidalia should read the control password from stdin.
87  */
88  static bool readPasswordFromStdin();
89 
90  /** Writes <b>msg</b> with severity <b>level</b> to Vidalia's log. */
91  static Log::LogMessage log(Log::LogLevel level, QString msg);
92 
93  /** Enters the main event loop and waits until exit() is called. The signal
94  * running() will be emitted when the event loop has started. */
95  static int run();
96 
97  /** Creates and binds a shortcut such that when <b>key</b> is pressed in
98  * <b>sender</b>'s context, <b>receiver</b>'s <b>slot</b> will be called. */
99  static void createShortcut(const QKeySequence &key, QWidget *sender,
100  QObject *receiver, const char *slot);
101 
102  /** Creates and binds a shortcut such that when <b>key</b> is pressed in
103  * <b>sender</b>'s context, <b>receiver</b>'s <b>slot</b> will be called. */
104  static void createShortcut(const QString &key, QWidget *sender,
105  QObject *receiver, const char *slot);
106 
107  /** Loads and installs all available translators for the specified
108  * <b>languageCode</b>. All currently installed QTranslator objects will be
109  * removed. Returns true if at least Vidalia's language file can be loaded
110  * for the given language. Otherwise, returns false and no change is made
111  * to the current translators.
112  */
113  static bool retranslateUi(const QString &languageCode);
114 
115 signals:
116  /** Emitted when the application is running and the main event loop has
117  * started. */
118  void running();
119 
120 protected:
121 #if defined(Q_OS_WIN)
122  /** Filters Windows events, looking for events of interest */
123  bool winEventFilter(MSG *msg, long *result);
124 #endif
125 
126  /** Removes all currently installed QTranslators. */
127  static void removeAllTranslators();
128 
129 private slots:
130  /** Called when the application's main event loop has started. This method
131  * will emit the running() signal to indicate that the application's event
132  * loop is running. */
133  void onEventLoopStarted();
134 
135 private:
136  /** Catches debugging messages from Qt and sends them to
137  * Vidalia's logs. */
138  static void qt_msg_handler(QtMsgType type, const char *msg);
139 
140  /** Parse the list of command-line arguments. */
141  void parseArguments(QStringList args);
142  /** Returns true if the specified arguments wants a value. */
143  bool argNeedsValue(QString argName);
144 
145  /** Copies a default settings file (if one exists) to Vidalia's data
146  * directory.
147  */
148  void copyDefaultSettingsFile() const;
149 
150  /** Clears the list of default CA certificates and adds only the ones
151  * Vidalia is interested in.
152  */
153  void loadDefaultCaCertificates() const;
154 
155  static QMap<QString, QString> _args; /**< List of command-line arguments. */
156  static QString _style; /**< The current GUI style. */
157  static QString _language; /**< The current language. */
158  static TorControl* _torControl; /**< Vidalia's main TorControl object.*/
159  static Log _log; /**< Logs debugging messages to file or stdout. */
160  static QList<QTranslator *> _translators; /**< List of installed translators. */
161 };
162 
163 #endif
164 
static int run()
Definition: Vidalia.cpp:157
static void removeAllTranslators()
Definition: Vidalia.cpp:419
static QList< QTranslator * > _translators
Definition: Vidalia.h:160
static QMap< QString, QString > _args
Definition: Vidalia.h:155
static bool retranslateUi(const QString &languageCode)
Definition: Vidalia.cpp:430
void loadDefaultCaCertificates() const
Definition: Vidalia.cpp:521
static QString defaultDataDirectory()
Definition: Vidalia.cpp:365
static QString _style
Definition: Vidalia.h:156
Vidalia(QStringList args, int &argc, char **argv)
Definition: Vidalia.cpp:93
static QString pidFile()
Definition: Vidalia.cpp:378
void onEventLoopStarted()
Definition: Vidalia.cpp:167
LogLevel
Definition: Log.h:34
static QString language()
Definition: Vidalia.h:69
bool validateArguments(QString &errmsg)
Definition: Vidalia.cpp:272
static QString _language
Definition: Vidalia.h:157
bool argNeedsValue(QString argName)
Definition: Vidalia.cpp:228
static QString style()
Definition: Vidalia.h:71
static void qt_msg_handler(QtMsgType type, const char *msg)
Definition: Vidalia.cpp:67
static bool setStyle(QString styleKey=QString())
Definition: Vidalia.cpp:338
static TorControl * torControl()
Definition: Vidalia.h:76
static QString dataDirectory()
Definition: Vidalia.cpp:355
static QString version()
Definition: Vidalia.h:73
static bool readPasswordFromStdin()
Definition: Vidalia.cpp:387
#define VIDALIA_VERSION
Definition: config.h:17
void copyDefaultSettingsFile() const
Definition: Vidalia.cpp:490
static Log _log
Definition: Vidalia.h:159
void running()
void parseArguments(QStringList args)
Definition: Vidalia.cpp:241
Definition: Log.h:30
static Log::LogMessage log(Log::LogLevel level, QString msg)
Definition: Vidalia.cpp:394
~Vidalia()
Definition: Vidalia.cpp:149
static TorControl * _torControl
Definition: Vidalia.h:158
static bool showUsage()
Definition: Vidalia.cpp:187
static bool setLanguage(QString languageCode=QString())
Definition: Vidalia.cpp:318
static void createShortcut(const QKeySequence &key, QWidget *sender, QObject *receiver, const char *slot)
Definition: Vidalia.cpp:402
static void showUsageMessageBox()
Definition: Vidalia.cpp:194