Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / fpicker / source / aqua / CFStringUtilities.mm
blob65faf9a93c50b55af3e48272874debc62bf69a50
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  */
20 #include "sal/log.hxx"
21 #include "CFStringUtilities.hxx"
23 rtl::OUString CFStringToOUString(const CFStringRef sOrig) {
24     //DBG_PRINT_ENTRY("CFStringUtilities", __func__, "sOrig", sOrig);
26     if (NULL == sOrig) {
27         return rtl::OUString();
28     }
30     CFRetain(sOrig);
31     CFIndex nFileNameLength = CFStringGetLength(sOrig);
32     //SAL_INFO("fpicker.aqua","FH: string length: " << (int)(nFileNameLength));
33     UniChar unichars[nFileNameLength+1];
34     //'close' the string buffer correctly
35     unichars[nFileNameLength] = '\0';
37     CFStringGetCharacters (sOrig, CFRangeMake(0,nFileNameLength), unichars);
39     //we no longer need the original string
40     CFRelease(sOrig);
42     //DBG_PRINT_EXIT("CFStringUtilities", __func__, unichars);
44     return rtl::OUString(unichars);
47 CFStringRef CFStringCreateWithOUString(const rtl::OUString& aString) {
48     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
50     CFStringRef ref = CFStringCreateWithCharacters(kCFAllocatorDefault, aString.getStr(), aString.getLength());
52     //DBG_PRINT_EXIT("CFStringUtilities", __func__, ref);
54     return ref;
57 rtl::OUString FSRefToOUString(FSRef fsRef, InfoType info)
59     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
61     CFURLRef aUrlRef = CFURLCreateFromFSRef(NULL, &fsRef);
63     rtl::OUString sResult = CFURLRefToOUString(aUrlRef, info);
65     //we no longer need the CFURLRef
66     CFRelease(aUrlRef);
68     //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
70     return sResult;
73 rtl::OUString CFURLRefToOUString(CFURLRef aUrlRef, InfoType info)
75     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
77     CFStringRef sURLString = NULL;
79     switch(info) {
80         case FULLPATH:
81             SAL_INFO("fpicker.aqua","Extracting the full path of an item");
82             sURLString = CFURLGetString(aUrlRef);
83             CFRetain(sURLString);
84             break;
85         case FILENAME:
86             {
87                 SAL_INFO("fpicker.aqua","Extracting the file name of an item");
88                 CFStringRef fullString = CFURLGetString(aUrlRef);
89                 CFURLRef dirRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
90                 CFIndex dirLength = CFStringGetLength(CFURLGetString(dirRef));
91                 CFRelease(dirRef);
92                 CFIndex fullLength = CFStringGetLength(fullString);
93                 CFRange substringRange = CFRangeMake(dirLength, fullLength - dirLength);
94                 sURLString = CFStringCreateWithSubstring(NULL, fullString, substringRange);
95             }
96             break;
97         case PATHWITHOUTLASTCOMPONENT:
98             {
99                 SAL_INFO("fpicker.aqua","Extracting the last but one component of an item's path");
100                 CFURLRef directoryRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
101                 sURLString = CFURLGetString(directoryRef);
102                 CFRetain(sURLString);
103                 CFRelease(directoryRef);
104             }
105             break;
106         default:
107             break;
108     }
110     rtl::OUString sResult = CFStringToOUString(sURLString);
112     CFRelease(sURLString);
114     //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
116     return sResult;
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */