[qnut] fixed main window show/hide on tray icon click event
[nut.git] / libnutcommon / config.h
blob9703c150d71fe53332261f547fda425058360601
1 #ifndef NUT_COMMON_CONFIG_H
2 #define NUT_COMMON_CONFIG_H
4 #include <QObject>
5 #include <QString>
6 #include <QList>
7 #include <QVector>
8 #include <QHostAddress>
9 #include <QHash>
10 #include <QDBusArgument>
12 #include "macaddress.h"
14 namespace nuts {
15 class ConfigParser;
18 namespace libnutcommon {
19 class Config;
20 class DeviceConfig;
21 class SelectRule;
22 class SelectConfig;
23 class EnvironmentConfig;
24 class IPv4Config;
26 QDBusArgument &operator<< (QDBusArgument &argument, const Config &data);
27 const QDBusArgument &operator>> (const QDBusArgument &argument, Config &data);
29 QDBusArgument &operator<< (QDBusArgument &argument, const DeviceConfig &data);
30 const QDBusArgument &operator>> (const QDBusArgument &argument, DeviceConfig &data);
32 QDBusArgument &operator<< (QDBusArgument &argument, const SelectRule &data);
33 const QDBusArgument &operator>> (const QDBusArgument &argument, SelectRule &data);
35 QDBusArgument &operator<< (QDBusArgument &argument, const SelectConfig &data);
36 const QDBusArgument &operator>> (const QDBusArgument &argument, SelectConfig &data);
38 QDBusArgument &operator<< (QDBusArgument &argument, const EnvironmentConfig &data);
39 const QDBusArgument &operator>> (const QDBusArgument &argument, EnvironmentConfig &data);
41 QDBusArgument &operator<< (QDBusArgument &argument, const IPv4Config &data);
42 const QDBusArgument &operator>> (const QDBusArgument &argument, IPv4Config &data);
45 namespace libnutcommon {
46 namespace internal {
47 /**
48 * @brief Used as a member in other classes, it determines wheter that class is a original or a copy.
50 * On normal construct (-> default constructor) without params,
51 * the instance evals to False (no copy)
52 * On copy, it changes to true.
53 * So obviously this breaks the idea that a copy is equal to the original.
54 * This helps you to build copy-constructor (or to remove them), and do
55 * some cleanups only in the original destructor.
57 * In a ideal world we would never copy objects which need this behaviour,
58 * but in conjunction with the Qt DBus layer this is helpful.
60 class CopyMark {
61 private:
62 bool m_isCopy;
63 CopyMark(bool isCopy) : m_isCopy(isCopy) { }
64 public:
65 CopyMark() : m_isCopy(false) { }
66 CopyMark(const CopyMark &) : m_isCopy(true) { }
67 operator bool () { return m_isCopy; }
69 static CopyMark Copy() { return CopyMark(true); }
70 static CopyMark NoCopy() { return CopyMark(false); }
74 /** @brief Container for all \link DeviceConfig device configs\endlink. A deviceName may contain wildcards (not supported yet).
76 class Config {
77 private:
78 internal::CopyMark m_isCopy;
80 protected:
81 friend class nuts::ConfigParser;
82 friend QDBusArgument &operator<< (QDBusArgument &argument, const Config &data);
83 friend const QDBusArgument &operator>> (const QDBusArgument &argument, Config &data);
85 QHash<QString, DeviceConfig*> m_devices;
87 public:
88 Config();
89 virtual ~Config();
91 DeviceConfig* getDevice(const QString &deviceName) {
92 return m_devices.value(deviceName, 0);
95 const QHash<QString, DeviceConfig*> &getDevices() {
96 return m_devices;
100 /** @brief Each device has a list of \link EnvironmentConfig Environments\endlink and some additional config values.
102 * Each device can have many Environments, but it can only be in one; each
103 * Environment consists of zero, one or more Interfaces.
105 * Which Environment is taken is controlled by the SelectRules of an Environment.
107 * For WLAN devices supported by wpa_supplicant you can specify a configfile and a driver (wext by default).
109 class DeviceConfig {
110 private:
111 internal::CopyMark m_isCopy;
113 protected:
114 friend class nuts::ConfigParser;
115 friend QDBusArgument &operator<< (QDBusArgument &argument, const DeviceConfig &data);
116 friend const QDBusArgument &operator>> (const QDBusArgument &argument, DeviceConfig &data);
118 QList<EnvironmentConfig*> m_environments;
119 bool m_noAutoStart;
120 QString m_wpaConfigFile;
121 QString m_wpaDriver;
123 public:
124 DeviceConfig();
125 virtual ~DeviceConfig();
127 const QList<EnvironmentConfig*>& getEnvironments() {
128 return m_environments;
131 bool noAutoStart() { return m_noAutoStart; }
133 QString wpaConfigFile() { return m_wpaConfigFile; }
134 QString wpaDriver() { return m_wpaDriver; }
137 /** @brief Result type of a select test.
140 class SelectResult {
141 public:
142 typedef enum {
143 False = 0, //!< Cannot select
144 User = 1, //!< Do select only if user does want it
145 NotUser = 2, //!< Should not happen; "only select if user does not want it..." is not good
146 True = 3 //!< Select
147 } bool_t;
148 protected:
149 friend QDBusArgument &operator<< (QDBusArgument &argument, const SelectResult &data);
150 friend const QDBusArgument &operator>> (const QDBusArgument &argument, SelectResult &data);
152 bool_t m_value;
154 public:
155 SelectResult(bool_t value = False)
156 : m_value(value) { }
158 SelectResult& operator = (bool_t value) {
159 m_value = value;
160 return *this;
162 SelectResult& operator = (const SelectResult &other) {
163 m_value = other.m_value;
164 return *this;
166 SelectResult& operator = (qint8 value) {
167 m_value = (bool_t) value;
168 return *this;
171 SelectResult operator || (const SelectResult &other) {
172 const bool_t op_or[16] = {
173 False , User , NotUser, True,
174 User , User , True , True,
175 NotUser, True , NotUser, True,
176 True , True , True , True
178 return op_or[m_value*4 + other.m_value];
180 SelectResult operator && (const SelectResult &other) {
181 const bool_t op_and[16] = {
182 False , False, False , False,
183 False , User , False , User,
184 False , False, NotUser, NotUser,
185 False , User , NotUser, True
187 return op_and[m_value*4 + other.m_value];
190 operator bool_t () const {
191 return m_value;
194 operator qint8 () const {
195 return (qint8) m_value;
198 SelectResult operator !() const {
199 return (SelectResult::bool_t) (3 - m_value);
203 /** @brief A select operation.
206 class SelectRule {
207 protected:
208 friend QDBusArgument &operator<< (QDBusArgument &argument, const SelectRule &data);
209 friend const QDBusArgument &operator>> (const QDBusArgument &argument, SelectRule &data);
211 public:
212 typedef enum {
213 SEL_USER, //!< Select if user does want it (=> return SelectResult::User)
214 SEL_ARP, //!< Select if ipAddr is found on the network from the device; if macAddr != 0 it is matched too.
215 SEL_ESSID, //!< Select if in wlan essid;
216 SEL_AND_BLOCK, //!< Select a list of \link SelectRule SelectRules\endlink, results combined with AND
217 SEL_OR_BLOCK //!< Select a list of \link SelectRule SelectRules\endlink, results combined with OR
218 } SelectType;
220 SelectRule() : invert(false), selType(SEL_USER) { }
221 SelectRule(const QHostAddress &ipAddr, bool invert = false) : invert(invert), selType(SEL_ARP), ipAddr(ipAddr) { }
222 SelectRule(const QHostAddress &ipAddr, const libnutcommon::MacAddress &macAddr, bool invert = false) : invert(invert), selType(SEL_ARP), ipAddr(ipAddr), macAddr(macAddr) { }
223 SelectRule(const QString &essid, bool invert = false) : invert(invert), selType(SEL_ESSID), essid(essid) { }
224 SelectRule(quint32 block, SelectType blockType, bool invert = false) : invert(invert), selType(blockType), block(block) { }
226 bool invert; //!< Invert result; unused for now.
227 SelectType selType;
228 quint32 block; //!< Block identifier in SelectConfig for SEL_*_BLOCK
229 QString essid;
230 QHostAddress ipAddr;
231 libnutcommon::MacAddress macAddr;
234 /** @brief SelectConfig for an environment.
236 * This structure represents a tree of SelectRules; try qnut for visualization.
238 class SelectConfig {
239 friend class nuts::ConfigParser;
240 friend QDBusArgument &operator<< (QDBusArgument &argument, const SelectConfig &data);
241 friend const QDBusArgument &operator>> (const QDBusArgument &argument, SelectConfig &data);
243 public:
244 SelectConfig() { }
246 QVector<SelectRule> filters; //!< List of \link SelectRule SelectRules\endlink
247 //! List of blocks; each block is a list of filter ids.
248 //! The type of the block (AND/OR) is specified in the rule for the block
249 QVector< QVector<quint32> > blocks;
252 /** @brief Each EnvironmentConfig of a \link DeviceConfig device\endlink has a list
253 * of \link IPv4Config interfaces\endlink, which configure the ips.
255 * And it contains a SelectConfig, which determines which Environment is loaded for a device.
257 class EnvironmentConfig {
258 private:
259 internal::CopyMark m_isCopy;
260 protected:
261 friend class nuts::ConfigParser;
262 friend QDBusArgument &operator<< (QDBusArgument &argument, const EnvironmentConfig &data);
263 friend const QDBusArgument &operator>> (const QDBusArgument &argument, EnvironmentConfig &data);
265 QString m_name;
266 QList<IPv4Config*> m_ipv4Interfaces;
267 SelectConfig m_select;
269 public:
270 EnvironmentConfig(const QString &name = "");
271 virtual ~EnvironmentConfig();
273 QString getName() { return m_name; } //!< A description for that environment. It does not have to be unique.
274 const QList<IPv4Config*>& getIPv4Interfaces() { return m_ipv4Interfaces; }
275 const SelectConfig &getSelect() { return m_select; }
278 /** @brief Each IPv4Config stands for one ip of an interface.
280 * There are several methods how to to this, and some
281 * additional values: netmask, gateway, dns-servers.
283 * If the interface has the method IPv4Config::DO_STATIC,
284 * the configured values for ip/netmask/... can be queried
285 * with the corresponding functions.
287 class IPv4Config {
288 public:
289 /** @brief Selects which method is used to determine the ip address.
291 * Not all flags can be set:
292 * assert(USERSTATIC xor (DHCP or (ZEROCONF xor STATIC)))
294 typedef enum {
295 DO_DHCP = 1, //!< Find ip/gateway/dns via DHCP
296 DO_ZEROCONF = 2, //!< Probe for ip in the IPv4 Link-Local range (RFC 3927).
297 DO_STATIC = 4, //!< Use values from config file.
298 DO_USERSTATIC = 8 //!< Use values specified at runtime by a user
299 } Flags;
301 /** @brief Unused/Unsupported. Could be used to overwrite some value with static configured ones.
304 typedef enum {
305 OW_IP = 1,
306 OW_NETMASK = 2,
307 OW_GATEWAY = 4,
308 OW_DNSSERVER = 8
309 } OverwriteFlags;
311 protected:
312 friend class nuts::ConfigParser;
313 friend QDBusArgument &operator<< (QDBusArgument &argument, const IPv4Config &data);
314 friend const QDBusArgument &operator>> (const QDBusArgument &argument, IPv4Config &data);
316 QHostAddress m_static_ip, m_static_netmask, m_static_gateway;
317 QList<QHostAddress> m_static_dnsservers;
319 int m_flags;
320 int m_overwriteFlags;
321 int m_timeout;
322 bool m_continue_dhcp;
324 public:
325 IPv4Config(int flags = IPv4Config::DO_DHCP | IPv4Config::DO_ZEROCONF, int overwriteFlags = 0);
327 const QHostAddress& getStaticIP() const { return m_static_ip; }
328 const QHostAddress& getStaticNetmask() const { return m_static_netmask; }
329 const QHostAddress& getStaticGateway() const { return m_static_gateway; }
330 const QList<QHostAddress>& getStaticDNS() const { return m_static_dnsservers; }
332 Flags getFlags() const { return (Flags) m_flags; }
333 OverwriteFlags getOverwriteFlags() const { return (OverwriteFlags) m_overwriteFlags; }
334 int getTimeOut() const { return m_timeout; }
337 /** @brief If an interface has to be configured by the user (IPv4Config::DO_USERSTATIC), he/she has to
338 * set that information with this class.
341 class IPv4UserConfig {
342 protected:
343 friend QDBusArgument &operator<< (QDBusArgument &argument, const IPv4UserConfig &data);
344 friend const QDBusArgument &operator>> (const QDBusArgument &argument, IPv4UserConfig &data);
346 QHostAddress m_ip, m_netmask, m_gateway;
347 QList<QHostAddress> m_dnsservers;
349 public:
350 const QHostAddress& ip() const { return m_ip; }
351 bool setIP(const QHostAddress &ip) { m_ip = ip; return true; }
353 const QHostAddress& netmask() const { return m_netmask; }
354 bool setNetmask(const QHostAddress &netmask) { m_netmask = netmask; return true; }
356 const QHostAddress& gateway() const { return m_gateway; }
357 bool setGateway(const QHostAddress &gateway) { m_gateway = gateway; return true; }
359 const QList<QHostAddress>& dnsservers() const { return m_dnsservers; }
360 bool setDnsservers(const QList<QHostAddress>& dnsservers) { m_dnsservers = dnsservers; return true; }
362 /** @brief A very basic check if the configuration is valid.
364 bool valid() {
365 return !m_ip.isNull();
370 Q_DECLARE_METATYPE(libnutcommon::Config)
371 Q_DECLARE_METATYPE(libnutcommon::DeviceConfig)
372 Q_DECLARE_METATYPE(libnutcommon::SelectResult)
373 Q_DECLARE_METATYPE(QVector< libnutcommon::SelectResult >)
374 Q_DECLARE_METATYPE(libnutcommon::SelectRule)
375 Q_DECLARE_METATYPE(libnutcommon::SelectConfig)
376 Q_DECLARE_METATYPE(libnutcommon::EnvironmentConfig)
377 Q_DECLARE_METATYPE(libnutcommon::IPv4Config)
378 Q_DECLARE_METATYPE(libnutcommon::IPv4UserConfig)
380 #endif