/*************************************************************************** * prefs.c - use gconf to store and retrieve preferences * * Tue Sep 28 02:08:58 2004 * Copyright 2004 imcintosh * ian_mcintosh@linuxadvocate.org ****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include "../include/main.h" #include "../include/gui.h" // // Local defines // #define GCONF_ROOT "/apps/gruler" // Glade XML context containing our GUI static GConfClient* g_pGConfClient; static gboolean g_bPrefsRead = FALSE; // don't allow any pref SETing until they have been read once // // Utilities // static gboolean gconf_client_set_color_hex_triplet(GConfClient* pConf, const gchar* pKey, GdkColor* pColor) { gchar buffer[10]; g_snprintf(buffer, 10, "#%02X%02X%02X", (int)((pColor->red / 256L)), (int)((pColor->green / 256L)), (int)((pColor->blue / 256L))); return gconf_client_set_string(pConf, pKey, buffer, NULL); } gboolean pref_read_bool(const gchar* pKey, gboolean bDefault) { gboolean bReturn = bDefault; GConfValue* pValue = gconf_client_get(g_pGConfClient, pKey, NULL); if(pValue) { if(pValue->type == GCONF_VALUE_BOOL) { bReturn = gconf_value_get_bool(pValue); } gconf_value_free(pValue); } return bReturn; } // // Read / Write // void prefs_read() { gchar* str = NULL; // Read foreground color GdkColor clrForeground = DEFAULT_FOREGROUND_COLOR; if((str = gconf_client_get_string(g_pGConfClient, GCONF_ROOT"/foreground_color", NULL)) != NULL) { gdk_color_parse(str, &clrForeground); } gui_set_foreground_color(&clrForeground); // Read background color GdkColor clrBackground = DEFAULT_BACKGROUND_COLOR; if((str = gconf_client_get_string(g_pGConfClient, GCONF_ROOT"/background_color", NULL)) != NULL) { gdk_color_parse(str, &clrBackground); } gui_set_background_color(&clrBackground); // Read font if((str = gconf_client_get_string(g_pGConfClient, GCONF_ROOT"/font", NULL)) != NULL) { gui_set_font_string(str); } // Read ruler length gint nLength; nLength=gconf_client_get_int(g_pGConfClient, GCONF_ROOT"/ruler_length", NULL); if(nLength == 0) { nLength = DEFAULT_RULER_LENGTH; } gui_set_ruler_length(NULL, nLength); // Set mouse-measurement-lines preference gboolean bDrawMeasurementLines = gconf_client_get_bool(g_pGConfClient, GCONF_ROOT"/measurement_lines", NULL); gui_set_draw_mouse_measurement_lines(bDrawMeasurementLines); // Set metric gint nMetric; nMetric = gconf_client_get_int(g_pGConfClient, GCONF_ROOT"/metric", NULL); gui_set_ruler_metric(nMetric); // get window keepabove ("On Top") //gboolean bKeepAbove = gconf_client_get_bool(g_pGConfClient, GCONF_ROOT"/keep_above", NULL); gboolean bKeepAbove = pref_read_bool(GCONF_ROOT"/keep_above", DEFAULT_KEEP_ABOVE); gui_set_keep_above(bKeepAbove); GConfValue* pValue; // get ruler x,y if present (otherwise it'll end up centered) pValue = gconf_client_get(g_pGConfClient, GCONF_ROOT"/ruler_x", NULL); if(pValue) { gint nX,nY; if(pValue->type == GCONF_VALUE_INT) { nX = gconf_value_get_int(pValue); // will be 0 if it doesn't exist, but it should if X exists nY = gconf_client_get_int(g_pGConfClient, GCONF_ROOT"/ruler_y", NULL); gui_ruler_set_position(nX, nY); } gconf_value_free(pValue); } // get ruler orientation pValue = gconf_client_get(g_pGConfClient, GCONF_ROOT"/ruler_orientation", NULL); if(pValue) { if(pValue->type == GCONF_VALUE_INT) { gui_set_ruler_orientation(gconf_value_get_int(pValue)); } gconf_value_free(pValue); } // All done g_bPrefsRead = TRUE; } void prefs_write_style() { if(!g_bPrefsRead) return; // Write background color GdkColor clrBackground; gui_get_background_color(&clrBackground); gconf_client_set_color_hex_triplet(g_pGConfClient, GCONF_ROOT"/background_color", &clrBackground); // Write background color GdkColor clrForeground; gui_get_foreground_color(&clrForeground); gconf_client_set_color_hex_triplet(g_pGConfClient, GCONF_ROOT"/foreground_color", &clrForeground); // Write font gconf_client_set_string(g_pGConfClient, GCONF_ROOT"/font", gui_get_font_string(), NULL); // Write measurement lines setting gboolean bDrawMeasurementLines = gui_get_draw_mouse_measurement_lines(); gconf_client_set_bool(g_pGConfClient, GCONF_ROOT"/measurement_lines", bDrawMeasurementLines, NULL); // write metric gconf_client_set_int(g_pGConfClient, GCONF_ROOT"/metric", gui_get_ruler_metric(), NULL); // write keepabove ("On Top") gboolean bKeepAbove = gui_get_keep_above(); gconf_client_set_bool(g_pGConfClient, GCONF_ROOT"/keep_above", bKeepAbove, NULL); } //~ void prefs_write_horizontal_length(gint nLength) //~ { //~ if(!g_bPrefsRead) return; //~ if(nLength == 0) { //~ nLength = gui_get_horizontal_length(); //~ } //~ gconf_client_set_int(g_pGConfClient, GCONF_ROOT"/horizontal_length", nLength, NULL); //~ } void prefs_write_length(gint nLength) { if(!g_bPrefsRead) return; if(nLength == 0) { nLength = gui_get_ruler_length(NULL); } gconf_client_set_int(g_pGConfClient, GCONF_ROOT"/ruler_length", nLength, NULL); } void prefs_write_postition() { gint nX,nY; gui_ruler_get_position(&nX, &nY); gconf_client_set_int(g_pGConfClient, GCONF_ROOT"/ruler_x", nX, NULL); gconf_client_set_int(g_pGConfClient, GCONF_ROOT"/ruler_y", nY, NULL); gconf_client_set_int(g_pGConfClient, GCONF_ROOT"/ruler_orientation", gui_get_ruler_orientation(), NULL); } void prefs_write() { if(!g_bPrefsRead) return; // don't ever write them before they've been read in at least once prefs_write_style(); prefs_write_length(0); prefs_write_postition(); } gboolean prefs_init(int argc, char *argv[]) { gconf_init(argc, argv, NULL); g_pGConfClient = gconf_client_get_default(); //gconf_client_new(); g_return_val_if_fail(g_pGConfClient != NULL, FALSE); return TRUE; }