Serializes favorites list to NSUserDefaults (~/Library/Preferences/Celestia.plist)

Sloppy stuff, needs to be cleaned up.. but it works.
pull/3/head
Bob Ippolito 2002-06-25 02:18:50 +00:00
parent 7e4a7e52a7
commit 112ec4e019
15 changed files with 238 additions and 20 deletions

View File

@ -17,6 +17,7 @@
@interface CelestiaAppCore : NSObject {
CelestiaDestinations* _destinations;
}
-(void)archive;
+(CelestiaAppCore *)sharedAppCore;
-(BOOL)initSimulation;
-(BOOL)initRenderer;

View File

@ -76,9 +76,15 @@ void ContextMenuCallback(float x,float y, Selection selection) {
_destinations = nil;
return self;
}
-(void)archive
{
NSLog(@"[CelestiaAppCore archive]");
[[CelestiaFavorites sharedFavorites] archive];
}
- (void)dealloc
{
NSLog(@"[CelestiaAppCore dealloc]");
if (_destinations != nil) {
[_destinations release];
_destinations = nil;

View File

@ -21,6 +21,7 @@
IBOutlet FavoritesDrawerController *favoritesDrawerController;
NSTimer* timer;
}
-(BOOL)applicationShouldTerminate:(id)sender;
- (BOOL)windowShouldClose:(id)sender;
- (IBAction)showGotoObject:(id)sender;
- (IBAction)gotoObject:(id)sender;

View File

@ -13,6 +13,17 @@
@implementation CelestiaController
-(BOOL)applicationShouldTerminate:(id)sender
{
if (timer != nil) {
[timer invalidate];
[timer release];
}
timer = nil;
[[CelestiaAppCore sharedAppCore] archive];
return YES;
}
-(BOOL)windowShouldClose:(id)sender
{
[NSApp terminate:nil];

View File

@ -11,10 +11,14 @@
// parentFolder is totally vestigal crap
@interface CelestiaFavorite : NSObject {
@interface CelestiaFavorite : NSObject <NSCoding> {
NSValue* _data;
BOOL _freeWhenDone;
}
-(void)encodeWithCoder:(NSCoder*)coder;
-(id)initWithCoder:(NSCoder*)coder;
-(id)initWithDictionary:(NSDictionary*)dictionary;
-(NSDictionary*)dictionary;
-(void)activate;
-(void)setName:(NSString*)name;
-(id)initWithName:(NSString*)name;
@ -32,7 +36,7 @@
-(BOOL)isEqualToFavorite:(CelestiaFavorite*)fav;
-(BOOL)isEqualToString:(NSString*)str;
-(BOOL)isEqual:(id)obj;
-(NSDictionary*)dictionaryRepresentation;
//-(NSDictionary*)dictionaryRepresentation;
-(NSString*)description;
-(void)setName:(NSString*)name;
-(NSString*)selectionName;

View File

@ -28,9 +28,64 @@
{
return reinterpret_cast<FavoritesEntry*>([_data pointerValue]);
}
-(void)setSelectionName:(NSString*)name
{
if (name == nil)
name = @"";
[self favorite]->selectionName = [name stdString];
}
-(void)setPosition:(CelestiaUniversalCoord*)position
{
[self favorite]->position = [position universalCoord];
}
-(void)setOrientation:(CelestiaVector*)orientation
{
[self favorite]->orientation = [orientation quatf];
}
-(void)setJd:(NSNumber*)jd
{
[self favorite]->jd = [jd doubleValue];
}
-(void)setCoordinateSystem:(NSString*)coordSys
{
[self favorite]->coordSys = (astro::CoordinateSystem)[[Astro coordinateSystem:coordSys] intValue];
}
@end
@implementation CelestiaFavorite
-(void)encodeWithCoder:(NSCoder*)coder
{
//[super encodeWithCoder:coder];
[coder encodeObject:[NSNumber numberWithBool:[self isFolder]]];
[coder encodeObject:[self name]];
[coder encodeObject:[self parentFolder]];
if ([self isFolder])
return;
[coder encodeObject:[self selectionName]];
[coder encodeObject:[self position]];
[coder encodeObject:[self orientation]];
[coder encodeObject:[self jd]];
[coder encodeObject:[self coordinateSystem]];
}
-(id)initWithCoder:(NSCoder*)coder
{
FavoritesEntry* fav = new FavoritesEntry();
//self = [super initWithCoder:coder];
self = [self init];
_data = [[NSValue alloc] initWithBytes:reinterpret_cast<void*>(&fav) objCType:@encode(FavoritesEntry*)];
_freeWhenDone = YES;
fav->isFolder = (bool)[[coder decodeObject] boolValue];
[self setName:[coder decodeObject]];
[self setParentFolder:[coder decodeObject]];
if ([self isFolder])
return self;
[self setSelectionName:[coder decodeObject]];
[self setPosition:[coder decodeObject]];
[self setOrientation:[coder decodeObject]];
[self setJd:[coder decodeObject]];
[self setCoordinateSystem:[coder decodeObject]];
return self;
}
-(void)activate
{
[[CelestiaAppCore sharedAppCore] activateFavorite:self];
@ -111,22 +166,48 @@
}
return NO;
}
-(NSDictionary*)dictionaryRepresentation
-(NSDictionary*)dictionary
{
if ([self isFolder])
return [NSDictionary dictionaryWithObjectsAndKeys:[self name],@"name",@"YES",@"isFolder",nil,nil];
return [NSDictionary dictionaryWithObjectsAndKeys:[self name],@"name",[self parentFolder],@"parentFolder",[self selectionName],@"selectionName",[self position],@"position",[self orientation],@"orientation",[self jd],@"jd",@"NO",@"isFolder",[self coordinateSystem],@"coordinateSystem",nil,nil];
return [NSDictionary dictionaryWithObjectsAndKeys:[self name],@"name",[NSNumber numberWithBool:YES],@"isFolder",nil,nil];
return [NSDictionary dictionaryWithObjectsAndKeys:[self name],@"name",[self parentFolder],@"parentFolder",[self selectionName],@"selectionName",[NSArray arrayWithArray:[self orientation]],@"orientation",[[self position] data],@"position",[self jd],@"jd",[NSNumber numberWithBool:NO],@"isFolder",[self coordinateSystem],@"coordinateSystem",nil,nil];
}
-(id)initWithDictionary:(NSDictionary*)dict
{
FavoritesEntry* fav;
if ([[dict objectForKey:@"isFolder"] boolValue]) {
self = [self initWithFolderName:[dict objectForKey:@"name"]];
[self setParentFolder:[dict objectForKey:@"parentFolder"]];
return self;
}
fav = new FavoritesEntry();
self = [self init];
_data = [[NSValue alloc] initWithBytes:reinterpret_cast<void*>(&fav) objCType:@encode(FavoritesEntry*)];
_freeWhenDone = YES;
fav->isFolder = false;
[self setName:[dict objectForKey:@"name"]];
[self setParentFolder:[dict objectForKey:@"parentFolder"]];
[self setSelectionName:[dict objectForKey:@"selectionName"]];
[self setOrientation:[CelestiaVector vectorWithArray:[dict objectForKey:@"orientation"]]];
[self setPosition:[[[CelestiaUniversalCoord alloc] initWithData:[dict objectForKey:@"position"]] autorelease]];
[self setJd:[dict objectForKey:@"jd"]];
[self setCoordinateSystem:[dict objectForKey:@"coordinateSystem"]];
return self;
}
-(NSString*)description
{
return [[self dictionaryRepresentation] description];
return [[self dictionary] description];
}
-(void)setName:(NSString*)name
{
if (name == nil)
name = @"";
[self favorite]->name = [name stdString];
}
-(void)setParentFolder:(NSString*)parentFolder
{
if (parentFolder == nil)
parentFolder = @"";
[self favorite]->parentFolder = [parentFolder stdString];
}
-(NSString*)name

View File

@ -12,4 +12,9 @@
@interface CelestiaFavorite(PrivateAPI)
-(CelestiaFavorite*)initWithFavorite:(FavoritesEntry*)fav;
-(FavoritesEntry*)favorite;
-(void)setSelectionName:(NSString*)name;
-(void)setPosition:(CelestiaUniversalCoord*)position;
-(void)setOrientation:(CelestiaVector*)orientation;
-(void)setJd:(NSNumber*)jd;
-(void)setCoordinateSystem:(NSString*)coordSys;
@end

View File

@ -12,6 +12,7 @@
@interface CelestiaFavorites : MyTree
-(void)archive;
-(void)setSynchronize:(NSInvocation*)synchronize;
-(void)synchronize;
+(CelestiaFavorites*)sharedFavorites;

View File

@ -4,13 +4,24 @@
//
// Created by Bob Ippolito on Thu Jun 20 2002.
// Copyright (c) 2002 Chris Laurel. All rights reserved.
//
//
#import "CelestiaFavorites.h"
#import "CelestiaFavorite.h"
@implementation CelestiaFavorites
static NSInvocation* _synchronize;
static CelestiaFavorites* _celestiaFavorites;
-(void)archive
{
NSMutableArray* children = [NSMutableArray arrayWithCapacity:[self numberOfChildren]];
NSEnumerator* enumerator = [[self children] objectEnumerator];
id obj = nil;
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
while ((obj = [enumerator nextObject]) != nil)
[children addObject:[(MyTree*)obj recursiveDictionary]];
[defaults setObject:children forKey:@"favorites"];
NSLog(@"\"favorites\" = %@",[defaults objectForKey:@"favorites"]);
}
-(void)setSynchronize:(NSInvocation*)synchronize
{
if (_synchronize != nil)
@ -45,9 +56,19 @@ static CelestiaFavorites* _celestiaFavorites;
}
+(CelestiaFavorites*)sharedFavorites
{
if (_celestiaFavorites != nil)
if (_celestiaFavorites != nil) {
return _celestiaFavorites;
_celestiaFavorites = [[CelestiaFavorites alloc] initWithNode:nil parent:nil children:[NSArray array]];
return _celestiaFavorites;
} else {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
_celestiaFavorites = [[CelestiaFavorites alloc] initWithNode:nil parent:nil children:[NSArray array]];
if ([defaults objectForKey:@"favorites"] != nil) {
NSEnumerator *enumerator = [[defaults objectForKey:@"favorites"] objectEnumerator];
MyVector* children = [_celestiaFavorites children];
id obj = nil;
while ((obj = [enumerator nextObject]) != nil)
[children addObject:[[[MyTree alloc] initWithDictionary:obj parent:_celestiaFavorites] autorelease]];
}
return _celestiaFavorites;
}
}
@end

View File

@ -10,9 +10,13 @@
#import "CelestiaVector.h"
#import "CelestiaUniversalCoord.h"
@interface CelestiaUniversalCoord : NSObject {
@interface CelestiaUniversalCoord : NSObject <NSCoding> {
NSData* _data;
}
-(void)encodeWithCoder:(NSCoder*)coder;
-(id)initWithCoder:(NSCoder*)coder;
-(NSData*)data;
-(id)initWithData:(NSData*)data;
-(CelestiaVector*)celestiaVector;
-(NSNumber*)distanceTo:(CelestiaUniversalCoord*)t;
-(CelestiaUniversalCoord*)difference:(CelestiaUniversalCoord*)t;

View File

@ -24,6 +24,30 @@
@end
@implementation CelestiaUniversalCoord
-(NSData*)data
{
return _data;
}
-(id)initWithData:(NSData*)data
{
self = [super init];
_data = [data retain];
return self;
}
-(void)encodeWithCoder:(NSCoder*)coder
{
NSLog(@"[CelestiaUniversalCoord encodeWithCoder:%@]",coder);
//[super encodeWithCoder:coder];
[coder encodeObject:_data];
}
-(id)initWithCoder:(NSCoder*)coder
{
NSLog(@"[CelestiaUniversalCoord initWithCoder:%@]",coder);
//self = [super initWithCoder:coder];
self = [super init];
_data = [[coder decodeObject] retain];
return self;
}
-(void)dealloc
{
if (_data != nil) {

View File

@ -8,10 +8,12 @@
#import <Foundation/Foundation.h>
@interface CelestiaVector : NSArray
@interface CelestiaVector : NSArray <NSCoding>
{
NSArray* _array;
}
-(void)encodeWithCoder:(NSCoder*)coder;
-(id)initWithCoder:(NSCoder*)coder;
+(CelestiaVector*)vectorWithArray:(NSArray*)v;
+(CelestiaVector*)vectorWithx:(NSNumber*)v y:(NSNumber*)y;
+(CelestiaVector*)vectorWithx:(NSNumber*)v y:(NSNumber*)y z:(NSNumber*)z;

View File

@ -135,6 +135,18 @@
@end
@implementation CelestiaVector
-(void)encodeWithCoder:(NSCoder*)coder
{
//[super encodeWithCoder:coder];
[coder encodeObject:_array];
}
-(id)initWithCoder:(NSCoder*)coder
{
//self = [super initWithCoder:coder];
self = [self init];
_array = [[coder decodeObject] retain];
return self;
}
-(void)dealloc
{
if (_array != nil) {

View File

@ -9,10 +9,12 @@
#import <Foundation/Foundation.h>
#import "NSArray_Extensions.h"
@interface MyVector : NSMutableArray {
@interface MyVector : NSMutableArray <NSCoding> {
NSMutableArray *_array;
Class _myClass;
}
-(void)encodeWithCoder:(NSCoder*)coder;
-(id)initWithCoder:(NSCoder*)coder;
-(id)initWithClass:(Class)myClass;
-(void)addObject:(id)obj;
-(void)insertObject:(id)obj atIndex:(unsigned)idx;
@ -23,11 +25,13 @@
-(id)objectAtIndex:(unsigned)idx;
@end
@interface MyTree : NSObject {
id _nodeValue;
@interface MyTree : NSObject <NSCoding> {
id <NSCoding> _nodeValue;
MyVector* _children;
MyTree* _parent;
}
-(void)encodeWithCoder:(NSCoder*)coder;
-(id)initWithCoder:(NSCoder*)coder;
// for initializing a tree root node
-(id)init;
// initializing a leaf node
@ -45,6 +49,10 @@
-(BOOL)isDescendantOfNodeInArray:(NSArray*)array;
+(NSArray*)minimumNodeCoverFromNodesInArray:(NSArray*)allNodes;
-(id)initWithDictionary:(NSDictionary*)dict parent:(MyTree*)parent;
-(NSDictionary*)dictionary;
-(NSDictionary*)recursiveDictionary;
- (void)insertChild:(MyTree*)child atIndex:(int)index;
- (void)insertChildren:(NSArray*)children atIndex:(int)index;
- (void)removeChild:(MyTree*)child;
@ -54,7 +62,6 @@
- (int)indexOfChildIdenticalTo:(MyTree*)child;
- (int)numberOfChildren;
- (NSArray*)children;
- (MyTree*)firstChild;
- (MyTree*)lastChild;
- (MyTree*)childAtIndex:(int)index;

View File

@ -7,8 +7,26 @@
//
#import "MyTree.h"
// HACK
#import "CelestiaFavorite.h"
#import <objc/objc-class.h>
@implementation MyVector
-(void)encodeWithCoder:(NSCoder*)coder
{
NSLog(@"[MyVector encodeWithCoder:%@]",coder);
//[super encodeWithCoder:coder];
[coder encodeObject:_array];
//[coder encodeValueOfObjCType:@encode(Class) at:&_myClass];
}
-(id)initWithCoder:(NSCoder*)coder
{
NSLog(@"[MyVector initWithCoder:%@]",coder);
//self = [super initWithCoder:coder];
self = [self init];
_array = [[coder decodeObject] retain];
//[coder decodeValueOfObjCType:@encode(Class) at:&_myClass];
return self;
}
-(id)init
{
self = [super init];
@ -59,6 +77,24 @@
@end
@implementation MyTree
-(void)encodeWithCoder:(NSCoder*)coder
{
NSLog(@"[MyTree encodeWithCoder:%@]",coder);
//[super encodeWithCoder:coder];
[coder encodeObject:_nodeValue];
[coder encodeObject:_children];
}
-(id)initWithCoder:(NSCoder*)coder
{
NSLog(@"[MyTree initWithCoder:%@]",coder);
//self = [super initWithCoder:coder];
self = [self init];
_parent = nil;
_nodeValue = [[coder decodeObject] retain];
_children = [[coder decodeObject] retain];
[_children makeObjectsPerformSelector:@selector(setParent:) withObject:self];
return self;
}
-(id)init
{
self = [super init];
@ -104,8 +140,10 @@
NSArray* origArray = nil;
NSEnumerator* enumerator = nil;
NSDictionary* childDict = nil;
id nodeValue = nil;
nodeValue = [dict objectForKey:@"nodeValue"];
id <NSCoding> nodeValue = nil;
NSLog(@"[MyTree initWithDictionary:%@ parent:%@]",dict,parent);
// this part could use some work
nodeValue = [[[CelestiaFavorite alloc] initWithDictionary:[dict objectForKey:@"nodeValue"]] autorelease];
// Leaf
if ((origArray = [dict objectForKey:@"children"]) == nil)
return [self initWithNode:nodeValue parent:parent];
@ -158,7 +196,7 @@
}
-(NSDictionary*)dictionary
{
return [NSDictionary dictionaryWithObjectsAndKeys:[self nodeValue],@"nodeValue",[NSNumber numberWithBool:[self isLeaf]],@"isLeaf",[self children],@"children",nil,nil];
return [NSDictionary dictionaryWithObjectsAndKeys:[[self nodeValue] dictionary],@"nodeValue",[NSNumber numberWithBool:[self isLeaf]],@"isLeaf",[self children],@"children",nil,nil];
}
-(NSDictionary*)recursiveDictionary
{
@ -171,7 +209,7 @@
array = [NSMutableArray arrayWithCapacity:[[self children] count]];
while ((obj = [enumerator nextObject]) != nil)
[array addObject:[obj recursiveDictionary]];
return [NSDictionary dictionaryWithObjectsAndKeys:_nodeValue,@"nodeValue",array,@"children",nil,nil];
return [NSDictionary dictionaryWithObjectsAndKeys:[[self nodeValue] dictionary],@"nodeValue",[NSArray arrayWithArray:array],@"children",nil,nil];
}
-(NSString*)description
{