]> Dogcows Code - chaz/thecheat/blob - ServerPrefs.m
The Cheat 1.2
[chaz/thecheat] / ServerPrefs.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
9 // any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20
21 #import "ServerPrefs.h"
22
23 #include "cheat_global.h"
24
25 #import "AppController.h"
26
27 #import "CheatServer.h"
28 #import "ServerChild.h"
29
30
31 @interface ServerPrefs ( PrivateAPI )
32
33 - (void)_serverStarted:(NSNotification *)note;
34 - (void)_serverStopped:(NSNotification *)note;
35 - (void)_childrenChanged:(NSNotification *)note;
36
37 @end
38
39
40 @implementation ServerPrefs
41
42
43 - (id)init
44 {
45 if ( self = [super init] ) {
46 NSNotificationCenter *nc= [NSNotificationCenter defaultCenter];
47
48 // register for server notifications
49 [nc addObserver:self selector:@selector(_serverStarted:) name:TCServerStartedNote object:nil];
50 [nc addObserver:self selector:@selector(_serverStopped:) name:TCServerStoppedNote object:nil];
51 [nc addObserver:self selector:@selector(_childrenChanged:) name:TCServerConnectionsChangedNote object:nil];
52 }
53 return self;
54 }
55
56 - (void)dealloc
57 {
58 [[NSNotificationCenter defaultCenter] removeObserver:self];
59 [super dealloc];
60 }
61
62
63 - (void)awakeFromNib
64 {
65 [ibDefaultPortText setStringValue:[NSString stringWithFormat:@"Default cheat port is %i.", TCDefaultListenPort]];
66
67 // set initial states
68 [ibNameField setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey:TCBroadcastNamePref]];
69 [ibPortField setIntValue:[[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref]];
70
71 if ( [[NSApp cheatServer] isListening] ) {
72 [self _serverStarted:nil];
73 }
74 else {
75 [self _serverStopped:nil];
76 }
77 }
78
79
80
81 - (IBAction)ibSetListenPort:(id)sender
82 {
83 short unsigned port = [ibPortField intValue];
84
85 if ( port < 1024 ) {
86 port = TCDefaultListenPort;
87 [sender setIntValue:port];
88 }
89
90 [[NSUserDefaults standardUserDefaults] setInteger:[ibPortField intValue] forKey:TCListenPortPref];
91 }
92
93 - (IBAction)ibSetBroadcast:(id)sender
94 {
95 [[NSUserDefaults standardUserDefaults] setObject:[ibNameField stringValue] forKey:TCBroadcastNamePref];
96 }
97
98 - (IBAction)ibStartServer:(id)sender
99 {
100 CheatServer *server = [NSApp cheatServer];
101
102 [self ibSetListenPort:nil];
103 [self ibSetBroadcast:nil];
104
105 if ( [server isListening] ) {
106 // stop it
107 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:TCRunServerPref];
108 [NSApp stopCheatServer];
109 }
110 else {
111 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:TCRunServerPref];
112 if ( ![NSApp startCheatServer] ) {
113 // cheat server failed to start
114 NSBeginAlertSheet( @"The Cheat could not start the server.", @"OK", nil, nil, [sender window], nil, NULL, NULL, NULL,
115 @"The cheat server failed to start. Make sure the port is not in use by another program and try again." );
116 }
117 }
118 }
119
120
121 - (int)numberOfRowsInTableView:(NSTableView *)table
122 {
123 CheatServer *server = [NSApp cheatServer];
124 if ( [server isListening] ) {
125 return [server childCount];
126 }
127 return 0;
128 }
129
130 - (id)tableView:(NSTableView *)table objectValueForTableColumn:(NSTableColumn *)column row:(int)row
131 {
132 NSString *identifier = [column identifier];
133 ServerChild *child;
134
135 child = [[[NSApp cheatServer] children] objectAtIndex:row];
136
137 return [child valueForKey:identifier];
138 }
139
140 - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)column row:(int)row
141 {
142
143 }
144
145 - (BOOL)tableView:(NSTableView *)aTableView shouldSelectTableColumn:(NSTableColumn *)aTableColumn
146 {
147 return NO;
148 }
149
150 - (void)tableView:(NSTableView *)aTableView deleteRows:(NSArray *)rows
151 {
152 int i, len;
153
154 len = [rows count];
155 for ( i = len-1; i >= 0; i-- ) {
156 [[NSApp cheatServer] removeChildAtIndex:[[rows objectAtIndex:i] unsignedIntValue]];
157 }
158 // reselect the last item if the selection is now invalid
159 len = [[NSApp cheatServer] childCount] - 1;
160 if ( [aTableView selectedRow] > len ) {
161 [aTableView selectRow:len byExtendingSelection:NO];
162 }
163 [aTableView reloadData];
164 }
165
166
167 - (void)_serverStarted:(NSNotification *)note
168 {
169 CheatServer *server = [NSApp cheatServer];
170 int port;
171 // server is running
172 port = [server port];
173 if ( port != TCDefaultListenPort ) {
174 [ibStatusField setDefaultStatus:[NSString stringWithFormat:@"cheat://%@:%i", [server host], port]];
175 }
176 else {
177 [ibStatusField setDefaultStatus:[NSString stringWithFormat:@"cheat://%@", [server host]]];
178 }
179 [ibNameField setEnabled:NO];
180 [ibPortField setEnabled:NO];
181 [ibStartButton setTitle:@"Stop Server"];
182 [ibSessionTable reloadData];
183
184 _tableTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(_childrenChanged:)
185 userInfo:nil repeats:YES] retain];
186 }
187
188 - (void)_serverStopped:(NSNotification *)note
189 {
190 // server is not running
191 [ibStatusField setDefaultStatus:@"Not Running"];
192 [ibNameField setEnabled:YES];
193 [ibPortField setEnabled:YES];
194 [ibStartButton setTitle:@"Start Server"];
195 [ibSessionTable reloadData];
196
197 [_tableTimer invalidate];
198 [_tableTimer release];
199 _tableTimer = nil;
200 }
201
202 - (void)_childrenChanged:(NSNotification *)note
203 {
204 [ibSessionTable reloadData];
205 }
206
207
208 @end
This page took 0.051224 seconds and 4 git commands to generate.