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