Folder renaming support & a refactoring



Hi,

I'm planning to upload the attached patch that adds support to rename a
folder in libtinymail-camel.

The patch adds also a refactoring of the methods
tny_camel_store_account_subscribe/tny_camel_store_account_unsubscribe
due to they do almost the same operations with different parameters.

It fixes also an small leak.

Comments?

Br
Index: libtinymail-camel/tny-camel-folder.c
===================================================================
--- libtinymail-camel/tny-camel-folder.c	(revision 1209)
+++ libtinymail-camel/tny-camel-folder.c	(working copy)
@@ -195,7 +195,10 @@
 			return FALSE;
 		}
 
-	priv->cached_length = camel_folder_get_message_count (priv->folder);
+		priv->subscribed = 
+			camel_store_folder_subscribed (store,
+						       camel_folder_get_full_name (priv->folder));
+		priv->cached_length = camel_folder_get_message_count (priv->folder);
 
 		/* priv->folder_changed_id = camel_object_hook_event (priv->folder, 
 			"folder_changed", (CamelObjectEventHookFunc)folder_changed, 
@@ -380,6 +383,24 @@
 	gboolean retval;
 
 	g_mutex_lock (priv->folder_lock);
+
+	if (!priv->folder || !priv->loaded || !CAMEL_IS_FOLDER (priv->folder))
+	{
+		CamelStore *store;
+		CamelFolder *cfolder;
+
+		if (!load_folder_no_lock (priv)) 
+		{
+			g_mutex_unlock (priv->folder_lock);
+			return;
+		}
+		store = (CamelStore*) _tny_camel_account_get_service 
+			(TNY_CAMEL_ACCOUNT (priv->account));
+		cfolder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER (self));
+		priv->subscribed = camel_store_folder_subscribed (store, 
+								  camel_folder_get_full_name (cfolder));
+	}
+
 	retval = priv->subscribed;
 	g_mutex_unlock (priv->folder_lock);
 
@@ -1067,18 +1088,59 @@
 tny_camel_folder_set_name_default (TnyFolder *self, const gchar *name)
 {
 	TnyCamelFolderPriv *priv = TNY_CAMEL_FOLDER_GET_PRIVATE (self);
+	gchar *full_name;
+	CamelFolder *cfolder;
+	CamelFolderInfo *parent_info;
+	const gchar *old_path;
+	gchar *new_path;
+	CamelException ex;
 
-	if (!load_folder (priv))
+	g_mutex_lock (priv->folder_lock);
+
+	if (!priv->folder || !priv->loaded || !CAMEL_IS_FOLDER (priv->folder))
+		if (!load_folder_no_lock (priv)) 
+		{
+			g_mutex_unlock (priv->folder_lock);
+			return;
+		}
+
+	if (!priv->iter || !priv->iter_parented)
 		return;
 
-	camel_folder_rename (priv->folder, name);
+	/* Create new full name */
+	cfolder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER (self));
+	old_path = camel_folder_get_full_name (cfolder);
+	parent_info = priv->iter->parent;
+	new_path = g_strdup_printf ("%s/%s", parent_info->name, name);
 
+	/* Check that the name really changes */
+	if (!strcmp (old_path, new_path)) 
+	{
+		g_free (new_path);
+		return;
+	}
+
+	/* Rename folder */
+	camel_exception_init (&ex);
+	camel_store_rename_folder (cfolder->parent_store, old_path, (const gchar *) new_path, &ex);
+	g_free (new_path);
+
+	if (camel_exception_is_set (&ex))
+	{
+		g_warning (N_("Renaming folder %s to %s failed: %s\n"),
+			   camel_folder_get_name (cfolder),
+			   name,
+			   camel_exception_get_description (&ex));
+		camel_exception_clear (&ex);
+		return;
+	}
+
 	if (G_UNLIKELY (priv->cached_name))
 		g_free (priv->cached_name);
 
 	priv->cached_name = g_strdup (name);
 
-	return;
+	g_mutex_unlock (priv->folder_lock);
 }
 
 /**
@@ -1355,8 +1417,9 @@
 	folder = tny_camel_folder_new ();
 	info = camel_store_create_folder (store, priv->folder_name, name, &ex);
 
-	if (camel_exception_is_set (&ex)) {
-		g_warning ("Creating folder failed: %s\n", 
+	if (camel_exception_is_set (&ex)) 
+	{
+		g_warning (N_("Creating folder failed: %s\n"), 
 			camel_exception_get_description (&ex));
 		g_object_unref (G_OBJECT (folder));
 		return NULL;
Index: libtinymail-camel/tny-camel-store-account.c
===================================================================
--- libtinymail-camel/tny-camel-store-account.c	(revision 1209)
+++ libtinymail-camel/tny-camel-store-account.c	(working copy)
@@ -88,7 +88,6 @@
 			g_free (proto);
 		
 			camel_url_set_protocol (url, priv->proto); 
-
 			camel_url_set_user (url, priv->user);
 			camel_url_set_host (url, priv->host);
 		
@@ -164,89 +163,81 @@
 	return;
 }
 
-
+/**
+ * This utility function performs folder subscriptions/unsubscriptions
+ * since the code for both operations is almost the same. If the
+ * subscribe parameter is TRUE, then we're asking for a folder
+ * subscription, else for a folder unsubscription */
 static void
-tny_camel_store_account_subscribe (TnyStoreAccount *self, TnyFolder *folder)
+set_subscription (TnyStoreAccount *self, TnyFolder *folder, gboolean subscribe)
 {
-	TnyCamelAccountPriv *apriv = TNY_CAMEL_ACCOUNT_GET_PRIVATE (self);
+	TnyCamelAccountPriv *apriv;
 	CamelException ex = CAMEL_EXCEPTION_INITIALISER;
 	CamelStore *store;
 	CamelFolder *cfolder;
 	const gchar *folder_full_name;
 
-	g_assert (TNY_IS_CAMEL_FOLDER (folder));
+	apriv = TNY_CAMEL_ACCOUNT_GET_PRIVATE (self);
 
+	/* Get store */
 	g_static_rec_mutex_lock (apriv->service_lock);
 	store = camel_session_get_store ((CamelSession*) apriv->session, 
 			apriv->url_string, &ex);
 	g_static_rec_mutex_unlock (apriv->service_lock);
 
+	if (!camel_store_supports_subscriptions (store)) 
+		goto cleanup;
+
 	/* Retrieve the folder full name */
 	cfolder = tny_camel_folder_get_folder (TNY_CAMEL_FOLDER (folder));
 	folder_full_name = camel_folder_get_full_name (cfolder);
 
-	if (camel_store_supports_subscriptions (store)
-	    && !camel_store_folder_subscribed (store, folder_full_name)) {
-		
+	if (camel_store_folder_subscribed (store, folder_full_name) == subscribe)
+		goto cleanup;
+
+	/* Subscribe or unsubscribe */
+	if (subscribe)
 		camel_store_subscribe_folder (store, folder_full_name, &ex);
+	else
+		camel_store_unsubscribe_folder (store, folder_full_name, &ex);
 
-		if (camel_exception_is_set (&ex)) {
-			g_warning (N_("Could not subscribe to folder %s: %s\n"),
-				   tny_folder_get_name (folder), 
-				   camel_exception_get_description (&ex));
-			camel_exception_clear (&ex);
-		} else {
-			/* Sync */
-			_tny_camel_folder_set_subscribed (TNY_CAMEL_FOLDER (folder), TRUE);
-
-			g_signal_emit (self, 
-				       tny_store_account_signals [TNY_STORE_ACCOUNT_SUBSCRIPTION_CHANGED], 
-				       0, folder);
-		}
-	}
-
-	camel_object_unref (CAMEL_OBJECT (cfolder));
-    	camel_object_unref (CAMEL_OBJECT (store));
-    
-	return;
-}
-
-static void
-tny_camel_store_account_unsubscribe (TnyStoreAccount *self, TnyFolder *folder)
-{
-	TnyCamelAccountPriv *apriv = TNY_CAMEL_ACCOUNT_GET_PRIVATE (self);
-	CamelException ex = CAMEL_EXCEPTION_INITIALISER;
-	CamelStore *store;
-
-	g_assert (TNY_IS_CAMEL_FOLDER (folder));
-
-	g_static_rec_mutex_lock (apriv->service_lock);
-	store = camel_session_get_store ((CamelSession*) apriv->session, 
-			apriv->url_string, &ex);
-	g_static_rec_mutex_unlock (apriv->service_lock);
-
-	camel_store_unsubscribe_folder (store, tny_folder_get_name (folder), &ex);
-
 	if (camel_exception_is_set (&ex)) {
-		g_warning (N_("Could not unsubscribe to folder %s: %s\n"),
+		g_warning (N_("Could not %s folder %s: %s\n"),
+			   subscribe ? _("subscribe to") : _("unsubscribe"),
 			   tny_folder_get_name (folder), 
 			   camel_exception_get_description (&ex));
 		camel_exception_clear (&ex);
 	} else {
 		/* Sync */
-		_tny_camel_folder_set_subscribed (TNY_CAMEL_FOLDER (folder), FALSE);
-
+		_tny_camel_folder_set_subscribed (TNY_CAMEL_FOLDER (folder), subscribe);
+		
 		g_signal_emit (self, 
 			       tny_store_account_signals [TNY_STORE_ACCOUNT_SUBSCRIPTION_CHANGED], 
 			       0, folder);
 	}
+	camel_object_unref (CAMEL_OBJECT (cfolder));
 
-	camel_object_unref (CAMEL_OBJECT (store));
+cleanup:
+    	camel_object_unref (CAMEL_OBJECT (store));
+}
 
-	return;
+static void
+tny_camel_store_account_subscribe (TnyStoreAccount *self, TnyFolder *folder)
+{
+	g_assert (TNY_IS_CAMEL_FOLDER (folder));
+
+	set_subscription (self, folder, TRUE);
 }
 
+static void
+tny_camel_store_account_unsubscribe (TnyStoreAccount *self, TnyFolder *folder)
+{
+	g_assert (TNY_IS_CAMEL_FOLDER (folder));
 
+	set_subscription (self, folder, FALSE);
+}
+
+
 /**
  * tny_camel_store_account_new:
  * 
Index: libtinymail-gnome-desktop/tny-gnome-account-store.c
===================================================================
--- libtinymail-gnome-desktop/tny-gnome-account-store.c	(revision 1209)
+++ libtinymail-gnome-desktop/tny-gnome-account-store.c	(working copy)
@@ -414,13 +414,14 @@
 
 			if (options)
 			{
+				GSList *tmp = options;
 				while (options)
 				{
 					tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (account), options->data);
 					g_free (options->data);
 					options = g_slist_next (options);
 				}
-				g_slist_free (options);
+				g_slist_free (tmp);
 			}
 
 			/* Because we only check for the n first bytes, the pops, imaps and smtps also work */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]