You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
390 lines
20 KiB
390 lines
20 KiB
#include "libublsettingsui-gtk3.h"
|
|
|
|
|
|
|
|
//signal emmit handlers - no header initialization
|
|
|
|
void on_subwindow_close(GtkWidget *self){
|
|
gtk_widget_destroy(gtk_widget_get_toplevel(self));
|
|
}
|
|
|
|
void yon_open_browser(GtkWidget *self, char *link){
|
|
yon_ubl_browser_window_open(link,template_app_information.app_title);
|
|
}
|
|
|
|
void on_open_documentation_confirmation(GtkWidget *self, char *link){
|
|
if (template_app_information.always_open_documentation==0){
|
|
GtkBuilder *builder = gtk_builder_new_from_resource(ui_glade_path_documentation);
|
|
template_documentation_confirmation_window *window = malloc(sizeof(template_documentation_confirmation_window));
|
|
window->Window = yon_gtk_builder_get_widget(builder,"helpConfirmationWindow");
|
|
window->AcceptButton = yon_gtk_builder_get_widget(builder,"ReadHelpButton");
|
|
window->CloseButton = yon_gtk_builder_get_widget(builder,"CancelHelpButton");
|
|
window->HeaderLabel = yon_gtk_builder_get_widget(builder,"webHeaderNameLabel");
|
|
window->AlwaysOpenCheck = yon_gtk_builder_get_widget(builder,"AlwaysOpenDocumentationCheckbox");
|
|
gtk_label_set_text(GTK_LABEL(window->HeaderLabel),template_app_information.app_title);
|
|
gtk_widget_show_all(window->Window);
|
|
g_signal_connect(G_OBJECT(window->CloseButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
|
|
g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(yon_open_browser),yon_char_new(link));
|
|
g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
|
|
|
|
|
|
} else {
|
|
yon_open_browser(self,link);
|
|
}
|
|
}
|
|
|
|
/**on_link(GtkWidget *self, char* uri, gpointer user_data)
|
|
* [EN]
|
|
* Signal for hadnling AboutDialog links.
|
|
* Connect to "activate-link" signal.
|
|
* [self] is AboutDialog window;
|
|
* [uri] is activated link;
|
|
* [user_data] is pointer for user data, hasn't used in standard handler;
|
|
* [RU]
|
|
* Функция для обработки сигнала нажатия на ссылку окна AboutDialog.
|
|
* Присоединять к сигналу "activate-link".
|
|
* [self] - окно AboutDialog;
|
|
* [uri] - ссылка, по которой совершается переход;
|
|
* [user_data] - указатель на любые другие данные, не используется в стандартном обработчике;
|
|
*/
|
|
void on_link(GtkWidget *self, char* uri, gpointer user_data){
|
|
gtk_widget_destroy(self);
|
|
on_open_documentation_confirmation(self,uri);
|
|
}
|
|
|
|
void on_about(GtkWidget *self, char *version_application){
|
|
GtkBuilder *builder=gtk_builder_new_from_resource(ui_glade_path_about);
|
|
GtkWidget *window=yon_gtk_builder_get_widget(builder,"AboutWindow");
|
|
GtkWidget *title=yon_gtk_builder_get_widget(builder,"headerAboutTopic");
|
|
GtkWidget *hideButtonBox=yon_gtk_builder_get_widget(builder,"buttonBoxHide");
|
|
gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(window),version_application);
|
|
gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(window),template_app_information.app_title);
|
|
gtk_about_dialog_set_logo_icon_name(GTK_ABOUT_DIALOG(window),yon_char_append("com.ublinux.",template_app_information.app_tech_name));
|
|
gtk_label_set_text(GTK_LABEL(title),template_app_information.app_title);
|
|
g_signal_connect(G_OBJECT(window),"activate-link",G_CALLBACK(on_link),NULL);
|
|
gtk_widget_set_visible(hideButtonBox,0);
|
|
gtk_widget_destroy(hideButtonBox);
|
|
gtk_widget_show(window);
|
|
}
|
|
|
|
//functions
|
|
|
|
void on_save_window_parameter_switched(GtkCellRendererToggle *self, gchar *path, template_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, template_saving_window *window){
|
|
char *append_command = yon_char_unite("ubconfig --target ",template_config->load_mode==1?"global":"system"," set ",NULL);
|
|
char *remove_command = yon_char_unite("ubconfig --target ",template_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[0]!='\"'?"\"":"",new_value,new_value[strlen(new_value)-1]!='\"'?"\"":"",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_subwindow_close(self);
|
|
}
|
|
|
|
template_saving_window *yon_save_proceed(char *path,YON_CONFIG_TYPE type, ...){
|
|
if (((type==YON_CONFIG_LOCAL&& template_config->load_mode==1)||(type==YON_CONFIG_GLOBAL&& template_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 (template_config->load_mode==1){
|
|
config_to_save="global";
|
|
yon_config_save_registered("system");
|
|
} else if (template_config->load_mode==0){
|
|
config_to_save="system";
|
|
yon_config_save_registered("global");
|
|
}
|
|
}
|
|
yon_config_set_status(0);
|
|
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(ui_glade_path_saving);
|
|
template_saving_window *window = malloc(sizeof(template_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_icon_name(GTK_WINDOW(window->Window),yon_char_append("com.ublinux.",template_app_information.app_tech_name));
|
|
gtk_window_set_title(GTK_WINDOW(window->Window),template_app_information.app_title);
|
|
gtk_label_set_text(GTK_LABEL(window->HeaderTopic),template_app_information.app_title);
|
|
gtk_image_set_from_pixbuf(GTK_IMAGE(window->HeaderImage),gtk_icon_theme_load_icon_for_scale(gtk_icon_theme_get_default(),yon_char_append("com.ublinux.",template_app_information.app_tech_name),32,1,GTK_ICON_LOOKUP_FORCE_SIZE,NULL));
|
|
GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(window->ParametersTree));
|
|
for (int i=0;i<g_list_length(list);i++){
|
|
yon_gtk_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
|
|
}
|
|
g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),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;i<compare_keys_size;i++){
|
|
gtk_list_store_append(window->list,&iter);
|
|
gtk_list_store_set(window->list,&iter,0,0,1,compare_keys[i],5,1,-1);
|
|
for (int j=0;j<compare_size;j++){
|
|
if(config_compare[j][strlen(config_compare[j])-1]=='\n') config_compare[j][strlen(config_compare[j])-1]='\0';
|
|
char *compare_value = yon_char_new(config_compare[j]);
|
|
char *compare_name = yon_char_divide_search(compare_value,"=",-1);
|
|
if (!strcmp(compare_name,compare_keys[i])){
|
|
char *cur_section = (char*)yon_dictionary_get(&loaded.dict->first,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;i<config_keys_size;i++){
|
|
int found=0;
|
|
char *compare_value = yon_char_new(config_strings[i]);
|
|
char *compare_name = yon_char_divide_search(compare_value,"=",-1);
|
|
char *section = yon_config_get_section_for_key(compare_name);
|
|
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)){
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// standard functions
|
|
|
|
int yon_ubl_connect_config(_template_config *config){
|
|
int replaces;
|
|
if(!template_config)
|
|
replaces=0;
|
|
else
|
|
replaces=1;
|
|
template_config = config;
|
|
return replaces;
|
|
}
|
|
|
|
template_main_window *setup_window(){
|
|
/* Widgets getting | Получение виджетов */
|
|
template_main_window *widgets = malloc(sizeof(template_main_window));
|
|
GtkBuilder *builder = gtk_builder_new_from_resource(ui_glade_path);
|
|
widgets->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
|
|
widgets->HeadLabel = yon_gtk_builder_get_widget(builder,"headerTopic");
|
|
widgets->PlugBox = yon_gtk_builder_get_widget(builder,"plugBox");
|
|
|
|
widgets->HeadOverlay = yon_gtk_builder_get_widget(builder,"HeadOverlay");
|
|
widgets->HeadImage = yon_gtk_builder_get_widget(builder,"HeadBackgroundImage");
|
|
widgets->HeadBox = yon_gtk_builder_get_widget(builder,"HeaderBox");
|
|
widgets->HeadTitleLabel = yon_gtk_builder_get_widget(builder,"HeaderTitleLabel");
|
|
widgets->HeadInfoLabel = yon_gtk_builder_get_widget(builder,"HeaderInfoLabel");
|
|
|
|
widgets->StatusBox = yon_gtk_builder_get_widget(builder,"mainStatusBox");
|
|
widgets->StatusIcon = yon_gtk_builder_get_widget(builder,"mainStatusIcon");
|
|
widgets->StatusLabel = yon_gtk_builder_get_widget(builder,"mainStatusLabel");
|
|
|
|
widgets->SaveMenuItem = yon_gtk_builder_get_widget(builder,"SaveGlobalLocalConfigurationMenuItem");
|
|
widgets->SaveGlobalMenuItem = yon_gtk_builder_get_widget(builder,"SaveGlobalConfigurationMenuItem");
|
|
widgets->SaveLocalMenuItem = yon_gtk_builder_get_widget(builder,"SaveLocalConfigurationMenuItem");
|
|
widgets->RightBox = yon_gtk_builder_get_widget(builder,"HeaderRightBox");
|
|
|
|
widgets->LoadGlobalMenuItem = yon_gtk_builder_get_widget(builder,"LoadGlobalConfigurationMenuItem");
|
|
widgets->LoadLocalMenuItem = yon_gtk_builder_get_widget(builder,"LoadLocalConfigurationMenuItem");
|
|
widgets->LeftBox = yon_gtk_builder_get_widget(builder,"HeaderLeftBox");
|
|
|
|
widgets->InterfaceBox = yon_gtk_builder_get_widget(builder,"InterfaceBox");
|
|
GtkWidget *HatIcon = yon_gtk_builder_get_widget(builder,"HatIcon");
|
|
GtkWidget *HeadImage = yon_gtk_builder_get_widget(builder,"HeadImage");
|
|
|
|
widgets->DocumentationMenuItem = yon_ubl_menu_item_documentation_new(DOCUMENTATION_LABEL);
|
|
widgets->AboutMenuItem = yon_ubl_menu_item_about_new(ABOUT_LABEL);
|
|
|
|
gtk_window_set_icon_name(GTK_WINDOW(widgets->Window),yon_char_append("com.ublinux.",template_app_information.app_tech_name));
|
|
// gtk_image_set_from_icon_name(widgets->HeadImage,)
|
|
gtk_image_set_from_pixbuf(GTK_IMAGE(HeadImage),gtk_icon_theme_load_icon_for_scale(gtk_icon_theme_get_default(),yon_char_append("com.ublinux.",template_app_information.app_tech_name),32,1,GTK_ICON_LOOKUP_FORCE_SIZE,NULL));
|
|
gtk_image_set_from_pixbuf(GTK_IMAGE(HatIcon),gtk_icon_theme_load_icon_for_scale(gtk_icon_theme_get_default(),yon_char_append("com.ublinux.",template_app_information.app_tech_name),64,1,GTK_ICON_LOOKUP_FORCE_SIZE,NULL));
|
|
gtk_window_set_title(GTK_WINDOW(widgets->Window),template_app_information.app_title);
|
|
gtk_label_set_text(GTK_LABEL(widgets->HeadLabel),template_app_information.app_title);
|
|
gtk_label_set_text(GTK_LABEL(widgets->HeadTitleLabel),template_app_information.app_title);
|
|
gtk_label_set_text(GTK_LABEL(widgets->HeadInfoLabel),template_app_information.app_description);
|
|
|
|
GtkWidget *menu = yon_gtk_builder_get_widget(builder,"menu2");
|
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->DocumentationMenuItem);
|
|
gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->AboutMenuItem);
|
|
|
|
/* Widget registration for config monitoring | Регистрация виджетов для мониторинга конфига */
|
|
// yon_window_config_add_listener(widgets->HeadInfoLabel,"head-text","label",YON_TYPE_STRING);
|
|
|
|
/* Signal connection | Присоединение сигналов */
|
|
g_signal_connect(G_OBJECT(widgets->Window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
|
|
g_signal_connect(G_OBJECT(widgets->DocumentationMenuItem),"activate",G_CALLBACK(on_open_documentation_confirmation),template_app_information.wiki_link);
|
|
g_signal_connect(G_OBJECT(widgets->AboutMenuItem),"activate",G_CALLBACK(on_about),NULL);
|
|
|
|
|
|
gtk_widget_show(widgets->Window);
|
|
return widgets;
|
|
}
|
|
|
|
int yon_ubl_window_init(char *app_title, char *app_description, char *locale, char *css, char *tech_name, char *version, char *wiki){
|
|
template_app_information.app_title=app_title;
|
|
template_app_information.css_path=css;
|
|
template_app_information.app_tech_name=tech_name;
|
|
template_app_information.app_version=version;
|
|
template_app_information.wiki_link=wiki;
|
|
template_app_information.app_description=app_description;
|
|
|
|
template_app_information.app_locale=locale;
|
|
}
|
|
|
|
template_main_window *yon_ubl_window_setup(){
|
|
textdomain (template_ui_LocaleName);
|
|
setlocale(LC_ALL,"");
|
|
|
|
template_main_window *widgets = setup_window();
|
|
yon_ubl_header_setup_resource(widgets->HeadOverlay,widgets->HeadBox,widgets->HeadImage,ui_banner_path);
|
|
|
|
if (yon_ubl_status_box_setup(widgets->StatusIcon,widgets->StatusBox,widgets->StatusLabel)){
|
|
if (getuid()!=0)
|
|
yon_ubl_status_box_render(ROOT_WARNING_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
|
|
else
|
|
yon_ubl_status_box_render(LOCAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
|
|
}
|
|
template_config->load_mode=1;
|
|
if(template_config)
|
|
yon_ubl_setup_sockets(widgets->PlugBox,widgets->LeftBox,widgets->RightBox,template_config->socket_id,template_config->load_socket_id,template_config->save_socket_id);
|
|
yon_window_config_setup(GTK_WINDOW(widgets->Window));
|
|
yon_window_config_load(ui_config_path);
|
|
GtkCssProvider *css=gtk_css_provider_new();
|
|
gtk_css_provider_load_from_resource(css,ui_CssPath);
|
|
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
|
|
GTK_STYLE_PROVIDER(css),
|
|
-1);
|
|
|
|
return widgets;
|
|
}
|