Implement 'load images' preference

This commit is contained in:
anthony 2021-04-28 21:32:32 +01:00
parent 56496c6b2a
commit 953c9fd4d4
5 changed files with 87 additions and 15 deletions

View File

@ -281,9 +281,15 @@ int main(int argc, char **argv) {
/* common initialisation */
error = nsoption_init(set_defaults, &nsoptions, &nsoptions_default);
NSCAssert(error == NSERROR_OK, @"Options failed to initialise");
error = netsurf_init(NULL);
NSCAssert(error == NSERROR_OK, @"NetSurf failed to initialise");
NSCAssert(error == NSERROR_OK, @"Options failed to initialise");
error = netsurf_init(NULL);
NSCAssert(error == NSERROR_OK, @"NetSurf failed to initialise");
/* Load user options */
error = nsoption_read([NS_PREFS_FILE cString], nsoptions);
if (error != NSERROR_OK) {
NSLog(@"Failed to load user preferences");
}
NSApplication *app = [NSApplication sharedApplication];
AppDelegate *delegate = [AppDelegate new];

View File

@ -1,6 +1,9 @@
#import <Foundation/Foundation.h>
#import "SearchProvider.h"
#define NS_PREFS_DIR ([@"~/.config/NetSurf/" stringByExpandingTildeInPath])
#define NS_PREFS_FILE ([@"~/.config/NetSurf/prefs" stringByExpandingTildeInPath])
typedef NS_ENUM(NSInteger, ViewLocation) {
ViewLocationWindow = 0,
ViewLocationTab,
@ -20,6 +23,13 @@ typedef NS_ENUM(NSInteger, UrlBarButtonType) {
UrlBarButtonTypeImage
};
typedef NS_ENUM(NSInteger, LoadImages) {
LoadImagesAll = 0,
LoadImagesForeground,
LoadImagesBackground,
LoadImagesNone
};
// Certain preferences will notify that they have been updated using this key.
#define PreferencesUpdatedNotificationName @"PreferencesUpdatedNotification"
typedef NS_ENUM(NSInteger, PreferenceType) {
@ -72,5 +82,8 @@ typedef NS_ENUM(NSInteger, PreferenceType) {
-(UrlBarButtonType)urlBarButtonType;
-(void)setUrlBarButtonType: (UrlBarButtonType)buttonType;
-(LoadImages)loadImages;
-(void)setLoadImages: (LoadImages)loadImages;
+(Preferences*)defaultPreferences;
@end

View File

@ -1,4 +1,6 @@
#import <Foundation/Foundation.h>
#import <stdbool.h>
#import "utils/nsoption.h"
#import "SearchProvider.h"
#import "Preferences.h"
@ -16,10 +18,11 @@
#define KEY_SHOW_URL_SUGGESTIONS @"show_url_suggestions"
#define KEY_URL_BAR_BUTTON_TYPE @"url_bar_button_type"
@interface Preferences (Private)
-(void)notifyPreferenceUpdated: (PreferenceType)type;
-(void)saveNetsurfPrefsFile;
@end
@implementation Preferences
@ -202,6 +205,43 @@
[self notifyPreferenceUpdated: PreferenceTypeUrlBarButtonType];
}
-(LoadImages)loadImages {
bool loadForeground = nsoption_bool(foreground_images);
bool loadBackground = nsoption_bool(background_images);
if (loadForeground && loadBackground) {
return LoadImagesAll;
} else if (loadForeground) {
return LoadImagesForeground;
} else if (loadBackground) {
return LoadImagesBackground;
} else {
NSLog(@"none");
return LoadImagesNone;
}
}
-(void)setLoadImages: (LoadImages)loadImages {
switch (loadImages) {
case LoadImagesAll:
nsoption_set_bool(foreground_images, true);
nsoption_set_bool(background_images, true);
break;
case LoadImagesForeground:
nsoption_set_bool(foreground_images, true);
nsoption_set_bool(background_images, false);
break;
case LoadImagesBackground:
nsoption_set_bool(foreground_images, false);
nsoption_set_bool(background_images, true);
break;
default:
nsoption_set_bool(foreground_images, false);
nsoption_set_bool(background_images, false);
break;
}
[self saveNetsurfPrefsFile];
}
+(Preferences*)defaultPreferences {
static Preferences *prefs;
if (prefs == nil) {
@ -218,4 +258,12 @@
[[NSNotificationCenter defaultCenter] postNotificationName:
PreferencesUpdatedNotificationName object: dict];
}
-(void)saveNetsurfPrefsFile {
[[NSFileManager defaultManager] createDirectoryAtPath: NS_PREFS_DIR
attributes: nil];
if (nsoption_write([NS_PREFS_FILE cString], NULL, NULL) != NSERROR_OK) {
NSLog(@"Failed to write prefs to file %@", NS_PREFS_FILE);
}
}
@end

View File

@ -60,6 +60,7 @@
-(void)awakeFromNib {
[self configureMainTab];
[self configureAppearanceTab];
[self configureContentTab];
}
// MARK: - MAIN TAB
@ -261,47 +262,51 @@
// MARK: - CONTENT TAB
-(void)configureContentTab {
LoadImages loadImages = [[Preferences defaultPreferences] loadImages];
[displayImagesButton selectItemAtIndex: (NSInteger)loadImages];
}
-(void)didChangeFontSizeStepper: (id)sender {
NSLog(@"didChangeFontSizeStepper");
}
-(void)didEnterFontSize: (id)sender {
NSLog(@"didEnterFontSize");
}
-(void)didPickDefaultFont: (id)sender {
NSLog(@"didPickDefualtFont");
}
-(void)didPickLoadImages: (id)sender {
NSLog(@"didPickLoadImages");
LoadImages loadImages = (LoadImages)[sender indexOfItem: [sender
selectedItem]];
[[Preferences defaultPreferences] setLoadImages: loadImages];
}
-(void)didPressEnableAnimations: (id)sender {
NSLog(@"didPressEnableAnimations");
}
-(void)didPressEnableJavascript: (id)sender {
NSLog(@"didPressEnableJavascript");
}
-(void)didPressHideAdverts: (id)sender {
NSLog(@"didPressHideAdverts");
}
-(void)didPressPreventPopups: (id)sender {
NSLog(@"didPressPreventPopups");
}
-(void)didPressPreviewFont: (id)sender {
NSLog(@"didPressPreviewFont");
}
-(void)didPickPreferredLanguage: (id)sender {
NSLog(@"didPickPreferredLanguage");
}
@end