]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
Merge tag 'debian/0.12-1'
[notmuch] / notmuch-count.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see http://www.gnu.org/licenses/ .
18  *
19  * Author: Keith Packard <keithp@keithp.com>
20  */
21
22 #include "notmuch-client.h"
23
24 enum {
25     OUTPUT_THREADS,
26     OUTPUT_MESSAGES,
27 };
28
29 int
30 notmuch_count_command (void *ctx, int argc, char *argv[])
31 {
32     notmuch_config_t *config;
33     notmuch_database_t *notmuch;
34     notmuch_query_t *query;
35     char *query_str;
36     int opt_index;
37     int output = OUTPUT_MESSAGES;
38     notmuch_bool_t no_exclude = FALSE;
39     unsigned int i;
40
41     notmuch_opt_desc_t options[] = {
42         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
43           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
44                                   { "messages", OUTPUT_MESSAGES },
45                                   { 0, 0 } } },
46         { NOTMUCH_OPT_BOOLEAN, &no_exclude, "no-exclude", 'd', 0 },
47         { 0, 0, 0, 0, 0 }
48     };
49
50     opt_index = parse_arguments (argc, argv, options, 1);
51
52     if (opt_index < 0) {
53         return 1;
54     }
55
56     config = notmuch_config_open (ctx, NULL, NULL);
57     if (config == NULL)
58         return 1;
59
60     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
61                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
62     if (notmuch == NULL)
63         return 1;
64
65     query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index);
66     if (query_str == NULL) {
67         fprintf (stderr, "Out of memory.\n");
68         return 1;
69     }
70
71     if (*query_str == '\0') {
72         query_str = talloc_strdup (ctx, "");
73     }
74
75     query = notmuch_query_create (notmuch, query_str);
76     if (query == NULL) {
77         fprintf (stderr, "Out of memory\n");
78         return 1;
79     }
80
81     if (!no_exclude) {
82         const char **search_exclude_tags;
83         size_t search_exclude_tags_length;
84
85         search_exclude_tags = notmuch_config_get_search_exclude_tags
86             (config, &search_exclude_tags_length);
87         for (i = 0; i < search_exclude_tags_length; i++)
88             notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);
89     }
90
91     notmuch_query_set_omit_excluded_messages (query, TRUE);
92
93     switch (output) {
94     case OUTPUT_MESSAGES:
95         printf ("%u\n", notmuch_query_count_messages (query));
96         break;
97     case OUTPUT_THREADS:
98         printf ("%u\n", notmuch_query_count_threads (query));
99         break;
100     }
101
102     notmuch_query_destroy (query);
103     notmuch_database_close (notmuch);
104
105     return 0;
106 }