]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
emacs: support "notmuch new" as a notmuch-poll-script
[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 int
25 notmuch_count_command (void *ctx, int argc, char *argv[])
26 {
27     notmuch_config_t *config;
28     notmuch_database_t *notmuch;
29     notmuch_query_t *query;
30     char *query_str;
31     int i;
32     notmuch_bool_t output_messages = TRUE;
33
34     argc--; argv++; /* skip subcommand argument */
35
36     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
37         if (strcmp (argv[i], "--") == 0) {
38             i++;
39             break;
40         }
41         if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
42             const char *opt = argv[i] + sizeof ("--output=") - 1;
43             if (strcmp (opt, "threads") == 0) {
44                 output_messages = FALSE;
45             } else if (strcmp (opt, "messages") == 0) {
46                 output_messages = TRUE;
47             } else {
48                 fprintf (stderr, "Invalid value for --output: %s\n", opt);
49                 return 1;
50             }
51         } else {
52             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
53             return 1;
54         }
55     }
56
57     argc -= i;
58     argv += i;
59
60     config = notmuch_config_open (ctx, NULL, NULL);
61     if (config == NULL)
62         return 1;
63
64     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
65                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
66     if (notmuch == NULL)
67         return 1;
68
69     query_str = query_string_from_args (ctx, argc, argv);
70     if (query_str == NULL) {
71         fprintf (stderr, "Out of memory.\n");
72         return 1;
73     }
74
75     if (*query_str == '\0') {
76         query_str = talloc_strdup (ctx, "");
77     }
78
79     query = notmuch_query_create (notmuch, query_str);
80     if (query == NULL) {
81         fprintf (stderr, "Out of memory\n");
82         return 1;
83     }
84
85     if (output_messages)
86         printf ("%u\n", notmuch_query_count_messages (query));
87     else
88         printf ("%u\n", notmuch_query_count_threads (query));
89
90     notmuch_query_destroy (query);
91     notmuch_database_close (notmuch);
92
93     return 0;
94 }