/* CardModel.m - card model class for Popup.app Copyright (C) 2003, 2004 Rob Burns This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02111, USA. */ #include "CardModel.h" #include "Constants.h" @implementation CardModel + (CardModel *) cardWithDictionary: (NSDictionary *)cDict { CardModel *card = AUTORELEASE([[CardModel alloc] init]); NSMutableArray *tempHistory = nil; NSEnumerator *hEnum = nil; NSDictionary *hEntry = nil; [card setFront: [cDict objectForKey: @"FirstValue"]]; [card setBack: [cDict objectForKey: @"SecondValue"]]; if( [cDict objectForKey: @"Slot"] ) [card setSlot: [[cDict objectForKey: @"Slot"] intValue]]; else [card setSlot: NOSLOT]; if( [cDict objectForKey: @"Time"] ) [card setTime: [NSDate dateWithString: [cDict objectForKey: @"Time"]]]; if( [[cDict objectForKey: @"History"] count] > 0) { tempHistory = [NSMutableArray arrayWithCapacity: 1]; hEnum = [[cDict objectForKey: @"History"] objectEnumerator]; while( (hEntry = [hEnum nextObject]) ) { [tempHistory addObject: [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: [[hEntry objectForKey: @"Slot"] intValue]], @"Slot", [NSDate dateWithString: [hEntry objectForKey: @"Time"]], @"Time", [NSNumber numberWithInt: [[hEntry objectForKey: @"Result"] intValue]], @"Result", nil]]; } [card setHistory: tempHistory]; } return card; } - (void) dealloc { RELEASE(front); RELEASE(back); RELEASE(answers); RELEASE(history); TEST_RELEASE(time); [super dealloc]; } - (id) init { if( (self = [super init]) ) { front = [[NSMutableString alloc] init]; back = [[NSMutableString alloc] init]; answers = [[NSMutableArray alloc] init]; slot = NOSLOT; time = nil; history = [[NSMutableArray alloc] init]; return self; } return nil; } - (NSString *) front { return front; } - (void) setFront: (NSString *) aWord { [front setString: aWord]; } - (NSString *) back { return back; } - (void) setBack: (NSString *) aWord { [back setString: aWord]; } - (int) slot { return slot; } - (void) setSlot: (int) newSlot { slot = newSlot; } - (NSDate *) time { return time; } - (void) setTime: (NSDate *) newTime { ASSIGN(time, newTime); } - (NSMutableArray *) history { return history; } - (void) setHistory: (NSArray *)anArray { ASSIGN(history, anArray); } - (void) addHistoryEntry: (NSDictionary *)anEntry { [history addObject: anEntry]; } -(NSString *) description; { NSMutableString *desc; desc = [[NSMutableString init] alloc]; [desc appendFormat: @"Front - %@, Back - %@\n", front, back]; [desc appendFormat: @"Slot - %i, Time - %@\n", slot, [time description]]; [desc appendFormat: @"History - %@\n", [history description]]; return [NSString stringWithString: desc]; } - (NSDictionary *) words { return [NSDictionary dictionaryWithObjectsAndKeys: [self front], @"Front", [self back], @"Back", nil]; } - (NSDictionary *) cardFileRep; { NSMutableDictionary *cDict = [NSMutableDictionary dictionaryWithCapacity: 1]; NSMutableArray *hArray = [NSMutableArray arrayWithCapacity: 1]; NSEnumerator *hEnum = [history objectEnumerator]; NSDictionary *hEntry = nil; [cDict setObject: front forKey: @"FirstValue"]; [cDict setObject: back forKey: @"SecondValue"]; [cDict setObject: [NSString stringWithFormat: @"%i", slot] forKey: @"Slot"]; if( time != nil ) [cDict setObject: [time description] forKey: @"Time"]; while( (hEntry = [hEnum nextObject]) ) { [hArray addObject: [NSDictionary dictionaryWithObjectsAndKeys: [[hEntry objectForKey: @"Slot"] stringValue], @"Slot", [[hEntry objectForKey: @"Time"] description], @"Time", [[hEntry objectForKey: @"Result"] stringValue], @"Result", nil]]; } [cDict setObject: hArray forKey: @"History"]; return cDict; } // For use in cards returned to quizes. cards found // in the stackmodel do not have these attributes. // index reflects the index in the current set of // filtered cards. The quizes should probably be // restructured, so that this index stuff isn't // necessary. - (int) index { return index; } - (void) setIndex: (int) newIndex { index = newIndex; } - (NSArray *) answers { return answers; } - (void) setAnswers: (NSArray *) newAnswers { [answers setArray: newAnswers]; } @end