/* PreferencesController.m - preferences controller 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 "PreferencesController.h" #include "Constants.h" static PreferencesController *singleInstance = nil; @implementation PreferencesController + (void) initialize { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys: FLASHCARD_QUIZ, @"DefaultQuiz", FLASHCARD_QUIZ, @"DefaultPractice", @"3", @"Repetitions", nil]; [defaults registerDefaults: appDefaults]; } + (id) sharedPreferences { if ( !singleInstance ) { singleInstance = [[PreferencesController alloc] init]; } return singleInstance; } - (void) dealloc { [super dealloc]; } - (id) init { if( (self = [super initWithWindowNibName: @"Preferences" owner: self]) ) { [[self window] setFrameAutosaveName: @"Preferences"]; [[self window] setFrameUsingName: @"Preferences"]; return self; } return nil; } - (void) awakeFromNib { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // Localize the interface [[self window] setTitle: _(@"Preferences")]; [[tabView tabViewItemAtIndex: 0] setLabel: _(@"General")]; [[tabView tabViewItemAtIndex: 1] setLabel: _(@"Schedule")]; [quizBox setTitle: _(@"Quiz")]; [defQuizLabel setStringValue: _(@"Default Type:")]; [pracBox setTitle: _(@"Practice")]; [defPracLabel setStringValue: _(@"Default Type:")]; [repetitionsLabel setStringValue: _(@"Repetitions:")]; [[[scheduleTable tableColumnWithIdentifier: @"Title"] headerCell] setTitle: _(@"Title")]; [[[scheduleTable tableColumnWithIdentifier: @"Min"] headerCell] setTitle: _(@"Min")]; // Populate the popup buttons with valid choices [defQuizButton removeAllItems]; [defQuizButton addItemsWithTitles: [NSArray arrayWithObjects: _(@"Flashcard"), _(@"Multiple choice"), _(@"Spelling"), nil]]; [[defQuizButton itemWithTitle: _(@"Flashcard")] setRepresentedObject: FLASHCARD_QUIZ]; [[defQuizButton itemWithTitle: _(@"Multiple choice")] setRepresentedObject: MULTIPLE_CHOICE_QUIZ]; [[defQuizButton itemWithTitle: _(@"Spelling")] setRepresentedObject: SPELLING_QUIZ]; [defPracButton removeAllItems]; [defPracButton addItemsWithTitles: [NSArray arrayWithObjects: _(@"Flashcard"), _(@"Multiple choice"), _(@"Spelling"), nil]]; [[defPracButton itemWithTitle: _(@"Flashcard")] setRepresentedObject: FLASHCARD_QUIZ]; [[defPracButton itemWithTitle: _(@"Multiple choice")] setRepresentedObject: MULTIPLE_CHOICE_QUIZ]; [[defPracButton itemWithTitle: _(@"Spelling")] setRepresentedObject: SPELLING_QUIZ]; // Populate the interface with current values [defQuizButton selectItemAtIndex: [defQuizButton indexOfItemWithRepresentedObject: [defaults objectForKey: @"DefaultQuiz"]]]; [defPracButton selectItemAtIndex: [defQuizButton indexOfItemWithRepresentedObject: [defaults objectForKey: @"DefaultPractice"]]]; [repetitionsField setStringValue: [defaults objectForKey: @"Repetitions"]]; } // ************** // Action methods // ************** - (void) defQuizChanged: (id)sender { if( sender == defQuizButton ) { [[NSUserDefaults standardUserDefaults] setObject: [[sender selectedItem] representedObject] forKey: @"DefaultQuiz"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } - (void) defPracChanged: (id)sender; { if( sender == defPracButton ) { [[NSUserDefaults standardUserDefaults] setObject: [[sender selectedItem] representedObject] forKey: @"DefaultPractice"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } // ********************************** // NSTableDataSource protocol methods // ********************************** - (int) numberOfRowsInTableView: (NSTableView *)aTableView { return 0; } - (id) tableView: (NSTableView *)aTableView objectValueForTableColumn: (NSTableColumn *)aTableColumn row: (int)rowIndex { return @"blah"; } - (void) tableView: (NSTableView *)aTableView setObjectValue: (id)anObject forTableColumn: (NSTableColumn *)aTableColumn row: (int)rowIndex { } // **************************** // NSTextField delagate methods // **************************** - (void) controlTextDidEndEditing: (NSNotification *)aNotification { if( [aNotification object] == repetitionsField ) { if( [[repetitionsField stringValue] intValue] == 0 || [[repetitionsField stringValue] intValue] > 10 ) { NSRunInformationalAlertPanel(_(@"Invalid Input"), @"You must enter a number between 1 and 10. Try again.", _(@"OK"),nil,nil); } else { [[NSUserDefaults standardUserDefaults] setObject: [repetitionsField stringValue] forKey: @"Repetitions"]; [[NSUserDefaults standardUserDefaults] synchronize]; } } } @end