1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _CONNECTIVITY_MACAB_UTILITIES_HXX_
29 #define _CONNECTIVITY_MACAB_UTILITIES_HXX_
30 
31 #include <com/sun/star/util/DateTime.hpp>
32 #include <com/sun/star/sdbc/DataType.hpp>
33 
34 #include <time.h>
35 #include <premac.h>
36 #include <Carbon/Carbon.h>
37 #include <AddressBook/ABAddressBookC.h>
38 #include <postmac.h>
39 
40 namespace connectivity
41 {
42 	namespace macab
43 	{
44 		// -------------------------------------------------------------------------
45 		inline ::rtl::OUString CFStringToOUString(const CFStringRef sOrig)
46 		{
47 			/* Copied all-but directly from code by Florian Heckl in
48 			 * cws_src680_aquafilepicker01
49 			 * File was: fpicker/source/aqua/CFStringUtilities
50 			 * I only removed commented debugging lines and changed variable
51 			 * names.
52 			 */
53 			if (NULL == sOrig) {
54 					return rtl::OUString();
55 			}
56 
57 			CFRetain(sOrig);
58 			CFIndex nStringLength = CFStringGetLength(sOrig);
59 
60 			UniChar unichars[nStringLength+1];
61 
62 			//'close' the string buffer correctly
63 			unichars[nStringLength] = '\0';
64 
65 			CFStringGetCharacters (sOrig, CFRangeMake(0,nStringLength), unichars);
66 			CFRelease(sOrig);
67 
68 			return rtl::OUString(unichars);
69 		}
70 
71 		// -------------------------------------------------------------------------
72 		inline CFStringRef OUStringToCFString(const ::rtl::OUString& aString)
73 		{
74 			/* Copied directly from code by Florian Heckl in
75 			 * cws_src680_aquafilepicker01
76 			 * File was: fpicker/source/aqua/CFStringUtilities
77 			 */
78 
79 			CFStringRef ref = CFStringCreateWithCharacters(kCFAllocatorDefault, aString.getStr(), aString.getLength());
80 
81 			return ref;
82 		}
83 
84 		// -------------------------------------------------------------------------
85 		inline com::sun::star::util::DateTime CFDateToDateTime(const CFDateRef _cfDate)
86 		{
87 				/* Carbon can give us the time since 2001 of any CFDateRef,
88 				 * and it also stores the time since 1970 as a constant,
89 				 * basically allowing us to get the unixtime of any
90 				 * CFDateRef. From there, it is just a matter of choosing what
91 				 * we want to do with it.
92 				 */
93 			com::sun::star::util::DateTime nRet;
94 			double timeSince2001 = CFDateGetAbsoluteTime(_cfDate);
95 			time_t unixtime = timeSince2001+kCFAbsoluteTimeIntervalSince1970;
96 			struct tm *ptm = localtime(&unixtime);
97 			nRet.Year = ptm->tm_year+1900;
98 			nRet.Month = ptm->tm_mon+1;
99 			nRet.Day = ptm->tm_mday;
100 			nRet.Hours = ptm->tm_hour;
101 			nRet.Minutes = ptm->tm_min;
102 			nRet.Seconds = ptm->tm_sec;
103 			nRet.HundredthSeconds = 0;
104 			return nRet;
105 		}
106 
107 		// -------------------------------------------------------------------------
108 		inline ::rtl::OUString fixLabel(const ::rtl::OUString _originalLabel)
109 		{
110 			/* Get the length, and make sure that there is actually a string
111 			 * here.
112 			 */
113 			if(_originalLabel.indexOf(::rtl::OUString::createFromAscii("_$!<")) == 0)
114 			{
115 				return _originalLabel.copy(4,_originalLabel.getLength()-8);
116 			}
117 
118 			return _originalLabel;
119 		}
120 
121 		// -------------------------------------------------------------------------
122 		inline sal_Int32 ABTypeToDataType(const ABPropertyType _abType)
123 		{
124 			sal_Int32 dataType;
125 			switch(_abType)
126 			{
127 				case kABStringProperty:
128 					dataType = ::com::sun::star::sdbc::DataType::CHAR;
129 					break;
130 				case kABDateProperty:
131 					dataType = ::com::sun::star::sdbc::DataType::TIMESTAMP;
132 					break;
133 				case kABIntegerProperty:
134 					dataType = ::com::sun::star::sdbc::DataType::INTEGER;
135 					break;
136 				case kABRealProperty:
137 					dataType = ::com::sun::star::sdbc::DataType::FLOAT;
138 					break;
139 				default:
140 					dataType = -1;
141 			}
142 			return dataType;
143 		}
144 
145         void impl_throwError(sal_uInt16 _nErrorId);
146 	}
147 }
148 
149 #endif // _ CONNECTIVITY_MACAB_UTILITIES_HXX_
150