[qnut] fixed main window show/hide on tray icon click event
[nut.git] / libnutcommon / macaddress.h
blob755bb44ff267052fd8f279445878c4a676f1142a
1 #ifndef NUT_COMMON_MACADDRESS_H
2 #define NUT_COMMON_MACADDRESS_H
4 #include <QString>
5 #include <QDBusArgument>
6 #include <QHash>
7 extern "C" {
8 #include <net/ethernet.h>
10 namespace libnutcommon {
11 class MacAddress;
13 QDBusArgument &operator<< (QDBusArgument &argument, const MacAddress &data);
14 const QDBusArgument &operator>> (const QDBusArgument &argument, MacAddress &data);
17 namespace libnutcommon {
18 /**
19 @author Stefan Bühler <stbuehler@web.de>
21 class MacAddress {
22 public:
23 MacAddress() { clear(); }
24 MacAddress(const QString &str);
25 MacAddress(const quint8 *d);
26 MacAddress(const ether_addr * eth);
27 quint8 data[6];
29 inline bool operator==(const MacAddress &ma) const {
30 for (int i = 0; i < 6; i++)
31 if (data[i] != ma.data[i])
32 return false;
33 return true;
35 inline bool operator!=(const MacAddress &ma) const {
36 return !(*this == ma);
38 inline bool operator<(const MacAddress &b) {
39 if ( *((quint32*)data) < *((quint32*)b.data) ) {
40 return true;
42 else if ( *((quint32*)data) == *((quint32*)b.data) ) { //First is equal
43 return ( (*((quint16*)(data+4))) < *((quint16*)((b.data)+4)) );
45 else {
46 return false;
49 inline QString toString() const {
50 char buf[sizeof("00:00:00:00:00:00")];
51 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
52 data[0],data[1],data[2],data[3],data[4],data[5]);
53 return QString(buf);
56 inline bool zero() const {
57 return *((quint32*)data) == 0 && *((quint16*)(data+4)) == 0;
60 inline bool valid() const {
61 return !zero();
64 inline void clear() {
65 *((quint32*)data) = 0;
66 *((quint16*)(data+4)) = 0;
71 static inline uint qHash(const libnutcommon::MacAddress &key) {
72 quint8 data[8] = { key.data[0], key.data[2], key.data[3], key.data[4], key.data[5], key.data[6], 0, 0};
73 return qHash(*((quint64*)data));
76 Q_DECLARE_METATYPE(libnutcommon::MacAddress)
78 #endif