2018-10-06 21:33:02 +02:00
|
|
|
/* This file is part of whyblocked.
|
2019-01-12 17:39:20 +01:00
|
|
|
* Copyright © 2018, 2019 tastytea <tastytea@tastytea.de>
|
2018-10-06 21:33:02 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-10-07 01:40:04 +02:00
|
|
|
#include <regex>
|
2018-11-02 11:54:40 +01:00
|
|
|
#include <array>
|
2019-01-11 20:50:22 +01:00
|
|
|
#include <locale>
|
|
|
|
#include <codecvt>
|
|
|
|
#include <algorithm>
|
2018-10-06 21:33:02 +02:00
|
|
|
#include <QMessageBox>
|
2018-11-02 11:54:40 +01:00
|
|
|
#include <QtCore/qmimedata.h>
|
2018-11-03 12:59:49 +01:00
|
|
|
#include <libconfig.h++>
|
2018-10-07 01:40:04 +02:00
|
|
|
#include "version.hpp"
|
2019-01-14 15:45:05 +01:00
|
|
|
#include "mainwindow.hpp"
|
|
|
|
#include "dialog_add.hpp"
|
2018-10-06 21:33:02 +02:00
|
|
|
|
2019-01-11 20:50:22 +01:00
|
|
|
using std::wstring;
|
|
|
|
|
2018-11-03 12:59:49 +01:00
|
|
|
MainWindow::MainWindow(QMainWindow *parent)
|
|
|
|
: QMainWindow(parent)
|
|
|
|
, _config("whyblocked.cfg")
|
|
|
|
, _headersize({ 250, 125, 125 })
|
2019-01-12 21:24:09 +01:00
|
|
|
, _database()
|
|
|
|
, _dbdata(_database.get_data())
|
2018-10-06 21:33:02 +02:00
|
|
|
{
|
2019-01-11 20:50:22 +01:00
|
|
|
std::locale::global(std::locale(""));
|
|
|
|
|
2018-10-06 21:33:02 +02:00
|
|
|
setupUi(this);
|
2018-10-13 05:07:34 +02:00
|
|
|
|
|
|
|
_model = new QStandardItemModel;
|
|
|
|
tableview->setModel(_model);
|
2018-11-03 12:59:49 +01:00
|
|
|
|
|
|
|
if (_config.read() == 0)
|
|
|
|
{
|
|
|
|
libconfig::Setting &root = _config.get_cfg().getRoot();
|
|
|
|
string key;
|
|
|
|
|
|
|
|
key = "size";
|
|
|
|
if (root.exists(key) && root[key.c_str()].isArray())
|
|
|
|
{
|
|
|
|
this->resize(root[key.c_str()][0], root[key.c_str()][1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = "toolbar_position";
|
|
|
|
if (root.exists(key))
|
|
|
|
{
|
|
|
|
const string value = root[key.c_str()].c_str();
|
|
|
|
if (value == "top")
|
|
|
|
{
|
|
|
|
this->removeToolBar(toolbar);
|
|
|
|
this->addToolBar(Qt::TopToolBarArea, toolbar);
|
|
|
|
}
|
|
|
|
else if (value == "right")
|
|
|
|
{
|
|
|
|
this->removeToolBar(toolbar);
|
|
|
|
this->addToolBar(Qt::RightToolBarArea, toolbar);
|
|
|
|
}
|
|
|
|
else if (value == "bottom")
|
|
|
|
{
|
|
|
|
this->removeToolBar(toolbar);
|
|
|
|
this->addToolBar(Qt::BottomToolBarArea, toolbar);
|
|
|
|
}
|
|
|
|
else if (value == "left")
|
|
|
|
{
|
|
|
|
this->removeToolBar(toolbar);
|
|
|
|
this->addToolBar(Qt::LeftToolBarArea, toolbar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
key = "toolbar_visible";
|
|
|
|
if (root.exists(key))
|
|
|
|
{
|
|
|
|
toolbar->setVisible(root[key.c_str()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
key = "table_headers";
|
|
|
|
if (root.exists(key) && root[key.c_str()].isArray())
|
|
|
|
{
|
|
|
|
const libconfig::Setting &value = root[key.c_str()];
|
|
|
|
_headersize = { value[0], value[1], value[2] };
|
|
|
|
}
|
2019-01-11 18:58:20 +01:00
|
|
|
|
2019-01-11 21:13:45 +01:00
|
|
|
key = "find_in";
|
|
|
|
if (root.exists(key) && root[key.c_str()].isGroup())
|
|
|
|
{
|
|
|
|
const libconfig::Setting &value = root[key.c_str()];
|
|
|
|
check_user->setChecked(value["user"]);
|
|
|
|
check_reason->setChecked(value["reason"]);
|
|
|
|
}
|
|
|
|
|
2018-11-03 12:59:49 +01:00
|
|
|
}
|
|
|
|
|
2019-01-11 18:58:20 +01:00
|
|
|
widget_find->hide();
|
|
|
|
|
2019-01-11 20:50:22 +01:00
|
|
|
reload();
|
|
|
|
|
2018-12-06 15:32:28 +01:00
|
|
|
statusBar()->showMessage(tr("Try dragging an account from your webbrowser "
|
|
|
|
"into this window."));
|
2018-10-06 21:33:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-03 12:59:49 +01:00
|
|
|
MainWindow::~MainWindow()
|
|
|
|
{
|
|
|
|
libconfig::Setting &root = _config.get_cfg().getRoot();
|
|
|
|
// We can't add an element that already exists, so we delete it beforehand.
|
|
|
|
for (const string &key :
|
2019-01-11 21:13:45 +01:00
|
|
|
{ "size", "toolbar_position", "toolbar_visible", "table_headers",
|
|
|
|
"find_in" })
|
2018-11-03 12:59:49 +01:00
|
|
|
{
|
|
|
|
if (root.exists(key))
|
|
|
|
{
|
|
|
|
root.remove(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
libconfig::Setting &size = root.add("size", libconfig::Setting::TypeArray);
|
|
|
|
size.add(libconfig::Setting::TypeInt) = this->width();
|
|
|
|
size.add(libconfig::Setting::TypeInt) = this->height();
|
|
|
|
|
|
|
|
libconfig::Setting &pos = root.add("toolbar_position",
|
|
|
|
libconfig::Setting::TypeString);
|
|
|
|
if (toolbar->orientation() == Qt::Orientation::Horizontal)
|
|
|
|
{
|
|
|
|
if (toolbar-> geometry().top() < 100)
|
|
|
|
{
|
|
|
|
pos = "top";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = "bottom";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (toolbar->geometry().left() == 0)
|
|
|
|
{
|
|
|
|
pos = "left";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = "right";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
root.add("toolbar_visible", libconfig::Setting::TypeBoolean)
|
|
|
|
= !toolbar->isHidden();
|
|
|
|
|
|
|
|
libconfig::Setting &headers = root.add("table_headers",
|
|
|
|
libconfig::Setting::TypeArray);
|
|
|
|
headers.add(libconfig::Setting::TypeInt) =
|
|
|
|
tableview->horizontalHeader()->sectionSize(0);
|
|
|
|
headers.add(libconfig::Setting::TypeInt) =
|
|
|
|
tableview->horizontalHeader()->sectionSize(1);
|
2019-01-14 20:50:14 +01:00
|
|
|
headers.add(libconfig::Setting::TypeInt) = 125;
|
2018-11-03 12:59:49 +01:00
|
|
|
|
2019-01-11 21:13:45 +01:00
|
|
|
libconfig::Setting &find_in = root.add("find_in",
|
|
|
|
libconfig::Setting::TypeGroup);
|
|
|
|
find_in.add("user", libconfig::Setting::TypeBoolean) =
|
|
|
|
check_user->isChecked();
|
|
|
|
find_in.add("reason", libconfig::Setting::TypeBoolean) =
|
|
|
|
check_reason->isChecked();
|
|
|
|
|
2018-11-03 12:59:49 +01:00
|
|
|
_config.write();
|
|
|
|
}
|
|
|
|
|
2018-10-23 08:29:07 +02:00
|
|
|
void MainWindow::add_row(const QString &user, const int &blocked,
|
|
|
|
const QString &reason)
|
2018-10-06 21:33:02 +02:00
|
|
|
{
|
2018-10-13 05:07:34 +02:00
|
|
|
QList<QStandardItem*> items;
|
|
|
|
items.append(new QStandardItem(user));
|
|
|
|
if (blocked == 1)
|
|
|
|
{
|
|
|
|
items.append(new QStandardItem(QString(tr("blocked"))));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
items.append(new QStandardItem(QString(tr("silenced"))));
|
|
|
|
}
|
|
|
|
items.append(new QStandardItem(reason));
|
|
|
|
_model->appendRow(items);
|
|
|
|
}
|
2018-10-06 21:33:02 +02:00
|
|
|
|
|
|
|
void MainWindow::remove()
|
|
|
|
{
|
2018-10-13 05:07:34 +02:00
|
|
|
QItemSelectionModel *selection = tableview->selectionModel();
|
|
|
|
if (selection->hasSelection())
|
|
|
|
{
|
2019-01-15 01:37:55 +01:00
|
|
|
// I use this construct here because the number of selected rows
|
|
|
|
// decrease by 1 each iteration.
|
|
|
|
for (; selection->selectedRows().count() > 0;)
|
2018-10-13 05:07:34 +02:00
|
|
|
{
|
2019-01-15 01:37:55 +01:00
|
|
|
const QModelIndex row = selection->selectedRows().front();
|
2018-10-13 05:07:34 +02:00
|
|
|
const string user = row.data().toString().toStdString();
|
2019-01-12 21:24:09 +01:00
|
|
|
_database.remove(user);
|
2018-10-13 05:07:34 +02:00
|
|
|
_model->removeRow(row.row());
|
|
|
|
}
|
2018-10-14 01:26:22 +02:00
|
|
|
label_receipts->clear();
|
2018-10-13 05:07:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 15:32:28 +01:00
|
|
|
QMessageBox::warning(this, tr("Nothing selected"),
|
|
|
|
tr("Please select entries to remove."));
|
2018-10-13 05:07:34 +02:00
|
|
|
}
|
2018-10-06 21:33:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:45:05 +01:00
|
|
|
const string MainWindow::urls_to_hyperlinks(const string &text)
|
2019-01-11 18:58:20 +01:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
std::regex re_url("((https?|gopher|ftps?)\\://[^ <]*)");
|
|
|
|
return std::regex_replace(text, re_url, "<a href=\"$1\">$1</a>");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("text/plain"))
|
2019-01-11 18:58:20 +01:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
event->acceptProposedAction();
|
2019-01-11 18:58:20 +01:00
|
|
|
}
|
2019-01-14 15:45:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
string text = event->mimeData()->text().toStdString();
|
|
|
|
const std::array<const std::regex, 4> fediverse =
|
2019-01-11 18:58:20 +01:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
std::regex("https://([^/]+)/@([^/]+)"), // Mastodon
|
|
|
|
std::regex("https://([^/]+)/profile/([^/]+)"), // Friendica
|
|
|
|
std::regex("https://([^/]+)/users/([^/]+)"), // Pleroma
|
|
|
|
std::regex("https://([^/]+)/([^/]+)") // Gnusocial
|
|
|
|
};
|
|
|
|
std::smatch match;
|
|
|
|
|
|
|
|
for (const std::regex &re : fediverse)
|
|
|
|
{
|
|
|
|
std::regex_match(text, match, re);
|
|
|
|
const string instance = match[1];
|
|
|
|
const string user = match[2];
|
|
|
|
if (!instance.empty() && !user.empty())
|
|
|
|
{
|
|
|
|
text = '@' + user + '@' + instance;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-11 18:58:20 +01:00
|
|
|
}
|
2019-01-14 15:45:05 +01:00
|
|
|
|
|
|
|
DialogAdd *dialog = new DialogAdd(_database, this);
|
|
|
|
Database::data data;
|
|
|
|
data.user = text;
|
2019-01-16 15:49:36 +01:00
|
|
|
data.blocked = true;
|
2019-01-14 15:45:05 +01:00
|
|
|
dialog->set_data(data);
|
|
|
|
dialog->show();
|
2019-01-11 18:58:20 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:45:05 +01:00
|
|
|
void MainWindow::add()
|
|
|
|
{
|
|
|
|
DialogAdd *dialog = new DialogAdd(_database, this);
|
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::edit()
|
|
|
|
{
|
|
|
|
if (tableview->selectionModel()->selectedRows().count() != 1)
|
|
|
|
{
|
|
|
|
QMessageBox::warning(this, tr("Invalid selection"),
|
|
|
|
tr("Please select only 1 entry to edit."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
dialog->set_data(_database.get_user(user));
|
|
|
|
dialog->setProperty("edit", true);
|
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
2018-10-06 21:33:02 +02:00
|
|
|
void MainWindow::about()
|
|
|
|
{
|
2018-10-18 00:18:11 +02:00
|
|
|
QMessageBox::about(this, tr("About Whyblocked"),
|
|
|
|
tr("<p><b>Whyblocked</b> %1</p>"
|
2018-10-07 01:40:04 +02:00
|
|
|
"<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>"
|
2018-10-16 22:37:11 +02:00
|
|
|
"This program comes with ABSOLUTELY NO WARRANTY. This is free software, "
|
2018-10-07 01:40:04 +02:00
|
|
|
"and you are welcome to redistribute it under certain conditions.</small></p>")
|
|
|
|
.arg(global::version));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::show_details(QModelIndex index)
|
|
|
|
{
|
2018-10-23 08:29:07 +02:00
|
|
|
const string user = index.sibling(index.row(), 0).data()
|
|
|
|
.toString().toStdString();
|
2019-01-12 21:24:09 +01:00
|
|
|
Database::data data = _database.get_user(user);
|
2018-10-07 01:40:04 +02:00
|
|
|
string text = "";
|
|
|
|
|
2019-01-12 20:37:43 +01:00
|
|
|
if (!data.receipts.empty())
|
2018-10-07 01:40:04 +02:00
|
|
|
{
|
2019-01-12 20:37:43 +01:00
|
|
|
text += string("<b>") + tr("Receipts:").toStdString() + "</b>";
|
|
|
|
for (const string &url : data.receipts)
|
2018-10-07 01:40:04 +02:00
|
|
|
{
|
2019-01-12 20:37:43 +01:00
|
|
|
text += "<br>" + url;
|
2018-10-07 01:40:04 +02:00
|
|
|
}
|
2019-01-12 20:37:43 +01:00
|
|
|
text = urls_to_hyperlinks(text);
|
2018-10-07 01:40:04 +02:00
|
|
|
}
|
2019-01-12 20:37:43 +01:00
|
|
|
label_receipts->setText(QString::fromStdString((text)));
|
2018-10-07 01:40:04 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:45:05 +01:00
|
|
|
void MainWindow::populate_tableview(const vector<Database::data> &entries)
|
2018-10-13 05:07:34 +02:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
_model->clear();
|
|
|
|
_model->setHorizontalHeaderLabels(
|
2018-10-14 01:26:22 +02:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
tr("User/Instance"),
|
|
|
|
tr("Blocked/Silenced"),
|
|
|
|
tr("Reason")
|
|
|
|
});
|
|
|
|
tableview->horizontalHeader()->resizeSection(0, _headersize[0]);
|
|
|
|
tableview->horizontalHeader()->resizeSection(1, _headersize[1]);
|
|
|
|
tableview->horizontalHeader()->resizeSection(2, _headersize[2]);
|
2018-10-17 20:44:58 +02:00
|
|
|
|
2019-01-14 15:45:05 +01:00
|
|
|
for (const Database::data &entry : entries)
|
2018-10-17 20:44:58 +02:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
add_row(QString::fromStdString(entry.user),
|
|
|
|
entry.blocked,
|
|
|
|
QString::fromStdString(entry.reason));
|
2018-10-17 20:44:58 +02:00
|
|
|
}
|
2018-10-14 01:26:22 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:45:05 +01:00
|
|
|
void MainWindow::reload()
|
2018-10-14 01:26:22 +02:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
_database.reload();
|
|
|
|
populate_tableview(_dbdata);
|
2018-10-13 05:07:34 +02:00
|
|
|
}
|
|
|
|
|
2019-01-14 15:45:05 +01:00
|
|
|
void MainWindow::find()
|
2018-10-14 03:53:30 +02:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
if (widget_find->isVisible())
|
2018-10-14 03:53:30 +02:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
widget_find->hide();
|
2018-10-14 03:53:30 +02:00
|
|
|
}
|
2019-01-14 15:45:05 +01:00
|
|
|
else
|
2018-11-02 12:00:51 +01:00
|
|
|
{
|
2019-01-14 15:45:05 +01:00
|
|
|
widget_find->show();
|
|
|
|
text_find->setFocus();
|
2018-11-02 12:00:51 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-14 20:45:22 +01:00
|
|
|
|
|
|
|
void MainWindow::update_search(const QString &text)
|
|
|
|
{
|
|
|
|
const wstring searchfor = text.toLower().toStdWString();
|
|
|
|
|
|
|
|
vector<Database::data> filtered_entries;
|
|
|
|
if (!_dbdata.empty())
|
|
|
|
{
|
|
|
|
for (const Database::data &entry : _dbdata)
|
|
|
|
{
|
|
|
|
wstring searchstring;
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
|
|
|
|
|
|
|
|
if (check_user->isChecked())
|
|
|
|
{
|
|
|
|
searchstring += convert.from_bytes(entry.user);
|
|
|
|
}
|
|
|
|
if (check_reason->isChecked())
|
|
|
|
{
|
|
|
|
searchstring += convert.from_bytes(entry.reason);
|
|
|
|
}
|
|
|
|
std::transform(searchstring.begin(), searchstring.end(),
|
|
|
|
searchstring.begin(), ::towlower);
|
|
|
|
if (searchstring.find(searchfor) != std::string::npos)
|
|
|
|
{
|
|
|
|
filtered_entries.push_back(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
populate_tableview(filtered_entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::update_search()
|
|
|
|
{
|
|
|
|
update_search(text_find->text());
|
|
|
|
}
|