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