Initial ruby bindings
[notmuch] / bindings / ruby / gc.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 #include <stdlib.h>
24
25 static notmuch_rb_database_t *
26 _notmuch_rb_messages_db(notmuch_rb_messages_t *messages);
27
28 static notmuch_rb_database_t *
29 _notmuch_rb_message_db(notmuch_rb_message_t *message)
30 {
31     notmuch_rb_messages_t *messages;
32     notmuch_rb_database_t *db;
33
34     if (rb_obj_is_instance_of(message->parent, notmuch_rb_cDatabase)) {
35         Data_Get_Struct(message->parent, notmuch_rb_database_t, db);
36     }
37     else if (rb_obj_is_instance_of(message->parent, notmuch_rb_cMessages)) {
38         Data_Get_Struct(message->parent, notmuch_rb_messages_t, messages);
39         db = _notmuch_rb_messages_db(messages);
40     }
41     else
42         rb_bug("message's parent is neither database nor messages");
43
44     return db;
45 }
46
47 static notmuch_rb_database_t *
48 _notmuch_rb_messages_db(notmuch_rb_messages_t *messages)
49 {
50     notmuch_rb_query_t *query;
51     notmuch_rb_thread_t *thread;
52     notmuch_rb_message_t *message;
53     notmuch_rb_threads_t *threads;
54     notmuch_rb_database_t *db;
55
56     if (rb_obj_is_instance_of(messages->parent, notmuch_rb_cQuery)) {
57         Data_Get_Struct(messages->parent, notmuch_rb_query_t, query);
58         Data_Get_Struct(query->db, notmuch_rb_database_t, db);
59     }
60     else if (rb_obj_is_instance_of(messages->parent, notmuch_rb_cThread)) {
61         Data_Get_Struct(messages->parent, notmuch_rb_thread_t, thread);
62         Data_Get_Struct(thread->threads, notmuch_rb_threads_t, threads);
63         Data_Get_Struct(threads->query, notmuch_rb_query_t, query);
64         Data_Get_Struct(query->db, notmuch_rb_database_t, db);
65     }
66     else if (rb_obj_is_instance_of(messages->parent, notmuch_rb_cMessage)) {
67         Data_Get_Struct(messages->parent, notmuch_rb_message_t, message);
68         db = _notmuch_rb_message_db(message);
69     }
70     else
71         rb_bug("messages' parent is neither query nor thread nor message");
72
73     return db;
74 }
75
76 static notmuch_rb_database_t *
77 _notmuch_rb_thread_db(notmuch_rb_thread_t *thread)
78 {
79     notmuch_rb_threads_t *threads;
80     notmuch_rb_query_t *query;
81     notmuch_rb_database_t *db;
82
83     Data_Get_Struct(thread->threads, notmuch_rb_threads_t, threads);
84     Data_Get_Struct(threads->query, notmuch_rb_query_t, query);
85     Data_Get_Struct(query->db, notmuch_rb_database_t, db);
86
87     return db;
88 }
89
90 void
91 notmuch_rb_database_free(notmuch_rb_database_t *db)
92 {
93     if (db->nm_db)
94         notmuch_database_close(db->nm_db);
95
96     free(db);
97 }
98
99 void
100 notmuch_rb_directory_mark(notmuch_rb_directory_t *dir)
101 {
102     rb_gc_mark(dir->db);
103 }
104
105 void
106 notmuch_rb_directory_free(notmuch_rb_directory_t *dir)
107 {
108     notmuch_rb_database_t *db;
109
110     Data_Get_Struct(dir->db, notmuch_rb_database_t, db);
111
112     if (db->nm_db && dir->nm_dir)
113         notmuch_directory_destroy(dir->nm_dir);
114
115     free(dir);
116 }
117
118 void
119 notmuch_rb_filenames_mark(notmuch_rb_filenames_t *flist)
120 {
121     rb_gc_mark(flist->dir);
122 }
123
124 void
125 notmuch_rb_filenames_free(notmuch_rb_filenames_t *flist)
126 {
127     notmuch_rb_directory_t *dir;
128     notmuch_rb_database_t *db;
129
130     Data_Get_Struct(flist->dir, notmuch_rb_directory_t, dir);
131     Data_Get_Struct(dir->db, notmuch_rb_database_t, db);
132
133     if (db->nm_db && flist->nm_flist)
134         notmuch_filenames_destroy(flist->nm_flist);
135
136     free(flist);
137 }
138
139 void
140 notmuch_rb_query_mark(notmuch_rb_query_t *query)
141 {
142     rb_gc_mark(query->db);
143 }
144
145 void
146 notmuch_rb_query_free(notmuch_rb_query_t *query)
147 {
148     notmuch_rb_database_t *db;
149
150     Data_Get_Struct(query->db, notmuch_rb_database_t, db);
151
152     if (db->nm_db && query->nm_query)
153         notmuch_query_destroy(query->nm_query);
154
155     free(query);
156 }
157
158 void
159 notmuch_rb_threads_mark(notmuch_rb_threads_t *threads)
160 {
161     rb_gc_mark(threads->query);
162 }
163
164 void
165 notmuch_rb_threads_free(notmuch_rb_threads_t *threads)
166 {
167     notmuch_rb_query_t *query;
168     notmuch_rb_database_t *db;
169
170     Data_Get_Struct(threads->query, notmuch_rb_query_t, query);
171     Data_Get_Struct(query->db, notmuch_rb_database_t, db);
172
173     if (db->nm_db && threads->nm_threads)
174         notmuch_threads_destroy(threads->nm_threads);
175
176     free(threads);
177 }
178
179 void
180 notmuch_rb_messages_mark(notmuch_rb_messages_t *messages)
181 {
182     rb_gc_mark(messages->parent);
183 }
184
185 void
186 notmuch_rb_messages_free(notmuch_rb_messages_t *messages)
187 {
188     notmuch_rb_database_t *db;
189
190     db = _notmuch_rb_messages_db(messages);
191
192     if (db->nm_db && messages->nm_messages)
193         notmuch_messages_destroy(messages->nm_messages);
194
195     free(messages);
196 }
197
198 void
199 notmuch_rb_thread_mark(notmuch_rb_thread_t *thread)
200 {
201     rb_gc_mark(thread->threads);
202 }
203
204 void
205 notmuch_rb_thread_free(notmuch_rb_thread_t *thread)
206 {
207     notmuch_rb_database_t *db;
208
209     db = _notmuch_rb_thread_db(thread);
210
211     if (db->nm_db && thread->nm_thread)
212         notmuch_thread_destroy(thread->nm_thread);
213
214     free(thread);
215 }
216
217 void
218 notmuch_rb_message_mark(notmuch_rb_message_t *message)
219 {
220     rb_gc_mark(message->parent);
221 }
222
223 void
224 notmuch_rb_message_free(notmuch_rb_message_t *message)
225 {
226     notmuch_rb_database_t *db;
227
228     db = _notmuch_rb_message_db(message);
229     if (db->nm_db && message->nm_message)
230         notmuch_message_destroy(message->nm_message);
231
232     free(message);
233 }
234
235 void
236 notmuch_rb_tags_mark(notmuch_rb_tags_t *tags)
237 {
238     rb_gc_mark(tags->parent);
239 }
240
241 void
242 notmuch_rb_tags_free(notmuch_rb_tags_t *tags)
243 {
244     notmuch_rb_message_t *message;
245     notmuch_rb_messages_t *messages;
246     notmuch_rb_thread_t *thread;
247     notmuch_rb_database_t *db;
248
249     if (rb_obj_is_instance_of(tags->parent, notmuch_rb_cThread)) {
250         Data_Get_Struct(tags->parent, notmuch_rb_thread_t, thread);
251         db = _notmuch_rb_thread_db(thread);
252     }
253     else if (rb_obj_is_instance_of(tags->parent, notmuch_rb_cMessage)) {
254         Data_Get_Struct(tags->parent, notmuch_rb_message_t, message);
255         db = _notmuch_rb_message_db(message);
256     }
257     else if (rb_obj_is_instance_of(tags->parent, notmuch_rb_cMessages)) {
258         Data_Get_Struct(tags->parent, notmuch_rb_messages_t, messages);
259         db = _notmuch_rb_messages_db(messages);
260     }
261     else
262         rb_bug("tags' parent is neither thread nor message nor messages");
263
264     if (db->nm_db && tags->nm_tags)
265         notmuch_tags_destroy(tags->nm_tags);
266
267     free(tags);
268 }