michael@0: /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #include "nsUserInfoMac.h" michael@0: #include "nsObjCExceptions.h" michael@0: #include "nsString.h" michael@0: michael@0: #import michael@0: #import michael@0: michael@0: NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo) michael@0: michael@0: nsUserInfo::nsUserInfo() {} michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetFullname(char16_t **aFullname) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT michael@0: michael@0: NS_ConvertUTF8toUTF16 fullName([NSFullUserName() UTF8String]); michael@0: *aFullname = ToNewUnicode(fullName); michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetUsername(char **aUsername) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT michael@0: michael@0: nsAutoCString username([NSUserName() UTF8String]); michael@0: *aUsername = ToNewCString(username); michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT michael@0: } michael@0: michael@0: nsresult michael@0: nsUserInfo::GetPrimaryEmailAddress(nsCString &aEmailAddress) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT michael@0: michael@0: // Try to get this user's primary email from the system addressbook's "me card" michael@0: // (if they've filled it) michael@0: ABPerson *me = [[ABAddressBook sharedAddressBook] me]; michael@0: ABMultiValue *emailAddresses = [me valueForProperty:kABEmailProperty]; michael@0: if ([emailAddresses count] > 0) { michael@0: // get the index of the primary email, in case there are more than one michael@0: int primaryEmailIndex = [emailAddresses indexForIdentifier:[emailAddresses primaryIdentifier]]; michael@0: aEmailAddress.Assign([[emailAddresses valueAtIndex:primaryEmailIndex] UTF8String]); michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetEmailAddress(char **aEmailAddress) michael@0: { michael@0: nsAutoCString email; michael@0: if (NS_SUCCEEDED(GetPrimaryEmailAddress(email))) michael@0: *aEmailAddress = ToNewCString(email); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsUserInfo::GetDomain(char **aDomain) michael@0: { michael@0: nsAutoCString email; michael@0: if (NS_SUCCEEDED(GetPrimaryEmailAddress(email))) { michael@0: int32_t index = email.FindChar('@'); michael@0: if (index != -1) { michael@0: // chop off everything before, and including the '@' michael@0: *aDomain = ToNewCString(Substring(email, index + 1)); michael@0: } michael@0: } michael@0: return NS_OK; michael@0: }