From bac367f9ed45249f909a5522b82ec7e8387125c9 Mon Sep 17 00:00:00 2001 From: daleclack Date: Tue, 26 Jul 2022 11:00:41 +0800 Subject: [PATCH] Adjust the method to load config file --- XeRelease_Gtkmm3/src/MyDialog.cc | 64 ++++++++++++++++--------------- XeRelease_Gtkmm3/src/MyDialog.hh | 4 +- XeRelease_Gtkmm3/src/MyWin.cc | 30 +++++++++------ XeRelease_Gtkmm3/src/MyWin.hh | 3 ++ XeRelease_Gtkmm3/src/xerelease.cc | 18 ++++----- XeRelease_Gtkmm3/src/xerelease.hh | 6 +++ 6 files changed, 69 insertions(+), 56 deletions(-) diff --git a/XeRelease_Gtkmm3/src/MyDialog.cc b/XeRelease_Gtkmm3/src/MyDialog.cc index 248bed1..b502f48 100644 --- a/XeRelease_Gtkmm3/src/MyDialog.cc +++ b/XeRelease_Gtkmm3/src/MyDialog.cc @@ -13,27 +13,7 @@ MyDialog::MyDialog(BaseObjectType *cobject, const Glib::RefPtr &re ref_builder->get_widget("entry_path", entry_path); ref_builder->get_widget("btnpath", btnpath); - // Read Configs - std::string config_longterm, config_stable, config_devel; - - // Open json file - std::ifstream json_file("xe_config.json"); - if (json_file.is_open()) - { - // Read data from json file - data = json::parse(json_file); - config_longterm = data["Longterm"]; - config_stable = data["Stable"]; - config_devel = data["Develop"]; - config_unix = data["Release_Path_Unix"]; - config_win32 = data["Release_Path_Win32"]; - - // Set text from json file data - entry_lts->set_text(config_longterm); - entry_stable->set_text(config_stable); - entry_dev->set_text(config_devel); - } - json_file.close(); + // json_file.close(); // std::string config; // if(readCfgFile("xe_config","Longterm",config)){ @@ -48,16 +28,6 @@ MyDialog::MyDialog(BaseObjectType *cobject, const Glib::RefPtr &re // readCfgFile("xe_config","Release_Path_Unix",config_unix); // readCfgFile("xe_config","Release_Path_Win32",config_win32); - // Use different path for Linux filesystem and Windows - if (unix_file_system_detected()) - { - entry_path->set_text(config_unix); - } - else - { - entry_path->set_text(config_win32); - } - // Connect Signal btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyDialog::btnpath_clicked)); } @@ -127,6 +97,38 @@ void MyDialog::on_response(int response_id) hide(); } +void MyDialog::init_json_data(json &data1) +{ + // Read Configs + std::string config_longterm, config_stable, config_devel; + // Open json file + if (!data1.empty()) + { + // Read data from json file + // data = json::parse(json_file); + config_longterm = data1["Longterm"]; + config_stable = data1["Stable"]; + config_devel = data1["Develop"]; + config_unix = data1["Release_Path_Unix"]; + config_win32 = data1["Release_Path_Win32"]; + + // Set text from json file data + entry_lts->set_text(config_longterm); + entry_stable->set_text(config_stable); + entry_dev->set_text(config_devel); + + // Use different path for Linux filesystem and Windows + if (unix_file_system_detected()) + { + entry_path->set_text(config_unix); + } + else + { + entry_path->set_text(config_win32); + } + } +} + MyDialog *MyDialog::create(Gtk::Window &parent) { // Create a dialog diff --git a/XeRelease_Gtkmm3/src/MyDialog.hh b/XeRelease_Gtkmm3/src/MyDialog.hh index 6e0f2ca..8b3a4d9 100644 --- a/XeRelease_Gtkmm3/src/MyDialog.hh +++ b/XeRelease_Gtkmm3/src/MyDialog.hh @@ -10,6 +10,7 @@ class MyDialog : public Gtk::Dialog{ public: MyDialog(BaseObjectType* cobject,const Glib::RefPtr& ref_builder); static MyDialog * create(Gtk::Window& parent); + void init_json_data(json &data1); protected: void on_response(int response_id) override; private: @@ -21,9 +22,6 @@ private: //Strings to store path on Windows and Unix-Like systems std::string config_win32,config_unix; - // Json data - json data; - //Signal Handlers Glib::RefPtr dialog; void btnpath_clicked(); diff --git a/XeRelease_Gtkmm3/src/MyWin.cc b/XeRelease_Gtkmm3/src/MyWin.cc index e108fc8..86b3a42 100644 --- a/XeRelease_Gtkmm3/src/MyWin.cc +++ b/XeRelease_Gtkmm3/src/MyWin.cc @@ -134,26 +134,18 @@ void MyWin::background3() void MyWin::config_dialog() { + load_config(); auto dialog = MyDialog::create(*this); + dialog->init_json_data(data); dialog->signal_hide().connect(sigc::bind(sigc::mem_fun(*this, &MyWin::on_window_hide), dialog)); dialog->present(); } -void MyWin::on_window_hide(Gtk::Window *window) -{ - delete window; -} +void MyWin::load_config(){ + // Load/Reload json config file -void MyWin::main_releases() -{ - // Get Selection - int version = combo.get_active_row_number(); - char str[57]; - // Get Configs - std::string config_longterm, config_stable, config_devel; // Open json file std::ifstream json_file("xe_config.json"); - json data; if (json_file.is_open()) { // Read data from json file @@ -168,8 +160,22 @@ void MyWin::main_releases() msg_dialog.show_all(); return; } + json_config_init(data); json_file.close(); +} +void MyWin::on_window_hide(Gtk::Window *window) +{ + delete window; +} + +void MyWin::main_releases() +{ + // Get Selection + int version = combo.get_active_row_number(); + char str[57]; + // Get Configs + load_config(); switch (version) // Use Selection to Perform { case Releases::LTS: diff --git a/XeRelease_Gtkmm3/src/MyWin.hh b/XeRelease_Gtkmm3/src/MyWin.hh index 6f2af85..9e3d8d3 100644 --- a/XeRelease_Gtkmm3/src/MyWin.hh +++ b/XeRelease_Gtkmm3/src/MyWin.hh @@ -39,6 +39,9 @@ private: //Version Configs struct tm *local; char api_version[57]; + json data; + std::string config_longterm, config_stable, config_devel; + void load_config(); void config_dialog(); //Signal Handlers diff --git a/XeRelease_Gtkmm3/src/xerelease.cc b/XeRelease_Gtkmm3/src/xerelease.cc index baf610f..76308c3 100644 --- a/XeRelease_Gtkmm3/src/xerelease.cc +++ b/XeRelease_Gtkmm3/src/xerelease.cc @@ -5,9 +5,8 @@ #include "xerelease.hh" #include "xeapi.hh" #include "cfgfile2/cfgfile.hh" -#include "../json_nlohmann/json.hpp" -using json = nlohmann::json; +static json data1; // typedef void(*LP)(struct tm *local);//define a pointer function @@ -78,22 +77,18 @@ int total_year_day(int year1, int year2) static void path_translate(char *result, const char *version) { - // Get Config from json file - std::ifstream json_file("xe_config.json"); - if (json_file.is_open()) + if (!data1.empty()) { - // Read data from json file - json data = json::parse(json_file); // Just combine the release file path and filename std::string path; if (rel_unix_file_system_detected()) { - path = data["Release_Path_Unix"]; + path = data1["Release_Path_Unix"]; sprintf(result, "%s/xe-%c.x", path.c_str(), version[0]); } else { - path = data["Release_Path_Win32"]; + path = data1["Release_Path_Win32"]; sprintf(result, "%s\\xe-%c.x", path.c_str(), version[0]); } } @@ -101,7 +96,6 @@ static void path_translate(char *result, const char *version) { sprintf(result, "./xe-%c.x", version[0]); } - json_file.close(); } void dale(struct tm *local) @@ -175,3 +169,7 @@ void develop(struct tm *local, const char *devel, char *str) fclose(stdout); return; } + +void json_config_init(json &user_data){ + data1 = user_data; +} diff --git a/XeRelease_Gtkmm3/src/xerelease.hh b/XeRelease_Gtkmm3/src/xerelease.hh index 5332ad4..25955ab 100644 --- a/XeRelease_Gtkmm3/src/xerelease.hh +++ b/XeRelease_Gtkmm3/src/xerelease.hh @@ -1,12 +1,18 @@ #ifndef __XE_RELEASE_ #define __XE_RELEASE_ +#include "../json_nlohmann/json.hpp" + +using json = nlohmann::json; + int total_day(int year,int month,int day); int total_year_day(int year1,int year2); void dale(struct tm *local); +void json_config_init(json &user_data); + void longterm(struct tm *local,const char * lts,char *str); void stable(struct tm *local,const char * rel,char *str);