1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9b5730f6SAndrew Rist  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9b5730f6SAndrew Rist  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19*9b5730f6SAndrew Rist  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "MacabGroup.hxx"
28cdf0e10cSrcweir #include "MacabRecords.hxx"
29cdf0e10cSrcweir #include "macabutilities.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace connectivity::macab;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // -------------------------------------------------------------------------
34cdf0e10cSrcweir /* A MacabGroup is basically a MacabRecords with a different constructor.
35cdf0e10cSrcweir  * It only exists as a different entity for clarification purposes (a group
36cdf0e10cSrcweir  * is its own entity in the Mac OS X Address Book) and because its
37cdf0e10cSrcweir  * construction is so unique (it is based on an already existent
38cdf0e10cSrcweir  * MacabRecords of the entire address book).
39cdf0e10cSrcweir  */
MacabGroup(const ABAddressBookRef _addressBook,const MacabRecords * _allRecords,const ABGroupRef _xGroup)40cdf0e10cSrcweir MacabGroup::MacabGroup(const ABAddressBookRef _addressBook, const MacabRecords *_allRecords, const ABGroupRef _xGroup)
41cdf0e10cSrcweir 	: MacabRecords(_addressBook)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 	sal_Int32 i, j, nAllRecordsSize;
44cdf0e10cSrcweir 	CFArrayRef xGroupMembers = ABGroupCopyArrayOfAllMembers(_xGroup);
45cdf0e10cSrcweir 	ABPersonRef xPerson;
46cdf0e10cSrcweir 	CFStringRef sGroupMemberUID;
47cdf0e10cSrcweir 	sal_Bool bFound;
48cdf0e10cSrcweir 	macabfield *xRecordField;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir 	// Set the group's name (stored in MacabRecords as m_sName)
51cdf0e10cSrcweir 	CFStringRef sGroupName;
52cdf0e10cSrcweir 	sGroupName = (CFStringRef) ABRecordCopyValue(_xGroup, kABGroupNameProperty);
53cdf0e10cSrcweir 	m_sName = CFStringToOUString(sGroupName);
54cdf0e10cSrcweir 	CFRelease(sGroupName);
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	// The _group's_ records (remember MacabGroup inherits from MacabRecords)
57cdf0e10cSrcweir 	recordsSize = (sal_Int32) CFArrayGetCount(xGroupMembers);
58cdf0e10cSrcweir 	records = new MacabRecord *[recordsSize];
59cdf0e10cSrcweir 	setHeader(_allRecords->getHeader());
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	/* Go through each record in the group and try to find that record's UID
62cdf0e10cSrcweir 	 * in the MacabRecords that was passed in. If it is found, add that
63cdf0e10cSrcweir 	 * record to the group. Otherwise, report an error. (All records should
64cdf0e10cSrcweir 	 * exist in the MacabRecords that was passed in.)
65cdf0e10cSrcweir 	 */
66cdf0e10cSrcweir 	nAllRecordsSize = _allRecords->size();
67cdf0e10cSrcweir 	for(i = 0; i < recordsSize; i++)
68cdf0e10cSrcweir 	{
69cdf0e10cSrcweir 		xPerson = (ABPersonRef) CFArrayGetValueAtIndex(xGroupMembers,i);
70cdf0e10cSrcweir 		if(xPerson != NULL)
71cdf0e10cSrcweir 		{
72cdf0e10cSrcweir 			sGroupMemberUID = (CFStringRef) ABRecordCopyValue(xPerson, kABUIDProperty);
73cdf0e10cSrcweir 			if(sGroupMemberUID != NULL)
74cdf0e10cSrcweir 			{
75cdf0e10cSrcweir 				bFound = sal_False;
76cdf0e10cSrcweir 				for(j = 0; j < nAllRecordsSize; j++)
77cdf0e10cSrcweir 				{
78cdf0e10cSrcweir 					xRecordField = _allRecords->getField(j,CFStringToOUString(kABUIDProperty));
79cdf0e10cSrcweir 					if(xRecordField != NULL && xRecordField->value != NULL)
80cdf0e10cSrcweir 					{
81cdf0e10cSrcweir 						if(CFEqual(xRecordField->value, sGroupMemberUID))
82cdf0e10cSrcweir 						{
83cdf0e10cSrcweir 							/* Found the matching UID! Insert into the group... */
84cdf0e10cSrcweir 							insertRecord(_allRecords->getRecord(j));
85cdf0e10cSrcweir 							bFound = sal_True;
86cdf0e10cSrcweir 							break;
87cdf0e10cSrcweir 						}
88cdf0e10cSrcweir 					}
89cdf0e10cSrcweir 				}
90cdf0e10cSrcweir 				OSL_ENSURE(bFound, "MacabGroup::MacabGroup : Could not find group member based on UID!\n");
91cdf0e10cSrcweir 				CFRelease(sGroupMemberUID);
92cdf0e10cSrcweir 			}
93cdf0e10cSrcweir 		}
94cdf0e10cSrcweir 	}
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	CFRelease(xGroupMembers);
97cdf0e10cSrcweir }
98cdf0e10cSrcweir 
99