| |
1 /* |
| |
2 * Pidgin - Internet Messenger |
| |
3 * Copyright (C) Pidgin Developers <devel@pidgin.im> |
| |
4 * |
| |
5 * Pidgin is the legal property of its developers, whose names are too numerous |
| |
6 * to list here. Please refer to the COPYRIGHT file distributed with this |
| |
7 * source distribution. |
| |
8 * |
| |
9 * This program is free software; you can redistribute it and/or modify |
| |
10 * it under the terms of the GNU General Public License as published by |
| |
11 * the Free Software Foundation; either version 2 of the License, or |
| |
12 * (at your option) any later version. |
| |
13 * |
| |
14 * This program is distributed in the hope that it will be useful, |
| |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
17 * GNU General Public License for more details. |
| |
18 * |
| |
19 * You should have received a copy of the GNU General Public License |
| |
20 * along with this program; if not, see <https://www.gnu.org/licenses/>. |
| |
21 */ |
| |
22 |
| |
23 #include <glib/gi18n.h> |
| |
24 |
| |
25 #include "pidginaccountsenabledmenu.h" |
| |
26 |
| |
27 #include "pidgincore.h" |
| |
28 |
| |
29 /****************************************************************************** |
| |
30 * Helpers |
| |
31 *****************************************************************************/ |
| |
32 static GMenu * |
| |
33 pidgin_accounts_enabled_menu_build_submenu(PurpleAccount *account) { |
| |
34 GMenu *menu = NULL, *section = NULL; |
| |
35 gchar *action_id = NULL; |
| |
36 const gchar *account_id = NULL; |
| |
37 |
| |
38 menu = g_menu_new(); |
| |
39 account_id = purple_account_get_id(account); |
| |
40 |
| |
41 /* Add the "Edit Account" section. */ |
| |
42 section = g_menu_new(); |
| |
43 g_menu_append_section(menu, NULL, G_MENU_MODEL(section)); |
| |
44 |
| |
45 action_id = g_strdup_printf("app.edit-account::%s", account_id); |
| |
46 g_menu_append(section, _("Edit Account"), action_id); |
| |
47 g_free(action_id); |
| |
48 |
| |
49 /* Add the "Disable Account" section. */ |
| |
50 section = g_menu_new(); |
| |
51 g_menu_append_section(menu, NULL, G_MENU_MODEL(section)); |
| |
52 |
| |
53 action_id = g_strdup_printf("app.disable-account::%s", account_id); |
| |
54 g_menu_append(section, _("Disable"), action_id); |
| |
55 g_free(action_id); |
| |
56 |
| |
57 return menu; |
| |
58 } |
| |
59 |
| |
60 static void |
| |
61 pidgin_accounts_enabled_menu_refresh_helper(PurpleAccount *account, |
| |
62 gpointer data) |
| |
63 { |
| |
64 GMenu *menu = data; |
| |
65 |
| |
66 if(purple_account_get_enabled(account, PIDGIN_UI)) { |
| |
67 GMenu *submenu = NULL; |
| |
68 gchar *label = NULL; |
| |
69 const gchar *account_name = purple_account_get_username(account); |
| |
70 const gchar *protocol_name = purple_account_get_protocol_name(account); |
| |
71 |
| |
72 submenu = pidgin_accounts_enabled_menu_build_submenu(account); |
| |
73 |
| |
74 /* translators: This format string is intended to contain the account |
| |
75 * name followed by the protocol name to uniquely identify a specific |
| |
76 * account. |
| |
77 */ |
| |
78 label = g_strdup_printf(_("%s (%s)"), account_name, protocol_name); |
| |
79 |
| |
80 g_menu_append_submenu(menu, label, G_MENU_MODEL(submenu)); |
| |
81 |
| |
82 g_free(label); |
| |
83 } |
| |
84 } |
| |
85 |
| |
86 static void |
| |
87 pidgin_accounts_enabled_menu_refresh(GMenu *menu) { |
| |
88 PurpleAccountManager *manager = NULL; |
| |
89 |
| |
90 g_menu_remove_all(menu); |
| |
91 |
| |
92 manager = purple_account_manager_get_default(); |
| |
93 purple_account_manager_foreach(manager, |
| |
94 pidgin_accounts_enabled_menu_refresh_helper, |
| |
95 menu); |
| |
96 } |
| |
97 |
| |
98 /****************************************************************************** |
| |
99 * Callbacks |
| |
100 *****************************************************************************/ |
| |
101 static void |
| |
102 pidgin_accounts_enabled_menu_enabled_cb(G_GNUC_UNUSED PurpleAccount *account, |
| |
103 gpointer data) |
| |
104 { |
| |
105 pidgin_accounts_enabled_menu_refresh(data); |
| |
106 } |
| |
107 |
| |
108 static void |
| |
109 pidgin_accounts_enabled_menu_disabled_cb(G_GNUC_UNUSED PurpleAccount *account, |
| |
110 gpointer data) |
| |
111 { |
| |
112 pidgin_accounts_enabled_menu_refresh(data); |
| |
113 } |
| |
114 |
| |
115 static void |
| |
116 pidgin_accounts_enabled_menu_weak_notify_cb(G_GNUC_UNUSED gpointer data, |
| |
117 GObject *obj) |
| |
118 { |
| |
119 purple_signals_disconnect_by_handle(obj); |
| |
120 } |
| |
121 |
| |
122 /****************************************************************************** |
| |
123 * Public API |
| |
124 *****************************************************************************/ |
| |
125 GMenu * |
| |
126 pidgin_accounts_enabled_menu_new(void) { |
| |
127 GMenu *menu = NULL; |
| |
128 gpointer handle = NULL; |
| |
129 |
| |
130 /* Create the menu and set our instance as data on it so it'll be freed |
| |
131 * when the menu is destroyed. |
| |
132 */ |
| |
133 menu = g_menu_new(); |
| |
134 g_object_weak_ref(G_OBJECT(menu), |
| |
135 pidgin_accounts_enabled_menu_weak_notify_cb, NULL); |
| |
136 |
| |
137 /* Populate ourselves with any accounts that are already enabled. */ |
| |
138 pidgin_accounts_enabled_menu_refresh(menu); |
| |
139 |
| |
140 /* Wire up the purple signals we care about. */ |
| |
141 handle = purple_accounts_get_handle(); |
| |
142 purple_signal_connect(handle, "account-enabled", menu, |
| |
143 G_CALLBACK(pidgin_accounts_enabled_menu_enabled_cb), |
| |
144 menu); |
| |
145 purple_signal_connect(handle, "account-disabled", menu, |
| |
146 G_CALLBACK(pidgin_accounts_enabled_menu_disabled_cb), |
| |
147 menu); |
| |
148 |
| |
149 return menu; |
| |
150 } |