]> err.no Git - sope/blob - sope-core/NGExtensions/NGFileManagerURL.m
Drop apache 1 build-dependency
[sope] / sope-core / NGExtensions / NGFileManagerURL.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 "NGFileManagerURL.h"
23 #include "common.h"
24
25 @interface NGFileManagerURLHandle : NSURLHandle
26 {
27   id<NSObject,NGFileManager> fileManager;
28   NSString          *path;
29   BOOL              shallCache;
30   NSURLHandleStatus status;
31   NSData            *cachedData;
32   NSDictionary      *cachedProperties;
33 }
34 @end
35
36 @implementation NGFileManagerURL
37
38 - (id)initWithPath:(NSString *)_path
39   fileManager:(id<NSObject,NGFileManager>)_fm
40 {
41   static BOOL didRegisterHandleClass = NO;
42   if (!didRegisterHandleClass) {
43     [NSURLHandle registerURLHandleClass:[NGFileManagerURLHandle class]];
44     didRegisterHandleClass = YES;
45   }
46   
47   self->path        = [[_fm standardizePath:_path] copy];
48   self->fileManager = [_fm retain];
49   return self;
50 }
51
52 - (void)dealloc {
53   [self->path        release];
54   [self->fileManager release];
55   [super dealloc];
56 }
57
58 /* accessors */
59
60 - (id<NSObject,NGFileManager>)fileManager {
61   return self->fileManager;
62 }
63
64 - (NSString *)fragment {
65   return nil;
66 }
67 - (NSString *)host {
68   return nil;
69 }
70 - (NSString *)path {
71   return self->path;
72 }
73 - (NSString *)scheme {
74   return nil;
75 }
76 - (NSString *)user {
77   return nil;
78 }
79 - (NSString *)password {
80   return nil;
81 }
82 - (NSNumber *)port {
83   return nil;
84 }
85 - (NSString *)query {
86   return nil;
87 }
88
89 - (BOOL)isFileURL {
90   return NO;
91 }
92
93 @end /* NGFileManagerURL */
94
95 @implementation NGFileManagerURLHandle
96
97 + (BOOL)canInitWithURL:(NSURL *)_url {
98   return [_url isKindOfClass:[NGFileManagerURL class]] ? YES : NO;
99 }
100
101 - (id)initWithURL:(NSURL *)_url cached:(BOOL)_flag {
102   if (![[self class] canInitWithURL:_url]) {
103     [self release];
104     return nil;
105   }
106
107   self->fileManager = [[(NGFileManagerURL *)_url fileManager] retain];
108   self->path        = [[_url path] copy];
109   self->shallCache  = _flag;
110   self->status      = NSURLHandleNotLoaded;
111   return self;
112 }
113 - (void)dealloc {
114   [self->cachedData  release];
115   [self->cachedProperties release];
116   [self->path        release];
117   [self->fileManager release];
118   [super dealloc];
119 }
120
121 - (NSData *)loadInForeground {
122   [self->cachedProperties release]; self->cachedProperties = nil;
123   [self->cachedData       release]; self->cachedData       = nil;
124   
125   self->cachedData = [[self->fileManager contentsAtPath:self->path] retain];
126   self->cachedProperties =
127     [[self->fileManager fileAttributesAtPath:self->path traverseLink:YES]
128                         copy];
129   
130   return self->cachedData;
131 }
132 - (void)loadInBackground {
133   [self loadInBackground];
134 }
135
136 - (void)flushCachedData {
137   [self->cachedData       release]; self->cachedData       = nil;
138   [self->cachedProperties release]; self->cachedProperties = nil;
139 }
140
141 - (NSData *)resourceData {
142   NSData *data;
143   
144   if (self->cachedData)
145     return [[self->cachedData copy] autorelease];
146   
147   data = [self loadInForeground];
148   data = [data copy];
149   
150   if (!self->shallCache)
151     [self flushCachedData];
152   
153   return [data autorelease];
154 }
155
156 - (NSData *)availableResourceData {
157   return [[self->cachedData copy] autorelease];
158 }
159
160 - (NSURLHandleStatus)status {
161   return self->status;
162 }
163 - (NSString *)failureReason {
164   if (self->status != NSURLHandleLoadFailed)
165     return nil;
166   
167   return @"loading of URL failed";
168 }
169
170 /* properties */
171
172 - (id)propertyForKey:(NSString *)_key {
173   if (self->cachedProperties)
174     return [self->cachedProperties objectForKey:_key];
175   
176   if ([self loadInForeground]) {
177     id value;
178     
179     value = [self->cachedProperties objectForKey:_key];
180     value = [value retain];
181     
182     if (!self->shallCache)
183       [self flushCachedData];
184
185     return [value autorelease];
186   }
187   else {
188     [self flushCachedData];
189     return nil;
190   }
191 }
192 - (id)propertyForKeyIfAvailable:(NSString *)_key {
193   return [self->cachedProperties objectForKey:_key];
194 }
195
196 /* writing */
197
198 - (BOOL)writeData:(NSData *)_data {
199   [self flushCachedData];
200
201   return NO;
202 }
203
204 @end /* NGFileManagerURLHandle */