2018-10-06 21:33:02 +02:00
|
|
|
/* This file is part of whyblocked.
|
|
|
|
* Copyright © 2018 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* 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>
|
2018-10-06 21:33:02 +02:00
|
|
|
#include <QMessageBox>
|
2018-10-18 00:18:11 +02:00
|
|
|
#include <QTranslator>
|
|
|
|
#include <QLibraryInfo>
|
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"
|
2018-10-06 21:33:02 +02:00
|
|
|
#include "whyblocked.hpp"
|
|
|
|
#include "interface_qt.hpp"
|
|
|
|
|
2018-11-03 12:59:49 +01:00
|
|
|
MainWindow::MainWindow(QMainWindow *parent)
|
|
|
|
: QMainWindow(parent)
|
|
|
|
, _config("whyblocked.cfg")
|
|
|
|
, _headersize({ 250, 125, 125 })
|
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] };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 05:07:34 +02:00
|
|
|
populate_tableview();
|
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 :
|
|
|
|
{ "size", "toolbar_position", "toolbar_visible", "table_headers" })
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
headers.add(libconfig::Setting::TypeInt) =
|
|
|
|
tableview->horizontalHeader()->sectionSize(2);
|
|
|
|
|
|
|
|
_config.write();
|
|
|
|
}
|
|
|
|
|
2018-10-13 05:07:34 +02:00
|
|
|
void MainWindow::populate_tableview()
|
2018-10-06 21:33:02 +02:00
|
|
|
{
|
2018-10-13 05:07:34 +02:00
|
|
|
_model->clear();
|
2018-10-14 05:18:44 +02:00
|
|
|
_model->setHorizontalHeaderLabels(
|
|
|
|
{
|
|
|
|
tr("User/Instance"),
|
|
|
|
tr("Blocked/Silenced"),
|
|
|
|
tr("Reason")
|
|
|
|
});
|
2018-11-03 12:59:49 +01:00
|
|
|
tableview->horizontalHeader()->resizeSection(0, _headersize[0]);
|
|
|
|
tableview->horizontalHeader()->resizeSection(1, _headersize[1]);
|
|
|
|
tableview->horizontalHeader()->resizeSection(2, _headersize[2]);
|
2018-10-14 05:18:44 +02:00
|
|
|
|
2018-10-06 21:33:02 +02:00
|
|
|
result_view result;
|
2018-10-17 17:28:29 +02:00
|
|
|
if (database::view(result))
|
2018-10-06 21:33:02 +02:00
|
|
|
{
|
|
|
|
for (const std::tuple<string, int, string> &line : result)
|
|
|
|
{
|
2018-10-13 05:07:34 +02:00
|
|
|
add_row(QString::fromStdString(std::get<0>(line)),
|
|
|
|
std::get<1>(line),
|
|
|
|
QString::fromStdString(std::get<2>(line)));
|
2018-10-06 21:33:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-13 05:07:34 +02:00
|
|
|
void MainWindow::add()
|
|
|
|
{
|
2018-10-14 03:53:30 +02:00
|
|
|
DialogAdd *dialog = new DialogAdd(this);
|
|
|
|
dialog->show();
|
2018-10-06 21:33:02 +02:00
|
|
|
}
|
|
|
|
|
2018-10-17 20:44:58 +02:00
|
|
|
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(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);
|
|
|
|
|
|
|
|
dialog->set_data(data);
|
|
|
|
dialog->setProperty("edit", true);
|
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
for (auto &row : selection->selectedRows())
|
|
|
|
{
|
|
|
|
const string user = row.data().toString().toStdString();
|
2018-10-17 17:28:29 +02: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
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2018-10-07 01:40:04 +02:00
|
|
|
result_details result;
|
|
|
|
string text = "";
|
|
|
|
|
2018-10-17 17:28:29 +02:00
|
|
|
if (database::details(user, result))
|
2018-10-07 01:40:04 +02:00
|
|
|
{
|
|
|
|
if (!std::get<2>(result).empty())
|
|
|
|
{
|
2018-10-25 07:36:58 +02:00
|
|
|
text += string("<b>") + tr("Receipts:").toStdString() + "</b>";
|
2018-10-07 01:40:04 +02:00
|
|
|
for (const string &url : std::get<2>(result))
|
|
|
|
{
|
|
|
|
text += "<br>" + url;
|
|
|
|
}
|
|
|
|
text = urls_to_hyperlinks(text);
|
|
|
|
}
|
|
|
|
label_receipts->setText(QString::fromStdString((text)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const string MainWindow::urls_to_hyperlinks(const string &text)
|
|
|
|
{
|
|
|
|
std::regex re_url("((https?|gopher|ftps?)\\://[^ <]*)");
|
|
|
|
return std::regex_replace(text, re_url, "<a href=\"$1\">$1</a>");
|
2018-10-06 21:33:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-02 11:54:40 +01:00
|
|
|
void MainWindow::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("text/plain"))
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
string text = event->mimeData()->text().toStdString();
|
|
|
|
const std::array<const std::regex, 4> fediverse =
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DialogAdd *dialog = new DialogAdd(this);
|
|
|
|
Dialogdata data;
|
|
|
|
data.user = text;
|
|
|
|
dialog->set_data(data);
|
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
2018-10-14 03:53:30 +02:00
|
|
|
DialogAdd::DialogAdd(QMainWindow *parent)
|
|
|
|
: QDialog(parent)
|
|
|
|
, _parent(static_cast<MainWindow*>(parent))
|
2018-10-13 05:07:34 +02:00
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:44:58 +02:00
|
|
|
const Dialogdata DialogAdd::get_data() const
|
2018-10-13 05:07:34 +02:00
|
|
|
{
|
2018-10-14 01:26:22 +02:00
|
|
|
std::vector<string> receipts;
|
|
|
|
for (int row = 0; row <= list_receipts->count() - 1; ++row)
|
|
|
|
{
|
|
|
|
receipts.push_back(list_receipts->item(row)->text().toStdString());
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:44:58 +02:00
|
|
|
Dialogdata data;
|
|
|
|
data.user = text_user->text().toStdString();
|
|
|
|
data.blocked = radio_blocked->isChecked();
|
|
|
|
data.reason = text_reason->text().toStdString();
|
|
|
|
data.receipts = receipts;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void DialogAdd::set_data(const Dialogdata &data)
|
|
|
|
{
|
|
|
|
text_user->setText(QString::fromStdString(data.user));
|
|
|
|
radio_blocked->setChecked(data.blocked);
|
|
|
|
radio_silcenced->setChecked(!data.blocked);
|
|
|
|
text_reason->setText(QString::fromStdString(data.reason));
|
|
|
|
for (const string &receipt : data.receipts)
|
|
|
|
{
|
2018-10-23 08:29:07 +02:00
|
|
|
QListWidgetItem *item =
|
|
|
|
new QListWidgetItem(QString::fromStdString(receipt));
|
2018-10-17 20:44:58 +02:00
|
|
|
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
list_receipts->insertItem(list_receipts->count(), item);
|
|
|
|
}
|
2018-10-14 01:26:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DialogAdd::add_receipt()
|
|
|
|
{
|
2018-10-14 03:17:21 +02:00
|
|
|
QListWidgetItem *item = new QListWidgetItem(tr("Insert receipt here."));
|
2018-10-14 01:26:22 +02:00
|
|
|
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
list_receipts->insertItem(list_receipts->count(), item);
|
|
|
|
list_receipts->editItem(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogAdd::remove_receipt()
|
|
|
|
{
|
|
|
|
for (auto item :list_receipts->selectedItems())
|
|
|
|
{
|
|
|
|
delete item;
|
|
|
|
}
|
2018-10-13 05:07:34 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 03:53:30 +02:00
|
|
|
void DialogAdd::accept()
|
|
|
|
{
|
2018-10-17 20:44:58 +02:00
|
|
|
if (property("edit").toBool())
|
|
|
|
{
|
|
|
|
_parent->remove();
|
|
|
|
}
|
|
|
|
Dialogdata data = get_data();
|
2018-10-14 03:53:30 +02:00
|
|
|
|
2018-10-17 20:44:58 +02:00
|
|
|
if (data.user.empty())
|
2018-10-14 03:53:30 +02:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-10-17 20:44:58 +02:00
|
|
|
database::add_block(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)
|
2018-10-14 03:53:30 +02:00
|
|
|
{
|
2018-10-17 20:44:58 +02:00
|
|
|
database::add_receipt(data.user, receipt);
|
2018-10-14 03:53:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2018-11-02 12:00:51 +01:00
|
|
|
void DialogAdd::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("text/plain"))
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DialogAdd::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
const QString text = event->mimeData()->text();
|
|
|
|
QListWidgetItem *item = new QListWidgetItem(text);
|
|
|
|
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
list_receipts->insertItem(list_receipts->count(), item);
|
|
|
|
}
|
|
|
|
|
2018-10-06 21:33:02 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
2018-10-16 17:41:44 +02:00
|
|
|
QCoreApplication::setApplicationName("Whyblocked");
|
2018-10-18 00:18:11 +02:00
|
|
|
|
|
|
|
QTranslator qtTranslator;
|
|
|
|
qtTranslator.load("qt_" + QLocale::system().name(),
|
|
|
|
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
app.installTranslator(&qtTranslator);
|
|
|
|
QTranslator appTranslator;
|
|
|
|
appTranslator.load("whyblocked_" + QLocale::system().name(),
|
|
|
|
QLibraryInfo::location(QLibraryInfo::TranslationsPath));
|
|
|
|
app.installTranslator(&appTranslator);
|
2018-10-06 21:33:02 +02:00
|
|
|
|
|
|
|
MainWindow win;
|
|
|
|
win.show();
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|