feat: front proxy for group

This commit is contained in:
arm64v8a
2023-05-25 09:34:56 +09:00
parent fbd2a3354c
commit b5e0b16a73
11 changed files with 162 additions and 49 deletions

View File

@@ -93,27 +93,46 @@ namespace NekoGui {
} }
QString BuildChain(int chainId, const std::shared_ptr<BuildConfigStatus> &status) { QString BuildChain(int chainId, const std::shared_ptr<BuildConfigStatus> &status) {
// Make list auto group = profileManager->GetGroup(status->ent->gid);
QList<std::shared_ptr<ProxyEntity>> ents; if (group == nullptr) {
auto ent = status->ent; status->result->error = QString("This profile is not in any group, your data may be corrupted.");
auto result = status->result; return {};
}
auto resolveChain = [=](const std::shared_ptr<ProxyEntity> &ent) {
QList<std::shared_ptr<ProxyEntity>> resolved;
if (ent->type == "chain") { if (ent->type == "chain") {
auto list = ent->ChainBean()->list; auto list = ent->ChainBean()->list;
std::reverse(std::begin(list), std::end(list)); std::reverse(std::begin(list), std::end(list));
for (auto id: list) { for (auto id: list) {
ents += profileManager->GetProfile(id); resolved += profileManager->GetProfile(id);
if (ents.last() == nullptr) { if (resolved.last() == nullptr) {
status->result->error = QString("chain missing ent: %1").arg(id); status->result->error = QString("chain missing ent: %1").arg(id);
return {}; break;
} }
if (ents.last()->type == "chain") { if (resolved.last()->type == "chain") {
status->result->error = QString("chain in chain is not allowed: %1").arg(id); status->result->error = QString("chain in chain is not allowed: %1").arg(id);
return {}; break;
} }
} }
} else { } else {
ents += ent; resolved += ent;
};
return resolved;
};
// Make list
auto ents = resolveChain(status->ent);
if (!status->result->error.isEmpty()) return {};
if (group->front_proxy_id >= 0) {
auto fEnt = profileManager->GetProfile(group->front_proxy_id);
if (fEnt == nullptr) {
status->result->error = QString("front proxy ent not found.");
return {};
}
ents += resolveChain(fEnt);
if (!status->result->error.isEmpty()) return {};
} }
// BuildChain // BuildChain

View File

@@ -207,6 +207,7 @@ namespace NekoGui {
Group::Group() { Group::Group() {
_add(new configItem("id", &id, itemType::integer)); _add(new configItem("id", &id, itemType::integer));
_add(new configItem("front_proxy_id", &front_proxy_id, itemType::integer));
_add(new configItem("archive", &archive, itemType::boolean)); _add(new configItem("archive", &archive, itemType::boolean));
_add(new configItem("name", &name, itemType::string)); _add(new configItem("name", &name, itemType::string));
_add(new configItem("order", &order, itemType::integerList)); _add(new configItem("order", &order, itemType::integerList));

View File

@@ -12,6 +12,7 @@ namespace NekoGui {
QString url = ""; QString url = "";
QString info = ""; QString info = "";
qint64 sub_last_update = 0; qint64 sub_last_update = 0;
int front_proxy_id = -1;
// list ui // list ui
bool manually_column_width = false; bool manually_column_width = false;

View File

@@ -290,6 +290,14 @@ For NekoBox, this rewrites the underlying(localhost) DNS in VPN mode, normal mod
<source>Manually column width</source> <source>Manually column width</source>
<translation>عرض ستون به صورت دستی</translation> <translation>عرض ستون به صورت دستی</translation>
</message> </message>
<message>
<source>Front Proxy</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>None</source>
<translation type="unfinished">هیچ یک</translation>
</message>
</context> </context>
<context> <context>
<name>DialogEditProfile</name> <name>DialogEditProfile</name>

View File

@@ -290,6 +290,14 @@ For NekoBox, this rewrites the underlying(localhost) DNS in VPN mode, normal mod
<source>Manually column width</source> <source>Manually column width</source>
<translation></translation> <translation></translation>
</message> </message>
<message>
<source>Front Proxy</source>
<translation></translation>
</message>
<message>
<source>None</source>
<translation></translation>
</message>
</context> </context>
<context> <context>
<name>DialogEditProfile</name> <name>DialogEditProfile</name>

View File

@@ -2,12 +2,13 @@
#include "ui_dialog_edit_group.h" #include "ui_dialog_edit_group.h"
#include "db/Database.hpp" #include "db/Database.hpp"
#include "ui/mainwindow_interface.h"
#include <QClipboard> #include <QClipboard>
DialogEditGroup::DialogEditGroup(const std::shared_ptr<NekoGui::Group> &ent, QWidget *parent) DialogEditGroup::DialogEditGroup(const std::shared_ptr<NekoGui::Group> &ent, QWidget *parent) : QDialog(parent), ui(new Ui::DialogEditGroup) {
: QDialog(parent), ui(new Ui::DialogEditGroup) {
ui->setupUi(this); ui->setupUi(this);
this->ent = ent;
connect(ui->type, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [=](int index) { connect(ui->type, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [=](int index) {
ui->cat_sub->setHidden(index == 0); ui->cat_sub->setHidden(index == 0);
@@ -21,27 +22,20 @@ DialogEditGroup::DialogEditGroup(const std::shared_ptr<NekoGui::Group> &ent, QWi
ui->manually_column_width->setChecked(ent->manually_column_width); ui->manually_column_width->setChecked(ent->manually_column_width);
ui->copy_links->setVisible(false); ui->copy_links->setVisible(false);
ui->copy_links_nkr->setVisible(false); ui->copy_links_nkr->setVisible(false);
if (ent->id >= 0) { // already a group if (ent->id >= 0) { // already a group
ui->type->setDisabled(true); ui->type->setDisabled(true);
if (!ent->Profiles().isEmpty()) { if (!ent->Profiles().isEmpty()) {
ui->copy_links->setVisible(true); ui->copy_links->setVisible(true);
ui->copy_links_nkr->setVisible(true); ui->copy_links_nkr->setVisible(true);
} }
} else { // new group
ui->front_proxy->hide();
ui->front_proxy_l->hide();
} }
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, [=] { CACHE.front_proxy = ent->front_proxy_id;
if (ent->id >= 0) { // already a group refresh_front_proxy();
if (!ent->url.isEmpty() && ui->url->text().isEmpty()) {
MessageBoxWarning(tr("Warning"), tr("Please input URL"));
return;
}
}
ent->name = ui->name->text();
ent->url = ui->url->text();
ent->archive = ui->archive->isChecked();
ent->manually_column_width = ui->manually_column_width->isChecked();
QDialog::accept();
});
connect(ui->copy_links, &QPushButton::clicked, this, [=] { connect(ui->copy_links, &QPushButton::clicked, this, [=] {
QStringList links; QStringList links;
@@ -66,3 +60,35 @@ DialogEditGroup::DialogEditGroup(const std::shared_ptr<NekoGui::Group> &ent, QWi
DialogEditGroup::~DialogEditGroup() { DialogEditGroup::~DialogEditGroup() {
delete ui; delete ui;
} }
void DialogEditGroup::accept() {
if (ent->id >= 0) { // already a group
if (!ent->url.isEmpty() && ui->url->text().isEmpty()) {
MessageBoxWarning(tr("Warning"), tr("Please input URL"));
return;
}
}
ent->name = ui->name->text();
ent->url = ui->url->text();
ent->archive = ui->archive->isChecked();
ent->manually_column_width = ui->manually_column_width->isChecked();
ent->front_proxy_id = CACHE.front_proxy;
QDialog::accept();
}
void DialogEditGroup::refresh_front_proxy() {
auto fEnt = NekoGui::profileManager->GetProfile(CACHE.front_proxy);
ui->front_proxy->setText(fEnt == nullptr ? tr("None") : fEnt->bean->DisplayTypeAndName());
}
void DialogEditGroup::on_front_proxy_clicked() {
auto parent = dynamic_cast<QWidget *>(this->parent());
parent->hide();
this->hide();
GetMainWindow()->start_select_mode(this, [=](int id) {
CACHE.front_proxy = id;
refresh_front_proxy();
parent->show();
show();
});
}

View File

@@ -19,4 +19,18 @@ public:
private: private:
Ui::DialogEditGroup *ui; Ui::DialogEditGroup *ui;
std::shared_ptr<NekoGui::Group> ent;
struct {
int front_proxy;
} CACHE;
void refresh_front_proxy();
private slots:
void accept() override;
void on_front_proxy_clicked();
}; };

View File

@@ -84,6 +84,20 @@
</item> </item>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QPushButton" name="front_proxy">
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="front_proxy_l">
<property name="text">
<string>Front Proxy</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>
@@ -177,5 +191,21 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>DialogEditGroup</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>199</x>
<y>291</y>
</hint>
<hint type="destinationlabel">
<x>199</x>
<y>157</y>
</hint>
</hints>
</connection>
</connections> </connections>
</ui> </ui>

View File

@@ -596,9 +596,11 @@ inline bool dialog_is_using = false;
if (dialog_is_using) return; \ if (dialog_is_using) return; \
dialog_is_using = true; \ dialog_is_using = true; \
auto dialog = new a(this); \ auto dialog = new a(this); \
dialog->exec(); \ connect(dialog, &QDialog::finished, this, [=] { \
dialog->deleteLater(); \ dialog->deleteLater(); \
dialog_is_using = false; dialog_is_using = false; \
}); \
dialog->show();
void MainWindow::on_menu_basic_settings_triggered() { void MainWindow::on_menu_basic_settings_triggered() {
USE_DIALOG(DialogBasicSettings) USE_DIALOG(DialogBasicSettings)

View File

@@ -45,6 +45,7 @@ GroupItem::GroupItem(QWidget *parent, const std::shared_ptr<NekoGui::Group> &ent
ui->setupUi(this); ui->setupUi(this);
this->setLayoutDirection(Qt::LeftToRight); this->setLayoutDirection(Qt::LeftToRight);
this->parentWindow = parent;
this->ent = ent; this->ent = ent;
this->item = item; this->item = item;
if (ent == nullptr) return; if (ent == nullptr) return;
@@ -102,15 +103,16 @@ void GroupItem::on_update_sub_clicked() {
} }
void GroupItem::on_edit_clicked() { void GroupItem::on_edit_clicked() {
auto dialog = new DialogEditGroup(ent, this); auto dialog = new DialogEditGroup(ent, parentWindow);
int ret = dialog->exec(); connect(dialog, &QDialog::finished, this, [=] {
dialog->deleteLater(); if (dialog->result() == QDialog::Accepted) {
if (ret == QDialog::Accepted) {
ent->Save(); ent->Save();
refresh_data(); refresh_data();
MW_dialog_message(Dialog_DialogManageGroups, "refresh" + Int2String(ent->id)); MW_dialog_message(Dialog_DialogManageGroups, "refresh" + Int2String(ent->id));
} }
dialog->deleteLater();
});
dialog->show();
} }
void GroupItem::on_remove_clicked() { void GroupItem::on_remove_clicked() {

View File

@@ -27,6 +27,8 @@ public:
private: private:
Ui::GroupItem *ui; Ui::GroupItem *ui;
QWidget *parentWindow;
signals: signals:
void edit_clicked(); void edit_clicked();