mirror of
https://github.com/Qv2ray/Qv2ray.git
synced 2025-05-20 02:40:20 +08:00
impl: importing schemes
This commit is contained in:
parent
eb45a1be7d
commit
90ce2ce8f3
@ -3,6 +3,8 @@
|
|||||||
#include "common/QvHelpers.hpp"
|
#include "common/QvHelpers.hpp"
|
||||||
#include "components/geosite/QvGeositeReader.hpp"
|
#include "components/geosite/QvGeositeReader.hpp"
|
||||||
|
|
||||||
|
#include <QFileDialog>
|
||||||
|
|
||||||
RouteSettingsMatrixWidget::RouteSettingsMatrixWidget(const QString &assetsDirPath, QWidget *parent)
|
RouteSettingsMatrixWidget::RouteSettingsMatrixWidget(const QString &assetsDirPath, QWidget *parent)
|
||||||
: QWidget(parent), assetsDirPath(assetsDirPath)
|
: QWidget(parent), assetsDirPath(assetsDirPath)
|
||||||
{
|
{
|
||||||
@ -62,3 +64,85 @@ config::Qv2rayRouteConfig RouteSettingsMatrixWidget::GetRouteConfig() const
|
|||||||
RouteSettingsMatrixWidget::~RouteSettingsMatrixWidget()
|
RouteSettingsMatrixWidget::~RouteSettingsMatrixWidget()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The Qv2rayRouteScheme struct
|
||||||
|
* @author DuckSoft <realducksoft@gmail.com>
|
||||||
|
*/
|
||||||
|
struct Qv2rayRouteScheme : config::Qv2rayRouteConfig
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @brief the name of the scheme.
|
||||||
|
* @example "Untitled Scheme"
|
||||||
|
*/
|
||||||
|
QString name;
|
||||||
|
/**
|
||||||
|
* @brief the author of the scheme.
|
||||||
|
* @example "DuckSoft <realducksoft@gmail.com>"
|
||||||
|
*/
|
||||||
|
QString author;
|
||||||
|
/**
|
||||||
|
* @brief details of this scheme.
|
||||||
|
* @example "A scheme to bypass China mainland, while allowing bilibili to go through proxy."
|
||||||
|
*/
|
||||||
|
QString description;
|
||||||
|
|
||||||
|
// M: all these fields are mandatory
|
||||||
|
XTOSTRUCT(M(name, author, description, domains, ips));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief RouteSettingsMatrixWidget::on_importSchemeBtn_clicked
|
||||||
|
* @author DuckSoft <realducksoft@gmail.com>
|
||||||
|
* @todo add some debug output
|
||||||
|
*/
|
||||||
|
void RouteSettingsMatrixWidget::on_importSchemeBtn_clicked()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// open up the file dialog and choose a file.
|
||||||
|
auto filePath = this->openFileDialog();
|
||||||
|
if (!filePath)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// read the file and parse back to struct.
|
||||||
|
// if error occurred on parsing, an exception will be thrown.
|
||||||
|
auto content = StringFromFile(filePath.value());
|
||||||
|
auto scheme = StructFromJsonString<Qv2rayRouteScheme>(content);
|
||||||
|
|
||||||
|
// show the information of this scheme to user,
|
||||||
|
// and ask user if he/she wants to import and apply this.
|
||||||
|
auto strPrompt = tr("Import scheme '%1' by '%2'?" NEWLINE "Description: %3").arg(scheme.name, scheme.author, scheme.description);
|
||||||
|
auto decision = QvMessageBoxAsk(this, tr("Importing Scheme"), strPrompt);
|
||||||
|
|
||||||
|
// if user don't want to import, just leave.
|
||||||
|
if (decision != QMessageBox::Yes)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// write the scheme onto the window
|
||||||
|
this->SetRouteConfig(static_cast<Qv2rayRouteConfig>(scheme));
|
||||||
|
|
||||||
|
// done
|
||||||
|
}
|
||||||
|
catch (exception)
|
||||||
|
{
|
||||||
|
// TODO: Give some error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief opens up a dialog and asks user to choose a scheme file.
|
||||||
|
* @return the selected file path, if any
|
||||||
|
* @author DuckSoft <realducksoft@gmail.com>
|
||||||
|
*/
|
||||||
|
std::optional<QString> RouteSettingsMatrixWidget::openFileDialog()
|
||||||
|
{
|
||||||
|
QFileDialog dialog;
|
||||||
|
dialog.setFileMode(QFileDialog::ExistingFile);
|
||||||
|
dialog.setNameFilter(tr("QvRoute Schemes (*.json)"));
|
||||||
|
if (!dialog.exec() || dialog.selectedFiles().length() != 1)
|
||||||
|
{
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
return dialog.selectedFiles().first();
|
||||||
|
}
|
||||||
|
@ -17,6 +17,12 @@ class RouteSettingsMatrixWidget
|
|||||||
Qv2ray::base::config::Qv2rayRouteConfig GetRouteConfig() const;
|
Qv2ray::base::config::Qv2rayRouteConfig GetRouteConfig() const;
|
||||||
~RouteSettingsMatrixWidget();
|
~RouteSettingsMatrixWidget();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::optional<QString> openFileDialog();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void on_importSchemeBtn_clicked();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const QString &assetsDirPath;
|
const QString &assetsDirPath;
|
||||||
|
|
||||||
|
@ -2,6 +2,14 @@
|
|||||||
<ui version="4.0">
|
<ui version="4.0">
|
||||||
<class>RouteSettingsMatrix</class>
|
<class>RouteSettingsMatrix</class>
|
||||||
<widget class="QWidget" name="RouteSettingsMatrix">
|
<widget class="QWidget" name="RouteSettingsMatrix">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>927</width>
|
||||||
|
<height>198</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
<string>Form</string>
|
<string>Form</string>
|
||||||
</property>
|
</property>
|
||||||
@ -115,7 +123,7 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QPushButton" name="importSchemeBtn">
|
<widget class="QPushButton" name="importSchemeBtn">
|
||||||
<property name="enabled">
|
<property name="enabled">
|
||||||
<bool>false</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
<property name="toolTip">
|
<property name="toolTip">
|
||||||
<string>Import route scheme from file</string>
|
<string>Import route scheme from file</string>
|
||||||
|
Loading…
Reference in New Issue
Block a user