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 #ifndef _CONNECTIVITY_MACAB_RECORDS_HXX_
25 #define _CONNECTIVITY_MACAB_RECORDS_HXX_
26 
27 #include "MacabRecord.hxx"
28 #include "MacabHeader.hxx"
29 
30 #include <premac.h>
31 #include <Carbon/Carbon.h>
32 #include <AddressBook/ABAddressBookC.h>
33 #include <postmac.h>
34 #include <com/sun/star/util/DateTime.hpp>
35 
36 namespace connectivity
37 {
38 	namespace macab
39 	{
40 		/* This struct is for converting CF types to AB types (Core Foundation
41 		 * types to Address Book types).
42 		 */
43 		struct lcl_CFType {
44 			sal_Int32 cf;
45 			sal_Int32 ab;
46 		};
47 
48 		class MacabRecords{
49 			protected:
50 				/* MacabRecords is, at its core, a table of macabfields, in the
51 				 * form of a header and a list of objects of type MacabRecord.
52 				 * It also has a unique name that refers to the name of the table.
53 				 */
54 				sal_Int32 recordsSize;
55 				sal_Int32 currentRecord;
56 				CFStringRef recordType;
57 				MacabHeader *header;
58 				MacabRecord **records;
59 				ABAddressBookRef addressBook;
60 				::rtl::OUString m_sName;
61 
62 				/* For converting CF types to AB types */
63 				sal_Int32 lcl_CFTypesLength;
64 				lcl_CFType *lcl_CFTypes;
65 
66 				/* For required properties */
67 				CFStringRef *requiredProperties;
68 				sal_Int32 numRequiredProperties;
69 
70 			private:
71 				/* All of the private methods are for creating a MacabHeader or a
72 				 * MacabRecord. They are used by the initialize method that goes
73 				 * about filling a MacabRecords using all of the records in the
74 				 * Mac OS X Address Book.
75 				 */
76 				void bootstrap_CF_types();
77 				void bootstrap_requiredProperties();
78 				MacabHeader *createHeaderForProperty(const ABRecordRef _record, const CFStringRef _propertyName, const CFStringRef _recordType, const sal_Bool _isPropertyRequired) const;
79 				MacabHeader *createHeaderForProperty(const ABPropertyType _propertyType, const CFTypeRef _propertyValue, const CFStringRef _propertyName) const;
80 				void manageDuplicateHeaders(macabfield **_headerNames, const sal_Int32 _length) const;
81 				ABPropertyType getABTypeFromCFType(const CFTypeID cf_type ) const;
82 				void insertPropertyIntoMacabRecord(MacabRecord *_abrecord, const MacabHeader *_header, const ::rtl::OUString  _propertyName, const CFTypeRef _propertyValue) const;
83 				void insertPropertyIntoMacabRecord(const ABPropertyType _propertyType, MacabRecord *_abrecord, const MacabHeader *_header, const ::rtl::OUString  _propertyName, const CFTypeRef _propertyValue) const;
84 			public:
85 				MacabRecords(const ABAddressBookRef _addressBook, MacabHeader *_header, MacabRecord **_records, sal_Int32 _numRecords);
86 				MacabRecords(const MacabRecords *_copy);
87 				MacabRecords(const ABAddressBookRef _addressBook);
88 				~MacabRecords();
89 
90 				void initialize();
91 
92 				void setHeader(MacabHeader *_header);
93 				MacabHeader *getHeader() const;
94 
95 				void setName(const ::rtl::OUString _sName);
96 				::rtl::OUString getName() const;
97 
98 				MacabRecord *insertRecord(MacabRecord *_newRecord, const sal_Int32 _location);
99 				void insertRecord(MacabRecord *_newRecord);
100 				MacabRecord *getRecord(const sal_Int32 _location) const;
101 				void swap(const sal_Int32 _id1, const sal_Int32 _id2);
102 
103 				macabfield *getField(const sal_Int32 _recordNumber, const sal_Int32 _columnNumber) const;
104 				macabfield *getField(const sal_Int32 _recordNumber, const ::rtl::OUString  _columnName) const;
105 				sal_Int32 getFieldNumber(const ::rtl::OUString  _columnName) const;
106 
107 				sal_Int32 size() const;
108 
109 				MacabHeader *createHeaderForRecordType(const CFArrayRef _records, const CFStringRef _recordType) const;
110 				MacabRecord *createMacabRecord(const ABRecordRef _abrecord, const MacabHeader *_header, const CFStringRef _recordType) const;
111 
112 				MacabRecords *begin();
113 				sal_Int32 end() const;
114 				class iterator{
115 					protected:
116 						MacabRecords *records;
117 					public:
118 						sal_Int32 id;
119 						void operator= (MacabRecords *_records);
120 						iterator();
121 						~iterator();
122 						void operator++ ();
123 						sal_Bool operator!= (const sal_Int32 i) const;
124 						sal_Bool operator== (const sal_Int32 i) const;
125 						MacabRecord *operator* () const;
126 				};
127 
128 		};
129 	}
130 }
131 
132 #endif // _CONNECTIVITY_MACAB_RECORDS_HXX_
133