From 90ce2ce8f3cc6e587cd04cc4b29935d1d14c63e2 Mon Sep 17 00:00:00 2001 From: DuckSoft Date: Fri, 20 Mar 2020 17:03:18 +0800 Subject: [PATCH] impl: importing schemes --- src/ui/widgets/RouteSettingsMatrix.cpp | 84 ++++++++++++++++++++++++++ src/ui/widgets/RouteSettingsMatrix.hpp | 6 ++ src/ui/widgets/RouteSettingsMatrix.ui | 10 ++- 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/src/ui/widgets/RouteSettingsMatrix.cpp b/src/ui/widgets/RouteSettingsMatrix.cpp index 5c6442c7..e85e5d0c 100644 --- a/src/ui/widgets/RouteSettingsMatrix.cpp +++ b/src/ui/widgets/RouteSettingsMatrix.cpp @@ -3,6 +3,8 @@ #include "common/QvHelpers.hpp" #include "components/geosite/QvGeositeReader.hpp" +#include + RouteSettingsMatrixWidget::RouteSettingsMatrixWidget(const QString &assetsDirPath, QWidget *parent) : QWidget(parent), assetsDirPath(assetsDirPath) { @@ -62,3 +64,85 @@ config::Qv2rayRouteConfig RouteSettingsMatrixWidget::GetRouteConfig() const RouteSettingsMatrixWidget::~RouteSettingsMatrixWidget() { } + +/** + * @brief The Qv2rayRouteScheme struct + * @author DuckSoft + */ +struct Qv2rayRouteScheme : config::Qv2rayRouteConfig +{ + /** + * @brief the name of the scheme. + * @example "Untitled Scheme" + */ + QString name; + /** + * @brief the author of the scheme. + * @example "DuckSoft " + */ + 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 + * @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(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(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 + */ +std::optional 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(); +} diff --git a/src/ui/widgets/RouteSettingsMatrix.hpp b/src/ui/widgets/RouteSettingsMatrix.hpp index 72adef68..a23e91f9 100644 --- a/src/ui/widgets/RouteSettingsMatrix.hpp +++ b/src/ui/widgets/RouteSettingsMatrix.hpp @@ -17,6 +17,12 @@ class RouteSettingsMatrixWidget Qv2ray::base::config::Qv2rayRouteConfig GetRouteConfig() const; ~RouteSettingsMatrixWidget(); + private: + std::optional openFileDialog(); + + private slots: + void on_importSchemeBtn_clicked(); + private: const QString &assetsDirPath; diff --git a/src/ui/widgets/RouteSettingsMatrix.ui b/src/ui/widgets/RouteSettingsMatrix.ui index 199a8406..430865ee 100644 --- a/src/ui/widgets/RouteSettingsMatrix.ui +++ b/src/ui/widgets/RouteSettingsMatrix.ui @@ -2,6 +2,14 @@ RouteSettingsMatrix + + + 0 + 0 + 927 + 198 + + Form @@ -115,7 +123,7 @@ - false + true Import route scheme from file