/* CStackDocument.m - NSDocument subclass 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 "CStackDocument.h" #include "EditorWindowController.h" #include "ChoiceQuizController.h" #include "SpellingQuizController.h" #include "CardQuizController.h" #include "Constants.h" @implementation CStackDocument - (void) dealloc { RELEASE(cardStack); [super dealloc]; } - (id) init { if( (self = [super init]) ) { cardStack = [[StackModel alloc] init]; return self; } return nil; } - (void) insertImportedCards: (NSArray *)cards { EditorWindowController *ec = [[self windowControllers] objectAtIndex: 0]; [[ec cardStack] insertData: cards atRow: 0 inTableView: [ec cardsView]]; } - (void) exportFile: (id)sender { NSArray *cards = [[[[self windowControllers] objectAtIndex: 0] cardStack] cards]; NSString *path = [[self fileName] stringByDeletingLastPathComponent]; NSString *name = [[[self fileName] lastPathComponent] stringByDeletingPathExtension]; NSMutableString *stringData = [[NSMutableString alloc] init]; NSSavePanel *sPanel = [NSSavePanel savePanel]; int i; [sPanel setAccessoryView: nil]; [sPanel setRequiredFileType: @"txt"]; [sPanel setTitle: _(@"Export")]; [sPanel setPrompt: _(@"Export")]; if( [sPanel runModalForDirectory: path file: name] ) { for( i=0; i < [cards count]; i++) { [stringData appendString: [NSString stringWithFormat: @"\n%@\n%@\n", [[cards objectAtIndex: i] front], [[cards objectAtIndex: i] back]]]; } if( stringData != nil ) [stringData writeToFile: [sPanel filename] atomically: YES]; else NSRunInformationalAlertPanel(_(@"Export Error"), [NSString stringWithFormat: _(@"An error occured while trying to export %@"), name], nil,nil,@"OK"); } } - (void) startDefaultQuizWithMethod: (int)method { NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; NSString *quiz; if( method == SHOWONCE_QUIZ_METHOD ) quiz = [def objectForKey: @"DefaultQuiz"]; else quiz = [def objectForKey: @"DefaultPractice"]; if( [quiz isEqual: @"MULTIPLE_CHOICE_QUIZ"] ) [self startChoiceQuizWithMethod: method]; else if( [quiz isEqual: @"SPELLING_QUIZ"] ) [self startSpellingQuizWithMethod: method]; else [self startCardQuizWithMethod: method]; } - (void) startChoiceQuizWithMethod: (int)method { ChoiceQuizController *quiz; NSRect editorF; NSPoint center; NSPoint quizO; editorF = [[[[self windowControllers] objectAtIndex: 0] window] frame]; center = NSMakePoint(editorF.origin.x + editorF.size.width/2, editorF.origin.y + editorF.size.height/2); quizO = NSMakePoint(center.x-210, center.y-132); // Numbers must be in sync with the gorm file [[[[self windowControllers] objectAtIndex: 0] window] orderOut: self]; quiz = [[ChoiceQuizController alloc] initWithStack: cardStack]; [quiz setQuizMethod: method]; [[quiz window] setFrameOrigin: quizO]; [[quiz window] makeKeyAndOrderFront: self]; [self addWindowController: quiz]; [quiz beginQuiz: self]; } - (void) startCardQuizWithMethod: (int)method { CardQuizController *quiz; NSRect editorF; NSPoint center; NSPoint quizO; editorF = [[[[self windowControllers] objectAtIndex: 0] window] frame]; center = NSMakePoint(editorF.origin.x + editorF.size.width/2, editorF.origin.y + editorF.size.height/2); quizO = NSMakePoint(center.x-210, center.y-120); // Numbers must be in sync with the gorm file [[[[self windowControllers] objectAtIndex: 0] window] orderOut: self]; quiz = [[CardQuizController alloc] initWithStack: cardStack]; [quiz setQuizMethod: method]; [[quiz window] setFrameOrigin: quizO]; [[quiz window] makeKeyAndOrderFront: self]; [self addWindowController: quiz]; [quiz beginQuiz: self]; } - (void) startSpellingQuizWithMethod: (int)method { SpellingQuizController *quiz; NSRect editorF; NSPoint center; NSPoint quizO; editorF = [[[[self windowControllers] objectAtIndex: 0] window] frame]; center = NSMakePoint(editorF.origin.x + editorF.size.width/2, editorF.origin.y + editorF.size.height/2); quizO = NSMakePoint(center.x-210, center.y-125); // Numbers must be in sync with the gorm file [[[[self windowControllers] objectAtIndex: 0] window] orderOut: self]; quiz = [[SpellingQuizController alloc] initWithStack: cardStack]; [quiz setQuizMethod: method]; [[quiz window] setFrameOrigin: quizO]; [[quiz window] makeKeyAndOrderFront: self]; [self addWindowController: quiz]; [quiz beginQuiz: self]; } - (void) editStack { if( [[self windowControllers] count] > 1 ) { [self removeWindowController: [[self windowControllers] objectAtIndex: 1]]; } [[[[self windowControllers] objectAtIndex: 0] window] makeKeyAndOrderFront: self]; [[[self windowControllers] objectAtIndex: 0] reloadScheduleView: self]; } // NSDocument Override methods // *************************** - (BOOL) loadDataRepresentation: (NSData *)docData ofType: (NSString *)docType { NSString *tmp = AUTORELEASE([[NSString alloc] initWithData: docData encoding: NSASCIIStringEncoding]); if( [docType isEqualToString: @"card stack"] && tmp != nil ) { if( [tmp rangeOfString: @" 0 ) { tmp = AUTORELEASE([[NSString alloc] initWithData: docData encoding: NSUTF8StringEncoding]); } RELEASE(cardStack); cardStack = [(StackModel *)[StackModel alloc] initWithDictionary: [tmp propertyList]]; return YES; } return NO; } - (NSData *) dataRepresentationOfType: (NSString *)aType { if ( [aType isEqualToString: @"card stack"] ) { return [[[cardStack stackFileRep] description] dataUsingEncoding: NSASCIIStringEncoding]; } return nil; } - (void) makeWindowControllers { EditorWindowController *editor; editor = AUTORELEASE([[EditorWindowController alloc] initWithStack: cardStack]); [self addWindowController: editor]; } @end