-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathISystemInfoFactory.cpp
102 lines (86 loc) · 2.7 KB
/
ISystemInfoFactory.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
#include "ISystemInfoFactory.h"
#include <QEventLoop>
#include <QHostInfo>
#include <QNetworkAccessManager>
#include <QNetworkInterface>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QTimer>
ISystemInfoFactory::ISystemInfoFactory() {}
QString ISystemInfoFactory::localmachineName()
{
return QHostInfo::localHostName();
}
QString ISystemInfoFactory::localIP()
{
QString ip = "";
QList<QNetworkInterface> interFaceList = QNetworkInterface::allInterfaces();
for (int i = 0; i < interFaceList.size(); i++)
{
QNetworkInterface info = interFaceList.at(i);
if (info.flags().testFlag(QNetworkInterface::IsRunning))
{
QList<QNetworkAddressEntry> entryList = info.addressEntries();
foreach (QNetworkAddressEntry entry, entryList)
{
if (QAbstractSocket::IPv4Protocol == entry.ip().protocol() && entry.ip() != QHostAddress::LocalHost
&& entry.ip().toString().startsWith("192.168."))
{
ip = entry.ip().toString();
break;
}
}
}
}
return ip;
}
QString ISystemInfoFactory::publicIP()
{
QString strIp;
QString webCode = getHtml("http://whois.pconline.com.cn/");
if (!webCode.isEmpty())
{
QString web = webCode.replace(" ", "");
web = web.replace("\r", "");
web = web.replace("\n", "");
QStringList list = web.split("<br/>");
QString tar = list[3];
QStringList ip = tar.split("=");
strIp = ip[1];
}
else
strIp = "无法获取公网ip";
return strIp;
}
QString ISystemInfoFactory::macAddress()
{
QString strMac = "";
QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface item, netList)
{
if ((QNetworkInterface::IsUp & item.flags()) && (QNetworkInterface::IsRunning & item.flags()))
{
if (strMac.isEmpty() || strMac < item.hardwareAddress())
{
strMac = item.hardwareAddress();
}
}
}
return strMac;
}
QString ISystemInfoFactory::getHtml(QString url)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
QByteArray responseData;
QEventLoop eventLoop;
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
connect(timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
timer->start(300);
eventLoop.exec();
responseData = reply->readAll();
delete timer;
return QString(responseData);
}