From 5c808943ad63f96aab07ad8f65ca3ababd02389b Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Wed, 13 Dec 2023 16:33:18 +0600 Subject: [PATCH] Added new window for saving confirmation --- gresource.xml | 1 + source/CMakeLists.txt | 1 + source/ubl-settings-logging.c | 233 ++++++++++++++++++++++++++++-- source/ubl-settings-logging.h | 14 ++ ubl-settings-logging-saving.glade | 216 +++++++++++++++++++++++++++ 5 files changed, 449 insertions(+), 16 deletions(-) create mode 100644 ubl-settings-logging-saving.glade diff --git a/gresource.xml b/gresource.xml index 557ead6..23b9b6a 100644 --- a/gresource.xml +++ b/gresource.xml @@ -11,6 +11,7 @@ ubl-settings-logging-filechooser.glade ubl-settings-logging-logrotate-table.glade ubl-settings-logging-terminal.glade + ubl-settings-logging-saving.glade ubl-settings-logging.css diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index c09ec59..0f52e90 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -50,6 +50,7 @@ set(DEPENDFILES ../ubl-settings-logging-logrotate-table.glade ../ubl-settings-logging-filechooser.glade ../ubl-settings-logging-terminal.glade + ../ubl-settings-logging-saving.glade ../gresource.xml ../ubl-settings-logging-banner.png ../ubl-settings-logging.css diff --git a/source/ubl-settings-logging.c b/source/ubl-settings-logging.c index 6ccbbac..6ec2246 100644 --- a/source/ubl-settings-logging.c +++ b/source/ubl-settings-logging.c @@ -26,6 +26,207 @@ void yon_open_browser(GtkWidget *self, char *link){ yon_ubl_browser_window_open(link,TITLE_LABEL); } +void on_save_window_parameter_switched(GtkCellRendererToggle *self, gchar *path, saving_window *window){ + GtkTreeIter iter; + if (gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->list),&iter,path)){ + gboolean is_active; + gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&is_active,-1); + gtk_list_store_set(window->list,&iter,0,!is_active,-1); + } + +} + +void on_save_parameters(GtkWidget *self, saving_window *window){ + char *append_command = yon_char_unite("ubconfig --target ",main_config.load_mode==1?"global":"system"," set ",NULL); + char *remove_command = yon_char_unite("ubconfig --target ",main_config.load_mode==1?"global":"system"," remove ",NULL); + dictionary *final_append=NULL; + dictionary *final_remove=NULL; + GtkTreeIter iter; + int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&iter); + for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&iter)){ + gboolean is_active,can_save; + char *parameter,*old_value,*new_value,*section; + gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&is_active,1,¶meter,2,&old_value,3,&new_value,5,&can_save,6,§ion,-1); + if (is_active&&can_save){ + if(!yon_char_is_empty(parameter)){ + if (yon_char_is_empty(new_value)){ // empty new value - delete + if (yon_dictionary_get(&final_remove,section)){ + final_remove->data = yon_char_unite((char*)final_remove->data," ",parameter,NULL); + } else { + yon_dictionary_add_or_create_if_exists_with_data(final_remove,section, parameter); + } + } else { // non-empty new value - add + if (yon_dictionary_get(&final_append,section)){ + final_append->data=yon_char_unite((char*)final_append->data," ",yon_char_unite(parameter,"=\"",new_value,"\"",NULL),NULL); + } else { + yon_dictionary_add_or_create_if_exists_with_data(final_append,section, yon_char_unite(parameter,"=\"",new_value,"\"",NULL)); + } + } + } + } + } + dictionary *dict = NULL; + if (final_remove){ + for_dictionaries(dict,final_remove){ + char *final_command = yon_char_unite(remove_command," ",dict->key," ",(char*)dict->data,NULL); + system(final_command); + free(final_command); + } + } + if (final_append){ + for_dictionaries(dict,final_append){ + char *final_command = yon_char_unite(append_command," ",dict->key," ",(char*)dict->data,NULL); + system(final_command); + free(final_command); + } + } + on_close_subwindow(self,"SavingWindow"); +} + +saving_window *yon_save_proceed(char *path,YON_CONFIG_TYPE type, ...){ + if (((type==YON_CONFIG_LOCAL&& main_config.load_mode==1)||(type==YON_CONFIG_GLOBAL&& main_config.load_mode==0))){ + yon_config_save_registered(path); + return NULL; + } else { + char *config_to_save = NULL; + if (type==YON_CONFIG_GLOBAL) config_to_save="global"; + else if (type==YON_CONFIG_LOCAL) config_to_save="system"; + else if (type==YON_CONFIG_BOTH) { + if (main_config.load_mode==1){ + config_to_save="global"; + yon_config_save_registered("system"); + } else if (main_config.load_mode==0){ + config_to_save="system"; + yon_config_save_registered("global"); + } + } + config_str config_compare=NULL; + int compare_size=0; + va_list args; + va_start(args,type); + char *cur_section = NULL; + dictionary *section_commands=NULL; + while ((cur_section=va_arg(args,char*))){ + char *cur_parameter = va_arg(args,char*); + yon_dictionary_add_or_create_if_exists_with_data(section_commands,cur_section,cur_parameter); + } + struct loaded_config { + dictionary *dict; + char *section; + }; + + struct loaded_config loaded; + loaded.dict = yon_dictionary_new(); + dictionary *dct; + for_dictionaries(dct,section_commands){ + char *command = yon_char_unite(ubconfig_load_command," ", config_to_save," get ", dct->key," ", yon_dictionary_get_data(dct,char*),NULL); + FILE *output = popen(command, "r"); + char **output_strings = NULL; + output_strings = malloc(sizeof(char*)); + int i = 0; + char str[4096]; + memset(str, 0, 4096); + while (fgets(str, 4096, output)) + { + if (!yon_char_is_empty(str)&& strcmp(str,"(null)\n")!=0) + { + char *final_str = yon_char_new(str); + char *key =yon_char_divide_search(final_str,"=",-1); + if (final_str[strlen(final_str)-1]=='\n')final_str[strlen(final_str)-1]='\0'; + yon_dictionary_add_or_create_if_exists_with_data(loaded.dict,NULL,yon_char_new(dct->key)); + yon_dictionary_add_or_create_if_exists_with_data(loaded.dict,key,yon_char_new(final_str)); + yon_char_parsed_add_or_create_if_exists(config_compare,&compare_size,yon_char_new(str)); + } + } + } + GtkBuilder *builder = gtk_builder_new_from_resource(glade_saving_path); + saving_window *window = malloc(sizeof(saving_window)); + window->Window = yon_gtk_builder_get_widget(builder,"Window"); + window->HeaderImage = yon_gtk_builder_get_widget(builder,"HeaderImage"); + window->HeaderTopic = yon_gtk_builder_get_widget(builder,"HeaderTopic"); + window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox"); + window->ParametersTree = yon_gtk_builder_get_widget(builder,"ParametersTree"); + window->SaveButton = yon_gtk_builder_get_widget(builder,"SaveButton"); + window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton"); + window->ToggleCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"ToggleCell")); + window->list = GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1")); + window->type=type; + gtk_window_set_title(GTK_WINDOW(window->Window),TITLE_LABEL); + g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_close_subwindow),NULL); + g_signal_connect(G_OBJECT(window->SaveButton),"clicked", G_CALLBACK(on_save_parameters),window); + g_signal_connect(G_OBJECT(window->ToggleCell),"toggled", G_CALLBACK(on_save_window_parameter_switched),window); + int config_size=0; + config_str config_strings = yon_config_get_all(&config_size); + if (config_strings){ + GtkTreeIter iter; + gtk_tree_view_set_model(GTK_TREE_VIEW(window->ParametersTree),NULL); + config_str compare_keys = NULL; + compare_keys = yon_char_parsed_copy(config_compare,compare_size); + int compare_keys_size=compare_size; + yon_char_parsed_divide_search_full(compare_keys,compare_keys_size,"=",-1); + int config_keys_size=0; + config_str config_keys = yon_config_get_all_keys(&config_keys_size); + int final_size=0; + GdkRGBA rgba; + rgba.alpha=0.8; + rgba.red=1; + rgba.blue=0.3; + rgba.green=0.65; + char *rgba_string = gdk_rgba_to_string(&rgba); + for (int i=0;ilist,&iter); + gtk_list_store_set(window->list,&iter,0,1,1,compare_keys[i],5,1,-1); + for (int j=0;jfirst,compare_keys[i])->prev->data; + gtk_list_store_set(window->list,&iter,2,compare_value,4,rgba_string,6,cur_section,-1); + } + free(compare_value); + free(compare_name); + } + + } + char *name,*value; + for (int i=0;ilist),&iter); + for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&iter)){ + gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,1,&name,2,&value,-1); + if (!yon_char_is_empty(name)&&!strcmp(name,config_keys[i])){ + gtk_list_store_set(window->list,&iter,3,compare_value,4,NULL,6,section,-1); + if (!strcmp(value,compare_value)){ + gtk_list_store_set(window->list,&iter,0,0,5,0,-1); + } + found=1; + break; + } + } + if (!found){ + GtkTreeIter itar; + gtk_list_store_append(window->list,&itar); + gtk_list_store_set(window->list,&itar,0,1,1,compare_name,3,compare_value,5,1,6,section,-1); + } + free(compare_value); + free(compare_name); + } + free(rgba_string); + + gtk_tree_view_set_model(GTK_TREE_VIEW(window->ParametersTree),GTK_TREE_MODEL(window->list)); + } + + gtk_widget_show(window->Window); + return window; + + } + +} + /**on_open_documentation_confirmation(GtkWidget *self, char *link) * [EN] * Opens confirmation window for [link] link. @@ -204,35 +405,35 @@ void on_load_local(){ } -void yon_save_proceed(char *path,YON_CONFIG_TYPE type){ - if (((type==YON_CONFIG_LOCAL&& main_config.load_mode==1)||(type==YON_CONFIG_GLOBAL&& main_config.load_mode==0))) - yon_config_save_registered(path); - else{ - if (type==YON_CONFIG_BOTH) - yon_launch("ubconfig remove logging LOGROTATE[*] JOURNALD[*]"); - else if (type==YON_CONFIG_LOCAL) - yon_launch("ubconfig --target system remove logging LOGROTATE[*] JOURNALD[*]"); - else if (type==YON_CONFIG_GLOBAL) - yon_launch("ubconfig --target global remove logging LOGROTATE[*] JOURNALD[*]"); - yon_config_force_save_registered(path); - } +// void yon_save_proceed(char *path,YON_CONFIG_TYPE type){ +// if (((type==YON_CONFIG_LOCAL&& main_config.load_mode==1)||(type==YON_CONFIG_GLOBAL&& main_config.load_mode==0))) +// yon_config_save_registered(path); +// else{ +// if (type==YON_CONFIG_BOTH) +// yon_launch("ubconfig remove logging LOGROTATE[*] JOURNALD[*]"); +// else if (type==YON_CONFIG_LOCAL) +// yon_launch("ubconfig --target system remove logging LOGROTATE[*] JOURNALD[*]"); +// else if (type==YON_CONFIG_GLOBAL) +// yon_launch("ubconfig --target global remove logging LOGROTATE[*] JOURNALD[*]"); +// yon_config_force_save_registered(path); +// } -} +// } void on_save_global_local(){ - yon_save_proceed(NULL,YON_CONFIG_BOTH); + yon_save_proceed(NULL,YON_CONFIG_BOTH,"logging", "JOURNALD[*] LOGROTATE[*]",NULL); yon_ubl_status_box_render(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE); } void on_save_global(){ - yon_save_proceed("global",YON_CONFIG_GLOBAL); + yon_save_proceed("global",YON_CONFIG_GLOBAL,"logging", "JOURNALD[*] LOGROTATE[*]",NULL); yon_ubl_status_box_render(LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE); } void on_save_local(){ - yon_save_proceed("system",YON_CONFIG_LOCAL); + yon_save_proceed("system",YON_CONFIG_LOCAL,"logging", "JOURNALD[*] LOGROTATE[*]",NULL); yon_ubl_status_box_render(GLOBAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE); } diff --git a/source/ubl-settings-logging.h b/source/ubl-settings-logging.h index 27c68c3..2e0494e 100644 --- a/source/ubl-settings-logging.h +++ b/source/ubl-settings-logging.h @@ -33,6 +33,7 @@ #define glade_rules_path "/com/ublinux/ui/ubl-settings-logging-rules.glade" #define glade_filechooser_path "/com/ublinux/ui/ubl-settings-logging-filechooser.glade" #define glade_terminal_path "/com/ublinux/ui/ubl-settings-logging-terminal.glade" +#define glade_saving_path "/com/ublinux/ui/ubl-settings-logging-saving.glade" #define banner_path "/com/ublinux/images/ubl-settings-logging-banner.png" #define CssPath "/com/ublinux/css/ubl-settings-logging.css" #define config_path yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL) @@ -357,4 +358,17 @@ typedef struct { char *paths; } logrotate_configure_window; +typedef struct { + GtkWidget *Window; + GtkWidget *HeaderTopic; + GtkWidget *HeaderImage; + GtkWidget *StatusBox; + GtkWidget *ParametersTree; + GtkCellRenderer *ToggleCell; + GtkWidget *CancelButton; + GtkWidget *SaveButton; + GtkListStore *list; + YON_CONFIG_TYPE type; +} saving_window; + main_window *setup_window(); \ No newline at end of file diff --git a/ubl-settings-logging-saving.glade b/ubl-settings-logging-saving.glade new file mode 100644 index 0000000..6dfa9bb --- /dev/null +++ b/ubl-settings-logging-saving.glade @@ -0,0 +1,216 @@ + + + + + + + Save + True + True + False + True + + + True + False + process-stop-symbolic + + + True + False + emblem-ok-symbolic + + + + + + + + + + + + + + + + + + + + + 450 + 500 + False + True + com.ublinux.ubl-settings-logging + dialog + + + False + 5 + vertical + 5 + + + False + end + + + Cancel + True + True + True + image1 + + + True + True + 0 + + + + + Save + True + True + True + image2 + + + True + True + 1 + + + + + False + False + end + 0 + + + + + True + False + vertical + + + + + + False + True + 0 + + + + + True + True + in + + + True + True + liststore1 + + + none + + + + + Save + + + + 4 + 5 + 0 + + + + + + + Parameter + + + + 4 + 5 + 1 + + + + + + + Old value + + + + 4 + 5 + 2 + + + + + + + New value + + + + 4 + 5 + 3 + + + + + + + + + True + True + 1 + + + + + + + True + False + True + + + True + False + Logs and events + + + + + + + + True + False + 32 + com.ublinux.ubl-settings-logging + + + + + + +