2001-10-19 19:41:13 +00:00
# import "Resource.h"
2002-04-29 00:05:34 +00:00
# import "ResourceDocument.h"
# import "ResourceDataSource.h"
2001-10-19 19:41:13 +00:00
2008-07-31 20:27:55 +00:00
NSString * RKResourcePboardType = @ "RKResourcePboardType" ;
2002-11-13 03:35:54 +00:00
2002-03-31 12:00:02 +00:00
@ implementation Resource
2001-10-19 19:41:13 +00:00
- ( id ) init
{
self = [ super init ] ;
2002-02-14 23:24:53 +00:00
[ self initWithType : @ "NULL" andID : [ NSNumber numberWithShort : 128 ] ] ;
2001-10-19 19:41:13 +00:00
return self ;
}
2002-02-23 03:40:24 +00:00
- ( id ) initWithType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue
{
[ self initWithType : typeValue andID : resIDValue withName : @ "" andAttributes : [ NSNumber numberWithUnsignedShort : 0 ] ] ;
return self ;
}
- ( id ) initWithType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue withName : ( NSString * ) nameValue andAttributes : ( NSNumber * ) attributesValue
{
[ self initWithType : typeValue andID : resIDValue withName : nameValue andAttributes : attributesValue data : [ NSData data ] ] ;
return self ;
}
- ( id ) initWithType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue withName : ( NSString * ) nameValue andAttributes : ( NSNumber * ) attributesValue data : ( NSData * ) dataValue
{
// sets values directly for speed reasons ( less messaging overhead )
self = [ super init ] ;
dirty = NO ;
2003-04-03 15:38:42 +00:00
representedFork = nil ;
2002-02-23 03:40:24 +00:00
name = [ nameValue copy ] ;
type = [ typeValue copy ] ;
resID = [ resIDValue copy ] ;
attributes = [ attributesValue copy ] ;
data = [ dataValue retain ] ;
return self ;
}
2003-08-01 22:23:50 +00:00
2002-02-23 03:40:24 +00:00
+ ( id ) resourceOfType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue
{
Resource * resource = [ [ Resource allocWithZone : [ self zone ] ] initWithType : typeValue andID : resIDValue ] ;
return [ resource autorelease ] ;
}
+ ( id ) resourceOfType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue withName : ( NSString * ) nameValue andAttributes : ( NSNumber * ) attributesValue
{
Resource * resource = [ [ Resource allocWithZone : [ self zone ] ] initWithType : typeValue andID : resIDValue withName : nameValue andAttributes : attributesValue ] ;
return [ resource autorelease ] ;
}
+ ( id ) resourceOfType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue withName : ( NSString * ) nameValue andAttributes : ( NSNumber * ) attributesValue data : ( NSData * ) dataValue
{
Resource * resource = [ [ Resource allocWithZone : [ self zone ] ] initWithType : typeValue andID : resIDValue withName : nameValue andAttributes : attributesValue data : dataValue ] ;
return [ resource autorelease ] ;
}
2008-07-31 20:27:55 +00:00
+ ( Resource * ) getResourceOfType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue inDocument : ( NSDocument * ) searchDoc
2002-05-31 17:31:41 +00:00
{
NSDocument * doc ;
NSEnumerator * enumerator = [ [ [ NSDocumentController sharedDocumentController ] documents ] objectEnumerator ] ;
2008-07-31 20:27:55 +00:00
while ( doc = [ enumerator nextObject ] )
2002-05-31 17:31:41 +00:00
{
2008-07-31 20:27:55 +00:00
if ( searchDoc = = nil || searchDoc = = doc )
2002-05-31 17:31:41 +00:00
{
// parse document for correct resource
Resource * resource = [ [ ( ResourceDocument * ) doc dataSource ] resourceOfType : typeValue andID : resIDValue ] ;
2008-07-31 20:27:55 +00:00
if ( resource ) return resource ;
2002-05-31 17:31:41 +00:00
}
}
return nil ;
}
/ * ResKnifeResourceProtocol implementation * /
2008-07-31 20:27:55 +00:00
+ ( NSArray * ) allResourcesOfType : ( NSString * ) typeValue inDocument : ( NSDocument * ) searchDoc
2002-04-29 00:05:34 +00:00
{
NSMutableArray * array = [ NSMutableArray array ] ;
NSDocument * doc ;
NSEnumerator * enumerator = [ [ [ NSDocumentController sharedDocumentController ] documents ] objectEnumerator ] ;
2008-07-31 20:27:55 +00:00
while ( doc = [ enumerator nextObject ] )
2002-04-29 00:05:34 +00:00
{
// parse document for resources
2008-07-31 20:27:55 +00:00
if ( searchDoc = = nil || searchDoc = = doc )
2002-04-29 00:05:34 +00:00
[ array addObjectsFromArray : [ [ ( ResourceDocument * ) doc dataSource ] allResourcesOfType : typeValue ] ] ;
}
return [ NSArray arrayWithArray : array ] ;
}
2010-08-31 10:01:42 +00:00
+ ( Resource * ) resourceOfType : ( NSString * ) typeValue withName : ( NSString * ) nameValue inDocument : ( NSDocument * ) searchDoc
2002-04-29 00:05:34 +00:00
{
NSDocument * doc ;
NSEnumerator * enumerator = [ [ [ NSDocumentController sharedDocumentController ] documents ] objectEnumerator ] ;
2008-07-31 20:27:55 +00:00
while ( doc = [ enumerator nextObject ] )
2002-04-29 00:05:34 +00:00
{
2008-07-31 20:27:55 +00:00
if ( searchDoc = = nil || searchDoc = = doc )
2002-04-29 00:05:34 +00:00
{
// parse document for correct resource
2002-05-31 17:31:41 +00:00
Resource * resource = [ [ ( ResourceDocument * ) doc dataSource ] resourceOfType : typeValue withName : nameValue ] ;
2008-07-31 20:27:55 +00:00
if ( resource ) return resource ;
2002-04-29 00:05:34 +00:00
}
}
return nil ;
}
2010-08-31 10:01:42 +00:00
+ ( Resource * ) resourceOfType : ( NSString * ) typeValue andID : ( NSNumber * ) resIDValue inDocument : ( NSDocument * ) searchDoc
2002-04-29 00:05:34 +00:00
{
NSDocument * doc ;
NSEnumerator * enumerator = [ [ [ NSDocumentController sharedDocumentController ] documents ] objectEnumerator ] ;
2008-07-31 20:27:55 +00:00
while ( doc = [ enumerator nextObject ] )
2002-04-29 00:05:34 +00:00
{
2008-07-31 20:27:55 +00:00
if ( searchDoc = = nil || searchDoc = = doc )
2002-04-29 00:05:34 +00:00
{
// parse document for correct resource
2002-05-31 17:31:41 +00:00
Resource * resource = [ [ ( ResourceDocument * ) doc dataSource ] resourceOfType : typeValue andID : resIDValue ] ;
2008-07-31 20:27:55 +00:00
if ( resource ) return resource ;
}
}
return nil ;
}
// should probably be in resource document , not resource , but it fits in with the above methods quite well
+ ( NSDocument * ) documentForResource : ( Resource * ) resource
{
NSDocument * doc ;
NSEnumerator * enumerator = [ [ [ NSDocumentController sharedDocumentController ] documents ] objectEnumerator ] ;
while ( doc = [ enumerator nextObject ] )
{
Resource * res ;
NSEnumerator * enumerator2 = [ [ ( ResourceDocument * ) doc resources ] objectEnumerator ] ;
while ( res = [ enumerator2 nextObject ] )
{
if ( [ res isEqual : resource ] )
return doc ;
2002-04-29 00:05:34 +00:00
}
}
return nil ;
}
2001-10-19 19:41:13 +00:00
- ( void ) dealloc
{
2003-04-03 15:38:42 +00:00
[ representedFork release ] ;
2001-10-19 19:41:13 +00:00
[ name release ] ;
[ type release ] ;
[ resID release ] ;
[ attributes release ] ;
[ data release ] ;
2008-07-31 20:27:55 +00:00
[ _docName release ] ;
2001-10-19 19:41:13 +00:00
[ super dealloc ] ;
}
2002-12-31 19:06:40 +00:00
- ( id ) copyWithZone : ( NSZone * ) zone
{
Resource * copy = [ [ Resource alloc ] initWithType : type andID : resID withName : name andAttributes : attributes data : [ data copy ] ] ;
2008-07-31 20:27:55 +00:00
[ copy setDocumentName : _docName ] ;
2002-12-31 19:06:40 +00:00
return copy ;
}
2008-07-31 20:27:55 +00:00
/ * accessors * /
2002-02-14 23:24:53 +00:00
2002-10-04 19:54:05 +00:00
- ( void ) touch
{
[ self setDirty : YES ] ;
}
2002-02-14 23:24:53 +00:00
- ( BOOL ) isDirty
{
return dirty ;
}
- ( void ) setDirty : ( BOOL ) newValue
{
dirty = newValue ;
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceDidChangeNotification object : self ] ;
}
2009-11-09 10:17:40 +00:00
- ( NSDocument * ) document
2003-08-01 22:23:50 +00:00
{
2008-07-31 20:27:55 +00:00
return [ Resource documentForResource : self ] ;
2003-08-01 22:23:50 +00:00
}
2008-07-31 20:27:55 +00:00
- ( void ) setDocumentName : ( NSString * ) docName
2003-08-01 22:23:50 +00:00
{
2008-07-31 20:27:55 +00:00
_docName = [ docName copy ] ;
2003-08-01 22:23:50 +00:00
}
2003-04-03 15:38:42 +00:00
- ( NSString * ) representedFork
{
return representedFork ;
}
- ( void ) setRepresentedFork : ( NSString * ) forkName
{
representedFork = [ forkName copy ] ;
}
2001-10-19 19:41:13 +00:00
- ( NSString * ) name
{
return name ;
}
2008-07-31 20:27:55 +00:00
// shouldn ' t need this - it ' s used by forks to give them alternate names - should use name formatter replacement instead
- ( void ) _setName : ( NSString * ) newName
{
id old = name ;
name = [ newName copy ] ;
[ old release ] ;
}
2001-10-19 19:41:13 +00:00
- ( void ) setName : ( NSString * ) newName
{
2008-07-31 20:27:55 +00:00
if ( ! [ name isEqualToString : newName ] )
2002-03-31 12:00:02 +00:00
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceWillChangeNotification object : self ] ;
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceNameWillChangeNotification object : self ] ;
2008-07-31 20:27:55 +00:00
id old = name ;
2002-03-31 12:00:02 +00:00
name = [ newName copy ] ;
2008-07-31 20:27:55 +00:00
[ old release ] ;
2002-03-31 12:00:02 +00:00
2008-07-31 20:27:55 +00:00
// bug : this line is causing crashes !
// [ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceNameDidChangeNotification object : self ] ;
2002-03-31 12:00:02 +00:00
[ self setDirty : YES ] ;
}
2001-10-19 19:41:13 +00:00
}
2008-07-31 20:27:55 +00:00
- ( NSString * ) defaultWindowTitle
2003-08-06 17:40:46 +00:00
{
2008-07-31 20:27:55 +00:00
if ( [ name length ] > 0 ) return [ NSString stringWithFormat : NSLocalizedString ( @ "%@: %@ %@ '%@'" , @ "default window title format with resource name" ) , _docName , type , resID , name ] ;
else return [ NSString stringWithFormat : NSLocalizedString ( @ "%@: %@ %@" , @ "default window title format without resource name" ) , _docName , type , resID ] ;
2003-08-06 17:40:46 +00:00
}
2001-10-19 19:41:13 +00:00
- ( NSString * ) type
{
return type ;
}
- ( void ) setType : ( NSString * ) newType
{
2008-07-31 20:27:55 +00:00
if ( ! [ type isEqualToString : newType ] )
2002-03-31 12:00:02 +00:00
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceWillChangeNotification object : self ] ;
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceTypeWillChangeNotification object : self ] ;
2008-07-31 20:27:55 +00:00
id old = type ;
2002-03-31 12:00:02 +00:00
type = [ newType copy ] ;
2008-07-31 20:27:55 +00:00
[ old release ] ;
2002-03-31 12:00:02 +00:00
2008-07-31 20:27:55 +00:00
// bug : this line is causing crashes !
// [ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceTypeDidChangeNotification object : self ] ;
2002-03-31 12:00:02 +00:00
[ self setDirty : YES ] ;
}
2001-10-19 19:41:13 +00:00
}
- ( NSNumber * ) resID
{
return resID ;
}
- ( void ) setResID : ( NSNumber * ) newResID
{
2008-07-31 20:27:55 +00:00
if ( ! [ resID isEqualToNumber : newResID ] )
2002-03-31 12:00:02 +00:00
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceWillChangeNotification object : self ] ;
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceIDWillChangeNotification object : self ] ;
2008-07-31 20:27:55 +00:00
id old = resID ;
2002-03-31 12:00:02 +00:00
resID = [ newResID copy ] ;
2008-07-31 20:27:55 +00:00
[ old release ] ;
2002-03-31 12:00:02 +00:00
2008-07-31 20:27:55 +00:00
// bug : this line is causing crashes !
// [ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceIDDidChangeNotification object : self ] ;
2002-03-31 12:00:02 +00:00
[ self setDirty : YES ] ;
}
2001-10-19 19:41:13 +00:00
}
- ( NSNumber * ) attributes
{
return attributes ;
}
- ( void ) setAttributes : ( NSNumber * ) newAttributes
{
2008-07-31 20:27:55 +00:00
if ( ! [ attributes isEqualToNumber : newAttributes ] )
2002-03-31 12:00:02 +00:00
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceWillChangeNotification object : self ] ;
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceAttributesWillChangeNotification object : self ] ;
2008-07-31 20:27:55 +00:00
id old = attributes ;
2002-03-31 12:00:02 +00:00
attributes = [ newAttributes copy ] ;
2008-07-31 20:27:55 +00:00
[ old release ] ;
2002-03-31 12:00:02 +00:00
2008-07-31 20:27:55 +00:00
// bug : this line is causing crashes !
// [ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceAttributesDidChangeNotification object : self ] ;
2002-03-31 12:00:02 +00:00
[ self setDirty : YES ] ;
}
2001-10-19 19:41:13 +00:00
}
2002-02-14 23:24:53 +00:00
- ( NSNumber * ) size
2001-10-19 19:41:13 +00:00
{
2002-02-14 23:24:53 +00:00
return [ NSNumber numberWithUnsignedLong : [ data length ] ] ;
2001-10-19 19:41:13 +00:00
}
- ( NSData * ) data
{
return data ;
}
- ( void ) setData : ( NSData * ) newData
{
2008-07-31 20:27:55 +00:00
if ( ! [ data isEqualToData : newData ] )
2002-03-31 12:00:02 +00:00
{
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceWillChangeNotification object : self ] ;
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceDataWillChangeNotification object : self ] ;
// note : this function retains , rather than copies , the supplied data
2008-07-31 20:27:55 +00:00
id old = data ;
2002-03-31 12:00:02 +00:00
data = [ newData retain ] ;
2008-07-31 20:27:55 +00:00
[ old release ] ;
2002-03-31 12:00:02 +00:00
[ [ NSNotificationCenter defaultCenter ] postNotificationName : ResourceDataDidChangeNotification object : self ] ;
[ self setDirty : YES ] ;
}
2001-10-19 19:41:13 +00:00
}
2002-11-13 03:35:54 +00:00
/ * encoding * /
- ( id ) initWithCoder : ( NSCoder * ) decoder
{
self = [ super init ] ;
2008-07-31 20:27:55 +00:00
if ( self )
2002-11-13 03:35:54 +00:00
{
dirty = YES ;
name = [ [ decoder decodeObject ] retain ] ;
type = [ [ decoder decodeObject ] retain ] ;
resID = [ [ decoder decodeObject ] retain ] ;
attributes = [ [ decoder decodeObject ] retain ] ;
data = [ [ decoder decodeDataObject ] retain ] ;
}
return self ;
}
- ( void ) encodeWithCoder : ( NSCoder * ) encoder
{
[ encoder encodeObject : name ] ;
[ encoder encodeObject : type ] ;
[ encoder encodeObject : resID ] ;
[ encoder encodeObject : attributes ] ;
[ encoder encodeDataObject : data ] ;
}
2002-10-04 19:54:05 +00:00
/ * description * /
- ( NSString * ) description
{
2002-12-31 19:06:40 +00:00
return [ NSString stringWithFormat : @ "\n%@\nName: %@\nType: %@ ID: %@\nSize: %d Modified: %@" , [ super description ] , name , type , resID , [ data length ] , dirty ? @ "YES" : @ "NO" ] ;
2002-10-04 19:54:05 +00:00
}
2001-10-19 19:41:13 +00:00
@ end