Rewrote QT interface to use new database interface.
All checks were successful
the build was successful

This commit is contained in:
tastytea 2019-01-12 19:35:24 +01:00
parent 75dc40406c
commit 0f13bc8d9b
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
7 changed files with 107 additions and 100 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 3.2)
project (whyblocked
VERSION 0.13.1
VERSION 0.14.0
LANGUAGES CXX
)

View File

@ -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<Database::data> &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<string, int, string> &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<Database::data> 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<Database::data> entries = _database.query();
vector<Database::data> filtered_entries;
if (!entries.empty())
{
for (const std::tuple<string, int, string> &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<std::codecvt_utf8_utf16<wchar_t>> 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<Database::data> 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("<b>") + tr("Receipts:").toStdString() + "</b>";
for (const string &url : std::get<2>(result))
for (const string &url : dbdata.front().receipts)
{
text += "<br>" + 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<string> 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;

View File

@ -20,6 +20,7 @@
#include <string>
#include <memory>
#include <array>
#include <vector>
#include <QMainWindow>
#include <QStandardItemModel>
#include <QDialog>
@ -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<string> receipts = {};
};
// struct Dialogdata
// {
// string user = "";
// bool blocked = true;
// string reason = "";
// std::vector<string> receipts = {};
// };
class MainWindow : public QMainWindow, private Ui::MainWindow
{
@ -61,13 +63,14 @@ private:
QStandardItemModel *_model;
xdgcfg _config;
std::array<int, 3> _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<Database::data> &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);

View File

@ -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::data> Database::query(const string &sql_query) const
const vector<Database::data> Database::query(const string &sql_query)
{
try
{
@ -132,6 +137,11 @@ const vector<Database::data> 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::data> Database::query(const string &sql_query) const
result.push_back(
{
user,
blocked,
blocked_bool,
reason,
receipts
});

View File

@ -29,23 +29,24 @@ class Database
public:
struct data
{
const string user;
const int blocked;
const string reason;
const vector<string> receipts;
string user;
bool blocked;
string reason;
vector<string> 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<data> 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<data> query(const string &sql_query =
"SELECT * FROM blocks;");
private:
const string get_filepath() const;
static const string get_filepath();
};
#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="467"/>
<location filename="../src/interface_qt.cpp" line="460"/>
<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="239"/>
<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="184"/>
<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="185"/>
<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="186"/>
<source>Reason</source>
<translation>Begründung</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="109"/>
<location filename="../src/interface_qt.cpp" line="110"/>
<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="213"/>
<location filename="../src/interface_qt.cpp" line="214"/>
<source>blocked</source>
<translation>blockiert</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="217"/>
<location filename="../src/interface_qt.cpp" line="218"/>
<source>silenced</source>
<translation>gedämpft</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="233"/>
<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="234"/>
<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="277"/>
<location filename="../src/interface_qt.cpp" line="272"/>
<source>Nothing selected</source>
<translation>Nichts ausgewählt</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="278"/>
<location filename="../src/interface_qt.cpp" line="273"/>
<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="346"/>
<location filename="../src/interface_qt.cpp" line="338"/>
<source>About Whyblocked</source>
<translation>Über Whyblocked</translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="347"/>
<location filename="../src/interface_qt.cpp" line="339"/>
<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="370"/>
<location filename="../src/interface_qt.cpp" line="363"/>
<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="467"/>
<location filename="../src/interface_qt.cpp" line="460"/>
<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="239"/>
<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="184"/>
<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="185"/>
<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="186"/>
<source>Reason</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="109"/>
<location filename="../src/interface_qt.cpp" line="110"/>
<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="213"/>
<location filename="../src/interface_qt.cpp" line="214"/>
<source>blocked</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="217"/>
<location filename="../src/interface_qt.cpp" line="218"/>
<source>silenced</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="233"/>
<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="234"/>
<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="277"/>
<location filename="../src/interface_qt.cpp" line="272"/>
<source>Nothing selected</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="278"/>
<location filename="../src/interface_qt.cpp" line="273"/>
<source>Please select entries to remove.</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="346"/>
<location filename="../src/interface_qt.cpp" line="338"/>
<source>About Whyblocked</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../src/interface_qt.cpp" line="347"/>
<location filename="../src/interface_qt.cpp" line="339"/>
<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="370"/>
<location filename="../src/interface_qt.cpp" line="363"/>
<source>Receipts:</source>
<translation type="unfinished"></translation>
</message>