From 0f13bc8d9b79e414efc9cf55c1904a75bdfbafde Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 12 Jan 2019 19:35:24 +0100 Subject: [PATCH] Rewrote QT interface to use new database interface. --- CMakeLists.txt | 2 +- src/interface_qt.cpp | 81 ++++++++++++++++------------------- src/interface_qt.hpp | 23 +++++----- src/whyblocked.cpp | 20 ++++++--- src/whyblocked.hpp | 21 ++++----- translations/whyblocked_de.ts | 30 ++++++------- translations/whyblocked_en.ts | 30 ++++++------- 7 files changed, 107 insertions(+), 100 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a4402e..4e9634b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.2) project (whyblocked - VERSION 0.13.1 + VERSION 0.14.0 LANGUAGES CXX ) diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index 09d29fc..0207924 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -33,6 +33,7 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent) , _config("whyblocked.cfg") , _headersize({ 250, 125, 125 }) +, _database() { std::locale::global(std::locale("")); @@ -175,7 +176,7 @@ MainWindow::~MainWindow() _config.write(); } -void MainWindow::populate_tableview(const result_view &entries) +void MainWindow::populate_tableview(const vector &entries) { _model->clear(); _model->setHorizontalHeaderLabels( @@ -188,18 +189,18 @@ void MainWindow::populate_tableview(const result_view &entries) tableview->horizontalHeader()->resizeSection(1, _headersize[1]); tableview->horizontalHeader()->resizeSection(2, _headersize[2]); - for (const std::tuple &line : entries) + for (const Database::data &entry : entries) { - add_row(QString::fromStdString(std::get<0>(line)), - std::get<1>(line), - QString::fromStdString(std::get<2>(line))); + add_row(QString::fromStdString(entry.user), + entry.blocked, + QString::fromStdString(entry.reason)); } } void MainWindow::reload() { - result_view entries; - database::view(entries); + vector entries; + entries = _database.query(); populate_tableview(entries); } @@ -238,23 +239,17 @@ void MainWindow::edit() DialogAdd *dialog = new DialogAdd(this); dialog->setWindowTitle(tr("Edit entry")); - Dialogdata data; QModelIndex index = tableview->selectionModel()->selectedRows().first(); - data.user = index.sibling(index.row(), 0).data().toString().toStdString(); - result_details details; - database::details(data.user, details); - if (std::get<0>(details) == true) - { - data.blocked = 1; - } - else - { - data.blocked = 0; - } - data.reason = std::get<1>(details); - data.receipts = std::get<2>(details); + const string user = index.sibling(index.row(), 0).data() + .toString().toStdString(); - dialog->set_data(data); + Database::data dbdata = + _database.query("SELECT * FROM blocks WHERE user = '" + + user + "';").front(); + dbdata.reason = dbdata.reason; + dbdata.receipts = dbdata.receipts; + + dialog->set_data(dbdata); dialog->setProperty("edit", true); dialog->show(); } @@ -267,7 +262,7 @@ void MainWindow::remove() for (auto &row : selection->selectedRows()) { const string user = row.data().toString().toStdString(); - database::remove(user); + _database.remove(user); _model->removeRow(row.row()); } label_receipts->clear(); @@ -303,25 +298,23 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *event) columns = "user"; } - result_view entries; - result_view filtered_entries; - if (database::view(entries)) + vector entries = _database.query(); + vector filtered_entries; + if (!entries.empty()) { - for (const std::tuple &line : entries) + for (const Database::data &entry : entries) { - const string user = std::get<0>(line); - const string reason = std::get<2>(line); wstring searchstring; std::wstring_convert> convert; if (check_user->isChecked()) { - searchstring += convert.from_bytes(user); + searchstring += convert.from_bytes(entry.user); } if (check_reason->isChecked()) { - searchstring += convert.from_bytes(reason); + searchstring += convert.from_bytes(entry.reason); } std::transform(searchstring.begin(), searchstring.end(), searchstring.begin(), ::towlower); @@ -329,8 +322,7 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *event) text_find->text().toLower().toStdWString()) != std::string::npos) { - filtered_entries.push_back({ - user, std::get<1>(line), reason }); + filtered_entries.push_back(entry); } } } @@ -360,15 +352,16 @@ void MainWindow::show_details(QModelIndex index) { const string user = index.sibling(index.row(), 0).data() .toString().toStdString(); - result_details result; + vector dbdata = + _database.query("SELECT * FROM blocks WHERE user = '" + user + "';"); string text = ""; - if (database::details(user, result)) + if (!dbdata.empty()) { - if (!std::get<2>(result).empty()) + if (!dbdata.front().receipts.empty()) { text += string("") + tr("Receipts:").toStdString() + ""; - for (const string &url : std::get<2>(result)) + for (const string &url : dbdata.front().receipts) { text += "
" + url; } @@ -417,7 +410,7 @@ void MainWindow::dropEvent(QDropEvent *event) } DialogAdd *dialog = new DialogAdd(this); - Dialogdata data; + Database::data data; data.user = text; dialog->set_data(data); dialog->show(); @@ -430,7 +423,7 @@ DialogAdd::DialogAdd(QMainWindow *parent) setupUi(this); } -const Dialogdata DialogAdd::get_data() const +const Database::data DialogAdd::get_data() const { std::vector receipts; for (int row = 0; row <= list_receipts->count() - 1; ++row) @@ -438,7 +431,7 @@ const Dialogdata DialogAdd::get_data() const receipts.push_back(list_receipts->item(row)->text().toStdString()); } - Dialogdata data; + Database::data data; data.user = text_user->text().toStdString(); data.blocked = radio_blocked->isChecked(); data.reason = text_reason->text().toStdString(); @@ -447,7 +440,7 @@ const Dialogdata DialogAdd::get_data() const return data; } -void DialogAdd::set_data(const Dialogdata &data) +void DialogAdd::set_data(const Database::data &data) { text_user->setText(QString::fromStdString(data.user)); radio_blocked->setChecked(data.blocked); @@ -484,19 +477,19 @@ void DialogAdd::accept() { _parent->remove(); } - Dialogdata data = get_data(); + Database::data data = get_data(); if (data.user.empty()) { return; } - database::add_block(data.user, data.blocked, data.reason); + Database::add_user(data.user, data.blocked, data.reason); _parent->add_row(QString::fromStdString(data.user), data.blocked, QString::fromStdString(data.reason)); for (const string &receipt : data.receipts) { - database::add_receipt(data.user, receipt); + Database::add_receipt(data.user, receipt); } delete this; diff --git a/src/interface_qt.hpp b/src/interface_qt.hpp index 2acb99d..726c0f2 100644 --- a/src/interface_qt.hpp +++ b/src/interface_qt.hpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -30,14 +31,15 @@ #include "ui_whyblocked_add.h" using std::string; +using std::vector; -struct Dialogdata -{ - string user = ""; - bool blocked = true; - string reason = ""; - std::vector receipts = {}; -}; +// struct Dialogdata +// { +// string user = ""; +// bool blocked = true; +// string reason = ""; +// std::vector receipts = {}; +// }; class MainWindow : public QMainWindow, private Ui::MainWindow { @@ -61,13 +63,14 @@ private: QStandardItemModel *_model; xdgcfg _config; std::array _headersize; + Database _database; private slots: void add(); void edit(); void about(); void show_details(QModelIndex index); - void populate_tableview(const result_view &entries); + void populate_tableview(const vector &entries); void reload(); void find(); @@ -79,10 +82,10 @@ class DialogAdd : public QDialog, private Ui::DialogAdd public: explicit DialogAdd(QMainWindow *parent = nullptr); - void set_data(const Dialogdata &data); + void set_data(const Database::data &data); private: - const Dialogdata get_data() const; + const Database::data get_data() const; void dragEnterEvent(QDragEnterEvent *event); void dropEvent(QDropEvent *event); diff --git a/src/whyblocked.cpp b/src/whyblocked.cpp index 7766024..6439adc 100644 --- a/src/whyblocked.cpp +++ b/src/whyblocked.cpp @@ -35,7 +35,7 @@ Database::data::operator bool() const return !user.empty(); } -const string Database::get_filepath() const +const string Database::get_filepath() { fs::path filepath; xdgHandle xdg; @@ -60,14 +60,19 @@ const string Database::get_filepath() const return filepath; } -bool Database::add_user(const string &user, const int blocked, +bool Database::add_user(const string &user, const bool blocked, const string &reason) { try { + int blocked_int = 0; + if (blocked) + { + blocked_int = 1; + } sqlite::connection con(get_filepath()); sqlite::execute ins(con, "INSERT INTO blocks VALUES(?, ?, ?);"); - ins % user % blocked % reason; + ins % user % blocked_int % reason; ins(); } catch (const std::exception &e) @@ -118,7 +123,7 @@ bool Database::remove(const string &user) return true; } -const vector Database::query(const string &sql_query) const +const vector Database::query(const string &sql_query) { try { @@ -132,6 +137,11 @@ const vector Database::query(const string &sql_query) const const string user = res_blocks->get_string(0); const int blocked = res_blocks->get_int(1); const string reason = res_blocks->get_string(2); + bool blocked_bool = false; + if (blocked == 1) + { + blocked_bool = true; + } sqlite::query q_urls(con, "SELECT * FROM urls WHERE user = \'" + user + "\';"); @@ -145,7 +155,7 @@ const vector Database::query(const string &sql_query) const result.push_back( { user, - blocked, + blocked_bool, reason, receipts }); diff --git a/src/whyblocked.hpp b/src/whyblocked.hpp index d5be2f7..95634e1 100644 --- a/src/whyblocked.hpp +++ b/src/whyblocked.hpp @@ -29,23 +29,24 @@ class Database public: struct data { - const string user; - const int blocked; - const string reason; - const vector receipts; + string user; + bool blocked; + string reason; + vector receipts; explicit operator bool() const; }; Database(); - bool add_user(const string &user, const int blocked, const string &reason); - bool add_receipt(const string &user, const string &receipt); - bool remove(const string &user); - const vector query(const string &sql_query = "SELECT * FROM blocks;") - const; + static bool add_user(const string &user, const bool blocked, + const string &reason); + static bool add_receipt(const string &user, const string &receipt); + static bool remove(const string &user); + static const vector query(const string &sql_query = + "SELECT * FROM blocks;"); private: - const string get_filepath() const; + static const string get_filepath(); }; #endif // WHYBLOCKED_HPP diff --git a/translations/whyblocked_de.ts b/translations/whyblocked_de.ts index af104dc..e160fff 100644 --- a/translations/whyblocked_de.ts +++ b/translations/whyblocked_de.ts @@ -74,7 +74,7 @@ Du kannst URLs hier hineinziehen - + Insert receipt here. Beleg hier einfügen. @@ -198,7 +198,7 @@ - + Edit entry Eintrag bearbeiten @@ -219,68 +219,68 @@ - + User/Instance Benutzer/Instanz - + Blocked/Silenced Blockiert/Gedämpft - + Reason Begründung - + Try dragging an account from your webbrowser into this window. Versuche, einen account von deinem webbrowser in dieses fenster zu ziehen. - + blocked blockiert - + silenced gedämpft - + Invalid selection Ungültige Auswahl - + Please select only 1 entry to edit. Bitte nur 1 Eintrag zum bearbeiten auswählen. - + Nothing selected Nichts ausgewählt - + Please select entries to remove. Bitte wähle einträge aus, die gelöscht werden sollen. - + About Whyblocked Über Whyblocked - + <p><b>Whyblocked</b> %1</p><p>Reminds you why you blocked someone.</p><p>Sourcecode: <a href="https://schlomp.space/tastytea/whyblocked">https://schlomp.space/tastytea/whyblocked</a></p><p><small>Copyright © 2018 <a href="mailto:tastytea@tastytea.de">tastytea</a>.<br>Licence GPLv3: <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU GPL version 3</a>.<br>This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.</small></p> <p><b>Whyblocked</b> %1</p><p>Erinnert dich, warum du jemanden blockiertest.</p><p>Quelltext: <a href="https://schlomp.space/tastytea/whyblocked">https://schlomp.space/tastytea/whyblocked</a></p><p><small>Copyright © 2018 <a href="mailto:tastytea@tastytea.de">tastytea</a>.<br>Lizenz GPLv3: <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU GPL version 3</a>.<br>Für dieses Programm besteht KEINERLEI GARANTIE. Dies ist freie Software, die Sie unter bestimmten Bedingungen weitergeben dürfen.</small></p> - + Receipts: Belege: diff --git a/translations/whyblocked_en.ts b/translations/whyblocked_en.ts index 354e62c..8c726b2 100644 --- a/translations/whyblocked_en.ts +++ b/translations/whyblocked_en.ts @@ -74,7 +74,7 @@ - + Insert receipt here. @@ -198,7 +198,7 @@ - + Edit entry @@ -219,68 +219,68 @@ - + User/Instance - + Blocked/Silenced - + Reason - + Try dragging an account from your webbrowser into this window. - + blocked - + silenced - + Invalid selection - + Please select only 1 entry to edit. - + Nothing selected - + Please select entries to remove. - + About Whyblocked - + <p><b>Whyblocked</b> %1</p><p>Reminds you why you blocked someone.</p><p>Sourcecode: <a href="https://schlomp.space/tastytea/whyblocked">https://schlomp.space/tastytea/whyblocked</a></p><p><small>Copyright © 2018 <a href="mailto:tastytea@tastytea.de">tastytea</a>.<br>Licence GPLv3: <a href="https://www.gnu.org/licenses/gpl-3.0.html">GNU GPL version 3</a>.<br>This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.</small></p> - + Receipts: