]> Dogcows Code - chaz/thecheat/blob - ServerPrefs.m
update contact information and project URL
[chaz/thecheat] / ServerPrefs.m
1
2 /*
3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
5 *
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
7 *
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
10 */
11
12 #import "ServerPrefs.h"
13
14 #include "cheat_global.h"
15
16 #import "AppController.h"
17
18 #import "CheatServer.h"
19 #import "ServerChild.h"
20
21
22 @interface ServerPrefs ( PrivateAPI )
23
24 - (void)_serverStarted:(NSNotification *)note;
25 - (void)_serverStopped:(NSNotification *)note;
26 - (void)_childrenChanged:(NSNotification *)note;
27
28 @end
29
30
31 @implementation ServerPrefs
32
33
34 - (id)init
35 {
36 if ( self = [super init] ) {
37 NSNotificationCenter *nc= [NSNotificationCenter defaultCenter];
38
39 // register for server notifications
40 [nc addObserver:self selector:@selector(_serverStarted:) name:TCServerStartedNote object:nil];
41 [nc addObserver:self selector:@selector(_serverStopped:) name:TCServerStoppedNote object:nil];
42 [nc addObserver:self selector:@selector(_childrenChanged:) name:TCServerConnectionsChangedNote object:nil];
43 }
44 return self;
45 }
46
47 - (void)dealloc
48 {
49 [[NSNotificationCenter defaultCenter] removeObserver:self];
50 [super dealloc];
51 }
52
53
54 - (void)awakeFromNib
55 {
56 [ibDefaultPortText setStringValue:[NSString stringWithFormat:@"Default cheat port is %i.", TCDefaultListenPort]];
57
58 // set initial states
59 [ibNameField setStringValue:[[NSUserDefaults standardUserDefaults] objectForKey:TCBroadcastNamePref]];
60 [ibPortField setIntValue:[[NSUserDefaults standardUserDefaults] integerForKey:TCListenPortPref]];
61
62 if ( [[NSApp cheatServer] isListening] ) {
63 [self _serverStarted:nil];
64 }
65 else {
66 [self _serverStopped:nil];
67 }
68 }
69
70
71
72 - (IBAction)ibSetListenPort:(id)sender
73 {
74 short unsigned port = [ibPortField intValue];
75
76 if ( port < 1024 ) {
77 port = TCDefaultListenPort;
78 [sender setIntValue:port];
79 }
80
81 [[NSUserDefaults standardUserDefaults] setInteger:[ibPortField intValue] forKey:TCListenPortPref];
82 }
83
84 - (IBAction)ibSetBroadcast:(id)sender
85 {
86 [[NSUserDefaults standardUserDefaults] setObject:[ibNameField stringValue] forKey:TCBroadcastNamePref];
87 }
88
89 - (IBAction)ibStartServer:(id)sender
90 {
91 CheatServer *server = [NSApp cheatServer];
92
93 [self ibSetListenPort:nil];
94 [self ibSetBroadcast:nil];
95
96 if ( [server isListening] ) {
97 // stop it
98 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:TCRunServerPref];
99 [NSApp stopCheatServer];
100 }
101 else {
102 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:TCRunServerPref];
103 if ( ![NSApp startCheatServer] ) {
104 // cheat server failed to start
105 NSBeginAlertSheet( @"The Cheat could not start the server.", @"OK", nil, nil, [sender window], nil, NULL, NULL, NULL,
106 @"The cheat server failed to start. Make sure the port is not in use by another program and try again." );
107 }
108 }
109 }
110
111
112 - (int)numberOfRowsInTableView:(NSTableView *)table
113 {
114 CheatServer *server = [NSApp cheatServer];
115 if ( [server isListening] ) {
116 return [server childCount];
117 }
118 return 0;
119 }
120
121 - (id)tableView:(NSTableView *)table objectValueForTableColumn:(NSTableColumn *)column row:(int)row
122 {
123 NSString *identifier = [column identifier];
124 ServerChild *child;
125
126 child = [[[NSApp cheatServer] children] objectAtIndex:row];
127
128 return [child valueForKey:identifier];
129 }
130
131 - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)column row:(int)row
132 {
133
134 }
135
136 - (BOOL)tableView:(NSTableView *)aTableView shouldSelectTableColumn:(NSTableColumn *)aTableColumn
137 {
138 return NO;
139 }
140
141 - (void)tableView:(NSTableView *)aTableView deleteRows:(NSArray *)rows
142 {
143 int i, len;
144
145 len = [rows count];
146 for ( i = len-1; i >= 0; i-- ) {
147 [[NSApp cheatServer] removeChildAtIndex:[[rows objectAtIndex:i] unsignedIntValue]];
148 }
149 // reselect the last item if the selection is now invalid
150 len = [[NSApp cheatServer] childCount] - 1;
151 if ( [aTableView selectedRow] > len ) {
152 [aTableView selectRow:len byExtendingSelection:NO];
153 }
154 [aTableView reloadData];
155 }
156
157
158 - (void)_serverStarted:(NSNotification *)note
159 {
160 CheatServer *server = [NSApp cheatServer];
161 int port;
162 // server is running
163 port = [server port];
164 if ( port != TCDefaultListenPort ) {
165 [ibStatusField setDefaultStatus:[NSString stringWithFormat:@"cheat://%@:%i", [server host], port]];
166 }
167 else {
168 [ibStatusField setDefaultStatus:[NSString stringWithFormat:@"cheat://%@", [server host]]];
169 }
170 [ibNameField setEnabled:NO];
171 [ibPortField setEnabled:NO];
172 [ibStartButton setTitle:@"Stop Server"];
173 [ibSessionTable reloadData];
174
175 _tableTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(_childrenChanged:)
176 userInfo:nil repeats:YES] retain];
177 }
178
179 - (void)_serverStopped:(NSNotification *)note
180 {
181 // server is not running
182 [ibStatusField setDefaultStatus:@"Not Running"];
183 [ibNameField setEnabled:YES];
184 [ibPortField setEnabled:YES];
185 [ibStartButton setTitle:@"Start Server"];
186 [ibSessionTable reloadData];
187
188 [_tableTimer invalidate];
189 [_tableTimer release];
190 _tableTimer = nil;
191 }
192
193 - (void)_childrenChanged:(NSNotification *)note
194 {
195 [ibSessionTable reloadData];
196 }
197
198
199 @end
This page took 0.037583 seconds and 4 git commands to generate.