WIP hooking main preferences tab UI up to logic

This commit is contained in:
anthony 2021-01-27 20:04:36 +00:00
parent af28773254
commit 6009b38cf3
6 changed files with 145 additions and 9 deletions

View File

@ -22,4 +22,6 @@
-(NSString*)downloadLocationPath;
-(void)setDownloadLocationPath: (NSString*)aPath;
+(Preferences*)defaultPreferences;
@end

View File

@ -43,34 +43,64 @@
}
}
-(void)setSearchFromUrlBar: (BOOL)value {
[defaults setBool: value forKey: KEY_SEARCH_FROM_URL_BAR];
}
-(SearchProvider*)searchProvider {
return nil;
NSDictionary *dict = [defaults dictionaryForKey: KEY_SEARCH_PROVIDER];
SearchProvider *ret;
if (dict != nil) {
ret = [[SearchProvider alloc] initWithDictionary: dict];
[ret autorelease];
} else {
ret = [SearchProvider defaultSearchProvider];
}
return ret;
}
-(void)setSearchProvider: (SearchProvider*)aProvider {
[defaults setObject: [aProvider dictionaryRepresentation] forKey:
KEY_SEARCH_PROVIDER];
}
-(BOOL)removeDownloadsOnComplete {
return NO;
if ([defaults objectForKey: KEY_REMOVE_DOWNLOADS_COMPLETE] != nil) {
return [defaults boolForKey: KEY_REMOVE_DOWNLOADS_COMPLETE];
} else {
return NO;
}
}
-(void)setRemoveDownloadsOnComplete: (BOOL)value {
[defaults setBool: value forKey: KEY_REMOVE_DOWNLOADS_COMPLETE];
}
-(BOOL)confirmBeforeOverwriting {
return NO;
if ([defaults objectForKey: KEY_CONFIRM_OVERWRITE] != nil) {
return [defaults boolForKey: KEY_CONFIRM_OVERWRITE];
} else {
return NO;
}
}
-(void)setConfirmBeforeOverwriting: (BOOL)value {
[defaults setBool: value forKey: KEY_CONFIRM_OVERWRITE];
}
-(NSString*)downloadLocationPath {
return nil;
NSString *downloadPath = [defaults stringForKey: KEY_DOWNLOAD_LOCATION];
if (downloadPath != nil) {
return downloadPath;
} else {
return [@"~/Downloads" stringByExpandingTildeInPath];
}
}
-(void)setDownloadLocationPath: (NSString*)aPath {
[defaults setObject: aPath forKey: KEY_DOWNLOAD_LOCATION];
}
+(Preferences*)defaultPreferences {
static Preferences *prefs;
if (prefs == nil) {
prefs = [[Preferences alloc] init];
}
return prefs;
}
@end

View File

@ -7,6 +7,8 @@
id searchFromUrlButton;
id searchProviderButton;
id startupPageField;
NSMutableArray *downloadLocations;
}
-(void)didEnterStartupPage: (id)sender;
-(void)didPickDownloadLocation: (id)sender;

View File

@ -1,9 +1,43 @@
#import <AppKit/AppKit.h>
#import "PreferencesWindowController.h"
#import "Preferences.h"
#import "SearchProvider.h"
#define DL_DOWNLOADS_PATH [@"~/Downloads" stringByExpandingTildeInPath]
#define DL_HOME_PATH [@"~/" stringByExpandingTildeInPath]
#define DL_DESKTOP_PATH [@"~/Desktop" stringByExpandingTildeInPath]
@interface PreferencesWindowController (Private)
-(void)configureMainTab;
@end
@interface DownloadLocation: NSObject {
NSString *name;
NSString *path;
}
@end
@implementation DownloadLocation
-(id)initWithName: (NSString*)aName path: (NSString*)aPath {
if ((self = [super init])) {
name = [aName retain];
path = [aPath retain];
}
return self;
}
+(DownloadLocation*)downloadLocationWithName: (NSString*)aName path: (NSString*)aPath {
return [[[DownloadLocation alloc] initWithName: aName path: aPath] autorelease];
}
-(void)dealloc {
[name release];
[path release];
[super dealloc];
}
-(NSString*)name {
return name;
}
-(NSString*)path {
return path;
}
@end
@implementation PreferencesWindowController
@ -14,6 +48,11 @@
return self;
}
-(void)dealloc {
[downloadLocations release];
[super dealloc];
}
-(void)awakeFromNib {
[self configureMainTab];
}
@ -22,15 +61,72 @@
-(void)configureMainTab {
NSLog(@"configure main tab");
[startupPageField setStringValue: [[Preferences defaultPreferences] startupUrl]];
[searchFromUrlButton setState: [[Preferences defaultPreferences] searchFromUrlBar] ?
NSOnState : NSOffState];
[searchProviderButton removeAllItems];
NSArray *searchProviders = [SearchProvider allProviders];
SearchProvider *currentProvider = [[Preferences defaultPreferences] searchProvider];
NSInteger selectedIndex = 0;
for (NSUInteger i = 0; i < [searchProviders count]; i++) {
SearchProvider *provider = [searchProviders objectAtIndex: i];
[searchProviderButton addItemWithTitle: [provider name]];
if ([[provider name] isEqual: [currentProvider name]]) {
selectedIndex = i;
}
}
[searchProviderButton selectItemAtIndex: selectedIndex];
[downloadRemoveOnCompleteButton setState: [[Preferences defaultPreferences]
removeDownloadsOnComplete] ? NSOnState : NSOffState];
[downloadConfirmOverwriteButton setState: [[Preferences defaultPreferences]
removeDownloadsOnComplete] ? NSOnState : NSOffState];
downloadLocations = [NSMutableArray arrayWithObjects:
[DownloadLocation downloadLocationWithName: @"Downloads" path:
DL_DOWNLOADS_PATH],
[DownloadLocation downloadLocationWithName: @"Desktop" path:
DL_DESKTOP_PATH],
[DownloadLocation downloadLocationWithName: @"Home" path: DL_HOME_PATH],
[DownloadLocation downloadLocationWithName: @"Other..." path: nil],
nil
];
[downloadLocations retain];
[downloadLocationButton removeAllItems];
NSString *path = [[Preferences defaultPreferences] downloadLocationPath];
selectedIndex = -1;
for (NSUInteger i = 0; i < [downloadLocations count]; i++) {
DownloadLocation *loc = [downloadLocations objectAtIndex: i];
[downloadLocationButton addItemWithTitle: [loc name]];
if ([path isEqual: [loc path]]) {
selectedIndex = i;
}
}
if (selectedIndex == -1) {
[downloadLocations addObject: [DownloadLocation downloadLocationWithName:
[path stringByAbbreviatingWithTildeInPath] path: path]];
selectedIndex = [downloadLocations count] - 1;
}
[downloadLocationButton selectItemAtIndex: selectedIndex];
}
-(void)didEnterStartupPage: (id)sender {
NSLog(@"Did enter startup page");
[[Preferences defaultPreferences] setStartupUrl: [sender stringValue]];
}
-(void)didPickDownloadLocation: (id)sender {
NSLog(@"Did pick download location");
NSInteger idx = [sender indexOfItem: [sender selectedItem]];
DownloadLocation *loc = [downloadLocations objectAtIndex: idx];
if ([loc path] == nil) {
// Show file picker.
} else {
[[Preferences defaultPreferences] setDownloadLocationPath: [loc path]];
}
}

View File

@ -12,6 +12,8 @@
-(Website*)websiteForQuery: (NSString*)queryString;
-(NSDictionary*)dictionaryRepresentation;
-(NSString*)name;
+(NSArray*)allProviders;
+(SearchProvider*)defaultSearchProvider;

View File

@ -26,6 +26,10 @@
[super dealloc];
}
-(NSString*)name {
return name;
}
-(Website*)websiteForQuery: (NSString*)queryString {
NSString *url = [searchUrl stringByReplacingOccurrencesOfString: @"%s" withString:
[queryString stringByAddingPercentEscapesUsingEncoding:
@ -39,7 +43,7 @@
}
+(NSArray*)allProviders {
return [NSArray array];
return [NSArray arrayWithObject: [SearchProvider defaultSearchProvider]];
}
+(SearchProvider*)defaultSearchProvider {