[PATCH 6/8] CLI: refactor dumping of tags.
[notmuch-archives.git] / cc / 255cf74386b66c2ee315eeff90082b1200cd74
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 1A44E431FBF\r
6         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:31 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id mh+LsAt6J0Ju for <notmuch@notmuchmail.org>;\r
17         Mon, 26 Mar 2012 14:04:29 -0700 (PDT)\r
18 Received: from mail-lb0-f181.google.com (mail-lb0-f181.google.com\r
19         [209.85.217.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 9236A431FAF\r
22         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:29 -0700 (PDT)\r
23 Received: by lbok6 with SMTP id k6so4671094lbo.26\r
24         for <notmuch@notmuchmail.org>; Mon, 26 Mar 2012 14:04:26 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
28         :in-reply-to:references:x-gm-message-state;\r
29         bh=D56dZfuAcdnIg2Ra6n886KLjYTaJlZGih4CRIc3PEgo=;\r
30         b=UYN/18Rdyi5AJcDXnWDB7yY7U/D0TpGIG+Dz2jiux+8ozydFmzeaERdnuughK8QO09\r
31         ivCQglPqtZ8qiGvqv9IOCXDiPsFOWBumKh3A+Uy2xliv2Z+SmTABvic60WFoHlli0Svy\r
32         TvmtxMPckRbcWjLcXrnWqF4PvyZyOCezSKpMAGyDLDZsidyro6zSq9NzdbY0R8B/E3Cm\r
33         WGkXa2Q+n68OjD1LeuQaJXamg9yFBXJgkkXlfrmWY5dluWd9Vy1yWxHVK26GBrl9mRkq\r
34         btWHPPWQvWZRIjai0y2K+2EnNTdqBvaMTR1DSI6ByhAurN5wLJIhTzsF7QxBHZLTnZ/a\r
35         0UBQ==\r
36 Received: by 10.152.145.135 with SMTP id su7mr17135117lab.5.1332795866454;\r
37         Mon, 26 Mar 2012 14:04:26 -0700 (PDT)\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
39         [84.248.80.253])\r
40         by mx.google.com with ESMTPS id oy17sm18177700lab.7.2012.03.26.14.04.24\r
41         (version=SSLv3 cipher=OTHER); Mon, 26 Mar 2012 14:04:25 -0700 (PDT)\r
42 From: Jani Nikula <jani@nikula.org>\r
43 To: notmuch@notmuchmail.org\r
44 Subject: [PATCH v3 3/4] cli: refactor "notmuch tag" query tagging into a\r
45         separate function\r
46 Date: Tue, 27 Mar 2012 00:04:10 +0300\r
47 Message-Id:\r
48  <9c547e08d098cf75e52e6ecb7f2af2ca75730274.1332795415.git.jani@nikula.org>\r
49 X-Mailer: git-send-email 1.7.5.4\r
50 In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
51 References: <cover.1332795415.git.jani@nikula.org>\r
52 In-Reply-To: <cover.1332795415.git.jani@nikula.org>\r
53 References: <cover.1332795415.git.jani@nikula.org>\r
54 X-Gm-Message-State:\r
55  ALoCoQkeZlT38jroLnKl94fvz8v/POdErNgBl+HjVRSu/Bt1BgoB7/MtdSSSL6kz59jb0GYTNBts\r
56 Cc: tomi.ollila@iki.fi\r
57 X-BeenThere: notmuch@notmuchmail.org\r
58 X-Mailman-Version: 2.1.13\r
59 Precedence: list\r
60 List-Id: "Use and development of the notmuch mail system."\r
61         <notmuch.notmuchmail.org>\r
62 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
63         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
64 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
65 List-Post: <mailto:notmuch@notmuchmail.org>\r
66 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
67 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
68         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
69 X-List-Received-Date: Mon, 26 Mar 2012 21:04:31 -0000\r
70 \r
71 Refactor to make tagging code easier to reuse in the future. No\r
72 functional changes.\r
73 \r
74 Signed-off-by: Jani Nikula <jani@nikula.org>\r
75 ---\r
76  notmuch-tag.c |  104 +++++++++++++++++++++++++++++++++------------------------\r
77  1 files changed, 60 insertions(+), 44 deletions(-)\r
78 \r
79 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
80 index 0a6b140..05feed3 100644\r
81 --- a/notmuch-tag.c\r
82 +++ b/notmuch-tag.c\r
83 @@ -110,6 +110,63 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
84      return query_string;\r
85  }\r
86  \r
87 +/* Tag messages matching 'query_string' according to 'tag_ops', which\r
88 + * must be an array of tagging operations terminated with an empty\r
89 + * element. */\r
90 +static int\r
91 +tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,\r
92 +          tag_operation_t *tag_ops, notmuch_bool_t synchronize_flags)\r
93 +{\r
94 +    notmuch_query_t *query;\r
95 +    notmuch_messages_t *messages;\r
96 +    notmuch_message_t *message;\r
97 +    int i;\r
98 +\r
99 +    /* Optimize the query so it excludes messages that already have\r
100 +     * the specified set of tags. */\r
101 +    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
102 +    if (query_string == NULL) {\r
103 +       fprintf (stderr, "Out of memory.\n");\r
104 +       return 1;\r
105 +    }\r
106 +\r
107 +    query = notmuch_query_create (notmuch, query_string);\r
108 +    if (query == NULL) {\r
109 +       fprintf (stderr, "Out of memory.\n");\r
110 +       return 1;\r
111 +    }\r
112 +\r
113 +    /* tagging is not interested in any special sort order */\r
114 +    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
115 +\r
116 +    for (messages = notmuch_query_search_messages (query);\r
117 +        notmuch_messages_valid (messages) && !interrupted;\r
118 +        notmuch_messages_move_to_next (messages))\r
119 +    {\r
120 +       message = notmuch_messages_get (messages);\r
121 +\r
122 +       notmuch_message_freeze (message);\r
123 +\r
124 +       for (i = 0; tag_ops[i].tag; i++) {\r
125 +           if (tag_ops[i].remove)\r
126 +               notmuch_message_remove_tag (message, tag_ops[i].tag);\r
127 +           else\r
128 +               notmuch_message_add_tag (message, tag_ops[i].tag);\r
129 +       }\r
130 +\r
131 +       notmuch_message_thaw (message);\r
132 +\r
133 +       if (synchronize_flags)\r
134 +           notmuch_message_tags_to_maildir_flags (message);\r
135 +\r
136 +       notmuch_message_destroy (message);\r
137 +    }\r
138 +\r
139 +    notmuch_query_destroy (query);\r
140 +\r
141 +    return interrupted;\r
142 +}\r
143 +\r
144  int\r
145  notmuch_tag_command (void *ctx, int argc, char *argv[])\r
146  {\r
147 @@ -118,12 +175,10 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
148      char *query_string;\r
149      notmuch_config_t *config;\r
150      notmuch_database_t *notmuch;\r
151 -    notmuch_query_t *query;\r
152 -    notmuch_messages_t *messages;\r
153 -    notmuch_message_t *message;\r
154      struct sigaction action;\r
155      notmuch_bool_t synchronize_flags;\r
156      int i;\r
157 +    int ret;\r
158  \r
159      /* Setup our handler for SIGINT */\r
160      memset (&action, 0, sizeof (struct sigaction));\r
161 @@ -170,14 +225,6 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
162         return 1;\r
163      }\r
164  \r
165 -    /* Optimize the query so it excludes messages that already have\r
166 -     * the specified set of tags. */\r
167 -    query_string = _optimize_tag_query (ctx, query_string, tag_ops);\r
168 -    if (query_string == NULL) {\r
169 -       fprintf (stderr, "Out of memory.\n");\r
170 -       return 1;\r
171 -    }\r
172 -\r
173      config = notmuch_config_open (ctx, NULL, NULL);\r
174      if (config == NULL)\r
175         return 1;\r
176 @@ -189,40 +236,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
177  \r
178      synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
179  \r
180 -    query = notmuch_query_create (notmuch, query_string);\r
181 -    if (query == NULL) {\r
182 -       fprintf (stderr, "Out of memory.\n");\r
183 -       return 1;\r
184 -    }\r
185 -\r
186 -    /* tagging is not interested in any special sort order */\r
187 -    notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);\r
188 +    ret = tag_query (ctx, notmuch, query_string, tag_ops, synchronize_flags);\r
189  \r
190 -    for (messages = notmuch_query_search_messages (query);\r
191 -        notmuch_messages_valid (messages) && !interrupted;\r
192 -        notmuch_messages_move_to_next (messages))\r
193 -    {\r
194 -       message = notmuch_messages_get (messages);\r
195 -\r
196 -       notmuch_message_freeze (message);\r
197 -\r
198 -       for (i = 0; tag_ops[i].tag; i++) {\r
199 -           if (tag_ops[i].remove)\r
200 -               notmuch_message_remove_tag (message, tag_ops[i].tag);\r
201 -           else\r
202 -               notmuch_message_add_tag (message, tag_ops[i].tag);\r
203 -       }\r
204 -\r
205 -       notmuch_message_thaw (message);\r
206 -\r
207 -       if (synchronize_flags)\r
208 -           notmuch_message_tags_to_maildir_flags (message);\r
209 -\r
210 -       notmuch_message_destroy (message);\r
211 -    }\r
212 -\r
213 -    notmuch_query_destroy (query);\r
214      notmuch_database_close (notmuch);\r
215  \r
216 -    return interrupted;\r
217 +    return ret;\r
218  }\r
219 -- \r
220 1.7.5.4\r
221 \r