2018-10-06 15:33:02 -04: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-06 19:40:04 -04:00
|
|
|
#include <regex>
|
2018-11-02 06:54:40 -04:00
|
|
|
#include <array>
|
2018-10-06 15:33:02 -04:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QDebug>
|
2018-10-17 18:18:11 -04:00
|
|
|
#include <QTranslator>
|
|
|
|
#include <QLibraryInfo>
|
2018-11-02 06:54:40 -04:00
|
|
|
#include <QtCore/qmimedata.h>
|
2018-10-06 19:40:04 -04:00
|
|
|
#include "version.hpp"
|
2018-10-06 15:33:02 -04:00
|
|
|
#include "whyblocked.hpp"
|
|
|
|
#include "interface_qt.hpp"
|
|
|
|
|
|
|
|
MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
|
|
|
|
{
|
|
|
|
setupUi(this);
|
2018-10-12 23:07:34 -04:00
|
|
|
|
|
|
|
_model = new QStandardItemModel;
|
|
|
|
tableview->setModel(_model);
|
|
|
|
populate_tableview();
|
2018-10-06 15:33:02 -04:00
|
|
|
}
|
|
|
|
|
2018-10-12 23:07:34 -04:00
|
|
|
void MainWindow::populate_tableview()
|
2018-10-06 15:33:02 -04:00
|
|
|
{
|
2018-10-12 23:07:34 -04:00
|
|
|
_model->clear();
|
2018-10-13 23:18:44 -04:00
|
|
|
_model->setHorizontalHeaderLabels(
|
|
|
|
{
|
|
|
|
tr("User/Instance"),
|
|
|
|
tr("Blocked/Silenced"),
|
|
|
|
tr("Reason")
|
|
|
|
});
|
|
|
|
tableview->horizontalHeader()->resizeSection(0, 250);
|
|
|
|
|
2018-10-06 15:33:02 -04:00
|
|
|
result_view result;
|
2018-10-17 11:28:29 -04:00
|
|
|
if (database::view(result))
|
2018-10-06 15:33:02 -04:00
|
|
|
{
|
|
|
|
for (const std::tuple<string, int, string> &line : result)
|
|
|
|
{
|
2018-10-12 23:07:34 -04:00
|
|
|
add_row(QString::fromStdString(std::get<0>(line)),
|
|
|
|
std::get<1>(line),
|
|
|
|
QString::fromStdString(std::get<2>(line)));
|
2018-10-06 15:33:02 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-12 23:07:34 -04:00
|
|
|
|
|
|
|
statusBar()->showMessage(tr("Database loaded."));
|
2018-10-06 15:33:02 -04:00
|
|
|
}
|
|
|
|
|
2018-10-23 02:29:07 -04:00
|
|
|
void MainWindow::add_row(const QString &user, const int &blocked,
|
|
|
|
const QString &reason)
|
2018-10-06 15:33:02 -04:00
|
|
|
{
|
2018-10-12 23:07:34 -04: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 15:33:02 -04:00
|
|
|
|
2018-10-12 23:07:34 -04:00
|
|
|
void MainWindow::add()
|
|
|
|
{
|
2018-10-13 21:53:30 -04:00
|
|
|
DialogAdd *dialog = new DialogAdd(this);
|
|
|
|
dialog->show();
|
2018-10-06 15:33:02 -04:00
|
|
|
}
|
|
|
|
|
2018-10-17 14:44:58 -04: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 15:33:02 -04:00
|
|
|
void MainWindow::remove()
|
|
|
|
{
|
2018-10-12 23:07:34 -04:00
|
|
|
QItemSelectionModel *selection = tableview->selectionModel();
|
|
|
|
if (selection->hasSelection())
|
|
|
|
{
|
|
|
|
for (auto &row : selection->selectedRows())
|
|
|
|
{
|
|
|
|
const string user = row.data().toString().toStdString();
|
2018-10-17 11:28:29 -04:00
|
|
|
database::remove(user);
|
2018-10-12 23:07:34 -04:00
|
|
|
statusBar()->showMessage(tr("Removed %1 from database.")
|
|
|
|
.arg(QString::fromStdString(user)));
|
|
|
|
_model->removeRow(row.row());
|
|
|
|
}
|
2018-10-13 19:26:22 -04:00
|
|
|
label_receipts->clear();
|
2018-10-12 23:07:34 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
statusBar()->showMessage(tr("Select data to remove."));
|
|
|
|
}
|
2018-10-06 15:33:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainWindow::about()
|
|
|
|
{
|
2018-10-17 18:18:11 -04:00
|
|
|
QMessageBox::about(this, tr("About Whyblocked"),
|
|
|
|
tr("<p><b>Whyblocked</b> %1</p>"
|
2018-10-06 19:40:04 -04: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 16:37:11 -04:00
|
|
|
"This program comes with ABSOLUTELY NO WARRANTY. This is free software, "
|
2018-10-06 19:40:04 -04: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 02:29:07 -04:00
|
|
|
const string user = index.sibling(index.row(), 0).data()
|
|
|
|
.toString().toStdString();
|
2018-10-06 19:40:04 -04:00
|
|
|
result_details result;
|
|
|
|
string text = "";
|
|
|
|
|
2018-10-17 11:28:29 -04:00
|
|
|
if (database::details(user, result))
|
2018-10-06 19:40:04 -04:00
|
|
|
{
|
|
|
|
if (!std::get<2>(result).empty())
|
|
|
|
{
|
2018-10-25 01:36:58 -04:00
|
|
|
text += string("<b>") + tr("Receipts:").toStdString() + "</b>";
|
2018-10-06 19:40:04 -04: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 15:33:02 -04:00
|
|
|
}
|
|
|
|
|
2018-11-02 06:54:40 -04: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-13 21:53:30 -04:00
|
|
|
DialogAdd::DialogAdd(QMainWindow *parent)
|
|
|
|
: QDialog(parent)
|
|
|
|
, _parent(static_cast<MainWindow*>(parent))
|
2018-10-12 23:07:34 -04:00
|
|
|
{
|
|
|
|
setupUi(this);
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:44:58 -04:00
|
|
|
const Dialogdata DialogAdd::get_data() const
|
2018-10-12 23:07:34 -04:00
|
|
|
{
|
2018-10-13 19:26:22 -04: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 14:44:58 -04: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 02:29:07 -04:00
|
|
|
QListWidgetItem *item =
|
|
|
|
new QListWidgetItem(QString::fromStdString(receipt));
|
2018-10-17 14:44:58 -04:00
|
|
|
item->setFlags(item->flags() | Qt::ItemIsEditable);
|
|
|
|
list_receipts->insertItem(list_receipts->count(), item);
|
|
|
|
}
|
2018-10-13 19:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DialogAdd::add_receipt()
|
|
|
|
{
|
2018-10-13 21:17:21 -04:00
|
|
|
QListWidgetItem *item = new QListWidgetItem(tr("Insert receipt here."));
|
2018-10-13 19:26:22 -04: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-12 23:07:34 -04:00
|
|
|
}
|
|
|
|
|
2018-10-13 21:53:30 -04:00
|
|
|
void DialogAdd::accept()
|
|
|
|
{
|
2018-10-17 14:44:58 -04:00
|
|
|
if (property("edit").toBool())
|
|
|
|
{
|
|
|
|
_parent->remove();
|
|
|
|
}
|
|
|
|
Dialogdata data = get_data();
|
2018-10-13 21:53:30 -04:00
|
|
|
|
2018-10-17 14:44:58 -04:00
|
|
|
if (data.user.empty())
|
2018-10-13 21:53:30 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-10-17 14:44:58 -04: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-13 21:53:30 -04:00
|
|
|
{
|
2018-10-17 14:44:58 -04:00
|
|
|
database::add_receipt(data.user, receipt);
|
2018-10-13 21:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_parent->statusBar()->showMessage(tr("Added %1 to database.")
|
2018-10-17 14:44:58 -04:00
|
|
|
.arg(QString::fromStdString(data.user)));
|
2018-10-13 21:53:30 -04:00
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2018-10-06 15:33:02 -04:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
QApplication app(argc, argv);
|
2018-10-16 11:41:44 -04:00
|
|
|
QCoreApplication::setApplicationName("Whyblocked");
|
2018-10-17 18:18:11 -04: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 15:33:02 -04:00
|
|
|
|
|
|
|
MainWindow win;
|
|
|
|
win.show();
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|