From e72ce0546e2306dfc9c725afeca2dd24ce499352 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 5 Sep 2017 18:56:11 +0500 Subject: [PATCH] Add HostInfo class. --- src/ui/FortFirewall.pro | 2 + src/ui/fortmanager.cpp | 2 + src/ui/qml/pages/ActivityPage.qml | 145 +++++++++++++++++++++--------- src/ui/util/hostinfo.cpp | 50 +++++++++++ src/ui/util/hostinfo.h | 42 +++++++++ 5 files changed, 199 insertions(+), 42 deletions(-) create mode 100644 src/ui/util/hostinfo.cpp create mode 100644 src/ui/util/hostinfo.h diff --git a/src/ui/FortFirewall.pro b/src/ui/FortFirewall.pro index 6788abae..767994fe 100644 --- a/src/ui/FortFirewall.pro +++ b/src/ui/FortFirewall.pro @@ -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 \ diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index f0c56046..24590869 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -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("com.fortfirewall", 1, 0, "LogEntry"); qmlRegisterType("com.fortfirewall", 1, 0, "FileUtil"); + qmlRegisterType("com.fortfirewall", 1, 0, "HostInfo"); qmlRegisterType("com.fortfirewall", 1, 0, "NetUtil"); qmlRegisterType("com.fortfirewall", 1, 0, "OsUtil"); } diff --git a/src/ui/qml/pages/ActivityPage.qml b/src/ui/qml/pages/ActivityPage.qml index 041cf656..26f8d018 100644 --- a/src/ui/qml/pages/ActivityPage.qml +++ b/src/ui/qml/pages/ActivityPage.qml @@ -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 } } } diff --git a/src/ui/util/hostinfo.cpp b/src/ui/util/hostinfo.cpp new file mode 100644 index 00000000..d36cc6e8 --- /dev/null +++ b/src/ui/util/hostinfo.cpp @@ -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); +} diff --git a/src/ui/util/hostinfo.h b/src/ui/util/hostinfo.h new file mode 100644 index 00000000..6971b55e --- /dev/null +++ b/src/ui/util/hostinfo.h @@ -0,0 +1,42 @@ +#ifndef HOSTINFO_H +#define HOSTINFO_H + +#include +#include +#include + +typedef QHash 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