Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: David Edmondson <dme-zDQXUQW/Few <at> public.gmane.org>
Subject: [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same.
Newsgroups: gmane.mail.notmuch.general
Date: Friday 2nd May 2014 12:14:43 UTC (over 3 years ago)
Rather than hand-coding the application of tags to new messages, use
the existing tag_op_list_apply().
fixup.
---
 notmuch-new.c | 31 ++++++++++++++++---------------
 tag-util.c    |  8 ++++++++
 tag-util.h    | 10 +++++++---
 3 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index d269c7c..2c3d680 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -45,10 +45,9 @@ typedef struct {
     int output_is_a_tty;
     enum verbosity verbosity;
     notmuch_bool_t debug;
-    const char **new_tags;
-    size_t new_tags_length;
     const char **new_ignore;
     size_t new_ignore_length;
+    tag_op_list_t *tag_ops;
 
     int total_files;
     int processed_files;
@@ -253,7 +252,6 @@ add_file (notmuch_database_t *notmuch, const char
*filename,
 	  add_files_state_t *state)
 {
     notmuch_message_t *message = NULL;
-    const char **tag;
     notmuch_status_t status;
 
     status = notmuch_database_begin_atomic (notmuch);
@@ -263,14 +261,13 @@ add_file (notmuch_database_t *notmuch, const char
*filename,
     status = notmuch_database_add_message (notmuch, filename, &message);
     switch (status) {
     /* Success. */
-    case NOTMUCH_STATUS_SUCCESS:
-	state->added_messages++;
-	notmuch_message_freeze (message);
-	for (tag = state->new_tags; *tag != NULL; tag++)
-	    notmuch_message_add_tag (message, *tag);
+    case NOTMUCH_STATUS_SUCCESS:;
+	tag_op_flag_t flags = 0;
+
 	if (state->synchronize_flags)
-	    notmuch_message_maildir_flags_to_tags (message);
-	notmuch_message_thaw (message);
+	    flags |= TAG_FLAG_TAG_SYNC;
+	state->added_messages++;
+	(void) tag_op_list_apply (message, state->tag_ops, flags);
 	break;
     /* Non-fatal issues (go on to next file). */
     case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
@@ -923,6 +920,8 @@ notmuch_new_command (notmuch_config_t *config, int
argc, char *argv[])
     notmuch_bool_t timer_is_active = FALSE;
     notmuch_bool_t no_hooks = FALSE;
     notmuch_bool_t quiet = FALSE, verbose = FALSE;
+    const char **new_tags;
+    size_t new_tags_length;
 
     add_files_state.verbosity = VERBOSITY_NORMAL;
     add_files_state.debug = FALSE;
@@ -946,20 +945,22 @@ notmuch_new_command (notmuch_config_t *config, int
argc, char *argv[])
     else if (verbose)
 	add_files_state.verbosity = VERBOSITY_VERBOSE;
 
-    add_files_state.new_tags = notmuch_config_get_new_tags (config,
&add_files_state.new_tags_length);
+    new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
     add_files_state.new_ignore = notmuch_config_get_new_ignore (config,
&add_files_state.new_ignore_length);
     add_files_state.synchronize_flags =
notmuch_config_get_maildir_synchronize_flags (config);
+    add_files_state.tag_ops = tag_op_list_create (config);
     db_path = notmuch_config_get_database_path (config);
 
-    for (i = 0; i < add_files_state.new_tags_length; i++) {
+    for (i = 0; i < new_tags_length; i++) {
 	const char *error_msg;
 
-	error_msg = illegal_tag (add_files_state.new_tags[i], FALSE);
+	error_msg = illegal_tag (new_tags[i], FALSE);
 	if (error_msg) {
-	    fprintf (stderr, "Error: tag '%s' in new.tags: %s\n",
-		     add_files_state.new_tags[i], error_msg);
+	    fprintf (stderr, "Error: tag '%s' in new.tags: %s\n", new_tags[i],
error_msg);
 	    return EXIT_FAILURE;
 	}
+	if (tag_op_list_append (add_files_state.tag_ops, new_tags[i], FALSE) !=
0)
+	    return EXIT_FAILURE;
     }
 
     if (!no_hooks) {
diff --git a/tag-util.c b/tag-util.c
index 343c161..1dee2f3 100644
--- a/tag-util.c
+++ b/tag-util.c
@@ -327,6 +327,14 @@ tag_op_list_apply (notmuch_message_t *message,
 	}
     }
 
+    if (flags & TAG_FLAG_TAG_SYNC) {
+	status = notmuch_message_maildir_flags_to_tags (message);
+	if (status) {
+	    message_error (message, status, "synching maildir to tags");
+	    return status;
+	}
+    }
+
     return NOTMUCH_STATUS_SUCCESS;
 
 }
diff --git a/tag-util.h b/tag-util.h
index 8a4074c..512cfac 100644
--- a/tag-util.h
+++ b/tag-util.h
@@ -14,19 +14,23 @@ typedef enum {
      */
     TAG_FLAG_MAILDIR_SYNC = (1 << 0),
 
+    /* Operations are synced from maildir, if possible.
+     */
+    TAG_FLAG_TAG_SYNC = (1 << 1),
+
     /* Remove all tags from message before applying list.
      */
-    TAG_FLAG_REMOVE_ALL = (1 << 1),
+    TAG_FLAG_REMOVE_ALL = (1 << 2),
 
     /* Don't try to avoid database operations. Useful when we
      * know that message passed needs these operations.
      */
-    TAG_FLAG_PRE_OPTIMIZED = (1 << 2),
+    TAG_FLAG_PRE_OPTIMIZED = (1 << 3),
 
     /* Accept strange tags that might be user error;
      * intended for use by notmuch-restore.
      */
-    TAG_FLAG_BE_GENEROUS = (1 << 3)
+    TAG_FLAG_BE_GENEROUS = (1 << 4)
 
 } tag_op_flag_t;
 
-- 
1.9.2
 
CD: 2ms