Open an explorer.exe window at the location of the file when clicking
[pidgin-git.git] / pidgin / gtkprivacy.c
blob78f7278f1344e7ab2bd062d4c51ccb3b4b534b1a
1 /**
2 * @file gtkprivacy.c GTK+ Privacy UI
3 * @ingroup pidgin
4 */
6 /* pidgin
8 * Pidgin is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "internal.h"
27 #include "pidgin.h"
29 #include "connection.h"
30 #include "debug.h"
31 #include "privacy.h"
32 #include "request.h"
33 #include "util.h"
35 #include "gtkblist.h"
36 #include "gtkprivacy.h"
37 #include "gtkutils.h"
39 typedef struct
41 GtkWidget *win;
43 GtkWidget *type_menu;
45 GtkWidget *add_button;
46 GtkWidget *remove_button;
47 GtkWidget *removeall_button;
48 GtkWidget *close_button;
50 GtkWidget *button_box;
51 GtkWidget *allow_widget;
52 GtkWidget *block_widget;
54 GtkListStore *allow_store;
55 GtkListStore *block_store;
57 GtkWidget *allow_list;
58 GtkWidget *block_list;
60 gboolean in_allow_list;
62 PurpleAccount *account;
64 } PidginPrivacyDialog;
66 typedef struct
68 PurpleAccount *account;
69 char *name;
70 gboolean block;
72 } PidginPrivacyRequestData;
74 static struct
76 const char *text;
77 int num;
79 } const menu_entries[] =
81 { N_("Allow all users to contact me"), PURPLE_PRIVACY_ALLOW_ALL },
82 { N_("Allow only the users on my buddy list"), PURPLE_PRIVACY_ALLOW_BUDDYLIST },
83 { N_("Allow only the users below"), PURPLE_PRIVACY_ALLOW_USERS },
84 { N_("Block all users"), PURPLE_PRIVACY_DENY_ALL },
85 { N_("Block only the users below"), PURPLE_PRIVACY_DENY_USERS }
88 static const size_t menu_entry_count = sizeof(menu_entries) / sizeof(*menu_entries);
90 static PidginPrivacyDialog *privacy_dialog = NULL;
92 static void
93 rebuild_allow_list(PidginPrivacyDialog *dialog)
95 GSList *l;
96 GtkTreeIter iter;
98 gtk_list_store_clear(dialog->allow_store);
100 for (l = dialog->account->permit; l != NULL; l = l->next) {
101 gtk_list_store_append(dialog->allow_store, &iter);
102 gtk_list_store_set(dialog->allow_store, &iter, 0, l->data, -1);
106 static void
107 rebuild_block_list(PidginPrivacyDialog *dialog)
109 GSList *l;
110 GtkTreeIter iter;
112 gtk_list_store_clear(dialog->block_store);
114 for (l = dialog->account->deny; l != NULL; l = l->next) {
115 gtk_list_store_append(dialog->block_store, &iter);
116 gtk_list_store_set(dialog->block_store, &iter, 0, l->data, -1);
120 static void
121 user_selected_cb(GtkTreeSelection *sel, PidginPrivacyDialog *dialog)
123 gtk_widget_set_sensitive(dialog->remove_button, TRUE);
126 static GtkWidget *
127 build_list(PidginPrivacyDialog *dialog, GtkListStore *model,
128 GtkWidget **ret_treeview)
130 GtkWidget *sw;
131 GtkWidget *treeview;
132 GtkCellRenderer *rend;
133 GtkTreeViewColumn *column;
134 GtkTreeSelection *sel;
136 treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
137 *ret_treeview = treeview;
139 rend = gtk_cell_renderer_text_new();
141 column = gtk_tree_view_column_new_with_attributes(NULL, rend,
142 "text", 0,
143 NULL);
144 gtk_tree_view_column_set_clickable(GTK_TREE_VIEW_COLUMN(column), TRUE);
145 gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
146 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
147 sw = pidgin_make_scrollable(treeview, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC, GTK_SHADOW_IN, -1, 200);
149 gtk_widget_show(treeview);
151 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
153 g_signal_connect(G_OBJECT(sel), "changed",
154 G_CALLBACK(user_selected_cb), dialog);
156 return sw;
159 static GtkWidget *
160 build_allow_list(PidginPrivacyDialog *dialog)
162 GtkWidget *widget;
163 GtkWidget *list;
165 dialog->allow_store = gtk_list_store_new(1, G_TYPE_STRING);
167 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dialog->allow_store), 0, GTK_SORT_ASCENDING);
169 widget = build_list(dialog, dialog->allow_store, &list);
171 dialog->allow_list = list;
173 rebuild_allow_list(dialog);
175 return widget;
178 static GtkWidget *
179 build_block_list(PidginPrivacyDialog *dialog)
181 GtkWidget *widget;
182 GtkWidget *list;
184 dialog->block_store = gtk_list_store_new(1, G_TYPE_STRING);
186 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dialog->block_store), 0, GTK_SORT_ASCENDING);
188 widget = build_list(dialog, dialog->block_store, &list);
190 dialog->block_list = list;
192 rebuild_block_list(dialog);
194 return widget;
197 static gint
198 destroy_cb(GtkWidget *w, GdkEvent *event, PidginPrivacyDialog *dialog)
200 pidgin_privacy_dialog_hide();
202 return 0;
205 static void
206 select_account_cb(GtkWidget *dropdown, PurpleAccount *account,
207 PidginPrivacyDialog *dialog)
209 int i;
211 dialog->account = account;
213 for (i = 0; i < menu_entry_count; i++) {
214 if (menu_entries[i].num == account->perm_deny) {
215 gtk_combo_box_set_active(GTK_COMBO_BOX(dialog->type_menu), i);
216 break;
220 rebuild_allow_list(dialog);
221 rebuild_block_list(dialog);
225 * TODO: Setting the permit/deny setting needs to go through privacy.c
226 * Even better: the privacy API needs to not suck.
228 static void
229 type_changed_cb(GtkComboBox *combo, PidginPrivacyDialog *dialog)
231 int new_type = menu_entries[gtk_combo_box_get_active(combo)].num;
233 dialog->account->perm_deny = new_type;
234 serv_set_permit_deny(purple_account_get_connection(dialog->account));
236 gtk_widget_hide(dialog->allow_widget);
237 gtk_widget_hide(dialog->block_widget);
238 gtk_widget_hide_all(dialog->button_box);
240 if (new_type == PURPLE_PRIVACY_ALLOW_USERS) {
241 gtk_widget_show(dialog->allow_widget);
242 gtk_widget_show_all(dialog->button_box);
243 dialog->in_allow_list = TRUE;
245 else if (new_type == PURPLE_PRIVACY_DENY_USERS) {
246 gtk_widget_show(dialog->block_widget);
247 gtk_widget_show_all(dialog->button_box);
248 dialog->in_allow_list = FALSE;
251 gtk_widget_show_all(dialog->close_button);
252 gtk_widget_show(dialog->button_box);
254 purple_blist_schedule_save();
255 pidgin_blist_refresh(purple_get_blist());
258 static void
259 add_cb(GtkWidget *button, PidginPrivacyDialog *dialog)
261 if (dialog->in_allow_list)
262 pidgin_request_add_permit(dialog->account, NULL);
263 else
264 pidgin_request_add_block(dialog->account, NULL);
267 static void
268 remove_cb(GtkWidget *button, PidginPrivacyDialog *dialog)
270 GtkTreeIter iter;
271 GtkTreeModel *model;
272 GtkTreeSelection *sel;
273 char *name;
275 if (dialog->in_allow_list && dialog->allow_store == NULL)
276 return;
278 if (!dialog->in_allow_list && dialog->block_store == NULL)
279 return;
281 if (dialog->in_allow_list) {
282 model = GTK_TREE_MODEL(dialog->allow_store);
283 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->allow_list));
285 else {
286 model = GTK_TREE_MODEL(dialog->block_store);
287 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->block_list));
290 if (gtk_tree_selection_get_selected(sel, NULL, &iter))
291 gtk_tree_model_get(model, &iter, 0, &name, -1);
292 else
293 return;
295 if (dialog->in_allow_list)
296 purple_privacy_permit_remove(dialog->account, name, FALSE);
297 else
298 purple_privacy_deny_remove(dialog->account, name, FALSE);
300 g_free(name);
303 static void
304 removeall_cb(GtkWidget *button, PidginPrivacyDialog *dialog)
306 GSList *l;
307 if (dialog->in_allow_list)
308 l = dialog->account->permit;
309 else
310 l = dialog->account->deny;
311 while (l) {
312 char *user;
313 user = l->data;
314 l = l->next;
315 if (dialog->in_allow_list)
316 purple_privacy_permit_remove(dialog->account, user, FALSE);
317 else
318 purple_privacy_deny_remove(dialog->account, user, FALSE);
322 static void
323 close_cb(GtkWidget *button, PidginPrivacyDialog *dialog)
325 gtk_widget_destroy(dialog->win);
327 pidgin_privacy_dialog_hide();
330 static PidginPrivacyDialog *
331 privacy_dialog_new(void)
333 PidginPrivacyDialog *dialog;
334 GtkWidget *vbox;
335 GtkWidget *button;
336 GtkWidget *dropdown;
337 GtkWidget *label;
338 int selected = -1;
339 int i;
341 dialog = g_new0(PidginPrivacyDialog, 1);
343 dialog->win = pidgin_create_dialog(_("Privacy"), PIDGIN_HIG_BORDER, "privacy", TRUE);
345 g_signal_connect(G_OBJECT(dialog->win), "delete_event",
346 G_CALLBACK(destroy_cb), dialog);
348 /* Main vbox */
349 vbox = pidgin_dialog_get_vbox_with_properties(GTK_DIALOG(dialog->win), FALSE, PIDGIN_HIG_BORDER);
351 /* Description label */
352 label = gtk_label_new(
353 _("Changes to privacy settings take effect immediately."));
355 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
356 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
357 gtk_widget_show(label);
359 /* Accounts drop-down */
360 dropdown = pidgin_account_option_menu_new(NULL, FALSE,
361 G_CALLBACK(select_account_cb), NULL, dialog);
362 pidgin_add_widget_to_vbox(GTK_BOX(vbox), _("Set privacy for:"), NULL, dropdown, TRUE, NULL);
363 dialog->account = pidgin_account_option_menu_get_selected(dropdown);
365 /* Add the drop-down list with the allow/block types. */
366 dialog->type_menu = gtk_combo_box_new_text();
367 gtk_box_pack_start(GTK_BOX(vbox), dialog->type_menu, FALSE, FALSE, 0);
368 gtk_widget_show(dialog->type_menu);
370 for (i = 0; i < menu_entry_count; i++) {
371 gtk_combo_box_append_text(GTK_COMBO_BOX(dialog->type_menu),
372 _(menu_entries[i].text));
374 if (menu_entries[i].num == dialog->account->perm_deny)
375 selected = i;
378 gtk_combo_box_set_active(GTK_COMBO_BOX(dialog->type_menu), selected);
380 g_signal_connect(G_OBJECT(dialog->type_menu), "changed",
381 G_CALLBACK(type_changed_cb), dialog);
383 /* Build the treeview for the allow list. */
384 dialog->allow_widget = build_allow_list(dialog);
385 gtk_box_pack_start(GTK_BOX(vbox), dialog->allow_widget, TRUE, TRUE, 0);
387 /* Build the treeview for the block list. */
388 dialog->block_widget = build_block_list(dialog);
389 gtk_box_pack_start(GTK_BOX(vbox), dialog->block_widget, TRUE, TRUE, 0);
391 /* Add the button box for Add, Remove, Remove All */
392 dialog->button_box = pidgin_dialog_get_action_area(GTK_DIALOG(dialog->win));
394 /* Add button */
395 button = pidgin_dialog_add_button(GTK_DIALOG(dialog->win), GTK_STOCK_ADD, G_CALLBACK(add_cb), dialog);
396 dialog->add_button = button;
398 /* Remove button */
399 button = pidgin_dialog_add_button(GTK_DIALOG(dialog->win), GTK_STOCK_REMOVE, G_CALLBACK(remove_cb), dialog);
400 dialog->remove_button = button;
401 /* TODO: This button should be sensitive/invisitive more cleverly */
402 gtk_widget_set_sensitive(button, FALSE);
404 /* Remove All button */
405 button = pidgin_dialog_add_button(GTK_DIALOG(dialog->win), _("Remove Al_l"), G_CALLBACK(removeall_cb), dialog);
406 dialog->removeall_button = button;
408 /* Close button */
409 button = pidgin_dialog_add_button(GTK_DIALOG(dialog->win), GTK_STOCK_CLOSE, G_CALLBACK(close_cb), dialog);
410 dialog->close_button = button;
412 type_changed_cb(GTK_COMBO_BOX(dialog->type_menu), dialog);
413 #if 0
414 if (dialog->account->perm_deny == PURPLE_PRIVACY_ALLOW_USERS) {
415 gtk_widget_show(dialog->allow_widget);
416 gtk_widget_show(dialog->button_box);
417 dialog->in_allow_list = TRUE;
419 else if (dialog->account->perm_deny == PURPLE_PRIVACY_DENY_USERS) {
420 gtk_widget_show(dialog->block_widget);
421 gtk_widget_show(dialog->button_box);
422 dialog->in_allow_list = FALSE;
424 #endif
425 return dialog;
428 void
429 pidgin_privacy_dialog_show(void)
431 g_return_if_fail(purple_connections_get_all() != NULL);
433 if (privacy_dialog == NULL)
434 privacy_dialog = privacy_dialog_new();
436 gtk_widget_show(privacy_dialog->win);
437 gdk_window_raise(privacy_dialog->win->window);
440 void
441 pidgin_privacy_dialog_hide(void)
443 if (privacy_dialog == NULL)
444 return;
446 g_object_unref(G_OBJECT(privacy_dialog->allow_store));
447 g_object_unref(G_OBJECT(privacy_dialog->block_store));
448 g_free(privacy_dialog);
449 privacy_dialog = NULL;
452 static void
453 destroy_request_data(PidginPrivacyRequestData *data)
455 g_free(data->name);
456 g_free(data);
459 static void
460 confirm_permit_block_cb(PidginPrivacyRequestData *data, int option)
462 if (data->block)
463 purple_privacy_deny(data->account, data->name, FALSE, FALSE);
464 else
465 purple_privacy_allow(data->account, data->name, FALSE, FALSE);
467 destroy_request_data(data);
470 static void
471 add_permit_block_cb(PidginPrivacyRequestData *data, const char *name)
473 data->name = g_strdup(name);
475 confirm_permit_block_cb(data, 0);
478 void
479 pidgin_request_add_permit(PurpleAccount *account, const char *name)
481 PidginPrivacyRequestData *data;
483 g_return_if_fail(account != NULL);
485 data = g_new0(PidginPrivacyRequestData, 1);
486 data->account = account;
487 data->name = g_strdup(name);
488 data->block = FALSE;
490 if (name == NULL) {
491 purple_request_input(account, _("Permit User"),
492 _("Type a user you permit to contact you."),
493 _("Please enter the name of the user you wish to be "
494 "able to contact you."),
495 NULL, FALSE, FALSE, NULL,
496 _("_Permit"), G_CALLBACK(add_permit_block_cb),
497 _("Cancel"), G_CALLBACK(destroy_request_data),
498 account, name, NULL,
499 data);
501 else {
502 char *primary = g_strdup_printf(_("Allow %s to contact you?"), name);
503 char *secondary =
504 g_strdup_printf(_("Are you sure you wish to allow "
505 "%s to contact you?"), name);
508 purple_request_action(account, _("Permit User"), primary, secondary,
510 account, name, NULL,
511 data, 2,
512 _("_Permit"), G_CALLBACK(confirm_permit_block_cb),
513 _("Cancel"), G_CALLBACK(destroy_request_data));
515 g_free(primary);
516 g_free(secondary);
520 void
521 pidgin_request_add_block(PurpleAccount *account, const char *name)
523 PidginPrivacyRequestData *data;
525 g_return_if_fail(account != NULL);
527 data = g_new0(PidginPrivacyRequestData, 1);
528 data->account = account;
529 data->name = g_strdup(name);
530 data->block = TRUE;
532 if (name == NULL) {
533 purple_request_input(account, _("Block User"),
534 _("Type a user to block."),
535 _("Please enter the name of the user you wish to block."),
536 NULL, FALSE, FALSE, NULL,
537 _("_Block"), G_CALLBACK(add_permit_block_cb),
538 _("Cancel"), G_CALLBACK(destroy_request_data),
539 account, name, NULL,
540 data);
542 else {
543 char *primary = g_strdup_printf(_("Block %s?"), name);
544 char *secondary =
545 g_strdup_printf(_("Are you sure you want to block %s?"), name);
547 purple_request_action(account, _("Block User"), primary, secondary,
549 account, name, NULL,
550 data, 2,
551 _("_Block"), G_CALLBACK(confirm_permit_block_cb),
552 _("Cancel"), G_CALLBACK(destroy_request_data));
554 g_free(primary);
555 g_free(secondary);
559 static void
560 pidgin_permit_added_removed(PurpleAccount *account, const char *name)
562 if (privacy_dialog != NULL)
563 rebuild_allow_list(privacy_dialog);
566 static void
567 pidgin_deny_added_removed(PurpleAccount *account, const char *name)
569 if (privacy_dialog != NULL)
570 rebuild_block_list(privacy_dialog);
573 static PurplePrivacyUiOps privacy_ops =
575 pidgin_permit_added_removed,
576 pidgin_permit_added_removed,
577 pidgin_deny_added_removed,
578 pidgin_deny_added_removed,
579 NULL,
580 NULL,
581 NULL,
582 NULL
585 PurplePrivacyUiOps *
586 pidgin_privacy_get_ui_ops(void)
588 return &privacy_ops;
591 void
592 pidgin_privacy_init(void)