From 0fdaad70ee7f1c291d7c014315436e0fe8de65f0 Mon Sep 17 00:00:00 2001 From: tastytea Date: Sat, 12 Jan 2019 20:37:43 +0100 Subject: [PATCH] Only reload database on start and when the reload button is clicked. --- src/interface_qt.cpp | 61 +++++++++++++++++------------------ src/interface_qt.hpp | 2 +- src/whyblocked.cpp | 2 -- src/whyblocked.hpp | 1 - translations/whyblocked_de.ts | 30 ++++++++--------- translations/whyblocked_en.ts | 30 ++++++++--------- 6 files changed, 61 insertions(+), 65 deletions(-) diff --git a/src/interface_qt.cpp b/src/interface_qt.cpp index 0207924..bc92355 100644 --- a/src/interface_qt.cpp +++ b/src/interface_qt.cpp @@ -33,7 +33,6 @@ MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent) , _config("whyblocked.cfg") , _headersize({ 250, 125, 125 }) -, _database() { std::locale::global(std::locale("")); @@ -199,9 +198,8 @@ void MainWindow::populate_tableview(const vector &entries) void MainWindow::reload() { - vector entries; - entries = _database.query(); - populate_tableview(entries); + _dbdata = Database::query(); + populate_tableview(_dbdata); } void MainWindow::add_row(const QString &user, const int &blocked, @@ -243,13 +241,17 @@ void MainWindow::edit() const string user = index.sibling(index.row(), 0).data() .toString().toStdString(); - Database::data dbdata = - _database.query("SELECT * FROM blocks WHERE user = '" + - user + "';").front(); - dbdata.reason = dbdata.reason; - dbdata.receipts = dbdata.receipts; + Database::data data; + for (const Database::data &entry : _dbdata) + { + if (entry.user == user) + { + data = entry; + break; + } + } - dialog->set_data(dbdata); + dialog->set_data(data); dialog->setProperty("edit", true); dialog->show(); } @@ -262,7 +264,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(); @@ -292,17 +294,10 @@ bool MainWindow::eventFilter(QObject *obj, QEvent *event) if (obj == text_find && (event->type() == QEvent::KeyRelease || event->type() == QEvent::Enter)) { - string columns; - if (check_user->isChecked()) - { - columns = "user"; - } - - vector entries = _database.query(); vector filtered_entries; - if (!entries.empty()) + if (!_dbdata.empty()) { - for (const Database::data &entry : entries) + for (const Database::data &entry : _dbdata) { wstring searchstring; @@ -352,23 +347,27 @@ void MainWindow::show_details(QModelIndex index) { const string user = index.sibling(index.row(), 0).data() .toString().toStdString(); - vector dbdata = - _database.query("SELECT * FROM blocks WHERE user = '" + user + "';"); + Database::data data; + for (const Database::data &entry : _dbdata) + { + if (entry.user == user) + { + data = entry; + break; + } + } string text = ""; - if (!dbdata.empty()) + if (!data.receipts.empty()) { - if (!dbdata.front().receipts.empty()) + text += string("") + tr("Receipts:").toStdString() + ""; + for (const string &url : data.receipts) { - text += string("") + tr("Receipts:").toStdString() + ""; - for (const string &url : dbdata.front().receipts) - { - text += "
" + url; - } - text = urls_to_hyperlinks(text); + text += "
" + url; } - label_receipts->setText(QString::fromStdString((text))); + text = urls_to_hyperlinks(text); } + label_receipts->setText(QString::fromStdString((text))); } const string MainWindow::urls_to_hyperlinks(const string &text) diff --git a/src/interface_qt.hpp b/src/interface_qt.hpp index 726c0f2..6918edf 100644 --- a/src/interface_qt.hpp +++ b/src/interface_qt.hpp @@ -63,7 +63,7 @@ private: QStandardItemModel *_model; xdgcfg _config; std::array _headersize; - Database _database; + std::vector _dbdata; private slots: void add(); diff --git a/src/whyblocked.cpp b/src/whyblocked.cpp index 6439adc..c2a2a30 100644 --- a/src/whyblocked.cpp +++ b/src/whyblocked.cpp @@ -28,8 +28,6 @@ using std::cerr; namespace fs = std::experimental::filesystem; -Database::Database() {}; - Database::data::operator bool() const { return !user.empty(); diff --git a/src/whyblocked.hpp b/src/whyblocked.hpp index 95634e1..5f4e5e1 100644 --- a/src/whyblocked.hpp +++ b/src/whyblocked.hpp @@ -37,7 +37,6 @@ public: explicit operator bool() const; }; - Database(); static bool add_user(const string &user, const bool blocked, const string &reason); static bool add_receipt(const string &user, const string &receipt); diff --git a/translations/whyblocked_de.ts b/translations/whyblocked_de.ts index e160fff..81d1914 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 8c726b2..098edae 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: