Mercurial > pidgin
annotate plugins/gevolution/assoc-buddy.c @ 9951:42cdec4f639b
[gaim-migrate @ 10847]
<faceprint> datallah: i can just commit that without thinking, right?
<audiokat2> faceprint: you can't break HEAD any worse right now :)
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Sat, 04 Sep 2004 16:51:49 +0000 |
parents | c00def44d76a |
children | ff4be2d1401d |
rev | line source |
---|---|
8089 | 1 /* |
2 * Evolution integration plugin for Gaim | |
3 * | |
4 * Copyright (C) 2003 Christian Hammond. | |
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 */ | |
9825
c00def44d76a
[gaim-migrate @ 10696]
Christian Hammond <chipx86@chipx86.com>
parents:
9824
diff
changeset
|
21 #include "internal.h" |
8089 | 22 #include "gtkblist.h" |
9824 | 23 #include "gtkgaim.h" |
8089 | 24 #include "gtkutils.h" |
25 #include "gtkimhtml.h" | |
26 #include "gaim-disclosure.h" | |
27 | |
28 #include "debug.h" | |
29 | |
30 #include "gevolution.h" | |
31 | |
32 #include <stdlib.h> | |
33 #include <bonobo/bonobo-main.h> | |
34 | |
35 enum | |
36 { | |
37 COLUMN_NAME, | |
38 COLUMN_DATA, | |
39 NUM_COLUMNS | |
40 }; | |
41 | |
42 static gint | |
43 delete_win_cb(GtkWidget *w, GdkEvent *event, GevoAssociateBuddyDialog *dialog) | |
44 { | |
45 gtk_widget_destroy(dialog->win); | |
46 | |
47 g_list_foreach(dialog->contacts, (GFunc)g_free, NULL); | |
48 | |
49 if (dialog->contacts != NULL) | |
50 g_list_free(dialog->contacts); | |
51 | |
52 g_object_unref(dialog->book); | |
53 | |
54 g_free(dialog); | |
55 | |
56 return 0; | |
57 } | |
58 | |
59 static void | |
60 populate_address_books(GevoAssociateBuddyDialog *dialog) | |
61 { | |
62 GtkWidget *item; | |
63 GtkWidget *menu; | |
64 #if notyet | |
65 ESourceList *addressbooks; | |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
66 GSList *groups, *g; |
8089 | 67 #endif |
68 | |
69 menu = | |
70 gtk_option_menu_get_menu(GTK_OPTION_MENU(dialog->addressbooks_menu)); | |
71 | |
72 item = gtk_menu_item_new_with_label(_("Local Addressbook")); | |
73 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
74 gtk_widget_show(item); | |
75 | |
76 #if notyet | |
77 if (!e_book_get_addressbooks(&addressbooks, NULL)) | |
78 { | |
79 gaim_debug_error("evolution", | |
80 "Unable to fetch list of address books.\n"); | |
81 | |
82 item = gtk_menu_item_new_with_label(_("None")); | |
83 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
84 gtk_widget_show(item); | |
85 | |
86 return; | |
87 } | |
88 | |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
89 groups = e_source_list_peek_groups(addressbooks); |
8089 | 90 |
91 if (groups == NULL) | |
92 { | |
93 item = gtk_menu_item_new_with_label(_("None")); | |
94 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
95 gtk_widget_show(item); | |
96 | |
97 return; | |
98 } | |
99 | |
100 for (g = groups; g != NULL; g = g->next) | |
101 { | |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
102 GSList *sources, *s; |
8089 | 103 |
104 sources = e_source_group_peek_sources(g->data); | |
105 | |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
106 for (s = sources; s != NULL; s = s->next) |
8089 | 107 { |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
108 ESource *source = E_SOURCE(s->data); |
8089 | 109 |
110 item = gtk_menu_item_new_with_label(e_source_peek_name(source)); | |
111 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); | |
112 gtk_widget_show(item); | |
113 } | |
114 } | |
115 #endif | |
116 } | |
117 | |
118 static void | |
119 search_changed_cb(GtkEntry *entry, GevoAssociateBuddyDialog *dialog) | |
120 { | |
121 const char *text = gtk_entry_get_text(entry); | |
122 GList *l; | |
123 | |
124 gtk_list_store_clear(dialog->model); | |
125 | |
126 for (l = dialog->contacts; l != NULL; l = l->next) | |
127 { | |
128 EContact *contact = E_CONTACT(l->data); | |
129 const char *name; | |
130 GtkTreeIter iter; | |
131 | |
132 name = e_contact_get_const(contact, E_CONTACT_FULL_NAME); | |
133 | |
134 if (text != NULL && *text != '\0' && name != NULL && | |
135 g_ascii_strncasecmp(name, text, strlen(text))) | |
136 { | |
137 continue; | |
138 } | |
139 | |
140 gtk_list_store_append(dialog->model, &iter); | |
141 | |
142 gtk_list_store_set(dialog->model, &iter, | |
143 COLUMN_NAME, name, | |
144 COLUMN_DATA, contact, | |
145 -1); | |
146 } | |
147 } | |
148 | |
149 static void | |
150 clear_cb(GtkWidget *w, GevoAssociateBuddyDialog *dialog) | |
151 { | |
152 static gboolean lock = FALSE; | |
153 | |
154 if (lock) | |
155 return; | |
156 | |
157 lock = TRUE; | |
158 gtk_entry_set_text(GTK_ENTRY(dialog->search_field), ""); | |
159 lock = FALSE; | |
160 } | |
161 | |
162 static void | |
163 selected_cb(GtkTreeSelection *sel, GevoAssociateBuddyDialog *dialog) | |
164 { | |
165 gtk_widget_set_sensitive(dialog->assoc_button, TRUE); | |
166 } | |
167 | |
168 static void | |
169 add_columns(GevoAssociateBuddyDialog *dialog) | |
170 { | |
171 GtkCellRenderer *renderer; | |
172 GtkTreeViewColumn *column; | |
173 | |
174 /* Name column */ | |
175 column = gtk_tree_view_column_new(); | |
176 gtk_tree_view_column_set_title(column, _("Name")); | |
177 gtk_tree_view_insert_column(GTK_TREE_VIEW(dialog->treeview), column, -1); | |
178 gtk_tree_view_column_set_sort_column_id(column, COLUMN_NAME); | |
179 | |
180 renderer = gtk_cell_renderer_text_new(); | |
181 gtk_tree_view_column_pack_start(column, renderer, TRUE); | |
182 gtk_tree_view_column_add_attribute(column, renderer, | |
183 "text", COLUMN_NAME); | |
184 } | |
185 | |
186 static void | |
187 populate_treeview(GevoAssociateBuddyDialog *dialog) | |
188 { | |
189 EBookQuery *query; | |
190 EBook *book; | |
191 const char *prpl_id; | |
192 gboolean status; | |
193 GList *cards, *c; | |
194 | |
195 if (!gevo_load_addressbook(&book, NULL)) | |
196 { | |
197 gaim_debug_error("evolution", | |
198 "Error retrieving default addressbook\n"); | |
199 | |
200 return; | |
201 } | |
202 | |
203 query = e_book_query_field_exists(E_CONTACT_FULL_NAME); | |
204 | |
205 if (query == NULL) | |
206 { | |
207 gaim_debug_error("evolution", "Error in creating query\n"); | |
208 | |
209 g_object_unref(book); | |
210 | |
211 return; | |
212 } | |
213 | |
214 status = e_book_get_contacts(book, query, &cards, NULL); | |
215 | |
216 e_book_query_unref(query); | |
217 | |
218 if (!status) | |
219 { | |
220 gaim_debug_error("evolution", "Error %d in getting card list\n", | |
221 status); | |
222 | |
223 g_object_unref(book); | |
224 | |
225 return; | |
226 } | |
227 | |
228 prpl_id = gaim_account_get_protocol_id(dialog->buddy->account); | |
229 | |
230 for (c = cards; c != NULL; c = c->next) | |
231 { | |
232 EContact *contact = E_CONTACT(c->data); | |
233 const char *name; | |
234 GtkTreeIter iter; | |
235 EContactField protocol_field = 0; | |
236 | |
237 name = e_contact_get_const(contact, E_CONTACT_FULL_NAME); | |
238 | |
239 gtk_list_store_append(dialog->model, &iter); | |
240 | |
241 gtk_list_store_set(dialog->model, &iter, | |
242 COLUMN_NAME, name, | |
243 COLUMN_DATA, contact, | |
244 -1); | |
245 | |
246 /* See if this user has the buddy in its list. */ | |
247 protocol_field = gevo_prpl_get_field(dialog->buddy->account, | |
248 dialog->buddy); | |
249 | |
250 if (protocol_field > 0) | |
251 { | |
252 GList *ims, *l; | |
253 | |
254 ims = e_contact_get(contact, protocol_field); | |
255 | |
256 for (l = ims; l != NULL; l = l->next) | |
257 { | |
258 if (!strcmp(l->data, dialog->buddy->name)) | |
259 { | |
260 GtkTreeSelection *selection; | |
261 | |
262 /* This is it. Select it. */ | |
263 selection = gtk_tree_view_get_selection( | |
264 GTK_TREE_VIEW(dialog->treeview)); | |
265 | |
266 gtk_tree_selection_select_iter(selection, &iter); | |
267 break; | |
268 } | |
269 } | |
270 } | |
271 } | |
272 | |
273 dialog->contacts = cards; | |
274 dialog->book = book; | |
275 } | |
276 | |
277 static void | |
278 new_person_cb(GtkWidget *w, GevoAssociateBuddyDialog *dialog) | |
279 { | |
280 gevo_new_person_dialog_show(NULL, dialog->buddy->account, | |
281 dialog->buddy->name, NULL, dialog->buddy, | |
282 TRUE); | |
283 | |
284 delete_win_cb(NULL, NULL, dialog); | |
285 } | |
286 | |
287 static void | |
288 cancel_cb(GtkWidget *w, GevoAssociateBuddyDialog *dialog) | |
289 { | |
290 delete_win_cb(NULL, NULL, dialog); | |
291 } | |
292 | |
293 static void | |
294 assoc_buddy_cb(GtkWidget *w, GevoAssociateBuddyDialog *dialog) | |
295 { | |
296 GtkTreeSelection *selection; | |
297 GtkTreeIter iter; | |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
298 GList *list; |
8089 | 299 const char *fullname; |
300 EContactField protocol_field; | |
301 EContact *contact; | |
302 | |
303 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview)); | |
304 | |
305 gtk_tree_selection_get_selected(selection, NULL, &iter); | |
306 | |
307 gtk_tree_model_get(GTK_TREE_MODEL(dialog->model), &iter, | |
308 COLUMN_NAME, &fullname, | |
309 COLUMN_DATA, &contact, | |
310 -1); | |
311 | |
312 protocol_field = gevo_prpl_get_field(dialog->buddy->account, dialog->buddy); | |
313 | |
314 if (protocol_field == 0) | |
315 return; /* XXX */ | |
316 | |
317 list = e_contact_get(contact, protocol_field); | |
318 | |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
319 list = g_list_append(list, g_strdup(dialog->buddy->name)); |
8089 | 320 |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
321 e_contact_set(contact, protocol_field, list); |
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
322 if (!e_book_commit_contact(dialog->book, contact, NULL)) |
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
323 { |
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
324 gaim_debug_error("evolution", "Error adding contact to book\n"); |
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
325 } |
8089 | 326 |
327 /* Free the list. */ | |
9566
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
328 g_list_foreach(list, (GFunc)g_free, NULL); |
7a149eac59a3
[gaim-migrate @ 10409]
Christian Hammond <chipx86@chipx86.com>
parents:
9354
diff
changeset
|
329 g_list_free(list); |
8089 | 330 |
331 delete_win_cb(NULL, NULL, dialog); | |
332 } | |
333 | |
334 GevoAssociateBuddyDialog * | |
335 gevo_associate_buddy_dialog_new(GaimBuddy *buddy) | |
336 { | |
337 GevoAssociateBuddyDialog *dialog; | |
338 GtkWidget *button; | |
339 GtkWidget *sw; | |
340 GtkWidget *label; | |
341 GtkWidget *vbox; | |
342 GtkWidget *hbox; | |
343 GtkWidget *bbox; | |
344 GtkWidget *menu; | |
345 GtkWidget *sep; | |
346 GtkWidget *disclosure; | |
347 GtkTreeSelection *selection; | |
348 | |
349 g_return_val_if_fail(buddy != NULL, NULL); | |
350 | |
351 dialog = g_new0(GevoAssociateBuddyDialog, 1); | |
352 | |
353 dialog->buddy = buddy; | |
354 | |
355 dialog->win = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
356 gtk_window_set_role(GTK_WINDOW(dialog->win), "assoc_buddy"); | |
357 gtk_container_set_border_width(GTK_CONTAINER(dialog->win), 12); | |
358 | |
359 g_signal_connect(G_OBJECT(dialog->win), "delete_event", | |
360 G_CALLBACK(delete_win_cb), dialog); | |
361 | |
362 /* Setup the vbox */ | |
363 vbox = gtk_vbox_new(FALSE, 12); | |
364 gtk_container_add(GTK_CONTAINER(dialog->win), vbox); | |
365 gtk_widget_show(vbox); | |
366 | |
367 /* Add the label. */ | |
368 label = gtk_label_new(_("Select a person from your address book to " | |
369 "add this buddy to, or create a new person.")); | |
370 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE); | |
371 gtk_misc_set_alignment(GTK_MISC(label), 0, 0); | |
372 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, TRUE, 0); | |
373 gtk_widget_show(label); | |
374 | |
375 /* Add the search hbox */ | |
376 hbox = gtk_hbox_new(FALSE, 6); | |
377 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0); | |
378 gtk_widget_show(hbox); | |
379 | |
380 /* "Search" */ | |
381 label = gtk_label_new(_("Search")); | |
382 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0); | |
383 gtk_widget_show(label); | |
384 | |
385 /* Addressbooks */ | |
386 dialog->addressbooks_menu = gtk_option_menu_new(); | |
387 menu = gtk_menu_new(); | |
388 gtk_option_menu_set_menu(GTK_OPTION_MENU(dialog->addressbooks_menu), menu); | |
389 | |
390 populate_address_books(dialog); | |
391 | |
392 gtk_option_menu_set_history(GTK_OPTION_MENU(dialog->addressbooks_menu), 0); | |
393 | |
394 gtk_box_pack_start(GTK_BOX(hbox), dialog->addressbooks_menu, | |
395 FALSE, FALSE, 0); | |
396 gtk_widget_show(dialog->addressbooks_menu); | |
397 | |
398 /* Search field */ | |
399 dialog->search_field = gtk_entry_new(); | |
400 gtk_box_pack_start(GTK_BOX(hbox), dialog->search_field, TRUE, TRUE, 0); | |
401 gtk_widget_show(dialog->search_field); | |
402 | |
403 g_signal_connect(G_OBJECT(dialog->search_field), "changed", | |
404 G_CALLBACK(search_changed_cb), dialog); | |
405 | |
406 /* Clear button */ | |
407 button = gtk_button_new_from_stock(GTK_STOCK_CLEAR); | |
408 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
409 gtk_widget_show(button); | |
410 | |
411 g_signal_connect(G_OBJECT(button), "clicked", | |
412 G_CALLBACK(clear_cb), dialog); | |
413 | |
414 /* Scrolled Window */ | |
415 sw = gtk_scrolled_window_new(0, 0); | |
416 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), | |
417 GTK_POLICY_AUTOMATIC, | |
418 GTK_POLICY_ALWAYS); | |
419 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), | |
420 GTK_SHADOW_IN); | |
421 gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0); | |
422 gtk_widget_show(sw); | |
423 | |
424 /* Create the list model for the treeview. */ | |
425 dialog->model = gtk_list_store_new(NUM_COLUMNS, | |
426 G_TYPE_STRING, G_TYPE_POINTER); | |
427 | |
428 /* Now for the treeview */ | |
429 dialog->treeview = | |
430 gtk_tree_view_new_with_model(GTK_TREE_MODEL(dialog->model)); | |
431 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(dialog->treeview), TRUE); | |
432 gtk_container_add(GTK_CONTAINER(sw), dialog->treeview); | |
433 gtk_widget_show(dialog->treeview); | |
434 | |
435 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->treeview)); | |
436 | |
437 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE); | |
438 | |
439 g_signal_connect(G_OBJECT(selection), "changed", | |
440 G_CALLBACK(selected_cb), dialog); | |
441 | |
442 add_columns(dialog); | |
443 | |
444 populate_treeview(dialog); | |
445 | |
446 /* Add the disclosure */ | |
447 disclosure = gaim_disclosure_new(_("Show user details"), | |
448 _("Hide user details")); | |
449 gtk_box_pack_start(GTK_BOX(vbox), disclosure, FALSE, FALSE, 0); | |
450 gtk_widget_show(disclosure); | |
451 | |
452 /* | |
453 * User details | |
454 */ | |
455 | |
456 /* Scrolled Window */ | |
457 sw = gtk_scrolled_window_new(0, 0); | |
458 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw), | |
459 GTK_POLICY_NEVER, | |
460 GTK_POLICY_ALWAYS); | |
461 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(sw), | |
462 GTK_SHADOW_IN); | |
463 gtk_box_pack_start(GTK_BOX(vbox), sw, TRUE, TRUE, 0); | |
464 gaim_disclosure_set_container(GAIM_DISCLOSURE(disclosure), sw); | |
465 | |
466 /* Textview */ | |
467 dialog->imhtml = gtk_imhtml_new(NULL, NULL); | |
468 gtk_container_add(GTK_CONTAINER(sw), dialog->imhtml); | |
469 gtk_widget_show(dialog->imhtml); | |
470 | |
471 /* Separator. */ | |
472 sep = gtk_hseparator_new(); | |
473 gtk_box_pack_start(GTK_BOX(vbox), sep, FALSE, FALSE, 0); | |
474 gtk_widget_show(sep); | |
475 | |
476 /* Button box */ | |
477 bbox = gtk_hbutton_box_new(); | |
478 gtk_box_set_spacing(GTK_BOX(bbox), 6); | |
479 gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END); | |
480 gtk_box_pack_end(GTK_BOX(vbox), bbox, FALSE, TRUE, 0); | |
481 gtk_widget_show(bbox); | |
482 | |
483 /* "New Person" button */ | |
484 button = gaim_pixbuf_button_from_stock(_("New Person"), GTK_STOCK_NEW, | |
485 GAIM_BUTTON_HORIZONTAL); | |
486 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); | |
487 gtk_widget_show(button); | |
488 | |
489 g_signal_connect(G_OBJECT(button), "clicked", | |
490 G_CALLBACK(new_person_cb), dialog); | |
491 | |
492 /* "Cancel" button */ | |
493 button = gtk_button_new_from_stock(GTK_STOCK_CANCEL); | |
494 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); | |
495 gtk_widget_show(button); | |
496 | |
497 g_signal_connect(G_OBJECT(button), "clicked", | |
498 G_CALLBACK(cancel_cb), dialog); | |
499 | |
500 /* "Associate Buddy" button */ | |
501 button = gaim_pixbuf_button_from_stock(_("_Associate Buddy"), | |
502 GTK_STOCK_APPLY, | |
503 GAIM_BUTTON_HORIZONTAL); | |
504 dialog->assoc_button = button; | |
505 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0); | |
506 gtk_widget_set_sensitive(button, FALSE); | |
507 gtk_widget_show(button); | |
508 | |
509 g_signal_connect(G_OBJECT(button), "clicked", | |
510 G_CALLBACK(assoc_buddy_cb), dialog); | |
511 | |
512 /* Show it. */ | |
513 gtk_widget_show(dialog->win); | |
514 | |
515 return dialog; | |
516 } |