1/*
2   Project: MPDCon
3
4   Copyright (C) 2004
5
6   Author: Daniel Luederwald
7
8   Created: 2004-05-12 17:59:14 +0200 by flip
9
10   Preferences Controller
11
12   This application is free software; you can redistribute it and/or
13   modify it under the terms of the GNU General Public
14   License as published by the Free Software Foundation; either
15   version 2 of the License, or (at your option) any later version.
16
17   This application is distributed in the hope that it will be useful,
18   but WITHOUT ANY WARRANTY; without even the implied warranty of
19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20   Library General Public License for more details.
21
22   You should have received a copy of the GNU General Public
23   License along with this library; if not, write to the Free
24   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25*/
26
27#include <AppKit/AppKit.h>
28#include "PreferencesController.h"
29
30@implementation PreferencesController
31
32/* --------------------------
33   - Initialization Methods -
34   --------------------------*/
35
36+ (id) sharedPreferencesController
37{
38  static PreferencesController *_sharedPrefsController = nil;
39
40  if (! _sharedPrefsController)
41    {
42      _sharedPrefsController = [[PreferencesController
43				  allocWithZone: [self zone]] init];
44    }
45
46  return _sharedPrefsController;
47}
48
49- (id) init
50{
51  self = [self initWithWindowNibName: @"Preferences"];
52
53  if (self)
54    {
55      [self setWindowFrameAutosaveName: @"Preferences"];
56      defaults = [NSUserDefaults standardUserDefaults];
57    }
58
59  return self;
60}
61
62/* ---------------
63   - Gui Methods -
64   ---------------*/
65
66- (void) awakeFromNib
67{
68  [self revert: nil];
69}
70
71- (void) apply: (id)sender
72{
73  NSNotification *aNotif;
74
75  if ([mpdHost stringValue] != nil) {
76    [defaults setObject: [mpdHost stringValue] forKey: @"mpdHost"];
77  }
78
79  if ([mpdPort stringValue] != nil) {
80    [defaults setObject: [mpdPort stringValue] forKey: @"mpdPort"];
81  }
82
83  if ([mpdTimeout stringValue] != nil) {
84    [defaults setObject: [mpdTimeout stringValue] forKey: @"mpdTimeout"];
85  }
86
87  if (([password stringValue] != nil) && ([usePassword state] != 0))
88    {
89      [defaults setObject: [password stringValue] forKey: @"mpdPassword"];
90    }
91  else
92    {
93      [defaults removeObjectForKey: @"mpdPassword"];
94    }
95
96  [defaults setInteger: [usePassword state] forKey: @"usePassword"];
97
98  [defaults setInteger: [scrollSwitch state] forKey: @"enableScroll"];
99
100  [defaults setObject: [NSArchiver archivedDataWithRootObject: [colorWell color]] forKey: @"displayColor"];
101
102  [defaults synchronize];
103
104  [[self window] close];
105
106  aNotif = [NSNotification notificationWithName:
107			     PreferencesChangedNotification
108			                 object: nil];
109
110  [[NSNotificationCenter defaultCenter] postNotification: aNotif];
111}
112
113
114- (void) revert: (id)sender
115{
116  NSData *colorData;
117
118  [mpdHost setStringValue: [defaults objectForKey: @"mpdHost"]?[defaults objectForKey: @"mpdHost"]:@"localhost"];
119  [mpdPort setStringValue: [defaults objectForKey: @"mpdPort"]?[defaults objectForKey: @"mpdPort"]:@"6600"];
120  [mpdTimeout setStringValue: [defaults objectForKey: @"mpdTimeout"]?[defaults objectForKey: @"mpdTimeout"]:@"1500"];
121  if ([defaults objectForKey: @"mpdPassword"] != nil)
122  {
123    [password setStringValue: [defaults objectForKey: @"mpdPassword"]];
124  } else {
125    [defaults removeObjectForKey: @"mpdPassword"];
126  }
127  [scrollSwitch setState: [defaults integerForKey: @"enableScroll"]];
128  [usePassword setState: [defaults integerForKey: @"usePassword"]];
129  colorData = [defaults dataForKey: @"displayColor"];
130
131  if (colorData != nil)
132    {
133      [colorWell setColor: [NSUnarchiver unarchiveObjectWithData: colorData]];
134    }
135
136  [self usePasswordChanged: sender];
137}
138
139- (void) usePasswordChanged: (id)sender
140{
141  if ([usePassword state] != 0)
142    {
143      [password setEditable: YES];
144      [password setEnabled: YES];
145      [password setBackgroundColor: [NSColor whiteColor]];
146      [password setSelectable: YES];
147    }
148  else
149    {
150      [password setEditable: NO];
151      [password setEnabled: NO];
152      [password setBackgroundColor: [NSColor lightGrayColor]];
153      [password setSelectable: NO];
154    }
155}
156@end
157
158
159