Save and restore configuration using NSUserDefaults interface.

Subversion-branch: /launcher
Subversion-revision: 1761
This commit is contained in:
Simon Howard 2009-12-26 02:10:31 +00:00
parent c0ed4e93e2
commit d74fb3d055
8 changed files with 226 additions and 40 deletions

View file

@ -20,15 +20,20 @@
// //
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef LAUNCHER_IWADCONTROLLER_H
#define LAUNCHER_IWADCONTROLLER_H
#include <AppKit/AppKit.h> #include <AppKit/AppKit.h>
#include <AppKit/NSNibLoading.h>
@interface IWADController : NSObject @interface IWADController : NSObject
{ {
id iwadSelector;
id configWindow;
id chex; id chex;
id doom1; id doom1;
id doom2; id doom2;
id iwadSelector;
id configWindow;
id plutonia; id plutonia;
id tnt; id tnt;
} }
@ -36,6 +41,12 @@
- (void) closeConfigWindow: (id)sender; - (void) closeConfigWindow: (id)sender;
- (void) openConfigWindow: (id)sender; - (void) openConfigWindow: (id)sender;
- (NSString *) getIWADLocation; - (NSString *) getIWADLocation;
- (void) awakeFromNib;
- (BOOL) setDropdownList;
- (void) setDropdownSelection;
- (void) saveConfig;
@end @end
#endif /* #ifndef LAUNCHER_IWADCONTROLLER_H */

View file

@ -43,6 +43,16 @@ static NSString *IWADLabels[NUM_IWAD_TYPES] =
@"Chex Quest" @"Chex Quest"
}; };
static NSString *IWADFilenames[NUM_IWAD_TYPES + 1] =
{
@"doom.wad",
@"doom2.wad",
@"tnt.wad",
@"plutonia.wad",
@"chex.wad",
@"undefined"
};
@implementation IWADController @implementation IWADController
- (void) getIWADList: (IWADLocation **) iwadList - (void) getIWADList: (IWADLocation **) iwadList
@ -54,39 +64,6 @@ static NSString *IWADLabels[NUM_IWAD_TYPES] =
iwadList[IWAD_CHEX] = self->chex; iwadList[IWAD_CHEX] = self->chex;
} }
// Set the dropdown list to include an entry for each IWAD that has
// been configured. Returns true if at least one IWAD is configured.
- (BOOL) setDropdownList
{
IWADLocation *iwadList[NUM_IWAD_TYPES];
BOOL have_wads;
id location;
unsigned int i;
unsigned int enabled_wads;
[self getIWADList: iwadList];
[self->iwadSelector removeAllItems];
enabled_wads = 0;
for (i=0; i<NUM_IWAD_TYPES; ++i)
{
location = [iwadList[i] getLocation];
if (location != nil && [location length] > 0)
{
[self->iwadSelector addItemWithTitle: IWADLabels[i]];
++enabled_wads;
}
}
have_wads = enabled_wads > 0;
[self->iwadSelector setEnabled: have_wads];
return have_wads;
}
- (IWAD) getSelectedIWAD - (IWAD) getSelectedIWAD
{ {
unsigned int i; unsigned int i;
@ -123,6 +100,132 @@ static NSString *IWADLabels[NUM_IWAD_TYPES] =
} }
} }
- (void) setIWADConfig
{
IWADLocation *iwadList[NUM_IWAD_TYPES];
NSUserDefaults *defaults;
NSString *key;
NSString *value;
unsigned int i;
[self getIWADList: iwadList];
// Load IWAD filename paths
defaults = [NSUserDefaults standardUserDefaults];
for (i=0; i<NUM_IWAD_TYPES; ++i)
{
key = IWADFilenames[i];
value = [defaults stringForKey:key];
if (value != nil)
{
[iwadList[i] setLocation:value];
}
}
}
// On startup, set the selected item in the IWAD dropdown
- (void) setDropdownSelection
{
NSUserDefaults *defaults;
NSString *selected;
unsigned int i;
defaults = [NSUserDefaults standardUserDefaults];
selected = [defaults stringForKey: @"selected_iwad"];
if (selected == nil)
{
return;
}
// Find this IWAD in the filenames list, and select it.
for (i=0; i<NUM_IWAD_TYPES; ++i)
{
if ([selected isEqualToString:IWADFilenames[i]])
{
[self->iwadSelector selectItemWithTitle:IWADLabels[i]];
break;
}
}
}
// Set the dropdown list to include an entry for each IWAD that has
// been configured. Returns true if at least one IWAD is configured.
- (BOOL) setDropdownList
{
IWADLocation *iwadList[NUM_IWAD_TYPES];
BOOL have_wads;
id location;
unsigned int i;
unsigned int enabled_wads;
// Build the new list.
[self getIWADList: iwadList];
[self->iwadSelector removeAllItems];
enabled_wads = 0;
for (i=0; i<NUM_IWAD_TYPES; ++i)
{
location = [iwadList[i] getLocation];
if (location != nil && [location length] > 0)
{
[self->iwadSelector addItemWithTitle: IWADLabels[i]];
++enabled_wads;
}
}
// Enable/disable the dropdown depending on whether there
// were any configured IWADs.
have_wads = enabled_wads > 0;
[self->iwadSelector setEnabled: have_wads];
// Restore the old selection.
[self setDropdownSelection];
return have_wads;
}
- (void) saveConfig
{
IWADLocation *iwadList[NUM_IWAD_TYPES];
IWAD selectedIWAD;
NSUserDefaults *defaults;
NSString *key;
NSString *value;
unsigned int i;
[self getIWADList: iwadList];
// Store all IWAD locations to user defaults.
defaults = [NSUserDefaults standardUserDefaults];
for (i=0; i<NUM_IWAD_TYPES; ++i)
{
key = IWADFilenames[i];
value = [iwadList[i] getLocation];
[defaults setObject:value forKey:key];
}
// Save currently selected IWAD.
selectedIWAD = [self getSelectedIWAD];
[defaults setObject:IWADFilenames[selectedIWAD]
forKey:@"selected_iwad"];
}
// Callback method invoked when the configuration button in the main // Callback method invoked when the configuration button in the main
// window is clicked. // window is clicked.
@ -139,15 +242,24 @@ static NSString *IWADLabels[NUM_IWAD_TYPES] =
- (void) closeConfigWindow: (id)sender - (void) closeConfigWindow: (id)sender
{ {
[self->configWindow orderOut: sender]; [self->configWindow orderOut: sender];
[self saveConfig];
[self setDropdownList]; [self setDropdownList];
} }
- (void) awakeFromNib - (void) awakeFromNib
{ {
// Set configuration for all IWADs from configuration file.
[self setIWADConfig];
// Populate the dropdown IWAD list, and open the configuration // Populate the dropdown IWAD list, and open the configuration
// dialog if not yet configured. // dialog if not yet configured.
if (![self setDropdownList]) if ([self setDropdownList])
{
[self setDropdownSelection];
}
else
{ {
[self openConfigWindow: nil]; [self openConfigWindow: nil];
} }

View file

@ -20,10 +20,17 @@
// //
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef LAUNCHER_IWADLOCATION_H
#define LAUNCHER_IWADLOCATION_H
#include <AppKit/AppKit.h> #include <AppKit/AppKit.h>
#include "IWADController.h"
@interface IWADLocation : NSObject @interface IWADLocation : NSObject
{ {
IWADController *iwadController;
id locationConfigBox; id locationConfigBox;
} }
@ -33,3 +40,5 @@
@end @end
#endif /* #ifndef LAUNCHER_IWADLOCATION_H */

View file

@ -54,6 +54,9 @@ static id WAD_TYPES[] =
{ {
filenames = [openPanel filenames]; filenames = [openPanel filenames];
[self setLocation: [filenames lastObject]]; [self setLocation: [filenames lastObject]];
[self->iwadController saveConfig];
[self->iwadController setDropdownList];
} }
} }

View file

@ -20,19 +20,28 @@
// //
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef LAUNCHER_LAUNCHERMANAGER_H
#define LAUNCHER_LAUNCHERMANAGER_H
#include <AppKit/AppKit.h> #include <AppKit/AppKit.h>
#include <AppKit/NSNibLoading.h>
#include "IWADController.h" #include "IWADController.h"
@interface LauncherManager : NSObject @interface LauncherManager : NSObject
{ {
IWADController *iwadController;
id launcherWindow;
id commandLineArguments; id commandLineArguments;
id packageLabel; id packageLabel;
id launcherWindow;
IWADController *iwadController;
} }
- (void) launch: (id)sender; - (void) launch: (id)sender;
- (void) runSetup: (id)sender; - (void) runSetup: (id)sender;
- (void) awakeFromNib;
@end @end
#endif /* #ifndef LAUNCHER_LAUNCHERMANAGER_H */

View file

@ -26,25 +26,66 @@
@implementation LauncherManager @implementation LauncherManager
// Save configuration. Invoked when we launch the game or quit.
- (void) saveConfig
{
NSUserDefaults *defaults;
// Save IWAD configuration and selected IWAD.
[self->iwadController saveConfig];
// Save command line arguments.
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[self->commandLineArguments stringValue]
forKey:@"command_line_args"];
}
// Load configuration, invoked on startup.
- (void) setConfig
{
NSUserDefaults *defaults;
NSString *args;
defaults = [NSUserDefaults standardUserDefaults];
args = [defaults stringForKey:@"command_line_args"];
if (args != nil)
{
[self->commandLineArguments setStringValue:args];
}
}
- (void) launch: (id)sender - (void) launch: (id)sender
{ {
NSString *iwad; NSString *iwad;
NSString *args; NSString *args;
[self saveConfig];
iwad = [self->iwadController getIWADLocation]; iwad = [self->iwadController getIWADLocation];
args = [self->commandLineArguments stringValue]; args = [self->commandLineArguments stringValue];
printf("Command line: %s %s\n", [iwad UTF8String], [args UTF8String]); if (iwad != nil)
{
printf("Command line: %s %s\n", [iwad UTF8String], [args UTF8String]);
}
} }
- (void) runSetup: (id)sender - (void) runSetup: (id)sender
{ {
[self saveConfig];
} }
- (void) awakeFromNib - (void) awakeFromNib
{ {
[self->packageLabel setStringValue: @PACKAGE_STRING]; [self->packageLabel setStringValue: @PACKAGE_STRING];
[self->launcherWindow setTitle: @PACKAGE_NAME " Launcher"]; [self->launcherWindow setTitle: @PACKAGE_NAME " Launcher"];
[self setConfig];
} }
@end @end

View file

@ -43,7 +43,8 @@
"setButtonClicked:" "setButtonClicked:"
); );
Outlets = ( Outlets = (
locationConfigBox locationConfigBox,
iwadController
); );
Super = NSObject; Super = NSObject;
}; };

Binary file not shown.