comparison libpurple/protocols/bonjour/bonjour.c @ 19646:55d3f1622cf7

Lookup the default First and Last name values for bonjour on win32 in a thread to avoid delays when the plugin is being loaded. Apparently for some people the delay is quite significant when they're part of a domain, but the domain server isn't accessible.
author Daniel Atallah <daniel.atallah@gmail.com>
date Tue, 04 Sep 2007 19:00:57 +0000
parents affacee881e8
children ccf627e2e0cc
comparison
equal deleted inserted replaced
19645:5c92f70f8b0c 19646:55d3f1622cf7
484 NULL, 484 NULL,
485 NULL, 485 NULL,
486 NULL 486 NULL
487 }; 487 };
488 488
489 #ifdef WIN32
490 static gboolean _set_default_name_cb(gpointer data) {
491 gchar *fullname = data;
492 const char *splitpoint;
493 GList *tmp = prpl_info.protocol_options;
494 PurpleAccountOption *option;
495
496 if (!fullname) {
497 purple_debug_info("bonjour", "Unable to look up First and Last name or Username from system; using defaults.\n");
498 return FALSE;
499 }
500
501 g_free(default_firstname);
502 g_free(default_lastname);
503
504 /* Split the real name into a first and last name */
505 splitpoint = strchr(fullname, ' ');
506 if (splitpoint != NULL) {
507 default_firstname = g_strndup(fullname, splitpoint - fullname);
508 default_lastname = g_strdup(&splitpoint[1]);
509 } else {
510 default_firstname = g_strdup(fullname);
511 default_lastname = g_strdup("");
512 }
513 g_free(fullname);
514
515
516 for(; tmp != NULL; tmp = tmp->next) {
517 option = tmp->data;
518 if (strcmp("first", purple_account_option_get_setting(option)) == 0)
519 purple_account_option_set_default_string(option, default_firstname);
520 else if (strcmp("last", purple_account_option_get_setting(option)) == 0)
521 purple_account_option_set_default_string(option, default_lastname);
522 }
523
524 return FALSE;
525 }
526
527 static gpointer _win32_name_lookup_thread(gpointer data) {
528 gchar *fullname = NULL;
529 wchar_t username[UNLEN + 1];
530 DWORD dwLenUsername = UNLEN + 1;
531
532 GetUserNameW((LPWSTR) &username, &dwLenUsername);
533
534 if (username != NULL && *username != '\0') {
535 LPBYTE servername = NULL;
536 LPBYTE info = NULL;
537
538 NetGetDCName(NULL, NULL, &servername);
539
540 /* purple_debug_info("bonjour", "Looking up the full name from the %s.\n", (servername ? "domain controller" : "local machine")); */
541
542 if (NetUserGetInfo((LPCWSTR) servername, username, 10, &info) == NERR_Success
543 && info != NULL && ((LPUSER_INFO_10) info)->usri10_full_name != NULL
544 && *(((LPUSER_INFO_10) info)->usri10_full_name) != '\0') {
545 fullname = g_utf16_to_utf8(
546 ((LPUSER_INFO_10) info)->usri10_full_name,
547 -1, NULL, NULL, NULL);
548 }
549 /* Fall back to the local machine if we didn't get the full name from the domain controller */
550 else if (servername != NULL) {
551 /* purple_debug_info("bonjour", "Looking up the full name from the local machine"); */
552
553 if (info != NULL) NetApiBufferFree(info);
554 info = NULL;
555
556 if (NetUserGetInfo(NULL, username, 10, &info) == NERR_Success
557 && info != NULL && ((LPUSER_INFO_10) info)->usri10_full_name != NULL
558 && *(((LPUSER_INFO_10) info)->usri10_full_name) != '\0') {
559 fullname = g_utf16_to_utf8(
560 ((LPUSER_INFO_10) info)->usri10_full_name,
561 -1, NULL, NULL, NULL);
562 }
563 }
564
565 if (info != NULL) NetApiBufferFree(info);
566 if (servername != NULL) NetApiBufferFree(servername);
567
568 if (!fullname)
569 fullname = g_utf16_to_utf8(username, -1, NULL, NULL, NULL);
570 }
571
572 g_idle_add(_set_default_name_cb, fullname);
573
574 return NULL;
575 }
576 #endif
577
489 static void 578 static void
490 initialize_default_account_values() 579 initialize_default_account_values()
491 { 580 {
492 #ifdef _WIN32 581 #ifndef _WIN32
493 char *fullname = NULL;
494 #else
495 struct passwd *info; 582 struct passwd *info;
496 const char *fullname = NULL;
497 #endif 583 #endif
498 char *splitpoint = NULL; 584 const char *fullname = NULL, *splitpoint, *tmp;
499 char *tmp;
500 char hostname[255]; 585 char hostname[255];
586 gchar *conv = NULL;
501 587
502 #ifndef _WIN32 588 #ifndef _WIN32
503 /* Try to figure out the user's real name */ 589 /* Try to figure out the user's real name */
504 info = getpwuid(getuid()); 590 info = getpwuid(getuid());
505 if ((info != NULL) && (info->pw_gecos != NULL) && (info->pw_gecos[0] != '\0')) 591 if ((info != NULL) && (info->pw_gecos != NULL) && (info->pw_gecos[0] != '\0'))
506 fullname = info->pw_gecos; 592 fullname = info->pw_gecos;
507 else if ((info != NULL) && (info->pw_name != NULL) && (info->pw_name[0] != '\0')) 593 else if ((info != NULL) && (info->pw_name != NULL) && (info->pw_name[0] != '\0'))
508 fullname = info->pw_name; 594 fullname = info->pw_name;
509 else if (((fullname = getlogin()) != NULL) && (fullname[0] != '\0')) 595 else if (((fullname = getlogin()) != NULL) && (fullname[0] != '\0'))
510 ; 596 ;
511 else 597 #else
598 /* The Win32 username lookup functions are synchronous so we do it in a thread */
599 g_thread_create(_win32_name_lookup_thread, NULL, FALSE, NULL);
600 #endif
601
602 /* Make sure fullname is valid UTF-8. If not, try to convert it. */
603 if (fullname != NULL && !g_utf8_validate(fullname, -1, NULL)) {
604 fullname = conv = g_locale_to_utf8(fullname, -1, NULL, NULL, NULL);
605 if (conv == NULL || *conv == '\0')
606 fullname = NULL;
607 }
608
609 if (fullname == NULL)
512 fullname = _("Purple Person"); 610 fullname = _("Purple Person");
513 /* Make sure fullname is valid UTF-8. If not, try to convert it. */
514 if (!g_utf8_validate(fullname, -1, NULL))
515 {
516 gchar *tmp;
517 tmp = g_locale_to_utf8(fullname, -1, NULL, NULL, NULL);
518 if ((tmp == NULL) || (*tmp == '\0'))
519 fullname = _("Purple Person");
520 }
521
522 #else
523 wchar_t username[UNLEN + 1];
524 DWORD dwLenUsername = UNLEN + 1;
525
526 if (!GetUserNameW((LPWSTR) &username, &dwLenUsername))
527 purple_debug_warning("bonjour", "Unable to look up username\n");
528
529 if (username != NULL && *username != '\0') {
530 LPBYTE servername = NULL;
531 LPBYTE info = NULL;
532
533 NetGetDCName(NULL, NULL, &servername);
534
535 purple_debug_info("bonjour", "Looking up the full name from the %s.\n", (servername ? "domain controller" : "local machine"));
536
537 if (NetUserGetInfo((LPCWSTR) servername, username, 10, &info) == NERR_Success
538 && info != NULL && ((LPUSER_INFO_10) info)->usri10_full_name != NULL
539 && *(((LPUSER_INFO_10) info)->usri10_full_name) != '\0') {
540 fullname = g_utf16_to_utf8(
541 ((LPUSER_INFO_10) info)->usri10_full_name,
542 -1, NULL, NULL, NULL);
543 }
544 /* Fall back to the local machine if we didn't get the full name from the domain controller */
545 else if (servername != NULL) {
546 purple_debug_info("bonjour", "Looking up the full name from the local machine");
547
548 if (info != NULL) NetApiBufferFree(info);
549 info = NULL;
550
551 if (NetUserGetInfo(NULL, username, 10, &info) == NERR_Success
552 && info != NULL && ((LPUSER_INFO_10) info)->usri10_full_name != NULL
553 && *(((LPUSER_INFO_10) info)->usri10_full_name) != '\0') {
554 fullname = g_utf16_to_utf8(
555 ((LPUSER_INFO_10) info)->usri10_full_name,
556 -1, NULL, NULL, NULL);
557 }
558 }
559
560 if (info != NULL) NetApiBufferFree(info);
561 if (servername != NULL) NetApiBufferFree(servername);
562 }
563
564 if (!fullname) {
565 if (username != NULL && *username != '\0')
566 fullname = g_utf16_to_utf8(username, -1, NULL, NULL, NULL);
567 else
568 fullname = g_strdup(_("Purple Person"));
569 }
570 #endif
571 611
572 /* Split the real name into a first and last name */ 612 /* Split the real name into a first and last name */
573 splitpoint = strchr(fullname, ' '); 613 splitpoint = strchr(fullname, ' ');
574 if (splitpoint != NULL) 614 if (splitpoint != NULL) {
575 {
576 default_firstname = g_strndup(fullname, splitpoint - fullname); 615 default_firstname = g_strndup(fullname, splitpoint - fullname);
577 tmp = &splitpoint[1]; 616 tmp = &splitpoint[1];
578 617
579 /* The last name may be followed by a comma and additional data. 618 /* The last name may be followed by a comma and additional data.
580 * Only use the last name itself. 619 * Only use the last name itself.
582 splitpoint = strchr(tmp, ','); 621 splitpoint = strchr(tmp, ',');
583 if (splitpoint != NULL) 622 if (splitpoint != NULL)
584 default_lastname = g_strndup(tmp, splitpoint - tmp); 623 default_lastname = g_strndup(tmp, splitpoint - tmp);
585 else 624 else
586 default_lastname = g_strdup(tmp); 625 default_lastname = g_strdup(tmp);
587 } 626 } else {
588 else
589 {
590 default_firstname = g_strdup(fullname); 627 default_firstname = g_strdup(fullname);
591 default_lastname = g_strdup(""); 628 default_lastname = g_strdup("");
592 } 629 }
593 630
594 #ifdef _WIN32 631 g_free(conv);
595 g_free(fullname);
596 #endif
597 632
598 /* Try to figure out a good host name to use */ 633 /* Try to figure out a good host name to use */
599 /* TODO: Avoid 'localhost,' if possible */ 634 /* TODO: Avoid 'localhost,' if possible */
600 if (gethostname(hostname, 255) != 0) { 635 if (gethostname(hostname, 255) != 0) {
601 purple_debug_warning("bonjour", "Error when getting host name: %s. Using \"localhost.\"\n", 636 purple_debug_warning("bonjour", "Error when getting host name: %s. Using \"localhost.\"\n",