| 1 /* pidgin |
|
| 2 * |
|
| 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. |
|
| 6 * |
|
| 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. |
|
| 11 * |
|
| 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. |
|
| 16 * |
|
| 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 |
|
| 20 */ |
|
| 21 |
|
| 22 #include "pidgincontactcompletion.h" |
|
| 23 |
|
| 24 #include <purple.h> |
|
| 25 |
|
| 26 struct _PidginContactCompletion { |
|
| 27 GtkEntryCompletion parent; |
|
| 28 PurpleAccount *account; |
|
| 29 }; |
|
| 30 |
|
| 31 enum { |
|
| 32 PIDGIN_CONTACT_COMPLETION_COLUMN_CONTACT, |
|
| 33 PIDGIN_CONTACT_COMPLETION_COLUMN_ACCOUNT, |
|
| 34 PIDGIN_CONTACT_COMPLETION_N_COLUMNS, |
|
| 35 }; |
|
| 36 |
|
| 37 enum { |
|
| 38 PROP_0, |
|
| 39 PROP_ACCOUNT, |
|
| 40 N_PROPERTIES |
|
| 41 }; |
|
| 42 |
|
| 43 static GParamSpec *properties[N_PROPERTIES] = {0, }; |
|
| 44 |
|
| 45 G_DEFINE_TYPE(PidginContactCompletion, pidgin_contact_completion, GTK_TYPE_ENTRY_COMPLETION); |
|
| 46 |
|
| 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; |
|
| 57 |
|
| 58 name = purple_buddy_get_name(buddy); |
|
| 59 if(name == NULL) { |
|
| 60 name = ""; |
|
| 61 } |
|
| 62 |
|
| 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, |
|
| 69 -1 |
|
| 70 ); |
|
| 71 } |
|
| 72 |
|
| 73 static GtkTreeModel * |
|
| 74 pidgin_contact_completion_create_model(void) { |
|
| 75 GtkListStore *store = NULL; |
|
| 76 |
|
| 77 store = gtk_list_store_new( |
|
| 78 PIDGIN_CONTACT_COMPLETION_N_COLUMNS, |
|
| 79 G_TYPE_STRING, |
|
| 80 PURPLE_TYPE_ACCOUNT |
|
| 81 ); |
|
| 82 |
|
| 83 purple_blist_walk(NULL, |
|
| 84 NULL, |
|
| 85 NULL, |
|
| 86 pidgin_contact_completion_walk_contact_func, |
|
| 87 store |
|
| 88 ); |
|
| 89 |
|
| 90 return GTK_TREE_MODEL(store); |
|
| 91 } |
|
| 92 |
|
| 93 static gboolean |
|
| 94 pidgin_contact_completion_match_func(GtkEntryCompletion *completion, |
|
| 95 const gchar *key, |
|
| 96 GtkTreeIter *iter, |
|
| 97 gpointer data) |
|
| 98 { |
|
| 99 GtkTreeModel *model = NULL; |
|
| 100 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(completion); |
|
| 101 gchar *name = NULL; |
|
| 102 |
|
| 103 model = gtk_entry_completion_get_model(completion); |
|
| 104 |
|
| 105 gtk_tree_model_get( |
|
| 106 model, |
|
| 107 iter, |
|
| 108 PIDGIN_CONTACT_COMPLETION_COLUMN_CONTACT, &name, |
|
| 109 -1 |
|
| 110 ); |
|
| 111 |
|
| 112 if(name == NULL) { |
|
| 113 return FALSE; |
|
| 114 } |
|
| 115 |
|
| 116 if (!g_str_has_prefix(name, key)) { |
|
| 117 g_free(name); |
|
| 118 return FALSE; |
|
| 119 } |
|
| 120 |
|
| 121 if(PURPLE_IS_ACCOUNT(comp->account)) { |
|
| 122 PurpleAccount *account = NULL; |
|
| 123 |
|
| 124 gtk_tree_model_get( |
|
| 125 model, |
|
| 126 iter, |
|
| 127 PIDGIN_CONTACT_COMPLETION_COLUMN_ACCOUNT, &account, |
|
| 128 -1 |
|
| 129 ); |
|
| 130 |
|
| 131 if(account != comp->account) { |
|
| 132 g_object_unref(account); |
|
| 133 return FALSE; |
|
| 134 } |
|
| 135 |
|
| 136 g_object_unref(account); |
|
| 137 } |
|
| 138 |
|
| 139 return TRUE; |
|
| 140 } |
|
| 141 |
|
| 142 /****************************************************************************** |
|
| 143 * GObject Implementation |
|
| 144 *****************************************************************************/ |
|
| 145 static void |
|
| 146 pidgin_contact_completion_init(PidginContactCompletion *comp) { |
|
| 147 } |
|
| 148 |
|
| 149 static void |
|
| 150 pidgin_contact_completion_constructed(GObject *obj) { |
|
| 151 GtkTreeModel *model = NULL; |
|
| 152 |
|
| 153 G_OBJECT_CLASS(pidgin_contact_completion_parent_class)->constructed(obj); |
|
| 154 |
|
| 155 model = pidgin_contact_completion_create_model(); |
|
| 156 |
|
| 157 gtk_entry_completion_set_model( |
|
| 158 GTK_ENTRY_COMPLETION(obj), |
|
| 159 model |
|
| 160 ); |
|
| 161 |
|
| 162 gtk_entry_completion_set_match_func( |
|
| 163 GTK_ENTRY_COMPLETION(obj), |
|
| 164 pidgin_contact_completion_match_func, |
|
| 165 NULL, |
|
| 166 NULL |
|
| 167 ); |
|
| 168 |
|
| 169 gtk_entry_completion_set_text_column( |
|
| 170 GTK_ENTRY_COMPLETION(obj), |
|
| 171 PIDGIN_CONTACT_COMPLETION_COLUMN_CONTACT |
|
| 172 ); |
|
| 173 } |
|
| 174 |
|
| 175 static void |
|
| 176 pidgin_contact_completion_get_property(GObject *obj, |
|
| 177 guint param_id, |
|
| 178 GValue *value, |
|
| 179 GParamSpec *pspec) |
|
| 180 { |
|
| 181 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(obj); |
|
| 182 |
|
| 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; |
|
| 190 } |
|
| 191 } |
|
| 192 |
|
| 193 static void |
|
| 194 pidgin_contact_completion_set_property(GObject *obj, |
|
| 195 guint param_id, |
|
| 196 const GValue *value, |
|
| 197 GParamSpec *pspec) |
|
| 198 { |
|
| 199 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(obj); |
|
| 200 |
|
| 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; |
|
| 209 } |
|
| 210 } |
|
| 211 |
|
| 212 static void |
|
| 213 pidgin_contact_completion_finalize(GObject *obj) { |
|
| 214 PidginContactCompletion *comp = PIDGIN_CONTACT_COMPLETION(obj); |
|
| 215 |
|
| 216 g_object_unref(comp->account); |
|
| 217 |
|
| 218 G_OBJECT_CLASS(pidgin_contact_completion_parent_class)->finalize(obj); |
|
| 219 } |
|
| 220 |
|
| 221 static void |
|
| 222 pidgin_contact_completion_class_init(PidginContactCompletionClass *klass) { |
|
| 223 GObjectClass *obj_class = G_OBJECT_CLASS(klass); |
|
| 224 |
|
| 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 :) |
|
| 228 */ |
|
| 229 obj_class->constructed = pidgin_contact_completion_constructed; |
|
| 230 |
|
| 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; |
|
| 234 |
|
| 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 |
|
| 241 ); |
|
| 242 |
|
| 243 g_object_class_install_properties(obj_class, N_PROPERTIES, properties); |
|
| 244 } |
|
| 245 |
|
| 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)); |
|
| 252 } |
|
| 253 |
|
| 254 PurpleAccount * |
|
| 255 pidgin_contact_completion_get_account(PidginContactCompletion *completion) { |
|
| 256 g_return_val_if_fail(PIDGIN_IS_CONTACT_COMPLETION(completion), NULL); |
|
| 257 |
|
| 258 return g_object_ref(completion->account); |
|
| 259 } |
|
| 260 |
|
| 261 void |
|
| 262 pidgin_contact_completion_set_account(PidginContactCompletion *completion, |
|
| 263 PurpleAccount *account) |
|
| 264 { |
|
| 265 g_return_if_fail(PIDGIN_IS_CONTACT_COMPLETION(completion)); |
|
| 266 |
|
| 267 if(g_set_object(&completion->account, account)) { |
|
| 268 g_object_notify_by_pspec(G_OBJECT(completion), |
|
| 269 properties[PROP_ACCOUNT]); |
|
| 270 } |
|
| 271 } |
|