Add HostInfo class.

This commit is contained in:
Nodir Temirkhodjaev 2017-09-05 18:56:11 +05:00
parent bd29dc6784
commit e72ce0546e
5 changed files with 199 additions and 42 deletions

View File

@ -20,6 +20,7 @@ SOURCES += \
util/confutil.cpp \
util/device.cpp \
util/fileutil.cpp \
util/hostinfo.cpp \
util/ip4range.cpp \
util/netutil.cpp \
util/processinfo.cpp \
@ -39,6 +40,7 @@ HEADERS += \
util/confutil.h \
util/device.h \
util/fileutil.h \
util/hostinfo.h \
util/ip4range.h \
util/netutil.h \
util/processinfo.h \

View File

@ -16,6 +16,7 @@
#include "driver/drivermanager.h"
#include "fortsettings.h"
#include "util/fileutil.h"
#include "util/hostinfo.h"
#include "util/netutil.h"
#include "util/osutil.h"
@ -55,6 +56,7 @@ void FortManager::registerQmlTypes()
qmlRegisterType<LogEntry>("com.fortfirewall", 1, 0, "LogEntry");
qmlRegisterType<FileUtil>("com.fortfirewall", 1, 0, "FileUtil");
qmlRegisterType<HostInfo>("com.fortfirewall", 1, 0, "HostInfo");
qmlRegisterType<NetUtil>("com.fortfirewall", 1, 0, "NetUtil");
qmlRegisterType<OsUtil>("com.fortfirewall", 1, 0, "OsUtil");
}

View File

@ -10,7 +10,10 @@ BasePage {
property alias enableLogReading: cbShowBlockedApps.checked
property var appPaths: []
property var appPathsMap: ({})
property var appPathIpMap: ({})
property var appPathIpArray: ({})
property var hostNames: ({})
function readLogAsync() {
driverManager.readLogAsync(logBuffer);
@ -33,34 +36,45 @@ BasePage {
}
function clearAppPaths() {
listView.model = undefined;
appPaths = [];
appPathIpMap = ({});
appPathIpArray = ({});
appPaths = ([]);
appPathsMap = ({});
hostNames = ({});
hostInfo.abortHostLookups();
refreshListViews();
}
function processLogBuffer() {
const curIndex = listView.currentIndex;
listView.model = undefined;
while (logBuffer.read(logEntry)) {
var path = getEntryPath(logEntry);
var ipText = netUtil.ip4ToText(logEntry.ip);
var ipTextsMap = appPathsMap[path];
var ipTextsMap = appPathIpMap[path];
if (!ipTextsMap) {
ipTextsMap = ({});
appPathsMap[path] = ipTextsMap;
curIndex = appPaths.length;
appPathIpMap[path] = ipTextsMap;
appPathIpArray[path] = [];
appPaths.push(path);
}
ipTextsMap[ipText] = (ipTextsMap[ipText] || 0) + 1;
var ipCount = ipTextsMap[ipText];
ipTextsMap[ipText] = (ipCount || 0) + 1;
var ipTextsArray = appPathIpArray[path];
if (!ipCount) {
ipTextsArray.push(ipText);
}
// Host name
if (hostNames[ipText] === undefined) {
hostNames[ipText] = false;
hostInfo.lookupHost(ipText);
}
}
listView.model = appPaths;
listView.currentIndex = curIndex;
refreshListViews();
}
function getEntryPath(logEntry) {
@ -72,6 +86,14 @@ BasePage {
return fileUtil.dosPathToPath(dosPath);
}
function refreshListViews() {
const curIndex = appListView.currentIndex;
appListView.model = undefined;
appListView.model = appPaths;
appListView.currentIndex = curIndex;
}
Connections {
target: mainPage
onClosed: switchLogReading(false)
@ -90,6 +112,16 @@ BasePage {
}
}
HostInfo {
id: hostInfo
onHostLookedup: {
if (success) {
hostNames[name] = hostName;
refreshListViews();
}
}
}
LogBuffer {
id: logBuffer
}
@ -110,8 +142,8 @@ BasePage {
Button {
id: btCopy
enabled: currentItem
text: QT_TRANSLATE_NOOP("qml", "Copy")
readonly property Item currentItem: listView.currentItem
text: QT_TRANSLATE_NOOP("qml", "Copy Path")
readonly property Item currentItem: appListView.currentItem
onClicked: {
osUtil.setClipboardData(currentItem.text);
}
@ -133,40 +165,69 @@ BasePage {
Layout.fillHeight: true
clip: true
ListView {
id: listView
RowLayout {
anchors.fill: parent
spacing: 10
spacing: 20
highlightRangeMode: ListView.ApplyRange
highlightResizeDuration: 150
highlightMoveDuration: 200
ListView {
id: appListView
Layout.fillWidth: true
Layout.fillHeight: true
spacing: 10
highlight: Item {
Rectangle {
highlightRangeMode: ListView.ApplyRange
highlightResizeDuration: 0
highlightMoveDuration: 200
highlight: Item {
Rectangle {
anchors.fill: parent
anchors.margins: -7
radius: 2
border.width: 3
border.color: "black"
color: "transparent"
}
}
delegate: Label {
width: appListView.width
font.pixelSize: 20
elide: Text.ElideRight
text: modelData
}
MouseArea {
anchors.fill: parent
anchors.margins: -7
radius: 2
border.width: 3
border.color: "black"
color: "transparent"
onClicked: {
const index = appListView.indexAt(mouse.x, mouse.y);
if (index >= 0) {
appListView.currentIndex = index;
}
}
}
}
delegate: Label {
width: listView.width
font.pixelSize: 20
elide: Text.ElideRight
text: modelData
}
ListView {
id: ipListView
Layout.fillWidth: true
Layout.fillHeight: true
spacing: 4
MouseArea {
anchors.fill: parent
onClicked: {
const index = listView.indexAt(mouse.x, mouse.y);
if (index >= 0) {
listView.currentIndex = index;
}
model: {
const curIndex = appListView.currentIndex;
if (curIndex < 0)
return undefined;
const path = appPaths[curIndex];
return appPathIpArray[path];
}
delegate: Label {
width: ipListView.width
elide: Text.ElideRight
text: hostNames[ipText] || ipText
readonly property string ipText: modelData
}
}
}

50
src/ui/util/hostinfo.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "hostinfo.h"
HostInfo::HostInfo(QObject *parent) :
QObject(parent)
{
}
HostInfo::~HostInfo()
{
abortHostLookups();
}
void HostInfo::lookupHost(const QString &name)
{
const int lookupId = QHostInfo::lookupHost(
name, this, &HostInfo::handleLookedupHost);
m_lookupIds.insert(lookupId, name);
}
void HostInfo::abortHostLookup(int lookupId)
{
m_lookupIds.remove(lookupId);
QHostInfo::abortHostLookup(lookupId);
}
void HostInfo::abortHostLookups()
{
lookupids_map_t::key_iterator it = m_lookupIds.keyBegin();
lookupids_map_t::key_iterator end = m_lookupIds.keyEnd();
for (; it != end; ++it) {
const int lookupId = *it;
abortHostLookup(lookupId);
}
}
void HostInfo::handleLookedupHost(const QHostInfo &info)
{
const int lookupId = info.lookupId();
const QString name = m_lookupIds.value(lookupId);
const bool success = (info.error() == QHostInfo::NoError);
m_info = info;
m_lookupIds.remove(lookupId);
emit hostLookedup(name, success);
}

42
src/ui/util/hostinfo.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef HOSTINFO_H
#define HOSTINFO_H
#include <QObject>
#include <QHostInfo>
#include <QHash>
typedef QHash<int, QString> lookupids_map_t;
class HostInfo : public QObject
{
Q_OBJECT
Q_PROPERTY(QString hostName READ hostName CONSTANT)
public:
explicit HostInfo(QObject *parent = nullptr);
virtual ~HostInfo();
QString hostName() const { return m_info.hostName(); }
QString errorString() const { return m_info.errorString(); }
signals:
void hostLookedup(const QString &name, bool success);
public slots:
void lookupHost(const QString &name);
void abortHostLookups();
private slots:
void handleLookedupHost(const QHostInfo &info);
private:
void abortHostLookup(int lookupId);
private:
QHostInfo m_info;
lookupids_map_t m_lookupIds;
};
#endif // HOSTINFO_H