| |
1 /* |
| |
2 * Evolution integration plugin for Gaim |
| |
3 * |
| |
4 * Copyright (C) 2004 Henry Jen. |
| |
5 * |
| |
6 * This program is free software; you can redistribute it and/or |
| |
7 * modify it under the terms of the GNU General Public License as |
| |
8 * published by the Free Software Foundation; either version 2 of the |
| |
9 * License, or (at your option) any later version. |
| |
10 * |
| |
11 * This program is distributed in the hope that it will be useful, but |
| |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| |
14 * 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, write to the Free Software |
| |
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| |
19 * 02111-1307, USA. |
| |
20 */ |
| |
21 |
| |
22 #include "internal.h" |
| |
23 #include "gtkblist.h" |
| |
24 #include "gtkgaim.h" |
| |
25 #include "gtkutils.h" |
| |
26 #include "gtkimhtml.h" |
| |
27 |
| |
28 #include "debug.h" |
| |
29 #include "gevolution.h" |
| |
30 |
| |
31 GtkTreeModel * |
| |
32 gevo_addrbooks_model_new() |
| |
33 { |
| |
34 return GTK_TREE_MODEL(gtk_list_store_new(NUM_ADDRBOOK_COLUMNS, |
| |
35 G_TYPE_STRING, G_TYPE_STRING)); |
| |
36 } |
| |
37 |
| |
38 void |
| |
39 gevo_addrbooks_model_unref(GtkTreeModel *model) |
| |
40 { |
| |
41 GtkTreeIter iter; |
| |
42 |
| |
43 g_return_if_fail(model != NULL); |
| |
44 g_return_if_fail(GTK_IS_LIST_STORE(model)); |
| |
45 |
| |
46 if (!gtk_tree_model_get_iter_first(model, &iter)) |
| |
47 return; |
| |
48 |
| |
49 g_object_unref(model); |
| |
50 } |
| |
51 |
| |
52 void |
| |
53 gevo_addrbooks_model_populate(GtkTreeModel *model) |
| |
54 { |
| |
55 ESourceList *addressbooks; |
| |
56 GError *err; |
| |
57 GSList *groups, *g; |
| |
58 GtkTreeIter iter; |
| |
59 GtkListStore *list; |
| |
60 |
| |
61 g_return_if_fail(model != NULL); |
| |
62 g_return_if_fail(GTK_IS_LIST_STORE(model)); |
| |
63 |
| |
64 list = GTK_LIST_STORE(model); |
| |
65 |
| |
66 if (!e_book_get_addressbooks(&addressbooks, &err)) |
| |
67 { |
| |
68 gaim_debug_error("evolution", |
| |
69 "Unable to fetch list of address books.\n"); |
| |
70 |
| |
71 gtk_list_store_append(list, &iter); |
| |
72 gtk_list_store_set(list, &iter, |
| |
73 ADDRBOOK_COLUMN_NAME, _("None"), |
| |
74 ADDRBOOK_COLUMN_URI, NULL, |
| |
75 -1); |
| |
76 |
| |
77 return; |
| |
78 } |
| |
79 |
| |
80 groups = e_source_list_peek_groups(addressbooks); |
| |
81 |
| |
82 if (groups == NULL) |
| |
83 { |
| |
84 gtk_list_store_append(list, &iter); |
| |
85 gtk_list_store_set(list, &iter, |
| |
86 ADDRBOOK_COLUMN_NAME, _("None"), |
| |
87 ADDRBOOK_COLUMN_URI, NULL, |
| |
88 -1); |
| |
89 |
| |
90 return; |
| |
91 } |
| |
92 |
| |
93 for (g = groups; g != NULL; g = g->next) |
| |
94 { |
| |
95 GSList *sources, *s; |
| |
96 |
| |
97 sources = e_source_group_peek_sources(g->data); |
| |
98 |
| |
99 for (s = sources; s != NULL; s = s->next) |
| |
100 { |
| |
101 ESource *source = E_SOURCE(s->data); |
| |
102 |
| |
103 g_object_ref(source); |
| |
104 |
| |
105 gtk_list_store_append(list, &iter); |
| |
106 gtk_list_store_set(list, &iter, |
| |
107 ADDRBOOK_COLUMN_NAME, e_source_peek_name(source), |
| |
108 ADDRBOOK_COLUMN_URI, e_source_get_uri(source), |
| |
109 -1); |
| |
110 } |
| |
111 } |
| |
112 |
| |
113 g_object_unref(addressbooks); |
| |
114 } |
| |
115 |
| |
116 static EContact * |
| |
117 gevo_run_query_in_uri(const gchar *uri, EBookQuery *query) |
| |
118 { |
| |
119 EBook *book; |
| |
120 gboolean status; |
| |
121 GList *cards; |
| |
122 |
| |
123 if (!gevo_load_addressbook(uri, &book, NULL)) |
| |
124 { |
| |
125 gaim_debug_error("evolution", |
| |
126 "Error retrieving addressbook\n"); |
| |
127 return NULL; |
| |
128 } |
| |
129 |
| |
130 status = e_book_get_contacts(book, query, &cards, NULL); |
| |
131 if (!status) |
| |
132 { |
| |
133 gaim_debug_error("evolution", "Error %d in getting card list\n", |
| |
134 status); |
| |
135 g_object_unref(book); |
| |
136 return NULL; |
| |
137 } |
| |
138 g_object_unref(book); |
| |
139 |
| |
140 if (cards != NULL) |
| |
141 { |
| |
142 EContact *contact = E_CONTACT(cards->data); |
| |
143 GList *cards2 = cards->next; |
| |
144 |
| |
145 if (cards2 != NULL) |
| |
146 { |
| |
147 /* Break off the first contact and free the rest. */ |
| |
148 cards->next = NULL; |
| |
149 cards2->prev = NULL; |
| |
150 g_list_foreach(cards2, (GFunc)g_object_unref, NULL); |
| |
151 } |
| |
152 |
| |
153 /* Free the whole list. */ |
| |
154 g_list_free(cards); |
| |
155 |
| |
156 return contact; |
| |
157 } |
| |
158 |
| |
159 return NULL; |
| |
160 } |
| |
161 |
| |
162 /* |
| |
163 * Search for a buddy in the Evolution contacts. |
| |
164 * |
| |
165 * @param buddy The buddy to search for. |
| |
166 * @param query An optional query. This function takes ownership of @a query, |
| |
167 * so callers must e_book_query_ref() it in advance (to obtain a |
| |
168 * second reference) if they want to reuse @a query. |
| |
169 */ |
| |
170 EContact * |
| |
171 gevo_search_buddy_in_contacts(GaimBuddy *buddy, EBookQuery *query) |
| |
172 { |
| |
173 ESourceList *addressbooks; |
| |
174 GError *err; |
| |
175 EBookQuery *full_query; |
| |
176 GSList *groups, *g; |
| |
177 EContact *result; |
| |
178 EContactField protocol_field = gevo_prpl_get_field(buddy->account, buddy); |
| |
179 |
| |
180 if (protocol_field == 0) |
| |
181 return NULL; |
| |
182 |
| |
183 if (query != NULL) |
| |
184 { |
| |
185 EBookQuery *queries[2]; |
| |
186 |
| |
187 queries[0] = query; |
| |
188 queries[1] = e_book_query_field_test(protocol_field, E_BOOK_QUERY_IS, buddy->name); |
| |
189 if (queries[1] == NULL) |
| |
190 { |
| |
191 gaim_debug_error("evolution", "Error in creating protocol query\n"); |
| |
192 e_book_query_unref(query); |
| |
193 return NULL; |
| |
194 } |
| |
195 |
| |
196 full_query = e_book_query_and(2, queries, TRUE); |
| |
197 } |
| |
198 else |
| |
199 { |
| |
200 full_query = e_book_query_field_test(protocol_field, E_BOOK_QUERY_IS, buddy->name); |
| |
201 if (full_query == NULL) |
| |
202 { |
| |
203 gaim_debug_error("evolution", "Error in creating protocol query\n"); |
| |
204 return NULL; |
| |
205 } |
| |
206 } |
| |
207 |
| |
208 if (!e_book_get_addressbooks(&addressbooks, &err)) |
| |
209 { |
| |
210 gaim_debug_error("evolution", |
| |
211 "Unable to fetch list of address books.\n"); |
| |
212 e_book_query_unref(full_query); |
| |
213 if (err != NULL) |
| |
214 g_error_free(err); |
| |
215 return NULL; |
| |
216 } |
| |
217 |
| |
218 groups = e_source_list_peek_groups(addressbooks); |
| |
219 if (groups == NULL) |
| |
220 { |
| |
221 g_object_unref(addressbooks); |
| |
222 e_book_query_unref(full_query); |
| |
223 return NULL; |
| |
224 } |
| |
225 |
| |
226 for (g = groups; g != NULL; g = g->next) |
| |
227 { |
| |
228 GSList *sources, *s; |
| |
229 sources = e_source_group_peek_sources(g->data); |
| |
230 for (s = sources; s != NULL; s = s->next) |
| |
231 { |
| |
232 result = gevo_run_query_in_uri(e_source_get_uri(E_SOURCE(s->data)), full_query); |
| |
233 if (result != NULL) { |
| |
234 g_object_unref(addressbooks); |
| |
235 e_book_query_unref(full_query); |
| |
236 return result; |
| |
237 } |
| |
238 } |
| |
239 } |
| |
240 |
| |
241 g_object_unref(addressbooks); |
| |
242 e_book_query_unref(full_query); |
| |
243 return NULL; |
| |
244 } |