]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSFileManager+Extensions.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / FdExt.subproj / NSFileManager+Extensions.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "NSFileManager+Extensions.h"
23 #include "NGFileFolderInfoDataSource.h"
24 #import <EOControl/EOGlobalID.h>
25 #include "common.h"
26
27 @interface NSFileManagerGlobalID : EOGlobalID < NSCopying >
28 {
29 @public
30   NSString *path;
31 }
32 @end
33
34 // TODO: support -lastException
35 // TODO: add stuff like -dictionaryAtPath:, -arrayAtPath:, -propertyListAtPath:
36
37 @implementation NSFileManager(ExtendedFileManagerImp)
38
39 /* directories */
40
41 - (BOOL)createDirectoriesAtPath:(NSString *)_p attributes:(NSDictionary *)_a {
42   unsigned i, count;
43   NSArray *pc;
44   BOOL    isDir;
45   
46   if ([_p length] == 0)
47     return NO;
48   if ([self fileExistsAtPath:_p isDirectory:&isDir])
49     return isDir;
50   
51   pc = [_p pathComponents];
52   if ((count = [pc count]) == 0)
53     return YES;
54   
55   for (i = 0; i < count; i++) {
56     NSString *fp;
57     NSRange  r;
58     
59     r.location = 0;
60     r.length   = i + 1;
61     
62     fp = [NSString pathWithComponents:[pc subarrayWithRange:r]];
63     if ([self fileExistsAtPath:fp isDirectory:&isDir]) {
64       if (!isDir) /* exists, but is a file */
65         return NO;
66       continue;
67     }
68     
69     if (![self createDirectoryAtPath:fp attributes:_a])
70       /* failed to create */
71       return NO;
72   }
73   return YES;
74 }
75
76 /* path modifications */
77
78 - (NSString *)standardizePath:(NSString *)_path {
79   if (![_path isAbsolutePath])
80     _path = [[self currentDirectoryPath] stringByAppendingPathComponent:_path];
81   
82   return [_path stringByStandardizingPath];
83 }
84 - (NSString *)resolveSymlinksInPath:(NSString *)_path {
85   return [_path stringByResolvingSymlinksInPath];
86 }
87 - (NSString *)expandTildeInPath:(NSString *)_path {
88   return [_path stringByExpandingTildeInPath];
89 }
90
91 /* feature check */
92
93 - (BOOL)supportsVersioningAtPath:(NSString *)_path {
94   return NO;
95 }
96 - (BOOL)supportsLockingAtPath:(NSString *)_path {
97   return NO;
98 }
99 - (BOOL)supportsFolderDataSourceAtPath:(NSString *)_path {
100   return YES;
101 }
102
103 - (BOOL)supportsFeature:(NSString *)_featureURI atPath:(NSString *)_path {
104   if ([_featureURI isEqualToString:NGFileManagerFeature_DataSources])
105     return YES;
106   
107   return NO;
108 }
109
110 /* writing */
111
112 - (BOOL)writeContents:(NSData *)_content atPath:(NSString *)_path {
113   return [_content writeToFile:_path atomically:YES];
114 }
115
116 /* global-IDs */
117
118 - (EOGlobalID *)globalIDForPath:(NSString *)_path {
119   NSFileManagerGlobalID *gid;
120   
121   _path = [self standardizePath:_path];
122   
123   gid = [[NSFileManagerGlobalID alloc] init];
124   gid->path = [_path copy];
125   return [gid autorelease];
126 }
127 - (NSString *)pathForGlobalID:(EOGlobalID *)_gid {
128   NSFileManagerGlobalID *gid;
129   
130   if (![_gid isKindOfClass:[NSFileManagerGlobalID class]])
131     /* not a gid we can handle ... */
132     return nil;
133   
134   gid = (NSFileManagerGlobalID *)_gid;
135   return gid->path;
136 }
137
138 /* datasources (work on folders) */
139
140 - (EODataSource *)dataSourceAtPath:(NSString *)_path {
141   return [[[NGFileFolderInfoDataSource alloc] initWithFolderPath:_path] 
142            autorelease];
143 }
144
145 - (EODataSource *)dataSource {
146   return [self dataSourceAtPath:[self currentDirectoryPath]];
147 }
148
149 /* trash */
150
151 - (BOOL)supportsTrashFolderAtPath:(NSString *)_path {
152   return NO;
153 }
154 - (NSString *)trashFolderForPath:(NSString *)_path {
155   return nil;
156 }
157
158 - (BOOL)trashFileAtPath:(NSString *)_path handler:(id)_handler {
159   // TODO: support trashfolder on MacOSX ?
160   return NO;
161 }
162
163 @end /* NSFileManager(ExtendedFileManagerImp) */
164
165 @implementation NSFileManagerGlobalID
166
167 - (void)dealloc {
168   [self->path release];
169   [super dealloc];
170 }
171
172 /* NSCopying */
173
174 - (id)copyWithZone:(NSZone *)_zone {
175   /* global IDs are immutable, so we can return an retained object ... */
176   return [self retain];
177 }
178
179 /* description */
180
181 - (NSString *)description {
182   NSMutableString *ms = [NSMutableString stringWithCapacity:32];
183   
184   [ms appendFormat:@"<0x%p[%@]", self, NSStringFromClass([self class])];
185   [ms appendFormat:@" path=%@", self->path];
186   [ms appendString:@">"];
187   return ms;
188 }
189
190 @end /* NSFileManagerGlobalID */