| |
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-lib.h> |
| |
24 |
| |
25 #include <purple.h> |
| |
26 |
| |
27 #include "pidgin/pidginaccountrow.h" |
| |
28 |
| |
29 struct _PidginAccountRow { |
| |
30 AdwComboRow parent; |
| |
31 |
| |
32 GtkFilterListModel *filter; |
| |
33 }; |
| |
34 |
| |
35 enum { |
| |
36 PROP_0, |
| |
37 PROP_ACCOUNT, |
| |
38 PROP_FILTER, |
| |
39 N_PROPERTIES, |
| |
40 }; |
| |
41 static GParamSpec *properties[N_PROPERTIES] = { NULL, }; |
| |
42 |
| |
43 G_DEFINE_TYPE(PidginAccountRow, pidgin_account_row, ADW_TYPE_COMBO_ROW) |
| |
44 |
| |
45 /****************************************************************************** |
| |
46 * Callbacks |
| |
47 *****************************************************************************/ |
| |
48 static void |
| |
49 pidgin_account_row_changed_cb(G_GNUC_UNUSED GObject *obj, |
| |
50 G_GNUC_UNUSED GParamSpec *pspec, |
| |
51 gpointer data) |
| |
52 { |
| |
53 g_object_notify_by_pspec(G_OBJECT(data), properties[PROP_ACCOUNT]); |
| |
54 } |
| |
55 |
| |
56 /****************************************************************************** |
| |
57 * GObject Implementation |
| |
58 *****************************************************************************/ |
| |
59 static void |
| |
60 pidgin_account_row_get_property(GObject *obj, guint param_id, GValue *value, |
| |
61 GParamSpec *pspec) |
| |
62 { |
| |
63 PidginAccountRow *row = PIDGIN_ACCOUNT_ROW(obj); |
| |
64 |
| |
65 switch(param_id) { |
| |
66 case PROP_ACCOUNT: |
| |
67 g_value_set_object(value, pidgin_account_row_get_account(row)); |
| |
68 break; |
| |
69 case PROP_FILTER: |
| |
70 g_value_set_object(value, pidgin_account_row_get_filter(row)); |
| |
71 break; |
| |
72 default: |
| |
73 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); |
| |
74 break; |
| |
75 } |
| |
76 } |
| |
77 |
| |
78 static void |
| |
79 pidgin_account_row_set_property(GObject *obj, guint param_id, |
| |
80 const GValue *value, GParamSpec *pspec) |
| |
81 { |
| |
82 PidginAccountRow *row = PIDGIN_ACCOUNT_ROW(obj); |
| |
83 |
| |
84 switch(param_id) { |
| |
85 case PROP_ACCOUNT: |
| |
86 pidgin_account_row_set_account(row, g_value_get_object(value)); |
| |
87 break; |
| |
88 case PROP_FILTER: |
| |
89 pidgin_account_row_set_filter(row, g_value_get_object(value)); |
| |
90 break; |
| |
91 default: |
| |
92 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec); |
| |
93 break; |
| |
94 } |
| |
95 } |
| |
96 |
| |
97 static void |
| |
98 pidgin_account_row_init(PidginAccountRow *row) { |
| |
99 GListModel *model = NULL; |
| |
100 |
| |
101 gtk_widget_init_template(GTK_WIDGET(row)); |
| |
102 |
| |
103 model = purple_account_manager_get_default_as_model(); |
| |
104 gtk_filter_list_model_set_model(row->filter, model); |
| |
105 } |
| |
106 |
| |
107 static void |
| |
108 pidgin_account_row_class_init(PidginAccountRowClass *klass) { |
| |
109 GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
| |
110 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| |
111 |
| |
112 obj_class->get_property = pidgin_account_row_get_property; |
| |
113 obj_class->set_property = pidgin_account_row_set_property; |
| |
114 |
| |
115 /** |
| |
116 * PidginAccountRow:account: |
| |
117 * |
| |
118 * The [class@Purple.Account] that is selected. |
| |
119 * |
| |
120 * Since: 3.0.0 |
| |
121 */ |
| |
122 properties[PROP_ACCOUNT] = g_param_spec_object( |
| |
123 "account", "account", |
| |
124 "The account that is selected.", |
| |
125 PURPLE_TYPE_ACCOUNT, |
| |
126 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| |
127 |
| |
128 /** |
| |
129 * PidginAccountRow:filter: |
| |
130 * |
| |
131 * The filter to use on the list of accounts. |
| |
132 * |
| |
133 * Since: 3.0.0 |
| |
134 */ |
| |
135 properties[PROP_FILTER] = g_param_spec_object( |
| |
136 "filter", "filter", |
| |
137 "The filter to be applied on the list of accounts.", |
| |
138 GTK_TYPE_FILTER, |
| |
139 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| |
140 g_object_class_install_properties(obj_class, N_PROPERTIES, properties); |
| |
141 |
| |
142 gtk_widget_class_set_template_from_resource( |
| |
143 widget_class, |
| |
144 "/im/pidgin/Pidgin3/account-row.ui" |
| |
145 ); |
| |
146 |
| |
147 gtk_widget_class_bind_template_child(widget_class, PidginAccountRow, |
| |
148 filter); |
| |
149 |
| |
150 gtk_widget_class_bind_template_callback(widget_class, |
| |
151 pidgin_account_row_changed_cb); |
| |
152 } |
| |
153 |
| |
154 /****************************************************************************** |
| |
155 * API |
| |
156 *****************************************************************************/ |
| |
157 GtkWidget * |
| |
158 pidgin_account_row_new(void) { |
| |
159 return g_object_new(PIDGIN_TYPE_ACCOUNT_ROW, NULL); |
| |
160 } |
| |
161 |
| |
162 PurpleAccount * |
| |
163 pidgin_account_row_get_account(PidginAccountRow *row) { |
| |
164 g_return_val_if_fail(PIDGIN_IS_ACCOUNT_ROW(row), NULL); |
| |
165 |
| |
166 return adw_combo_row_get_selected_item(ADW_COMBO_ROW(row)); |
| |
167 } |
| |
168 |
| |
169 void |
| |
170 pidgin_account_row_set_account(PidginAccountRow *row, PurpleAccount *account) { |
| |
171 GListModel *model = NULL; |
| |
172 guint n_items = 0; |
| |
173 |
| |
174 g_return_if_fail(PIDGIN_IS_ACCOUNT_ROW(row)); |
| |
175 |
| |
176 model = adw_combo_row_get_model(ADW_COMBO_ROW(row)); |
| |
177 g_return_if_fail(G_IS_LIST_MODEL(model)); |
| |
178 |
| |
179 n_items = g_list_model_get_n_items(model); |
| |
180 for(guint position = 0; position < n_items; position++) { |
| |
181 PurpleAccount *account1 = g_list_model_get_item(model, position); |
| |
182 |
| |
183 if(account1 == account) { |
| |
184 /* NOTE: Property notification occurs in 'changed' signal |
| |
185 * callback. |
| |
186 */ |
| |
187 adw_combo_row_set_selected(ADW_COMBO_ROW(row), position); |
| |
188 |
| |
189 g_clear_object(&account1); |
| |
190 |
| |
191 return; |
| |
192 } |
| |
193 |
| |
194 g_clear_object(&account1); |
| |
195 } |
| |
196 } |
| |
197 |
| |
198 GtkFilter * |
| |
199 pidgin_account_row_get_filter(PidginAccountRow *row) { |
| |
200 g_return_val_if_fail(PIDGIN_IS_ACCOUNT_ROW(row), NULL); |
| |
201 |
| |
202 return gtk_filter_list_model_get_filter(row->filter); |
| |
203 } |
| |
204 |
| |
205 void |
| |
206 pidgin_account_row_set_filter(PidginAccountRow *row, GtkFilter *filter) { |
| |
207 g_return_if_fail(PIDGIN_IS_ACCOUNT_ROW(row)); |
| |
208 |
| |
209 gtk_filter_list_model_set_filter(row->filter, filter); |
| |
210 g_object_notify_by_pspec(G_OBJECT(row), properties[PROP_FILTER]); |
| |
211 } |
| |
212 |