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  * @fn void verify_preference_file_path_presence(gchar *pathname)
00046  *  verify preference file path presence and creates it if it does
00047  *  not already exists
00048  *  @param pathname is a path to look presence for
00049  */
00050 static void verify_preference_file_path_presence(gchar *pathname)
00051 {
00052         struct stat *buf = NULL;
00053         gint result = 0;
00054         
00055         buf = (struct stat *) g_malloc0(sizeof(struct stat));
00056         result = g_stat(pathname, buf);
00057 
00058         if (result != 0)
00059         {
00060                 g_mkdir_with_parents(pathname, 488);
00061         }
00062 }
00063 
00064 /**
00065  * @fn void verify_preference_file_name_presence(gchar *filename)
00066  *  Verify preference file's presence and creates it if it does 
00067  *  not exists already
00068  *  @param filename is a name of a file to look presence for
00069  */
00070 static void verify_preference_file_name_presence(gchar *filename)
00071 {
00072         FILE *fp = NULL;
00073         
00074         fp = g_fopen(filename, "r");
00075         
00076         if (fp == NULL)
00077         {
00078                 fp = g_fopen(filename, "w");    
00079                 if (fp == NULL)
00080                 {
00081                         fprintf(stderr, "Unable to open and create the main preference file %s\n", filename);
00082                 }
00083                 else
00084                 {
00085                         fprintf(stderr, "Main preference file %s created successfully\n", filename);
00086                         fclose(fp);
00087                 }
00088         }
00089         else
00090         {
00091                 fclose(fp);
00092         }
00093 }
00094 
00095 /**
00096  * @fn verify_preference_file(gchar *pathname, gchar *filename)
00097  *  Verify preference file presence and creates it if it does not
00098  *  already exists
00099  * @param pathname is the full pathname
00100  * @param filename is the filename containing the path itself
00101  */
00102 void verify_preference_file(gchar *pathname, gchar *filename)
00103 {
00104         
00105         verify_preference_file_path_presence(pathname);
00106         verify_preference_file_name_presence(filename);
00107         
00108 }
00109 
00110 /**
00111  * @fn void save_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00112  *  Window preferences
00113  *  @param file a GKeyFile where values are stored
00114  *  @param name a keyname (basically a window name)
00115  *  @param window_prop all window properties to save (structure window_prop_t)
00116  */
00117 static void save_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00118 {
00119         gchar *keyname = NULL;
00120         
00121         keyname = g_strconcat(name, " Displayed", NULL);
00122         g_key_file_set_boolean(file, GN_GLOBAL_PREFS, keyname, window_prop->displayed);
00123         g_free(keyname);
00124         
00125         keyname = g_strconcat(name, " X_pos", NULL);
00126         g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->x);
00127         g_free(keyname);
00128         
00129         keyname = g_strconcat(name, " Y_pos", NULL);
00130         g_key_file_set_integer(file, GN_GLOBAL_PREFS, keyname, window_prop->y);
00131         g_free(keyname);
00132 }
00133 
00134 /**
00135  * @fn void save_mp_file_preferences_options(heraia_window_t *main_window)
00136  *  Save only file preferences related options
00137  *  @param main_window the main structure
00138  */
00139 static void save_mp_file_preferences_options(heraia_window_t *main_window)
00140 {
00141         prefs_t *prefs = NULL;
00142         gboolean activated = FALSE;
00143         
00144         if (main_window != NULL)
00145         {
00146                 prefs = main_window->prefs;
00147                 
00148                 if (prefs->file == NULL)
00149                 {
00150                         prefs->file = g_key_file_new();
00151                 }
00152                 
00153                 if (prefs != NULL && prefs->file != NULL)
00154                 {
00155                         /* Saves the position */
00156                         activated = is_toggle_button_activated(main_window->xmls->main, "save_window_position_bt");
00157                         g_key_file_set_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, activated);
00158                         
00159                         /* Saving all window preferences if necessary */
00160                         if (activated == TRUE)
00161                         {
00162                                 save_window_preferences(prefs->file, KN_ABOUT_BOX, main_window->win_prop->about_box);
00163                                 save_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_window->win_prop->data_interpretor);
00164                                 save_window_preferences(prefs->file, KN_LOG_BOX, main_window->win_prop->log_box);
00165                                 save_window_preferences(prefs->file, KN_MAIN_DIALOG, main_window->win_prop->main_dialog);
00166                                 save_window_preferences(prefs->file, KN_PLUGIN_LIST, main_window->win_prop->plugin_list);
00167                                 save_window_preferences(prefs->file, KN_LDT, main_window->win_prop->ldt);
00168                                 save_window_preferences(prefs->file, KN_MAIN_PREFS, main_window->win_prop->main_pref_window);
00169                         }
00170                 }
00171         }
00172 }
00173 
00174 /**
00175  * @fn void save_mp_display_preferences_options(heraia_window_t *main_window)
00176  *  Save only display related preferences
00177  *  @param main_window the main structure
00178  */
00179 static void save_mp_display_preferences_options(heraia_window_t *main_window)
00180 {
00181         prefs_t *prefs = NULL;
00182         gboolean activated = FALSE;
00183         
00184         if (main_window != NULL)
00185         {
00186                 prefs = main_window->prefs;
00187                 
00188                 if (prefs->file == NULL)
00189                 {
00190                         prefs->file = g_key_file_new();
00191                 }
00192                 
00193                 if (prefs != NULL && prefs->file != NULL)
00194                 {
00195                         /* Display Thousand (or not) */
00196                         activated = is_toggle_button_activated(main_window->xmls->main, "mp_thousand_bt");
00197                         g_key_file_set_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, activated);
00198                 }
00199         }
00200 }
00201 
00202 
00203 
00204 /**
00205  * @fn void save_main_preferences(heraia_window_t *main_window)
00206  * Save all preferences to the user preference file
00207  * @param main_window the main structure
00208  */
00209 void save_main_preferences(heraia_window_t *main_window)
00210 {
00211         if (main_window != NULL)
00212         {
00213                 /* 1. Saving main Preferences */
00214                 save_mp_file_preferences_options(main_window);
00215                 
00216                 /* 2. Saving Display Preferences */
00217                 save_mp_display_preferences_options(main_window);
00218                 
00219                 if (main_window->prefs != NULL)
00220                 {
00221                         /* Saving to file */
00222                         save_preferences_to_file(main_window->prefs);
00223                 }
00224         }
00225 }
00226 
00227 
00228 /**
00229  * @fn void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00230  *  window preferences
00231  *  @param file a GKeyFile where values are stored
00232  *  @param name a keyname (basically a window name)
00233  *  @param window_prop all window properties to save (structure window_prop_t)
00234  */
00235 static void load_window_preferences(GKeyFile *file, gchar *name, window_prop_t *window_prop)
00236 {
00237         gchar *keyname = NULL;
00238         
00239         keyname = g_strconcat(name, " Displayed", NULL);
00240         window_prop->displayed = g_key_file_get_boolean(file, GN_GLOBAL_PREFS, keyname, NULL);
00241         g_free(keyname);
00242         
00243         keyname = g_strconcat(name, " X_pos", NULL);
00244         window_prop->x = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00245         g_free(keyname);
00246         
00247         keyname = g_strconcat(name, " Y_pos", NULL);
00248         window_prop->y = g_key_file_get_integer(file, GN_GLOBAL_PREFS, keyname, NULL);
00249         g_free(keyname);
00250 }
00251 
00252 
00253 /**
00254  * @fn void load_mp_file_preferences_options(heraia_window_t *main_window)
00255  *  Load only main preferences related options
00256  * @param main_window the main structure
00257  */
00258 static void load_mp_file_preferences_options(heraia_window_t *main_window)
00259 {
00260         prefs_t *prefs = NULL;
00261         GtkWidget *save_window_position_bt = NULL;
00262         gboolean activated = FALSE;
00263         
00264         if (main_window != NULL)
00265         {
00266                 prefs = main_window->prefs;
00267                 
00268                 if (prefs != NULL && prefs->file != NULL)
00269                 {
00270                         /* Saving window's positions ? */
00271                         activated = g_key_file_get_boolean(prefs->file, GN_GLOBAL_PREFS, KN_SAVE_WINDOW_PREFS, NULL);
00272                         save_window_position_bt = heraia_get_widget(main_window->xmls->main, "save_window_position_bt");
00273                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(save_window_position_bt), activated);
00274                         
00275                         if (activated == TRUE)
00276                         {
00277                                 /* window's positions */
00278                                 load_window_preferences(prefs->file, KN_ABOUT_BOX, main_window->win_prop->about_box);
00279                                 load_window_preferences(prefs->file, KN_DATA_INTERPRETOR, main_window->win_prop->data_interpretor);
00280                                 load_window_preferences(prefs->file, KN_LOG_BOX, main_window->win_prop->log_box);
00281                                 load_window_preferences(prefs->file, KN_MAIN_DIALOG, main_window->win_prop->main_dialog);
00282                                 load_window_preferences(prefs->file, KN_PLUGIN_LIST, main_window->win_prop->plugin_list);
00283                                 load_window_preferences(prefs->file, KN_LDT, main_window->win_prop->ldt);
00284                                 load_window_preferences(prefs->file, KN_MAIN_PREFS, main_window->win_prop->main_pref_window);   
00285                         }
00286                 }       
00287         }
00288 }
00289 
00290 
00291 /**
00292  * @fn void load_mp_display_preferences_options(heraia_window_t *main_window)
00293  *  Load display related preferences
00294  * @param main_window the main structure
00295  */
00296 static void load_mp_display_preferences_options(heraia_window_t *main_window)
00297 {
00298         prefs_t *prefs = NULL;
00299         GtkWidget *display_thousand_bt = NULL;
00300         gboolean activated = FALSE;
00301         
00302         if (main_window != NULL)
00303         {
00304                 prefs = main_window->prefs;
00305                 
00306                 if (prefs != NULL && prefs->file != NULL)
00307                 {
00308                         /* Display thousands (or not) */
00309                         activated = g_key_file_get_boolean(prefs->file, GN_DISPLAY_PREFS, KN_DISP_THOUSAND, NULL);
00310                         display_thousand_bt = heraia_get_widget(main_window->xmls->main, "mp_thousand_bt");
00311                         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(display_thousand_bt), activated);
00312                 }
00313         }
00314 }
00315 
00316 
00317 /**
00318  * @fn setup_preferences(heraia_window_t *main_window)
00319  *  Sets up the preferences as loaded in the preference file
00320  * @param main_window the main structure
00321  */
00322 void setup_preferences(heraia_window_t *main_window)
00323 {
00324         if (main_window != NULL)
00325         {
00326                 /* 1. Main Preferences */
00327                 load_mp_file_preferences_options(main_window);
00328                 
00329                 /* 2. Display preferences */
00330                 load_mp_display_preferences_options(main_window);
00331         }
00332 }

Generated on Sat Feb 14 11:44:16 2009 for Heraia by  doxygen 1.5.6