/dev/tomkiewicz/new-smileys: 45bde03f86a6: Custom smileys: simpl...

Tomasz Wasilczyk twasilczyk at pidgin.im
Tue Apr 1 20:37:43 EDT 2014


Changeset: 45bde03f86a671d0f7ee6e3df2725effa58f7e03
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-04-02 02:37 +0200
Branch:	 default
URL: https://hg.pidgin.im/dev/tomkiewicz/new-smileys/rev/45bde03f86a6

Description:

Custom smileys: simplify storage implementation

diffstat:

 libpurple/smiley-custom.c  |  43 +++++++++++++++----------------------------
 libpurple/smiley-custom.h  |   9 +++------
 libpurple/smiley-list.c    |  11 +++++++++++
 libpurple/smiley-list.h    |   4 ++++
 libpurple/smiley-parser.c  |   5 ++++-
 libpurple/trie.c           |  11 +++++++++++
 libpurple/trie.h           |   3 +++
 pidgin/gtkwebviewtoolbar.c |  18 +++++++++++++-----
 8 files changed, 64 insertions(+), 40 deletions(-)

diffs (282 lines):

diff --git a/libpurple/smiley-custom.c b/libpurple/smiley-custom.c
--- a/libpurple/smiley-custom.c
+++ b/libpurple/smiley-custom.c
@@ -22,7 +22,6 @@
 #include "smiley-custom.h"
 
 #include "debug.h"
-#include "smiley-list.h"
 
 #include <glib/gstdio.h>
 
@@ -33,7 +32,6 @@ static gchar *smileys_dir;
 static gchar *smileys_index;
 
 static PurpleSmileyList *smileys_list;
-static GHashTable *smileys_table;
 static gboolean disable_write = FALSE;
 
 /*******************************************************************************
@@ -82,9 +80,8 @@ purple_smiley_custom_load(void)
 			smiley = purple_smiley_new(shortcut, file_path);
 			g_free(file_path);
 
-			g_hash_table_insert(smileys_table,
-				g_strdup(shortcut), smiley);
 			purple_smiley_list_add(smileys_list, smiley);
+			g_object_unref(smiley);
 		}
 
 		smiley_node = purple_xmlnode_get_next_twin(smiley_node);
@@ -100,8 +97,7 @@ static PurpleXmlNode *
 smileys_to_xmlnode(void)
 {
 	PurpleXmlNode *root_node, *profile_node, *smileyset_node;
-	GHashTableIter it;
-	gpointer value;
+	GList *smileys, *it;
 
 	root_node = purple_xmlnode_new("smileys");
 	purple_xmlnode_set_attrib(root_node, "version", "1.0");
@@ -113,9 +109,10 @@ smileys_to_xmlnode(void)
 	smileyset_node = purple_xmlnode_new("smiley_set");
 	purple_xmlnode_insert_child(profile_node, smileyset_node);
 
-	g_hash_table_iter_init(&it, smileys_table);
-	while (g_hash_table_iter_next(&it, NULL, &value)) {
-		PurpleSmiley *smiley = PURPLE_SMILEY(value);
+	smileys = purple_smiley_list_get_unique(smileys_list);
+
+	for (it = smileys; it; it = g_list_next(it)) {
+		PurpleSmiley *smiley = PURPLE_SMILEY(it->data);
 		PurpleXmlNode *smiley_node;
 
 		smiley_node = purple_xmlnode_new("smiley");
@@ -181,7 +178,8 @@ purple_smiley_custom_add(PurpleStoredIma
 
 	g_return_val_if_fail(PURPLE_IS_STORED_IMAGE(img), NULL);
 
-	existing_smiley = g_hash_table_lookup(smileys_table, shortcut);
+	existing_smiley = purple_smiley_list_get_by_shortcut(
+		smileys_list, shortcut);
 
 	g_object_ref(img);
 
@@ -216,9 +214,9 @@ purple_smiley_custom_add(PurpleStoredIma
 
 	smiley = purple_smiley_new(shortcut, file_path);
 	g_free(file_path);
+	purple_smiley_list_add(smileys_list, smiley);
+	g_object_unref(smiley);
 
-	g_hash_table_insert(smileys_table, g_strdup(shortcut), smiley);
-	purple_smiley_list_add(smileys_list, smiley);
 	purple_smiley_custom_save();
 
 	return smiley;
@@ -233,7 +231,8 @@ purple_smiley_custom_remove(PurpleSmiley
 	g_return_if_fail(PURPLE_IS_SMILEY(smiley));
 
 	smiley_shortcut = purple_smiley_get_shortcut(smiley);
-	existing_smiley = g_hash_table_lookup(smileys_table, smiley_shortcut);
+	existing_smiley = purple_smiley_list_get_by_shortcut(
+		smileys_list, smiley_shortcut);
 	if (existing_smiley == NULL)
 		return;
 	if (existing_smiley != smiley) {
@@ -243,22 +242,13 @@ purple_smiley_custom_remove(PurpleSmiley
 
 	g_unlink(purple_smiley_get_path(smiley));
 	purple_smiley_list_remove(smileys_list, smiley);
-	g_hash_table_remove(smileys_table, smiley_shortcut);
 	purple_smiley_custom_save();
 }
 
-GList *
-purple_smiley_custom_get_all(void)
+PurpleSmileyList *
+purple_smiley_custom_get_list(void)
 {
-	return g_hash_table_get_values(smileys_table);
-}
-
-PurpleTrie *
-purple_smiley_custom_get_trie(void)
-{
-	if (g_hash_table_size(smileys_table) == 0)
-		return NULL;
-	return purple_smiley_list_get_trie(smileys_list);
+	return smileys_list;
 }
 
 
@@ -274,8 +264,6 @@ purple_smiley_custom_init(void)
 	smileys_index = g_build_filename(purple_user_dir(),
 		SMILEYS_INDEX_FILE, NULL);
 	smileys_list = purple_smiley_list_new();
-	smileys_table = g_hash_table_new_full(g_str_hash, g_str_equal,
-		g_free, g_object_unref);
 
 	purple_smiley_custom_load();
 }
@@ -286,5 +274,4 @@ purple_smiley_custom_uninit(void)
 	g_free(smileys_dir);
 	g_free(smileys_index);
 	g_object_unref(smileys_list);
-	g_hash_table_destroy(smileys_table);
 }
diff --git a/libpurple/smiley-custom.h b/libpurple/smiley-custom.h
--- a/libpurple/smiley-custom.h
+++ b/libpurple/smiley-custom.h
@@ -23,7 +23,7 @@
 #define _PURPLE_SMILEY_CUSTOM_H_
 
 #include "smiley.h"
-#include "trie.h"
+#include "smiley-list.h"
 
 PurpleSmiley *
 purple_smiley_custom_add(PurpleStoredImage *img, const gchar *shortcut);
@@ -31,11 +31,8 @@ purple_smiley_custom_add(PurpleStoredIma
 void
 purple_smiley_custom_remove(PurpleSmiley *smiley);
 
-GList *
-purple_smiley_custom_get_all(void);
-
-PurpleTrie *
-purple_smiley_custom_get_trie(void);
+PurpleSmileyList *
+purple_smiley_custom_get_list(void);
 
 void
 purple_smiley_custom_init(void);
diff --git a/libpurple/smiley-list.c b/libpurple/smiley-list.c
--- a/libpurple/smiley-list.c
+++ b/libpurple/smiley-list.c
@@ -155,6 +155,17 @@ purple_smiley_list_remove(PurpleSmileyLi
 	g_object_unref(smiley);
 }
 
+PurpleSmiley *
+purple_smiley_list_get_by_shortcut(PurpleSmileyList *list,
+	const gchar *shortcut)
+{
+	PurpleSmileyListPrivate *priv = PURPLE_SMILEY_LIST_GET_PRIVATE(list);
+
+	g_return_val_if_fail(priv != NULL, NULL);
+
+	return g_hash_table_lookup(priv->unique_map, shortcut);
+}
+
 PurpleTrie *
 purple_smiley_list_get_trie(PurpleSmileyList *list)
 {
diff --git a/libpurple/smiley-list.h b/libpurple/smiley-list.h
--- a/libpurple/smiley-list.h
+++ b/libpurple/smiley-list.h
@@ -78,6 +78,10 @@ purple_smiley_list_add(PurpleSmileyList 
 void
 purple_smiley_list_remove(PurpleSmileyList *list, PurpleSmiley *smiley);
 
+PurpleSmiley *
+purple_smiley_list_get_by_shortcut(PurpleSmileyList *list,
+	const gchar *shortcut);
+
 PurpleTrie *
 purple_smiley_list_get_trie(PurpleSmileyList *list);
 
diff --git a/libpurple/smiley-parser.c b/libpurple/smiley-parser.c
--- a/libpurple/smiley-parser.c
+++ b/libpurple/smiley-parser.c
@@ -69,7 +69,10 @@ purple_smiley_parse(const gchar *message
 	if (message == NULL || message[0] == '\0')
 		return g_strdup(message);
 
-	custom_trie = purple_smiley_custom_get_trie();
+	custom_trie = purple_smiley_list_get_trie(
+		purple_smiley_custom_get_list());
+	if (purple_trie_get_size(custom_trie) == 0)
+		custom_trie = NULL;
 
 	theme = purple_smiley_theme_get_current();
 	if (theme != NULL)
diff --git a/libpurple/trie.c b/libpurple/trie.c
--- a/libpurple/trie.c
+++ b/libpurple/trie.c
@@ -625,6 +625,17 @@ purple_trie_remove(PurpleTrie *trie, con
 	purple_memory_pool_free(priv->records_obj_mempool, it);
 }
 
+guint
+purple_trie_get_size(PurpleTrie *trie)
+{
+	PurpleTriePrivate *priv = PURPLE_TRIE_GET_PRIVATE(trie);
+
+	g_return_val_if_fail(priv != NULL, 0);
+
+	return g_hash_table_size(priv->records_map);
+}
+
+
 /*******************************************************************************
  * API implementation
  ******************************************************************************/
diff --git a/libpurple/trie.h b/libpurple/trie.h
--- a/libpurple/trie.h
+++ b/libpurple/trie.h
@@ -135,6 +135,9 @@ purple_trie_add(PurpleTrie *trie, const 
 void
 purple_trie_remove(PurpleTrie *trie, const gchar *word);
 
+guint
+purple_trie_get_size(PurpleTrie *trie);
+
 /**
  * purple_trie_replace:
  * @trie: The trie.
diff --git a/pidgin/gtkwebviewtoolbar.c b/pidgin/gtkwebviewtoolbar.c
--- a/pidgin/gtkwebviewtoolbar.c
+++ b/pidgin/gtkwebviewtoolbar.c
@@ -821,7 +821,7 @@ static void
 insert_smiley_cb(GtkAction *smiley, PidginWebViewToolbar *toolbar)
 {
 	PidginWebViewToolbarPriv *priv = PIDGIN_WEBVIEWTOOLBAR_GET_PRIVATE(toolbar);
-	PurpleSmileyList *smileys_from_theme;
+	PurpleSmileyList *smileys_from_theme, *smileys_from_custom = NULL;
 	GList *theme_smileys, *custom_smileys = NULL;
 	PidginWebViewButtons webview_format;
 
@@ -845,8 +845,11 @@ insert_smiley_cb(GtkAction *smiley, Pidg
 	/* TODO: remove hidden */
 
 	supports_custom = (webview_format & PIDGIN_WEBVIEW_CUSTOM_SMILEY);
-	if (supports_custom)
-		custom_smileys = purple_smiley_custom_get_all();
+	if (supports_custom) {
+		smileys_from_custom = purple_smiley_custom_get_list();
+		custom_smileys =
+			purple_smiley_list_get_unique(smileys_from_custom);
+	}
 
 	dialog = pidgin_create_dialog(_("Smile!"), 0, "smiley_dialog", FALSE);
 	gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
@@ -892,11 +895,16 @@ insert_smiley_cb(GtkAction *smiley, Pidg
 		max_line_width = MAX(button_width, max_line_width / num_lines);
 
 		/* pack buttons of the list */
-		add_smiley_list(toolbar, smiley_table, theme_smileys, max_line_width);
-		if (supports_custom) {
+		if (theme_smileys) {
+			add_smiley_list(toolbar, smiley_table,
+				theme_smileys, max_line_width);
+		}
+		if (theme_smileys && custom_smileys) {
 			gtk_box_pack_start(GTK_BOX(smiley_table),
 				gtk_separator_new(GTK_ORIENTATION_HORIZONTAL),
 				TRUE, FALSE, 0);
+		}
+		if (custom_smileys) {
 			add_smiley_list(toolbar, smiley_table,
 				custom_smileys, max_line_width);
 		}



More information about the Commits mailing list