UI: BlockedPage, StatisticsPage: Improve "Clear" button.

This commit is contained in:
Nodir Temirkhodjaev 2017-12-14 09:39:57 +05:00
parent 5c2474b1d7
commit 1647429d3b
19 changed files with 216 additions and 34 deletions

View File

@ -180,7 +180,7 @@ void DatabaseManager::logStatTraf(quint16 procCount, const quint8 *procBits,
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafDay, oldTrafDay) << getTrafficStmt(DatabaseSql::sqlDeleteTrafDay, oldTrafDay)
<< getTrafficStmt(DatabaseSql::sqlDeleteTrafMonth, oldTrafMonth); << getTrafficStmt(DatabaseSql::sqlDeleteTrafMonth, oldTrafMonth);
deleteTrafficList(deleteTrafStmts); stepStmtList(deleteTrafStmts);
} }
m_sqliteDb->commitTransaction(); m_sqliteDb->commitTransaction();
@ -229,6 +229,33 @@ qint64 DatabaseManager::getAppId(const QString &appPath)
return appId; return appId;
} }
void DatabaseManager::deleteApp(qint64 appId)
{
// Delete Statemets
const QStmtList deleteAppStmts = QStmtList()
<< getAppStmt(DatabaseSql::sqlDeleteAppTrafHour, appId)
<< getAppStmt(DatabaseSql::sqlDeleteAppTrafDay, appId)
<< getAppStmt(DatabaseSql::sqlDeleteAppTrafMonth, appId)
<< getAppStmt(DatabaseSql::sqlDeleteAppId, appId);
stepStmtList(deleteAppStmts);
}
void DatabaseManager::resetAppTotals()
{
m_sqliteDb->beginTransaction();
SqliteStmt *stmt = getSqliteStmt(DatabaseSql::sqlResetAppTrafTotals);
const qint64 unixTime = DateUtil::getUnixTime();
stmt->bindInt(1, DateUtil::getUnixHour(unixTime));
stmt->step();
stmt->reset();
m_sqliteDb->commitTransaction();
}
qint64 DatabaseManager::createAppId(const QString &appPath) qint64 DatabaseManager::createAppId(const QString &appPath)
{ {
qint64 appId = 0; qint64 appId = 0;
@ -296,9 +323,9 @@ bool DatabaseManager::updateTraffic(SqliteStmt *stmt, quint32 inBytes,
&& m_sqliteDb->changes() != 0; && m_sqliteDb->changes() != 0;
} }
void DatabaseManager::deleteTrafficList(const QStmtList &deleteStmtList) void DatabaseManager::stepStmtList(const QStmtList &stmtList)
{ {
foreach (SqliteStmt *stmtDelete, deleteStmtList) { foreach (SqliteStmt *stmtDelete, stmtList) {
stmtDelete->step(); stmtDelete->step();
stmtDelete->reset(); stmtDelete->reset();
} }
@ -353,6 +380,15 @@ SqliteStmt *DatabaseManager::getTrafficStmt(const char *sql, qint32 trafTime)
return stmt; return stmt;
} }
SqliteStmt *DatabaseManager::getAppStmt(const char *sql, qint64 appId)
{
SqliteStmt *stmt = getSqliteStmt(sql);
stmt->bindInt64(1, appId);
return stmt;
}
SqliteStmt *DatabaseManager::getSqliteStmt(const char *sql) SqliteStmt *DatabaseManager::getSqliteStmt(const char *sql)
{ {
SqliteStmt *stmt = m_sqliteStmts.value(sql); SqliteStmt *stmt = m_sqliteStmts.value(sql);

View File

@ -35,6 +35,10 @@ public:
qint64 getAppId(const QString &appPath); qint64 getAppId(const QString &appPath);
void deleteApp(qint64 appId);
void resetAppTotals();
qint32 getTrafficTime(const char *sql, qint64 appId = 0); qint32 getTrafficTime(const char *sql, qint64 appId = 0);
void getTraffic(const char *sql, qint32 trafTime, void getTraffic(const char *sql, qint32 trafTime,
@ -63,9 +67,10 @@ private:
bool updateTraffic(SqliteStmt *stmt, quint32 inBytes, bool updateTraffic(SqliteStmt *stmt, quint32 inBytes,
quint32 outBytes, qint64 appId = 0); quint32 outBytes, qint64 appId = 0);
void deleteTrafficList(const QStmtList &deleteStmtList); void stepStmtList(const QStmtList &stmtList);
SqliteStmt *getTrafficStmt(const char *sql, qint32 trafTime); SqliteStmt *getTrafficStmt(const char *sql, qint32 trafTime);
SqliteStmt *getAppStmt(const char *sql, qint64 appId);
SqliteStmt *getSqliteStmt(const char *sql); SqliteStmt *getSqliteStmt(const char *sql);

View File

@ -68,6 +68,10 @@ const char * const DatabaseSql::sqlInsertAppId =
" VALUES(?1, ?2, ?3, 0, 0);" " VALUES(?1, ?2, ?3, 0, 0);"
; ;
const char * const DatabaseSql::sqlDeleteAppId =
"DELETE FROM app WHERE app_id = ?1;"
;
const char * const DatabaseSql::sqlSelectAppPaths = const char * const DatabaseSql::sqlSelectAppPaths =
"SELECT app_id, path FROM app ORDER BY creat_time;" "SELECT app_id, path FROM app ORDER BY creat_time;"
; ;
@ -259,3 +263,22 @@ const char * const DatabaseSql::sqlDeleteTrafDay =
const char * const DatabaseSql::sqlDeleteTrafMonth = const char * const DatabaseSql::sqlDeleteTrafMonth =
"DELETE FROM traffic_month WHERE traf_time < ?1;" "DELETE FROM traffic_month WHERE traf_time < ?1;"
; ;
const char * const DatabaseSql::sqlDeleteAppTrafHour =
"DELETE FROM traffic_app_hour"
" WHERE app_id = ?1;"
;
const char * const DatabaseSql::sqlDeleteAppTrafDay =
"DELETE FROM traffic_app_day"
" WHERE app_id = ?1;"
;
const char * const DatabaseSql::sqlDeleteAppTrafMonth =
"DELETE FROM traffic_app_month"
" WHERE app_id = ?1;"
;
const char * const DatabaseSql::sqlResetAppTrafTotals =
"UPDATE app SET traf_time = ?1, in_bytes = 0, out_bytes = 0;"
;

View File

@ -9,6 +9,7 @@ public:
static const char * const sqlSelectAppId; static const char * const sqlSelectAppId;
static const char * const sqlInsertAppId; static const char * const sqlInsertAppId;
static const char * const sqlDeleteAppId;
static const char * const sqlSelectAppPaths; static const char * const sqlSelectAppPaths;
@ -57,6 +58,12 @@ public:
static const char * const sqlDeleteTrafHour; static const char * const sqlDeleteTrafHour;
static const char * const sqlDeleteTrafDay; static const char * const sqlDeleteTrafDay;
static const char * const sqlDeleteTrafMonth; static const char * const sqlDeleteTrafMonth;
static const char * const sqlDeleteAppTrafHour;
static const char * const sqlDeleteAppTrafDay;
static const char * const sqlDeleteAppTrafMonth;
static const char * const sqlResetAppTrafTotals;
}; };
#endif // DATABASESQL_H #endif // DATABASESQL_H

View File

@ -7,6 +7,8 @@
<file>images/application_double.png</file> <file>images/application_double.png</file>
<file>images/application_edit.png</file> <file>images/application_edit.png</file>
<file>images/application_error.png</file> <file>images/application_error.png</file>
<file>images/arrow_refresh.png</file>
<file>images/bin_empty.png</file>
<file>images/cancel.png</file> <file>images/cancel.png</file>
<file>images/chart_line.png</file> <file>images/chart_line.png</file>
<file>images/clock.png</file> <file>images/clock.png</file>

View File

@ -1,5 +1,6 @@
<RCC> <RCC>
<qresource prefix="/"> <qresource prefix="/">
<file>qml/controls/ButtonMenu.qml</file>
<file>qml/controls/TextAreaFrame.qml</file> <file>qml/controls/TextAreaFrame.qml</file>
<file>qml/controls/TextContextMenu.qml</file> <file>qml/controls/TextContextMenu.qml</file>
<file>qml/controls/TextFieldFrame.qml</file> <file>qml/controls/TextFieldFrame.qml</file>

Binary file not shown.

After

Width:  |  Height:  |  Size: 685 B

BIN
src/ui/images/bin_empty.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 475 B

View File

@ -32,6 +32,22 @@ void AppBlockedModel::clear()
StringListModel::clear(); StringListModel::clear();
} }
void AppBlockedModel::remove(int row)
{
row = adjustRow(row);
beginRemoveRows(QModelIndex(), row, row);
const QString appPath = list().at(row);
m_appIpList.remove(appPath);
m_appIpSet.remove(appPath);
removeRow(row);
endRemoveRows();
}
void AppBlockedModel::addLogEntry(const LogEntryBlocked &logEntry) void AppBlockedModel::addLogEntry(const LogEntryBlocked &logEntry)
{ {
const QString appPath = logEntry.path(); const QString appPath = logEntry.path();

View File

@ -25,6 +25,8 @@ signals:
public slots: public slots:
void clear() override; void clear() override;
void remove(int row = -1) override;
private: private:
QHash<QString, QStringList> m_appIpList; QHash<QString, QStringList> m_appIpList;
QHash<QString, QSet<QString>> m_appIpSet; QHash<QString, QSet<QString>> m_appIpSet;

View File

@ -16,8 +16,11 @@ void AppStatModel::initialize()
updateList(); updateList();
} }
TrafListModel *AppStatModel::trafListModel(int trafType, int row) const TrafListModel *AppStatModel::trafListModel(int trafType, int row,
const QString &appPath) const
{ {
Q_UNUSED(appPath) // used to properly refresh trafListModel
Q_ASSERT(row < m_appIds.size()); Q_ASSERT(row < m_appIds.size());
m_trafListModel->setType(static_cast<TrafListModel::TrafType>(trafType)); m_trafListModel->setType(static_cast<TrafListModel::TrafType>(trafType));
@ -34,6 +37,23 @@ void AppStatModel::clear()
updateList(); updateList();
} }
void AppStatModel::remove(int row)
{
row = adjustRow(row);
beginRemoveRows(QModelIndex(), row, row);
const qint64 appId = m_appIds.at(row);
m_databaseManager->deleteApp(appId);
m_appIds.remove(row);
removeRow(row);
endRemoveRows();
}
void AppStatModel::updateList() void AppStatModel::updateList()
{ {
QStringList list; QStringList list;

View File

@ -16,7 +16,8 @@ public:
void initialize(); void initialize();
Q_INVOKABLE TrafListModel *trafListModel(int trafType, int row) const; Q_INVOKABLE TrafListModel *trafListModel(int trafType, int row,
const QString &appPath) const;
void handleProcNew(const QString &appPath); void handleProcNew(const QString &appPath);
void handleStatTraf(quint16 procCount, const quint8 *procBits, void handleStatTraf(quint16 procCount, const quint8 *procBits,
@ -27,6 +28,8 @@ signals:
public slots: public slots:
void clear() override; void clear() override;
void remove(int row = -1) override;
private: private:
void updateList(); void updateList();

View File

@ -27,6 +27,11 @@ void StringListModel::setList(const QStringList &list)
endResetModel(); endResetModel();
} }
void StringListModel::clear()
{
setList(QStringList());
}
void StringListModel::insert(const QString &text, int row) void StringListModel::insert(const QString &text, int row)
{ {
row = adjustRow(row); row = adjustRow(row);
@ -41,7 +46,7 @@ void StringListModel::remove(int row)
row = adjustRow(row); row = adjustRow(row);
beginRemoveRows(QModelIndex(), row, row); beginRemoveRows(QModelIndex(), row, row);
m_list.removeAt(row); removeRow(row);
endRemoveRows(); endRemoveRows();
} }
@ -56,9 +61,9 @@ void StringListModel::replace(const QString &text, int row)
emit dataChanged(modelIndex, modelIndex); emit dataChanged(modelIndex, modelIndex);
} }
void StringListModel::clear() void StringListModel::removeRow(int row)
{ {
setList(QStringList()); m_list.removeAt(row);
} }
int StringListModel::adjustRow(int row) const int StringListModel::adjustRow(int row) const

View File

@ -17,17 +17,18 @@ public:
const QStringList &list() const { return m_list; } const QStringList &list() const { return m_list; }
void setList(const QStringList &list); void setList(const QStringList &list);
protected:
void insert(const QString &text, int row = -1);
void remove(int row = -1);
void replace(const QString &text, int row = -1);
signals: signals:
public slots: public slots:
virtual void clear(); virtual void clear();
private: virtual void insert(const QString &text, int row = -1);
virtual void remove(int row = -1);
virtual void replace(const QString &text, int row = -1);
protected:
void removeRow(int row);
int adjustRow(int row) const; int adjustRow(int row) const;
private: private:

View File

@ -45,7 +45,7 @@ void TrafListModel::reset()
m_trafCount = getTrafCount(m_type, m_minTrafTime, m_maxTrafTime); m_trafCount = getTrafCount(m_type, m_minTrafTime, m_maxTrafTime);
m_rowCache.invalidate(); invalidateRowCache();
endResetModel(); endResetModel();
} }
@ -96,13 +96,25 @@ void TrafListModel::clear()
reset(); reset();
} }
void TrafListModel::resetAppTotals()
{
m_databaseManager->resetAppTotals();
reset();
}
void TrafListModel::refresh() void TrafListModel::refresh()
{ {
beginResetModel(); beginResetModel();
m_rowCache.invalidate(); invalidateRowCache();
endResetModel(); endResetModel();
} }
void TrafListModel::invalidateRowCache()
{
m_rowCache.invalidate();
}
void TrafListModel::updateRowCache(int row) const void TrafListModel::updateRowCache(int row) const
{ {
m_rowCache.row = row; m_rowCache.row = row;

View File

@ -56,9 +56,12 @@ signals:
public slots: public slots:
void clear(); void clear();
void resetAppTotals();
void refresh(); void refresh();
private: private:
void invalidateRowCache();
void updateRowCache(int row) const; void updateRowCache(int row) const;
QString formatTrafUnit(qint64 bytes) const; QString formatTrafUnit(qint64 bytes) const;

View File

@ -0,0 +1,20 @@
import QtQuick 2.9
import QtQuick.Controls 2.2
Button {
id: bt
checkable: true
default property alias content: menu.contentData
Menu {
id: menu
y: bt.height
visible: bt.checked
onClosed: {
bt.checked = false;
}
}
}

View File

@ -16,12 +16,6 @@ BasePage {
(appListView.currentIndex >= 0 && appListView.currentItem) (appListView.currentIndex >= 0 && appListView.currentItem)
? appListView.currentItem.appPath : "" ? appListView.currentItem.appPath : ""
function clearAppPaths() {
appListView.currentIndex = -1;
appBlockedModel.clear();
}
HostInfoCache { HostInfoCache {
id: hostInfoCache id: hostInfoCache
} }
@ -31,13 +25,25 @@ BasePage {
spacing: 10 spacing: 10
RowLayout { RowLayout {
spacing: 15 ButtonMenu {
Button {
enabled: appListView.count enabled: appListView.count
icon.source: "qrc:/images/bin_empty.png"
text: translationManager.dummyBool text: translationManager.dummyBool
&& qsTranslate("qml", "Clear") && qsTranslate("qml", "Clear")
onClicked: clearAppPaths()
MenuItem {
enabled: appListView.currentIndex >= 0
text: "Remove Application"
onTriggered: appBlockedModel.remove(
appListView.currentIndex)
}
MenuItem {
text: "Clear All"
onTriggered: {
appListView.currentIndex = -1;
appBlockedModel.clear();
}
}
} }
CheckBox { CheckBox {

View File

@ -11,7 +11,9 @@ BasePage {
readonly property LogManager logManager: fortManager.logManager readonly property LogManager logManager: fortManager.logManager
readonly property AppStatModel appStatModel: logManager.appStatModel readonly property AppStatModel appStatModel: logManager.appStatModel
readonly property TrafListModel trafListModel: readonly property TrafListModel trafListModel:
appStatModel.trafListModel(tabBar.currentIndex, appListView.currentIndex) appStatModel.trafListModel(tabBar.currentIndex,
appListView.currentIndex,
currentAppPath)
readonly property string currentAppPath: readonly property string currentAppPath:
(appListView.currentIndex >= 0 && appListView.currentItem) (appListView.currentIndex >= 0 && appListView.currentItem)
@ -40,10 +42,9 @@ BasePage {
spacing: 10 spacing: 10
RowLayout { RowLayout {
spacing: 15
Button { Button {
enabled: appListView.count enabled: appListView.count
icon.source: "qrc:/images/arrow_refresh.png"
text: translationManager.dummyBool text: translationManager.dummyBool
&& qsTranslate("qml", "Refresh") && qsTranslate("qml", "Refresh")
onClicked: trafListModel.refresh() onClicked: trafListModel.refresh()
@ -52,6 +53,11 @@ BasePage {
Row { Row {
spacing: 5 spacing: 5
Item {
width: 1
height: 1
}
Label { Label {
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
text: translationManager.dummyBool text: translationManager.dummyBool
@ -74,14 +80,28 @@ BasePage {
} }
} }
Button { ButtonMenu {
enabled: appListView.count enabled: appListView.count
icon.source: "qrc:/images/bin_empty.png"
text: translationManager.dummyBool text: translationManager.dummyBool
&& qsTranslate("qml", "Clear") && qsTranslate("qml", "Clear")
onClicked: {
appStatModel.clear();
appListView.currentIndex = 0; MenuItem {
enabled: appListView.currentIndex > 0
text: "Remove Application"
onTriggered: appStatModel.remove(
appListView.currentIndex)
}
MenuItem {
text: "Reset Totals"
onTriggered: trafListModel.resetAppTotals()
}
MenuItem {
text: "Clear All"
onTriggered: {
appListView.currentIndex = 0;
appStatModel.clear();
}
} }
} }