/** * a_preferences_lookup: * @key: The name of a preference * * See if a key exists in the preferences file * (without actually processing the file into preferences) */ gboolean a_preferences_lookup(const gchar *key) { gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL); FILE *f = g_fopen(fn, "r"); g_free ( fn ); gboolean ans = FALSE; if ( f ) { gchar buf[4096]; gchar *fkey = NULL; gchar *val = NULL; gboolean exit_now = FALSE; while ( ! feof (f) ) { if (fgets(buf,sizeof(buf),f) == NULL) break; if ( split_string_from_file_on_equals ( buf, &fkey, &val ) ) { if ( g_strcmp0 (key, fkey) == 0 ) { ans = TRUE; exit_now = TRUE; } g_free(fkey); g_free(val); if ( exit_now ) break; } } fclose(f); } return ans; }
/** * a_preferences_save_to_file: * * Returns: TRUE on success */ gboolean a_preferences_save_to_file() { gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL); // TODO: error checking FILE *f = g_fopen(fn, "w"); /* Since preferences files saves OSM login credentials, * it'll be better to store it in secret. */ g_chmod(fn, 0600); g_free ( fn ); if ( f ) { VikLayerParam *param; VikLayerTypedParamData *val; int i; for ( i = 0; i < params->len; i++ ) { param = (VikLayerParam *) g_ptr_array_index(params,i); val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name ); if ( val ) if ( val->type != VIK_LAYER_PARAM_PTR ) file_write_layer_param ( f, param->name, val->type, val->data ); } fclose(f); f = NULL; return TRUE; } return FALSE; }
static gboolean defaults_load_from_file() { GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS; GError *error = NULL; gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_LAYER_DEFAULTS_INI_FILE, NULL ); if ( !g_key_file_load_from_file ( keyfile, fn, flags, &error ) ) { g_warning ( "%s: %s", error->message, fn ); g_free ( fn ); g_error_free ( error ); return FALSE; } g_free ( fn ); // Ensure if have a key file, then any missing values are set from the internal defaults VikLayerTypeEnum layer; for ( layer = 0; layer < VIK_LAYER_NUM_TYPES; layer++ ) { use_internal_defaults_if_missing_default ( layer ); } return TRUE; }
/** * a_preferences_save_to_file: * * Returns: TRUE on success */ gboolean a_preferences_save_to_file() { gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL); FILE *f = g_fopen(fn, "w"); // Since preferences file may contain sensitive information, // it'll be better to store it so it can only be read by the user if ( g_chmod(fn, 0600) != 0 ) g_warning ( "%s: Failed to set permissions on %s", __FUNCTION__, fn ); g_free ( fn ); if ( f ) { VikLayerParam *param; VikLayerTypedParamData *val; int i; for ( i = 0; i < params->len; i++ ) { param = (VikLayerParam *) g_ptr_array_index(params,i); val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name ); if ( val ) if ( val->type != VIK_LAYER_PARAM_PTR ) file_write_layer_param ( f, param->name, val->type, val->data ); } fclose(f); f = NULL; return TRUE; } return FALSE; }
/** * a_settings_uninit: * * ATM: The only time settings are saved is on program exit * Could change this to occur on window exit or dialog exit or have memory hash of values...? */ void a_settings_uninit() { GError *error = NULL; gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL ); gsize size; gchar *keyfilestr = g_key_file_to_data ( keyfile, &size, &error ); if ( error ) { g_warning ( "%s", error->message ); g_error_free ( error ); goto tidy; } g_file_set_contents ( fn, keyfilestr, size, &error ); if ( error ) { g_warning ( "%s: %s", error->message, fn ); g_error_free ( error ); } g_key_file_free ( keyfile ); tidy: g_free ( keyfilestr ); g_free ( fn ); }
/* TRUE on success */ static gboolean preferences_save_to_file() { gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL); // TODO: error checking FILE *f = g_fopen(fn, "w"); g_free ( fn ); if ( f ) { VikLayerParam *param; VikLayerTypedParamData *val; int i; for ( i = 0; i < params->len; i++ ) { param = (VikLayerParam *) g_ptr_array_index(params,i); val = (VikLayerTypedParamData *) g_hash_table_lookup ( values, param->name ); g_assert ( val != NULL ); file_write_layer_param ( f, param->name, val->type, val->data ); } fclose(f); f = NULL; return TRUE; } return FALSE; }
static gchar *get_cookie_file(gboolean init) { static gchar *cookie_file = NULL; static GMutex *mutex = NULL; if (init) { /* to make sure it's thread safe */ mutex = g_mutex_new(); static gchar *cookie_fn = "cookies.txt"; const gchar *viking_dir = a_get_viking_dir(); cookie_file = g_build_filename(viking_dir, cookie_fn, NULL); g_unlink(cookie_file); return NULL; } g_assert(cookie_file != NULL); g_mutex_lock(mutex); if (g_file_test(cookie_file, G_FILE_TEST_EXISTS) == FALSE) { /* file not there */ gchar * name_tmp = NULL; FILE * out_file = tmpfile(); if (out_file == NULL) { // Something wrong with previous call (unsuported?) name_tmp = g_strdup_printf("%s.tmp", cookie_file); out_file = g_fopen(name_tmp, "w+b"); } CURLcode res; CURL *curl = curl_easy_init(); if (vik_verbose) curl_easy_setopt ( curl, CURLOPT_VERBOSE, 1 ); curl_easy_setopt(curl, CURLOPT_URL, "http://maps.google.com/"); /* google.com sets "PREF" cookie */ curl_easy_setopt ( curl, CURLOPT_WRITEDATA, out_file ); curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curl_write_func); curl_easy_setopt(curl, CURLOPT_COOKIEJAR, cookie_file); res = curl_easy_perform(curl); if (res != CURLE_OK) { g_warning(_("%s() Curl perform failed: %s"), __PRETTY_FUNCTION__, curl_easy_strerror(res)); g_unlink(cookie_file); } curl_easy_cleanup(curl); fclose(out_file); out_file = NULL; if (name_tmp != NULL) { g_remove(name_tmp); g_free(name_tmp); name_tmp = NULL; } } g_mutex_unlock(mutex); return(cookie_file); }
static gchar *get_cookie_file(gboolean init) { static gchar *cookie_file = NULL; // Wipe any previous cookies set on startup (for some reason??) // Startup is single threaded so don't care about mutexes if (init) { static gchar *cookie_fn = "cookies.txt"; const gchar *viking_dir = a_get_viking_dir(); cookie_file = g_build_filename(viking_dir, cookie_fn, NULL); g_unlink(cookie_file); return NULL; } return(cookie_file); }
/* TRUE on success */ static gboolean layer_defaults_save_to_file() { gboolean answer = TRUE; GError *error = NULL; gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_LAYER_DEFAULTS_INI_FILE, NULL ); gsize size; gchar *keyfilestr = g_key_file_to_data ( keyfile, &size, &error ); if ( error ) { g_warning ( error->message ); g_error_free ( error ); answer = FALSE; goto tidy; } // optionally could do: // g_file_set_contents ( fn, keyfilestr, size, &error ); // if ( error ) { // g_warning ( "%s: %s", error->message, fn ); // g_error_free ( error ); // answer = FALSE; // goto tidy; // } FILE *ff; if ( !(ff = g_fopen ( fn, "w")) ) { g_warning ( _("Could not open file: %s"), fn ); answer = FALSE; goto tidy; } // Layer defaults not that secret, but just incase... g_chmod ( fn, 0600 ); fputs ( keyfilestr, ff ); fclose ( ff ); tidy: g_free ( keyfilestr ); g_free ( fn ); return answer; }
static gboolean settings_load_from_file() { GKeyFileFlags flags = G_KEY_FILE_KEEP_COMMENTS; GError *error = NULL; gchar *fn = g_build_filename ( a_get_viking_dir(), VIKING_INI_FILE, NULL ); if ( !g_key_file_load_from_file ( keyfile, fn, flags, &error ) ) { g_warning ( "%s: %s", error->message, fn ); g_free ( fn ); g_error_free ( error ); return FALSE; } g_free ( fn ); return TRUE; }
static gboolean preferences_load_from_file() { gchar *fn = g_build_filename(a_get_viking_dir(), VIKING_PREFS_FILE, NULL); FILE *f = g_fopen(fn, "r"); g_free ( fn ); if ( f ) { gchar buf[4096]; gchar *key = NULL; gchar *val = NULL; VikLayerTypedParamData *oldval, *newval; while ( ! feof (f) ) { if (fgets(buf,sizeof(buf),f) == NULL) break; if ( split_string_from_file_on_equals ( buf, &key, &val ) ) { // if it's not in there, ignore it oldval = g_hash_table_lookup ( values, key ); if ( ! oldval ) { g_free(key); g_free(val); continue; } // otherwise change it (you know the type!) // if it's a string list do some funky stuff ... yuck... not yet. if ( oldval->type == VIK_LAYER_PARAM_STRING_LIST ) g_critical ( "Param strings not implemented in preferences"); // fake it newval = vik_layer_data_typed_param_copy_from_string ( oldval->type, val ); g_hash_table_insert ( values, key, newval ); g_free(key); g_free(val); // change value } } fclose(f); f = NULL; return TRUE; } return FALSE; }