1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_MACAB_MACABUTILITIES_HXX
21 #define INCLUDED_CONNECTIVITY_SOURCE_DRIVERS_MACAB_MACABUTILITIES_HXX
22 
23 #include <com/sun/star/util/DateTime.hpp>
24 #include <com/sun/star/sdbc/DataType.hpp>
25 
26 #include <time.h>
27 #include <premac.h>
28 #include <Carbon/Carbon.h>
29 #include <AddressBook/ABAddressBookC.h>
30 #include <postmac.h>
31 
32 namespace connectivity
33 {
34     namespace macab
35     {
36 
37         inline OUString CFStringToOUString(const CFStringRef sOrig)
38         {
39             /* Copied all-but directly from code by Florian Heckl in
40              * cws_src680_aquafilepicker01
41              * File was: fpicker/source/aqua/CFStringUtilities
42              * I only removed commented debugging lines and changed variable
43              * names.
44              */
45             if (nullptr == sOrig) {
46                     return OUString();
47             }
48 
49             CFRetain(sOrig);
50             CFIndex nStringLength = CFStringGetLength(sOrig);
51 
52             UniChar unichars[nStringLength+1];
53 
54             //'close' the string buffer correctly
55             unichars[nStringLength] = '\0';
56 
57             CFStringGetCharacters (sOrig, CFRangeMake(0,nStringLength), unichars);
58             CFRelease(sOrig);
59 
60             return OUString(reinterpret_cast<sal_Unicode *>(unichars));
61         }
62 
63 
64         inline CFStringRef OUStringToCFString(const OUString& aString)
65         {
66             /* Copied directly from code by Florian Heckl in
67              * cws_src680_aquafilepicker01
68              * File was: fpicker/source/aqua/CFStringUtilities
69              */
70 
71             CFStringRef ref = CFStringCreateWithCharacters(kCFAllocatorDefault, reinterpret_cast<UniChar const *>(aString.getStr()), aString.getLength());
72 
73             return ref;
74         }
75 
76 
77         inline css::util::DateTime CFDateToDateTime(const CFDateRef _cfDate)
78         {
79                 /* Carbon can give us the time since 2001 of any CFDateRef,
80                  * and it also stores the time since 1970 as a constant,
81                  * basically allowing us to get the unixtime of any
82                  * CFDateRef. From there, it is just a matter of choosing what
83                  * we want to do with it.
84                  */
85             css::util::DateTime nRet;
86             double timeSince2001 = CFDateGetAbsoluteTime(_cfDate);
87             time_t unixtime = timeSince2001+kCFAbsoluteTimeIntervalSince1970;
88             struct tm *ptm = localtime(&unixtime);
89             nRet.Year = ptm->tm_year+1900;
90             nRet.Month = ptm->tm_mon+1;
91             nRet.Day = ptm->tm_mday;
92             nRet.Hours = ptm->tm_hour;
93             nRet.Minutes = ptm->tm_min;
94             nRet.Seconds = ptm->tm_sec;
95             nRet.NanoSeconds = 0;
96             return nRet;
97         }
98 
99 
100         inline OUString fixLabel(const OUString& _originalLabel)
101         {
102             /* Get the length, and make sure that there is actually a string
103              * here.
104              */
105             if(_originalLabel.startsWith("_$!<"))
106             {
107                 return _originalLabel.copy(4,_originalLabel.getLength()-8);
108             }
109 
110             return _originalLabel;
111         }
112 
113 
114         inline sal_Int32 ABTypeToDataType(const ABPropertyType _abType)
115         {
116             sal_Int32 dataType;
117             switch(_abType)
118             {
119                 case kABStringProperty:
120                     dataType = css::sdbc::DataType::CHAR;
121                     break;
122                 case kABDateProperty:
123                     dataType = css::sdbc::DataType::TIMESTAMP;
124                     break;
125                 case kABIntegerProperty:
126                     dataType = css::sdbc::DataType::INTEGER;
127                     break;
128                 case kABRealProperty:
129                     dataType = css::sdbc::DataType::FLOAT;
130                     break;
131                 default:
132                     dataType = -1;
133             }
134             return dataType;
135         }
136 
137         void impl_throwError(const char* pErrorId);
138     }
139 }
140 
141 #endif // _ CONNECTIVITY_MACAB_UTILITIES_HXX_
142 
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
144