diff --git a/Gtk4/gtk153_time_reminder/src/MyReminder.cpp b/Gtk4/gtk153_time_reminder/src/MyReminder.cpp index 5cf40ff..551cc94 100644 --- a/Gtk4/gtk153_time_reminder/src/MyReminder.cpp +++ b/Gtk4/gtk153_time_reminder/src/MyReminder.cpp @@ -1,14 +1,58 @@ #include "MyReminder.h" +#include #include "timer.h" struct _MyReminder { GtkApplicationWindow parent_instance; GtkWidget *time_label; + int year, month, day; + std::string color; }; G_DEFINE_TYPE(MyReminder, my_reminder, GTK_TYPE_APPLICATION_WINDOW) +static void load_json_data(MyReminder *self) +{ + std::fstream json_file; + json json_data; + std::string default_color("blue"); + + // Open json file for data + json_file.open("config.json", std::ios_base::in); + + // Load json data + if (json_file.is_open()) + { + json_data = json::parse(json_file); + } + else + { + // Json file not exist, load default config + self->color = default_color; + self->year = 2023; + self->month = 12; + self->day = 23; + } + + if (!json_data.empty()){ + // Get json data + std::string tmp_color = json_data["color_set"]; + self->color = tmp_color; + self->year = json_data["year"]; + self->month = json_data["month"]; + self->day = json_data["day"]; + } + else + { + // No json data, load default config + self->color = default_color; + self->year = 2023; + self->month = 12; + self->day = 23; + } +} + static void my_reminder_init(MyReminder *self) { // Set properties of window @@ -16,18 +60,26 @@ static void my_reminder_init(MyReminder *self) gtk_window_set_default_size(GTK_WINDOW(self), 240, 200); gtk_window_set_title(GTK_WINDOW(self), "Reminder"); + // Load data from json file + load_json_data(self); + // Create and add a label self->time_label = gtk_label_new(" "); // Get time duration char time_str[57]; - int time = get_time_duration(2023, 12, 23); - if(time >= 0){ - snprintf(time_str, sizeof(time_str), "%d\nDays Left", time); - }else{ - strncpy(time_str, "Time is out!", 13); + int time = get_time_duration(self->year, self->month, self->day); + if (time >= 0) + { + snprintf(time_str, sizeof(time_str), + "%d\nDays Left", + self->color.c_str(), time); } - + else + { + strncpy(time_str, "Time is out!", 56); + } + // Text style of the label gtk_label_set_markup(GTK_LABEL(self->time_label), time_str); gtk_label_set_use_markup(GTK_LABEL(self->time_label), TRUE);