Skip to content

Commit

Permalink
support auto export current node configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
cocomeow committed Dec 6, 2021
1 parent afc58b6 commit cc67039
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 42 deletions.
33 changes: 22 additions & 11 deletions i18n/across_zh_CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,27 @@
<translation>接受</translation>
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="95"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="105"/>
<source>Set as Default &gt; Last Connected</source>
<translation>设置为默认 &gt; 上次已链接</translation>
</message>
<message>
<source>Default &gt; Last Connected (Updating groups will reset the default node)</source>
<translation>默认 &gt; 上次连接 (更新群组会重置默认节点)</translation>
<translation type="vanished">默认 &gt; 上次连接 (更新群组会重置默认节点)</translation>
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="111"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="126"/>
<source>Auto Export</source>
<translation>自动导出</translation>
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="110"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="132"/>
<source>Tray Icon</source>
<translation>托盘图标</translation>
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="127"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="149"/>
<source>Unavailable</source>
<translation>不可用</translation>
</message>
Expand All @@ -217,17 +227,17 @@
<translation type="vanished">默认 &gt; 上次连接</translation>
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="146"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="168"/>
<source>Minimize startup</source>
<translation>最小化启动</translation>
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="177"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="199"/>
<source>Log Outputs</source>
<translation>日志输出</translation>
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="208"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="230"/>
<source>Max Lines</source>
<translation>最大行数</translation>
</message>
Expand Down Expand Up @@ -261,6 +271,7 @@
</message>
<message>
<location filename="../src/views/setting/ApplicationItem.qml" line="87"/>
<location filename="../src/views/setting/ApplicationItem.qml" line="104"/>
<source>Auto Connect</source>
<translation>自动连接</translation>
</message>
Expand Down Expand Up @@ -1233,7 +1244,7 @@
<context>
<name>across::NodeList</name>
<message>
<location filename="../src/view_models/nodelist.cpp" line="342"/>
<location filename="../src/view_models/nodelist.cpp" line="344"/>
<source>Copy [%1] URL to clipboard</source>
<translation>复制 [%1] 链接到剪贴板</translation>
</message>
Expand Down Expand Up @@ -1284,12 +1295,12 @@
<context>
<name>across::setting::ConfigTools</name>
<message>
<location filename="../src/view_models/configtools.cpp" line="1039"/>
<location filename="../src/view_models/configtools.cpp" line="1054"/>
<source>Failed to parse version</source>
<translation>无法解析版本</translation>
</message>
<message>
<location filename="../src/view_models/configtools.cpp" line="1042"/>
<location filename="../src/view_models/configtools.cpp" line="1057"/>
<source>New Version: v%1</source>
<translation>新版本:v%1</translation>
</message>
Expand All @@ -1298,7 +1309,7 @@
<translation type="vanished">新版本:%1</translation>
</message>
<message>
<location filename="../src/view_models/configtools.cpp" line="1044"/>
<location filename="../src/view_models/configtools.cpp" line="1059"/>
<source>Already the latest version</source>
<translation>已经是最新版本</translation>
</message>
Expand Down
1 change: 1 addition & 0 deletions misc/across.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ message Interface
Tray tray = 3;
bool auto_connect = 4;
bool auto_start = 5;
bool auto_export = 6;
}

message Update
Expand Down
1 change: 1 addition & 0 deletions src/models/confighelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ config::Config ConfigHelper::defaultConfig() {
interface->set_language("en_US");
interface->set_auto_connect(false);
interface->set_auto_start(false);
interface->set_auto_export(true);

if (auto theme = interface->mutable_theme()) {
theme->set_theme("default-light");
Expand Down
23 changes: 19 additions & 4 deletions src/view_models/configtools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool ConfigTools::loadConfigPath(const QString &file_path) {
break;
}

this->m_config_path = config_dir.filePath(m_config_name);
this->m_config_path = config_dir.filePath(ACROSS_CONFIG);

if (!isFileExist(this->m_config_path)) {
saveConfig();
Expand Down Expand Up @@ -323,8 +323,7 @@ void ConfigTools::checkUpdate() {
}

void ConfigTools::setNews() {
auto news_path =
QDir(m_config.data_dir().c_str()).filePath(m_news_file_name);
auto news_path = QDir(m_config.data_dir().c_str()).filePath(ACROSS_NEWS);

if (auto news_file = QFile(news_path); news_file.exists()) {
if (news_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Expand Down Expand Up @@ -923,6 +922,10 @@ bool ConfigTools::enableAutoConnect() { return p_interface->auto_connect(); }

bool ConfigTools::enableAutoStart() { return p_interface->auto_start(); }

bool ConfigTools::enableAutoExport() const {
return p_interface->auto_export();
}

void ConfigTools::setEnableBanner(bool val) {
if (val == p_theme->banner().enable())
return;
Expand All @@ -944,6 +947,7 @@ void ConfigTools::setEnableBanner(bool val) {
void ConfigTools::setEnableAutoConnect(bool val) {
if (val == p_interface->auto_connect())
return;

p_interface->set_auto_connect(val);

emit configChanged();
Expand All @@ -953,12 +957,23 @@ void ConfigTools::setEnableAutoConnect(bool val) {
void ConfigTools::setEnableAutoStart(bool val) {
if (val == p_interface->auto_start())
return;

p_interface->set_auto_start(val);

emit configChanged();
emit enableAutoStartChanged();
}

void ConfigTools::setEnableAutoExport(bool val) {
if (val == p_interface->auto_export())
return;

p_interface->set_auto_export(val);

emit configChanged();
emit enableAutoExportChanged();
}

double ConfigTools::backgroundOpacity() {
return p_theme->banner().background_opacity();
}
Expand Down Expand Up @@ -1029,7 +1044,7 @@ void ConfigTools::handleUpdated(const QVariant &content) {
if (auto task = content.value<DownloadTask>(); !task.content.isEmpty()) {
if (QDir data_dir(m_config.data_dir().c_str()); data_dir.exists()) {
ConfigHelper::saveToFile(task.content.toStdString(),
data_dir.filePath(m_news_file_name));
data_dir.filePath(ACROSS_NEWS));

setNews();
}
Expand Down
21 changes: 14 additions & 7 deletions src/view_models/configtools.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ class ConfigTools : public QObject {
setEnableAutoConnect NOTIFY enableAutoConnectChanged)
Q_PROPERTY(bool enableAutoStart READ enableAutoStart WRITE
setEnableAutoStart NOTIFY enableAutoStartChanged)
Q_PROPERTY(bool enableAutoExport READ enableAutoExport WRITE
setEnableAutoExport NOTIFY enableAutoExportChanged)
Q_PROPERTY(QString backgroundImage READ backgroundImage WRITE
setBackgroundImage NOTIFY backgroundImageChanged)
Q_PROPERTY(double backgroundOpacity READ backgroundOpacity WRITE
Expand Down Expand Up @@ -186,14 +188,18 @@ class ConfigTools : public QObject {
Q_INVOKABLE void testAPI();
Q_INVOKABLE bool testAndSetAddr(const QString &addr);
Q_INVOKABLE void freshInbound();
Q_INVOKABLE void saveConfig(const QString& config_path = "");
Q_INVOKABLE void saveConfig(const QString &config_path = "");
Q_INVOKABLE void checkUpdate();
Q_INVOKABLE void setNews();

across::config::Config *config();
across::config::Theme *currentTheme();

static bool isFileExist(const QString& file_path);
static bool isFileExist(const QString &file_path);

const QString ACROSS_CONFIG = "across.json";
const QString ACROSS_NEWS = "across_news.json";
const QString CURRENT_CONFIG = "across_current.json";

public:
// database setting
Expand Down Expand Up @@ -253,6 +259,7 @@ class ConfigTools : public QObject {
bool enableBanner();
bool enableAutoConnect();
bool enableAutoStart();
bool enableAutoExport() const;
QString backgroundImage();
double backgroundOpacity();
QString logMode();
Expand Down Expand Up @@ -316,12 +323,13 @@ class ConfigTools : public QObject {
void setEnableBanner(bool val);
void setEnableAutoConnect(bool val);
void setEnableAutoStart(bool val);
void setEnableAutoExport(bool val);
void setBackgroundImage(const QString &val);
void setBackgroundOpacity(double val);
void setLogMode(QString log_mode = "");
void setLogLines(int val);

// network settingQDir data_dir(m_config.data_dir().c_str());
// network setting
void setNetworkTestMethod(const QString &val);
void setNetworkTestURL(const QString &val);
void setNetworkUserAgent(const QString &val);
Expand Down Expand Up @@ -378,6 +386,7 @@ class ConfigTools : public QObject {
void enableStartFromMinimizedChanged();
void enableAutoConnectChanged();
void enableAutoStartChanged();
void enableAutoExportChanged();
void buildInfoChanged();
void configChanged();
void enableBannerChanged();
Expand All @@ -398,10 +407,8 @@ class ConfigTools : public QObject {

private:
QQueue<QFuture<void>> m_tasks;
const QString m_config_name = "across.json";
const QString m_news_file_name = "across_news.json";
QStringList m_version_news;
QString m_config_path = "./" + m_config_name;
QString m_config_path = "./" + ACROSS_CONFIG;
QString m_api_result_text = "";

QSharedPointer<across::network::CURLTools> p_curl;
Expand All @@ -413,6 +420,6 @@ class ConfigTools : public QObject {
across::config::Inbound *p_inbound{};
across::config::Network *p_network{};
};
} // namespace across
} // namespace across::setting

#endif // CONFIGTOOLS_H
14 changes: 8 additions & 6 deletions src/view_models/nodelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,15 @@ QString NodeList::generateConfig() {
across::SerializeTools::ConfigToJson(node_config));
}

#ifdef QT_DEBUG
QFile file("generation_test.json");
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
file.write(json_str.toUtf8());
file.close();
if (p_config->enableAutoExport()) {
QDir dir = p_config->dataDir();

QFile file(dir.filePath(p_config->CURRENT_CONFIG));
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
file.write(json_str.toUtf8());
file.close();
}
}
#endif

return json_str;
}
Expand Down
6 changes: 1 addition & 5 deletions src/views/main/PopMessageBox.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import Arktoria.ACross

CardBox {
id: control
implicitWidth: Math.max(
displayTitle.width,
displayMessage.width) > 240 ? Math.max(
displayTitle.width,
displayMessage.width) : 240
implicitWidth: 320
implicitHeight: 84
clip: true

Expand Down
8 changes: 4 additions & 4 deletions src/views/main/PopNotify.qml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import QtQuick.Layouts
import Arktoria.ACross

Item {
id: control
implicitWidth: 240
id: popNotifyControl
implicitWidth: 320
implicitHeight: popNotifyListView.count >= 3 ? 84 * 3 : 84 * popNotifyListView.count
clip: true
visible: false
Expand Down Expand Up @@ -39,13 +39,13 @@ Item {

onCountChanged: {
if (count === 0) {
control.visible = false
popNotifyControl.visible = false
}
}
}

function notify(title = "", message = "") {
control.visible = true
popNotifyControl.visible = true
popNotifyModel.append({
"title": title,
"message": message
Expand Down
32 changes: 27 additions & 5 deletions src/views/setting/ApplicationItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,8 @@ Item {
color: acrossConfig.textColor
}

Label {
Item {
Layout.fillWidth: true
Layout.columnSpan: 4

text: qsTr("Default > Last Connected (Updating groups will reset the default node)")
color: acrossConfig.highlightColor
}

SwitchBox {
Expand All @@ -103,6 +99,32 @@ Item {
checked: acrossConfig.enableAutoConnect
onCheckedChanged: {
acrossConfig.enableAutoConnect = checked

if (checked) {
popNotify.notify(qsTr("Auto Connect"),
qsTr("Set as Default > Last Connected"))
}
}
}

Label {
text: qsTr("Auto Export")
color: acrossConfig.textColor
}

Item {
Layout.fillWidth: true
}

SwitchBox {
checked: acrossConfig.enableAutoExport

onCheckedChanged: {
acrossConfig.enableAutoExport = checked

if (checked) {
popNotify.notify(qsTr("Auto Export"), acrossConfig.dataDir)
}
}
}

Expand Down

0 comments on commit cc67039

Please sign in to comment.