forked from Libki/libki-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
162 lines (130 loc) · 4.44 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright 2020 Maryse Simard <[email protected]>
*
* This file is part of Libki.
*
* Libki is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Libki is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Libki. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h"
#include <QDebug>
#include <QLocale>
#include <QtNetwork/QHostInfo>
#include <QNetworkInterface>
#include <QSettings>
QString getLabel(QString labelcode) {
qDebug("ENTER utils/getLabel");
QSettings settings;
settings.setIniCodec("UTF-8");
QString locale = QLocale::system().name();
QString label = QString();
if (!settings.value("labels-" + locale + "/" + labelcode)
.toString()
.isEmpty()) {
label = settings.value("labels-" + locale + "/" + labelcode).toString();
} else if (!settings
.value("labels-" + locale.left(locale.indexOf('_')) + "/" +
labelcode)
.toString()
.isEmpty()) {
label = settings
.value("labels-" + locale.left(locale.indexOf('_')) + "/" +
labelcode)
.toString();
} else if (!settings.value("labels/" + labelcode).toString().isEmpty()) {
label = settings.value("labels/" + labelcode).toString();
}
qDebug("LEAVE utils/getLabel");
return label;
}
QString clientName = "";
QString getClientName() {
qDebug("ENTER utils/getClientName");
if ( clientName.length() == 0 ) {
QSettings settings;
settings.setIniCodec("UTF-8");
QString os_username;
#ifdef Q_OS_WIN
os_username = getenv("USERNAME");
#endif // ifdef Q_OS_WIN
#ifdef Q_OS_UNIX
os_username = getenv("USER");
#endif // ifdef Q_OS_UNIX
clientName = settings.value("node/name").toString();
qDebug() << "OS USERNAME: " << os_username;
qDebug() << "CONFIG NODE NAME: " << clientName;
if (clientName == "OS_USERNAME") {
clientName = os_username;
}
// Fail over to hostname if node name isn't defined.
if (clientName.isEmpty()) {
QHostInfo hostInfo;
hostInfo = QHostInfo::fromName(QHostInfo::localHostName());
clientName = QHostInfo::localHostName();
}
qDebug() << "NODE NAME: " << clientName;
}
qDebug("LEAVE utils/getClientName");
return clientName;
}
QNetworkInterface getNetworkInterface() {
QNetworkInterface netInterface;
foreach(QNetworkInterface ni, QNetworkInterface::allInterfaces()) {
// Get the first non-loopback MAC Address which is up & running
if (ni.isValid() && !(ni.flags() & QNetworkInterface::IsLoopBack)
&& ni.flags() & QNetworkInterface::IsRunning) {
netInterface = ni;
}
}
return netInterface;
}
QString IPv4Address = "";
QString getIPv4Address() {
qDebug("ENTER utils/getIPv4Address");
if ( IPv4Address.length() == 0 ) {
QNetworkInterface netInterface = getNetworkInterface();
if ( netInterface.isValid() ) {
foreach(QNetworkAddressEntry addressEntry, netInterface.addressEntries()) {
if ( addressEntry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
IPv4Address = addressEntry.ip().toString();
}
}
}
}
qDebug() << "IPv4 Address: " << IPv4Address;
qDebug("LEAVE utils/getIPv4Address");
return IPv4Address;
}
QString MACAddress = "";
QString getMACAddress() {
qDebug("ENTER utils/getMACAddress");
if ( MACAddress.length() == 0 ) {
QNetworkInterface netInterface = getNetworkInterface();
if ( netInterface.isValid() ) {
MACAddress = netInterface.hardwareAddress();
}
}
qDebug() << "MAC Address: " << MACAddress;
qDebug("LEAVE utils/getMACAddress");
return MACAddress;
}
QString hostname = "";
QString getHostname() {
qDebug("ENTER utils/getHostname");
if ( hostname.length() == 0 ) {
hostname = QHostInfo::localHostName();
}
qDebug() << "Hostname: " << hostname;
qDebug("LEAVE utils/getHostname");
return hostname;
}