This repository has been archived by the owner on Feb 28, 2018. It is now read-only.
forked from klausthul/kibitz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppController.m
184 lines (158 loc) · 5.72 KB
/
AppController.m
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
/*
$Id$
Copyright 2006 Klaus Thul ([email protected])
This file is part of Kibitz.
Kibitz 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.
Kibitz 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 Kibitz; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import "AppController.h"
#import "SeekControl.h"
#import "PreferenceController.h"
#import "ChessServerListControl.h"
#import "Sound.h"
#import "Kibitz-Swift.h"
@implementation AppController
+ (void) initialize
{
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
// See http://www.freechess.org/Help/HelpFiles/iset.html
ChessServer *s = [[ChessServer alloc] init];
s.serverName = @"Free Internet Chess Server (FICS)";
s.serverAddress = @"freechess.org";
s.serverPort = @(23); // another popular option is 5000
s.initCommands = @" ";
s.useTimeseal = NO;
s.userName = @"guest";
s.userPassword = @"guest";
NSMutableArray *defaultChessServers = [NSMutableArray arrayWithObject: s];
NSMutableArray *defaultSeeks = [NSMutableArray arrayWithObjects: [[[Seek alloc] init] autorelease], nil];
NSDictionary *defaultSeeksAndServers = [NSDictionary dictionaryWithObjectsAndKeys: defaultChessServers, @"chessServers", defaultSeeks, @"seeks", nil, nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject: defaultSeeksAndServers];
defaultValues[@"seeksAndServers"] = data;
[defaultValues setValue: @1 forKey: @"soundDefault"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
gSounds = [[Sound alloc] init];
}
- (AppController *) init
{
if ((self = [super init]) != nil) {
serverConnections = [[NSMutableArray arrayWithCapacity: 20] retain];
seekControl = [[SeekControl alloc] initSeekControlWithAppController: self];
chessServerListControl = [[ChessServerListControl alloc] initChessServerListControlWithAppController: self];
NSData *d = nil;
NSDictionary *seeksAndServers = nil;
if ((d = [[NSUserDefaults standardUserDefaults] objectForKey: @"seeksAndServers"]) != nil)
if ((seeksAndServers = [NSKeyedUnarchiver unarchiveObjectWithData: d]) != nil) {
chessServers = [seeksAndServers[@"chessServers"] retain];
seeks = [seeksAndServers[@"seeks"] retain];
}
if (chessServers == nil)
chessServers = [[NSMutableArray arrayWithCapacity: 10] retain];
if (seeks == nil)
seeks = [[NSMutableArray arrayWithCapacity: 10] retain];
}
return self;
}
- (void) awakeFromNib
{
NSEnumerator *e = [chessServers objectEnumerator];
ChessServer *cs;
bool autoConnect = FALSE;
while ((cs = [e nextObject]) != nil)
if (cs.connectAtStartup) {
[self connectChessServer: cs];
autoConnect = TRUE;
}
if (!autoConnect)
[chessServerListControl showWindow: self];
}
- (void) dealloc
{
[chessServerListControl release];
[serverConnections release];
[preferenceController release];
[seekControl release];
[chessServers release];
[super dealloc];
}
- (void) showChessServerSelectorWindow
{
[chessServerListControl show: self];
}
- (IBAction) selectServer: (id) sender
{
[self showChessServerSelectorWindow];
}
- (IBAction) newSeek: (id) sender
{
[seekControl show: sender];
}
- (void) newSeekForServer: (ChessServerConnection *) csc
{
[self newSeek: self];
[seekControl setSelectedConnection: csc];
}
- (void) connectChessServer: (ChessServer *) cs
{
ChessServerConnection *csc = [[[ChessServerConnection alloc] initWithChessServer: cs appController: self] autorelease];
if (csc != nil) {
// [chessServerListControl close];
[self willChangeValueForKey: @"serverConnections"];
[serverConnections addObject: csc];
[self didChangeValueForKey: @"serverConnections"];
[seekControl setValue: csc forKey: @"selectedConnection"];
}
}
- (void) closeServerConnection: (ChessServerConnection *) csc
{
if (csc != nil) {
[self willChangeValueForKey: @"serverConnections"];
[serverConnections removeObject: csc];
[self didChangeValueForKey: @"serverConnections"];
if (serverConnections.count == 0)
[self showChessServerSelectorWindow];
}
}
- (NSArray *) serverConnections
{
return serverConnections;
}
- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) sender
{
if ((serverConnections.count == 0)
|| (NSRunAlertPanel(@"Confirm quit", @"You are currently connected to a chess server. Quit anyway?", @"Yes", @"No", nil)
== NSAlertDefaultReturn)) {
NSMutableDictionary *seeksAndServers = [NSMutableDictionary dictionaryWithCapacity: 2];
seeksAndServers[@"chessServers"] = chessServers;
seeksAndServers[@"seeks"] = seeks;
NSData *d = [NSKeyedArchiver archivedDataWithRootObject: seeksAndServers];
[[NSUserDefaults standardUserDefaults] setObject: d forKey: @"seeksAndServers"];
return NSTerminateNow;
} else
return NSTerminateCancel;
}
- (IBAction) showPreferenceController: (id) sender {
if (preferenceController == nil)
preferenceController = [[PreferenceController alloc] initPreferenceControllerWithAppController: self];
[preferenceController showWindow: self];
}
- (SeekControl *) seekControl
{
return seekControl;
}
- (void) closeSeekWindow
{
[seekControl.window close];
}
- (IBAction) switchAllSoundsOff: (id) sender
{
NSEnumerator *e = [serverConnections objectEnumerator];
ChessServerConnection *csc;
while ((csc = [e nextObject]) != nil)
[csc switchAllSoundsOff];
}
@end