/* * utils.cc * This file is part of him-arabic * * Copyright (C) 2007 Mohammed Sameer * * 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 "utils.hh" #include #include #include #define GCONF_NODE "/apps/foolab/him_arabic" #define GCONF_KEY GCONF_NODE"/list" std::string get_data(const std::string& locale, const std::string& lang, std::string& which) { static std::stringstream data_dir; if (data_dir.str().size() == 0) { data_dir << DATADIR << G_DIR_SEPARATOR << PACKAGE << G_DIR_SEPARATOR; } std::string res; if (do_read(data_dir.str()+locale+".xml", res)) { which = locale; return res; } else if (do_read(data_dir.str()+lang+".xml", res)) { which = lang; return res; } else { return res; } } bool do_read(const std::string& file, std::string& res) { gchar *contents = NULL; if (g_file_get_contents(file.c_str(), &contents, NULL, NULL)) { res = contents; g_free(contents); return true; } else { return false; } } std::string escape(const std::string& str) { std::string ret; for (unsigned x = 0; x < str.size(); x++) { if (str[x] == '<') { ret += "<"; } else if (str[x] == '&') { ret += "&"; } else { ret += str[x]; } } return ret; } char **get_languages() { // Seems hildon-im-recache is calling us without initializing gtype. g_type_init(); GSList *list = get_languages_as_slist(); if (list == NULL) { return NULL; } gchar **ret = g_new(gchar *, g_slist_length(list)+1); gchar **elem = ret; for (GSList *l = list; l; l = g_slist_next(l)) { (*elem) = (char *)l->data; ++elem; } (*elem) = NULL; // We will not free because we are not copying the data. // g_slist_foreach(list, (GFunc)g_free, NULL); g_slist_free(list); return ret; } GSList *get_languages_as_slist() { GConfClient *client = gconf_client_get_default(); if (!client) { return NULL; } GSList *list = gconf_client_get_list(client, GCONF_KEY, GCONF_VALUE_STRING, NULL); g_object_unref(client); return list; } bool set_languages(GSList *& list) { GConfClient *client = gconf_client_get_default(); if (!client) { return false; } // Let's add it if it;s not there: gconf_client_add_dir(client, GCONF_NODE, GCONF_CLIENT_PRELOAD_NONE, NULL); bool ret = gconf_client_set_list(client, GCONF_KEY, GCONF_VALUE_STRING, list, NULL); g_object_unref(client); return ret; }