toolkit/components/startup/nsUserInfoMac.mm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/startup/nsUserInfoMac.mm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,84 @@
     1.4 +/* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#include "nsUserInfoMac.h"
    1.11 +#include "nsObjCExceptions.h"
    1.12 +#include "nsString.h"
    1.13 +
    1.14 +#import <Cocoa/Cocoa.h>
    1.15 +#import <AddressBook/AddressBook.h>
    1.16 +
    1.17 +NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo)
    1.18 +
    1.19 +nsUserInfo::nsUserInfo() {}
    1.20 +
    1.21 +NS_IMETHODIMP
    1.22 +nsUserInfo::GetFullname(char16_t **aFullname)
    1.23 +{
    1.24 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT
    1.25 +  
    1.26 +  NS_ConvertUTF8toUTF16 fullName([NSFullUserName() UTF8String]);
    1.27 +  *aFullname = ToNewUnicode(fullName);
    1.28 +  return NS_OK;
    1.29 +  
    1.30 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT
    1.31 +}
    1.32 +
    1.33 +NS_IMETHODIMP 
    1.34 +nsUserInfo::GetUsername(char **aUsername)
    1.35 +{
    1.36 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT
    1.37 +  
    1.38 +  nsAutoCString username([NSUserName() UTF8String]);
    1.39 +  *aUsername = ToNewCString(username);
    1.40 +  return NS_OK;
    1.41 +  
    1.42 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT
    1.43 +}
    1.44 +
    1.45 +nsresult 
    1.46 +nsUserInfo::GetPrimaryEmailAddress(nsCString &aEmailAddress)
    1.47 +{
    1.48 +  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT
    1.49 +  
    1.50 +  // Try to get this user's primary email from the system addressbook's "me card" 
    1.51 +  // (if they've filled it)
    1.52 +  ABPerson *me = [[ABAddressBook sharedAddressBook] me];
    1.53 +  ABMultiValue *emailAddresses = [me valueForProperty:kABEmailProperty];
    1.54 +  if ([emailAddresses count] > 0) {
    1.55 +    // get the index of the primary email, in case there are more than one
    1.56 +    int primaryEmailIndex = [emailAddresses indexForIdentifier:[emailAddresses primaryIdentifier]];
    1.57 +    aEmailAddress.Assign([[emailAddresses valueAtIndex:primaryEmailIndex] UTF8String]);
    1.58 +    return NS_OK;
    1.59 +  }
    1.60 +  
    1.61 +  return NS_ERROR_FAILURE;
    1.62 +  
    1.63 +  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT
    1.64 +}
    1.65 +
    1.66 +NS_IMETHODIMP 
    1.67 +nsUserInfo::GetEmailAddress(char **aEmailAddress)
    1.68 +{
    1.69 +  nsAutoCString email;
    1.70 +  if (NS_SUCCEEDED(GetPrimaryEmailAddress(email))) 
    1.71 +    *aEmailAddress = ToNewCString(email);
    1.72 +  return NS_OK;
    1.73 +}
    1.74 +
    1.75 +NS_IMETHODIMP 
    1.76 +nsUserInfo::GetDomain(char **aDomain)
    1.77 +{
    1.78 +  nsAutoCString email;
    1.79 +  if (NS_SUCCEEDED(GetPrimaryEmailAddress(email))) {
    1.80 +    int32_t index = email.FindChar('@');
    1.81 +    if (index != -1) {
    1.82 +      // chop off everything before, and including the '@'
    1.83 +      *aDomain = ToNewCString(Substring(email, index + 1));
    1.84 +    }
    1.85 +  }
    1.86 +  return NS_OK;
    1.87 +}

mercurial