Logo Search packages:      
Sourcecode: kanatest version File versions

prefs.c

/*
 * Kanatest
 *
 * Copyright (C) 2001-2004, 2006 Tomasz Mka <pasp@users.sourceforge.net>
 *
 * 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.
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <limits.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

#include "prefs.h"
#include "main.h"
#include "test.h"

struct jpprefs config;

gchar* 
prefs_get_config_filename (void)
{
static gchar dirname[PATH_MAX], filename[PATH_MAX];
struct stat cfg;

    strncpy (dirname, getenv ("HOME"), PATH_MAX);
    strcat (dirname, "/");
    strcat (dirname, CONFIG_DIRNAME);

    if(stat (dirname, &cfg) < 0)
        mkdir (dirname, S_IRUSR | S_IWUSR | S_IXUSR);

    strcpy (filename, dirname);
    strcat (filename, "/");
    strcat (filename, CONFIG_FILENAME);

    return filename;
}


void 
prefs_set_default_values (void)
{
    config.window_x = 180;
    config.window_y = 120;
    config.window_size_x = 365;
    config.window_size_y = 445;
    config.repeat_wrong = FALSE;
    config.kana_mode = HIRAGANA;
    config.kana_set = 0;            /* all kanas */
    config.ca_timeout = 1;          /* 1 sec */
    config.enable_tooltips = TRUE;
    config.stat_window_x = 45;
    config.stat_window_y = 120;
    config.stat_size_x = 760;
    config.stat_size_y = 450;
    config.chart_window_x = 200;
    config.chart_window_y = 60;
    config.about_window_x = 320;
    config.about_window_y = 165;
    config.options_window_x = 280;
    config.options_window_y = 150;
    config.stats_sort_column = 0;
    config.stats_sort_column_dir = 1;
    strncpy(config.kana_font_face, "Serif", MAX_FONTNAME_LEN-1);
    strncpy(config.kana_color, "#000000", MAX_COLORNAME_LEN-1);
    strncpy(config.romaji_color, "#BB1010", MAX_COLORNAME_LEN-1);
    strncpy(config.user_defined_lesson, "+++++----------------------------------------------------------------------------------------------------", NUMBER_OF_SIGNS);
}


void 
prefs_read_config (void)
{
gboolean cfg_file;
xmlDocPtr doc;
xmlNodePtr node;
xmlChar *key;

    cfg_file = g_file_test(prefs_get_config_filename(), G_FILE_TEST_IS_REGULAR);

    if (cfg_file == TRUE) {

        doc = xmlParseFile(prefs_get_config_filename());
        if (doc == NULL) 
                return;

        node = xmlDocGetRootElement(doc);
        if (node == NULL) {
                xmlFreeDoc(doc);
                return;
        }

        if (xmlStrcmp(node->name, (const xmlChar *) CONFIG_NAME)) {
                xmlFreeDoc(doc);
                return;
        }

        prefs_set_default_values();
       
        node = node->xmlChildrenNode;

        while (node != NULL) {

                if ((!xmlStrcmp(node->name, (const xmlChar *) "window_x"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.window_x);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "window_y"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.window_y);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "window_size_x"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.window_size_x);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "window_size_y"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.window_size_y);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "repeat_wrong"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.repeat_wrong);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "kana_mode"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.kana_mode);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "kana_set"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.kana_set);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "ca_timeout"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.ca_timeout);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "enable_tooltips"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.enable_tooltips);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "stat_window_x"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.stat_window_x);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "stat_window_y"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.stat_window_y);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "stat_size_x"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.stat_size_x);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "stat_size_y"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.stat_size_y);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "about_window_x"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.about_window_x);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "about_window_y"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.about_window_y);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "options_window_x"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.options_window_x);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "options_window_y"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.options_window_y);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "chart_window_x"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.chart_window_x);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "chart_window_y"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.chart_window_y);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "stats_sort_column"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.stats_sort_column);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *) "stats_sort_column_dir"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            sscanf((gchar *) key, "%d", &config.stats_sort_column_dir);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *)"kana_font_face"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            strncpy(config.kana_font_face, (gchar *) key, MAX_FONTNAME_LEN-1);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *)"kana_color"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            strncpy(config.kana_color, (gchar *) key, MAX_COLORNAME_LEN-1);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *)"romaji_color"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL)
                            strncpy(config.romaji_color, (gchar *) key, MAX_COLORNAME_LEN-1);
                    xmlFree(key);
                }

                if ((!xmlStrcmp(node->name, (const xmlChar *)"user_defined_lesson"))) {
                    key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
                    if (key != NULL) 
                    {
                            strncpy(config.user_defined_lesson, (gchar *) key, NUMBER_OF_SIGNS);
                            config.user_defined_lesson[NUMBER_OF_SIGNS+1] = '\0';
                    }
                    xmlFree(key);
                }



            node = node->next;
        }

        xmlFreeDoc(doc);

    } else {

        prefs_set_default_values();
        prefs_write_config ();
    }

}


void prefs_write_config(void)
{
xmlDocPtr doc;
xmlNodePtr node;
gchar temp[MAX_BUFFER_SIZE];

    doc = xmlNewDoc((const xmlChar *) "1.0");
    node = xmlNewNode(NULL, (const xmlChar *) CONFIG_NAME);
    xmlDocSetRootElement(doc, node);

    snprintf(temp, 32, "%d", config.window_x);
    xmlNewTextChild(node, NULL, (const xmlChar *) "window_x", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.window_y);
    xmlNewTextChild(node, NULL, (const xmlChar *) "window_y", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.window_size_x);
    xmlNewTextChild(node, NULL, (const xmlChar *) "window_size_x", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.window_size_y);
    xmlNewTextChild(node, NULL, (const xmlChar *) "window_size_y", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.repeat_wrong);
    xmlNewTextChild(node, NULL, (const xmlChar *) "repeat_wrong", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.kana_mode);
    xmlNewTextChild(node, NULL, (const xmlChar *) "kana_mode", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.kana_set);
    xmlNewTextChild(node, NULL, (const xmlChar *) "kana_set", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.ca_timeout);
    xmlNewTextChild(node, NULL, (const xmlChar *) "ca_timeout", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.enable_tooltips);
    xmlNewTextChild(node, NULL, (const xmlChar *) "enable_tooltips", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.stat_window_x);
    xmlNewTextChild(node, NULL, (const xmlChar *) "stat_window_x", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.stat_window_y);
    xmlNewTextChild(node, NULL, (const xmlChar *) "stat_window_y", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.stat_size_x);
    xmlNewTextChild(node, NULL, (const xmlChar *) "stat_size_x", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.stat_size_y);
    xmlNewTextChild(node, NULL, (const xmlChar *) "stat_size_y", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.about_window_x);
    xmlNewTextChild(node, NULL, (const xmlChar *) "about_window_x", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.about_window_y);
    xmlNewTextChild(node, NULL, (const xmlChar *) "about_window_y", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.options_window_x);
    xmlNewTextChild(node, NULL, (const xmlChar *) "options_window_x", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.options_window_y);
    xmlNewTextChild(node, NULL, (const xmlChar *) "options_window_y", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.chart_window_x);
    xmlNewTextChild(node, NULL, (const xmlChar *) "chart_window_x", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.chart_window_y);
    xmlNewTextChild(node, NULL, (const xmlChar *) "chart_window_y", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.stats_sort_column);
    xmlNewTextChild(node, NULL, (const xmlChar *) "stats_sort_column", (xmlChar *) temp);
    snprintf(temp, 32, "%d", config.stats_sort_column_dir);
    xmlNewTextChild(node, NULL, (const xmlChar *) "stats_sort_column_dir", (xmlChar *) temp);
    snprintf(temp, MAX_FONTNAME_LEN, "%s", config.kana_font_face);
    xmlNewTextChild(node, NULL, (const xmlChar *) "kana_font_face", (xmlChar *) temp);
    snprintf(temp, MAX_COLORNAME_LEN, "%s", config.kana_color);
    xmlNewTextChild(node, NULL, (const xmlChar *) "kana_color", (xmlChar *) temp);
    snprintf(temp, MAX_COLORNAME_LEN, "%s", config.romaji_color);
    xmlNewTextChild(node, NULL, (const xmlChar *) "romaji_color", (xmlChar *) temp);
    snprintf(temp, NUMBER_OF_SIGNS+1, "%s", config.user_defined_lesson);
    xmlNewTextChild(node, NULL, (const xmlChar *) "user_defined_lesson", (xmlChar *) temp);

    xmlSaveFormatFile(prefs_get_config_filename(), doc, 1);
    xmlFreeDoc(doc);
}



Generated by  Doxygen 1.6.0   Back to index