Let the Database-class handle the data.

develop
tastytea 2019-01-12 21:24:09 +01:00
parent 0fdaad70ee
commit d9065280ea
No known key found for this signature in database
GPG Key ID: CFC39497F1B26E07
6 changed files with 110 additions and 107 deletions

View File

@ -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<Database::data> &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<MainWindow*>(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;
}

View File

@ -33,14 +33,6 @@
using std::string;
using std::vector;
// struct Dialogdata
// {
// string user = "";
// bool blocked = true;
// string reason = "";
// std::vector<string> receipts = {};
// };
class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT
@ -63,7 +55,8 @@ private:
QStandardItemModel *_model;
xdgcfg _config;
std::array<int, 3> _headersize;
std::vector<Database::data> _dbdata;
Database _database;
std::vector<Database::data> &_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();

View File

@ -17,7 +17,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <tuple>
#include <iterator>
#include <experimental/filesystem>
#include <basedir.h>
#include <sqlite/connection.hpp>
@ -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::data> Database::query(const string &sql_query)
const vector<Database::data> Database::query(const string &sql_query) const
{
try
{
@ -167,3 +160,34 @@ const vector<Database::data> 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::data> &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 {};
}

View File

@ -18,7 +18,6 @@
#define WHYBLOCKED_HPP
#include <vector>
#include <tuple>
#include <string>
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<data> query(const string &sql_query =
"SELECT * FROM blocks;");
bool add_user(const data &userdata);
bool remove(const string &user);
const vector<data> query(const string &sql_query =
"SELECT * FROM blocks;") const;
bool reload();
std::vector<data> &get_data();
const data get_user(const string &user) const;
private:
static const string get_filepath();
std::vector<data> _data;
private:
const string get_filepath() const;
};
#endif // WHYBLOCKED_HPP

View File

@ -74,7 +74,7 @@
<translation>Du kannst URLs hier hineinziehen</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="465"/>
<location filename="../src/interface_qt.cpp" line="444"/>
<source>Insert receipt here.</source>
<translation>Beleg hier einfügen.</translation>
</message>
@ -198,7 +198,7 @@
</message>
<message>
<location filename="../src/whyblocked.ui" line="285"/>
<location filename="../src/interface_qt.cpp" line="238"/>
<location filename="../src/interface_qt.cpp" line="240"/>
<source>Edit entry</source>
<translation>Eintrag bearbeiten</translation>
</message>
@ -219,68 +219,68 @@
</message>
<message>
<location filename="../src/whyblocked.ui" line="59"/>
<location filename="../src/interface_qt.cpp" line="183"/>
<location filename="../src/interface_qt.cpp" line="185"/>
<source>User/Instance</source>
<translation>Benutzer/Instanz</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="184"/>
<location filename="../src/interface_qt.cpp" line="186"/>
<source>Blocked/Silenced</source>
<translation>Blockiert/Gedämpft</translation>
</message>
<message>
<location filename="../src/whyblocked.ui" line="72"/>
<location filename="../src/interface_qt.cpp" line="185"/>
<location filename="../src/interface_qt.cpp" line="187"/>
<source>Reason</source>
<translation>Begründung</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="109"/>
<location filename="../src/interface_qt.cpp" line="111"/>
<source>Try dragging an account from your webbrowser into this window.</source>
<translation>Versuche, einen account von deinem webbrowser in dieses fenster zu ziehen.</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="212"/>
<location filename="../src/interface_qt.cpp" line="214"/>
<source>blocked</source>
<translation>blockiert</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="216"/>
<location filename="../src/interface_qt.cpp" line="218"/>
<source>silenced</source>
<translation>gedämpft</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="232"/>
<location filename="../src/interface_qt.cpp" line="234"/>
<source>Invalid selection</source>
<translation>Ungültige Auswahl</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="233"/>
<location filename="../src/interface_qt.cpp" line="235"/>
<source>Please select only 1 entry to edit.</source>
<translation>Bitte nur 1 Eintrag zum bearbeiten auswählen.</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="274"/>
<location filename="../src/interface_qt.cpp" line="266"/>
<source>Nothing selected</source>
<translation>Nichts ausgewählt</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="275"/>
<location filename="../src/interface_qt.cpp" line="267"/>
<source>Please select entries to remove.</source>
<translation>Bitte wähle einträge aus, die gelöscht werden sollen.</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="339"/>
<location filename="../src/interface_qt.cpp" line="325"/>
<source>About Whyblocked</source>
<translation>Über Whyblocked</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="340"/>
<location filename="../src/interface_qt.cpp" line="326"/>
<source>&lt;p&gt;&lt;b&gt;Whyblocked&lt;/b&gt; %1&lt;/p&gt;&lt;p&gt;Reminds you why you blocked someone.&lt;/p&gt;&lt;p&gt;Sourcecode: &lt;a href=&quot;https://schlomp.space/tastytea/whyblocked&quot;&gt;https://schlomp.space/tastytea/whyblocked&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;small&gt;Copyright © 2018 &lt;a href=&quot;mailto:tastytea@tastytea.de&quot;&gt;tastytea&lt;/a&gt;.&lt;br&gt;Licence GPLv3: &lt;a href=&quot;https://www.gnu.org/licenses/gpl-3.0.html&quot;&gt;GNU GPL version 3&lt;/a&gt;.&lt;br&gt;This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.&lt;/small&gt;&lt;/p&gt;</source>
<translation>&lt;p&gt;&lt;b&gt;Whyblocked&lt;/b&gt; %1&lt;/p&gt;&lt;p&gt;Erinnert dich, warum du jemanden blockiertest.&lt;/p&gt;&lt;p&gt;Quelltext: &lt;a href=&quot;https://schlomp.space/tastytea/whyblocked&quot;&gt;https://schlomp.space/tastytea/whyblocked&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;small&gt;Copyright © 2018 &lt;a href=&quot;mailto:tastytea@tastytea.de&quot;&gt;tastytea&lt;/a&gt;.&lt;br&gt;Lizenz GPLv3: &lt;a href=&quot;https://www.gnu.org/licenses/gpl-3.0.html&quot;&gt;GNU GPL version 3&lt;/a&gt;.&lt;br&gt;Für dieses Programm besteht KEINERLEI GARANTIE. Dies ist freie Software, die Sie unter bestimmten Bedingungen weitergeben dürfen.&lt;/small&gt;&lt;/p&gt;</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="369"/>
<location filename="../src/interface_qt.cpp" line="347"/>
<source>Receipts:</source>
<translation>Belege:</translation>
</message>

View File

@ -74,7 +74,7 @@
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="465"/>
<location filename="../src/interface_qt.cpp" line="444"/>
<source>Insert receipt here.</source>
<translation type="unfinished"></translation>
</message>
@ -198,7 +198,7 @@
</message>
<message>
<location filename="../src/whyblocked.ui" line="285"/>
<location filename="../src/interface_qt.cpp" line="238"/>
<location filename="../src/interface_qt.cpp" line="240"/>
<source>Edit entry</source>
<translation type="unfinished"></translation>
</message>
@ -219,68 +219,68 @@
</message>
<message>
<location filename="../src/whyblocked.ui" line="59"/>
<location filename="../src/interface_qt.cpp" line="183"/>
<location filename="../src/interface_qt.cpp" line="185"/>
<source>User/Instance</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="184"/>
<location filename="../src/interface_qt.cpp" line="186"/>
<source>Blocked/Silenced</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/whyblocked.ui" line="72"/>
<location filename="../src/interface_qt.cpp" line="185"/>
<location filename="../src/interface_qt.cpp" line="187"/>
<source>Reason</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="109"/>
<location filename="../src/interface_qt.cpp" line="111"/>
<source>Try dragging an account from your webbrowser into this window.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="212"/>
<location filename="../src/interface_qt.cpp" line="214"/>
<source>blocked</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="216"/>
<location filename="../src/interface_qt.cpp" line="218"/>
<source>silenced</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="232"/>
<location filename="../src/interface_qt.cpp" line="234"/>
<source>Invalid selection</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="233"/>
<location filename="../src/interface_qt.cpp" line="235"/>
<source>Please select only 1 entry to edit.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="274"/>
<location filename="../src/interface_qt.cpp" line="266"/>
<source>Nothing selected</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="275"/>
<location filename="../src/interface_qt.cpp" line="267"/>
<source>Please select entries to remove.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="339"/>
<location filename="../src/interface_qt.cpp" line="325"/>
<source>About Whyblocked</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="340"/>
<location filename="../src/interface_qt.cpp" line="326"/>
<source>&lt;p&gt;&lt;b&gt;Whyblocked&lt;/b&gt; %1&lt;/p&gt;&lt;p&gt;Reminds you why you blocked someone.&lt;/p&gt;&lt;p&gt;Sourcecode: &lt;a href=&quot;https://schlomp.space/tastytea/whyblocked&quot;&gt;https://schlomp.space/tastytea/whyblocked&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;small&gt;Copyright © 2018 &lt;a href=&quot;mailto:tastytea@tastytea.de&quot;&gt;tastytea&lt;/a&gt;.&lt;br&gt;Licence GPLv3: &lt;a href=&quot;https://www.gnu.org/licenses/gpl-3.0.html&quot;&gt;GNU GPL version 3&lt;/a&gt;.&lt;br&gt;This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions.&lt;/small&gt;&lt;/p&gt;</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="369"/>
<location filename="../src/interface_qt.cpp" line="347"/>
<source>Receipts:</source>
<translation type="unfinished"></translation>
</message>