| 1 |
/* EsoundPreference.m - this file is part of Cynthiune |
| 2 |
* |
| 3 |
* Copyright (C) 2004 Wolfgang Sourdeau |
| 4 |
* |
| 5 |
* Author: Wolfgang Sourdeau <[email protected]> |
| 6 |
* |
| 7 |
* This file is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2, or (at your option) |
| 10 |
* any later version. |
| 11 |
* |
| 12 |
* This file is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; see the file COPYING. If not, write to |
| 19 |
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#import <AppKit/NSBox.h> |
| 24 |
#import <AppKit/NSButton.h> |
| 25 |
#import <AppKit/NSNibLoading.h> |
| 26 |
#import <AppKit/NSTextField.h> |
| 27 |
#import <AppKit/NSWindow.h> |
| 28 |
#import <Foundation/NSBundle.h> |
| 29 |
#import <Foundation/NSNotification.h> |
| 30 |
#import <Foundation/NSUserDefaults.h> |
| 31 |
#import <Foundation/NSDictionary.h> |
| 32 |
|
| 33 |
#import <Cynthiune/CynthiuneBundle.h> |
| 34 |
#import <Cynthiune/Output.h> |
| 35 |
#import <Cynthiune/Preference.h> |
| 36 |
#import <Cynthiune/utils.h> |
| 37 |
|
| 38 |
#import "Esound.h" |
| 39 |
#import "EsoundPreference.h" |
| 40 |
|
| 41 |
#define LOCALIZED(X) _b ([Esound class], X) |
| 42 |
|
| 43 |
@implementation EsoundPreference : NSObject |
| 44 |
|
| 45 |
// Preference protocol |
| 46 |
+ (id) instance |
| 47 |
{ |
| 48 |
static EsoundPreference *singleton = nil; |
| 49 |
|
| 50 |
if (!singleton) |
| 51 |
singleton = [[self alloc] _init]; |
| 52 |
|
| 53 |
return singleton; |
| 54 |
} |
| 55 |
|
| 56 |
- (EsoundPreference *) _init |
| 57 |
{ |
| 58 |
NSDictionary *tmpDict; |
| 59 |
|
| 60 |
if ((self = [super init])) |
| 61 |
{ |
| 62 |
tmpDict = [[NSUserDefaults standardUserDefaults] |
| 63 |
dictionaryForKey: @"Esound"]; |
| 64 |
preference = [NSMutableDictionary dictionaryWithDictionary: tmpDict]; |
| 65 |
[preference retain]; |
| 66 |
} |
| 67 |
|
| 68 |
return self; |
| 69 |
} |
| 70 |
|
| 71 |
- (NSView *) preferenceSheet |
| 72 |
{ |
| 73 |
NSView *aView; |
| 74 |
|
| 75 |
[NSBundle loadNibNamed: @"EsoundPreferences" |
| 76 |
owner: self]; |
| 77 |
aView = [prefsWindow contentView]; |
| 78 |
[aView retain]; |
| 79 |
[aView removeFromSuperview]; |
| 80 |
[prefsWindow release]; |
| 81 |
[aView autorelease]; |
| 82 |
|
| 83 |
return aView; |
| 84 |
} |
| 85 |
|
| 86 |
- (void) _initDefaults |
| 87 |
{ |
| 88 |
NSString *socketType, *tcpHost, *tcpPort; |
| 89 |
static BOOL initted = NO; |
| 90 |
|
| 91 |
if (!initted) |
| 92 |
{ |
| 93 |
socketType = [preference objectForKey: @"socketType"]; |
| 94 |
if (!socketType) |
| 95 |
{ |
| 96 |
socketType = @"UNIX"; |
| 97 |
[preference setObject: socketType |
| 98 |
forKey: @"socketType"]; |
| 99 |
} |
| 100 |
|
| 101 |
tcpHost = [preference objectForKey: @"tcpHostname"]; |
| 102 |
if (!tcpHost) |
| 103 |
{ |
| 104 |
tcpHost = @"localhost"; |
| 105 |
[preference setObject: tcpHost |
| 106 |
forKey: @"tcpHostname"]; |
| 107 |
} |
| 108 |
tcpPort = [preference objectForKey: @"tcpPort"]; |
| 109 |
if (!tcpPort) |
| 110 |
{ |
| 111 |
tcpPort = @"16001"; |
| 112 |
[preference setObject: tcpPort |
| 113 |
forKey: @"tcpPort"]; |
| 114 |
} |
| 115 |
initted = YES; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
- (void) awakeFromNib |
| 120 |
{ |
| 121 |
[self _initDefaults]; |
| 122 |
|
| 123 |
[connectionTypeBox setTitle: LOCALIZED (@"Connection type")]; |
| 124 |
[tcpOptionsBox setTitle: LOCALIZED (@"TCP options")]; |
| 125 |
[unixBtn setTitle: LOCALIZED (@"UNIX socket")]; |
| 126 |
[tcpBtn setTitle: LOCALIZED (@"TCP socket")]; |
| 127 |
[hostLabel setStringValue: LOCALIZED (@"Hostname (or IP)")]; |
| 128 |
[portLabel setStringValue: LOCALIZED (@"Port")]; |
| 129 |
|
| 130 |
if ([[preference objectForKey: @"socketType"] isEqualToString: @"UNIX"]) |
| 131 |
[self selectUnixBtn: self]; |
| 132 |
else |
| 133 |
[self selectTcpBtn: self]; |
| 134 |
|
| 135 |
[hostField setStringValue: [preference objectForKey: @"tcpHostname"]]; |
| 136 |
[portField setStringValue: [preference objectForKey: @"tcpPort"]]; |
| 137 |
} |
| 138 |
|
| 139 |
- (NSString *) preferenceTitle |
| 140 |
{ |
| 141 |
return @"Esound"; |
| 142 |
} |
| 143 |
|
| 144 |
- (void) save |
| 145 |
{ |
| 146 |
NSUserDefaults *defaults; |
| 147 |
|
| 148 |
defaults = [NSUserDefaults standardUserDefaults]; |
| 149 |
[defaults setObject: preference |
| 150 |
forKey: @"Esound"]; |
| 151 |
[defaults synchronize]; |
| 152 |
} |
| 153 |
|
| 154 |
- (BOOL) socketIsTCP |
| 155 |
{ |
| 156 |
NSString *socketType; |
| 157 |
|
| 158 |
[self _initDefaults]; |
| 159 |
|
| 160 |
socketType = [preference objectForKey: @"socketType"]; |
| 161 |
return [socketType isEqualToString: @"TCP"]; |
| 162 |
} |
| 163 |
|
| 164 |
- (NSString *) tcpHostConnectString |
| 165 |
{ |
| 166 |
[self _initDefaults]; |
| 167 |
|
| 168 |
return [NSString stringWithFormat: @"%@:%@", |
| 169 |
[preference objectForKey: @"tcpHostname"], |
| 170 |
[preference objectForKey: @"tcpPort"]]; |
| 171 |
} |
| 172 |
|
| 173 |
- (void) selectUnixBtn: (id) sender |
| 174 |
{ |
| 175 |
NSColor *disabledColor; |
| 176 |
|
| 177 |
disabledColor = [NSColor controlBackgroundColor]; |
| 178 |
[unixBtn setState: 1]; |
| 179 |
[tcpBtn setState: 0]; |
| 180 |
[hostField setEditable: NO]; |
| 181 |
[portField setEditable: NO]; |
| 182 |
[hostField setBackgroundColor: disabledColor]; |
| 183 |
[portField setBackgroundColor: disabledColor]; |
| 184 |
[portField display]; |
| 185 |
[hostField display]; |
| 186 |
|
| 187 |
[preference setObject: @"UNIX" forKey: @"socketType"]; |
| 188 |
} |
| 189 |
|
| 190 |
- (void) selectTcpBtn: (id) sender |
| 191 |
{ |
| 192 |
NSColor *enabledColor; |
| 193 |
|
| 194 |
enabledColor = [NSColor textBackgroundColor]; |
| 195 |
[unixBtn setState: 0]; |
| 196 |
[tcpBtn setState: 1]; |
| 197 |
[hostField setEditable: YES]; |
| 198 |
[portField setEditable: YES]; |
| 199 |
[hostField setBackgroundColor: enabledColor]; |
| 200 |
[portField setBackgroundColor: enabledColor]; |
| 201 |
[portField display]; |
| 202 |
[hostField display]; |
| 203 |
|
| 204 |
[preference setObject: @"TCP" |
| 205 |
forKey: @"socketType"]; |
| 206 |
} |
| 207 |
|
| 208 |
- (void) controlTextDidEndEditing: (NSNotification *) notification |
| 209 |
{ |
| 210 |
id object; |
| 211 |
NSString *prefKey; |
| 212 |
|
| 213 |
object = [notification object]; |
| 214 |
if (object == hostField) |
| 215 |
prefKey = @"tcpHostname"; |
| 216 |
else if (object == portField) |
| 217 |
prefKey = @"tcpPort"; |
| 218 |
else |
| 219 |
prefKey = @""; |
| 220 |
|
| 221 |
[preference setObject: [object stringValue] |
| 222 |
forKey: prefKey]; |
| 223 |
} |
| 224 |
|
| 225 |
@end |