2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
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)
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.
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.
21 #import "ServerPrefs.h"
23 #include "cheat_global.h"
25 #import "AppController.h"
27 #import "CheatServer.h"
28 #import "ServerChild.h"
31 @interface ServerPrefs ( PrivateAPI
)
33 - (void)_serverStarted
:(NSNotification
*)note
;
34 - (void)_serverStopped
:(NSNotification
*)note
;
35 - (void)_childrenChanged
:(NSNotification
*)note
;
40 @implementation ServerPrefs
45 if ( self = [super init
] ) {
46 NSNotificationCenter
*nc
= [NSNotificationCenter defaultCenter
];
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];
58 [[NSNotificationCenter defaultCenter
] removeObserver
:self];
65 [ibDefaultPortText setStringValue
:[NSString stringWithFormat
:@
"Default cheat port is %i.", TCDefaultListenPort
]];
68 [ibNameField setStringValue
:[[NSUserDefaults standardUserDefaults
] objectForKey
:TCBroadcastNamePref
]];
69 [ibPortField setIntValue
:[[NSUserDefaults standardUserDefaults
] integerForKey
:TCListenPortPref
]];
71 if ( [[NSApp cheatServer
] isListening
] ) {
72 [self _serverStarted
:nil];
75 [self _serverStopped
:nil];
81 - (IBAction
)ibSetListenPort
:(id)sender
83 short unsigned port
= [ibPortField intValue
];
86 port
= TCDefaultListenPort
;
87 [sender setIntValue
:port
];
90 [[NSUserDefaults standardUserDefaults
] setInteger
:[ibPortField intValue
] forKey
:TCListenPortPref
];
93 - (IBAction
)ibSetBroadcast
:(id)sender
95 [[NSUserDefaults standardUserDefaults
] setObject
:[ibNameField stringValue
] forKey
:TCBroadcastNamePref
];
98 - (IBAction
)ibStartServer
:(id)sender
100 CheatServer
*server
= [NSApp cheatServer
];
102 [self ibSetListenPort
:nil];
103 [self ibSetBroadcast
:nil];
105 if ( [server isListening
] ) {
107 [[NSUserDefaults standardUserDefaults
] setBool
:NO forKey
:TCRunServerPref
];
108 [NSApp stopCheatServer
];
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." );
121 - (int)numberOfRowsInTableView
:(NSTableView
*)table
123 CheatServer
*server
= [NSApp cheatServer
];
124 if ( [server isListening
] ) {
125 return [server childCount
];
130 - (id)tableView
:(NSTableView
*)table objectValueForTableColumn
:(NSTableColumn
*)column row
:(int)row
132 NSString
*identifier
= [column identifier
];
135 child
= [[[NSApp cheatServer
] children
] objectAtIndex
:row
];
137 return [child valueForKey
:identifier
];
140 - (void)tableView
:(NSTableView
*)aTableView setObjectValue
:(id)object forTableColumn
:(NSTableColumn
*)column row
:(int)row
145 - (BOOL)tableView
:(NSTableView
*)aTableView shouldSelectTableColumn
:(NSTableColumn
*)aTableColumn
150 - (void)tableView
:(NSTableView
*)aTableView deleteRows
:(NSArray
*)rows
155 for ( i
= len
-1; i
>= 0; i
-- ) {
156 [[NSApp cheatServer
] removeChildAtIndex
:[[rows objectAtIndex
:i
] unsignedIntValue
]];
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
];
163 [aTableView reloadData
];
167 - (void)_serverStarted
:(NSNotification
*)note
169 CheatServer
*server
= [NSApp cheatServer
];
172 port
= [server port
];
173 if ( port
!= TCDefaultListenPort
) {
174 [ibStatusField setDefaultStatus
:[NSString stringWithFormat
:@
"cheat://%@:%i", [server host
], port
]];
177 [ibStatusField setDefaultStatus
:[NSString stringWithFormat
:@
"cheat://%@", [server host
]]];
179 [ibNameField setEnabled
:NO
];
180 [ibPortField setEnabled
:NO
];
181 [ibStartButton setTitle
:@
"Stop Server"];
182 [ibSessionTable reloadData
];
184 _tableTimer
= [[NSTimer scheduledTimerWithTimeInterval
:1.0 target
:self selector
:@selector(_childrenChanged
:)
185 userInfo
:nil repeats
:YES
] retain
];
188 - (void)_serverStopped
:(NSNotification
*)note
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
];
197 [_tableTimer invalidate
];
198 [_tableTimer release
];
202 - (void)_childrenChanged
:(NSNotification
*)note
204 [ibSessionTable reloadData
];