// // TKAppSkin.m // ThemeKit // // Created by Colin Cornaby on 2/28/06. // Copyright 2006 __MyCompanyName__. All rights reserved. // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 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 // Lesser General Public License for more details. // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #import "TKAppSkin.h" #import "CSCheckSum.h" #import "TKVariation.h" #import "TKResourceFile.h" #import "TKPXMResource.h" #import "TKCLRResource.h" #import "TKPPATResource.h" #import "TKPICTResource.h" #import "TKLAYOResource.h" #import "TKTdatResource.h" #import "TKTheme.h" @implementation TKAppSkin -(void)setName:(NSString *)theName { [self setValue:theName forKey:@"skinName"]; } -(NSString *)name{ NSString *flavorName = [self valueForKey:@"skinName"]; if(flavorName==nil) return nil; return flavorName; } -(void)setIcon:(NSImage *)theIcon { [self willChangeValueForKey:@"icon"]; [self setPrimitiveValue:[theIcon TIFFRepresentation] forKey:@"icon"]; [self didChangeValueForKey:@"icon"]; } -(NSData *)icon{ [self willAccessValueForKey:@"icon"]; NSData *n = [self primitiveValueForKey:@"icon"]; if(n==nil) return nil; [self didAccessValueForKey:@"icon"]; return n; } -(NSData *)currentDRMKey { return nil; } -(void)setAppIdentifier:(NSString *)appIdentifier { [self willChangeValueForKey:@"appIdentifier"]; [self setPrimitiveValue:appIdentifier forKey:@"appIdentifier"]; [self didChangeValueForKey:@"appIdentifier"]; } -(NSString *)appIdentifier { [self willAccessValueForKey:@"appIdentifier"]; NSString *n = [self primitiveValueForKey:@"appIdentifier"]; [self didAccessValueForKey:@"appIdentifier"]; return n; } -(void)setDRMForUserName:(NSString *)username password:(NSString *)password { //NSString *userAndPass = [NSString stringWithFormat:@"%@%@", username, password]; //NSData *hash = [CSCheckSum hashForData:[userAndPass dataUsingEncoding:NSUnicodeStringEncoding]]; //[self setMetadataForKey:@"TKDRMKey" forValue:hash]; } -(void)removeAppSkinResource:(id)resource { [[self mutableSetValueForKey:@"resources"] removeObject:resource]; [[self managedObjectContext] deleteObject:resource]; } -(id)addAppSkinResource:(id)resource { id resourceToAdd = resource; if(!([resource managedObjectContext]==[self managedObjectContext])){ resourceToAdd = [NSEntityDescription insertNewObjectForEntityForName:[[resource entity] name] inManagedObjectContext:[self managedObjectContext]]; NSArray *attributeKeys = [[resourceToAdd entity] attributeKeys]; NSEnumerator *enumerator = [attributeKeys objectEnumerator]; NSString *attributeName = nil; while (nil != (attributeName = [enumerator nextObject])) { [resourceToAdd setValue: [resource valueForKey: attributeName] forKey: attributeName]; } [resourceToAdd setValue:[NSEntityDescription insertNewObjectForEntityForName:@"TKResourceMetadata" inManagedObjectContext:[self managedObjectContext]] forKey:@"metadata"]; [[resourceToAdd valueForKey:@"metadata"] setValue:[[resource valueForKey:@"metadata"] valueForKey:@"metadata"] forKey:@"metadata"]; } [[self mutableSetValueForKey:@"resources"] addObject:resourceToAdd]; return resourceToAdd; } -(NSArray *)getResourcesForType:(NSString *)resType withSortDescriptors:(NSArray *)descriptors { if([resType isEqualToString:@"TKResource"]){ NSArray *resources = [[self valueForKey:@"resources"] allObjects]; if(descriptors!=nil) resources = [resources sortedArrayUsingDescriptors:descriptors]; return resources; } NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; NSEntityDescription *entity = [NSEntityDescription entityForName:resType inManagedObjectContext:[self managedObjectContext]]; [request setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.owner = %@", self]; [request setPredicate:predicate]; if(descriptors!=nil) [request setSortDescriptors:descriptors]; NSError *error = nil; NSArray *array = [[self managedObjectContext] executeFetchRequest:request error:&error]; return array; } -(NSArray *)getResourcesForType:(NSString *)resType { return [self getResourcesForType:resType withSortDescriptors:nil]; } -(void)writeExtrasFileAtPath:(NSString *)path withEndianness:(TKEndianness)endianness writeComplete:(BOOL)writeCompleteFile { int b; TKResourceFile *myResFile; int i; OSErr theErr; [[NSFileManager defaultManager] removeFileAtPath:@"/tmp/Extras.rsrc" handler:nil]; TKEndianness trueEndianness = resolveEndianness(endianness); NSString *extrasFilePath = nil; if(writeCompleteFile==YES) { if(trueEndianness==kLittleEndian) extrasFilePath = @"/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Resources/Extras2.rsrc"; if(trueEndianness==kBigEndian) extrasFilePath = @"/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Resources/Extras.rsrc"; if(extrasFilePath==nil) [NSException raise:NSGenericException format:@"No output platform could be determined for resource export"]; [[NSFileManager defaultManager] copyPath:extrasFilePath toPath:@"/tmp/Extras.rsrc" handler:nil]; } myResFile = [[TKResourceFile alloc] initForWritingAtPath:@"/tmp/Extras.rsrc"]; [myResFile setEndiannessOfResourceFile:endianness]; ResType resType = 'pxm#'; NSArray *resourcesToImport = [self getResourcesForType:@"TKPXMResource"]; b = [resourcesToImport count]; for(i=0;i