Update config page

This commit is contained in:
daleclack 2022-07-26 13:34:28 +08:00
parent f4d34a4886
commit a7ac9b4649
3 changed files with 101 additions and 108 deletions

View File

@ -208,39 +208,4 @@
</packing> </packing>
</child> </child>
</object> </object>
<object class="GtkDialog" id="prefs1">
<property name="can-focus">False</property>
<property name="margin-end">10</property>
<property name="type-hint">dialog</property>
<child internal-child="vbox">
<object class="GtkBox">
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox">
<property name="can-focus">False</property>
<property name="layout-style">end</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface> </interface>

View File

@ -4,8 +4,6 @@ MyPrefs::MyPrefs(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_
: Gtk::Box(cobject), : Gtk::Box(cobject),
ref_Glade(ref_builder) ref_Glade(ref_builder)
{ {
// // Initalize
// set_icon_name("XeRelease");
// Get Widgets // Get Widgets
ref_builder->get_widget("entry_lts", entry_lts); ref_builder->get_widget("entry_lts", entry_lts);
ref_builder->get_widget("entry_stable", entry_stable); ref_builder->get_widget("entry_stable", entry_stable);
@ -13,25 +11,12 @@ MyPrefs::MyPrefs(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_
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);
ref_builder->get_widget("btn_ok", btnok); ref_builder->get_widget("btn_ok", btnok);
ref_builder->get_widget("btn_cancel", btncancel);
// json_file.close();
// std::string config;
// if(readCfgFile("xe_config","Longterm",config)){
// entry_lts->set_text(config);
// }
// if(readCfgFile("xe_config","Stable",config)){
// entry_stable->set_text(config);
// }
// if(readCfgFile("xe_config","Develop",config)){
// entry_dev->set_text(config);
// }
// readCfgFile("xe_config","Release_Path_Unix",config_unix);
// readCfgFile("xe_config","Release_Path_Win32",config_win32);
// Connect Signal // Connect Signal
btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnpath_clicked)); btnpath->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnpath_clicked));
btnok->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnok_clicked)); btnok->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnok_clicked));
btncancel->signal_clicked().connect(sigc::mem_fun(*this, &MyPrefs::btnreset_clicked));
} }
void MyPrefs::btnok_clicked() void MyPrefs::btnok_clicked()
@ -54,14 +39,19 @@ void MyPrefs::btnok_clicked()
outfile.open("xe_config.json", std::ios_base::out); outfile.open("xe_config.json", std::ios_base::out);
/*OutPut contents to the file /*OutPut contents to the file
Simple Contents of xe_config: Simple Contents of xe_config:
Longterm=x.x {
Stable=x.x "Longterm":"x.x",
Develop=x.x "Stable":"x.x",
Release_Path_Unix=/xxx/xxx "Develop":"x.x",
Release_Path_Win32=X:\xxx\xxx "Release_Path_Unix":"",
"Release_Path_Win32":""
}
*/ */
if (outfile.is_open()) if (outfile.is_open())
{ {
// BackUp the original data
data_backup = data;
// Create json object // Create json object
json out_data = json::parse(R"( json out_data = json::parse(R"(
{ {
@ -80,35 +70,51 @@ void MyPrefs::btnok_clicked()
out_data["Release_Path_Unix"] = config_unix; out_data["Release_Path_Unix"] = config_unix;
out_data["Release_Path_Win32"] = config_win32; out_data["Release_Path_Win32"] = config_win32;
outfile << out_data; outfile << out_data;
// outfile<<"This is the config file of Xe Release"<<std::endl;
// outfile<<"See more on github.com/daleclack/Xe-Release"<<std::endl; // Set Current json data
// outfile<<std::endl; data = out_data;
// config=entry_lts->get_text();
// outfile<<"Longterm="<<config<<std::endl; // Show Dialog
// config=entry_stable->get_text(); msg_dialog1.Init("Config File Saved!");
// outfile<<"Stable="<<config<<std::endl; msg_dialog1.show_all();
// config=entry_dev->get_text();
// outfile<<"Develop="<<config<<std::endl;
// outfile<<"Release_Path_Unix="<<config_unix<<std::endl;
// outfile<<"Release_Path_Win32="<<config_win32<<std::endl;
} }
outfile.close(); outfile.close();
} }
void MyPrefs::btnreset_clicked()
{
// Restore the backup data
data = data_backup;
// Reset content of entries
reset_entries();
// Show Dialog
msg_dialog1.Init("Config Reseted!\n Press \"OK\" to save.");
msg_dialog1.show_all();
}
void MyPrefs::init_json_data(json &data1) void MyPrefs::init_json_data(json &data1)
{ {
// Read Configs // Read Configs
std::string config_longterm, config_stable, config_devel;
// Open json file // Open json file
if (!data1.empty()) if (!data1.empty())
{ {
// Read data from json file data = data1;
// data = json::parse(json_file); // Set the content of entry
config_longterm = data1["Longterm"]; reset_entries();
config_stable = data1["Stable"]; }
config_devel = data1["Develop"]; }
config_unix = data1["Release_Path_Unix"];
config_win32 = data1["Release_Path_Win32"]; void MyPrefs::reset_entries()
{
std::string config_longterm, config_stable, config_devel;
// Read json data
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 // Set text from json file data
entry_lts->set_text(config_longterm); entry_lts->set_text(config_longterm);
@ -125,7 +131,6 @@ void MyPrefs::init_json_data(json &data1)
entry_path->set_text(config_win32); entry_path->set_text(config_win32);
} }
} }
}
MyPrefs *MyPrefs::create() MyPrefs *MyPrefs::create()
{ {
@ -138,8 +143,10 @@ MyPrefs *MyPrefs::create()
return box; return box;
} }
void MyPrefs::set_parent_win(Gtk::Window *parent){ void MyPrefs::set_parent_win(Gtk::Window *parent)
{
parent_win = parent; parent_win = parent;
msg_dialog1.set_transient_for(*parent);
} }
void MyPrefs::btnpath_clicked() void MyPrefs::btnpath_clicked()
@ -179,6 +186,20 @@ MsgBox::MsgBox(Gtk::Window &parent)
vbox->pack_start(hbox); vbox->pack_start(hbox);
} }
MsgBox::MsgBox()
{
// Initalize MsgBox
set_icon_name("Xe-Release");
set_default_size(300, 150);
add_button("OK", Gtk::RESPONSE_OK);
// Add Message
image.set_from_icon_name("Xe-Release", Gtk::ICON_SIZE_DIALOG);
vbox = get_content_area();
hbox.pack_start(image, Gtk::PACK_SHRINK);
hbox.pack_start(msg_label, Gtk::PACK_SHRINK);
vbox->pack_start(hbox);
}
void MsgBox::Init(Glib::ustring msg) void MsgBox::Init(Glib::ustring msg)
{ {
msg_label.set_label(msg); msg_label.set_label(msg);

View File

@ -6,6 +6,24 @@
using json = nlohmann::json; using json = nlohmann::json;
class MsgBox : public Gtk::Dialog
{
public:
MsgBox(Gtk::Window &parent);
MsgBox();
void Init(Glib::ustring msg);
protected:
// Signal Handler
void on_response(int response_id) override;
private:
// Child Widgets
Gtk::Image image;
Gtk::Label msg_label;
Gtk::Box *vbox, hbox;
};
class MyPrefs : public Gtk::Box class MyPrefs : public Gtk::Box
{ {
public: public:
@ -21,33 +39,22 @@ private:
// Child widgets // Child widgets
Gtk::Entry *entry_lts, *entry_stable, *entry_dev, *entry_path; Gtk::Entry *entry_lts, *entry_stable, *entry_dev, *entry_path;
Gtk::Button *btnpath, *btnok; Gtk::Button *btnpath, *btnok, *btncancel;
// 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, data_backup;
// Messagebox
MsgBox msg_dialog1;
// Signal Handlers // Signal Handlers
Glib::RefPtr<Gtk::FileChooserNative> dialog; Glib::RefPtr<Gtk::FileChooserNative> dialog;
void btnpath_clicked(); void btnpath_clicked();
void dialog_response(int response_id); void dialog_response(int response_id);
void btnok_clicked(); void btnok_clicked();
}; void btnreset_clicked();
void reset_entries();
class MsgBox : public Gtk::Dialog
{
public:
MsgBox(Gtk::Window &parent);
void Init(Glib::ustring msg);
protected:
// Signal Handler
void on_response(int response_id) override;
private:
// Child Widgets
Gtk::Image image;
Gtk::Label msg_label;
Gtk::Box *vbox, hbox;
}; };
static inline bool unix_file_system_detected() static inline bool unix_file_system_detected()