comparison icon.c @ 264:b7ac562d758c

- changed the pattern for twitter icons so that it may match to protected user's icon link too. - when pixbuf is null, pidgin-twitter will renew icon even if new and old URLs are same. - will fall back to default icon if pixbuf couldn't be made from retrieved data. - code cleanup.
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Thu, 04 Dec 2008 18:24:06 +0900
parents b2c64cd550da
children c2944685ac8e
comparison
equal deleted inserted replaced
263:b2c64cd550da 264:b7ac562d758c
154 GMatchInfo *match_info = NULL; 154 GMatchInfo *match_info = NULL;
155 icon_data *data = NULL; 155 icon_data *data = NULL;
156 gchar *url = NULL; 156 gchar *url = NULL;
157 gint regp_id = -1; 157 gint regp_id = -1;
158 158
159 twitter_debug("called\n");
160
159 if(service == twitter_service) { 161 if(service == twitter_service) {
160 data = (icon_data *)g_hash_table_lookup( 162 data = (icon_data *)g_hash_table_lookup(
161 icon_hash[service], user_name); 163 icon_hash[service], user_name);
162 regp_id = IMAGE_TWITTER; 164 regp_id = IMAGE_TWITTER;
163 } 165 }
175 data = (icon_data *)g_hash_table_lookup( 177 data = (icon_data *)g_hash_table_lookup(
176 icon_hash[service], user_name); 178 icon_hash[service], user_name);
177 regp_id = IMAGE_JISKO; 179 regp_id = IMAGE_JISKO;
178 } 180 }
179 181
180 if(!url_text) { 182 /* retrieved nothing or got a bad response */
183 if(!url_text ||
184 (!strstr(url_text, "HTTP/1.1 200 OK") &&
185 !strstr(url_text, "HTTP/1.0 200 OK"))) {
181 if(data) { 186 if(data) {
182 data->requested = FALSE; 187 data->requested = FALSE;
183 data->fetch_data = NULL; 188 data->fetch_data = NULL;
184 } 189 }
185 g_free(gotdata->user_name); 190 g_free(gotdata->user_name);
186 g_free(gotdata); 191 g_free(gotdata);
187 return; 192 return;
188 } 193 }
189 194
190 /* if we receive bad response */ 195 /* setup image url */
191 if(!strstr(url_text, "HTTP/1.1 200 OK") &&
192 !strstr(url_text, "HTTP/1.0 200 OK")) {
193 if(data) {
194 data->requested = FALSE;
195 data->fetch_data = NULL;
196 }
197 g_free(gotdata->user_name);
198 g_free(gotdata);
199 return;
200 }
201
202 /* setup image url */ /* xxx need simplify --yaz */
203 g_regex_match(regp[regp_id], url_text, 0, &match_info); 196 g_regex_match(regp[regp_id], url_text, 0, &match_info);
204 if(!g_match_info_matches(match_info)) { 197 if(!g_match_info_matches(match_info)) {
205 g_match_info_free(match_info); 198 g_match_info_free(match_info);
206 199
207 if(service == twitter_service) { 200 if(service == twitter_service) {
269 262
270 g_free(url); 263 g_free(url);
271 url = tmp; 264 url = tmp;
272 265
273 /* if requesting icon url is the same as old, return. */ 266 /* if requesting icon url is the same as old, return. */
274 if(url && data->icon_url && !strcmp(data->icon_url, url)) { 267 if(data && data->pixbuf &&
268 url && data->icon_url && !strcmp(data->icon_url, url)) {
275 twitter_debug("old url = %s new url = %s\n", data->icon_url, url); 269 twitter_debug("old url = %s new url = %s\n", data->icon_url, url);
276 data->requested = FALSE; 270 data->requested = FALSE;
277 data->fetch_data = NULL; 271 data->fetch_data = NULL;
278 g_free(url); 272 g_free(url);
279 return; 273 return;
294 288
295 /* request fetch image */ 289 /* request fetch image */
296 if(url) { 290 if(url) {
297 /* reuse gotdata. just pass given one */ 291 /* reuse gotdata. just pass given one */
298 /* gotdata will be released in got_icon_cb */ 292 /* gotdata will be released in got_icon_cb */
299 data->fetch_data = purple_util_fetch_url(url, 293 data->fetch_data =
300 TRUE, NULL, TRUE, 294 purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
301 got_icon_cb, gotdata); 295 FALSE, got_icon_cb, gotdata);
302 twitter_debug("request %s's icon\n", user_name); 296 twitter_debug("request %s's icon\n", user_name);
303 g_free(url); 297 g_free(url);
304 } 298 }
305 } 299 }
306 300
368 } 362 }
369 363
370 if(hash) 364 if(hash)
371 data = (icon_data *)g_hash_table_lookup(hash, user_name); 365 data = (icon_data *)g_hash_table_lookup(hash, user_name);
372 366
367 if(!data) {
368 twitter_debug("cannot retrieve icon_data from hash (should not be called)\n");
369 goto fin_got_icon_cb;
370 }
371
373 /* return if download failed */ 372 /* return if download failed */
374 if(!url_text) { 373 if(!url_text) {
375 twitter_debug("downloading %s's icon failed : %s\n", 374 twitter_debug("downloading %s's icon failed : %s\n",
376 user_name, error_message); 375 user_name, error_message);
377 if(data) 376
378 data->requested = FALSE; 377 data->requested = FALSE;
379 378
380 goto fin_got_icon_cb; 379 goto fin_got_icon_cb;
381 } 380 }
382 381
383 if(data) { 382 /* remove download request */
384 /* remove download request */ 383 data->requested = FALSE;
385 data->requested = FALSE; 384 data->fetch_data = NULL;
386 data->fetch_data = NULL; 385
387 386 /* return if user's icon has been downloaded */
388 /* return if user's icon has been downloaded */ 387 if(data->pixbuf) {
389 if(data->pixbuf) { 388 twitter_debug("%s's icon has already been downloaded\n",
390 twitter_debug("%s's icon has already been downloaded\n", 389 user_name);
391 user_name); 390
392 391 goto fin_got_icon_cb;
393 goto fin_got_icon_cb;
394 }
395 } 392 }
396 393
397 pixbuf = make_scaled_pixbuf(url_text, len); 394 pixbuf = make_scaled_pixbuf(url_text, len);
398 395
399 if(!pixbuf) 396 if(!pixbuf) {
397 twitter_debug("cannot make pixbuf from downloaded data\n");
398
399 /* ask to download default icon instead */
400 got_icon_data *gotdata2 = g_new0(got_icon_data, 1);
401 const gchar *url = NULL;
402
403 gotdata2->user_name = g_strdup(gotdata->user_name);
404 gotdata2->service = service;
405
406 switch(service) {
407 case twitter_service:
408 url = TWITTER_DEFAULT_ICON_URL;
409 break;
410 case identica_service:
411 url = IDENTICA_DEFAULT_ICON_URL;
412 break;
413 case jisko_service:
414 url = JISKO_DEFAULT_ICON_URL;
415 break;
416 }
417
418 g_free(data->icon_url);
419 data->icon_url = g_strdup(url);
420
421 data->requested = TRUE;
422 data->fetch_data =
423 purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
424 FALSE, got_icon_cb, gotdata2);
400 goto fin_got_icon_cb; 425 goto fin_got_icon_cb;
401
402
403 if(!data) {
404 twitter_debug("allocate icon_data (shouldn't be called)\n");
405 data = g_new0(icon_data, 1);
406 } 426 }
407 427
408 data->pixbuf = pixbuf; 428 data->pixbuf = pixbuf;
409 429
410 twitter_debug("new icon pixbuf = %p size = %d\n", 430 twitter_debug("new icon pixbuf = %p size = %d\n",
488 GHashTable *hash = NULL; 508 GHashTable *hash = NULL;
489 const gchar *suffix = NULL; 509 const gchar *suffix = NULL;
490 510
491 switch(service) { 511 switch(service) {
492 case twitter_service: 512 case twitter_service:
493 hash = icon_hash[twitter_service]; 513 hash = icon_hash[service];
494 suffix = "twitter"; 514 suffix = "twitter";
495 break; 515 break;
496 case wassr_service: 516 case wassr_service:
497 hash = icon_hash[wassr_service]; 517 hash = icon_hash[service];
498 suffix = "wassr"; 518 suffix = "wassr";
499 break; 519 break;
500 case identica_service: 520 case identica_service:
501 hash = icon_hash[identica_service]; 521 hash = icon_hash[service];
502 suffix = "identica"; 522 suffix = "identica";
503 break; 523 break;
504 case jisko_service: 524 case jisko_service:
505 hash = icon_hash[jisko_service]; 525 hash = icon_hash[service];
506 suffix = "jisko"; 526 suffix = "jisko";
507 break; 527 break;
508 default: 528 default:
509 twitter_debug("unknown service\n"); 529 twitter_debug("unknown service\n");
510 break; 530 break;
610 /* gotdata will be released in got_icon_cb */ 630 /* gotdata will be released in got_icon_cb */
611 if(service == twitter_service || 631 if(service == twitter_service ||
612 service == wassr_service || 632 service == wassr_service ||
613 service == identica_service || 633 service == identica_service ||
614 service == jisko_service) { 634 service == jisko_service) {
615 data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE, 635 data->fetch_data =
616 got_page_cb, gotdata); 636 purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
617 } 637 TRUE, got_page_cb, gotdata);
618 else { 638 }
619 data->fetch_data = purple_util_fetch_url(url, TRUE, NULL, TRUE, 639 else { /* unused */
620 got_icon_cb, gotdata); 640 data->fetch_data =
641 purple_util_fetch_url_request(url, TRUE, NULL, TRUE, NULL,
642 FALSE, got_icon_cb, gotdata);
621 } 643 }
622 g_free(url); url = NULL; 644 g_free(url); url = NULL;
623 645
624 twitter_debug("request %s's icon\n", user_name); 646 twitter_debug("request %s's icon\n", user_name);
625 } 647 }