/dev/tomkiewicz/new-smileys: 6cd9aee7f276: Smiley manager: edit,...

Tomasz Wasilczyk twasilczyk at pidgin.im
Wed Apr 2 15:03:33 EDT 2014


Changeset: 6cd9aee7f276e6746e621a56ad8e4fbb2814a088
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-04-02 21:03 +0200
Branch:	 default
URL: https://hg.pidgin.im/dev/tomkiewicz/new-smileys/rev/6cd9aee7f276

Description:

Smiley manager: edit, delete

diffstat:

 libpurple/smiley-custom.c  |  24 +++++++++++++----
 libpurple/smiley-list.c    |  62 +++++++++++++++++++++++++++++++++++++++------
 libpurple/smiley-list.h    |   3 ++
 pidgin/gtksmiley-manager.c |  56 ++--------------------------------------
 pidgin/gtkwebviewtoolbar.c |   4 +-
 5 files changed, 80 insertions(+), 69 deletions(-)

diffs (truncated from 326 to 300 lines):

diff --git a/libpurple/smiley-custom.c b/libpurple/smiley-custom.c
--- a/libpurple/smiley-custom.c
+++ b/libpurple/smiley-custom.c
@@ -24,6 +24,7 @@
 #include "debug.h"
 
 #include <glib/gstdio.h>
+#include <errno.h>
 
 #define SMILEYS_DEFAULT_FOLDER "custom_smiley"
 #define SMILEYS_INDEX_FILE "smileys.xml"
@@ -76,11 +77,15 @@ purple_smiley_custom_load(void)
 			gchar *file_path = g_build_filename(
 				smileys_dir, file_name, NULL);
 
-			got_smileys++;
 			smiley = purple_smiley_new(shortcut, file_path);
 			g_free(file_path);
 
-			purple_smiley_list_add(smileys_list, smiley);
+			if (purple_smiley_list_add(smileys_list, smiley)) {
+				got_smileys++;
+			} else {
+				purple_debug_warning("smiley-custom",
+					"Couldn't add '%s' smiley", shortcut);
+			}
 			g_object_unref(smiley);
 		}
 
@@ -109,7 +114,7 @@ smileys_to_xmlnode(void)
 	smileyset_node = purple_xmlnode_new("smiley_set");
 	purple_xmlnode_insert_child(profile_node, smileyset_node);
 
-	smileys = purple_smiley_list_get_unique(smileys_list);
+	smileys = purple_smiley_list_get_all(smileys_list);
 
 	for (it = smileys; it; it = g_list_next(it)) {
 		PurpleSmiley *smiley = PURPLE_SMILEY(it->data);
@@ -176,6 +181,7 @@ purple_smiley_custom_add(PurpleStoredIma
 	gchar file_name[256];
 	const gchar *file_ext;
 	GError *error = NULL;
+	gboolean succ;
 
 	g_return_val_if_fail(PURPLE_IS_STORED_IMAGE(img), NULL);
 
@@ -215,9 +221,12 @@ purple_smiley_custom_add(PurpleStoredIma
 
 	smiley = purple_smiley_new(shortcut, file_path);
 	g_free(file_path);
-	purple_smiley_list_add(smileys_list, smiley);
+	succ = purple_smiley_list_add(smileys_list, smiley);
 	g_object_unref(smiley);
 
+	if (!succ)
+		purple_debug_error("smiley-custom", "Failed adding a smiley");
+
 	purple_smiley_custom_save();
 
 	return smiley;
@@ -281,15 +290,18 @@ purple_smiley_custom_get_list(void)
 void
 purple_smiley_custom_init(void)
 {
+	gint ret;
+
 	smileys_dir = g_build_filename(purple_user_dir(),
 		SMILEYS_DEFAULT_FOLDER, NULL);
 	smileys_index = g_build_filename(purple_user_dir(),
 		SMILEYS_INDEX_FILE, NULL);
 	smileys_list = purple_smiley_list_new();
 
-	if (g_mkdir(smileys_dir, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
+	ret = g_mkdir(smileys_dir, S_IRUSR | S_IWUSR | S_IXUSR);
+	if (ret != 0 && errno != EEXIST) {
 		purple_debug_error("smiley-custom", "Failed creating custom "
-			"smileys directory");
+			"smileys directory: %s (%d)", g_strerror(errno), errno);
 	}
 
 	purple_smiley_custom_load();
diff --git a/libpurple/smiley-list.c b/libpurple/smiley-list.c
--- a/libpurple/smiley-list.c
+++ b/libpurple/smiley-list.c
@@ -34,7 +34,8 @@ typedef struct {
 	GList *smileys;
 	GList *smileys_end;
 	PurpleTrie *trie;
-	GHashTable *unique_map;
+	GHashTable *path_map;
+	GHashTable *shortcut_map;
 } PurpleSmileyListPrivate;
 
 enum
@@ -108,10 +109,15 @@ purple_smiley_list_add(PurpleSmileyList 
 	}
 
 	shortcut = purple_smiley_get_shortcut(smiley);
+
+	if (g_hash_table_lookup(priv->shortcut_map, shortcut) != NULL)
+		return FALSE;
+
 	if (purple_smiley_parse_escape())
 		shortcut = tmp = g_markup_escape_text(shortcut, -1);
 	succ = purple_trie_add(priv->trie, shortcut, smiley);
 	g_free(tmp);
+	shortcut = purple_smiley_get_shortcut(smiley);
 	if (!succ)
 		return FALSE;
 
@@ -122,10 +128,11 @@ purple_smiley_list_add(PurpleSmileyList 
 		priv->smileys_end);
 
 	smiley_path = purple_smiley_get_path(smiley);
-	if (g_hash_table_lookup(priv->unique_map, smiley_path) == NULL) {
-		g_hash_table_insert(priv->unique_map,
+	if (g_hash_table_lookup(priv->path_map, smiley_path) == NULL) {
+		g_hash_table_insert(priv->path_map,
 			g_strdup(smiley_path), smiley);
 	}
+	g_hash_table_insert(priv->shortcut_map, g_strdup(shortcut), smiley);
 
 	return TRUE;
 }
@@ -134,7 +141,9 @@ void
 purple_smiley_list_remove(PurpleSmileyList *list, PurpleSmiley *smiley)
 {
 	PurpleSmileyListPrivate *priv = PURPLE_SMILEY_LIST_GET_PRIVATE(list);
-	GList *list_elem;
+	GList *list_elem, *it;
+	const gchar *shortcut, *path;
+	gchar *tmp;
 
 	g_return_if_fail(priv != NULL);
 	g_return_if_fail(PURPLE_IS_SMILEY(smiley));
@@ -147,9 +156,31 @@ purple_smiley_list_remove(PurpleSmileyLi
 	list_elem = g_object_get_data(G_OBJECT(smiley),
 		"purple-smiley-list-elem");
 
-	purple_trie_remove(priv->trie, purple_smiley_get_shortcut(smiley));
+	shortcut = purple_smiley_get_shortcut(smiley);
+	path = purple_smiley_get_path(smiley);
+
+	g_hash_table_remove(priv->shortcut_map, shortcut);
+	g_hash_table_remove(priv->path_map, path);
+
+	if (purple_smiley_parse_escape())
+		shortcut = tmp = g_markup_escape_text(shortcut, -1);
+	purple_trie_remove(priv->trie, shortcut);
+	g_free(tmp);
+	shortcut = purple_smiley_get_shortcut(smiley);
+
 	_list_remove_link2(&priv->smileys, &priv->smileys_end, list_elem);
 
+	/* re-add entry to path_map if smiley was not unique */
+	for (it = priv->smileys; it; it = g_list_next(it)) {
+		PurpleSmiley *smiley = it->data;
+
+		if (g_strcmp0(purple_smiley_get_path(smiley), path) == 0) {
+			g_hash_table_insert(priv->path_map,
+				g_strdup(path), smiley);
+			break;
+		}
+	}
+
 	g_object_set_data(G_OBJECT(smiley), "purple-smiley-list", NULL);
 	g_object_set_data(G_OBJECT(smiley), "purple-smiley-list-elem", NULL);
 	g_object_unref(smiley);
@@ -163,7 +194,7 @@ purple_smiley_list_get_by_shortcut(Purpl
 
 	g_return_val_if_fail(priv != NULL, NULL);
 
-	return g_hash_table_lookup(priv->unique_map, shortcut);
+	return g_hash_table_lookup(priv->shortcut_map, shortcut);
 }
 
 PurpleTrie *
@@ -183,7 +214,17 @@ purple_smiley_list_get_unique(PurpleSmil
 
 	g_return_val_if_fail(priv != NULL, NULL);
 
-	return g_hash_table_get_values(priv->unique_map);
+	return g_hash_table_get_values(priv->path_map);
+}
+
+GList *
+purple_smiley_list_get_all(PurpleSmileyList *list)
+{
+	PurpleSmileyListPrivate *priv = PURPLE_SMILEY_LIST_GET_PRIVATE(list);
+
+	g_return_val_if_fail(priv != NULL, NULL);
+
+	return g_hash_table_get_values(priv->shortcut_map);
 }
 
 
@@ -198,7 +239,9 @@ purple_smiley_list_init(GTypeInstance *i
 	PurpleSmileyListPrivate *priv = PURPLE_SMILEY_LIST_GET_PRIVATE(sl);
 
 	priv->trie = purple_trie_new();
-	priv->unique_map = g_hash_table_new_full(g_str_hash, g_str_equal,
+	priv->path_map = g_hash_table_new_full(g_str_hash, g_str_equal,
+		g_free, NULL);
+	priv->shortcut_map = g_hash_table_new_full(g_str_hash, g_str_equal,
 		g_free, NULL);
 
 	PURPLE_DBUS_REGISTER_POINTER(sl, PurpleSmileyList);
@@ -212,7 +255,8 @@ purple_smiley_list_finalize(GObject *obj
 	GList *it;
 
 	g_object_unref(priv->trie);
-	g_hash_table_destroy(priv->unique_map);
+	g_hash_table_destroy(priv->path_map);
+	g_hash_table_destroy(priv->shortcut_map);
 
 	for (it = priv->smileys; it; it = g_list_next(it)) {
 		PurpleSmiley *smiley = it->data;
diff --git a/libpurple/smiley-list.h b/libpurple/smiley-list.h
--- a/libpurple/smiley-list.h
+++ b/libpurple/smiley-list.h
@@ -88,6 +88,9 @@ purple_smiley_list_get_trie(PurpleSmiley
 GList *
 purple_smiley_list_get_unique(PurpleSmileyList *list_);
 
+GList *
+purple_smiley_list_get_all(PurpleSmileyList *list_);
+
 G_END_DECLS
 
 #endif /* _PURPLE_SMILEY_H_ */
diff --git a/pidgin/gtksmiley-manager.c b/pidgin/gtksmiley-manager.c
--- a/pidgin/gtksmiley-manager.c
+++ b/pidgin/gtksmiley-manager.c
@@ -443,54 +443,6 @@ pidgin_smiley_editor_set_data(PidginSmil
 }
 #endif
 
-/******************************************************************************
- * Delete smiley
- *****************************************************************************/
-#if 0
-static void delete_foreach(GtkTreeModel *model, GtkTreePath *path,
-		GtkTreeIter *iter, gpointer data)
-{
-	PurpleSmiley *smiley = NULL;
-
-	gtk_tree_model_get(model, iter,
-			SMILEY, &smiley,
-			-1);
-
-	if(smiley != NULL) {
-		g_object_unref(G_OBJECT(smiley));
-		pidgin_smiley_del_from_list(smiley);
-		purple_smiley_delete(smiley);
-	}
-}
-#endif
-
-#if 0
-static void append_to_list(GtkTreeModel *model, GtkTreePath *path,
-		GtkTreeIter *iter, gpointer data)
-{
-	GList **list = data;
-	*list = g_list_prepend(*list, gtk_tree_path_copy(path));
-}
-
-static void smiley_delete(SmileyManager *dialog)
-{
-	GtkTreeSelection *selection;
-	GList *list = NULL;
-
-	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview));
-	gtk_tree_selection_selected_foreach(selection, delete_foreach, dialog);
-	gtk_tree_selection_selected_foreach(selection, append_to_list, &list);
-
-	while (list) {
-		GtkTreeIter iter;
-		if (gtk_tree_model_get_iter(GTK_TREE_MODEL(dialog->model), &iter, list->data))
-			gtk_list_store_remove(GTK_LIST_STORE(dialog->model), &iter);
-		gtk_tree_path_free(list->data);
-		list = g_list_delete_link(list, list);
-	}
-}
-#endif
-
 #if 0
 static void
 smiley_got_url(PurpleHttpConnection *http_conn, PurpleHttpResponse *response,
@@ -629,7 +581,7 @@ pidgin_smiley_manager_list_fill(SmileyMa
 	GList *custom_smileys, *it;
 	gtk_list_store_clear(manager->model);
 
-	custom_smileys = purple_smiley_list_get_unique(
+	custom_smileys = purple_smiley_list_get_all(
 		purple_smiley_custom_get_list());
 
 	for (it = custom_smileys; it; it = g_list_next(it)) {
@@ -745,11 +697,11 @@ smiley_manager_select_cb(GtkWidget *widg
 		case GTK_RESPONSE_YES:
 			pidgin_smiley_edit(manager, NULL);



More information about the Commits mailing list