[evolution] I#620 - Add tag to free form expression syntax for Location



commit 93902523fc7c3a5028574c95727b20956faca5b5
Author: Milan Crha <mcrha redhat com>
Date:   Tue Oct 20 16:57:17 2020 +0200

    I#620 - Add tag to free form expression syntax for Location
    
    Closes https://gitlab.gnome.org/GNOME/evolution/-/issues/620

 src/mail/e-mail-free-form-exp.c | 59 ++++++++++++++++++++++++++++------
 src/mail/em-utils.c             | 71 +++++++++++++++++++++++++++++++++++++++++
 src/mail/em-utils.h             |  3 ++
 3 files changed, 124 insertions(+), 9 deletions(-)
---
diff --git a/src/mail/e-mail-free-form-exp.c b/src/mail/e-mail-free-form-exp.c
index a9771f9ef0..1b8416f269 100644
--- a/src/mail/e-mail-free-form-exp.c
+++ b/src/mail/e-mail-free-form-exp.c
@@ -23,9 +23,11 @@
 #include <string.h>
 
 #include <camel/camel.h>
-#include <e-util/e-util.h>
 #include <libedataserver/libedataserver.h>
 
+#include "e-util/e-util.h"
+#include "em-utils.h"
+
 #include "e-mail-free-form-exp.h"
 
 static gchar *
@@ -474,27 +476,65 @@ mail_ffe_received (const gchar *word,
        return mail_ffe_process_date ("get-received-date", word, options);
 }
 
+static gboolean
+mail_ffe_is_neg (const gchar *value)
+{
+       return value &&
+              (g_ascii_strcasecmp (value, "!") == 0 ||
+               g_ascii_strcasecmp (value, "0") == 0 ||
+               g_ascii_strcasecmp (value, "no") == 0 ||
+               g_ascii_strcasecmp (value, "not") == 0 ||
+               g_ascii_strcasecmp (value, "false") == 0 ||
+               g_ascii_strcasecmp (value, C_("ffe", "no")) == 0 ||
+               g_ascii_strcasecmp (value, C_("ffe", "not")) == 0 ||
+               g_ascii_strcasecmp (value, C_("ffe", "false")) == 0);
+}
+
 static gchar *
 mail_ffe_attachment (const gchar *word,
                     const gchar *options,
                     const gchar *hint)
 {
-       gboolean is_neg = FALSE;
+       gboolean is_neg;
 
        if (!word)
                return NULL;
 
-       if (g_ascii_strcasecmp (word, "no") == 0 ||
-           g_ascii_strcasecmp (word, "false") == 0 ||
-           g_ascii_strcasecmp (word, C_("ffe", "no")) == 0 ||
-           g_ascii_strcasecmp (word, C_("ffe", "false")) == 0 ||
-           g_ascii_strcasecmp (word, "0") == 0) {
-               is_neg = TRUE;
-       }
+       is_neg = mail_ffe_is_neg (word);
 
        return g_strdup_printf ("%s(system-flag \"Attachments\")%s", is_neg ? "(not " : "", is_neg ? ")" : 
"");
 }
 
+static gchar *
+mail_ffe_location (const gchar *word,
+                  const gchar *options,
+                  const gchar *hint)
+{
+       GString *encoded_uri;
+       gchar *sexp, *folder_uri;
+       gboolean is_neg;
+
+       if (!word)
+               return NULL;
+
+       is_neg = mail_ffe_is_neg (options);
+
+       folder_uri = em_utils_account_path_to_folder_uri (NULL, word);
+
+       if (!folder_uri)
+               return NULL;
+
+       encoded_uri = g_string_new ("");
+       camel_sexp_encode_string (encoded_uri, folder_uri);
+
+       sexp = g_strdup_printf ("%s(message-location %s)%s", is_neg ? "(not " : "", encoded_uri->str, is_neg 
? ")" : "");
+
+       g_string_free (encoded_uri, TRUE);
+       g_free (folder_uri);
+
+       return sexp;
+}
+
 static const EFreeFormExpSymbol mail_ffe_symbols[] = {
        { "",           "1",    mail_ffe_recips },
        { "from:f",     NULL,   mail_ffe_from },
@@ -514,6 +554,7 @@ static const EFreeFormExpSymbol mail_ffe_symbols[] = {
        { "sent",       NULL,   mail_ffe_sent },
        { "received:rcv", NULL, mail_ffe_received },
        { "attachment:a", NULL, mail_ffe_attachment },
+       { "location:m", NULL,   mail_ffe_location },
        { NULL,         NULL,   NULL}
 };
 
diff --git a/src/mail/em-utils.c b/src/mail/em-utils.c
index 2d3c03dbc2..2ee4f8c6fd 100644
--- a/src/mail/em-utils.c
+++ b/src/mail/em-utils.c
@@ -1898,3 +1898,74 @@ em_utils_process_autoarchive_sync (EMailBackend *mail_backend,
 
        return success;
 }
+
+/**
+ * em_utils_account_path_to_folder_uri:
+ * @session: (nullable): a #CamelSession, or %NULL
+ * @account_path: the account path to transform to folder URI
+ *
+ * Transform the @account_path to a folder URI. It can be in a form
+ * of 'account-name/folder/path', aka 'On This Computer/Inbox/Private'.
+ * The account name is compared case insensitively.
+ *
+ * When the @session is %NULL, it' is taken from the default shell, if
+ * such exists.
+ *
+ * Returns: (nullable): a folder URI corresponding to the @account_path,
+ *    or %NULL, when the account could not be found. Free the returned
+ *    string with g_free(), when no longer needed.
+ *
+ * Since: 3.40
+ **/
+gchar *
+em_utils_account_path_to_folder_uri (CamelSession *session,
+                                    const gchar *account_path)
+{
+       GList *services, *link;
+       const gchar *dash;
+       gchar *folder_uri = NULL, *account_name;
+
+       g_return_val_if_fail (account_path != NULL, NULL);
+
+       dash = strchr (account_path, '/');
+       if (!dash)
+               return NULL;
+
+       if (!session) {
+               EShell *shell;
+               EShellBackend *shell_backend;
+               EMailSession *mail_session;
+
+               shell = e_shell_get_default ();
+               if (!shell)
+                       return NULL;
+
+               shell_backend = e_shell_get_backend_by_name (shell, "mail");
+               if (!shell_backend)
+                       return NULL;
+
+               mail_session = e_mail_backend_get_session (E_MAIL_BACKEND (shell_backend));
+               if (!mail_session)
+                       return NULL;
+
+               session = CAMEL_SESSION (mail_session);
+       }
+
+       account_name = e_util_utf8_data_make_valid (account_path, dash - account_path);
+
+       services = camel_session_list_services (session);
+       for (link = services; link; link = g_list_next (link)) {
+               CamelService *service = link->data;
+
+               /* Case sensitive account name compare, because the folder names are also compared case 
sensitively */
+               if (CAMEL_IS_STORE (service) && g_strcmp0 (camel_service_get_display_name (service), 
account_name) == 0) {
+                       folder_uri = e_mail_folder_uri_build (CAMEL_STORE (service), dash + 1);
+                       break;
+               }
+       }
+
+       g_list_free_full (services, g_object_unref);
+       g_free (account_name);
+
+       return folder_uri;
+}
diff --git a/src/mail/em-utils.h b/src/mail/em-utils.h
index d415659643..a258e3430c 100644
--- a/src/mail/em-utils.h
+++ b/src/mail/em-utils.h
@@ -110,6 +110,9 @@ gboolean    em_utils_process_autoarchive_sync
 gchar *                em_utils_build_export_basename  (CamelFolder *folder,
                                                 const gchar *uid,
                                                 const gchar *extension);
+gchar *                em_utils_account_path_to_folder_uri
+                                               (CamelSession *session,
+                                                const gchar *account_path); /* On This 
Computer/Inbox/Subfolder... */
 
 G_END_DECLS
 


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