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