Commit 6aeeda45 authored by Andrea Scarpino's avatar Andrea Scarpino

Add a PullDownMenu to clear history

parent 2baef784
......@@ -60,6 +60,13 @@ Page {
anchors.fill: parent
contentHeight: column.height
PullDownMenu {
MenuItem {
text: qsTr("Clear history")
onClicked: db.clearHistory()
}
}
Column {
id: column
width: parent.width
......
......@@ -37,9 +37,10 @@
const static QString DB_NAME = "hostisdown";
const static QString CLEAR_HISTORY = "DELETE FROM hosts;";
const static QString CREATE_HOSTS_TABLE = "CREATE TABLE IF NOT EXISTS hosts(host TEXT PRIMARY KEY, status SMALLINT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);";
const static QString INSERT_INTO_HOSTS = "INSERT INTO hosts(host, status) VALUES(\"%1\", %2);";
const static QString DELETE_HOST = "DELETE FROM hosts WHERE host=\"%1\";";
const static QString INSERT_INTO_HOSTS = "INSERT INTO hosts(host, status) VALUES(\"%1\", %2);";
DBManager::DBManager(QObject *parent) :
QObject(parent)
......@@ -50,10 +51,8 @@ DBManager::DBManager(QObject *parent) :
db.setDatabaseName(dbPath + QDir::separator() + DB_NAME + ".sql");
const QDir dir;
if (!dir.exists(dbPath))
{
if (!dir.mkpath(dbPath))
{
if (!dir.exists(dbPath)) {
if (!dir.mkpath(dbPath)) {
qCritical("Cannot create data folder!");
}
}
......@@ -67,14 +66,25 @@ DBManager::DBManager(QObject *parent) :
DBManager::~DBManager()
{
delete m_model;
db.close();
}
void DBManager::clearHistory()
{
QSqlQuery clearHistory(db);
if (!clearHistory.exec(CLEAR_HISTORY)) {
qCritical("Cannot clear history");
}
if (m_model) {
m_model->refresh();
}
}
void DBManager::init()
{
QSqlQuery createTable(db);
if (!createTable.exec(CREATE_HOSTS_TABLE))
{
if (!createTable.exec(CREATE_HOSTS_TABLE)) {
qCritical("Cannot create table!");
}
}
......
......@@ -41,6 +41,7 @@ public:
explicit DBManager(QObject *parent = 0);
virtual ~DBManager();
Q_INVOKABLE void clearHistory();
HostsSqlModel* recentHosts();
public Q_SLOTS:
......
......@@ -41,6 +41,7 @@ int main(int argc, char *argv[])
QQuickView *view = SailfishApp::createView();
DBManager db;
view->rootContext()->setContextProperty("db", &db);
QSqlQueryModel* recentHosts = db.recentHosts();
view->rootContext()->setContextProperty("recentHosts", recentHosts);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment