|
1 /* -*- Mode: Objective-C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 #include "nsUserInfoMac.h" |
|
8 #include "nsObjCExceptions.h" |
|
9 #include "nsString.h" |
|
10 |
|
11 #import <Cocoa/Cocoa.h> |
|
12 #import <AddressBook/AddressBook.h> |
|
13 |
|
14 NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo) |
|
15 |
|
16 nsUserInfo::nsUserInfo() {} |
|
17 |
|
18 NS_IMETHODIMP |
|
19 nsUserInfo::GetFullname(char16_t **aFullname) |
|
20 { |
|
21 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT |
|
22 |
|
23 NS_ConvertUTF8toUTF16 fullName([NSFullUserName() UTF8String]); |
|
24 *aFullname = ToNewUnicode(fullName); |
|
25 return NS_OK; |
|
26 |
|
27 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT |
|
28 } |
|
29 |
|
30 NS_IMETHODIMP |
|
31 nsUserInfo::GetUsername(char **aUsername) |
|
32 { |
|
33 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT |
|
34 |
|
35 nsAutoCString username([NSUserName() UTF8String]); |
|
36 *aUsername = ToNewCString(username); |
|
37 return NS_OK; |
|
38 |
|
39 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT |
|
40 } |
|
41 |
|
42 nsresult |
|
43 nsUserInfo::GetPrimaryEmailAddress(nsCString &aEmailAddress) |
|
44 { |
|
45 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT |
|
46 |
|
47 // Try to get this user's primary email from the system addressbook's "me card" |
|
48 // (if they've filled it) |
|
49 ABPerson *me = [[ABAddressBook sharedAddressBook] me]; |
|
50 ABMultiValue *emailAddresses = [me valueForProperty:kABEmailProperty]; |
|
51 if ([emailAddresses count] > 0) { |
|
52 // get the index of the primary email, in case there are more than one |
|
53 int primaryEmailIndex = [emailAddresses indexForIdentifier:[emailAddresses primaryIdentifier]]; |
|
54 aEmailAddress.Assign([[emailAddresses valueAtIndex:primaryEmailIndex] UTF8String]); |
|
55 return NS_OK; |
|
56 } |
|
57 |
|
58 return NS_ERROR_FAILURE; |
|
59 |
|
60 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT |
|
61 } |
|
62 |
|
63 NS_IMETHODIMP |
|
64 nsUserInfo::GetEmailAddress(char **aEmailAddress) |
|
65 { |
|
66 nsAutoCString email; |
|
67 if (NS_SUCCEEDED(GetPrimaryEmailAddress(email))) |
|
68 *aEmailAddress = ToNewCString(email); |
|
69 return NS_OK; |
|
70 } |
|
71 |
|
72 NS_IMETHODIMP |
|
73 nsUserInfo::GetDomain(char **aDomain) |
|
74 { |
|
75 nsAutoCString email; |
|
76 if (NS_SUCCEEDED(GetPrimaryEmailAddress(email))) { |
|
77 int32_t index = email.FindChar('@'); |
|
78 if (index != -1) { |
|
79 // chop off everything before, and including the '@' |
|
80 *aDomain = ToNewCString(Substring(email, index + 1)); |
|
81 } |
|
82 } |
|
83 return NS_OK; |
|
84 } |