Add json file support

This commit is contained in:
daleclack 2022-07-24 18:23:10 +08:00
parent 1e6260ad75
commit 241211c4e9
4 changed files with 22234 additions and 78 deletions

View File

@ -1,5 +1,11 @@
{ {
"C_Cpp.errorSquiggles": "Disabled", "C_Cpp.errorSquiggles": "Disabled",
"cmake.configureOnOpen": false, "cmake.configureOnOpen": false,
"C_Cpp.dimInactiveRegions": false "C_Cpp.dimInactiveRegions": false,
"files.associations": {
"array": "cpp",
"string": "cpp",
"string_view": "cpp",
"*.inc": "cpp"
}
} }

View File

@ -1,57 +1,86 @@
#include "MyDialog.hh" #include "MyDialog.hh"
#include "../cfgfile2/cfgfile.hh"
MyDialog::MyDialog(BaseObjectType* cobject,const Glib::RefPtr<Gtk::Builder>& ref_builder) MyDialog::MyDialog(BaseObjectType *cobject, const Glib::RefPtr<Gtk::Builder> &ref_builder)
:Gtk::Dialog(cobject), : Gtk::Dialog(cobject),
ref_Glade(ref_builder) ref_Glade(ref_builder)
{ {
//Initalize // Initalize
set_icon_name("XeRelease"); 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);
ref_builder->get_widget("entry_dev",entry_dev); ref_builder->get_widget("entry_dev", entry_dev);
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 // Read Configs
std::string config; std::string config_longterm, config_stable, config_devel;
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);
//Use different path for Linux filesystem and Windows // Open json file
if(unix_file_system_detected()){ 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);
}
// 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);
// Use different path for Linux filesystem and Windows
if (unix_file_system_detected())
{
entry_path->set_text(config_unix); entry_path->set_text(config_unix);
}else{ }
else
{
entry_path->set_text(config_win32); 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));
} }
void MyDialog::on_response(int response_id){ void MyDialog::on_response(int response_id)
//Save Configs to a file {
if(response_id == Gtk::RESPONSE_OK){ // Save Configs to a file
if (response_id == Gtk::RESPONSE_OK)
{
Glib::ustring config; Glib::ustring config;
std::fstream outfile; std::fstream outfile;
//Initalize Config for path
if(unix_file_system_detected()){ // Initalize Config for path
if (unix_file_system_detected())
{
config_unix = entry_path->get_text(); config_unix = entry_path->get_text();
}else{
config_win32 = entry_path->get_text();
} }
//Open the config file else
outfile.open("xe_config",std::ios_base::out); {
config_win32 = entry_path->get_text();
}
// Open the config file
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 Longterm=x.x
@ -59,48 +88,71 @@ void MyDialog::on_response(int response_id){
Develop=x.x Develop=x.x
Release_Path_Unix=/xxx/xxx Release_Path_Unix=/xxx/xxx
Release_Path_Win32=X:\xxx\xxx Release_Path_Win32=X:\xxx\xxx
*/ */
if(outfile.is_open()){ if (outfile.is_open())
outfile<<"This is the config file of Xe Release"<<std::endl; {
outfile<<"See more on github.com/daleclack/Xe-Release"<<std::endl; // Create json object
outfile<<std::endl; json out_data = json::parse(R"(
config=entry_lts->get_text(); {
outfile<<"Longterm="<<config<<std::endl; "Longterm":"",
config=entry_stable->get_text(); "Stable":"",
outfile<<"Stable="<<config<<std::endl; "Develop":"",
config=entry_dev->get_text(); "Release_Path_Unix":"",
outfile<<"Develop="<<config<<std::endl; "Release_Path_Win32":""
outfile<<"Release_Path_Unix="<<config_unix<<std::endl; }
outfile<<"Release_Path_Win32="<<config_win32<<std::endl; )");
// Load config to json file
out_data["Longterm"] = entry_lts->get_text();
out_data["Stable"] = entry_stable->get_text();
out_data["Develop"] = entry_dev->get_text();
out_data["Release_Path_Unix"] = config_unix;
out_data["Release_Path_Win32"] = config_win32;
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;
// outfile<<std::endl;
// config=entry_lts->get_text();
// outfile<<"Longterm="<<config<<std::endl;
// config=entry_stable->get_text();
// outfile<<"Stable="<<config<<std::endl;
// 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();
} }
hide(); hide();
} }
MyDialog * MyDialog::create(Gtk::Window& parent){ MyDialog *MyDialog::create(Gtk::Window &parent)
//Create a dialog {
auto builder=Gtk::Builder::create_from_resource("/org/gtk/daleclack/prefs.ui"); // Create a dialog
auto builder = Gtk::Builder::create_from_resource("/org/gtk/daleclack/prefs.ui");
MyDialog * dialog=nullptr;
builder->get_widget_derived("prefs",dialog); MyDialog *dialog = nullptr;
builder->get_widget_derived("prefs", dialog);
dialog->set_transient_for(parent); dialog->set_transient_for(parent);
return dialog; return dialog;
} }
void MyDialog::btnpath_clicked(){ void MyDialog::btnpath_clicked()
//Create a Dialog {
dialog = Gtk::FileChooserNative::create("Select a folder",*this, // Create a Dialog
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER,"OK","Cancel"); dialog = Gtk::FileChooserNative::create("Select a folder", *this,
Gtk::FILE_CHOOSER_ACTION_SELECT_FOLDER, "OK", "Cancel");
dialog->signal_response().connect(sigc::mem_fun(*this,&MyDialog::dialog_response));
dialog->signal_response().connect(sigc::mem_fun(*this, &MyDialog::dialog_response));
dialog->show(); dialog->show();
} }
void MyDialog::dialog_response(int response_id){ void MyDialog::dialog_response(int response_id)
if(response_id == Gtk::RESPONSE_ACCEPT){ {
if (response_id == Gtk::RESPONSE_ACCEPT)
{
Glib::ustring path = dialog->get_filename(); Glib::ustring path = dialog->get_filename();
entry_path->set_text(path); entry_path->set_text(path);
} }
@ -108,25 +160,27 @@ void MyDialog::dialog_response(int response_id){
} }
MsgBox::MsgBox(Gtk::Window &parent) MsgBox::MsgBox(Gtk::Window &parent)
:hbox(Gtk::ORIENTATION_HORIZONTAL,5) : hbox(Gtk::ORIENTATION_HORIZONTAL, 5)
{ {
//Initalize MsgBox // Initalize MsgBox
set_icon_name("Xe-Release"); set_icon_name("Xe-Release");
set_default_size(300,150); set_default_size(300, 150);
add_button("OK",Gtk::RESPONSE_OK); add_button("OK", Gtk::RESPONSE_OK);
set_transient_for(parent); set_transient_for(parent);
//Add Message // Add Message
image.set_from_icon_name("Xe-Release",Gtk::ICON_SIZE_DIALOG); image.set_from_icon_name("Xe-Release", Gtk::ICON_SIZE_DIALOG);
vbox=get_content_area(); vbox = get_content_area();
hbox.pack_start(image,Gtk::PACK_SHRINK); hbox.pack_start(image, Gtk::PACK_SHRINK);
hbox.pack_start(msg_label,Gtk::PACK_SHRINK); hbox.pack_start(msg_label, Gtk::PACK_SHRINK);
vbox->pack_start(hbox); 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);
} }
void MsgBox::on_response(int response_id){ void MsgBox::on_response(int response_id)
{
hide(); hide();
} }

View File

@ -2,7 +2,9 @@
#include <gtkmm.h> #include <gtkmm.h>
#include <fstream> #include <fstream>
#include "../cfgfile2/cfgfile.hh" #include "../json_nlohmann/json.hpp"
using json = nlohmann::json;
class MyDialog : public Gtk::Dialog{ class MyDialog : public Gtk::Dialog{
public: public:
@ -19,6 +21,9 @@ 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();

22091
json_nlohmann/json.hpp Normal file

File diff suppressed because it is too large Load Diff