]> git.notmuchmail.org Git - notmuch/blob - notmuch-search-tags.c
control: Update package description to follow upstream README.
[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 void
25 print_tags (notmuch_tags_t *tags)
26 {
27     const char *t;
28
29     while ((t = notmuch_tags_get (tags))) {
30         printf ("%s\n", t);
31         notmuch_tags_advance (tags);
32     }
33 }
34
35 int
36 notmuch_search_tags_command (void *ctx, int argc, char *argv[])
37 {
38     notmuch_messages_t *msgs;
39     notmuch_tags_t *tags;
40     notmuch_config_t *config;
41     notmuch_database_t *db;
42     notmuch_query_t *query;
43     char *query_str;
44
45     tags = NULL;
46     config = NULL;
47     db = NULL;
48     query = NULL;
49
50     if ((config = notmuch_config_open (ctx, NULL, NULL)) == NULL) {
51         goto error;
52     }
53
54     db = notmuch_database_open (notmuch_config_get_database_path (config),
55                                 NOTMUCH_DATABASE_MODE_READ_ONLY);
56     if (db == NULL) {
57         goto error;
58     }
59
60     if (argc > 0) {
61         if ((query_str = query_string_from_args (ctx, argc, argv)) == NULL) {
62             fprintf (stderr, "Out of memory.\n");
63             goto error;
64         }
65
66         if (*query_str == '\0') {
67             fprintf (stderr, "Error: Invalid search string.\n");
68             goto error;
69         }
70
71         if ((query = notmuch_query_create (db, query_str)) == NULL) {
72             fprintf (stderr, "Out of memory\n");
73             goto error;
74         }
75
76
77         msgs = notmuch_query_search_messages (query);
78         if ((tags = notmuch_messages_collect_tags (msgs)) == NULL) goto error;
79     } else {
80         if ((tags = notmuch_database_get_all_tags (db)) == NULL) {
81             fprintf (stderr, "Error while getting tags from the database.\n");
82             goto error;
83         }
84     }
85
86     print_tags (tags);
87
88     notmuch_tags_destroy (tags);
89     if (query) notmuch_query_destroy (query);
90     notmuch_database_close (db);
91     return 0;
92
93 error:
94     if (tags) notmuch_tags_destroy (tags);
95     if (query) notmuch_query_destroy (query);
96     if (db) notmuch_database_close (db);
97     return 1;
98 }