unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Igor Almeida <igor.contato@gmail.com>
To: notmuch@notmuchmail.org
Subject: [PATCH/RFC 2/3] notmuch new: tag messages based on maildir custom flags
Date: Wed, 25 Nov 2015 23:16:30 -0300	[thread overview]
Message-ID: <1448504191-30974-3-git-send-email-igor.contato@gmail.com> (raw)
In-Reply-To: <1448504191-30974-1-git-send-email-igor.contato@gmail.com>

Iterate through the flags in the message's filename, creating tags and
tagging the message as they appear.
This uses Bremner/Dovecot's convention of maildir_keyword_xxx, where xxx a char
between 'a' and 'z'.

Since more than one file may point to the same message in the database
(think renames), the tags related to maildir custom flags are always
removed and later re-added to maintain consistency.

Signed-off-by: Igor Almeida <igor.contato@gmail.com>
---
 lib/message.cc | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)

diff --git a/lib/message.cc b/lib/message.cc
index 26b5e76..8a89dee 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -1301,6 +1301,13 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
     unsigned i;
     int seen_maildir_info = 0;
 
+    /* For custom maildir flags */
+    char *c; /* to iterate over combined_flags */
+    const char *custom_flag_dbase_name;
+    int index;
+    notmuch_bool_t cancel_custom_flags = FALSE;
+    notmuch_bool_t must_free = FALSE;
+
     for (filenames = notmuch_message_get_filenames (message);
 	 notmuch_filenames_valid (filenames);
 	 notmuch_filenames_move_to_next (filenames))
@@ -1349,6 +1356,87 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
 	if (status)
 	    return status;
     }
+
+    /* Now let's find custom maildir flags */
+
+    status = notmuch_message_freeze (message);
+    if (status)
+	goto finish;
+
+    cancel_custom_flags = FALSE;
+
+    /* First we remove the tags for custom maildir flags, then
+     * we add only the ones in combined_flags
+     */
+    char letter;
+    for (letter = 'a'; letter <= 'z'; letter++) {
+	index = letter - 'a';
+	status = notmuch_database_get_maildir_keyword (
+	    message->notmuch, index, &custom_flag_dbase_name);
+	if (status) {
+	    /* TODO probably OOM, what now? */
+	}
+
+	if (custom_flag_dbase_name == NULL) {
+	    /* We don't have a custom flag for this letter, try the next
+	     * one now
+	     */
+	    continue;
+	}
+
+	status = notmuch_message_remove_tag (message, custom_flag_dbase_name);
+	if (status) {
+	    /* TODO tag too long? */
+	}
+    }
+
+    /* Go through combined_flags and tag the message accordingly */
+
+    for (c = combined_flags; *c; c++) {
+	if (*c >= 'a' && *c <= 'z') {
+	    index = *c - 'a';
+
+	    status = notmuch_database_get_maildir_keyword(
+		message->notmuch, index, &custom_flag_dbase_name);
+	    if (status) {
+		cancel_custom_flags = TRUE;
+		break;
+	    } else {
+		if (custom_flag_dbase_name == NULL) {
+		    /* Custom flag does not yet exist */
+		    custom_flag_dbase_name = talloc_asprintf (message,
+			"maildir_keyword_%c", 'a' + index);
+		    /* Add to the database */
+		    notmuch_database_set_maildir_keyword (
+			message->notmuch, index, custom_flag_dbase_name);
+
+		    must_free = TRUE;
+		}
+
+		/* Tag the message */
+		status = notmuch_message_add_tag (message,
+		    custom_flag_dbase_name);
+
+		if (must_free) {
+		    talloc_free((void*)custom_flag_dbase_name);
+		    must_free = FALSE;
+		}
+
+		if (status) {
+		    cancel_custom_flags = TRUE;
+		    break;
+		}
+	    }
+	}
+    }
+
+    if (cancel_custom_flags) {
+	/* TODO rollback the add_tag's */
+    }
+
+    status = notmuch_message_thaw (message);
+
+finish:
     status = notmuch_message_thaw (message);
 
     talloc_free (combined_flags);
-- 
2.5.3

  parent reply	other threads:[~2015-11-26  2:16 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-26  2:16 [PATCH/RFC 0/3] Maildir custom flags and notmuch tags Igor Almeida
2015-11-26  2:16 ` [PATCH/RFC 1/3] David Bremner's patch for custom maildir flags Igor Almeida
2015-11-26  2:16 ` Igor Almeida [this message]
2015-11-26  2:16 ` [PATCH/RFC 3/3] notmuch new: sync maildir custom flag user configuration Igor Almeida
2016-01-10 13:59 ` [PATCH/RFC 0/3] Maildir custom flags and notmuch tags David Bremner
2016-02-02 16:52   ` Jan Pobrislo
2016-02-03 12:03     ` David Bremner
2016-02-03 14:32       ` Jan Pobrislo
2016-02-03 15:38         ` David Bremner
2016-02-03 15:39           ` David Bremner
2016-02-03 16:00           ` Jan Pobrislo
     [not found]           ` <20160203165650.4ae528c1@dorje.v103.te2000>
2016-02-03 16:04             ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1448504191-30974-3-git-send-email-igor.contato@gmail.com \
    --to=igor.contato@gmail.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).