* Pidgin is the legal property of its developers, whose names are too numerous * to list here. Please refer to the COPYRIGHT file distributed with this * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA #include "gtksmiley-theme.h" #define PIDGIN_SMILEY_THEME_MAX_LINES 1024 #define PIDGIN_SMILEY_THEME_MAX_TOKENS 1024 * An implementation of a smiley theme. struct _PidginSmileyTheme PurpleSmileyTheme parent; GHashTable *smiley_lists_map; } PidginSmileyThemePrivate; static gchar **probe_dirs; static GList *smiley_themes = NULL; } PidginSmileyThemeIndex; } PidginSmileyThemeIndexProtocol; } PidginSmileyThemeIndexSmiley; G_DEFINE_TYPE_WITH_PRIVATE(PidginSmileyTheme, pidgin_smiley_theme, PURPLE_TYPE_SMILEY_THEME); /******************************************************************************* ******************************************************************************/ pidgin_smiley_theme_index_smiley_free(PidginSmileyThemeIndexSmiley *smiley) g_return_if_fail(smiley != NULL); g_list_free_full(smiley->shortcuts, g_free); pidgin_smiley_theme_index_protocol_free(PidginSmileyThemeIndexProtocol *proto) g_return_if_fail(proto != NULL); g_list_free_full(proto->smileys, (GDestroyNotify)pidgin_smiley_theme_index_smiley_free); pidgin_smiley_theme_index_free(PidginSmileyThemeIndex *index) g_return_if_fail(index != NULL); g_list_free_full(index->protocols, (GDestroyNotify)pidgin_smiley_theme_index_protocol_free); static PidginSmileyThemeIndex * pidgin_smiley_theme_index_parse(const gchar *theme_path, gboolean load_contents) PidginSmileyThemeIndex *index; PidginSmileyThemeIndexProtocol *proto = NULL; gboolean inv_frm = FALSE; index_path = g_build_filename(theme_path, "theme", NULL); file = g_fopen(index_path, "r"); purple_debug_error("gtksmiley-theme", "Failed to open index file %s", index_path); index = g_new0(PidginSmileyThemeIndex, 1); PidginSmileyThemeIndexSmiley *smiley; if (++line_no > PIDGIN_SMILEY_THEME_MAX_LINES) { purple_debug_warning("gtksmiley-theme", "file too big"); if (!fgets(buff, sizeof(buff), file)) if (!g_utf8_validate(buff, -1, NULL)) { purple_debug_error("gtksmiley-theme", "%s:%d is invalid UTF-8", proto->smileys = g_list_reverse(proto->smileys); proto = g_new0(PidginSmileyThemeIndexProtocol, 1); proto->name = g_strndup(line, end - line); g_list_prepend(index->protocols, proto); if ((eqchr = strchr(line, '='))) { if (g_ascii_strcasecmp(line, "name") == 0) { index->name = g_strdup(eqchr + 1); } else if (g_ascii_strcasecmp(line, "description") == 0) { index->desc = g_strdup(eqchr + 1); } else if (g_ascii_strcasecmp(line, "icon") == 0) { index->icon = g_strdup(eqchr + 1); } else if (g_ascii_strcasecmp(line, "author") == 0) { index->author = g_strdup(eqchr + 1); g_strchug(index->author); /* parsing section content */ smiley = g_new0(PidginSmileyThemeIndexSmiley, 1); proto->smileys = g_list_prepend(proto->smileys, smiley); split = g_strsplit_set(line, " \t", PIDGIN_SMILEY_THEME_MAX_TOKENS); for (i = 0; split[i]; i++) { if (i == PIDGIN_SMILEY_THEME_MAX_TOKENS - 1) smiley->file = g_strdup(token); smiley->shortcuts = g_list_prepend(smiley->shortcuts, smiley->shortcuts = g_list_reverse(smiley->shortcuts); proto->smileys = g_list_reverse(proto->smileys); purple_debug_error("gtksmiley-theme", "%s:%d" " invalid format", index_path, line_no); pidgin_smiley_theme_index_free(index); /******************************************************************************* ******************************************************************************/ pidgin_smiley_theme_load(const gchar *theme_path) PidginSmileyTheme *theme; PidginSmileyThemePrivate *priv; PidginSmileyThemeIndex *index; /* it's not super-efficient, but we don't expect huge amount of for (it = smiley_themes; it; it = g_list_next(it)) { PidginSmileyThemePrivate *priv = pidgin_smiley_theme_get_instance_private(it->data); /* theme is already loaded */ if (g_strcmp0(priv->path, theme_path) == 0) theme = g_object_new(PIDGIN_TYPE_SMILEY_THEME, NULL); priv = pidgin_smiley_theme_get_instance_private(theme); priv->path = g_strdup(theme_path); index = pidgin_smiley_theme_index_parse(theme_path, FALSE); if (!index->name || index->name[0] == '\0') { purple_debug_warning("gtksmiley-theme", "incomplete theme %s", theme_path); pidgin_smiley_theme_index_free(index); priv->name = g_strdup(index->name); if (index->desc && index->desc[0]) priv->desc = g_strdup(index->desc); if (index->icon && index->icon[0]) priv->icon = g_strdup(index->icon); if (index->author && index->author[0]) priv->author = g_strdup(index->author); pidgin_smiley_theme_index_free(index); smiley_themes = g_list_append(smiley_themes, theme); pidgin_smiley_theme_probe(void) /* remove non-existing themes */ for (it = smiley_themes; it; it = next) { PidginSmileyTheme *theme = it->data; PidginSmileyThemePrivate *priv = pidgin_smiley_theme_get_instance_private(theme); if (g_file_test(priv->path, G_FILE_TEST_EXISTS)) smiley_themes = g_list_delete_link(smiley_themes, it); for (i = 0; probe_dirs[i]; i++) { GDir *dir = g_dir_open(probe_dirs[i], 0, NULL); const gchar *theme_dir_name; while ((theme_dir_name = g_dir_read_name(dir))) { /* Ignore Pidgin 2.x.y "none" theme. */ if (g_strcmp0(theme_dir_name, "none") == 0) theme_path = g_build_filename( probe_dirs[i], theme_dir_name, NULL); if (g_file_test(theme_path, G_FILE_TEST_IS_DIR)) pidgin_smiley_theme_load(theme_path); /******************************************************************************* ******************************************************************************/ pidgin_smiley_theme_get_name(PidginSmileyTheme *theme) PidginSmileyThemePrivate *priv = NULL; g_return_val_if_fail(PIDGIN_IS_SMILEY_THEME(theme), NULL); priv = pidgin_smiley_theme_get_instance_private(theme); pidgin_smiley_theme_get_description(PidginSmileyTheme *theme) PidginSmileyThemePrivate *priv = NULL; g_return_val_if_fail(PIDGIN_IS_SMILEY_THEME(theme), NULL); priv = pidgin_smiley_theme_get_instance_private(theme); pidgin_smiley_theme_get_icon(PidginSmileyTheme *theme) PidginSmileyThemePrivate *priv = NULL; g_return_val_if_fail(PIDGIN_IS_SMILEY_THEME(theme), NULL); priv = pidgin_smiley_theme_get_instance_private(theme); if (!priv->icon_pixbuf) { gchar *icon_path = g_build_filename( priv->path, priv->icon, NULL); priv->icon_pixbuf = pidgin_pixbuf_new_from_file(icon_path); return priv->icon_pixbuf; pidgin_smiley_theme_get_author(PidginSmileyTheme *theme) PidginSmileyThemePrivate *priv = NULL; g_return_val_if_fail(PIDGIN_IS_SMILEY_THEME(theme), NULL); priv = pidgin_smiley_theme_get_instance_private(theme); pidgin_smiley_theme_for_conv(PurpleConversation *conv) PurpleAccount *acc = NULL; PurpleSmileyTheme *theme; const gchar *proto_name = NULL; theme = purple_smiley_theme_get_current(); acc = purple_conversation_get_account(conv); proto_name = purple_account_get_protocol_name(acc); return purple_smiley_theme_get_smileys(theme, (gpointer)proto_name); pidgin_smiley_theme_activate_impl(PurpleSmileyTheme *theme) PidginSmileyThemePrivate *priv = pidgin_smiley_theme_get_instance_private( PIDGIN_SMILEY_THEME(theme)); PidginSmileyThemeIndex *index; if (priv->smiley_lists_map) priv->smiley_lists_map = smap = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, g_object_unref); index = pidgin_smiley_theme_index_parse(priv->path, TRUE); for (it = index->protocols; it; it = g_list_next(it)) { PidginSmileyThemeIndexProtocol *proto_idx = it->data; PurpleSmileyList *proto_smileys; proto_smileys = g_hash_table_lookup(smap, proto_idx->name); proto_smileys = purple_smiley_list_new(); g_hash_table_insert(smap, g_strdup(proto_idx->name), proto_smileys); for (it2 = proto_idx->smileys; it2; it2 = g_list_next(it2)) { PidginSmileyThemeIndexSmiley *smiley_idx = it2->data; smiley_path = g_build_filename( priv->path, smiley_idx->file, NULL); if (!g_file_test(smiley_path, G_FILE_TEST_EXISTS)) { purple_debug_warning("gtksmiley-theme", "Smiley %s is missing", smiley_path); for (it3 = smiley_idx->shortcuts; it3; gchar *shortcut = it3->data; smiley = purple_smiley_new( g_object_set_data(G_OBJECT(smiley), GINT_TO_POINTER(smiley_idx->hidden)); purple_smiley_list_add(proto_smileys, smiley); pidgin_smiley_theme_index_free(index); static PurpleSmileyList * pidgin_smiley_theme_get_smileys_impl(PurpleSmileyTheme *theme, gpointer ui_data) PidginSmileyThemePrivate *priv = pidgin_smiley_theme_get_instance_private( PIDGIN_SMILEY_THEME(theme)); PurpleSmileyList *smileys = NULL; pidgin_smiley_theme_activate_impl(theme); smileys = g_hash_table_lookup(priv->smiley_lists_map, ui_data); return g_hash_table_lookup(priv->smiley_lists_map, "default"); pidgin_smiley_theme_get_all(void) pidgin_smiley_theme_probe(); _pidgin_smiley_theme_init(void) const gchar *user_smileys_dir; probe_dirs = g_new0(gchar*, 3); probe_dirs[0] = g_build_filename( PURPLE_DATADIR, "pixmaps", "pidgin", "emotes", NULL); user_smileys_dir = probe_dirs[1] = g_build_filename(purple_data_dir(), "smileys", NULL); if (!g_file_test(user_smileys_dir, G_FILE_TEST_IS_DIR)) { if (g_mkdir(user_smileys_dir, S_IRUSR | S_IWUSR | S_IXUSR) == 0) { purple_debug_error("gtksmiley-theme", "Failed to create user smileys dir"); /* setting theme by name (copy-paste from gtkprefs) */ pidgin_smiley_theme_probe(); theme_name = purple_prefs_get_string( PIDGIN_PREFS_ROOT "/smileys/theme"); for (it = smiley_themes; it; it = g_list_next(it)) { PidginSmileyTheme *theme = it->data; if (g_strcmp0(pidgin_smiley_theme_get_name(theme), theme_name)) purple_smiley_theme_set_current(PURPLE_SMILEY_THEME(theme)); _pidgin_smiley_theme_uninit(void) /******************************************************************************* ******************************************************************************/ pidgin_smiley_theme_finalize(GObject *obj) PidginSmileyThemePrivate *priv = pidgin_smiley_theme_get_instance_private( PIDGIN_SMILEY_THEME(obj)); g_object_unref(priv->icon_pixbuf); if (priv->smiley_lists_map) g_hash_table_destroy(priv->smiley_lists_map); G_OBJECT_CLASS(pidgin_smiley_theme_parent_class)->finalize(obj); pidgin_smiley_theme_class_init(PidginSmileyThemeClass *klass) GObjectClass *gobj_class = G_OBJECT_CLASS(klass); PurpleSmileyThemeClass *pst_class = PURPLE_SMILEY_THEME_CLASS(klass); gobj_class->finalize = pidgin_smiley_theme_finalize; pst_class->get_smileys = pidgin_smiley_theme_get_smileys_impl; pst_class->activate = pidgin_smiley_theme_activate_impl; pidgin_smiley_theme_init(PidginSmileyTheme *theme)