/pidgin/main: 7d81320ce705: Finch: add cache to tinyurl plugin

Tomasz Wasilczyk twasilczyk at pidgin.im
Sat Apr 26 18:47:49 EDT 2014


Changeset: 7d81320ce70545aeae9afbd3461314f6d90a6e1c
Author:	 Tomasz Wasilczyk <twasilczyk at pidgin.im>
Date:	 2014-04-27 00:47 +0200
Branch:	 default
URL: https://hg.pidgin.im/pidgin/main/rev/7d81320ce705

Description:

Finch: add cache to tinyurl plugin

diffstat:

 finch/plugins/gnttinyurl.c |  79 ++++++++++++++++++++++++++++++++++++---------
 1 files changed, 63 insertions(+), 16 deletions(-)

diffs (169 lines):

diff --git a/finch/plugins/gnttinyurl.c b/finch/plugins/gnttinyurl.c
--- a/finch/plugins/gnttinyurl.c
+++ b/finch/plugins/gnttinyurl.c
@@ -48,9 +48,11 @@
 #include <gntwindow.h>
 
 static int tag_num = 0;
+static GHashTable *tinyurl_cache = NULL;
 
 typedef struct
 {
+	gchar *original_url;
 	PurpleConversation *conv;
 	gchar *tag;
 	int num;
@@ -213,10 +215,13 @@ static void url_fetched(PurpleHttpConnec
 	GList *convs = purple_conversations_get_all();
 	const gchar *url;
 
-	if (purple_http_response_is_successful(response))
+	if (purple_http_response_is_successful(response)) {
 		url = purple_http_response_get_data(response, NULL);
-	else
+		g_hash_table_insert(tinyurl_cache, data->original_url, g_strdup(url));
+	} else {
 		url = _("Error while querying TinyURL");
+		g_free(data->original_url);
+	}
 
 	/* ensure the conversation still exists */
 	for (; convs; convs = convs->next) {
@@ -314,23 +319,38 @@ process_urls(PurpleConversation *conv, G
 	FinchConv *fconv = FINCH_CONV(conv);
 	GntTextView *tv = GNT_TEXT_VIEW(fconv->tv);
 
-	for (iter = urls, c = 0; iter; iter = iter->next) {
+	for (iter = urls, c = 1; iter; iter = iter->next, c++) {
 		int i;
 		CbInfo *cbdata;
-		gchar *url, *str, *tmp;
+		gchar *url, *str;
+		gchar *original_url;
+		const gchar *tiny_url;
+
+		i = gnt_text_view_get_lines_below(tv);
+
+		original_url = purple_unescape_html((char *)iter->data);
+		tiny_url = g_hash_table_lookup(tinyurl_cache, original_url);
+		if (tiny_url) {
+			gchar *str = g_strdup_printf("\n[%d] %s", c, tiny_url);
+
+			g_free(original_url);
+			gnt_text_view_append_text_with_flags(tv, str, GNT_TEXT_FLAG_DIM);
+			if (i == 0)
+				gnt_text_view_scroll(tv, 0);
+			g_free(str);
+			continue;
+		}
 		cbdata = g_new(CbInfo, 1);
-		cbdata->num = ++c;
+		cbdata->num = c;
+		cbdata->original_url = original_url;
 		cbdata->tag = g_strdup_printf("%s%d", "tiny_", tag_num++);
 		cbdata->conv = conv;
-		tmp = purple_unescape_html((char *)iter->data);
-		if (g_ascii_strncasecmp(tmp, "http://", 7) && g_ascii_strncasecmp(tmp, "https://", 8)) {
-			url = g_strdup_printf("%shttp%%3A%%2F%%2F%s", purple_prefs_get_string(PREF_URL), purple_url_encode(tmp));
+		if (g_ascii_strncasecmp(original_url, "http://", 7) && g_ascii_strncasecmp(original_url, "https://", 8)) {
+			url = g_strdup_printf("%shttp%%3A%%2F%%2F%s", purple_prefs_get_string(PREF_URL), purple_url_encode(original_url));
 		} else {
-			url = g_strdup_printf("%s%s", purple_prefs_get_string(PREF_URL), purple_url_encode(tmp));
+			url = g_strdup_printf("%s%s", purple_prefs_get_string(PREF_URL), purple_url_encode(original_url));
 		}
-		g_free(tmp);
 		purple_http_get(NULL, url_fetched, cbdata, url);
-		i = gnt_text_view_get_lines_below(tv);
 		str = g_strdup_printf(_("\nFetching TinyURL..."));
 		gnt_text_view_append_text_with_tag((tv), str, GNT_TEXT_FLAG_DIM, cbdata->tag);
 		g_free(str);
@@ -352,22 +372,33 @@ free_conv_urls(PurpleConversation *conv)
 }
 
 static void
+tinyurl_notify_tinyuri(GntWidget *win, const gchar *url)
+{
+	gchar *message;
+	GntWidget *label = g_object_get_data(G_OBJECT(win), "info-widget");
+
+	message = g_strdup_printf(_("TinyURL for above: %s"), url);
+	gnt_label_set_text(GNT_LABEL(label), message);
+	g_free(message);
+}
+
+static void
 tinyurl_notify_fetch_cb(PurpleHttpConnection *http_conn,
 	PurpleHttpResponse *response, gpointer _win)
 {
 	GntWidget *win = _win;
-	GntWidget *label = g_object_get_data(G_OBJECT(win), "info-widget");
-	char *message;
 	const gchar *url;
+	const gchar *original_url;
 
 	if (!purple_http_response_is_successful(response))
 		return;
 
+	original_url = g_object_get_data(G_OBJECT(win), "gnttinyurl-original");
 	url = purple_http_response_get_data(response, NULL);
+	g_hash_table_insert(tinyurl_cache,
+		g_strdup(original_url), g_strdup(url));
 
-	message = g_strdup_printf(_("TinyURL for above: %s"), url);
-	gnt_label_set_text(GNT_LABEL(label), message);
-	g_free(message);
+	tinyurl_notify_tinyuri(win, url);
 
 	g_signal_handlers_disconnect_matched(G_OBJECT(win), G_SIGNAL_MATCH_FUNC,
 			0, 0, NULL,
@@ -380,6 +411,7 @@ tinyurl_notify_uri(const char *uri)
 	char *fullurl = NULL;
 	GntWidget *win;
 	PurpleHttpConnection *hc;
+	const gchar *tiny_url;
 
 	/* XXX: The following expects that finch_notify_message gets called. This
 	 * may not always happen, e.g. when another plugin sets its own
@@ -389,6 +421,12 @@ tinyurl_notify_uri(const char *uri)
 	if (!GNT_IS_WINDOW(win) || !g_object_get_data(G_OBJECT(win), "info-widget"))
 		return win;
 
+	tiny_url = g_hash_table_lookup(tinyurl_cache, uri);
+	if (tiny_url) {
+		tinyurl_notify_tinyuri(win, tiny_url);
+		return win;
+	}
+
 	if (g_ascii_strncasecmp(uri, "http://", 7) && g_ascii_strncasecmp(uri, "https://", 8)) {
 		fullurl = g_strdup_printf("%shttp%%3A%%2F%%2F%s",
 				purple_prefs_get_string(PREF_URL), purple_url_encode(uri));
@@ -397,6 +435,8 @@ tinyurl_notify_uri(const char *uri)
 				purple_url_encode(uri));
 	}
 
+	g_object_set_data_full(G_OBJECT(win), "gnttinyurl-original", g_strdup(uri), g_free);
+
 	/* Store the return value of purple_http_get and destroy that when win
 	 * is destroyed, so that the callback for purple_http_get does not try
 	 * to molest a non-existent window
@@ -416,6 +456,9 @@ plugin_load(PurplePlugin *plugin)
 	plugin->extra = ops->notify_uri;
 	ops->notify_uri = tinyurl_notify_uri;
 
+	tinyurl_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+		g_free, g_free);
+
 	purple_signal_connect(purple_conversations_get_handle(),
 			"wrote-im-msg",
 			plugin, PURPLE_CALLBACK(wrote_msg), NULL);
@@ -441,6 +484,10 @@ plugin_unload(PurplePlugin *plugin)
 	PurpleNotifyUiOps *ops = purple_notify_get_ui_ops();
 	if (ops->notify_uri == tinyurl_notify_uri)
 		ops->notify_uri = plugin->extra;
+
+	g_hash_table_destroy(tinyurl_cache);
+	tinyurl_cache = NULL;
+
 	return TRUE;
 }
 



More information about the Commits mailing list