1999-09-16 07:21:34 +00:00
|
|
|
/* NSURLHandle.m - Class NSURLHandle
|
1999-02-13 00:50:41 +00:00
|
|
|
Copyright (C) 1999 Free Software Foundation, Inc.
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
Written by: Manuel Guesdon <mguesdon@sbuilders.com>
|
2000-09-21 12:31:29 +00:00
|
|
|
Date: Jan 1999
|
|
|
|
Update: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Date: Sep 2000
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
|
|
This file is part of the GNUstep Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
1999-09-09 02:56:20 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
1999-02-13 00:50:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <base/behavior.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
2000-09-21 12:31:29 +00:00
|
|
|
#include <Foundation/NSData.h>
|
|
|
|
#include <Foundation/NSEnumerator.h>
|
1999-02-13 00:50:41 +00:00
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
#include <Foundation/NSConcreteNumber.h>
|
2000-10-27 15:58:11 +00:00
|
|
|
#include <Foundation/NSLock.h>
|
1999-02-13 00:50:41 +00:00
|
|
|
#include <Foundation/NSURLHandle.h>
|
|
|
|
#include <Foundation/NSURL.h>
|
2000-10-27 21:17:09 +00:00
|
|
|
#include <Foundation/NSRunLoop.h>
|
2000-11-18 06:40:23 +00:00
|
|
|
#include <Foundation/NSFileManager.h>
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
@class GSFileURLHandle;
|
2000-11-17 16:19:56 +00:00
|
|
|
@class GSHTTPURLHandle;
|
2000-09-21 12:31:29 +00:00
|
|
|
|
1999-02-13 00:50:41 +00:00
|
|
|
@implementation NSURLHandle
|
|
|
|
|
2000-10-27 15:58:11 +00:00
|
|
|
static NSLock *registryLock = nil;
|
2000-09-19 08:31:40 +00:00
|
|
|
static NSMutableArray *registry = nil;
|
2000-10-27 15:58:11 +00:00
|
|
|
static Class NSURLHandleClass = 0;
|
2000-09-19 08:31:40 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)url
|
2000-09-19 08:31:40 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
/*
|
|
|
|
* Each subclass is supposed to do its own caching, so we must
|
|
|
|
* find the correct subclass and ask it for its cached handle.
|
|
|
|
*/
|
2000-10-27 15:58:11 +00:00
|
|
|
if (self == NSURLHandleClass)
|
2000-09-19 08:31:40 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
Class c = [self URLHandleClassForURL: url];
|
|
|
|
|
2000-10-27 15:58:11 +00:00
|
|
|
if (c == self || c == 0)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return [c cachedHandleForURL: url];
|
|
|
|
}
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return nil;
|
2000-09-19 08:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
+ (BOOL) canInitWithURL: (NSURL*)url
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
/*
|
|
|
|
* The semi-abstract base class can't handle ANY scheme
|
|
|
|
*/
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSURLHandle class])
|
2000-09-19 08:31:40 +00:00
|
|
|
{
|
2000-10-27 15:58:11 +00:00
|
|
|
NSURLHandleClass = self;
|
2000-09-21 12:31:29 +00:00
|
|
|
registry = [NSMutableArray new];
|
2000-10-27 15:58:11 +00:00
|
|
|
registryLock = [NSLock new];
|
2000-09-21 12:31:29 +00:00
|
|
|
[self registerURLHandleClass: [GSFileURLHandle class]];
|
2000-11-17 16:19:56 +00:00
|
|
|
[self registerURLHandleClass: [GSHTTPURLHandle class]];
|
2000-09-19 08:31:40 +00:00
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
+ (void) registerURLHandleClass: (Class)urlHandleSubclass
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Maintain a registry of classes that handle various schemes
|
|
|
|
* Re-adding a class moves it to the end of the registry - so it will
|
|
|
|
* be used in preference to any class added earlier.
|
|
|
|
*/
|
2000-10-27 15:58:11 +00:00
|
|
|
[registryLock lock];
|
2000-10-30 20:43:09 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[registry removeObjectIdenticalTo: urlHandleSubclass];
|
|
|
|
[registry addObject: urlHandleSubclass];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[registryLock unlock];
|
|
|
|
[localException raise];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
2000-10-27 15:58:11 +00:00
|
|
|
[registryLock unlock];
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (Class) URLHandleClassForURL: (NSURL*)url
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-10-27 15:58:11 +00:00
|
|
|
unsigned count;
|
2000-10-30 20:43:09 +00:00
|
|
|
Class c = 0;
|
2000-10-27 15:58:11 +00:00
|
|
|
|
|
|
|
[registryLock lock];
|
2000-10-30 20:43:09 +00:00
|
|
|
NS_DURING
|
2000-09-19 08:31:40 +00:00
|
|
|
{
|
2000-10-30 20:43:09 +00:00
|
|
|
count = [registry count];
|
2000-09-19 08:31:40 +00:00
|
|
|
|
2000-10-30 20:43:09 +00:00
|
|
|
/*
|
|
|
|
* Find a class to handle the URL, try most recently registered first.
|
|
|
|
*/
|
|
|
|
while (count-- > 0)
|
2000-09-19 08:31:40 +00:00
|
|
|
{
|
2000-10-30 20:43:09 +00:00
|
|
|
id found = [registry objectAtIndex: count];
|
|
|
|
|
|
|
|
if ([found canInitWithURL: url] == YES)
|
|
|
|
{
|
|
|
|
c = (Class)found;
|
|
|
|
}
|
2000-09-19 08:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
2000-10-30 20:43:09 +00:00
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[registryLock unlock];
|
|
|
|
[localException raise];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
2000-10-27 15:58:11 +00:00
|
|
|
[registryLock unlock];
|
2000-10-31 12:32:00 +00:00
|
|
|
return c;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-25 06:06:28 +00:00
|
|
|
/*
|
|
|
|
* Add a client object, making sure that it doesn't occur more than once.
|
|
|
|
*/
|
2000-09-21 12:31:29 +00:00
|
|
|
- (void) addClient: (id <NSURLHandleClient>)client
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-25 06:06:28 +00:00
|
|
|
RETAIN((id)client);
|
|
|
|
[_clients removeObjectIdenticalTo: client];
|
2000-09-21 12:31:29 +00:00
|
|
|
[_clients addObject: client];
|
2000-09-25 06:06:28 +00:00
|
|
|
RELEASE((id)client);
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
2000-09-19 08:31:40 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (NSData*) availableResourceData
|
|
|
|
{
|
2000-10-27 21:17:09 +00:00
|
|
|
if (_status == NSURLHandleLoadInProgress)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return _data;
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) backgroundLoadDidFailWithReason: (NSString*)reason
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator = [_clients objectEnumerator];
|
|
|
|
id <NSURLHandleClient> client;
|
|
|
|
|
|
|
|
_status = NSURLHandleLoadFailed;
|
2000-10-27 21:17:09 +00:00
|
|
|
DESTROY(_data);
|
2000-09-21 12:31:29 +00:00
|
|
|
ASSIGNCOPY(_failure, reason);
|
|
|
|
|
|
|
|
while ((client = [enumerator nextObject]) != nil)
|
2000-09-19 18:49:43 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[client URLHandle: self resourceDidFailLoadingWithReason: _failure];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) beginLoadInBackground
|
|
|
|
{
|
|
|
|
_status = NSURLHandleLoadInProgress;
|
2000-10-27 21:17:09 +00:00
|
|
|
DESTROY(_data);
|
|
|
|
_data = [NSMutableData new];
|
2000-09-21 12:31:29 +00:00
|
|
|
[_clients makeObjectsPerformSelector:
|
|
|
|
@selector(URLHandleResourceDidBeginLoading:)
|
|
|
|
withObject: self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) cancelLoadInBackground
|
|
|
|
{
|
|
|
|
[_clients makeObjectsPerformSelector:
|
|
|
|
@selector(URLHandleResourceDidCancelLoading:)
|
|
|
|
withObject: self];
|
|
|
|
[self endLoadInBackground];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_data);
|
|
|
|
RELEASE(_failure);
|
|
|
|
RELEASE(_clients);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mathod called by subclasses during process of loading a resource.
|
|
|
|
* The base class maintains a copy of the data being read in and
|
|
|
|
* accumulates separate parts of the data.
|
|
|
|
*/
|
|
|
|
- (void) didLoadBytes: (NSData*)newData
|
|
|
|
loadComplete: (BOOL)loadComplete
|
|
|
|
{
|
|
|
|
NSEnumerator *enumerator;
|
|
|
|
id <NSURLHandleClient> client;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Let clients know we are starting loading (unless this has already been
|
|
|
|
* done).
|
|
|
|
*/
|
|
|
|
if (_status != NSURLHandleLoadInProgress)
|
|
|
|
{
|
|
|
|
_status = NSURLHandleLoadInProgress;
|
2000-10-27 21:17:09 +00:00
|
|
|
DESTROY(_data);
|
|
|
|
_data = [NSMutableData new];
|
2000-09-21 12:31:29 +00:00
|
|
|
[_clients makeObjectsPerformSelector:
|
|
|
|
@selector(URLHandleResourceDidBeginLoading:)
|
|
|
|
withObject: self];
|
2000-09-19 18:49:43 +00:00
|
|
|
}
|
2000-09-21 12:31:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have been given nil data, there must have been a failure!
|
|
|
|
*/
|
|
|
|
if (newData == nil)
|
2000-09-19 08:31:40 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[self backgroundLoadDidFailWithReason: @"nil data"];
|
|
|
|
return;
|
2000-09-19 08:31:40 +00:00
|
|
|
}
|
2000-09-21 12:31:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Let clients know we have read some data.
|
|
|
|
*/
|
|
|
|
enumerator = [_clients objectEnumerator];
|
|
|
|
while ((client = [enumerator nextObject]) != nil)
|
2000-09-19 18:49:43 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[client URLHandle: self resourceDataDidBecomeAvailable: newData];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accumulate data in cache.
|
|
|
|
*/
|
|
|
|
[_data appendData: newData];
|
|
|
|
|
|
|
|
if (loadComplete == YES)
|
|
|
|
{
|
2000-12-05 18:34:06 +00:00
|
|
|
id tmp = _data;
|
|
|
|
|
|
|
|
_data = [tmp copy];
|
|
|
|
RELEASE(tmp);
|
2000-09-21 12:31:29 +00:00
|
|
|
/*
|
|
|
|
* Let clients know we have finished loading.
|
|
|
|
*/
|
|
|
|
_status = NSURLHandleLoadSucceeded;
|
|
|
|
[_clients makeObjectsPerformSelector:
|
|
|
|
@selector(URLHandleResourceDidFinishLoading:)
|
|
|
|
withObject: self];
|
2000-09-19 18:49:43 +00:00
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (void) endLoadInBackground
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
_status = NSURLHandleNotLoaded;
|
2000-12-05 18:34:06 +00:00
|
|
|
DESTROY(_data);
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
- (NSString*) failureReason
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
if (_status == NSURLHandleLoadFailed)
|
|
|
|
return _failure;
|
|
|
|
else
|
|
|
|
return nil;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (void) flushCachedData
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-10-27 21:17:09 +00:00
|
|
|
DESTROY(_data);
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (id) init
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-10-27 15:58:11 +00:00
|
|
|
return [self initWithURL: nil cached: NO];
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithURL: (NSURL*)url
|
|
|
|
cached: (BOOL)cached
|
|
|
|
{
|
2000-10-27 15:58:11 +00:00
|
|
|
_status = NSURLHandleNotLoaded;
|
|
|
|
_clients = [NSMutableArray new];
|
|
|
|
return self;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-10-27 21:17:09 +00:00
|
|
|
/*
|
|
|
|
* Do a background load by using loadInForeground -
|
|
|
|
* if this method is not overridden, loadInForeground MUST be.
|
|
|
|
*/
|
1999-06-24 19:30:29 +00:00
|
|
|
- (void) loadInBackground
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-10-27 21:17:09 +00:00
|
|
|
NSData *d;
|
|
|
|
|
|
|
|
[self beginLoadInBackground];
|
|
|
|
d = [self loadInForeground];
|
|
|
|
if (d == nil)
|
|
|
|
{
|
|
|
|
[self backgroundLoadDidFailWithReason: @"foreground load returned nil"];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[self didLoadBytes: d loadComplete: YES];
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-10-27 21:17:09 +00:00
|
|
|
/*
|
|
|
|
* Do a foreground load by using loadInBackground -
|
|
|
|
* if this method is not overridden, loadInBackground MUST be.
|
|
|
|
*/
|
2000-09-21 12:31:29 +00:00
|
|
|
- (NSData*) loadInForeground
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-10-27 21:17:09 +00:00
|
|
|
NSRunLoop *loop = [NSRunLoop currentRunLoop];
|
|
|
|
|
|
|
|
[self loadInBackground];
|
|
|
|
while ([self status] == NSURLHandleLoadInProgress)
|
|
|
|
{
|
|
|
|
NSDate *limit;
|
|
|
|
|
|
|
|
limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 1.0];
|
|
|
|
[loop runUntilDate: limit];
|
|
|
|
RELEASE(limit);
|
|
|
|
}
|
|
|
|
return _data;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (id) propertyForKey: (NSString*)propertyKey
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1999-06-24 19:30:29 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (id) propertyForKeyIfAvailable: (NSString*)propertyKey
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1999-06-24 19:30:29 +00:00
|
|
|
return nil;
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (void) removeClient: (id <NSURLHandleClient>)client
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[_clients removeObjectIdenticalTo: client];
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (NSData*) resourceData
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
if (_status == NSURLHandleLoadSucceeded)
|
|
|
|
{
|
|
|
|
return [self availableResourceData];
|
|
|
|
}
|
2000-12-05 18:34:06 +00:00
|
|
|
else if (_status == NSURLHandleLoadInProgress)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
2000-09-21 12:31:29 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
NSData *d = [self loadInForeground];
|
|
|
|
|
|
|
|
if (d != nil)
|
|
|
|
{
|
2000-10-27 21:17:09 +00:00
|
|
|
ASSIGNCOPY(_data, d);
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
2000-10-27 21:17:09 +00:00
|
|
|
return _data;
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (NSURLHandleStatus) status
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
return _status;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (BOOL) writeData: (NSData*)data
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
1999-06-24 19:30:29 +00:00
|
|
|
return NO;
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (BOOL) writeProperty: (id)propertyValue
|
|
|
|
forKey: (NSString*)propertyKey
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
return NO;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@interface GSFileURLHandle : NSURLHandle
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-11-18 06:40:23 +00:00
|
|
|
NSString *_path;
|
|
|
|
NSMutableDictionary *_attributes;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
2000-09-21 12:31:29 +00:00
|
|
|
@end
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
@implementation GSFileURLHandle
|
|
|
|
|
|
|
|
static NSMutableDictionary *fileCache = nil;
|
2000-10-27 21:17:09 +00:00
|
|
|
static NSLock *fileLock = nil;
|
2000-09-21 12:31:29 +00:00
|
|
|
|
|
|
|
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)url
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
NSURLHandle *obj = nil;
|
|
|
|
|
|
|
|
if ([url isFileURL] == YES)
|
|
|
|
{
|
|
|
|
NSString *path = [url path];
|
|
|
|
|
|
|
|
path = [path stringByStandardizingPath];
|
2000-10-27 21:17:09 +00:00
|
|
|
[fileLock lock];
|
2000-10-30 20:43:09 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
obj = [fileCache objectForKey: path];
|
|
|
|
AUTORELEASE(RETAIN(obj));
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[fileLock unlock];
|
|
|
|
[localException raise];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
2000-10-27 21:17:09 +00:00
|
|
|
[fileLock unlock];
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
return obj;
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
+ (BOOL) canInitWithURL: (NSURL*)url
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
if ([url isFileURL] == YES)
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
1999-06-24 19:30:29 +00:00
|
|
|
return NO;
|
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
+ (void) initialize
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
fileCache = [NSMutableDictionary new];
|
2000-10-27 21:17:09 +00:00
|
|
|
fileLock = [NSLock new];
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_path);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithURL: (NSURL*)url
|
|
|
|
cached: (BOOL)cached
|
|
|
|
{
|
|
|
|
NSString *path;
|
|
|
|
|
|
|
|
if ([url isFileURL] == NO)
|
|
|
|
{
|
|
|
|
NSLog(@"Attempt to init GSFileURLHandle with bad URL");
|
|
|
|
RELEASE(self);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
path = [url path];
|
|
|
|
path = [path stringByStandardizingPath];
|
|
|
|
|
|
|
|
if (cached == YES)
|
|
|
|
{
|
|
|
|
id obj;
|
|
|
|
|
2000-10-27 21:17:09 +00:00
|
|
|
[fileLock lock];
|
2000-10-30 20:43:09 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
obj = [fileCache objectForKey: path];
|
|
|
|
if (obj != nil)
|
|
|
|
{
|
|
|
|
DESTROY(self);
|
|
|
|
RETAIN(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
2000-09-21 12:31:29 +00:00
|
|
|
{
|
2001-03-03 14:49:11 +00:00
|
|
|
obj = nil;
|
2000-10-27 21:17:09 +00:00
|
|
|
[fileLock unlock];
|
2000-10-30 20:43:09 +00:00
|
|
|
[localException raise];
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
2000-10-30 20:43:09 +00:00
|
|
|
NS_ENDHANDLER
|
2000-10-27 21:17:09 +00:00
|
|
|
[fileLock unlock];
|
2000-10-30 20:43:09 +00:00
|
|
|
if (obj != nil)
|
|
|
|
{
|
|
|
|
return obj;
|
|
|
|
}
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
2000-10-27 15:58:11 +00:00
|
|
|
|
|
|
|
if ((self = [super initWithURL: url cached: cached]) != nil)
|
2000-09-21 12:31:29 +00:00
|
|
|
{
|
|
|
|
_path = [path copy];
|
|
|
|
if (cached == YES)
|
|
|
|
{
|
2000-10-27 21:17:09 +00:00
|
|
|
[fileLock lock];
|
2000-10-30 20:43:09 +00:00
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[fileCache setObject: self forKey: _path];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
[fileLock unlock];
|
|
|
|
[localException raise];
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
2000-10-27 21:17:09 +00:00
|
|
|
[fileLock unlock];
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
1999-06-24 19:30:29 +00:00
|
|
|
- (NSData*) loadInForeground
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-09-21 12:31:29 +00:00
|
|
|
NSData *d = [NSData dataWithContentsOfFile: _path];
|
|
|
|
|
|
|
|
[self didLoadBytes: d loadComplete: YES];
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) propertyForKey: (NSString*)propertyKey
|
|
|
|
{
|
2000-11-18 06:40:23 +00:00
|
|
|
NSDictionary *dict;
|
|
|
|
|
|
|
|
dict = [[NSFileManager defaultManager] fileAttributesAtPath: _path
|
|
|
|
traverseLink: YES];
|
|
|
|
RELEASE(_attributes);
|
|
|
|
_attributes = [dict mutableCopy];
|
|
|
|
return [_attributes objectForKey: propertyKey];
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (id) propertyForKeyIfAvailable: (NSString*)propertyKey
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-11-18 06:40:23 +00:00
|
|
|
return [_attributes objectForKey: propertyKey];
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
2000-09-21 12:31:29 +00:00
|
|
|
- (BOOL) writeData: (NSData*)data
|
1999-02-13 00:50:41 +00:00
|
|
|
{
|
2000-11-18 06:40:23 +00:00
|
|
|
if ([data writeToFile: _path atomically: YES] == YES)
|
|
|
|
{
|
|
|
|
ASSIGNCOPY(_data, data);
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
2000-09-21 12:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL) writeProperty: (id)propertyValue
|
|
|
|
forKey: (NSString*)propertyKey
|
|
|
|
{
|
2000-11-18 06:40:23 +00:00
|
|
|
if ([self propertyForKey: propertyKey] == nil)
|
|
|
|
{
|
|
|
|
return NO; /* Not a valid file property key. */
|
|
|
|
}
|
|
|
|
[_attributes setObject: propertyValue forKey: propertyKey];
|
|
|
|
return [[NSFileManager defaultManager] changeFileAttributes: _attributes
|
|
|
|
atPath: _path];
|
1999-06-24 19:30:29 +00:00
|
|
|
}
|
1999-02-13 00:50:41 +00:00
|
|
|
|
|
|
|
@end
|
2000-09-21 12:31:29 +00:00
|
|
|
|