summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-11-17 12:58:35 (GMT)
committerMontel Laurent <montel@kde.org>2016-11-17 12:58:35 (GMT)
commitb6fef0529619a40642724deff4f088cbc066bf4e (patch)
treee2eaab7bcfdb772f5cb4882913966bf5152a0128
parent00c89a0e3d961dcf3c4e9040bf0a6a35746cfc7a (diff)
Improve database management
-rw-r--r--webengineviewer/src/checkphishingurl/localdatabasemanager.cpp37
-rw-r--r--webengineviewer/src/checkphishingurl/localdatabasemanager.h13
-rw-r--r--webengineviewer/src/checkphishingurl/tests/managelocaldatabasegui.cpp1
3 files changed, 44 insertions, 7 deletions
diff --git a/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp b/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp
index 430b6f5..c22fab5 100644
--- a/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp
+++ b/webengineviewer/src/checkphishingurl/localdatabasemanager.cpp
@@ -23,17 +23,24 @@
#include <QStandardPaths>
#include <QSqlDatabase>
#include <QSqlError>
+#include <QSqlQuery>
+#include <QDebug>
+#include <QDir>
using namespace WebEngineViewer;
Q_GLOBAL_STATIC(LocalDataBaseManager, s_localDataBaseManager)
+namespace {
+inline QString tableName() {
+ return QStringLiteral("malware");
+}
+}
+
LocalDataBaseManager::LocalDataBaseManager(QObject *parent)
: QObject(parent),
mDataBaseOk(false)
{
-
- mDataBaseOk = initializeDataBase();
}
LocalDataBaseManager::~LocalDataBaseManager()
@@ -41,9 +48,19 @@ LocalDataBaseManager::~LocalDataBaseManager()
}
+void LocalDataBaseManager::start()
+{
+ mDataBaseOk = initializeDataBase();
+ if (initializeDataBase()) {
+ if ( !mDataBase.tables().contains( tableName() ) ) {
+ createTable();
+ }
+ }
+}
+
QString LocalDataBaseManager::localDataBasePath() const
{
- return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/phishingurl/");
+ return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/phishingurl/");
}
LocalDataBaseManager *LocalDataBaseManager::self()
@@ -61,10 +78,20 @@ bool LocalDataBaseManager::initializeDataBase()
return true;
}
+bool LocalDataBaseManager::createTable()
+{
+ QSqlQuery query;
+ query.exec(QStringLiteral("create table %1 (id int primary key, "
+ "hash varchar(20))").arg(tableName()));
+ return true;
+}
+
QSqlError LocalDataBaseManager::initDb()
{
mDataBase = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
- mDataBase.setDatabaseName(localDataBasePath());
+ qDebug()<<" localDataBasePath()"<<localDataBasePath();
+ QDir().mkpath(localDataBasePath());
+ mDataBase.setDatabaseName(localDataBasePath() + QStringLiteral("/malwaredb.sql"));
if (!mDataBase.open()) {
return mDataBase.lastError();
}
@@ -76,7 +103,7 @@ void LocalDataBaseManager::checkUrl(const QUrl &url)
if (mDataBaseOk) {
} else {
-
+ Q_EMIT checkUrlFinished(url, WebEngineViewer::LocalDataBaseManager::Unknown);
}
//TODO
}
diff --git a/webengineviewer/src/checkphishingurl/localdatabasemanager.h b/webengineviewer/src/checkphishingurl/localdatabasemanager.h
index 0e71331..c5803f8 100644
--- a/webengineviewer/src/checkphishingurl/localdatabasemanager.h
+++ b/webengineviewer/src/checkphishingurl/localdatabasemanager.h
@@ -21,15 +21,22 @@
#define LOCALDATABASEMANAGER_H
#include <QObject>
+#include "webengineviewer_export.h"
#include <QSqlDatabase>
#include <QUrl>
class QSqlError;
namespace WebEngineViewer
{
-class LocalDataBaseManager : public QObject
+class WEBENGINEVIEWER_EXPORT LocalDataBaseManager : public QObject
{
Q_OBJECT
public:
+ enum UrlStatus {
+ Unknown = 0,
+ UrlOk = 1,
+ Malware = 2
+ };
+
explicit LocalDataBaseManager(QObject *parent = Q_NULLPTR);
~LocalDataBaseManager();
@@ -37,11 +44,13 @@ public:
void checkUrl(const QUrl &url);
+ void start();
Q_SIGNALS:
- void checkUrlFinished(const QUrl &url);
+ void checkUrlFinished(const QUrl &url, WebEngineViewer::LocalDataBaseManager::UrlStatus status);
private:
QSqlError initDb();
bool initializeDataBase();
+ bool createTable();
QString localDataBasePath() const;
QSqlDatabase mDataBase;
bool mDataBaseOk;
diff --git a/webengineviewer/src/checkphishingurl/tests/managelocaldatabasegui.cpp b/webengineviewer/src/checkphishingurl/tests/managelocaldatabasegui.cpp
index 03a0c9e..fadd5d9 100644
--- a/webengineviewer/src/checkphishingurl/tests/managelocaldatabasegui.cpp
+++ b/webengineviewer/src/checkphishingurl/tests/managelocaldatabasegui.cpp
@@ -51,6 +51,7 @@ ManageLocalDataBaseGui::~ManageLocalDataBaseGui()
void ManageLocalDataBaseGui::slotDownloadFullDatabase()
{
+ WebEngineViewer::LocalDataBaseManager::self()->start();
}