#include "ubl-utils.h" // dictionary functions /**[EN] * yon_dictionary_create_empty(): * Creates and returns empty dictionary */ dictionary *yon_dictionary_create_empty() { dictionary *dict = malloc(sizeof(dictionary)); dict->data = NULL; dict->key = NULL; dict->next = NULL; dict->prev = NULL; dict->first = dict; dict->data_type = DICTIONARY_OTHER_TYPE; return dict; } dictionary *yon_dictionary_copy(dictionary *dict){ dictionary *dct = yon_dictionary_create_with_data(dict->key,dict->data); dct->data_type= dict->data_type; } /**yon_dictionary_copy_deep(dictionary *dict) * [EN] * * [RU] * Создаёт полную копию словаря [dict] и возвращает первый элемент */ dictionary *yon_dictionary_copy_deep(dictionary *dict){ dictionary *dct = NULL; dictionary *newone=NULL; for_dictionaries(dct,dict){ yon_dictionary_add_or_create_if_exists_with_data(newone,dct->key,dct->data); newone->data_type=dct->data_type; } return newone->first; } size_t yon_dict_len(dictionary *dict) { dictionary* ptr_dict = NULL; size_t size = 0; if (dict != NULL) { for_dictionaries(ptr_dict, dict) { size+=1; } } return size; } void yon_dictionary_switch_to_last(dictionary **dict) { if ((*dict)->next != NULL) for ((*dict) = (*dict)->first; (*dict)->next != NULL; (*dict) = (*dict)->next) { } } void philos_array_string_remove_char(char*** array, char* str_remove, int size) { for (int index = 0; index < size; index++) { (*array)[index] = yon_char_divide_search((*array)[index], str_remove, -1); } } dictionary *yon_dictionary_create_conneced(dictionary *targetdict) { targetdict = yon_dictionary_get_last(targetdict); targetdict->next = yon_dictionary_create_empty(); targetdict->next->prev = targetdict; targetdict->next->first = targetdict->first; targetdict->next->data_type = DICTIONARY_OTHER_TYPE; return targetdict->next; } dictionary *yon_dictionary_get_last(dictionary *dict) { dictionary *dct = NULL; for (dct = dict->first; dct->next != NULL; dct = dct->next) { } return dct; } dictionary *yon_dictionary_switch_places(dictionary *dict, int aim) { if (aim < 0) { if (dict->prev) { if (dict->prev->prev) { dictionary *next = dict->next, *prev = dict->prev, *preprev = prev->prev; if (next) { preprev->next = dict; dict->prev = preprev; dict->next = prev; prev->prev = dict; prev->next = next; next->prev = prev; } else { preprev->next = dict; dict->prev = preprev; dict->next = prev; prev->prev = dict; prev->next = NULL; } return prev; } else { dictionary *next = dict->next, *prev = dict->prev; if (next) { yon_dictionary_make_first(dict); dict->prev = NULL; dict->next = prev; prev->prev = dict; prev->next = next; next->prev = prev; } else { dict->prev = NULL; dict->next = prev; prev->prev = dict; prev->next = NULL; } return prev; } } } else if (aim > 0) { if (dict->next) { if (dict->next->next) { dictionary *next = dict->next, *prev = dict->prev, *afnext = next->next; if (prev) { prev->next = next; next->prev = prev; next->next = dict; dict->prev = next; dict->next = afnext; afnext->prev = dict; } else { yon_dictionary_make_first(next); next->prev = NULL; next->next = dict; dict->prev = next; dict->next = afnext; afnext->prev = dict; } return next; } else { dictionary *next = dict->next, *prev = dict->prev; if (prev) { prev->next = next; next->prev = prev; next->next = dict; dict->prev = next; dict->next = NULL; } else { next->prev = NULL; next->next = dict; dict->prev = next; dict->next = NULL; } } } } } void yon_dictionary_make_first(dictionary *dict) { for (dictionary *dct = dict->first; dct != NULL; dct = dct->next) { dct->first = dict; } } void yon_dictionary_make_nth(dictionary *dict, int nth) { dictionary *dct = dict->first; for (int i = 0; i < nth; i++) { if (dct == NULL) return; else dct = dct->next; } yon_dictionary_rip(dict); dictionary *prev = dct->prev; prev->next = dict; dict->prev = prev; dict->next = dct; dct->prev = dict; } dictionary *yon_dictionary_create_with_data(char *key, void *data) { dictionary *dct = yon_dictionary_create_empty(); dct->key = yon_char_new(key); dct->data = data; dct->data_type = DICTIONARY_OTHER_TYPE; return dct; } /** void *yon_dictionary_free_all(dictionary *dictionary,void *data_manipulation) * [EN] * Frees whole [dictionary] and activates [data_manipulation] function if not NULL with [dictionary]->data argument for each dictionary. * [RU] * Освобождает память для всех элементов словаря [dictionary] и активирует функцию [data_manipulation], если она была передана, с аргументом [dictionary]->data на каждый элемент словаря. */ void *yon_dictionary_free_all(dictionary *dictionary_to_free,void (*data_manipulation)(void*)){ dictionary *dict=NULL; for_dictionaries(dict,dictionary_to_free){ if(data_manipulation) data_manipulation(dict->data); if(dict->prev) free(dict->prev); } free(dict); return NULL; } dictionary *yon_dictionary_create_with_data_connected(dictionary *dict, char *key, void *data) { dictionary *dct = yon_dictionary_create_conneced(dict); dct->key = yon_char_new(key); dct->data = data; dct->data_type = DICTIONARY_OTHER_TYPE; return dct; } dictionary *yon_dictionary_connect(dictionary *old, dictionary *toconnect) { dictionary *dict = yon_dictionary_get_last(old); dict->next = toconnect; toconnect->prev = dict; toconnect->first = dict->first; return toconnect; } dictionary *yon_dictionary_find(dictionary **dict, char *key) { dictionary *dct = *dict; for (dictionary *pointer = dct->first; pointer != NULL; pointer = pointer->next) { if (strcmp(pointer->key, key) == 0) { *dict = pointer; return pointer; } } return NULL; } dictionary *yon_dictionary_rip(dictionary *dict) { if (!dict->next&&!dict->prev) return NULL; else if (!dict->next) { dictionary *prev = dict->prev; if (prev) { prev->next = NULL; return prev; } else return dict; } else if (!dict->prev) { dictionary *next = dict->next; if (next) { yon_dictionary_make_first(next); next->prev = NULL; return next; } else return dict; } else { dictionary *next = dict->next, *prev = dict->prev; next->prev = prev; prev->next = next; return next; } } dictionary *yon_dictionary_get_nth(dictionary *dict, int place) { if (dict){ dict = dict->first; int i = 0; for (i = 0; i < place; i++) if (dict->next) dict = dict->next; else break; if (i == place) return dict; else return NULL; } else return NULL; } // char functions /**[EN] * * creates new char string by combining two char strings. */ char *yon_char_get_augumented(char *source, char *append) { if (source && append) { int size = strlen(source) + strlen(append) + 1; char *final = malloc(size); memset(final, 0, size); if (strstr(source, "%%")) sprintf(final, source, append); else sprintf(final, "%s%s", source, append); return final; } else return NULL; } void philos_free_string_array(char ***array, int size) { if ((*array) == NULL) { return; } for (int i = 0; i < size; i++){ free((*array)[i]); } if (size!= 0) { free(*array); (*array) = NULL; } } void philos_free_string_array_n3(char ****array, int size) { if ((*array) == NULL || size == 0) { return; } int index_to_l2 = 0; for (int i = 0; i < size; i++){ index_to_l2 = 0; if ((*array)[i]!=NULL) { while ((*array)[i][index_to_l2] != NULL) { if ((*array)[i][index_to_l2] != NULL) { free((*array)[i][index_to_l2]); index_to_l2++; } } } free((*array)[i]); } if (size!= 0) { free(*array); (*array) = NULL; } } void philos_free_int_array(int **array, int size) { if (size!= 0) { free(*array); *array = NULL; } } void philos_free_int_array_n2(int ***array, int size) { if ((*array) == NULL || size == 0) { return; } for (int i = 0; i < size; i++){ free((*array)[i]); } if (size!= 0) { free(*array); (*array) = NULL; } } /**[EN] * * creates new char string by copying another char. */ char *yon_char_new(char *chr) { if (chr){ char *newchar = malloc(strlen(chr) + 1); memset(newchar, 0, strlen(chr) + 1); memcpy(newchar, chr, strlen(chr)); return newchar; } else return NULL; } // char *yon_char_unite(char *source, ...) char *yon_char_unite(char *source, ...){ va_list arglist; char *new_char=NULL; char *unite_char=NULL; new_char=yon_char_new(source); va_start(arglist,source); unite_char = va_arg(arglist,char*); while(unite_char){ new_char = yon_char_get_augumented(new_char,unite_char); unite_char = va_arg(arglist,char*); } va_end(arglist); return new_char; } /**[EN] * * cuts source string by size length from startpos position. */ char *yon_cut(char *source, int size, int startpos) { char *cut = NULL; cut = malloc(size + 1); memset(cut, 0, size + 1); memcpy(cut, source + startpos, size); return cut; } /**[EN] * * divides source string in dividepos position, * returning left part of divided string and * inserting right part to source string. */ char *yon_char_divide(char *source, int dividepos) { char *cut = malloc(dividepos + 1); memset(cut, 0, dividepos + 1); memcpy(cut, source, dividepos); char *left = malloc(strlen(source) - strlen(cut)); memset(left, 0, strlen(source) - strlen(cut)); memcpy(left, source + dividepos + 1, (strlen(source) - dividepos)); memset(source, 0, strlen(source)); memcpy(source, left, strlen(left)); return cut; } config_str philos_list_user(int* size) { char* str_uid_min = "UID_MIN"; char* str_uid_max = "UID_MAX"; unsigned short uid_min = philos_read_uid_min_max(file_source_login_min_max, str_uid_min); unsigned short uid_max = philos_read_uid_min_max(file_source_login_min_max, str_uid_max); config_str str_users = malloc(1); while (1) { errno = 0; // so we can distinguish errors from no more entries struct passwd* entry = getpwent(); if (!entry) { if (errno) { return str_users; } break; } //if ((entry->pw_uid >= uid_min && entry->pw_uid < uid_max) || entry->pw_uid == 0) { str_users = yon_char_parsed_append(str_users, size, entry->pw_name); //} } endpwent(); return str_users; } char* philos_str_size_pow_byte(GtkWidget *combo_box_text) { int menu_id = gtk_combo_box_get_active(GTK_COMBO_BOX(combo_box_text)); if (menu_id == 0) { return "K"; } else if (menu_id == 1) { return "M"; } else if (menu_id == 2) { return "G"; } else if (menu_id == 3) { return "T"; } else { return " "; } } config_str philos_list_group(int* size) { char* str_uid_min = "UID_MIN"; char* str_uid_max = "UID_MAX"; unsigned short uid_min = philos_read_uid_min_max(file_source_login_min_max, str_uid_min); unsigned short uid_max = philos_read_uid_min_max(file_source_login_min_max, str_uid_max); config_str str_users = malloc(1); while (1) { errno = 0; // so we can distinguish errors from no more entries struct passwd* entry = getpwent(); if (!entry) { if (errno) { return str_users; } break; } if ((entry->pw_uid >= uid_min && entry->pw_uid < uid_max) || entry->pw_uid == 0) { str_users = yon_char_parsed_append(str_users, size, entry->pw_name); } } endpwent(); return str_users; } unsigned short philos_read_uid_min_max(char* filename, char* search) { int uid = 0; char* remove_tab = "\t"; char* remove_space = " "; char* search_uid_min = "UID_MIN"; int buff_size = 255; char* line = g_malloc0(buff_size); char* search_true = yon_char_get_augumented("SYS_", search); FILE *fp = fopen(filename, "r"); if(fp) { while((fgets(line, buff_size, fp)) != NULL) { try{ if (yon_char_find_count(line, search) != 0 && yon_char_find_count(line, search_true) == 0) { line = philos_str_remove(line, search); line = philos_str_remove(line, remove_space); line = philos_str_remove(line, remove_tab); uid = atoi(line); } } catch (...) { if (yon_char_find_count(search, search_uid_min) != 0){ uid = 1000; } else{ uid = 65534; } } } } else{ if (yon_char_find_count(search, search_uid_min) != 0) { uid = 1000; } else{ uid = 65534; } } fclose(fp); free(line); free(search_true); return uid; } char* philos_str_remove(char *str, const char *sub) { size_t len = strlen(sub); if (len > 0) { char *p = str; size_t size = 0; while ((p = strstr(p, sub)) != NULL) { size = (size == 0) ? (p - str) + strlen(p + len) + 1 : size - len; memmove(p, p + len, size - (p - str)); } } return str; } int yon_char_find_count(char *source, char *find){ char *working_string=yon_char_new(source); int i=0; int size=0; int pos=0; config_str rtn = yon_char_parse(working_string,&size,"\n"); for (int j=0;j= 10; i++) { convert_check = convert_check / 10; } char *ch = g_malloc0(i * sizeof(char) + 1); sprintf(ch, "%d", int_to_convert); return ch; } char *yon_char_replace(char *source, char *find, char*replace){ char *final=NULL; char *temp=NULL; if(!strstr(replace,find)){ while ((final=strstr(source,find))){ temp=malloc(strlen(source)-strlen(final)); memset(temp,0,strlen(source)-strlen(final)+strlen(replace)); memcpy(temp,source,strlen(source)-strlen(final)); temp=yon_char_get_augumented(temp,replace); source=yon_char_get_augumented(temp,final+1); } return source; } return source; } void philos_split_size_memory(char* str_value, int* size, char* pow_memory) { // pow_memory = yon_char_new(str_value); (*size) = atoi(yon_char_divide_search(pow_memory, " ", -1)); } char* philos_format_cfg_str_size_memory(char* str_key, char* str_source_value, int pow_size_memory) { char* str_value = yon_char_new(str_source_value); str_value = yon_char_divide_search(str_value, " ", -1); if (pow_size_memory==0) { return yon_char_unite(str_key, str_value, "K" ,NULL); } else if (pow_size_memory==1) { return yon_char_unite(str_key, str_value, "M" ,NULL); } else if (pow_size_memory==2){ return yon_char_unite(str_key, str_value, "G" ,NULL); } else if (pow_size_memory== 3) { return yon_char_unite(str_key, str_value, "T" ,NULL); } else if (pow_size_memory== -1) { return yon_char_unite(str_key, str_value, "%" ,NULL); } else { return yon_char_unite(str_key, str_value, "-" ,NULL); } } /**[EN] * char **yon_char_parse(char *parameters, int *size, char *divider) * Parses string [parameters], divided by [divider], * then returns parsed string array and sets [size] to * size of returned array */ char **yon_char_parse(char *parameters, int *size, char *divider){ char **string=NULL; int i=1; string=malloc(sizeof(char*)); char *paramline=yon_char_new(parameters); char *param; while ((param=yon_char_divide_search(paramline,divider,1))){ string=realloc(string,sizeof(char*)*i); string[i-1]=yon_char_new(param); i++; if (strcmp(param,paramline)==0) break; } string=realloc(string,sizeof(char*)*i); string[i-1]=yon_char_new(paramline); i++; // printf("%d\n",i); *size=i-1; return string; } char** philos_str_split(char *parameters, int *size, char *divider) { char** array_split = NULL; char* ch= NULL; ch = strtok(parameters, divider); if (ch != NULL) { array_split = yon_char_parsed_append(array_split, size, ch); while (ch != NULL) { ch = strtok(NULL, divider); array_split = yon_char_parsed_append(array_split, size, ch); } } (*size) -= 1; return array_split; } char **yon_char_parsed_shrink(char **char_string, int *size, int item_to_delete){ char **new_char_parsed=NULL; new_char_parsed=malloc(sizeof(char*)*((*size)-1)); int flag = 0; for (int i=0;i < (*size);i++){ if (i==item_to_delete) { flag = 1; } if (flag == 0) { new_char_parsed[i]=yon_char_new(char_string[i]); } else if (flag == 1 && i!=item_to_delete) { new_char_parsed[i-1]=yon_char_new(char_string[i]); } } (*size)=(*size)-1; return new_char_parsed; } char*** yon_char_parsed_shrink_n3(char ***char_string, int *size, int item_to_delete){ char ***new_char_parsed=NULL; new_char_parsed=malloc(sizeof(char**)*((*size)-1)); int flag = 0; for (int i=0;i < (*size);i++){ if (i==item_to_delete) { flag = 1; } if (flag == 0) { yon_char_parsed_copy(&char_string[i], &new_char_parsed[i]); } else if (flag == 1 && i!=item_to_delete) { yon_char_parsed_copy(&char_string[i], &new_char_parsed[i-1]); } } (*size)=(*size)-1; return new_char_parsed; } /**[EN] * * Checks if [parameters] string array of length [size] * has [param] element; */ int yon_char_parsed_check_exist(char **parameters, int size, char *param){ for (int i=0;id_name); file = fopen(path, "r"); if (strlen(de->d_name) > 9) { char *extension = strstr(path, "."); if (extension != NULL) { if (strcmp(extension, ".desktop") == 0) { apps tempapp; GKeyFile *gfile = g_key_file_new(); GError *err = NULL; g_key_file_load_from_file(gfile, path, G_KEY_FILE_KEEP_TRANSLATIONS, NULL); char *Type = g_key_file_get_string(gfile, "Desktop Entry", "Type", &err); if (err) { printf("%s\n", err->message); } if (strcmp(Type, "Application") == 0) tempapp.Type = 1; else if (strcmp(Type, "pyApplication") == 0) tempapp.Type = 2; else continue; tempapp.Name = g_key_file_get_locale_string(gfile, "Desktop Entry", "Name", setlocale(LC_ALL, NULL), NULL); if (tempapp.Name == NULL) continue; tempapp.Categories = g_key_file_get_string(gfile, "Desktop Entry", "Categories", NULL); if (tempapp.Categories == NULL) continue; tempapp.Exec = g_key_file_get_string(gfile, "Desktop Entry", "Exec", NULL); if (tempapp.Exec == NULL) continue; tempapp.Icon = g_key_file_get_string(gfile, "Desktop Entry", "Icon", NULL); if (tempapp.Icon == NULL) continue; tempapp.Pluggable = g_key_file_get_boolean(gfile, "Desktop Entry", "Pluggable", NULL); if (!tempapp.Pluggable) tempapp.Pluggable = g_key_file_get_boolean(gfile, "Desktop Entry", "X-XfcePluggable", NULL); if (tempapp.Pluggable) tempapp.DualPluggable = g_key_file_get_boolean(gfile, "Desktop Entry", "X-UBLPluggable", NULL); if (g_key_file_get_boolean(gfile, "Desktop Entry", "X-UBL-SettingsManager-Hidden", NULL) == 0) if (size == 0) { applist = (apps *)malloc(size + 1 * sizeof(apps)); applist[0].Name = yon_char_new(tempapp.Name); applist[0].Categories = yon_char_new(tempapp.Categories); applist[0].Exec = yon_char_new(tempapp.Exec); applist[0].Icon = yon_char_new(tempapp.Icon); applist[0].Type = tempapp.Type; applist[0].Pluggable = tempapp.Pluggable; applist[0].DualPluggable = tempapp.DualPluggable; size++; } else { applist = (apps *)realloc(applist, (size + 1) * sizeof(apps)); applist[size].Name = yon_char_new(tempapp.Name); applist[size].Categories = yon_char_new(tempapp.Categories); applist[size].Exec = yon_char_new(tempapp.Exec); applist[size].Icon = yon_char_new(tempapp.Icon); applist[size].Pluggable = tempapp.Pluggable; applist[size].DualPluggable = tempapp.DualPluggable; applist[size].Type = tempapp.Type; size++; } } } } } } *sizef = size; return applist; }; void yon_apps_sort(apps *applist, int size) { apps tmp; if (size > 2) { for (int i = 1; i < size; i++) { for (int j = 1; j < size; j++) { if (strcmp(applist[j].Name, applist[j - 1].Name) < 0) { tmp = applist[j]; applist[j] = applist[j - 1]; applist[j - 1] = tmp; }; } }; } }; apps *yon_apps_get_by_name(apps *applist, char *name, int size) { for (int i = 0; i < size; i++) { if (strcmp(applist[i].Name, name) == 0) return &applist[i]; } return NULL; }; config_str yon_config_load(char *command, int *str_len) { FILE *output = popen(command, "r"); char **output_strings = NULL; output_strings = malloc(sizeof(char)); int i = 0; char str[1000]; memset(str, 0, 1000); while (fgets(str, 1000, output)) { if (strcmp(str, "") != 0) { output_strings = realloc(output_strings, sizeof(char *) * (i + 1)); // printf("%s\n", str); output_strings[i] = NULL; output_strings[i] = yon_char_new(str); memset(str, 0, 1000); i++; } } if (i>0){ *str_len = i; return output_strings; } else{ *str_len=-1; return NULL; } } /**[EN] * int yon_config_save(char *command) * Saves config with [command] * [RU] */ int yon_config_save(char *command) { FILE *output = popen(command, "r"); return 1; } /**[EN] * char *yon_config_get_parameter(config parameters, int size, char *param) * Gets parameter from parameter list; * * [RU] */ char *yon_config_get_parameter(config_str parameters, int size, char *param) { if (param[0]==' ') yon_char_divide_search(param," ",-1); param=yon_char_divide_search(yon_char_new(param)," ",-1); char *str = NULL; for (int j = 0; j < size; j++) { char *name = yon_char_divide_search(yon_char_new(parameters[j]), "=", 1); if (name) { if (strcmp(name, param) == 0) { str = yon_char_divide_search(yon_char_new(parameters[j]), "\n", 1); if (strcmp(str, "") != 0 && strcmp(str, "(null)") != 0) return str; else return NULL; } } } return NULL; } /**[EN] * Parses, modifies and connects string to use it as one of arguments, * sended to ubconfig. * * [RU] */ char *yon_config_make_save_parameter_with_multiple_arguments(char *parameter_string, char *config_parameter, char *divider){ char *final=""; int done=0; char *cur=yon_char_new(parameter_string); yon_char_divide_search(cur,"=",1); char *cur_param=NULL; while (cur_param=yon_char_divide_search(cur,",",1)){ if (done==0){ final=yon_char_get_augumented(final,yon_char_get_augumented(yon_char_get_augumented(config_parameter,"="), yon_char_get_augumented(cur_param,", "))); done=1; } else { final=yon_char_get_augumented(final,yon_char_get_augumented(yon_char_get_augumented(config_parameter,"+="), yon_char_get_augumented(cur_param,", "))); } } if (cur&&strcmp(cur,"")!=0) if (done==0) final=yon_char_get_augumented(final,yon_char_get_augumented(yon_char_get_augumented(config_parameter,"="), yon_char_get_augumented(cur,", "))); else final=yon_char_get_augumented(final,yon_char_get_augumented(yon_char_get_augumented(config_parameter,"+="), yon_char_get_augumented(cur,", "))); return final; } // terminal-using functions char** philos_pars_terminal_systemd_cgls(char* CMD_GET_SLICE_SERVICE, char* str_find, int* size_array_data) { int size = 0; char** terminal_print = yon_config_load(CMD_GET_SLICE_SERVICE, &size); char** array_data = NULL; for (int index = 0; index < size; index++) { if (strstr(terminal_print[index], str_find) != NULL) { yon_char_divide_search(terminal_print[index],"─",-1); terminal_print[index] = yon_char_divide_search(terminal_print[index]," ", -1); yon_char_divide(terminal_print[index],1); array_data = yon_char_parsed_append(array_data, size_array_data, terminal_print[index]); } } return array_data; } int yon_launch_app(char *name) { char *path = name; thread_output *thread = malloc(sizeof(thread_output)); thread->command = path; thread->exitcode = malloc(sizeof(int)); pthread_t thread_id; pthread_create(&thread_id, NULL, (void *)yon_launch, thread); return *thread->exitcode; }; int yon_launch_app_with_arguments(char *name, char *args) { char *path = yon_char_get_augumented("/usr/bin/", name); path = yon_char_get_augumented(path, " "); path = yon_char_get_augumented(path, args); pthread_t thread_id; thread_output *thread = malloc(sizeof(thread_output)); thread->command = path; thread->exitcode = malloc(sizeof(int)); pthread_create(&thread_id, NULL, (void *)yon_launch, thread); return *thread->exitcode; }; int yon_launch(thread_output *thread) { int a = 0; a = system(thread->command); *thread->exitcode = a; return *thread->exitcode; } int* philos_int_append(int* array, int* size, int value) { array = yon_remalloc(array, (*size+1)*sizeof(int)); array[(*size)] = value; (*size)++; return array; } int* remove_element_int_array(int* array, int* size, int item_to_delete) { int *new_int_parsed=NULL; new_int_parsed=malloc(sizeof(int)*((*size)-1)); int flag = 0; for (int i=0;i < (*size);i++){ if (i==item_to_delete) { flag = 1; } if (flag == 0) { memcpy(&(new_int_parsed[i]),&(array[i]),sizeof(int)); } else if (flag == 1 && i!=item_to_delete) { memcpy(&(new_int_parsed[i-1]),&(array[i]),sizeof(int)); } } (*size)=(*size)-1; return new_int_parsed; } int** remove_element_int_array_n3(int** array, int* size, int item_to_delete) { int **new_int_parsed=NULL; new_int_parsed=malloc(sizeof(int*)*((*size)-1)); int flag = 0; for (int i=0;i < (*size);i++){ if (i==item_to_delete) { flag = 1; } if (flag == 0) { philos_array_int_copy(&array[i],&new_int_parsed[i]); } else if (flag == 1 && i!=item_to_delete) { philos_array_int_copy(&array[i],&new_int_parsed[i-1]); } } (*size)=(*size)-1; return new_int_parsed; } // Gtk functions #ifdef __GTK_H__ static render_data render; #ifdef VTE_TERMINAL static void child_ready(VteTerminal *terminal, GPid pid, GError *error, gpointer user_data) { if (!terminal) return; if (pid == -1) printf("Error\n\n\n"); else vte_terminal_feed_child(VTE_TERMINAL(terminal),(char*)user_data,strlen((char*)user_data)); } /** * void yon_terminal_integrated_launch(GtkWidget *place_to_show, void *endwork_function, void* endwork_function_argument) * [EN] * launches terminal with specific [command], * terminal is shown in [place_to_show] container, * after terminal done its work [endwork_function] is called with [endwork_function_argument] argument. * [RU] * Запускает терминал с командой [command], * терминал добавляется в контейнер [place_to_show] виджета, * после завершения работы терминала вызывается функция [endwork_function] с аргументом [endwork_function_argument]. */ void yon_terminal_integrated_launch(GtkWidget *terminal, char* command, void *endwork_function, void* endwork_function_argument){ char **commands=new_arr(char*,2); gchar **envp = g_get_environ(); commands[0]=(gchar *)g_strdup(g_environ_getenv(envp, "SHELL")); commands[1]=NULL; char **env=new_arr(char*,2); env[0]=""; env[1]=NULL; vte_terminal_set_size(VTE_TERMINAL(terminal),10,15); VtePty *pty = vte_pty_new_sync(VTE_PTY_DEFAULT,NULL,NULL); vte_terminal_set_pty(VTE_TERMINAL(terminal),pty); char *install_command=yon_char_unite("tput cup 0 0 && tput ed; ",command, "; exit 0\n",NULL); if(endwork_function) g_signal_connect(G_OBJECT(terminal), "child-exited", G_CALLBACK(endwork_function), endwork_function_argument); vte_terminal_spawn_async(VTE_TERMINAL(terminal), VTE_PTY_DEFAULT, NULL, commands, NULL, 0, NULL, NULL, NULL, -1, NULL, child_ready, install_command); vte_pty_spawn_async(pty, NULL, commands, NULL, 0, NULL, NULL, NULL, -1, NULL, NULL, NULL); vte_terminal_set_scrollback_lines(VTE_TERMINAL(terminal), -1); vte_terminal_set_scroll_on_output(VTE_TERMINAL(terminal), TRUE); vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(terminal), TRUE); gtk_widget_show_all(terminal); } char* philos_get_size_bite(GtkWidget* chk_button, GtkWidget* spin, GtkWidget* combo_box_text) { if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(chk_button))) { char* size_prifics = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(combo_box_text)); int size_bite = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin)); char* str_size_bute = yon_char_unite(yon_char_from_int(size_bite), " ", size_prifics, NULL); return str_size_bute; } else { char* str = (char*)malloc(sizeof(char*)*2); str[0] = '-'; str[1] = '\0'; return str; } } void philos_fill_combo_box_text(GtkWidget *cbt, config_str list_data, int size) { for (int index = 0; index < size; index++) { gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(cbt), list_data[index]); } } #endif int yon_gtk_combo_box_fill(GtkWidget *combo, config_str parameters,int size){ if (combo&¶meters){ for (int i=0;ifirst; dct != NULL; dct = dct->next) { gtk_box_pack_start(GTK_BOX(destination), (GtkWidget *)dct->data, expand, fill, padding); } return 1; }else return 0; } int yon_dictionary_gtk_pack_end_multiple_widgets(dictionary *dict, GtkWidget *destination, gboolean expand, gboolean fill, int padding) { for (dictionary *dct = dict->first; dct != NULL; dct = dct->next) { gtk_box_pack_end(GTK_BOX(destination), (GtkWidget *)dct->data, expand, fill, padding); } return 1; } void _yon_ubl_header_setup(GtkWidget *Overlay, GtkWidget *Head, GtkWidget *Image, char *image_path) { gtk_overlay_add_overlay(GTK_OVERLAY(Overlay), Head); gtk_image_set_from_file(GTK_IMAGE(Image), image_path); } int yon_ubl_status_box_setup(GtkWidget *icon, GtkWidget *box, GtkWidget *label) { if(icon&&box&&label){ render.icon=icon; render.box=box; render.label=label; return 1; } else return 0; } /** * */ void _yon_ubl_status_box_render(char *text, BACKGROUND_IMAGE_TYPE type) { render_data data = render; GtkIconTheme *ictheme = gtk_icon_theme_get_default(); GError *err = NULL; if (err) { printf("%s\n", err->message); g_error_free(err); } if (type == BACKGROUND_IMAGE_SUCCESS_TYPE||! type) { gtk_style_context_remove_class(gtk_widget_get_style_context(data.box), "boxInfoMessError"); gtk_style_context_add_class(gtk_widget_get_style_context(data.box), "boxInfoMessOK"); gtk_image_set_from_pixbuf(GTK_IMAGE(data.icon), gtk_icon_theme_load_icon_for_scale(ictheme, "com.ublinux.ubl-settings-video.checked", 25, 1, GTK_ICON_LOOKUP_FORCE_SIZE, &err)); } else if (type == BACKGROUND_IMAGE_FAIL_TYPE) { gtk_style_context_remove_class(gtk_widget_get_style_context(data.box), "boxInfoMessOK"); gtk_style_context_add_class(gtk_widget_get_style_context(data.box), "boxInfoMessError"); gtk_image_set_from_pixbuf(GTK_IMAGE(data.icon), gtk_icon_theme_load_icon_for_scale(ictheme, "com.ublinux.ubl-settings-video.warning", 25, 1, GTK_ICON_LOOKUP_FORCE_SIZE, &err)); } if (text) gtk_label_set_text(GTK_LABEL(data.label), text); } void yon_ubl_status_box_render(char *text, BACKGROUND_IMAGE_TYPE type){ _yon_ubl_status_box_render(text,type); } /**yon_ubl_setup_sockets(GtkWidget *main_window, GtkWidget *left_window, GtkWidget *right_window, int socket_main_id, int socket_left_id, int socket_right_id) * [EN] * Set up plugs for using with GtkSockets insine ubl-settings-manager. * [main_window] is container widget, which holds main application functionality. * [left_window] is container widget, which holds widgets, have to be shown at left part of ubl-settings-manager header. * [right_window] is container widget, which holds widgets, have to be shown at right part of ubl-settings-manager header. * [socket_main_id] is id of socket for [main_window]. * [socket_left_id] is id of socket for [left_window]. * [socket_right_id] is id of socket for [right_window]. * [RU] * Настраивает плаги для работы с сокетами в утилите ubl-settings-manager. * [main_window] - контейнер основного интерфейса приложения. * [left_window] - контейнер для виджетов которые должны отображаться в левой части шапки ubl-settings-manager. * [right_window] - контейнер для виджетов которые должны отображаться в правой части шапки ubl-settings-manager. * [socket_main_id] - id сокета для [main_window]. * [socket_left_id] - id сокета для [left_window]. * [socket_right_id] - id сокета для [right_window]. */ void yon_ubl_setup_sockets(GtkWidget *main_window, GtkWidget *left_window, GtkWidget *right_window, int socket_main_id, int socket_left_id, int socket_right_id){ if (main_window&&socket_main_id>-1){ gtk_widget_hide(gtk_widget_get_toplevel(main_window)); GtkWidget *plug_main=gtk_plug_new(socket_main_id); GtkWidget *plug_left=NULL; GtkWidget *plug_right=NULL; GtkWidget *box=NULL; g_signal_connect(G_OBJECT(plug_main), "destroy", G_CALLBACK(gtk_main_quit),NULL); if (socket_left_id>-1&&left_window){ plug_left=gtk_plug_new(socket_left_id); g_object_ref(left_window); gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(left_window)),left_window); gtk_container_add(GTK_CONTAINER(plug_left),left_window); gtk_style_context_add_class(gtk_widget_get_style_context(plug_left),"primary-toolbar"); gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"button"); gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"opacited"); gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"color"); gtk_style_context_add_class(gtk_widget_get_style_context(plug_left),"noborder"); gtk_widget_show(plug_left); } else if (left_window){ if (box==NULL){ box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5); gtk_box_pack_start(GTK_BOX(main_window),box,0,0,5); gtk_box_reorder_child(GTK_BOX(main_window),box,0); gtk_widget_show(box); } gtk_style_context_add_class(gtk_widget_get_style_context(left_window),"inherited"); gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(left_window)),left_window); gtk_box_pack_end(GTK_BOX(box),left_window,0,0,5); } if (socket_right_id>-1&&right_window){ plug_right=gtk_plug_new(socket_right_id); g_object_ref(right_window); gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(right_window)),right_window); gtk_container_add(GTK_CONTAINER(plug_right),right_window); gtk_style_context_add_class(gtk_widget_get_style_context(plug_right),"primary-toolbar"); gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"button"); gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"opacited"); gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"color"); gtk_style_context_add_class(gtk_widget_get_style_context(plug_right),"noborder"); gtk_widget_show(plug_right); } else if (right_window){ if (box==NULL){ box=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5); gtk_box_pack_start(GTK_BOX(main_window),box,0,0,5); gtk_box_reorder_child(GTK_BOX(main_window),box,0); gtk_widget_show(box); } gtk_style_context_add_class(gtk_widget_get_style_context(right_window),"inherited"); gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(right_window)),right_window); gtk_box_pack_start(GTK_BOX(box),right_window,0,0,5); } g_object_ref(main_window); gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(main_window)),main_window); gtk_container_add(GTK_CONTAINER(plug_main),main_window); gtk_widget_show(plug_main); } } #ifdef WEBKIT_FOUND /**yon_ubl_browser_window_open(char *link, char *browser_window_name) * [EN] * Launches integrated browser window, named [browser_window_name] at header with [link]. * [RU] * Открывает встроенный браузер с именем [browser_window_name] и показываемой страницей по ссылке [link] */ void yon_ubl_browser_window_open(char *link, char *browser_window_name){ GtkWidget *browser=gtk_window_new(GTK_WINDOW_TOPLEVEL); GtkWidget *web_place=gtk_box_new(GTK_ORIENTATION_VERTICAL,0); GtkWidget *header=gtk_header_bar_new(); GtkWidget *header_label=gtk_label_new(browser_window_name); GtkWidget *WebView=webkit_web_view_new(); gtk_container_add(GTK_CONTAINER(browser),web_place); gtk_window_set_titlebar(GTK_WINDOW(browser),header); gtk_window_set_title(GTK_WINDOW(browser),browser_window_name); gtk_widget_set_size_request(browser,800,600); gtk_header_bar_set_custom_title(GTK_HEADER_BAR(header),header_label); gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(header),1); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(WebView),link); gtk_box_pack_start(GTK_BOX(web_place),WebView,1,1,0); gtk_widget_show_all(browser); } #else /**yon_ubl_browser_window_open(char *link, char *browser_window_name) * [EN] * Launches browser with [link]. * [browser_window_name] is't used. It's needed for compatibility with webkit version of that function. * [RU] * Открывает браузер со страницей по ссылке [link] * [browser_window_name] не используется. Нужна для совместимости с webkit версией этой функции. */ void yon_ubl_browser_window_open(char *link, char *browser_window_name){ char *user=getenv("SUDO_USER"); if (!user) user=getlogin(); char *command=yon_char_unite("sudo -u ",user," xdg-open ", link,NULL); yon_launch_app(command); } #endif // Mem Size pow void philos_set_spin_adjustment(GtkWidget *check, GtkWidget *spin, GtkWidget *combo, size_t value) { gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check)); guint value_spin = gtk_spin_button_get_digits(GTK_SPIN_BUTTON(spin)); if (active != -1) { GtkAdjustment* adjustment = NULL; adjustment = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(spin)); gtk_adjustment_set_lower(adjustment, 0.0); gtk_adjustment_set_page_increment(adjustment, 1.0); if (combo == NULL) { gtk_adjustment_set_upper(adjustment, (gdouble)(value*100)); if ((value*100)0) { gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),1); } else if (active == 0) { gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), 0); } if (flag_cpu == 0) { philos_set_spin_adjustment(check, spin, combo, get_resurs_total(cmd)); } else { philos_set_spin_adjustment(check, spin, NULL, get_resurs_total(cmd)); } } size_t get_resurs_total(char* cmd) { int size = 0; size_t size_memory = 0; char **responce = yon_config_load(cmd, &size); for (int index = 0; index < size; index++ ) { char* mem_size_kb = yon_char_divide_search(responce[index], "\n", -1); size_memory = atoll(mem_size_kb); free(mem_size_kb); } philos_free_string_array(&responce, size); return size_memory; } float get_size_pow_memory(size_t size_memory, int size) { float res = size_memory; for (size_t index = 0; index < size; index++) { res = res/1024; } return res; } void philos_set_active_widgets_device_io(GtkWidget* combo_to_l2,GtkWidget *check, GtkWidget *spin, GtkWidget *combo) { int menu_id = gtk_combo_box_get_active(GTK_COMBO_BOX(combo_to_l2)); if (menu_id != -1) { gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check)); gtk_widget_set_sensitive(spin, active); gtk_widget_set_sensitive(combo, active); if (gtk_spin_button_get_digits(GTK_SPIN_BUTTON(spin))>0) { gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),1); } else if (active == 0) { gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), 0); } } else { gtk_widget_set_sensitive(spin, 0); gtk_widget_set_sensitive(combo, 0); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), 0); gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), 0); } philos_set_spin_adjustment(check, spin, combo, 12582912); } #endif