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_UTILITIES_HXX_
25 #define _CONNECTIVITY_MACAB_UTILITIES_HXX_
26 
27 #include <com/sun/star/util/DateTime.hpp>
28 #include <com/sun/star/sdbc/DataType.hpp>
29 
30 #include <time.h>
31 #include <premac.h>
32 #include <Carbon/Carbon.h>
33 #include <AddressBook/ABAddressBookC.h>
34 #include <postmac.h>
35 
36 namespace connectivity
37 {
38 	namespace macab
39 	{
40 		// -------------------------------------------------------------------------
CFStringToOUString(const CFStringRef sOrig)41 		inline ::rtl::OUString CFStringToOUString(const CFStringRef sOrig)
42 		{
43 			/* Copied all-but directly from code by Florian Heckl in
44 			 * cws_src680_aquafilepicker01
45 			 * File was: fpicker/source/aqua/CFStringUtilities
46 			 * I only removed commented debugging lines and changed variable
47 			 * names.
48 			 */
49 			if (NULL == sOrig) {
50 					return rtl::OUString();
51 			}
52 
53 			CFRetain(sOrig);
54 			CFIndex nStringLength = CFStringGetLength(sOrig);
55 
56 			UniChar unichars[nStringLength+1];
57 
58 			//'close' the string buffer correctly
59 			unichars[nStringLength] = '\0';
60 
61 			CFStringGetCharacters (sOrig, CFRangeMake(0,nStringLength), unichars);
62 			CFRelease(sOrig);
63 
64 			return rtl::OUString(unichars);
65 		}
66 
67 		// -------------------------------------------------------------------------
OUStringToCFString(const::rtl::OUString & aString)68 		inline CFStringRef OUStringToCFString(const ::rtl::OUString& aString)
69 		{
70 			/* Copied directly from code by Florian Heckl in
71 			 * cws_src680_aquafilepicker01
72 			 * File was: fpicker/source/aqua/CFStringUtilities
73 			 */
74 
75 			CFStringRef ref = CFStringCreateWithCharacters(kCFAllocatorDefault, aString.getStr(), aString.getLength());
76 
77 			return ref;
78 		}
79 
80 		// -------------------------------------------------------------------------
CFDateToDateTime(const CFDateRef _cfDate)81 		inline com::sun::star::util::DateTime CFDateToDateTime(const CFDateRef _cfDate)
82 		{
83 				/* Carbon can give us the time since 2001 of any CFDateRef,
84 				 * and it also stores the time since 1970 as a constant,
85 				 * basically allowing us to get the unixtime of any
86 				 * CFDateRef. From there, it is just a matter of choosing what
87 				 * we want to do with it.
88 				 */
89 			com::sun::star::util::DateTime nRet;
90 			double timeSince2001 = CFDateGetAbsoluteTime(_cfDate);
91 			time_t unixtime = timeSince2001+kCFAbsoluteTimeIntervalSince1970;
92 			struct tm *ptm = localtime(&unixtime);
93 			nRet.Year = ptm->tm_year+1900;
94 			nRet.Month = ptm->tm_mon+1;
95 			nRet.Day = ptm->tm_mday;
96 			nRet.Hours = ptm->tm_hour;
97 			nRet.Minutes = ptm->tm_min;
98 			nRet.Seconds = ptm->tm_sec;
99 			nRet.HundredthSeconds = 0;
100 			return nRet;
101 		}
102 
103 		// -------------------------------------------------------------------------
fixLabel(const::rtl::OUString _originalLabel)104 		inline ::rtl::OUString fixLabel(const ::rtl::OUString _originalLabel)
105 		{
106 			/* Get the length, and make sure that there is actually a string
107 			 * here.
108 			 */
109 			if(_originalLabel.indexOf(::rtl::OUString::createFromAscii("_$!<")) == 0)
110 			{
111 				return _originalLabel.copy(4,_originalLabel.getLength()-8);
112 			}
113 
114 			return _originalLabel;
115 		}
116 
117 		// -------------------------------------------------------------------------
ABTypeToDataType(const ABPropertyType _abType)118 		inline sal_Int32 ABTypeToDataType(const ABPropertyType _abType)
119 		{
120 			sal_Int32 dataType;
121 			switch(_abType)
122 			{
123 				case kABStringProperty:
124 					dataType = ::com::sun::star::sdbc::DataType::CHAR;
125 					break;
126 				case kABDateProperty:
127 					dataType = ::com::sun::star::sdbc::DataType::TIMESTAMP;
128 					break;
129 				case kABIntegerProperty:
130 					dataType = ::com::sun::star::sdbc::DataType::INTEGER;
131 					break;
132 				case kABRealProperty:
133 					dataType = ::com::sun::star::sdbc::DataType::FLOAT;
134 					break;
135 				default:
136 					dataType = -1;
137 			}
138 			return dataType;
139 		}
140 
141         void impl_throwError(sal_uInt16 _nErrorId);
142 	}
143 }
144 
145 #endif // _ CONNECTIVITY_MACAB_UTILITIES_HXX_
146