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 - 2009 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_window_t *main_window);
00036 static void save_mp_display_preferences_options(heraia_window_t *main_window);
00037 
00038 static void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop);
00039 
00040 static void load_mp_file_preferences_options(heraia_window_t *main_window);
00041 static void load_mp_display_preferences_options(heraia_window_t *main_window);
00042 
00043 
00044 /**
00045  *  verify preference file path presence and creates it if it does
00046  *  not already exists
00047  *  @param pathname is a path to look presence for
00048  */
00049 static void verify_preference_file_path_presence(gchar *pathname)
00050 {
00051         struct stat *buf = NULL;
00052         gint result = 0;
00053         
00054         buf = (struct stat *) g_malloc0(sizeof(struct stat));
00055         result = g_stat(pathname, buf);
00056 
00057         if (result != 0)
00058         {
00059                 g_mkdir_with_parents(pathname, 488);
00060         }
00061 }
00062 
00063 
00064 /**
00065  *  Verify preference file's presence and creates it if it does 
00066  *  not exists already
00067  *  @param filename is a name of a file to look presence for
00068  */
00069 static void verify_preference_file_name_presence(gchar *filename)
00070 {
00071         FILE *fp = NULL;
00072         
00073         fp = g_fopen(filename, "r");
00074         
00075         if (fp == NULL)
00076         {
00077                 fp = g_fopen(filename, "w");    
00078                 if (fp == NULL)
00079                 {
00080                         fprintf(stderr, "Unable to open and create the main preference file %s\n", filename);
00081                 }
00082                 else
00083                 {
00084                         fprintf(stderr, "Main preference file %s created successfully\n", filename);
00085                         fclose(fp);
00086                 }
00087         }
00088         else
00089         {
00090                 fclose(fp);
00091         }
00092 }
00093 
00094 
00095 /**
00096  *  Verify preference file presence and creates it if it does not
00097  *  already exists
00098  * @param pathname is the full pathname
00099  * @param filename is the filename containing the path itself
00100  */
00101 void verify_preference_file(gchar *pathname, gchar *filename)
00102 {
00103         
00104         verify_preference_file_path_presence(pathname);
00105         verify_preference_file_name_presence(filename);
00106         
00107 }
00108 
00109 /**
00110  * look out if the preference structure exists or not. If not
00111  * it creates it.
00112  * @param main_window the main structure
00113  */
00114 void init_preference_struct(heraia_window_t *main_window)
00115 {
00116         prefs_t *prefs = NULL;
00117         
00118         if (main_window->prefs == NULL)
00119         {
00120            main_window->prefs = (prefs_t *) g_malloc0(sizeof(prefs_t));
00121            main_window->prefs->file = g_key_file_new();
00122            main_window->prefs->pathname = g_strdup_printf("%s%c.%s", g_get_home_dir(), G_DIR_SEPARATOR, "heraia");
00123            main_window->prefs->filename = g_strdup_printf("%s%c%s", main_window->prefs->pathname, G_DIR_SEPARATOR, "main_preferences");
00124         }
00125         else
00126         {
00127                 prefs = main_window->prefs;
00128                 
00129                 if (prefs->file == NULL)
00130                 {
00131                         prefs->file = g_key_file_new();
00132                 }
00133         }
00134 }
00135 
00136 /**
00137  *  Window preferences
00138  *  @param file a GKeyFile where values are stored
00139  *  @param name a keyname (basically a window name)
00140  *  @param window_prop all window properties to save (structure window_prop_t)
00141  */
00142 static void save_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00143 {
00144         gchar *keyname = NULL;
00145         
00146         keyname = g_strconcat(name, " Displayed", NULL);
00147         g_key_file_set_boolean(file, GN_GLOBAL_PREFS, keyname, window_prop->displayed);
00148         g_free(keyname);
00149         
00150         keyname = g_strconcat(name, " X_pos", NULL);
00151         g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->x);
00152         g_free(keyname);
00153         
00154         keyname = g_strconcat(name, " Y_pos", NULL);
00155         g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->y);
00156         g_free(keyname);
00157         
00158         keyname = g_strconcat(name, " Height", NULL);
00159         g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->height);
00160         g_free(keyname);
00161 
00162         keyname = g_strconcat(name, " Width", NULL);
00163         g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->width);
00164         g_free(keyname);
00165 }
00166 
00167 /**
00168  *  Save only file preferences related options
00169  *  @param main_window the main structure
00170  */
00171 static void save_mp_file_preferences_options(heraia_window_t *main_window)
00172 {
00173         prefs_t *prefs = NULL;
00174         gboolean activated = FALSE;
00175         
00176         if (main_window != NULL)
00177         {
00178                 prefs = main_window->prefs;
00179                 
00180                 /* Saves the position */
00181                 activated = is_toggle_button_activated(main_window->xmls->main, "save_window_position_bt");
00182                 g_key_file_set_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, activated);
00183                         
00184                 /* Saving all window preferences if necessary */
00185                 if (activated == TRUE)
00186                 {
00187                         save_window_preferences(prefs->file, KN_ABOUT_BOX, main_window->win_prop->about_box);
00188                         save_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_window->win_prop->data_interpretor);
00189                         save_window_preferences(prefs->file, KN_LOG_BOX, main_window->win_prop->log_box);
00190                         save_window_preferences(prefs->file, KN_MAIN_DIALOG, main_window->win_prop->main_dialog);
00191                         save_window_preferences(prefs->file, KN_PLUGIN_LIST, main_window->win_prop->plugin_list);
00192                         save_window_preferences(prefs->file, KN_LDT, main_window->win_prop->ldt);
00193                         save_window_preferences(prefs->file, KN_MAIN_PREFS, main_window->win_prop->main_pref_window);
00194                 }
00195         }
00196 }
00197 
00198 
00199 /**
00200  *  Save only display related preferences
00201  *  @param main_window : main structure
00202  */
00203 static void save_mp_display_preferences_options(heraia_window_t *main_window)
00204 {
00205         prefs_t *prefs = NULL;
00206         gboolean activated = FALSE;
00207         
00208         if (main_window != NULL)
00209         {
00210                 prefs = main_window->prefs;
00211                 
00212                 /* Display Thousand (or not) */
00213                 activated = is_toggle_button_activated(main_window->xmls->main, "mp_thousand_bt");
00214                 g_key_file_set_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, activated);
00215         }
00216 }
00217 
00218 
00219 /**
00220  * Saves data interpretor state and preferences
00221  * @param main_window : main structure
00222  */
00223 static void save_di_preferences(heraia_window_t *main_window)
00224 {
00225         GtkNotebook *notebook = NULL;  /**< data interpretor's notebook               */
00226         gint selected_tab;             /**< Selected tab in data interpretor's window */
00227         prefs_t *prefs = NULL;         /**< structure for preferences                 */
00228         
00229         if (main_window != NULL && main_window->current_DW != NULL)
00230         {
00231                 prefs = main_window->prefs;
00232                 
00233                 notebook = GTK_NOTEBOOK(heraia_get_widget(main_window->xmls->main, "diw_notebook"));
00234                 
00235                 if (notebook != NULL)
00236                 {
00237                         selected_tab = gtk_notebook_get_current_page(notebook);
00238                         
00239                         if (selected_tab >= 0)
00240                         {
00241                                 g_key_file_set_integer(prefs->file, GN_DI_PREFS, KN_DI_SELECTED_TAB, selected_tab);
00242                         }
00243                 }
00244         }
00245 }
00246  
00247 
00248 /**
00249  * Save all preferences to the user preference file
00250  * @param main_window the main structure
00251  */
00252 void save_preferences(heraia_window_t *main_window)
00253 {
00254         if (main_window != NULL)
00255         {
00256                 /* 0. Init or verify the struct */
00257                 /* init_preference_struct(main_window); */
00258                 
00259                 /* 1. Saving main Preferences */
00260                 save_mp_file_preferences_options(main_window);
00261                 
00262                 /* 2. Saving Display Preferences */
00263                 save_mp_display_preferences_options(main_window);
00264                 
00265                 /* 3. Saving Data Interpretor Preferences */
00266                 save_di_preferences(main_window);
00267                 
00268                 if (main_window->prefs != NULL)
00269                 {
00270                         /* Saving to file */
00271                         save_preferences_to_file(main_window->prefs);
00272                 }
00273         }
00274 }
00275 
00276 
00277 /**
00278  *  window preferences
00279  *  @param file a GKeyFile where values are stored
00280  *  @param name a keyname (basically a window name)
00281  *  @param window_prop all window properties to save (structure window_prop_t)
00282  */
00283 static void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00284 {
00285         gchar *keyname = NULL;
00286         
00287         keyname = g_strconcat(name, " Displayed", NULL);
00288         window_prop->displayed = g_key_file_get_boolean(file, GN_GLOBAL_PREFS, keyname, NULL);
00289         g_free(keyname);
00290         
00291         keyname = g_strconcat(name, " X_pos", NULL);
00292         window_prop->x = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00293         g_free(keyname);
00294         
00295         keyname = g_strconcat(name, " Y_pos", NULL);
00296         window_prop->y = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00297         g_free(keyname);
00298         
00299         keyname = g_strconcat(name, " Height", NULL);
00300         window_prop->height = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00301         g_free(keyname);
00302 
00303         keyname = g_strconcat(name, " Width", NULL);
00304         window_prop->width = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00305         g_free(keyname);
00306         
00307 }
00308 
00309 
00310 /**
00311  *  Load only main preferences related options
00312  * @param main_window the main structure
00313  */
00314 static void load_mp_file_preferences_options(heraia_window_t *main_window)
00315 {
00316         prefs_t *prefs = NULL;
00317         GtkWidget *save_window_position_bt = NULL;
00318         gboolean activated = FALSE;
00319         
00320         if (main_window != NULL)
00321         {
00322                 prefs = main_window->prefs;
00323                 
00324                 /* Saving window's positions ? */
00325                 activated = g_key_file_get_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, NULL);
00326                 save_window_position_bt = heraia_get_widget(main_window->xmls->main, "save_window_position_bt");
00327                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_window_position_bt), activated);
00328                         
00329                 if (activated == TRUE)
00330                 {
00331                         /* window's positions */
00332                         load_window_preferences(prefs->file, KN_ABOUT_BOX, main_window->win_prop->about_box);
00333                         load_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_window->win_prop->data_interpretor);
00334                         load_window_preferences(prefs->file, KN_LOG_BOX, main_window->win_prop->log_box);
00335                         load_window_preferences(prefs->file, KN_MAIN_DIALOG, main_window->win_prop->main_dialog);
00336                         load_window_preferences(prefs->file, KN_PLUGIN_LIST, main_window->win_prop->plugin_list);
00337                         load_window_preferences(prefs->file, KN_LDT, main_window->win_prop->ldt);
00338                         load_window_preferences(prefs->file, KN_MAIN_PREFS, main_window->win_prop->main_pref_window);   
00339                 }
00340         }
00341 }
00342 
00343 
00344 /**
00345  *  Load display related preferences
00346  * @param main_window the main structure
00347  */
00348 static void load_mp_display_preferences_options(heraia_window_t *main_window)
00349 {
00350         prefs_t *prefs = NULL;
00351         GtkWidget *display_thousand_bt = NULL;
00352         gboolean activated = FALSE;
00353         
00354         if (main_window != NULL)
00355         {
00356                 prefs = main_window->prefs;
00357                 
00358                 /* Display thousands (or not) */
00359                 activated = g_key_file_get_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, NULL);
00360                 display_thousand_bt = heraia_get_widget(main_window->xmls->main, "mp_thousand_bt");
00361                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(display_thousand_bt), activated);
00362         }
00363 }
00364 
00365 
00366 /**
00367  * Load data interpretor state and preferences
00368  * @param main_window : main structure
00369  */
00370 static void load_di_preferences(heraia_window_t *main_window)
00371 {
00372         GtkNotebook *notebook = NULL;  /**< data interpretor's notebook               */
00373         gint selected_tab;             /**< Selected tab in data interpretor's window */
00374         prefs_t *prefs = NULL;         /**< structure for preferences                 */
00375 
00376         if (main_window != NULL && main_window->current_DW != NULL && main_window->xmls != NULL && main_window->xmls->main != NULL)
00377         {
00378                 notebook = GTK_NOTEBOOK(heraia_get_widget(main_window->xmls->main, "diw_notebook"));
00379                 prefs = main_window->prefs;
00380                 
00381                 if (notebook != NULL)
00382                 {
00383                         selected_tab = g_key_file_get_integer(prefs->file, GN_DI_PREFS, KN_DI_SELECTED_TAB, NULL);
00384                         
00385                         if (selected_tab >= 0)
00386                         {
00387                                 gtk_notebook_set_current_page(notebook, selected_tab);
00388                                 main_window->current_DW->tab_displayed = selected_tab;
00389                         }
00390                 }
00391         }
00392 }
00393 
00394 
00395 /**
00396  *  Sets up the preferences as loaded in the preference file
00397  * @param main_window the main structure
00398  */
00399 void load_preferences(heraia_window_t *main_window)
00400 {
00401         if (main_window != NULL)
00402         {
00403                 /* 0. Init or verify the struct */
00404                 /* init_preference_struct(main_window); */
00405                 
00406                 /* 1. Main Preferences */
00407                 load_mp_file_preferences_options(main_window);
00408                 
00409                 /* 2. Display preferences */
00410                 load_mp_display_preferences_options(main_window);
00411                 
00412                 /* 3. Data Interpretor Preferences */
00413                 load_di_preferences(main_window);
00414         }
00415 }

Generated on Sat Mar 14 13:44:29 2009 for Heraia by  doxygen 1.5.6