]> git.notmuchmail.org Git - notmuch/blob - bindings/ruby/messages.c
Initial ruby bindings
[notmuch] / bindings / ruby / messages.c
1 /* The Ruby interface to the notmuch mail library
2  *
3  * Copyright © 2010 Ali Polatel
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Ali Polatel <alip@exherbo.org>
19  */
20
21 #include "defs.h"
22
23 /* call-seq: MESSAGES.each {|item| block } => MESSAGES
24  *
25  * Calls +block+ once for each message in +self+, passing that element as a
26  * parameter.
27  */
28 VALUE
29 notmuch_rb_messages_each(VALUE self)
30 {
31     notmuch_rb_message_t *message;
32     notmuch_rb_messages_t *messages;
33     VALUE messagev;
34
35     Data_Get_Struct(self, notmuch_rb_messages_t, messages);
36     if (!messages->nm_messages)
37         return self;
38
39     for (; notmuch_messages_valid(messages->nm_messages);
40             notmuch_messages_move_to_next(messages->nm_messages))
41     {
42         messagev = Data_Make_Struct(notmuch_rb_cMessage, notmuch_rb_message_t,
43                 notmuch_rb_message_mark, notmuch_rb_message_free, message);
44         message->nm_message = notmuch_messages_get(messages->nm_messages);
45         message->parent = self;
46         rb_yield(messagev);
47     }
48
49     return self;
50 }
51
52 /*
53  * call-seq: MESSAGES.tags => TAGS
54  *
55  * Collect tags from the messages
56  */
57 VALUE
58 notmuch_rb_messages_collect_tags(VALUE self)
59 {
60     notmuch_rb_tags_t *tags;
61     notmuch_rb_messages_t *messages;
62     VALUE tagsv;
63
64     Data_Get_Struct(self, notmuch_rb_messages_t, messages);
65
66     tagsv = Data_Make_Struct(notmuch_rb_cTags, notmuch_rb_tags_t,
67             notmuch_rb_tags_mark, notmuch_rb_tags_free, tags);
68     tags->nm_tags = notmuch_messages_collect_tags(messages->nm_messages);
69     tags->parent = self;
70     if (!tags->nm_tags)
71         rb_raise(notmuch_rb_eMemoryError, "out of memory");
72
73     return tagsv;
74 }