| 18 * |
18 * |
| 19 * You should have received a copy of the GNU General Public License |
19 * You should have received a copy of the GNU General Public License |
| 20 * along with this program; if not, write to the Free Software |
20 * along with this program; if not, write to the Free Software |
| 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 22 */ |
22 */ |
| |
23 #include <glib/gi18n-lib.h> |
| |
24 |
| 23 #include <gtk/gtk.h> |
25 #include <gtk/gtk.h> |
| 24 |
26 |
| 25 #include "pidginaccountchooser.h" |
27 #include "pidginaccountchooser.h" |
| 26 |
28 |
| 27 #include "pidginaccountstore.h" |
|
| 28 |
|
| 29 struct _PidginAccountChooser { |
29 struct _PidginAccountChooser { |
| 30 GtkComboBox parent; |
30 AdwBin parent; |
| 31 |
31 |
| 32 GtkFilter *filter; |
32 GtkDropDown *chooser; |
| |
33 GtkFilterListModel *filter; |
| 33 }; |
34 }; |
| 34 |
35 |
| 35 enum |
36 enum |
| 36 { |
37 { |
| 37 PROP_0, |
38 PROP_0, |
| 43 static GParamSpec *properties[PROP_LAST] = {NULL}; |
44 static GParamSpec *properties[PROP_LAST] = {NULL}; |
| 44 |
45 |
| 45 /****************************************************************************** |
46 /****************************************************************************** |
| 46 * Callbacks |
47 * Callbacks |
| 47 *****************************************************************************/ |
48 *****************************************************************************/ |
| 48 static void |
49 static char * |
| 49 pidgin_account_chooser_changed_cb(GtkComboBox *widget, gpointer user_data) { |
50 pidgin_account_chooser_icon_name_cb(G_GNUC_UNUSED GObject *self, |
| 50 g_object_notify_by_pspec(G_OBJECT(widget), properties[PROP_ACCOUNT]); |
51 PurpleAccount *account, |
| |
52 G_GNUC_UNUSED gpointer data) |
| |
53 { |
| |
54 const char *icon_name = NULL; |
| |
55 |
| |
56 if(PURPLE_IS_ACCOUNT(account)) { |
| |
57 PurpleProtocol *protocol = purple_account_get_protocol(account); |
| |
58 icon_name = purple_protocol_get_icon_name(protocol); |
| |
59 } |
| |
60 |
| |
61 return g_strdup(icon_name); |
| |
62 } |
| |
63 |
| |
64 static char * |
| |
65 pidgin_account_chooser_label_cb(G_GNUC_UNUSED GObject *self, |
| |
66 PurpleAccount *account, |
| |
67 G_GNUC_UNUSED gpointer data) |
| |
68 { |
| |
69 gchar *markup = NULL; |
| |
70 const gchar *alias = NULL; |
| |
71 |
| |
72 if(!PURPLE_IS_ACCOUNT(account)) { |
| |
73 return NULL; |
| |
74 } |
| |
75 |
| |
76 alias = purple_account_get_private_alias(account); |
| |
77 if(alias != NULL) { |
| |
78 markup = g_strdup_printf(_("%s (%s) (%s)"), |
| |
79 purple_account_get_username(account), |
| |
80 alias, |
| |
81 purple_account_get_protocol_name(account)); |
| |
82 } else { |
| |
83 markup = g_strdup_printf(_("%s (%s)"), |
| |
84 purple_account_get_username(account), |
| |
85 purple_account_get_protocol_name(account)); |
| |
86 } |
| |
87 |
| |
88 return markup; |
| |
89 } |
| |
90 |
| |
91 static void |
| |
92 pidgin_account_chooser_changed_cb(G_GNUC_UNUSED GObject *obj, |
| |
93 G_GNUC_UNUSED GParamSpec *pspec, |
| |
94 gpointer data) |
| |
95 { |
| |
96 g_object_notify_by_pspec(G_OBJECT(data), properties[PROP_ACCOUNT]); |
| 51 } |
97 } |
| 52 |
98 |
| 53 /****************************************************************************** |
99 /****************************************************************************** |
| 54 * GObject implementation |
100 * GObject implementation |
| 55 *****************************************************************************/ |
101 *****************************************************************************/ |
| 56 G_DEFINE_TYPE(PidginAccountChooser, pidgin_account_chooser, GTK_TYPE_COMBO_BOX) |
102 G_DEFINE_TYPE(PidginAccountChooser, pidgin_account_chooser, ADW_TYPE_BIN) |
| 57 |
103 |
| 58 static void |
104 static void |
| 59 pidgin_account_chooser_get_property(GObject *object, guint prop_id, |
105 pidgin_account_chooser_get_property(GObject *object, guint prop_id, |
| 60 GValue *value, GParamSpec *pspec) |
106 GValue *value, GParamSpec *pspec) |
| 61 { |
107 { |
| 119 g_object_class_install_properties(obj_class, PROP_LAST, properties); |
165 g_object_class_install_properties(obj_class, PROP_LAST, properties); |
| 120 |
166 |
| 121 /* Widget template */ |
167 /* Widget template */ |
| 122 gtk_widget_class_set_template_from_resource( |
168 gtk_widget_class_set_template_from_resource( |
| 123 widget_class, "/im/pidgin/Pidgin3/Accounts/chooser.ui"); |
169 widget_class, "/im/pidgin/Pidgin3/Accounts/chooser.ui"); |
| |
170 |
| |
171 gtk_widget_class_bind_template_child(widget_class, PidginAccountChooser, |
| |
172 chooser); |
| |
173 gtk_widget_class_bind_template_child(widget_class, PidginAccountChooser, |
| |
174 filter); |
| |
175 |
| |
176 gtk_widget_class_bind_template_callback(widget_class, |
| |
177 pidgin_account_chooser_icon_name_cb); |
| |
178 gtk_widget_class_bind_template_callback(widget_class, |
| |
179 pidgin_account_chooser_label_cb); |
| |
180 gtk_widget_class_bind_template_callback(widget_class, |
| |
181 pidgin_account_chooser_changed_cb); |
| 124 } |
182 } |
| 125 |
183 |
| 126 static void |
184 static void |
| 127 pidgin_account_chooser_init(PidginAccountChooser *chooser) { |
185 pidgin_account_chooser_init(PidginAccountChooser *chooser) { |
| |
186 GListModel *model = NULL; |
| |
187 |
| 128 gtk_widget_init_template(GTK_WIDGET(chooser)); |
188 gtk_widget_init_template(GTK_WIDGET(chooser)); |
| 129 |
189 |
| 130 /* this callback emits the notify for the account property */ |
190 model = purple_account_manager_get_default_as_model(); |
| 131 g_signal_connect(chooser, "changed", |
191 gtk_filter_list_model_set_model(chooser->filter, model); |
| 132 G_CALLBACK(pidgin_account_chooser_changed_cb), NULL); |
|
| 133 } |
192 } |
| 134 |
193 |
| 135 /****************************************************************************** |
194 /****************************************************************************** |
| 136 * Public API |
195 * Public API |
| 137 *****************************************************************************/ |
196 *****************************************************************************/ |
| 146 |
205 |
| 147 GtkFilter * |
206 GtkFilter * |
| 148 pidgin_account_chooser_get_filter(PidginAccountChooser *chooser) { |
207 pidgin_account_chooser_get_filter(PidginAccountChooser *chooser) { |
| 149 g_return_val_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser), NULL); |
208 g_return_val_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser), NULL); |
| 150 |
209 |
| 151 return chooser->filter; |
210 return gtk_filter_list_model_get_filter(chooser->filter); |
| 152 } |
211 } |
| 153 |
212 |
| 154 void |
213 void |
| 155 pidgin_account_chooser_set_filter(PidginAccountChooser *chooser, |
214 pidgin_account_chooser_set_filter(PidginAccountChooser *chooser, |
| 156 GtkFilter *filter) |
215 GtkFilter *filter) |
| 157 { |
216 { |
| 158 g_return_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser)); |
217 g_return_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser)); |
| 159 |
218 |
| 160 if(g_set_object(&chooser->filter, filter)) { |
219 gtk_filter_list_model_set_filter(chooser->filter, filter); |
| 161 g_object_notify_by_pspec(G_OBJECT(chooser), properties[PROP_FILTER]); |
220 g_object_notify_by_pspec(G_OBJECT(chooser), properties[PROP_FILTER]); |
| 162 } |
|
| 163 } |
221 } |
| 164 |
222 |
| 165 PurpleAccount * |
223 PurpleAccount * |
| 166 pidgin_account_chooser_get_selected(PidginAccountChooser *chooser) { |
224 pidgin_account_chooser_get_selected(PidginAccountChooser *chooser) { |
| 167 GtkTreeIter iter; |
|
| 168 gpointer account = NULL; |
|
| 169 |
|
| 170 g_return_val_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser), NULL); |
225 g_return_val_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser), NULL); |
| 171 |
226 |
| 172 if(gtk_combo_box_get_active_iter(GTK_COMBO_BOX(chooser), &iter)) { |
227 return gtk_drop_down_get_selected_item(chooser->chooser); |
| 173 GtkTreeModel *model = gtk_combo_box_get_model(GTK_COMBO_BOX(chooser)); |
|
| 174 gtk_tree_model_get(model, &iter, |
|
| 175 PIDGIN_ACCOUNT_STORE_COLUMN_ACCOUNT, &account, -1); |
|
| 176 } |
|
| 177 |
|
| 178 return account; |
|
| 179 } |
228 } |
| 180 |
229 |
| 181 void |
230 void |
| 182 pidgin_account_chooser_set_selected(PidginAccountChooser *chooser, |
231 pidgin_account_chooser_set_selected(PidginAccountChooser *chooser, |
| 183 PurpleAccount *account) |
232 PurpleAccount *account) |
| 184 { |
233 { |
| 185 GtkTreeModel *model = NULL; |
234 GListModel *model = NULL; |
| 186 GtkTreeIter iter; |
235 guint n_items = 0; |
| 187 PurpleAccount *acc = NULL; |
|
| 188 |
236 |
| 189 g_return_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser)); |
237 g_return_if_fail(PIDGIN_IS_ACCOUNT_CHOOSER(chooser)); |
| 190 |
238 |
| 191 model = gtk_combo_box_get_model(GTK_COMBO_BOX(chooser)); |
239 model = gtk_drop_down_get_model(chooser->chooser); |
| 192 g_return_if_fail(GTK_IS_TREE_MODEL(model)); |
240 g_return_if_fail(G_IS_LIST_MODEL(model)); |
| 193 |
241 |
| 194 if(gtk_tree_model_get_iter_first(model, &iter)) { |
242 n_items = g_list_model_get_n_items(model); |
| 195 do { |
243 for(guint position = 0; position < n_items; position++) { |
| 196 gtk_tree_model_get(model, &iter, |
244 PurpleAccount *acc = g_list_model_get_item(model, position); |
| 197 PIDGIN_ACCOUNT_STORE_COLUMN_ACCOUNT, &acc, -1); |
245 |
| 198 if(acc == account) { |
246 if(acc == account) { |
| 199 /* NOTE: Property notification occurs in 'changed' signal |
247 /* NOTE: Property notification occurs in 'changed' signal |
| 200 * callback. |
248 * callback. |
| 201 */ |
249 */ |
| 202 gtk_combo_box_set_active_iter(GTK_COMBO_BOX(chooser), &iter); |
250 gtk_drop_down_set_selected(chooser->chooser, position); |
| 203 |
251 |
| 204 g_object_unref(G_OBJECT(acc)); |
252 g_object_unref(acc); |
| 205 |
253 |
| 206 return; |
254 return; |
| 207 } |
255 } |
| 208 g_object_unref(G_OBJECT(acc)); |
256 |
| 209 } while(gtk_tree_model_iter_next(model, &iter)); |
257 g_object_unref(acc); |
| 210 } |
258 } |
| 211 } |
259 } |
| 212 |
|