00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <libheraia.h>
00029
00030
00031 static void verify_preference_file_path_presence(gchar *pathname);
00032 static void verify_preference_file_name_presence(gchar *filename);
00033
00034
00035 static void save_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop);
00036
00037 static void save_mp_file_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs);
00038 static void save_mp_display_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs);
00039 static void save_mp_files_filenames(heraia_struct_t *main_struct, prefs_t *prefs);
00040
00041 static void save_di_preferences(heraia_struct_t *main_struct, prefs_t *prefs);
00042
00043 static void save_mpwp_preferences(heraia_struct_t *main_struct, prefs_t *prefs);
00044
00045
00046 static void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop);
00047
00048 static void load_mp_file_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs);
00049 static void load_mp_display_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs);
00050 static void load_mp_files_filenames(heraia_struct_t *main_struct, prefs_t *prefs);
00051
00052 static void load_di_preferences(heraia_struct_t *main_struct, prefs_t *prefs);
00053
00054 static void load_mpwp_preferences(heraia_struct_t *main_struct, prefs_t *prefs);
00055
00056
00057
00058
00059
00060
00061
00062 static void verify_preference_file_path_presence(gchar *pathname)
00063 {
00064 struct stat *buf = NULL;
00065 gint result = 0;
00066
00067 buf = (struct stat *) g_malloc0(sizeof(struct stat));
00068 result = g_stat(pathname, buf);
00069
00070 if (result != 0)
00071 {
00072 g_mkdir_with_parents(pathname, 488);
00073 }
00074 }
00075
00076
00077
00078
00079
00080
00081
00082 static void verify_preference_file_name_presence(gchar *filename)
00083 {
00084 FILE *fp = NULL;
00085
00086 fp = g_fopen(filename, "r");
00087
00088 if (fp == NULL)
00089 {
00090 fp = g_fopen(filename, "w");
00091 if (fp == NULL)
00092 {
00093 fprintf(stderr, Q_("Unable to open and create the main preference file %s\n"), filename);
00094 }
00095 else
00096 {
00097 fprintf(stderr, Q_("Main preference file %s created successfully\n"), filename);
00098 fclose(fp);
00099 }
00100 }
00101 else
00102 {
00103 fclose(fp);
00104 }
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 void verify_preference_file(prefs_t *prefs)
00114 {
00115 if (prefs != NULL)
00116 {
00117 verify_preference_file_path_presence(prefs->pathname);
00118 verify_preference_file_name_presence(prefs->filename);
00119 }
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 prefs_t *init_preference_struct(gchar *pathname, gchar *filename)
00131 {
00132 prefs_t *prefs = NULL;
00133
00134 prefs = (prefs_t *) g_malloc0(sizeof(prefs_t));
00135
00136 prefs->file = g_key_file_new();
00137 prefs->pathname = pathname;
00138 prefs->filename = g_build_filename(prefs->pathname, filename, NULL);
00139
00140 return prefs;
00141 }
00142
00143
00144
00145
00146
00147
00148 void free_preference_struct(prefs_t *prefs)
00149 {
00150 if (prefs != NULL)
00151 {
00152 g_free(prefs->filename);
00153 g_free(prefs->pathname);
00154 g_key_file_free(prefs->file);
00155 }
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165 static void save_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00166 {
00167 gchar *keyname = NULL;
00168
00169 keyname = g_strconcat(name, " Displayed", NULL);
00170 g_key_file_set_boolean(file, GN_GLOBAL_PREFS, keyname, window_prop->displayed);
00171 g_free(keyname);
00172
00173 keyname = g_strconcat(name, " X_pos", NULL);
00174 g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->x);
00175 g_free(keyname);
00176
00177 keyname = g_strconcat(name, " Y_pos", NULL);
00178 g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->y);
00179 g_free(keyname);
00180
00181 keyname = g_strconcat(name, " Height", NULL);
00182 g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->height);
00183 g_free(keyname);
00184
00185 keyname = g_strconcat(name, " Width", NULL);
00186 g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->width);
00187 g_free(keyname);
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 static void save_mp_file_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs)
00197 {
00198 gboolean activated = FALSE;
00199
00200
00201 if (main_struct != NULL && prefs != NULL)
00202 {
00203
00204 activated = is_toggle_button_activated(main_struct->xmls->main, "save_window_position_bt");
00205 g_key_file_set_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, activated);
00206
00207
00208 if (activated == TRUE)
00209 {
00210 save_window_preferences(prefs->file, KN_ABOUT_BOX, main_struct->win_prop->about_box);
00211 save_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_struct->win_prop->data_interpretor);
00212 save_window_preferences(prefs->file, KN_LOG_BOX, main_struct->win_prop->log_box);
00213 save_window_preferences(prefs->file, KN_MAIN_DIALOG, main_struct->win_prop->main_dialog);
00214 save_window_preferences(prefs->file, KN_PLUGIN_LIST, main_struct->win_prop->plugin_list);
00215 save_window_preferences(prefs->file, KN_LDT, main_struct->win_prop->ldt);
00216 save_window_preferences(prefs->file, KN_MAIN_PREFS, main_struct->win_prop->main_pref_window);
00217 save_window_preferences(prefs->file, KN_GOTO_DIALOG, main_struct->win_prop->goto_window);
00218 save_window_preferences(prefs->file, KN_RESULT_WINDOW, main_struct->win_prop->result_window);
00219 save_window_preferences(prefs->file, KN_FIND_WINDOW, main_struct->win_prop->find_window);
00220 save_window_preferences(prefs->file, KN_FR_WINDOW, main_struct->win_prop->fr_window);
00221 save_window_preferences(prefs->file, KN_FDFT_WINDOW, main_struct->win_prop->fdft_window);
00222 }
00223
00224
00225 activated = is_toggle_button_activated(main_struct->xmls->main, "save_filenames_bt");
00226 g_key_file_set_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_OPENED_FILES_FILENAMES, activated);
00227
00228 if (activated == TRUE)
00229 {
00230 save_mp_files_filenames(main_struct, prefs);
00231 }
00232 }
00233 }
00234
00235
00236
00237
00238
00239
00240 static void save_mp_files_filenames(heraia_struct_t *main_struct, prefs_t *prefs)
00241 {
00242 const gchar **list = NULL;
00243 gchar *filename = NULL;
00244 gint *pos_list = NULL;
00245 gsize i = 0;
00246 gsize len = 0;
00247 gsize j = 0;
00248 gint current_tab = 0;
00249 doc_t *document = NULL;
00250 GtkWidget *notebook = NULL;
00251
00252 if (main_struct != NULL && prefs != NULL)
00253 {
00254
00255 i = 0;
00256 j = 0;
00257 len = main_struct->documents->len;
00258 list = (const gchar **) g_malloc0 (sizeof(gchar *)*len);
00259 pos_list = (gint *) g_malloc0 (sizeof(gint)*len);
00260
00261 while (i < len)
00262 {
00263 document = g_ptr_array_index(main_struct->documents, i);
00264
00265 if (document != NULL)
00266 {
00267 filename = g_strdup(doc_t_document_get_filename(document));
00268
00269 pos_list[j] = (gint) ghex_get_cursor_position(document->hex_widget);
00270 list[j] = filename;
00271 j++;
00272 }
00273 i++;
00274 }
00275
00276
00277 g_key_file_set_string_list(prefs->file, GN_GLOBAL_PREFS, KN_FILES_FILENAMES, list, j);
00278 g_key_file_set_integer_list(prefs->file, GN_GLOBAL_PREFS, KN_FILES_CURSOR_POSITIONS, pos_list, j);
00279
00280
00281 notebook = heraia_get_widget(main_struct->xmls->main, "file_notebook");
00282 current_tab = gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook));
00283 g_key_file_set_integer(prefs->file, GN_GLOBAL_PREFS, KN_CURRENT_TAB, current_tab);
00284 }
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 static void save_mp_display_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs)
00294 {
00295 gboolean activated = FALSE;
00296
00297 if (main_struct != NULL && prefs != NULL)
00298 {
00299
00300 activated = is_toggle_button_activated(main_struct->xmls->main, "mp_thousand_bt");
00301 g_key_file_set_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, activated);
00302
00303
00304 activated = is_toggle_button_activated(main_struct->xmls->main, "mp_display_offset_bt");
00305 g_key_file_set_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_OFFSETS, activated);
00306 }
00307 }
00308
00309
00310
00311
00312
00313
00314
00315 static void save_di_preferences(heraia_struct_t *main_struct, prefs_t *prefs)
00316 {
00317 gint selected_tab = -1;
00318 gint stream_size = -1;
00319 gint endianness = -1;
00320
00321 if (main_struct != NULL && main_struct->current_DW != NULL && prefs != NULL)
00322 {
00323 selected_tab = di_get_selected_tab(main_struct);
00324 if (selected_tab >= 0)
00325 {
00326 g_key_file_set_integer(prefs->file, GN_DI_PREFS, KN_DI_SELECTED_TAB, selected_tab);
00327 }
00328
00329 stream_size = di_get_stream_size(main_struct);
00330 if (stream_size >= 0)
00331 {
00332 g_key_file_set_integer(prefs->file, GN_DI_PREFS, KN_DI_STREAM_SIZE, stream_size);
00333 }
00334
00335 endianness = di_get_endianness(main_struct);
00336 if (endianness >= 0)
00337 {
00338 g_key_file_set_integer(prefs->file, GN_DI_PREFS, KN_DI_ENDIANNESS, endianness);
00339 }
00340 }
00341 }
00342
00343
00344
00345
00346
00347
00348
00349 static void save_mpwp_preferences(heraia_struct_t *main_struct, prefs_t *prefs)
00350 {
00351 GtkNotebook *notebook = NULL;
00352 gint selected_tab = -1;
00353
00354 if (main_struct != NULL && main_struct->current_DW != NULL && prefs != NULL)
00355 {
00356 notebook = GTK_NOTEBOOK(heraia_get_widget(main_struct->xmls->main, "mp_first_notebook"));
00357
00358 if (notebook != NULL)
00359 {
00360 selected_tab = gtk_notebook_get_current_page(notebook);
00361
00362 if (selected_tab >= 0)
00363 {
00364 g_key_file_set_integer(prefs->file, GN_MPWP_PREFS, KN_MPWP_SELECTED_TAB, selected_tab);
00365 }
00366 }
00367 }
00368 }
00369
00370
00371
00372
00373
00374
00375
00376 void save_preferences(heraia_struct_t *main_struct, prefs_t *prefs)
00377 {
00378 if (main_struct != NULL && prefs != NULL)
00379 {
00380
00381 save_mp_file_preferences_options(main_struct, prefs);
00382
00383
00384 save_mp_display_preferences_options(main_struct, prefs);
00385
00386
00387 save_di_preferences(main_struct, prefs);
00388
00389
00390 save_mpwp_preferences(main_struct, prefs);
00391
00392
00393 save_preferences_to_file(prefs);
00394 }
00395 }
00396
00397
00398
00399
00400
00401
00402
00403
00404 static void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00405 {
00406 gchar *keyname = NULL;
00407
00408 keyname = g_strconcat(name, " Displayed", NULL);
00409 window_prop->displayed = g_key_file_get_boolean(file, GN_GLOBAL_PREFS, keyname, NULL);
00410 g_free(keyname);
00411
00412 keyname = g_strconcat(name, " X_pos", NULL);
00413 window_prop->x = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00414 g_free(keyname);
00415
00416 keyname = g_strconcat(name, " Y_pos", NULL);
00417 window_prop->y = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00418 g_free(keyname);
00419
00420 keyname = g_strconcat(name, " Height", NULL);
00421 window_prop->height = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00422 g_free(keyname);
00423
00424 keyname = g_strconcat(name, " Width", NULL);
00425 window_prop->width = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00426 g_free(keyname);
00427
00428 }
00429
00430
00431
00432
00433
00434
00435
00436 static void load_mp_file_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs)
00437 {
00438 GtkWidget *save_window_position_bt = NULL;
00439 GtkWidget *save_filenames_bt = NULL;
00440 gboolean activated = FALSE;
00441
00442 if (main_struct != NULL && prefs != NULL)
00443 {
00444 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Loading window's positions"));
00445
00446 activated = g_key_file_get_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, NULL);
00447 save_window_position_bt = heraia_get_widget(main_struct->xmls->main, "save_window_position_bt");
00448 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_window_position_bt), activated);
00449
00450 if (activated == TRUE)
00451 {
00452
00453 load_window_preferences(prefs->file, KN_ABOUT_BOX, main_struct->win_prop->about_box);
00454 load_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_struct->win_prop->data_interpretor);
00455 load_window_preferences(prefs->file, KN_LOG_BOX, main_struct->win_prop->log_box);
00456 load_window_preferences(prefs->file, KN_MAIN_DIALOG, main_struct->win_prop->main_dialog);
00457 load_window_preferences(prefs->file, KN_PLUGIN_LIST, main_struct->win_prop->plugin_list);
00458 load_window_preferences(prefs->file, KN_LDT, main_struct->win_prop->ldt);
00459 load_window_preferences(prefs->file, KN_MAIN_PREFS, main_struct->win_prop->main_pref_window);
00460 load_window_preferences(prefs->file, KN_GOTO_DIALOG, main_struct->win_prop->goto_window);
00461 load_window_preferences(prefs->file, KN_RESULT_WINDOW, main_struct->win_prop->result_window);
00462 load_window_preferences(prefs->file, KN_FIND_WINDOW, main_struct->win_prop->find_window);
00463 load_window_preferences(prefs->file, KN_FR_WINDOW, main_struct->win_prop->fr_window);
00464 load_window_preferences(prefs->file, KN_FDFT_WINDOW, main_struct->win_prop->fdft_window);
00465 }
00466
00467
00468 activated = g_key_file_get_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_OPENED_FILES_FILENAMES, NULL);
00469 save_filenames_bt = heraia_get_widget(main_struct->xmls->main, "save_filenames_bt");
00470 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_filenames_bt), activated);
00471
00472 if (activated == TRUE)
00473 {
00474 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Loading files..."));
00475 load_mp_files_filenames(main_struct, prefs);
00476 }
00477 }
00478 }
00479
00480
00481
00482
00483
00484
00485
00486 static void load_mp_files_filenames(heraia_struct_t *main_struct, prefs_t *prefs)
00487 {
00488 gchar **list = NULL;
00489 gint *pos_list = NULL;
00490 gsize i = 0;
00491 gsize len = 0;
00492 gsize pos_len = 0;
00493 gint current_tab = 0;
00494 GtkWidget *notebook = NULL;
00495
00496 if (main_struct != NULL && prefs != NULL)
00497 {
00498
00499 list = g_key_file_get_string_list(prefs->file, GN_GLOBAL_PREFS, KN_FILES_FILENAMES, &len, NULL);
00500 pos_list = (gint *) g_key_file_get_integer_list(prefs->file, GN_GLOBAL_PREFS, KN_FILES_CURSOR_POSITIONS, &pos_len, NULL);
00501
00502 if (len == pos_len)
00503 {
00504 for (i = 0; i < len; i++)
00505 {
00506 if (load_file_to_analyse(main_struct, list[i]))
00507 {
00508
00509
00510
00511
00512 ghex_set_cursor_position(main_struct->current_doc->hex_widget, (guint64) pos_list[i]);
00513 }
00514 }
00515 }
00516
00517
00518 current_tab = g_key_file_get_integer(prefs->file, GN_GLOBAL_PREFS, KN_CURRENT_TAB, NULL);
00519 notebook = heraia_get_widget(main_struct->xmls->main, "file_notebook");
00520 gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), current_tab);
00521 }
00522 }
00523
00524
00525
00526
00527
00528
00529
00530 static void load_mp_display_preferences_options(heraia_struct_t *main_struct, prefs_t *prefs)
00531 {
00532 GtkWidget *toggle_button = NULL;
00533 gboolean activated = FALSE;
00534
00535 if (main_struct != NULL && prefs != NULL)
00536 {
00537
00538 activated = g_key_file_get_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, NULL);
00539 toggle_button = heraia_get_widget(main_struct->xmls->main, "mp_thousand_bt");
00540 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_button), activated);
00541
00542
00543 activated = g_key_file_get_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_OFFSETS, NULL);
00544 toggle_button = heraia_get_widget(main_struct->xmls->main, "mp_display_offset_bt");
00545 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle_button), activated);
00546 }
00547 }
00548
00549
00550
00551
00552
00553
00554
00555 static void load_di_preferences(heraia_struct_t *main_struct, prefs_t *prefs)
00556 {
00557 gint selected_tab = -1;
00558 gint stream_size = -1;
00559 gint endianness = -1;
00560
00561 if (main_struct != NULL && main_struct->current_DW != NULL && main_struct->xmls != NULL && main_struct->xmls->main != NULL && prefs != NULL)
00562 {
00563 selected_tab = g_key_file_get_integer(prefs->file, GN_DI_PREFS, KN_DI_SELECTED_TAB, NULL);
00564 stream_size = g_key_file_get_integer(prefs->file, GN_DI_PREFS, KN_DI_STREAM_SIZE, NULL);
00565 endianness = g_key_file_get_integer(prefs->file, GN_DI_PREFS, KN_DI_ENDIANNESS, NULL);
00566
00567 di_set_selected_tab(main_struct, selected_tab);
00568 di_set_stream_size(main_struct, stream_size);
00569 di_set_endianness(main_struct, endianness);
00570 }
00571 }
00572
00573
00574
00575
00576
00577
00578
00579 static void load_mpwp_preferences(heraia_struct_t *main_struct, prefs_t *prefs)
00580 {
00581 GtkNotebook *notebook = NULL;
00582 GtkWidget *button = NULL;
00583 gint selected_tab;
00584
00585 if (main_struct != NULL && main_struct->current_DW != NULL && main_struct->xmls != NULL && main_struct->xmls->main != NULL && prefs != NULL)
00586 {
00587 notebook = GTK_NOTEBOOK(heraia_get_widget(main_struct->xmls->main, "mp_first_notebook"));
00588
00589 if (notebook != NULL)
00590 {
00591 selected_tab = g_key_file_get_integer(prefs->file, GN_MPWP_PREFS, KN_MPWP_SELECTED_TAB, NULL);
00592
00593 switch (selected_tab)
00594 {
00595 case 0:
00596 gtk_notebook_set_current_page(notebook, selected_tab);
00597 button = heraia_get_widget(main_struct->xmls->main, "mp_tb_fp_bt");
00598 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE);
00599 break;
00600
00601 case 1:
00602 gtk_notebook_set_current_page(notebook, selected_tab);
00603 button = heraia_get_widget(main_struct->xmls->main, "mp_tb_display_bt");
00604 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE);
00605 break;
00606
00607 default:
00608 break;
00609 }
00610 }
00611 }
00612 }
00613
00614
00615
00616
00617
00618
00619
00620 void load_preferences(heraia_struct_t *main_struct, prefs_t *prefs)
00621 {
00622 if (main_struct != NULL && prefs != NULL)
00623 {
00624
00625 if (load_preference_file(prefs) == TRUE)
00626 {
00627
00628 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Loading main preferences"));
00629 load_mp_file_preferences_options(main_struct, prefs);
00630
00631
00632 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Loading main display preferences"));
00633 load_mp_display_preferences_options(main_struct, prefs);
00634
00635
00636 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Loading data interpretor preferences"));
00637 load_di_preferences(main_struct, prefs);
00638
00639
00640 log_message(main_struct, G_LOG_LEVEL_DEBUG, Q_("Loading window preference's main preferences"));
00641 load_mpwp_preferences(main_struct, prefs);
00642 }
00643 }
00644 }