]> err.no Git - sope/blob - libFoundation/Foundation/NSFileURLHandle.m
fixed some NGMail framework build issue
[sope] / libFoundation / Foundation / NSFileURLHandle.m
1 /* 
2    NSFileURLHandle.m
3
4    Copyright (C) 2000 MDlink GmbH, Helge Hess
5    All rights reserved.
6
7    Author: Helge Hess <helge.hess@mdlink.de>
8
9    This file is part of libFoundation.
10
11    Permission to use, copy, modify, and distribute this software and its
12    documentation for any purpose and without fee is hereby granted, provided
13    that the above copyright notice appear in all copies and that both that
14    copyright notice and this permission notice appear in supporting
15    documentation.
16
17    We disclaim all warranties with regard to this software, including all
18    implied warranties of merchantability and fitness, in no event shall
19    we be liable for any special, indirect or consequential damages or any
20    damages whatsoever resulting from loss of use, data or profits, whether in
21    an action of contract, negligence or other tortious action, arising out of
22    or in connection with the use or performance of this software.
23 */
24
25 #include "NSFileURLHandle.h"
26 #include <Foundation/NSURL.h>
27 #include <Foundation/NSFileManager.h>
28 #include <Foundation/NSData.h>
29 #include <Foundation/NSException.h>
30
31 @implementation NSFileURLHandle
32
33 + (BOOL)canInitWithURL:(NSURL *)_url
34 {
35     return [_url isFileURL];
36 }
37
38 - (id)initWithURL:(NSURL *)_url cached:(BOOL)_flag
39 {
40     NSAssert([_url isFileURL],
41              @"file handle can only load 'file' URLs (%@)", _url);
42     self->path      = [[_url path] copy];
43     self->cacheData = _flag;
44     return self;
45 }
46
47 - (void)dealloc
48 {
49     RELEASE(self->data);
50     RELEASE(self->path);
51     RELEASE(self->props);
52     [super dealloc];
53 }
54
55 /* path */
56
57 - (NSString *)filePath
58 {
59     return self->path;
60 }
61 - (NSFileManager *)fileManager
62 {
63     return [NSFileManager defaultManager];
64 }
65
66 /* loading */
67
68 - (NSData *)loadInForeground
69 {
70     NSData *ldata;
71     
72     RELEASE(self->data); self->data = nil;
73     
74     ldata = [NSData dataWithContentsOfFile:[self filePath]];
75     
76     self->status = (ldata)
77         ? NSURLHandleLoadSucceeded
78         : NSURLHandleLoadFailed;
79
80     if (self->cacheData)
81         self->data = RETAIN(ldata);
82     
83     return ldata;
84 }
85 - (NSURLHandleStatus)status
86 {
87     return self->status;
88 }
89
90 /* reading data */
91
92 - (NSData *)resourceData
93 {
94     if (self->cacheData && (self->data != nil))
95         return self->data;
96
97     return [self loadInForeground];
98 }
99 - (void)flushCachedData
100 {
101     self->status = NSURLHandleNotLoaded;
102     RELEASE(self->props); self->props = nil;
103     RELEASE(self->data);  self->data  = nil;
104 }
105
106 /* writing data */
107
108 - (BOOL)writeData:(NSData *)_data
109 {
110     if (_data == nil)
111         return YES;
112
113     return [_data writeToFile:[self filePath] atomically:YES];
114 }
115
116 /* properties */
117
118 - (id)propertyForKey:(NSString *)_propertyKey
119 {
120     if (self->props == nil) {
121         self->props = [[[self fileManager]
122                               fileAttributesAtPath:[self filePath]
123                               traverseLink:YES]
124                               copy];
125     }
126     return [self->props objectForKey:_propertyKey];
127 }
128
129 - (id)propertyForKeyIfAvailable:(NSString *)_propertyKey
130 {
131     return [self->props objectForKey:_propertyKey];
132 }
133
134 - (BOOL)writeProperty:(id)_propValue forKey:(NSString *)_propertyKey
135 {
136     NSDictionary *attrs;
137     
138     if ((_propertyKey == nil) || (_propValue == nil))
139         return NO;
140
141     RELEASE(self->props); self->props = nil;
142
143     attrs = [NSDictionary dictionaryWithObject:_propValue forKey:_propertyKey];
144     
145     return [[self fileManager] changeFileAttributes:attrs
146                                atPath:[self filePath]];
147 }
148
149 @end /* NSFileURLHandle */
150
151 /*
152   Local Variables:
153   c-basic-offset: 4
154   tab-width: 8
155   End:
156 */