finch: Mark unused variable.
[pidgin-git.git] / pidgin / pidgincontactcompletion.c
blobbdf35697161aff93949a466300bbabd065eaa4bf
1 /* pidgin
3 * Pidgin is the legal property of its developers, whose names are too numerous
4 * to list here. Please refer to the COPYRIGHT file distributed with this
5 * source distribution.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "pidgincontactcompletion.h"
24 #include <purple.h>
26 struct _PidginContactCompletion {
27 GtkEntryCompletion parent;
28 PurpleAccount *account;
31 enum {
32 PIDGIN_CONTACT_COMPLETION_COLUMN_CONTACT,
33 PIDGIN_CONTACT_COMPLETION_COLUMN_ACCOUNT,
34 PIDGIN_CONTACT_COMPLETION_N_COLUMNS,
37 enum {
38 PROP_0,
39 PROP_ACCOUNT,
40 N_PROPERTIES
43 static GParamSpec *properties[N_PROPERTIES] = {0, };
45 G_DEFINE_TYPE(PidginContactCompletion, pidgin_contact_completion, GTK_TYPE_ENTRY_COMPLETION);
47 /******************************************************************************
48 * Helpers
49 *****************************************************************************/
50 static void
51 pidgin_contact_completion_walk_contact_func(PurpleBlistNode *node, gpointer data) {
52 PurpleBuddy *buddy = PURPLE_BUDDY(node);
53 PurpleAccount *account = purple_buddy_get_account(buddy);
54 GtkListStore *store = GTK_LIST_STORE(data);
55 GtkTreeIter iter;
56 const gchar *name;
58 name = purple_buddy_get_name(buddy);
59 if(name == NULL) {
60 name = "";
63 gtk_list_store_append(store, &iter);
64 gtk_list_store_set(
65 store,
66 &iter,
67 PIDGIN_CONTACT_COMPLETION_COLUMN_CONTACT, name,
68 PIDGIN_CONTACT_COMPLETION_COLUMN_ACCOUNT, account,
73 static GtkTreeModel *
74 pidgin_contact_completion_create_model(void) {
75 GtkListStore *store = NULL;
77 store = gtk_list_store_new(
78 PIDGIN_CONTACT_COMPLETION_N_COLUMNS,
79 G_TYPE_STRING,
80 PURPLE_TYPE_ACCOUNT
83 purple_blist_walk(NULL,
84 NULL,
85 NULL,
86 pidgin_contact_completion_walk_contact_func,
87 store
90 return GTK_TREE_MODEL(store);
93 static gboolean
94 pidgin_contact_completion_match_func(GtkEntryCompletion *completion,
95 const gchar *key,
96 GtkTreeIter *iter,
97 gpointer data)
99 GtkTreeModel *model = NULL;
100 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(completion);
101 gchar *name = NULL;
103 model = gtk_entry_completion_get_model(completion);
105 gtk_tree_model_get(
106 model,
107 iter,
108 PIDGIN_CONTACT_COMPLETION_COLUMN_CONTACT, &name,
112 if(name == NULL) {
113 return FALSE;
116 if(!purple_str_has_prefix(name, key)) {
117 g_free(name);
118 return FALSE;
121 if(PURPLE_IS_ACCOUNT(comp->account)) {
122 PurpleAccount *account = NULL;
124 gtk_tree_model_get(
125 model,
126 iter,
127 PIDGIN_CONTACT_COMPLETION_COLUMN_ACCOUNT, &account,
131 if(account != comp->account) {
132 g_object_unref(account);
133 return FALSE;
136 g_object_unref(account);
139 return TRUE;
142 /******************************************************************************
143 * GObject Implemention
144 *****************************************************************************/
145 static void
146 pidgin_contact_completion_init(PidginContactCompletion *comp) {
149 static void
150 pidgin_contact_completion_constructed(GObject *obj) {
151 GtkTreeModel *model = NULL;
153 G_OBJECT_CLASS(pidgin_contact_completion_parent_class)->constructed(obj);
155 model = pidgin_contact_completion_create_model();
157 gtk_entry_completion_set_model(
158 GTK_ENTRY_COMPLETION(obj),
159 model
162 gtk_entry_completion_set_match_func(
163 GTK_ENTRY_COMPLETION(obj),
164 pidgin_contact_completion_match_func,
165 NULL,
166 NULL
169 gtk_entry_completion_set_text_column(
170 GTK_ENTRY_COMPLETION(obj),
171 PIDGIN_CONTACT_COMPLETION_COLUMN_CONTACT
175 static void
176 pidgin_contact_completion_get_property(GObject *obj,
177 guint param_id,
178 GValue *value,
179 GParamSpec *pspec)
181 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(obj);
183 switch(param_id) {
184 case PROP_ACCOUNT:
185 g_value_set_object(value, pidgin_contact_completion_get_account(comp));
186 break;
187 default:
188 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
189 break;
193 static void
194 pidgin_contact_completion_set_property(GObject *obj,
195 guint param_id,
196 const GValue *value,
197 GParamSpec *pspec)
199 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(obj);
201 switch(param_id) {
202 case PROP_ACCOUNT:
203 pidgin_contact_completion_set_account(comp,
204 g_value_get_object(value));
205 break;
206 default:
207 G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
208 break;
212 static void
213 pidgin_contact_completion_finalize(GObject *obj) {
214 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(obj);
216 g_object_unref(comp->account);
218 G_OBJECT_CLASS(pidgin_contact_completion_parent_class)->finalize(obj);
221 static void
222 pidgin_contact_completion_class_init(PidginContactCompletionClass *klass) {
223 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
225 /* The only solution I could find to make this work was to implement the
226 * constructed handler and chain up to the parent. If you find another,
227 * better way, please implement it :)
229 obj_class->constructed = pidgin_contact_completion_constructed;
231 obj_class->get_property = pidgin_contact_completion_get_property;
232 obj_class->set_property = pidgin_contact_completion_set_property;
233 obj_class->finalize = pidgin_contact_completion_finalize;
235 properties[PROP_ACCOUNT] = g_param_spec_object(
236 "account",
237 "account",
238 "The account to filter by or NULL for no filtering",
239 PURPLE_TYPE_ACCOUNT,
240 G_PARAM_READWRITE | G_PARAM_CONSTRUCT
243 g_object_class_install_properties(obj_class, N_PROPERTIES, properties);
246 /******************************************************************************
247 * Public API
248 *****************************************************************************/
249 GtkEntryCompletion *
250 pidgin_contact_completion_new(void) {
251 return GTK_ENTRY_COMPLETION(g_object_new(PIDGIN_TYPE_CONTACT_COMPLETION, NULL));
254 PurpleAccount *
255 pidgin_contact_completion_get_account(PidginContactCompletion *completion) {
256 g_return_val_if_fail(PIDGIN_IS_CONTACT_COMPLETION(completion), NULL);
258 return g_object_ref(completion->account);
261 void
262 pidgin_contact_completion_set_account(PidginContactCompletion *completion,
263 PurpleAccount *account)
265 g_return_if_fail(PIDGIN_IS_CONTACT_COMPLETION(completion));
267 if(g_set_object(&completion->account, account)) {
268 g_object_notify_by_pspec(G_OBJECT(completion),
269 properties[PROP_ACCOUNT]);