]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
bindings/python: Implement Tags().__nonzero__()
[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 #if 0
33     char *opt, *end;
34     int i, first = 0, max_threads = -1;
35     notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
36 #endif
37
38     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
39         if (strcmp (argv[i], "--") == 0) {
40             i++;
41             break;
42         }
43 #if 0
44         if (STRNCMP_LITERAL (argv[i], "--first=") == 0) {
45             opt = argv[i] + sizeof ("--first=") - 1;
46             first = strtoul (opt, &end, 10);
47             if (*opt == '\0' || *end != '\0') {
48                 fprintf (stderr, "Invalid value for --first: %s\n", opt);
49                 return 1;
50             }
51         } else if (STRNCMP_LITERAL (argv[i], "--max-threads=") == 0) {
52             opt = argv[i] + sizeof ("--max-threads=") - 1;
53             max_threads = strtoul (opt, &end, 10);
54             if (*opt == '\0' || *end != '\0') {
55                 fprintf (stderr, "Invalid value for --max-threads: %s\n", opt);
56                 return 1;
57             }
58         } else if (STRNCMP_LITERAL (argv[i], "--sort=") == 0) {
59             opt = argv[i] + sizeof ("--sort=") - 1;
60             if (strcmp (opt, "oldest-first") == 0) {
61                 sort = NOTMUCH_SORT_OLDEST_FIRST;
62             } else if (strcmp (opt, "newest-first") == 0) {
63                 sort = NOTMUCH_SORT_NEWEST_FIRST;
64             } else {
65                 fprintf (stderr, "Invalid value for --sort: %s\n", opt);
66                 return 1;
67             }
68         } else
69 #endif
70         {
71             fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
72             return 1;
73         }
74     }
75
76     argc -= i;
77     argv += i;
78
79     config = notmuch_config_open (ctx, NULL, NULL);
80     if (config == NULL)
81         return 1;
82
83     notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
84                                      NOTMUCH_DATABASE_MODE_READ_ONLY);
85     if (notmuch == NULL)
86         return 1;
87
88     query_str = query_string_from_args (ctx, argc, argv);
89     if (query_str == NULL) {
90         fprintf (stderr, "Out of memory.\n");
91         return 1;
92     }
93
94     if (*query_str == '\0') {
95         query_str = talloc_strdup (ctx, "");
96     }
97
98     query = notmuch_query_create (notmuch, query_str);
99     if (query == NULL) {
100         fprintf (stderr, "Out of memory\n");
101         return 1;
102     }
103
104     printf ("%u\n", notmuch_query_count_messages(query));
105
106     notmuch_query_destroy (query);
107     notmuch_database_close (notmuch);
108
109     return 0;
110 }