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