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#ifndef _NSSTRING_OOOADDITIONS_HXX_
29#include "NSString_OOoAdditions.hxx"
30#endif
31
32#include "NSURL_OOoAdditions.hxx"
33
34@implementation NSURL (OOoAdditions)
35- (rtl::OUString) OUStringForInfo:(InfoType)info
36{
37    NSAutoreleasePool *pool = [NSAutoreleasePool new];
38
39    NSString *sURLString = nil;
40
41    switch(info) {
42        case FULLPATH:
43            OSL_TRACE("Extracting the full path of an item");
44            sURLString = [self absoluteString];
45            [sURLString retain];
46            break;
47        case FILENAME:
48            OSL_TRACE("Extracting the file name of an item");
49            NSString *path = [self path];
50            if (path == nil) {
51                sURLString = @"";
52            }
53            else {
54                sURLString = [path lastPathComponent];
55            }
56            [sURLString retain];
57            break;
58        case PATHWITHOUTLASTCOMPONENT:
59            OSL_TRACE("Extracting the last but one component of an item's path");
60            path = [self absoluteString];
61            if (path == nil) {
62                sURLString = @"";
63            }
64            else {
65                NSString* lastComponent = [path lastPathComponent];
66                unsigned int lastLength = [lastComponent length];
67                sURLString = [path substringToIndex:([path length] - lastLength)];
68            }
69            [sURLString retain];
70            break;
71        default:
72            break;
73    }
74
75    rtl::OUString sResult = [sURLString OUString];
76    [sURLString release];
77
78    [pool release];
79
80    return sResult;
81}
82@end
83
84NSString* resolveAlias( NSString* i_pSystemPath )
85{
86    NSString* pResolvedPath = nil;
87    CFURLRef rUrl = CFURLCreateWithFileSystemPath( kCFAllocatorDefault,
88                                                   (CFStringRef)i_pSystemPath,
89                                                   kCFURLPOSIXPathStyle, false);
90    if( rUrl != NULL )
91    {
92        FSRef rFS;
93        if( CFURLGetFSRef( rUrl, &rFS ) )
94        {
95            Boolean bIsFolder = false;
96            Boolean bAlias = false;
97            OSErr err = FSResolveAliasFile( &rFS, true, &bIsFolder, &bAlias);
98            if( (err == noErr) && bAlias )
99            {
100                CFURLRef rResolvedUrl = CFURLCreateFromFSRef( kCFAllocatorDefault, &rFS );
101                if( rResolvedUrl != NULL )
102                {
103                    pResolvedPath = (NSString*)CFURLCopyFileSystemPath( rResolvedUrl, kCFURLPOSIXPathStyle );
104                    CFRelease( rResolvedUrl );
105                }
106            }
107        }
108        CFRelease( rUrl );
109    }
110
111    return pResolvedPath;
112}
113