1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements.  See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership.  The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License.  You may obtain a copy of the License at
10 *
11 *   http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied.  See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24#ifndef _NSSTRING_OOOADDITIONS_HXX_
25#include "NSString_OOoAdditions.hxx"
26#endif
27
28#include "NSURL_OOoAdditions.hxx"
29
30@implementation NSURL (OOoAdditions)
31- (rtl::OUString) OUStringForInfo:(InfoType)info
32{
33    NSAutoreleasePool *pool = [NSAutoreleasePool new];
34
35    NSString *sURLString = nil;
36
37    switch(info) {
38        case FULLPATH:
39            OSL_TRACE("Extracting the full path of an item");
40            sURLString = [self absoluteString];
41            [sURLString retain];
42            break;
43        case FILENAME:
44            OSL_TRACE("Extracting the file name of an item");
45            NSString *path = [self path];
46            if (path == nil) {
47                sURLString = @"";
48            }
49            else {
50                sURLString = [path lastPathComponent];
51            }
52            [sURLString retain];
53            break;
54        case PATHWITHOUTLASTCOMPONENT:
55            OSL_TRACE("Extracting the last but one component of an item's path");
56            path = [self absoluteString];
57            if (path == nil) {
58                sURLString = @"";
59            }
60            else {
61                NSString* lastComponent = [path lastPathComponent];
62                unsigned int lastLength = [lastComponent length];
63                sURLString = [path substringToIndex:([path length] - lastLength)];
64            }
65            [sURLString retain];
66            break;
67        default:
68            break;
69    }
70
71    rtl::OUString sResult = [sURLString OUString];
72    [sURLString release];
73
74    [pool release];
75
76    return sResult;
77}
78@end
79
80NSString* resolveAlias( NSString* i_pSystemPath )
81{
82    NSString* pResolvedPath = nil;
83    CFURLRef rUrl = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,
84                                                   (CFStringRef)i_pSystemPath,
85                                                   kCFURLPOSIXPathStyle, false);
86    if( rUrl != NULL )
87    {
88        FSRef rFS;
89        if( CFURLGetFSRef( rUrl, &rFS ) )
90        {
91            Boolean bIsFolder = false;
92            Boolean bAlias = false;
93            OSErr err = FSResolveAliasFile( &rFS, true, &bIsFolder, &bAlias);
94            if( (err == noErr) && bAlias )
95            {
96                CFURLRef rResolvedUrl = CFURLCreateFromFSRef( kCFAllocatorDefault, &rFS );
97                if( rResolvedUrl != NULL )
98                {
99                    pResolvedPath = (NSString*)CFURLCopyFileSystemPath( rResolvedUrl, kCFURLPOSIXPathStyle );
100                    CFRelease( rResolvedUrl );
101                }
102            }
103        }
104        CFRelease( rUrl );
105    }
106
107    return pResolvedPath;
108}
109