comparison pidgin-audacious.c @ 1:46071692f191

implement new replace logic to status and userinfo.
author Yoshiki Yazawa <yaz@cc.rim.or.jp>
date Thu, 28 Jun 2007 21:36:17 +0900
parents 8d4d17a528ef
children f1dc959e4706
comparison
equal deleted inserted replaced
0:8d4d17a528ef 1:46071692f191
50 #define aud_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, "Pidgin-Audacious", \ 50 #define aud_error(fmt, ...) purple_debug(PURPLE_DEBUG_ERROR, "Pidgin-Audacious", \
51 fmt, ## __VA_ARGS__); 51 fmt, ## __VA_ARGS__);
52 52
53 static gint timeout_tag = 0; 53 static gint timeout_tag = 0;
54 54
55 GHashTable *stored_status; 55 GHashTable *seed_status;
56 GHashTable *stored_userinfo; 56 GHashTable *seed_userinfo;
57 GHashTable *stored_alias; 57 GHashTable *seed_alias;
58 58
59 GHashTable *pushed_status; 59 GHashTable *pushed_status;
60 GHashTable *pushed_userinfo; 60 GHashTable *pushed_userinfo;
61 GHashTable *pushed_alias; 61 GHashTable *pushed_alias;
62 62
125 } 125 }
126 126
127 static void 127 static void
128 aud_process_status(PurpleConnection *gc, gchar *aud_info) 128 aud_process_status(PurpleConnection *gc, gchar *aud_info)
129 { 129 {
130 gchar *new; 130 gchar *new = NULL, *key = NULL;
131 const gchar *old, *proto; 131 const gchar *current, *seed, *pushed, *proto;
132 PurpleAccount *account; 132 PurpleAccount *account;
133 PurplePresence *presence; 133 PurplePresence *presence;
134 PurplePlugin *prpl; 134 PurplePlugin *prpl;
135 PurplePluginProtocolInfo *prpl_info; 135 PurplePluginProtocolInfo *prpl_info;
136 PurpleStatus *status; 136 PurpleStatus *status;
137 137
138 gpointer val; // for hash
139 gchar *key;
140
141 account = purple_connection_get_account(gc); 138 account = purple_connection_get_account(gc);
142 presence = purple_account_get_presence(account); 139 presence = purple_account_get_presence(account);
143 140
144 proto = purple_account_get_protocol_id(account); 141 proto = purple_account_get_protocol_id(account);
145 prpl = purple_find_prpl(proto); 142 prpl = purple_find_prpl(proto);
149 g_return_if_fail(prpl_info != NULL && prpl_info->set_status != NULL); 146 g_return_if_fail(prpl_info != NULL && prpl_info->set_status != NULL);
150 147
151 status = purple_presence_get_active_status(presence); 148 status = purple_presence_get_active_status(presence);
152 g_return_if_fail(status != NULL); 149 g_return_if_fail(status != NULL);
153 150
154 old = purple_status_get_attr_string(status, "message");
155 aud_debug("status current = %s\n", old);
156 if(old == NULL || strlen(old) == 0) { // auto away etc.
157 /* invalidate pushded status */
158 /* generate key for hash table */
159 key = g_strdup_printf("%s %s", account->username, account->protocol_id);
160 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup(""));
161 return;
162 }
163
164 /* generate key for hash table */ 151 /* generate key for hash table */
165 key = g_strdup_printf("%s %s", account->username, account->protocol_id); 152 key = g_strdup_printf("%s %s", account->username, account->protocol_id);
166 153
167 val = g_hash_table_lookup(pushed_status, key); 154 /* retrieve current user status */
168 155 current = purple_status_get_attr_string(status, "message");
169 /* if current alias differs from pushed_alias or contains token, replace seed with this. */ 156 aud_debug("status current = %s\n", current);
170 if( (val && g_ascii_strcasecmp(old, val)) || strstr(old, SONG_TOKEN) ) { 157
171 g_hash_table_replace(stored_status, g_strdup(key), g_strdup(old)); 158 /* invalidate pushded status on auto away etc. */
159 if(current == NULL || strlen(current) == 0) {
160 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup(""));
161 g_free(key);
162 return;
163 }
164
165 /* pop pushed_status */
166 pushed = (gchar *)g_hash_table_lookup(pushed_status, key);
167
168 /* if current status differs from pushed_status or contains token, replace hashes with current. */
169 if( (pushed && g_ascii_strcasecmp(current, pushed)) || strstr(current, SONG_TOKEN) ) {
170 g_hash_table_replace(seed_status, g_strdup(key), g_strdup(current));
171 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup(current));
172 } 172 }
173 173
174 /* construct new status message */ 174 /* construct new status message */
175 val = g_hash_table_lookup(stored_status, key); 175 seed = (gchar *)g_hash_table_lookup(seed_status, key);
176 g_return_if_fail(val != NULL); 176 g_return_if_fail(seed != NULL);
177 aud_debug("status stored = %s\n", (gchar *)val); 177 aud_debug("status seed = %s\n", seed);
178 178
179 if(aud_info){ 179 if(strstr(seed, SONG_TOKEN)) {
180 new = purple_strreplace(val, SONG_TOKEN, aud_info); 180 if(aud_info){
181 } 181 new = purple_strreplace(seed, SONG_TOKEN, aud_info);
182 else { 182 }
183 new = g_strdup(NO_SONG_MESSAGE); 183 else {
184 } 184 new = g_strdup(NO_SONG_MESSAGE);
185 185 }
186 }
186 g_return_if_fail(new != NULL); 187 g_return_if_fail(new != NULL);
187 188
188 /* set status message only if text has been changed */ 189 /* set status message only if text has been changed */
189 val = g_hash_table_lookup(pushed_status, key); 190 pushed = (gchar *)g_hash_table_lookup(pushed_status, key);
190 aud_debug("status pushed = %s\n", (gchar *)val); 191 aud_debug("status pushed = %s\n", pushed);
191 192
192 if (!val || g_ascii_strcasecmp(val, new) != 0) { 193 if (!pushed || g_ascii_strcasecmp(pushed, new)) {
193 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup(new)); 194 g_hash_table_replace(pushed_status, g_strdup(key), g_strdup(new));
194 purple_status_set_attr_string(status, "message", new); 195 purple_status_set_attr_string(status, "message", new);
195 prpl_info->set_status(account, status); 196 prpl_info->set_status(account, status);
196 } 197 }
197 g_free(key); 198 g_free(key);
200 201
201 202
202 static void 203 static void
203 aud_process_userinfo(PurpleConnection *gc, gchar *aud_info) 204 aud_process_userinfo(PurpleConnection *gc, gchar *aud_info)
204 { 205 {
205 gchar *new; 206 gchar *new = NULL, *key = NULL;
206 const gchar *old, *proto; 207 const gchar *current, *seed, *pushed, *proto;
207 PurpleAccount *account; 208 PurpleAccount *account;
208 PurplePlugin *prpl; 209 PurplePlugin *prpl;
209 PurplePluginProtocolInfo *prpl_info; 210 PurplePluginProtocolInfo *prpl_info;
210 211
211 gpointer val; // for hash
212 gchar *key;
213
214 account = purple_connection_get_account(gc); 212 account = purple_connection_get_account(gc);
215 213
216 proto = purple_account_get_protocol_id(account); 214 proto = purple_account_get_protocol_id(account);
217 prpl = purple_find_prpl(proto); 215 prpl = purple_find_prpl(proto);
218 g_return_if_fail(prpl != NULL); 216 g_return_if_fail(prpl != NULL);
219 217
220 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl); 218 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(prpl);
221 g_return_if_fail(prpl_info != NULL && prpl_info->set_info != NULL); 219 g_return_if_fail(prpl_info != NULL && prpl_info->set_info != NULL);
222 220
223 /* retrieve the old user info */
224 old = purple_account_get_user_info(account); /* it's always from account.xml! */
225 if(old == NULL || strlen(old) == 0)
226 return;
227
228 /* generate key for hash table*/ 221 /* generate key for hash table*/
229 key = g_strdup_printf("%s %s", account->username, account->protocol_id); 222 key = g_strdup_printf("%s %s", account->username, account->protocol_id);
230 223
231 val = g_hash_table_lookup(pushed_userinfo, key); 224 /* retrieve current user info */
225 current = purple_account_get_user_info(account); /* it's always from account.xml! */
226 aud_debug("userinfo current = %s\n", current);
227
228 /* invalidate pushded status on auto away etc. */
229 if(current == NULL || strlen(current) == 0) {
230 g_hash_table_replace(pushed_userinfo, g_strdup(key), g_strdup(""));
231 g_free(key);
232 return;
233 }
234
235 /* pop pushed_userinfo */
236 pushed = g_hash_table_lookup(pushed_userinfo, key);
232 237
233 /* if current alias differs from pushed_alias or contains token, replace seed with this. */ 238 /* if current alias differs from pushed_alias or contains token, replace seed with this. */
234 if( (val && g_ascii_strcasecmp(old, val)) || strstr(old, SONG_TOKEN) ) { 239 if( (pushed && g_ascii_strcasecmp(current, pushed)) || strstr(current, SONG_TOKEN) ) {
235 g_hash_table_replace(stored_userinfo, g_strdup(key), g_strdup(old)); 240 g_hash_table_replace(seed_userinfo, g_strdup(key), g_strdup(current));
241 g_hash_table_replace(pushed_userinfo, g_strdup(key), g_strdup(current));
236 } 242 }
237 243
238 /* construct new status message */ 244 /* construct new status message */
239 val = g_hash_table_lookup(stored_userinfo, key); 245 seed = (gchar *)g_hash_table_lookup(seed_userinfo, key);
240 g_return_if_fail(val != NULL); 246 g_return_if_fail(seed != NULL);
241 247 aud_debug("userinfo seed = %s\n", seed);
242 aud_debug("userinfo stored = %s\n", (gchar *)val); 248
243 249 if(strstr(seed, SONG_TOKEN)) {
244 if(aud_info){ 250 if(aud_info){
245 new = purple_strreplace(val, SONG_TOKEN, aud_info); 251 new = purple_strreplace(seed, SONG_TOKEN, aud_info);
246 } 252 }
247 else { 253 else {
248 new = g_strdup(NO_SONG_MESSAGE); 254 new = g_strdup(NO_SONG_MESSAGE);
249 } 255 }
250 256 }
251 g_return_if_fail(new != NULL); 257 g_return_if_fail(new != NULL);
252 258
253 /* set user info only if text has been changed */ 259 /* set user info only if text has been changed */
254 val = g_hash_table_lookup(pushed_userinfo, key); 260 pushed = (gchar *)g_hash_table_lookup(pushed_userinfo, key);
255 aud_debug("userinfo pushed = %s\n", (gchar *)val); 261 aud_debug("userinfo pushed = %s\n", pushed);
256 262
257 if (!val || g_ascii_strcasecmp(val, new) != 0) { 263 if (!pushed || g_ascii_strcasecmp(pushed, new) != 0) {
258 g_hash_table_replace(pushed_userinfo, g_strdup(key), g_strdup(new)); 264 g_hash_table_replace(pushed_userinfo, g_strdup(key), g_strdup(new));
259 prpl_info->set_info(gc, new); 265 prpl_info->set_info(gc, new);
260 } 266 }
261 g_free(key); 267 g_free(key);
262 g_free(new); 268 g_free(new);
299 305
300 val = g_hash_table_lookup(pushed_alias, key); 306 val = g_hash_table_lookup(pushed_alias, key);
301 307
302 /* if current alias differs from pushed_alias or contains token, replace seed with this. */ 308 /* if current alias differs from pushed_alias or contains token, replace seed with this. */
303 if( (val && g_ascii_strcasecmp(old, val)) || strstr(old, SONG_TOKEN) ) { 309 if( (val && g_ascii_strcasecmp(old, val)) || strstr(old, SONG_TOKEN) ) {
304 g_hash_table_replace(stored_alias, g_strdup(key), g_strdup(old)); 310 g_hash_table_replace(seed_alias, g_strdup(key), g_strdup(old));
305 } 311 }
306 312
307 /* construct new status message */ 313 /* construct new status message */
308 val = g_hash_table_lookup(stored_alias, key); 314 val = g_hash_table_lookup(seed_alias, key);
309 g_return_if_fail(val != NULL); 315 g_return_if_fail(val != NULL);
310 316
311 bytes = strlen(val); 317 bytes = strlen(val);
312 bytes -= strlen(SONG_TOKEN); 318 bytes -= strlen(SONG_TOKEN);
313 aud_debug("alias: bytes = %ld", bytes); 319 aud_debug("alias: bytes = %ld", bytes);
397 403
398 aud_debug("********** restore_alias called **********\n"); 404 aud_debug("********** restore_alias called **********\n");
399 account = purple_connection_get_account(gc); 405 account = purple_connection_get_account(gc);
400 406
401 key = g_strdup_printf("%s %s", account->username, account->protocol_id); 407 key = g_strdup_printf("%s %s", account->username, account->protocol_id);
402 val = g_hash_table_lookup(stored_alias, key); 408 val = g_hash_table_lookup(seed_alias, key);
403 g_return_val_if_fail(val != NULL, FALSE); 409 g_return_val_if_fail(val != NULL, FALSE);
404 410
405 aud_debug("write back alias %s\n", val); 411 aud_debug("write back alias %s\n", val);
406 purple_account_set_alias(account, val); //oct16 412 purple_account_set_alias(account, val); //oct16
407 413
412 418
413 419
414 static gboolean 420 static gboolean
415 load_plugin(PurplePlugin *plugin) 421 load_plugin(PurplePlugin *plugin)
416 { 422 {
417 stored_status = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval); 423 seed_status = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval);
418 stored_alias = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval); 424 seed_alias = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval);
419 stored_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval); 425 seed_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval);
420 426
421 pushed_status = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval); 427 pushed_status = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval);
422 pushed_alias = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval); 428 pushed_alias = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval);
423 pushed_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval); 429 pushed_userinfo = g_hash_table_new_full(g_str_hash, g_str_equal, removekey, removeval);
424 430
435 static gboolean 441 static gboolean
436 unload_plugin(PurplePlugin *plugin) 442 unload_plugin(PurplePlugin *plugin)
437 { 443 {
438 aud_debug("pidgin-audacious unload called\n"); 444 aud_debug("pidgin-audacious unload called\n");
439 445
440 g_hash_table_destroy(stored_status); 446 g_hash_table_destroy(seed_status);
441 g_hash_table_destroy(stored_alias); 447 g_hash_table_destroy(seed_alias);
442 g_hash_table_destroy(stored_userinfo); 448 g_hash_table_destroy(seed_userinfo);
443 449
444 g_hash_table_destroy(pushed_status); 450 g_hash_table_destroy(pushed_status);
445 g_hash_table_destroy(pushed_alias); 451 g_hash_table_destroy(pushed_alias);
446 g_hash_table_destroy(pushed_userinfo); 452 g_hash_table_destroy(pushed_userinfo);
447 453