diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index bc92355..71f394e 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -33,6 +33,8 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent) , _config("whyblocked.cfg") , _headersize({ 250, 125, 125 }) +, _database() +, _dbdata(_database.get_data()) { std::locale::global(std::locale("")); @@ -198,7 +200,7 @@ void MainWindow::populate_tableview(const vector &entries) void MainWindow::reload() { - _dbdata = Database::query(); + _database.reload(); populate_tableview(_dbdata); } @@ -221,7 +223,7 @@ void MainWindow::add_row(const QString &user, const int &blocked, void MainWindow::add() { - DialogAdd *dialog = new DialogAdd(this); + DialogAdd *dialog = new DialogAdd(_database, this); dialog->show(); } @@ -234,24 +236,14 @@ void MainWindow::edit() return; } - DialogAdd *dialog = new DialogAdd(this); + DialogAdd *dialog = new DialogAdd(_database, this); dialog->setWindowTitle(tr("Edit entry")); QModelIndex index = tableview->selectionModel()->selectedRows().first(); const string user = index.sibling(index.row(), 0).data() .toString().toStdString(); - Database::data data; - for (const Database::data &entry : _dbdata) - { - if (entry.user == user) - { - data = entry; - break; - } - } - - dialog->set_data(data); + dialog->set_data(_database.get_user(user)); dialog->setProperty("edit", true); dialog->show(); } @@ -264,7 +256,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(); @@ -347,15 +339,7 @@ void MainWindow::show_details(QModelIndex index) { const string user = index.sibling(index.row(), 0).data() .toString().toStdString(); - Database::data data; - for (const Database::data &entry : _dbdata) - { - if (entry.user == user) - { - data = entry; - break; - } - } + Database::data data = _database.get_user(user); string text = ""; if (!data.receipts.empty()) @@ -408,16 +392,17 @@ void MainWindow::dropEvent(QDropEvent *event) } } - DialogAdd *dialog = new DialogAdd(this); + DialogAdd *dialog = new DialogAdd(_database, this); Database::data data; data.user = text; dialog->set_data(data); dialog->show(); } -DialogAdd::DialogAdd(QMainWindow *parent) +DialogAdd::DialogAdd(Database &database, QMainWindow *parent) : QDialog(parent) , _parent(static_cast(parent)) +, _database(database) { setupUi(this); } @@ -478,18 +463,15 @@ void DialogAdd::accept() } Database::data data = get_data(); - if (data.user.empty()) + if (!data) { return; } - Database::add_user(data.user, data.blocked, data.reason); + + _database.add_user(data); _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); - } delete this; } diff --git a/src/interface_qt.hpp b/src/interface_qt.hpp index 6918edf..40a2b51 100644 --- a/src/interface_qt.hpp +++ b/src/interface_qt.hpp @@ -33,14 +33,6 @@ using std::string; using std::vector; -// struct Dialogdata -// { -// string user = ""; -// bool blocked = true; -// string reason = ""; -// std::vector receipts = {}; -// }; - class MainWindow : public QMainWindow, private Ui::MainWindow { Q_OBJECT @@ -63,7 +55,8 @@ private: QStandardItemModel *_model; xdgcfg _config; std::array _headersize; - std::vector _dbdata; + Database _database; + std::vector &_dbdata; private slots: void add(); @@ -81,7 +74,7 @@ class DialogAdd : public QDialog, private Ui::DialogAdd Q_OBJECT public: - explicit DialogAdd(QMainWindow *parent = nullptr); + explicit DialogAdd(Database &database, QMainWindow *parent = nullptr); void set_data(const Database::data &data); private: @@ -90,6 +83,7 @@ private: void dropEvent(QDropEvent *event); MainWindow *_parent; + Database &_database; private slots: void add_receipt(); diff --git a/src/whyblocked.cpp b/src/whyblocked.cpp index c2a2a30..41b5835 100644 --- a/src/whyblocked.cpp +++ b/src/whyblocked.cpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include #include @@ -33,7 +33,7 @@ Database::data::operator bool() const return !user.empty(); } -const string Database::get_filepath() +const string Database::get_filepath() const { fs::path filepath; xdgHandle xdg; @@ -58,38 +58,30 @@ const string Database::get_filepath() return filepath; } -bool Database::add_user(const string &user, const bool blocked, - const string &reason) +bool Database::add_user(const Database::data &userdata) { try { int blocked_int = 0; - if (blocked) + if (userdata.blocked) { blocked_int = 1; } sqlite::connection con(get_filepath()); - sqlite::execute ins(con, "INSERT INTO blocks VALUES(?, ?, ?);"); - ins % user % blocked_int % reason; - ins(); - } - catch (const std::exception &e) - { - cerr << "An error occurred: " << e.what() << std::endl; - return false; - } - - return true; -} - -bool Database::add_receipt(const string &user, const string &receipt) -{ - try - { - sqlite::connection con(get_filepath()); - sqlite::execute ins(con, "INSERT INTO urls VALUES(?, ?);"); - ins % user % receipt; - ins(); + { + sqlite::execute ins(con, "INSERT INTO blocks VALUES(?, ?, ?);"); + ins % userdata.user % blocked_int % userdata.reason; + ins(); + } + { + for (const string &receipt : userdata.receipts) + { + sqlite::execute ins(con, "INSERT INTO urls VALUES(?, ?);"); + ins % userdata.user % receipt; + ins(); + } + } + _data.push_back(userdata); } catch (const std::exception &e) { @@ -111,6 +103,7 @@ bool Database::remove(const string &user) rm_urls % user; rm_blocks(); rm_urls(); + reload(); } catch (const std::exception &e) { @@ -121,7 +114,7 @@ bool Database::remove(const string &user) return true; } -const vector Database::query(const string &sql_query) +const vector Database::query(const string &sql_query) const { try { @@ -167,3 +160,34 @@ const vector Database::query(const string &sql_query) return {}; } } + +bool Database::reload() +{ + auto buffer = query(); + if (buffer.empty()) + { + return false; + } + else + { + _data = std::move(buffer); + return true; + } +} + +std::vector &Database::get_data() +{ + return _data; +} + +const Database::data Database::get_user(const string &user) const +{ + for (const Database::data &entry : _data) + { + if (entry.user == user) + { + return entry; + } + } + return {}; +} diff --git a/src/whyblocked.hpp b/src/whyblocked.hpp index 5f4e5e1..6872a2a 100644 --- a/src/whyblocked.hpp +++ b/src/whyblocked.hpp @@ -18,7 +18,6 @@ #define WHYBLOCKED_HPP #include -#include #include using std::string; @@ -37,15 +36,19 @@ public: explicit operator bool() 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;"); + bool add_user(const data &userdata); + bool remove(const string &user); + const vector query(const string &sql_query = + "SELECT * FROM blocks;") const; + bool reload(); + std::vector &get_data(); + const data get_user(const string &user) const; private: - static const string get_filepath(); + std::vector _data; + +private: + const string get_filepath() const; }; #endif // WHYBLOCKED_HPP diff --git a/translations/whyblocked_de.ts b/translations/whyblocked_de.ts index 81d1914..8619fdb 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 098edae..cfa23c4 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: