changeset 18578:acd4ff9ddace

Bonjour only works on Windows2000 or newer, so we don't need to indirectly access the Net* functions. Avoid doing an unnecessary domain query to find the Domain Controller (which was causing a several second delay at startup). Also, try harder to avoid empty default names and falling back to "Purple Person".
author Daniel Atallah <daniel.atallah@gmail.com>
date Fri, 20 Jul 2007 22:24:10 +0000
parents 2a431f30af2a
children fbeda937002b
files libpurple/protocols/bonjour/Makefile.mingw libpurple/protocols/bonjour/bonjour.c
diffstat 2 files changed, 39 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/bonjour/Makefile.mingw	Fri Jul 20 21:49:53 2007 +0000
+++ b/libpurple/protocols/bonjour/Makefile.mingw	Fri Jul 20 22:24:10 2007 +0000
@@ -57,6 +57,7 @@
 			-lws2_32 \
 			-lintl \
 			-ldnssd \
+			-lnetapi32 \
 			-lpurple
 
 include $(PIDGIN_COMMON_RULES)
--- a/libpurple/protocols/bonjour/bonjour.c	Fri Jul 20 21:49:53 2007 +0000
+++ b/libpurple/protocols/bonjour/bonjour.c	Fri Jul 20 22:24:10 2007 +0000
@@ -478,61 +478,53 @@
 	}
 
 #else
-	FARPROC myNetUserGetInfo = wpurple_find_and_loadproc("Netapi32.dll",
-		"NetUserGetInfo");
+	wchar_t username[UNLEN + 1];
+	DWORD dwLenUsername = UNLEN + 1;
+
+	if (!GetUserNameW((LPWSTR) &username, &dwLenUsername))
+		purple_debug_warning("bonjour", "Unable to look up username\n");
 
-	if (myNetUserGetInfo) {
-		LPUSER_INFO_10 user_info = NULL;
-		LPSERVER_INFO_100 server_info = NULL;
-		wchar_t *servername = NULL;
-		wchar_t username[UNLEN + 1];
-		DWORD dwLenUsername = UNLEN + 1;
-		FARPROC myNetServerEnum = wpurple_find_and_loadproc(
-			"Netapi32.dll", "NetServerEnum");
-		FARPROC myNetApiBufferFree = wpurple_find_and_loadproc(
-			"Netapi32.dll", "NetApiBufferFree");
+	if (username != NULL && *username != '\0') {
+		LPBYTE servername = NULL;
+		LPBYTE info = NULL;
+
+		NetGetDCName(NULL, NULL, &servername);
+
+		purple_debug_info("bonjour", "Looking up the full name from the %s.\n", (servername ? "domain controller" : "local machine"));
 
-		if (myNetServerEnum && myNetApiBufferFree) {
-			DWORD dwEntriesRead = 0;
-			DWORD dwTotalEntries = 0;
-			DWORD dwResumeHandle = 0;
+		if (NetUserGetInfo((LPCWSTR) servername, username, 10, &info) == NERR_Success
+				&& info != NULL && ((LPUSER_INFO_10) info)->usri10_full_name != NULL
+				&& *(((LPUSER_INFO_10) info)->usri10_full_name) != '\0') {
+			fullname = g_utf16_to_utf8(
+				((LPUSER_INFO_10) info)->usri10_full_name,
+				-1, NULL, NULL, NULL);
+		}
+		/* Fall back to the local machine if we didn't get the full name from the domain controller */
+		else if (servername != NULL) {
+			purple_debug_info("bonjour", "Looking up the full name from the local machine");
 
-			NET_API_STATUS nStatus = (myNetServerEnum)(NULL, 100,
-				&server_info, MAX_PREFERRED_LENGTH,
-				&dwEntriesRead, &dwTotalEntries,
-				SV_TYPE_DOMAIN_CTRL, NULL, &dwResumeHandle);
+			if (info != NULL) NetApiBufferFree(info);
+			info = NULL;
 
-			if ((nStatus == NERR_Success
-					|| nStatus == ERROR_MORE_DATA)
-					&& dwEntriesRead > 0) {
-				servername = server_info->sv100_name;
-			} else {
-				purple_debug_warning("bonjour", "Unable to look up domain controller. NET_API_STATUS = %d, Entries Read = %d, Total Entries = %d\n", nStatus, dwEntriesRead, dwTotalEntries);
+			if (NetUserGetInfo(NULL, username, 10, &info) == NERR_Success
+					&& info != NULL && ((LPUSER_INFO_10) info)->usri10_full_name != NULL
+					&& *(((LPUSER_INFO_10) info)->usri10_full_name) != '\0') {
+				fullname = g_utf16_to_utf8(
+					((LPUSER_INFO_10) info)->usri10_full_name,
+					-1, NULL, NULL, NULL);
 			}
 		}
 
-		if (!GetUserNameW((LPWSTR) &username, &dwLenUsername)) {
-			purple_debug_warning("bonjour",
-				"Unable to look up username\n");
-		}
-
-		if (username != NULL && *username != '\0'
-				&& (myNetUserGetInfo)(servername, username, 10,
-					&user_info) == NERR_Success) {
-			if (user_info != NULL) {
-				fullname = g_utf16_to_utf8(
-					user_info->usri10_full_name,
-					-1, NULL, NULL, NULL);
-			}
-		}
-		if (user_info != NULL)
-			(myNetApiBufferFree)(user_info);
-		if (server_info != NULL)
-			(myNetApiBufferFree)(server_info);
+		if (info != NULL) NetApiBufferFree(info);
+		if (servername != NULL) NetApiBufferFree(servername);
 	}
 
-	if (!fullname)
-		fullname = g_strdup(_("Purple Person"));
+	if (!fullname) {
+		if (username != NULL && *username != '\0')
+			fullname = g_utf16_to_utf8(username, -1, NULL, NULL, NULL);
+		else
+			fullname = g_strdup(_("Purple Person"));
+	}
 #endif
 
 	/* Split the real name into a first and last name */