// // BPPilesDataController.m // Attempt2 // // Created by Bea on 12/05/05. // Copyright 2005 __MyCompanyName__. All rights reserved. // #import "BPPilesDataController.h" #import "BPPilesDisplayController.h" //#import "BPPilesData.h" #import "BPIcon.h" #import "BPIconGroup.h" #import "BPDocumentGroup.h" #import "BPDocument.h" #import "BPPerson.h" @implementation BPPilesDataController + (void)initialize { INBOX = [[BPDocumentGroup alloc] initWithLabel:@"Inbox" atPosition:NSMakePoint(50,40)]; [INBOX setIsSystemGroup:YES]; DRAFTS = [[BPDocumentGroup alloc] initWithLabel:@"Drafts" atPosition:NSMakePoint(400,100)]; [DRAFTS setIsSystemGroup:YES]; TRASH = [[BPDocumentGroup alloc] initWithLabel:@"Trash" atPosition:NSMakePoint(600,50)]; [TRASH setIsSystemGroup:YES]; } + (BPDocumentGroup *)INBOX { return INBOX; } + (BPDocumentGroup *)DRAFTS { return DRAFTS; } + (BPDocumentGroup *)TRASH { return TRASH; } - (void)registerForNotifications { NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; // icon notifications [defaultCenter addObserver:self selector:@selector(handleNotification:) name:@"Icon_PositionChanged" object:nil]; [defaultCenter addObserver:self selector:@selector(handleNotification:) name:@"Icon_ColourChanged" object:nil]; // icon group notifications [defaultCenter addObserver:self selector:@selector(handleNotification:) name:@"IconGroup_PositionChanged" object:nil]; [defaultCenter addObserver:self selector:@selector(handleNotification:) name:@"IconGroup_IconAdded" object:nil]; [defaultCenter addObserver:self selector:@selector(handleNotification:) name:@"IconGroup_IconRemoved" object:nil]; [defaultCenter addObserver:self selector:@selector(handleNotification:) name:@"IconGroup_LabelChanged" object:nil]; } - (void)awakeFromNib { [self addNewPile:INBOX]; [self addNewPile:DRAFTS]; [self addNewPile:TRASH]; [self registerForNotifications]; } - (id)init { [super init]; piles = [[NSMutableArray alloc] initWithCapacity:50]; return self; } - (void)savePileDataToDict:(NSMutableDictionary *)dict { [dict setValue:piles forKey:@"piles"]; } - (void)loadPileDataFromDict:(NSDictionary *)archivedDict { /* NSString *path = [self pathForDataFile]; NSDictionary *rootObject; rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; //[self addPiles:[rootObject valueForKey:@"piles"]]; */ NSArray *loadedPiles = [archivedDict valueForKey:@"piles"]; if (loadedPiles == nil) return; [displayController clearDisplay]; BPDocumentGroup *systemPile; BPDocumentGroup *pile; NSArray *docs; int i, j; for (i=0; i<[loadedPiles count]; i++) { pile = [loadedPiles objectAtIndex:i]; if ([pile isSystemGroup]) { // this is a special system pile, so don't add it again as a new pile. // just add all its contents into the corresponding system pile that already exists. if ([[pile label] isEqual:[INBOX label]]) { systemPile = INBOX; } else if ([[pile label] isEqual:[DRAFTS label]]) { systemPile = DRAFTS; } else if ([[pile label] isEqual:[TRASH label]]) { systemPile = TRASH; } else { NSLog(@"loadPileData error: cannot find corresponding existing system pile for pile %@", pile); continue; } // note the position [systemPile setPosition:[pile position]]; [displayController movedPile:systemPile]; docs = [pile documents]; for (j=0; j<[docs count]; j++) { [self addPileDocument:[docs objectAtIndex:j] toPile:systemPile]; } } else { [self addNewPile:pile]; } } } - (void)handleNotification:(NSNotification *)notif { if ([[notif name] isEqual:@"Icon_PositionChanged"]) { // record the details of the icon BPIcon *icon = [[notif userInfo] objectForKey:@"Icon"]; id document = [icon representedObject]; [document setMarkAmount:[icon markAmount]]; [document setFlagged:[icon isMarked]]; } else if ([[notif name] isEqual:@"Icon_ColourChanged"]) { BPIcon *icon = [[notif userInfo] objectForKey:@"Icon"]; id document = [icon representedObject]; id colour = [[notif userInfo] objectForKey:@"Colour"]; if ([colour isKindOfClass:[NSColor class]]) { [document setColourCode:colour]; } else { [document setColourCode:nil]; } } else if ([[notif name] isEqual:@"IconGroup_PositionChanged"]) { BPIconGroup *group = [[notif userInfo] objectForKey:@"IconGroup"]; [[group representedObject] setPosition:[group position]]; } else if ([[notif name] isEqual:@"IconGroup_IconAdded"]) { BPIconGroup *group = [[notif userInfo] objectForKey:@"IconGroup"]; BPIcon *icon = [[notif userInfo] objectForKey:@"Icon"]; [[group representedObject] add:[icon representedObject]]; } else if ([[notif name] isEqual:@"IconGroup_IconRemoved"]) { BPIconGroup *group = [[notif userInfo] objectForKey:@"IconGroup"]; BPIcon *icon = [[notif userInfo] objectForKey:@"Icon"]; [[group representedObject] remove:[icon representedObject]]; } else if ([[notif name] isEqual:@"IconGroup_LabelChanged"]) { BPIconGroup *group = [[notif userInfo] objectForKey:@"IconGroup"]; [[group representedObject] setLabel:[group label]]; //NSLog(@"new label %@ %@", [group representedObject], [[group representedObject] label]); // user has changed the label [[group representedObject] setLabelWasSetBySystem:NO]; } } - (BPDocumentGroup *)pileWithName:(NSString *)pileName { int i; BPDocumentGroup *group; for (i=0; i<[piles count]; i++) { group = [piles objectAtIndex:i]; if ([[group label] caseInsensitiveCompare:pileName] == NSOrderedSame) { return group; } } return nil; } - (void)addNewPile:(BPDocumentGroup *)pile { // store new pile [piles addObject:pile]; // update display [displayController addedPile:pile]; } - (void)addPileDocument:(NSObject *)document toPile:(BPDocumentGroup *)pile { if (![pile isKindOfClass:[BPDocumentGroup class]]) return; // add pile [pile add:document]; // update display [displayController addedDocument:document toPile:pile]; } - (void)removePileDocument:(NSObject *)document fromPile:(BPDocumentGroup *)pile { // update display [displayController removedDocument:document fromPile:pile]; // remove pile [pile remove:document]; } - (void)movePileDocument:(NSObject *)document toPile:(BPDocumentGroup *)destPile { // move the document // surround with retain/release in case removing document from its group will release it [document retain]; BPDocumentGroup *srcPile = [document documentGroup]; [srcPile remove:document]; [destPile add:document]; [document release]; // update display [displayController movedDocument:document fromPile:srcPile toPile:destPile]; } - (void)toggleFlagForDocument:(NSObject *)document { if ([document documentGroup] != nil) { [document setFlagged:![document isFlagged]]; [displayController changedFlagForDocument:document]; } } /* - (void)deleteDocument:(NSObject *)document moveToTrash:(BOOL)moveToTrash { if (moveToTrash) { [self movePileDocument:document toPile:[BPPilesDataController TRASH]]; } else { [self removePileDocument:document fromPile:[document documentGroup]]; } } */ - (void)emptyTrashPile { /* int i; NSObject *document; NSLog(@"[TRASH documents] %@", [TRASH documents]); for (i=0; i<[[TRASH documents] count]; i++) { document = [[TRASH documents] objectAtIndex:i]; NSLog(@"remove %@", document); [self removePileDocument:document fromPile:TRASH]; } */ //[displayController clearPile:TRASH]; } - (void)saveDocumentIntoAPile:(NSObject *)document { // we only want to deal with documents that aren't in a group yet if ([document documentGroup] != nil) return; [self addPileDocument:document toPile:[BPPilesDataController DRAFTS]]; [displayController refreshDraftsPile]; } #pragma - - (void)viewCreatedGroup:(BPDocumentGroup *)group { if (group == nil) { NSLog(@"viewCreatedGroup error: group is nil"); return; } // store new pile [piles addObject:group]; } - (void)viewDeletedGroup:(BPDocumentGroup *)group { if (group == nil) { NSLog(@"viewDeletedGroup error: group is nil (or I just haven't fixed the way deleting groups works)"); return; } // remove pile [piles removeObject:group]; } - (void)changedGlobalViewType { BOOL useMicroView = ([[NSUserDefaults standardUserDefaults] integerForKey:@"MailStacker_viewerType"] == 0); int i, j; BPDocumentGroup *group; NSArray *docs; id doc; for (i=0; i<[piles count]; i++) { group = [piles objectAtIndex:i]; docs = [group documents]; for (j=0; j<[docs count]; j++) { doc = [docs objectAtIndex:j]; [doc setInMicroView:useMicroView]; } } } #pragma - - (void)dealloc { NSLog(@"destroying %@", self); [piles release]; [super dealloc]; } @end