Adjust the method to load config file

This commit is contained in:
daleclack 2022-07-26 11:00:41 +08:00
parent 07aa1278a5
commit bac367f9ed
6 changed files with 69 additions and 56 deletions

View File

@ -13,27 +13,7 @@ MyDialog::MyDialog(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &re
ref_builder->get_widget("entry_path", entry_path); ref_builder->get_widget("entry_path", entry_path);
ref_builder->get_widget("btnpath", btnpath); ref_builder->get_widget("btnpath", btnpath);
// Read Configs // json_file.close();
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();
// std::string config; // std::string config;
// if(readCfgFile("xe_config","Longterm",config)){ // if(readCfgFile("xe_config","Longterm",config)){
@ -48,16 +28,6 @@ MyDialog::MyDialog(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &re
// readCfgFile("xe_config","Release_Path_Unix",config_unix); // readCfgFile("xe_config","Release_Path_Unix",config_unix);
// readCfgFile("xe_config","Release_Path_Win32",config_win32); // 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 // Connect Signal
btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyDialog::btnpath_clicked)); btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyDialog::btnpath_clicked));
} }
@ -127,6 +97,38 @@ void MyDialog::on_response(int response_id)
hide(); 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) MyDialog *MyDialog::create(Gtk::Window &parent)
{ {
// Create a dialog // Create a dialog

View File

@ -10,6 +10,7 @@ class MyDialog : public Gtk::Dialog{
public: public:
MyDialog(BaseObjectType* cobject,const Glib::RefPtr<Gtk::Builder>& ref_builder); MyDialog(BaseObjectType* cobject,const Glib::RefPtr<Gtk::Builder>& ref_builder);
static MyDialog * create(Gtk::Window& parent); static MyDialog * create(Gtk::Window& parent);
void init_json_data(json &data1);
protected: protected:
void on_response(int response_id) override; void on_response(int response_id) override;
private: private:
@ -21,9 +22,6 @@ private:
//Strings to store path on Windows and Unix-Like systems //Strings to store path on Windows and Unix-Like systems
std::string config_win32,config_unix; std::string config_win32,config_unix;
// Json data
json data;
//Signal Handlers //Signal Handlers
Glib::RefPtr<Gtk::FileChooserNative> dialog; Glib::RefPtr<Gtk::FileChooserNative> dialog;
void btnpath_clicked(); void btnpath_clicked();

View File

@ -134,26 +134,18 @@ void MyWin::background3()
void MyWin::config_dialog() void MyWin::config_dialog()
{ {
load_config();
auto dialog = MyDialog::create(*this); 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->signal_hide().connect(sigc::bind(sigc::mem_fun(*this, &MyWin::on_window_hide), dialog));
dialog->present(); dialog->present();
} }
void MyWin::on_window_hide(Gtk::Window *window) void MyWin::load_config(){
{ // Load/Reload json config file
delete window;
}
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 // Open json file
std::ifstream json_file("xe_config.json"); std::ifstream json_file("xe_config.json");
json data;
if (json_file.is_open()) if (json_file.is_open())
{ {
// Read data from json file // Read data from json file
@ -168,8 +160,22 @@ void MyWin::main_releases()
msg_dialog.show_all(); msg_dialog.show_all();
return; return;
} }
json_config_init(data);
json_file.close(); 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 switch (version) // Use Selection to Perform
{ {
case Releases::LTS: case Releases::LTS:

View File

@ -39,6 +39,9 @@ private:
//Version Configs //Version Configs
struct tm *local; struct tm *local;
char api_version[57]; char api_version[57];
json data;
std::string config_longterm, config_stable, config_devel;
void load_config();
void config_dialog(); void config_dialog();
//Signal Handlers //Signal Handlers

View File

@ -5,9 +5,8 @@
#include "xerelease.hh" #include "xerelease.hh"
#include "xeapi.hh" #include "xeapi.hh"
#include "cfgfile2/cfgfile.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 // 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) static void path_translate(char *result, const char *version)
{ {
// Get Config from json file if (!data1.empty())
std::ifstream json_file("xe_config.json");
if (json_file.is_open())
{ {
// Read data from json file
json data = json::parse(json_file);
// Just combine the release file path and filename // Just combine the release file path and filename
std::string path; std::string path;
if (rel_unix_file_system_detected()) 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]); sprintf(result, "%s/xe-%c.x", path.c_str(), version[0]);
} }
else else
{ {
path = data["Release_Path_Win32"]; path = data1["Release_Path_Win32"];
sprintf(result, "%s\\xe-%c.x", path.c_str(), version[0]); 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]); sprintf(result, "./xe-%c.x", version[0]);
} }
json_file.close();
} }
void dale(struct tm *local) void dale(struct tm *local)
@ -175,3 +169,7 @@ void develop(struct tm *local, const char *devel, char *str)
fclose(stdout); fclose(stdout);
return; return;
} }
void json_config_init(json &user_data){
data1 = user_data;
}

View File

@ -1,12 +1,18 @@
#ifndef __XE_RELEASE_ #ifndef __XE_RELEASE_
#define __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_day(int year,int month,int day);
int total_year_day(int year1,int year2); int total_year_day(int year1,int year2);
void dale(struct tm *local); void dale(struct tm *local);
void json_config_init(json &user_data);
void longterm(struct tm *local,const char * lts,char *str); void longterm(struct tm *local,const char * lts,char *str);
void stable(struct tm *local,const char * rel,char *str); void stable(struct tm *local,const char * rel,char *str);