]> git.notmuchmail.org Git - notmuch/blob - notmuch-search-tags.c
notmuch: New command 'search-tags'.
[notmuch] / notmuch-search-tags.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Jan Janak
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: Jan Janak <jan@ryngle.com>
20  */
21
22 #include "notmuch-client.h"
23
24 static int
25 list_all_tags (notmuch_database_t* db)
26 {
27     notmuch_tags_t* tags;
28     const char* t;
29
30     if ((tags = notmuch_database_get_all_tags (db)) == NULL) {
31         fprintf (stderr, "Error while obtaining tags from the database.\n");
32         return 1;
33     }
34
35     while((t = notmuch_tags_get (tags))) {
36         printf ("%s\n", t);
37         notmuch_tags_advance (tags);
38     }
39
40     notmuch_tags_destroy (tags);
41     return 0;
42 }
43
44 int
45 notmuch_search_tags_command (void *ctx, int argc, char *argv[])
46 {
47     notmuch_config_t *config;
48     notmuch_database_t *db;
49
50     config = NULL;
51     db = NULL;
52
53     if ((config = notmuch_config_open (ctx, NULL, NULL)) == NULL) {
54         goto error;
55     }
56
57     db = notmuch_database_open (notmuch_config_get_database_path (config),
58                                 NOTMUCH_DATABASE_MODE_READ_ONLY);
59     if (db == NULL) {
60         goto error;
61     }
62
63     if (list_all_tags (db) != 0) goto error;
64
65     notmuch_database_close (db);
66     return 0;
67
68 error:
69     if (db) notmuch_database_close (db);
70     return 1;
71 }