diff options
-rw-r--r-- | mail/e-mail-folder-utils.c | 155 | ||||
-rw-r--r-- | mail/e-mail-folder-utils.h | 10 |
2 files changed, 165 insertions, 0 deletions
diff --git a/mail/e-mail-folder-utils.c b/mail/e-mail-folder-utils.c index f2ba207e01..bcf649728a 100644 --- a/mail/e-mail-folder-utils.c +++ b/mail/e-mail-folder-utils.c @@ -161,3 +161,158 @@ e_mail_folder_append_message_finish (CamelFolder *folder, /* Assume success unless a GError is set. */ return !g_simple_async_result_propagate_error (simple, error); } + +/** + * e_mail_folder_uri_parse: + * @session: a #CamelSession + * @folder_uri: a folder URI + * @out_store: return location for a #CamelStore, or %NULL + * @out_folder_name: return location for a folder name, or %NULL + * @error: return location for a #GError, or %NULL + * + * Parses a folder URI and returns the corresponding #CamelStore instance + * in @out_store and folder name string in @out_folder_name. If the URI + * is malformed or no corresponding store exists, the function sets @error + * and returns %FALSE. + * + * If the function is able to parse the URI, the #CamelStore instance + * set in @out_store should be unreferenced with g_object_unref() when + * done with it, and the folder name string set in @out_folder_name + * should be freed with g_free(). + * + * Returns: %TRUE if @folder_uri could be parsed, %FALSE otherwise + **/ +gboolean +e_mail_folder_uri_parse (CamelSession *session, + const gchar *folder_uri, + CamelStore **out_store, + gchar **out_folder_name, + GError **error) +{ + CamelURL *url; + CamelService *service = NULL; + const gchar *folder_name = NULL; + gboolean success = FALSE; + + g_return_val_if_fail (CAMEL_IS_SESSION (session), FALSE); + g_return_val_if_fail (folder_uri != NULL, FALSE); + + url = camel_url_new (folder_uri, error); + if (url == NULL) + return FALSE; + + service = camel_session_get_service_by_url ( + session, url, CAMEL_PROVIDER_STORE); + + if (CAMEL_IS_STORE (service)) { + CamelProvider *provider; + + provider = camel_service_get_provider (service); + + if (provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH) + folder_name = url->fragment; + else if (url->path != NULL && *url->path == '/') + folder_name = url->path + 1; + } + + if (CAMEL_IS_STORE (service) && folder_name != NULL) { + if (out_store != NULL) + *out_store = g_object_ref (service); + + if (out_folder_name != NULL) + *out_folder_name = g_strdup (folder_name); + + success = TRUE; + } else { + g_set_error ( + error, CAMEL_FOLDER_ERROR, + CAMEL_FOLDER_ERROR_INVALID, + _("Invalid folder URI '%s'"), + folder_uri); + } + + camel_url_free (url); + + return success; +} + +/** + * e_mail_folder_uri_equal: + * @session: a #CamelSession + * @folder_uri_a: a folder URI + * @folder_uri_b: another folder URI + * + * Compares two folder URIs for equality. If either URI is invalid, + * the function returns %FALSE. + * + * Returns: %TRUE if the URIs are equal, %FALSE if not + **/ +gboolean +e_mail_folder_uri_equal (CamelSession *session, + const gchar *folder_uri_a, + const gchar *folder_uri_b) +{ + CamelStore *store_a; + CamelStore *store_b; + CamelStoreClass *class; + gchar *folder_name_a; + gchar *folder_name_b; + gboolean success_a; + gboolean success_b; + gboolean equal = FALSE; + + g_return_val_if_fail (CAMEL_IS_SESSION (session), FALSE); + g_return_val_if_fail (folder_uri_a != NULL, FALSE); + g_return_val_if_fail (folder_uri_b != NULL, FALSE); + + success_a = e_mail_folder_uri_parse ( + session, folder_uri_a, &store_a, &folder_name_a, NULL); + + success_b = e_mail_folder_uri_parse ( + session, folder_uri_b, &store_b, &folder_name_b, NULL); + + if (!success_a || !success_b) + goto exit; + + if (store_a != store_b) + goto exit; + + /* Doesn't matter which store we use since they're the same. */ + class = CAMEL_STORE_GET_CLASS (store_a); + g_return_val_if_fail (class->compare_folder_name != NULL, FALSE); + + equal = class->compare_folder_name (folder_name_a, folder_name_b); + +exit: + if (success_a) { + g_object_unref (store_a); + g_free (folder_name_a); + } + + if (success_b) { + g_object_unref (store_b); + g_free (folder_name_b); + } + + return equal; +} + +/** + * e_mail_folder_uri_from_folder: + * @folder: a #CamelFolder + * + * Convenience function for building a folder URI from a #CamelFolder. + * Free the returned URI string with g_free(). + * + * Returns: a newly-allocated folder URI string + **/ +gchar * +e_mail_folder_uri_from_folder (CamelFolder *folder) +{ + g_return_val_if_fail (CAMEL_IS_FOLDER (folder), NULL); + + /* XXX This looks silly because it's just a temporary + * implementation. I have other plans in store. */ + + return g_strdup (camel_folder_get_uri (folder)); +} diff --git a/mail/e-mail-folder-utils.h b/mail/e-mail-folder-utils.h index 7872f3e146..3187acfdf2 100644 --- a/mail/e-mail-folder-utils.h +++ b/mail/e-mail-folder-utils.h @@ -45,6 +45,16 @@ gboolean e_mail_folder_append_message_finish gchar **appended_uid, GError **error); +gboolean e_mail_folder_uri_parse (CamelSession *session, + const gchar *folder_uri, + CamelStore **out_store, + gchar **out_folder_name, + GError **error); +gboolean e_mail_folder_uri_equal (CamelSession *session, + const gchar *folder_uri_a, + const gchar *folder_uri_b); +gchar * e_mail_folder_uri_from_folder (CamelFolder *folder); + G_END_DECLS #endif /* E_MAIL_FOLDER_UTILS_H */ |