/* * Gnome Set game - cfg.c * Copyright 2007 Kirill Gorelov * * 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 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. */ #ifdef HAVE_CONFIG_H #include #endif #include #include "gsetgame.h" #include "cfg.h" #include "records.h" /* Gloabal config data structure */ cfg_t cfg; /* create a new ~/.gsetgame file */ static void config_create(void) { gchar *buffer, *file; gsize size; file = g_strconcat(g_get_home_dir(), "/.gsetgame", NULL); g_file_get_contents(PACKAGE_DATA_DIR"/gsetgame/gsetgame.cfg", &buffer, &size, NULL); g_file_set_contents(file, buffer, size, NULL); g_free(file); g_free(buffer); } static const gchar* cfg_get_record_section(GAME_TYPE game_type) { const gchar *tbl_name; /* Choose a correct section name */ switch (game_type) { case GAME_CLASSIC: tbl_name = "records_classic"; break; case GAME_PUZZLE: tbl_name = "records_puzzle"; break; default: tbl_name = NULL; } return tbl_name; } static void cfg_load_records(GKeyFile *file, GAME_TYPE game_type) { int i; const gchar *table_name = cfg_get_record_section(game_type); if (!table_name) return; /* Load records */ for (i=0; i < records_table_size(gset_records_handle(game_type)); i++) { time_t t = 0; int nsets = 0; gchar *name = NULL; gchar *param = NULL; /* Load player name */ param = g_strdup_printf("player_%d", i); name = g_key_file_get_string(file, table_name, param, NULL); g_free(param); /* Load number of sets player got */ param = g_strdup_printf("num_sets_%d", i); nsets = g_key_file_get_integer(file, table_name, param, NULL); g_free(param); /* Load player time */ param = g_strdup_printf("time_%d", i); t = g_key_file_get_integer(file, table_name, param, NULL); g_free(param); if (name) { records_commit_record(gset_records_handle(game_type), t, nsets, name); g_free(name); } } } static void cfg_save_records(GKeyFile *file, GAME_TYPE game_type) { int i; const gchar *table_name = cfg_get_record_section(game_type); /* Save records */ for (i=0; i < records_table_size(gset_records_handle(game_type)); i++) { time_t t = 0; int nsets = 0; gchar *name = NULL; if (records_table_get(gset_records_handle(game_type), i, &t, &nsets, &name)) { gchar *param; if (!name) continue; /* Save player name */ param = g_strdup_printf("player_%d", i); g_key_file_set_string(file, table_name, param, name); g_free(param); /* Save number of sets player got */ param = g_strdup_printf("num_sets_%d", i); g_key_file_set_integer(file, table_name, param, nsets); g_free(param); /* Save player time */ param = g_strdup_printf("time_%d", i); g_key_file_set_integer(file, table_name, param, t); g_free(param); g_free(name); } } } /* Load a configuration from a keyfile pointer */ static void cfg_load_from_keyfile(GKeyFile *file) { /* Load background and cards images locations */ cfg.bgrnd_img = g_key_file_get_string(file, "gsetgame", "bgrnd", NULL); cfg.bgrnd_tiled = g_key_file_get_boolean(file, "gsetgame", "bgrnd_tiled", NULL); /* Load default game type */ cfg.default_game = g_key_file_get_integer(file, "gameopts", "defaultgame", NULL); if (g_key_file_has_key(file, "gameopts", "nosets_auto", NULL)) { cfg.nosets_auto = g_key_file_get_boolean(file, "gameopts", "nosets_auto", NULL); } else { cfg.nosets_auto = TRUE; } if ((cfg.default_game != GAME_CLASSIC) && (cfg.default_game != GAME_PUZZLE)) { cfg.default_game = GAME_CLASSIC; } /* Load records table */ cfg_load_records(file, GAME_CLASSIC); cfg_load_records(file, GAME_PUZZLE); } /* Load the gsetgame configuration */ void cfg_load(void) { GKeyFile *file = g_key_file_new(); gchar *path = g_strconcat(g_get_home_dir(), "/.gsetgame", NULL); if (!g_file_test(path, G_FILE_TEST_EXISTS)) config_create(); g_key_file_load_from_file(file, path, G_KEY_FILE_KEEP_COMMENTS, NULL); cfg_load_from_keyfile(file); g_key_file_free(file); g_free(path); } /* Save the gsetgame configuration */ void cfg_save(void) { GKeyFile *file = g_key_file_new(); gchar *path = g_strconcat(g_get_home_dir(), "/.gsetgame", NULL); g_key_file_load_from_file(file, path, G_KEY_FILE_KEEP_COMMENTS, NULL); /* Save bgrnd and cards images file names */ g_key_file_set_string(file, "gsetgame", "bgrnd", cfg.bgrnd_img); g_key_file_set_boolean(file, "gsetgame", "bgrnd_tiled", cfg.bgrnd_tiled); /* Save default game type */ g_key_file_set_integer(file, "gameopts", "defaultgame", cfg.default_game); g_key_file_set_boolean(file, "gameopts", "nosets_auto", cfg.nosets_auto); cfg_save_records(file, GAME_CLASSIC); cfg_save_records(file, GAME_PUZZLE); gchar *buffer = g_key_file_to_data(file, NULL, NULL); g_key_file_free(file); /* write it to the cfg file */ GIOChannel *channel = g_io_channel_new_file(path, "w", NULL); g_io_channel_write_chars(channel, buffer, -1, NULL, NULL); g_io_channel_unref(channel); g_free(buffer); } void cfg_free(void) { g_free(cfg.bgrnd_img); }