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