Esempio n. 1
0
static int
print_count (notmuch_database_t *notmuch, const char *query_str,
	     const char **exclude_tags, size_t exclude_tags_length, int output)
{
    notmuch_query_t *query;
    size_t i;

    query = notmuch_query_create (notmuch, query_str);
    if (query == NULL) {
	fprintf (stderr, "Out of memory\n");
	return 1;
    }

    for (i = 0; i < exclude_tags_length; i++)
	notmuch_query_add_tag_exclude (query, exclude_tags[i]);

    switch (output) {
    case OUTPUT_MESSAGES:
	printf ("%u\n", notmuch_query_count_messages (query));
	break;
    case OUTPUT_THREADS:
	printf ("%u\n", notmuch_query_count_threads (query));
	break;
    }

    notmuch_query_destroy (query);

    return 0;
}
Esempio n. 2
0
int
notmuch_count_command (void *ctx, int argc, char *argv[])
{
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    notmuch_query_t *query;
    char *query_str;
    int i;
    notmuch_bool_t output_messages = TRUE;

    argc--; argv++; /* skip subcommand argument */

    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
	if (strcmp (argv[i], "--") == 0) {
	    i++;
	    break;
	}
	if (STRNCMP_LITERAL (argv[i], "--output=") == 0) {
	    const char *opt = argv[i] + sizeof ("--output=") - 1;
	    if (strcmp (opt, "threads") == 0) {
		output_messages = FALSE;
	    } else if (strcmp (opt, "messages") == 0) {
		output_messages = TRUE;
	    } else {
		fprintf (stderr, "Invalid value for --output: %s\n", opt);
		return 1;
	    }
	} else {
	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
	    return 1;
	}
    }

    argc -= i;
    argv += i;

    config = notmuch_config_open (ctx, NULL, NULL);
    if (config == NULL)
	return 1;

    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
				     NOTMUCH_DATABASE_MODE_READ_ONLY);
    if (notmuch == NULL)
	return 1;

    query_str = query_string_from_args (ctx, argc, argv);
    if (query_str == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return 1;
    }

    if (*query_str == '\0') {
	query_str = talloc_strdup (ctx, "");
    }

    query = notmuch_query_create (notmuch, query_str);
    if (query == NULL) {
	fprintf (stderr, "Out of memory\n");
	return 1;
    }

    if (output_messages)
	printf ("%u\n", notmuch_query_count_messages (query));
    else
	printf ("%u\n", notmuch_query_count_threads (query));

    notmuch_query_destroy (query);
    notmuch_database_close (notmuch);

    return 0;
}
Esempio n. 3
0
static int
do_search_threads (const search_format_t *format,
		   notmuch_query_t *query,
		   notmuch_sort_t sort,
		   output_t output,
		   int offset,
		   int limit)
{
    notmuch_thread_t *thread;
    notmuch_threads_t *threads;
    notmuch_tags_t *tags;
    time_t date;
    int first_thread = 1;
    int i;

    if (offset < 0) {
	offset += notmuch_query_count_threads (query);
	if (offset < 0)
	    offset = 0;
    }

    threads = notmuch_query_search_threads (query);
    if (threads == NULL)
	return 1;

    fputs (format->results_start, stdout);

    for (i = 0;
	 notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);
	 notmuch_threads_move_to_next (threads), i++)
    {
	int first_tag = 1;

	thread = notmuch_threads_get (threads);

	if (i < offset) {
	    notmuch_thread_destroy (thread);
	    continue;
	}

	if (! first_thread)
	    fputs (format->item_sep, stdout);

	if (output == OUTPUT_THREADS) {
	    format->item_id (thread, "thread:",
			     notmuch_thread_get_thread_id (thread));
	} else { /* output == OUTPUT_SUMMARY */
	    fputs (format->item_start, stdout);

	    if (sort == NOTMUCH_SORT_OLDEST_FIRST)
		date = notmuch_thread_get_oldest_date (thread);
	    else
		date = notmuch_thread_get_newest_date (thread);

	    format->thread_summary (thread,
				    notmuch_thread_get_thread_id (thread),
				    date,
				    notmuch_thread_get_matched_messages (thread),
				    notmuch_thread_get_total_messages (thread),
				    notmuch_thread_get_authors (thread),
				    notmuch_thread_get_subject (thread));

	    fputs (format->tag_start, stdout);

	    for (tags = notmuch_thread_get_tags (thread);
		 notmuch_tags_valid (tags);
		 notmuch_tags_move_to_next (tags))
	    {
		if (! first_tag)
		    fputs (format->tag_sep, stdout);
		printf (format->tag, notmuch_tags_get (tags));
		first_tag = 0;
	    }

	    fputs (format->tag_end, stdout);

	    fputs (format->item_end, stdout);
	}

	first_thread = 0;

	notmuch_thread_destroy (thread);
    }

    if (first_thread)
	fputs (format->results_null, stdout);
    else
	fputs (format->results_end, stdout);

    return 0;
}