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 #include <osl/diagnose.h>
29 #include "CFStringUtilities.hxx"
30 
31 rtl::OUString CFStringToOUString(const CFStringRef sOrig) {
32     //DBG_PRINT_ENTRY("CFStringUtilities", __func__, "sOrig", sOrig);
33 
34     if (NULL == sOrig) {
35         return rtl::OUString();
36     }
37 
38     CFRetain(sOrig);
39     CFIndex nFileNameLength = CFStringGetLength(sOrig);
40     //OSL_TRACE("FH: string length: %d", (int)(nFileNameLength));
41     UniChar unichars[nFileNameLength+1];
42     //'close' the string buffer correctly
43     unichars[nFileNameLength] = '\0';
44 
45     CFStringGetCharacters (sOrig, CFRangeMake(0,nFileNameLength), unichars);
46 
47     //we no longer need the original string
48     CFRelease(sOrig);
49 
50     //DBG_PRINT_EXIT("CFStringUtilities", __func__, unichars);
51 
52     return rtl::OUString(unichars);
53 }
54 
55 CFStringRef CFStringCreateWithOUString(const rtl::OUString& aString) {
56     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
57 
58     CFStringRef ref = CFStringCreateWithCharacters(kCFAllocatorDefault, aString.getStr(), aString.getLength());
59 
60     //DBG_PRINT_EXIT("CFStringUtilities", __func__, ref);
61 
62     return ref;
63 }
64 
65 rtl::OUString FSRefToOUString(FSRef fsRef, InfoType info)
66 {
67     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
68 
69     CFURLRef aUrlRef = CFURLCreateFromFSRef(NULL, &fsRef);
70 
71     rtl::OUString sResult = CFURLRefToOUString(aUrlRef, info);
72 
73     //we no longer need the CFURLRef
74     CFRelease(aUrlRef);
75 
76     //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
77 
78     return sResult;
79 }
80 
81 rtl::OUString CFURLRefToOUString(CFURLRef aUrlRef, InfoType info)
82 {
83     //DBG_PRINT_ENTRY("CFStringUtilities", __func__);
84 
85     CFStringRef sURLString = NULL;
86 
87     switch(info) {
88         case FULLPATH:
89             OSL_TRACE("Extracting the full path of an item");
90             sURLString = CFURLGetString(aUrlRef);
91             CFRetain(sURLString);
92             break;
93         case FILENAME:
94             OSL_TRACE("Extracting the file name of an item");
95             CFStringRef fullString = CFURLGetString(aUrlRef);
96             CFURLRef dirRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
97             CFIndex dirLength = CFStringGetLength(CFURLGetString(dirRef));
98             CFRelease(dirRef);
99             CFIndex fullLength = CFStringGetLength(fullString);
100             CFRange substringRange = CFRangeMake(dirLength, fullLength - dirLength);
101             sURLString = CFStringCreateWithSubstring(NULL, fullString, substringRange);
102             break;
103         case PATHWITHOUTLASTCOMPONENT:
104             OSL_TRACE("Extracting the last but one component of an item's path");
105             CFURLRef directoryRef = CFURLCreateCopyDeletingLastPathComponent(NULL,aUrlRef);
106             sURLString = CFURLGetString(directoryRef);
107             CFRetain(sURLString);
108             CFRelease(directoryRef);
109             break;
110         default:
111             break;
112     }
113 
114     rtl::OUString sResult = CFStringToOUString(sURLString);
115 
116     CFRelease(sURLString);
117 
118     //DBG_PRINT_EXIT("CFStringUtilities", __func__, OUStringToOString(sResult, RTL_TEXTENCODING_UTF8).getStr());
119 
120     return sResult;
121 }
122