user_prefs.c

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
00002 /*
00003  *  user_prefs.c
00004  *  heraia - an hexadecimal file editor and analyser based on ghex
00005  *
00006  *  (C) Copyright 2005 - 2010 Olivier Delhomme
00007  *  e-mail : heraia@delhomme.org
00008  *  URL    : http://heraia.tuxfamily.org
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2, or  (at your option)
00013  *  any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY;  without even the implied warranty of
00017  *  MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024 /**
00025  * @file user_prefs.c
00026  * Users preference may be somewhere around here
00027  */
00028 #include <libheraia.h>
00029 
00030 static void verify_preference_file_path_presence(gchar *pathname);
00031 static void verify_preference_file_name_presence(gchar *filename);
00032 
00033 static void save_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop);
00034 
00035 static void save_mp_file_preferences_options(heraia_struct_t *main_struct);
00036 static void save_mp_display_preferences_options(heraia_struct_t *main_struct);
00037 
00038 static void save_di_preferences(heraia_struct_t *main_struct);
00039 
00040 static void save_mpwp_preferences(heraia_struct_t *main_struct);
00041 
00042 
00043 static void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop);
00044 
00045 static void load_mp_file_preferences_options(heraia_struct_t *main_struct);
00046 static void load_mp_display_preferences_options(heraia_struct_t *main_struct);
00047 
00048 static void load_di_preferences(heraia_struct_t *main_struct);
00049 
00050 
00051 /**
00052  *  verify preference file path presence and creates it if it does
00053  *  not already exists
00054  *  @param pathname is a path to look presence for
00055  */
00056 static void verify_preference_file_path_presence(gchar *pathname)
00057 {
00058     struct stat *buf = NULL;
00059     gint result = 0;
00060 
00061     buf = (struct stat *) g_malloc0(sizeof(struct stat));
00062     result = g_stat(pathname, buf);
00063 
00064     if (result != 0)
00065         {
00066             g_mkdir_with_parents(pathname, 488);
00067         }
00068 }
00069 
00070 
00071 /**
00072  *  Verify preference file's presence and creates it if it does
00073  *  not exists already
00074  *  @param filename is a name of a file to look presence for
00075  */
00076 static void verify_preference_file_name_presence(gchar *filename)
00077 {
00078     FILE *fp = NULL;
00079 
00080     fp = g_fopen(filename, "r");
00081 
00082     if (fp == NULL)
00083         {
00084             fp = g_fopen(filename, "w");
00085             if (fp == NULL)
00086                 {
00087                     fprintf(stderr, "Unable to open and create the main preference file %s\n", filename);
00088                 }
00089             else
00090                 {
00091                     fprintf(stderr, "Main preference file %s created successfully\n", filename);
00092                     fclose(fp);
00093                 }
00094         }
00095     else
00096         {
00097             fclose(fp);
00098         }
00099 }
00100 
00101 
00102 /**
00103  *  Verify preference file presence and creates it if it does not
00104  *  already exists
00105  * @param pathname is the full pathname
00106  * @param filename is the filename containing the path itself
00107  */
00108 void verify_preference_file(gchar *pathname, gchar *filename)
00109 {
00110     verify_preference_file_path_presence(pathname);
00111     verify_preference_file_name_presence(filename);
00112 }
00113 
00114 
00115 /**
00116  * look out if the preference structure exists or not. If not
00117  * it creates it.
00118  * @see http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
00119  * @param main_struct the main structure
00120  */
00121 void init_preference_struct(heraia_struct_t *main_struct)
00122 {
00123     prefs_t *prefs = NULL;
00124 
00125     if (main_struct->prefs == NULL)
00126         {
00127            main_struct->prefs = (prefs_t *) g_malloc0(sizeof(prefs_t));
00128            main_struct->prefs->file = g_key_file_new();
00129            main_struct->prefs->pathname = g_build_path(G_DIR_SEPARATOR_S, g_get_user_config_dir(), "heraia", NULL);
00130            main_struct->prefs->filename = g_build_filename(main_struct->prefs->pathname, "main_preferences", NULL);
00131         }
00132     else
00133         {
00134             prefs = main_struct->prefs;
00135 
00136             if (prefs->file == NULL)
00137                 {
00138                     prefs->file = g_key_file_new();
00139                 }
00140         }
00141 }
00142 
00143 
00144 /**
00145  *  Window preferences
00146  *  @param file a GKeyFile where values are stored
00147  *  @param name a keyname (basically a window name)
00148  *  @param window_prop all window properties to save (structure window_prop_t)
00149  */
00150 static void save_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00151 {
00152     gchar *keyname = NULL;
00153 
00154     keyname = g_strconcat(name, " Displayed", NULL);
00155     g_key_file_set_boolean(file, GN_GLOBAL_PREFS, keyname, window_prop->displayed);
00156     g_free(keyname);
00157 
00158     keyname = g_strconcat(name, " X_pos", NULL);
00159     g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->x);
00160     g_free(keyname);
00161 
00162     keyname = g_strconcat(name, " Y_pos", NULL);
00163     g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->y);
00164     g_free(keyname);
00165 
00166     keyname = g_strconcat(name, " Height", NULL);
00167     g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->height);
00168     g_free(keyname);
00169 
00170     keyname = g_strconcat(name, " Width", NULL);
00171     g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->width);
00172     g_free(keyname);
00173 }
00174 
00175 
00176 /**
00177  *  Save only file preferences related options
00178  *  @param main_struct the main structure
00179  */
00180 static void save_mp_file_preferences_options(heraia_struct_t *main_struct)
00181 {
00182     prefs_t *prefs = NULL;
00183     gboolean activated = FALSE;
00184 
00185     if (main_struct != NULL)
00186         {
00187             prefs = main_struct->prefs;
00188 
00189             /* Saves the position */
00190             activated = is_toggle_button_activated(main_struct->xmls->main, "save_window_position_bt");
00191             g_key_file_set_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, activated);
00192 
00193             /* Saving all window preferences if necessary */
00194             if (activated == TRUE)
00195                 {
00196                     save_window_preferences(prefs->file, KN_ABOUT_BOX, main_struct->win_prop->about_box);
00197                     save_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_struct->win_prop->data_interpretor);
00198                     save_window_preferences(prefs->file, KN_LOG_BOX, main_struct->win_prop->log_box);
00199                     save_window_preferences(prefs->file, KN_MAIN_DIALOG, main_struct->win_prop->main_dialog);
00200                     save_window_preferences(prefs->file, KN_PLUGIN_LIST, main_struct->win_prop->plugin_list);
00201                     save_window_preferences(prefs->file, KN_LDT, main_struct->win_prop->ldt);
00202                     save_window_preferences(prefs->file, KN_MAIN_PREFS, main_struct->win_prop->main_pref_window);
00203                 }
00204         }
00205 }
00206 
00207 
00208 /**
00209  *  Save only display related preferences
00210  *  @param main_struct : main structure
00211  */
00212 static void save_mp_display_preferences_options(heraia_struct_t *main_struct)
00213 {
00214     prefs_t *prefs = NULL;
00215     gboolean activated = FALSE;
00216 
00217     if (main_struct != NULL)
00218         {
00219             prefs = main_struct->prefs;
00220 
00221             /* Display Thousand (or not) */
00222             activated = is_toggle_button_activated(main_struct->xmls->main, "mp_thousand_bt");
00223             g_key_file_set_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, activated);
00224         }
00225 }
00226 
00227 
00228 /**
00229  * Saves data interpretor state and preferences
00230  * @param main_struct : main structure
00231  */
00232 static void save_di_preferences(heraia_struct_t *main_struct)
00233 {
00234     GtkNotebook *notebook = NULL;  /**< data interpretor's notebook               */
00235     gint selected_tab;             /**< Selected tab in data interpretor's window */
00236     prefs_t *prefs = NULL;         /**< structure for preferences                 */
00237 
00238     if (main_struct != NULL && main_struct->current_DW != NULL)
00239         {
00240             prefs = main_struct->prefs;
00241 
00242             notebook = GTK_NOTEBOOK(heraia_get_widget(main_struct->xmls->main, "diw_notebook"));
00243 
00244             if (notebook != NULL)
00245                 {
00246                     selected_tab = gtk_notebook_get_current_page(notebook);
00247 
00248                     if (selected_tab >= 0)
00249                         {
00250                             g_key_file_set_integer(prefs->file, GN_DI_PREFS, KN_DI_SELECTED_TAB, selected_tab);
00251                         }
00252                 }
00253         }
00254 }
00255 
00256 
00257 /**
00258  * Saves main preferences window state and preferences
00259  * @param main_struct : main structure
00260  */
00261 static void save_mpwp_preferences(heraia_struct_t *main_struct)
00262 {
00263     GtkNotebook *notebook = NULL;  /**< main preferences's notebook               */
00264     gint selected_tab;             /**< Selected tab in data interpretor's window */
00265     prefs_t *prefs = NULL;         /**< structure for preferences                 */
00266 
00267     if (main_struct != NULL && main_struct->current_DW != NULL)
00268         {
00269             prefs = main_struct->prefs;
00270 
00271             notebook = GTK_NOTEBOOK(heraia_get_widget(main_struct->xmls->main, "mp_first_notebook"));
00272 
00273             if (notebook != NULL)
00274                 {
00275                     selected_tab = gtk_notebook_get_current_page(notebook);
00276 
00277                     if (selected_tab >= 0)
00278                         {
00279                             g_key_file_set_integer(prefs->file, GN_MPWP_PREFS, KN_MPWP_SELECTED_TAB, selected_tab);
00280                         }
00281                 }
00282         }
00283 }
00284 
00285 
00286 /**
00287  * Save all preferences to the user preference file
00288  * @param main_struct the main structure
00289  */
00290 void save_preferences(heraia_struct_t *main_struct)
00291 {
00292     if (main_struct != NULL)
00293         {
00294             /* 1. Saving main Preferences */
00295             save_mp_file_preferences_options(main_struct);
00296 
00297             /* 2. Saving Display Preferences */
00298             save_mp_display_preferences_options(main_struct);
00299 
00300             /* 3. Saving Data Interpretor Preferences */
00301             save_di_preferences(main_struct);
00302 
00303             /* 4. Saving Main Preferences Window Preferences */
00304             save_mpwp_preferences(main_struct);
00305 
00306             if (main_struct->prefs != NULL)
00307                 {
00308                     /* Saving to file */
00309                     save_preferences_to_file(main_struct->prefs);
00310                 }
00311         }
00312 }
00313 
00314 
00315 /**
00316  *  window preferences
00317  *  @param file a GKeyFile where values are stored
00318  *  @param name a keyname (basically a window name)
00319  *  @param window_prop all window properties to save (structure window_prop_t)
00320  */
00321 static void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00322 {
00323     gchar *keyname = NULL;
00324 
00325     keyname = g_strconcat(name, " Displayed", NULL);
00326     window_prop->displayed = g_key_file_get_boolean(file, GN_GLOBAL_PREFS, keyname, NULL);
00327     g_free(keyname);
00328 
00329     keyname = g_strconcat(name, " X_pos", NULL);
00330     window_prop->x = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00331     g_free(keyname);
00332 
00333     keyname = g_strconcat(name, " Y_pos", NULL);
00334     window_prop->y = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00335     g_free(keyname);
00336 
00337     keyname = g_strconcat(name, " Height", NULL);
00338     window_prop->height = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00339     g_free(keyname);
00340 
00341     keyname = g_strconcat(name, " Width", NULL);
00342     window_prop->width = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00343     g_free(keyname);
00344 
00345 }
00346 
00347 
00348 /**
00349  *  Load only main preferences related options
00350  * @param main_struct the main structure
00351  */
00352 static void load_mp_file_preferences_options(heraia_struct_t *main_struct)
00353 {
00354     prefs_t *prefs = NULL;
00355     GtkWidget *save_window_position_bt = NULL;
00356     gboolean activated = FALSE;
00357 
00358     if (main_struct != NULL)
00359         {
00360             prefs = main_struct->prefs;
00361 
00362             /* Saving window's positions ? */
00363             activated = g_key_file_get_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, NULL);
00364             save_window_position_bt = heraia_get_widget(main_struct->xmls->main, "save_window_position_bt");
00365             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_window_position_bt), activated);
00366 
00367             if (activated == TRUE)
00368                 {
00369                     /* window's positions */
00370                     load_window_preferences(prefs->file, KN_ABOUT_BOX, main_struct->win_prop->about_box);
00371                     load_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_struct->win_prop->data_interpretor);
00372                     load_window_preferences(prefs->file, KN_LOG_BOX, main_struct->win_prop->log_box);
00373                     load_window_preferences(prefs->file, KN_MAIN_DIALOG, main_struct->win_prop->main_dialog);
00374                     load_window_preferences(prefs->file, KN_PLUGIN_LIST, main_struct->win_prop->plugin_list);
00375                     load_window_preferences(prefs->file, KN_LDT, main_struct->win_prop->ldt);
00376                     load_window_preferences(prefs->file, KN_MAIN_PREFS, main_struct->win_prop->main_pref_window);
00377                 }
00378         }
00379 }
00380 
00381 
00382 /**
00383  *  Load display related preferences
00384  * @param main_struct the main structure
00385  */
00386 static void load_mp_display_preferences_options(heraia_struct_t *main_struct)
00387 {
00388     prefs_t *prefs = NULL;
00389     GtkWidget *display_thousand_bt = NULL;
00390     gboolean activated = FALSE;
00391 
00392     if (main_struct != NULL)
00393         {
00394             prefs = main_struct->prefs;
00395 
00396             /* Display thousands (or not) */
00397             activated = g_key_file_get_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, NULL);
00398             display_thousand_bt = heraia_get_widget(main_struct->xmls->main, "mp_thousand_bt");
00399             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(display_thousand_bt), activated);
00400         }
00401 }
00402 
00403 
00404 /**
00405  * Load data interpretor state and preferences
00406  * @param main_struct : main structure
00407  */
00408 static void load_di_preferences(heraia_struct_t *main_struct)
00409 {
00410     GtkNotebook *notebook = NULL;  /**< data interpretor's notebook               */
00411     gint selected_tab;             /**< Selected tab in data interpretor's window */
00412     prefs_t *prefs = NULL;         /**< structure for preferences                 */
00413 
00414     if (main_struct != NULL && main_struct->current_DW != NULL && main_struct->xmls != NULL && main_struct->xmls->main != NULL)
00415         {
00416             notebook = GTK_NOTEBOOK(heraia_get_widget(main_struct->xmls->main, "diw_notebook"));
00417             prefs = main_struct->prefs;
00418 
00419             if (notebook != NULL)
00420                 {
00421                     selected_tab = g_key_file_get_integer(prefs->file, GN_DI_PREFS, KN_DI_SELECTED_TAB, NULL);
00422 
00423                     if (selected_tab >= 0)
00424                         {
00425                             gtk_notebook_set_current_page(notebook, selected_tab);
00426                             main_struct->current_DW->tab_displayed = selected_tab;
00427                         }
00428                 }
00429         }
00430 }
00431 
00432 
00433 /**
00434  * Load main preferences window state and preferences
00435  * @param main_struct : main structure
00436  */
00437 static void load_mpwp_preferences(heraia_struct_t *main_struct)
00438 {
00439     GtkNotebook *notebook = NULL;  /**< main preferences's notebook               */
00440     GtkWidget *button = NULL;      /**< tool button from the toolbar              */
00441     gint selected_tab;             /**< Selected tab in data interpretor's window */
00442     prefs_t *prefs = NULL;         /**< structure for preferences                 */
00443 
00444     if (main_struct != NULL && main_struct->current_DW != NULL && main_struct->xmls != NULL && main_struct->xmls->main != NULL)
00445         {
00446             notebook = GTK_NOTEBOOK(heraia_get_widget(main_struct->xmls->main, "mp_first_notebook"));
00447             prefs = main_struct->prefs;
00448 
00449             if (notebook != NULL)
00450                 {
00451                     selected_tab = g_key_file_get_integer(prefs->file, GN_MPWP_PREFS, KN_MPWP_SELECTED_TAB, NULL);
00452 
00453                     switch (selected_tab)
00454                         {
00455                             case 0:
00456                                 gtk_notebook_set_current_page(notebook, selected_tab);
00457                                 button = heraia_get_widget(main_struct->xmls->main, "mp_tb_fp_bt");
00458                                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE);
00459                                 break;
00460 
00461                             case 1:
00462                                 gtk_notebook_set_current_page(notebook, selected_tab);
00463                                 button = heraia_get_widget(main_struct->xmls->main, "mp_tb_display_bt");
00464                                 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(button), TRUE);
00465                                 break;
00466 
00467                             default:
00468                             break;
00469                         }
00470                 }
00471         }
00472 }
00473 
00474 
00475 /**
00476  * Sets up the preferences as loaded in the preference file
00477  * @param main_struct the main structure
00478  */
00479 void load_preferences(heraia_struct_t *main_struct)
00480 {
00481     if (main_struct != NULL)
00482         {
00483             /* 1. Loading Main Preferences */
00484             load_mp_file_preferences_options(main_struct);
00485 
00486             /* 2. Loading Display preferences */
00487             load_mp_display_preferences_options(main_struct);
00488 
00489             /* 3. Loading Data Interpretor Preferences */
00490             load_di_preferences(main_struct);
00491 
00492             /* 4. Loading Main Preferences Window Preferences */
00493             load_mpwp_preferences(main_struct);
00494         }
00495 }
Generated on Tue May 11 18:46:08 2010 for Heraia by  doxygen 1.6.3