1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
//
// Lynkeos
// $Id$
//
// Created by Jean-Etienne LAMIAUD on Wed May 16 2007.
// Copyright (c) 2007-2018. Jean-Etienne LAMIAUD
//
// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include "MyGeneralPrefs.h"
NSString * const K_PREF_ADJUST_FFT_SIZES = @"Adjust FFT sizes";
NSString * const K_PREF_IMAGEPROC_MULTIPROC = @"Multiprocessor image processing";
NSString * const K_PREF_END_PROCESS_SOUND = @"End of processing sound";
//! MyGeneralPrefs sigleton instances
static MyGeneralPrefs *myGeneralPrefsInstance = nil;
@interface MyGeneralPrefs(Private)
- (void) initPrefs ;
- (void) findSounds ;
- (void) readPrefs;
- (void) updatePanel;
@end
@implementation MyGeneralPrefs(Private)
- (void) initPrefs
{
// Set the factory defaults value
_adjustFFTSizes = YES;
_imageProcOptim = FFTW3ThreadsOptimization|ListThreadsOptimizations;
_sound = @"Glass";
if ( ![_soundsNames containsObject:_sound] )
_sound = @"";
}
- (void) findSounds
{
#if !GNUSTEP
NSArray *extensions = [NSSound soundUnfilteredTypes];
#else
NSArray *extensions = [NSSound soundUnfilteredFileTypes];
#endif
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *librarySearchPaths;
NSEnumerator *searchPathEnum;
NSURL *currPath;
NSMutableArray *bundleSearchPaths = [NSMutableArray array];
// Build the sounds paths list
librarySearchPaths = [fileMgr URLsForDirectory:NSLibraryDirectory
inDomains:NSAllDomainsMask];
searchPathEnum = [librarySearchPaths objectEnumerator];
while( (currPath = [searchPathEnum nextObject]) != nil )
[bundleSearchPaths addObject: [currPath URLByAppendingPathComponent:
@"Sounds"]];
[bundleSearchPaths addObject: [[NSBundle mainBundle] resourceURL]];
// Memorize each sound file in each directory
searchPathEnum = [bundleSearchPaths objectEnumerator];
while( (currPath = [searchPathEnum nextObject]) != nil )
{
NSDirectoryEnumerator *bundleEnum;
NSURL *currBundlePath;
bundleEnum = [fileMgr enumeratorAtURL:currPath
includingPropertiesForKeys:
[NSArray arrayWithObject:NSURLTypeIdentifierKey]
options:0
errorHandler:NULL];
if(bundleEnum)
{
while( (currBundlePath = [bundleEnum nextObject]) != nil )
{
#if !GNUSTEP
NSString *uti;
[currBundlePath getResourceValue:&uti
forKey:NSURLTypeIdentifierKey
error:NULL];
if( [extensions containsObject:uti] )
#else
if( [extensions containsObject:[currBundlePath pathExtension]] )
#endif
[_soundsNames addObject:[[currBundlePath lastPathComponent]
stringByDeletingPathExtension]];
}
}
}
// Sort the sounds names
[_soundsNames sortUsingSelector:@selector(caseInsensitiveCompare:)];
}
- (void) readPrefs
{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
if ( [user objectForKey:K_PREF_ADJUST_FFT_SIZES] != nil )
_adjustFFTSizes = [user boolForKey:K_PREF_ADJUST_FFT_SIZES];
if ( [user objectForKey:K_PREF_IMAGEPROC_MULTIPROC] != nil )
_imageProcOptim = (ParallelOptimization_t)[user integerForKey:K_PREF_IMAGEPROC_MULTIPROC];
NSString *sound = [user objectForKey:K_PREF_END_PROCESS_SOUND];
NSUInteger soundIdx = NSNotFound;
// Always get _sound from our array to avoid memory management issues
if ( sound != nil )
soundIdx = [_soundsNames indexOfObject:sound];
if ( soundIdx != NSNotFound )
_sound = [_soundsNames objectAtIndex:soundIdx];
else
_sound = @"";
}
- (void) updatePanel
{
[_adjustFFTSizesButton setState: (_adjustFFTSizes ? NSOnState : NSOffState)];
[_imageProcOptimPopup selectItemWithTag:_imageProcOptim];
if ( [_sound length] != 0 )
[_soundPopup selectItemWithTitle:_sound];
else
[_soundPopup selectItemAtIndex:0];
}
@end
@implementation MyGeneralPrefs
+ (void) getPreferenceTitle:(NSString**)title
icon:(NSImage**)icon
tip:(NSString**)tip
{
*title = @"General";
*icon = [NSImage imageNamed:@"Lynkeos"];
*tip = @"Lynkeos general preferences";
}
+ (id <LynkeosPreferences>) getPreferenceInstance
{
if ( myGeneralPrefsInstance == nil )
[[self alloc] init];
return( myGeneralPrefsInstance );
}
- (id) init
{
NSAssert( myGeneralPrefsInstance == nil,
@"More than one creation of MyGeneralPrefs" );
if ( (self = [super init]) != nil )
{
_soundsNames = [[NSMutableArray array] retain];
[self findSounds];
[self initPrefs];
myGeneralPrefsInstance = self;
}
return( self );
}
- (void) awakeFromNib
{
// Update with database value, if any
[self readPrefs];
// And rewrite them to ensure correct values
[self savePreferences:[NSUserDefaults standardUserDefaults]];
// Update the sound popup
NSEnumerator *list = [_soundsNames objectEnumerator];
NSString *label;
while ( (label = [list nextObject]) != nil )
[_soundPopup addItemWithTitle:label];
// Finally initialize the GUI
[self updatePanel];
}
- (NSView*) getPreferencesView
{
return( _prefsView );
}
- (void) revertPreferences
{
[self readPrefs];
[self updatePanel];
}
- (void) resetPreferences:(NSUserDefaults*)prefs
{
[self initPrefs];
[self savePreferences:prefs];
[self updatePanel];
}
- (void) savePreferences:(NSUserDefaults*)prefs
{
[prefs setBool:_adjustFFTSizes forKey:K_PREF_ADJUST_FFT_SIZES];
[prefs setObject:_sound forKey:K_PREF_END_PROCESS_SOUND];
[prefs setInteger:_imageProcOptim forKey:K_PREF_IMAGEPROC_MULTIPROC];
}
- (IBAction)changeAdjustFFTSizes:(id)sender
{
_adjustFFTSizes = ([sender state] == NSOnState);
}
- (IBAction)changeImageProcOptim:(id)sender
{
_imageProcOptim = (ParallelOptimization_t)[sender selectedTag];
}
- (IBAction)changeEndProcessingSound:(id)sender
{
if ( [sender indexOfSelectedItem] == 0 )
_sound = @"";
else
{
_sound = [sender titleOfSelectedItem];
NSSound *snd = [NSSound soundNamed:_sound];
NSAssert( snd != nil, @"Unknown sound in preferences" );
[snd play];
}
}
@end
|