Mon, 31 Mar 2014 15:33:19 +0200
PidginSmileyTheme: probe themes
--- a/pidgin/gtksmiley-theme.c Mon Mar 31 13:26:10 2014 +0200 +++ b/pidgin/gtksmiley-theme.c Mon Mar 31 15:33:19 2014 +0200 @@ -21,16 +21,100 @@ #include "gtksmiley-theme.h" +#include <glib/gstdio.h> + #define PIDGIN_SMILEY_THEME_GET_PRIVATE(obj) \ (G_TYPE_INSTANCE_GET_PRIVATE((obj), PIDGIN_TYPE_SMILEY_THEME, \ - PurpleSmileyThemePrivate)) + PidginSmileyThemePrivate)) typedef struct { -} PurpleSmileyThemePrivate; + gchar *path; +} PidginSmileyThemePrivate; static GObjectClass *parent_class; +static gchar **probe_dirs; +static GList *smiley_themes = NULL; + +/******************************************************************************* + * Theme loading + ******************************************************************************/ + +static void +pidgin_smiley_theme_load(const gchar *theme_path) +{ + PidginSmileyTheme *theme; + PidginSmileyThemePrivate *priv; + GList *it; + gchar *index_path; + + /* it's not super-efficient, but we don't expect huge amount of + * installed themes */ + for (it = smiley_themes; it; it = g_list_next(it)) { + PidginSmileyThemePrivate *priv = + PIDGIN_SMILEY_THEME_GET_PRIVATE(it->data); + + /* theme is already loaded */ + if (g_strcmp0(priv->path, theme_path) == 0) + return; + } + + index_path = g_build_filename(theme_path, "theme", NULL); + + theme = g_object_new(PIDGIN_TYPE_SMILEY_THEME, NULL); + priv = PIDGIN_SMILEY_THEME_GET_PRIVATE(theme); + + priv->path = g_strdup(theme_path); + + /* TODO: parse index_path */ + + g_free(index_path); +} + +static void +pidgin_smiley_theme_probe(void) +{ + GList *it, *next; + int i; + + /* remove non-existing themes */ + for (it = smiley_themes; it; it = next) { + PidginSmileyTheme *theme = it->data; + PidginSmileyThemePrivate *priv = + PIDGIN_SMILEY_THEME_GET_PRIVATE(theme); + + next = g_list_next(it); + + if (g_file_test(priv->path, G_FILE_TEST_EXISTS)) + continue; + smiley_themes = g_list_remove_link(smiley_themes, it); + g_object_unref(theme); + } + + /* scan for themes */ + for (i = 0; probe_dirs[i]; i++) { + GDir *dir = g_dir_open(probe_dirs[i], 0, NULL); + const gchar *theme_dir_name; + + if (!dir) + continue; + + while ((theme_dir_name = g_dir_read_name(dir))) { + gchar *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); + + g_free(theme_path); + } + + g_dir_close(dir); + } +} + + /******************************************************************************* * API implementation ******************************************************************************/ @@ -44,8 +128,31 @@ void pidgin_smiley_theme_init(void) { + const gchar *user_smileys_dir; + + probe_dirs = g_new0(gchar*, 3); + probe_dirs[0] = g_build_filename( + DATADIR, "pixmaps", "pidgin", "emotes", NULL); + user_smileys_dir = probe_dirs[1] = g_build_filename( + purple_user_dir(), "smileys", NULL); + + if (!g_file_test(user_smileys_dir, G_FILE_TEST_IS_DIR)) + g_mkdir(user_smileys_dir, S_IRUSR | S_IWUSR | S_IXUSR); } +void +pidgin_smiley_theme_uninit(void) +{ + g_strfreev(probe_dirs); +} + +GList * +pidgin_smiley_theme_get_all(void) +{ + pidgin_smiley_theme_probe(); + + return NULL; +} /******************************************************************************* * Object stuff @@ -54,6 +161,10 @@ static void pidgin_smiley_theme_finalize(GObject *obj) { + PidginSmileyThemePrivate *priv = PIDGIN_SMILEY_THEME_GET_PRIVATE(obj); + + g_free(priv->path); + G_OBJECT_CLASS(parent_class)->finalize(obj); } @@ -65,7 +176,7 @@ parent_class = g_type_class_peek_parent(klass); - g_type_class_add_private(klass, sizeof(PurpleSmileyThemePrivate)); + g_type_class_add_private(klass, sizeof(PidginSmileyThemePrivate)); gobj_class->finalize = pidgin_smiley_theme_finalize;
--- a/pidgin/gtksmiley-theme.h Mon Mar 31 13:26:10 2014 +0200 +++ b/pidgin/gtksmiley-theme.h Mon Mar 31 15:33:19 2014 +0200 @@ -71,6 +71,12 @@ void pidgin_smiley_theme_init(void); +void +pidgin_smiley_theme_uninit(void); + +GList * +pidgin_smiley_theme_get_all(void); + G_END_DECLS #endif /* _PIDGIN_SMILEY_THEME_H_ */
--- a/pidgin/libpidgin.c Mon Mar 31 13:26:10 2014 +0200 +++ b/pidgin/libpidgin.c Mon Mar 31 15:33:19 2014 +0200 @@ -307,6 +307,7 @@ /* Uninit */ pidgin_utils_uninit(); pidgin_notify_uninit(); + pidgin_smiley_theme_uninit(); pidgin_smileys_uninit(); pidgin_conversations_uninit(); pidgin_status_uninit();