3
*************************************************************************
5
ArmageTron -- Just another Tron Lightcycle Game in 3D.
6
Copyright (C) 2000 Manuel Moos (manuel@moosnet.de)
8
**************************************************************************
10
This program is free software; you can redistribute it and/or
11
modify it under the terms of the GNU General Public License
12
as published by the Free Software Foundation; either version 2
13
of the License, or (at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
***************************************************************************
30
#include "gServerFavorites.h"
37
#include "tConfiguration.h"
39
#include "gServerBrowser.h"
40
#include "nServerInfo.h"
42
#ifdef CONNECTION_STRESS
43
static bool sg_ConnectionStress = false;
48
enum { NUM_FAVORITES = 10 };
50
//! favorite server information, just to connect
51
class gServerInfoFavorite: public nServerInfoBase
54
// construct a server directly with connection name and port
55
gServerInfoFavorite( tString const & connectionName, unsigned int port )
57
nServerInfoBase::SetConnectionName( connectionName );
58
nServerInfoBase::SetPort( port );
62
static tString sg_ConfName( int ID, char const * name )
65
s << "BOOKMARK_" << ID+1 << name;
67
return tString( s.str().c_str() );
70
//! server favorite: holds connection info and configuration items
74
friend class gServerFavoritesHolder;
77
gServerFavorite( int ID )
79
, port_( sn_defaultPort )
81
, confName_( sg_ConfName( ID, "_NAME") ,name_ )
82
, confAddress_( sg_ConfName( ID, "_ADDRESS"), address_ )
83
, confPort( sg_ConfName( ID, "_PORT"), port_ )
87
//! connects to the server
90
gServerInfoFavorite fav( address_, port_ );
92
gLogo::SetDisplayed(false);
94
ConnectToServer( &fav );
97
//! returns the index in the favorite holder
103
tString name_; //!< the human readable name
104
tString address_; //!< connection address
105
int port_; //!< port to connect to
108
int index_; //!< index in favorite holder
110
tConfItemLine confName_; //!< configuration item holding the name
111
tConfItemLine confAddress_; //!< configuration item holding the address
112
tConfItem<int> confPort; //!< configuration item holding the port
115
//! server favorites management class: holds an array of servers
116
class gServerFavoritesHolder
119
gServerFavoritesHolder()
122
// generate favorites
123
for (int i = NUM_FAVORITES-1; i>=0; --i )
124
favorites[i] = new gServerFavorite( i );
127
~gServerFavoritesHolder()
130
for (int i = NUM_FAVORITES-1; i>=0; --i )
134
//! returns the favorite server info with the given index
135
gServerFavorite & GetFavorite( int index )
140
tASSERT( index >=0 && index < NUM_FAVORITES );
141
tASSERT( favorites[index] );
142
return *favorites[index];
146
gServerFavorite * favorites[NUM_FAVORITES];
148
// custom connect server
149
gServerFavorite custom;
153
static gServerFavoritesHolder sg_holder;
155
//! edit submenu item quitting the parent menu when it's done
156
class gMenuItemEditSubmenu: public uMenuItemSubmenu
159
gMenuItemEditSubmenu(uMenu *M,uMenu *s,
161
: uMenuItemSubmenu( M, s, help )
164
//! enters the submenu
168
uMenuItemSubmenu::Enter();
170
// exit the parent menu (so we don't have to update the edit menu)
175
//! connect to a favorite server
176
static void sg_ConnectFavorite( int ID )
178
sg_holder.GetFavorite(ID).Connect();
181
//! conglomerate of menus and entries for custom connect
182
class gCustomConnectEntries
185
void Generate( gServerFavorite & fav, uMenu * menu )
187
// prepare output reading "Edit <server name>"
188
// create menu items (autodeleted when the edit menu is killed)
189
tNEW(uMenuItemFunctionInt) ( menu,"$network_custjoin_connect_text" ,"$network_custjoin_connect_help" ,&sg_ConnectFavorite, fav.GetIndex() );
190
tNEW(uMenuItemInt) ( menu,"$network_custjoin_port_text","$network_custjoin_port_help", fav.port_, gServerBrowser::lowPort, gServerBrowser::highPort);
191
tNEW(uMenuItemString) ( menu,"$bookmarks_menu_address","$bookmarks_menu_address_help",fav.address_);
194
gCustomConnectEntries()
198
gCustomConnectEntries( gServerFavorite & fav, uMenu * menu )
200
Generate( fav, menu );
203
~gCustomConnectEntries()
210
//! conglomerate of menus and entries
211
class gServerFavoriteMenuEntries: public gCustomConnectEntries
214
gServerFavoriteMenuEntries( gServerFavorite & fav, uMenu & edit_menu )
216
// prepare output reading "Edit <server name>"
218
tString serverName = tColoredString::RemoveColors(fav.name_);
219
if ( serverName == "" || serverName == "Empty" )
222
s << "Server " << fav.GetIndex()+1;
224
serverName = s.str().c_str();
226
fe.SetTemplateParameter(1, serverName);
227
fe << "$bookmarks_menu_edit_slot";
230
edit_ = tNEW(uMenu) (fe);
231
editmenu_ = tNEW(gMenuItemEditSubmenu) ( &edit_menu, edit_, fe);
233
Generate( fav, edit_ );
235
tNEW(uMenuItemString) ( edit_,"$bookmarks_menu_name","$bookmarks_menu_name_help",fav.name_);
238
~gServerFavoriteMenuEntries()
240
delete editmenu_; editmenu_ = 0;
241
delete edit_; edit_ = 0;
246
uMenuItem * editmenu_;
249
static void sg_GenerateConnectionItems();
251
// Edit servers submenu funcion
252
static void sg_EditServers()
257
uMenu edit_menu("$bookmarks_menu_edit");
259
// create menu entries
260
gServerFavoriteMenuEntries * entries[ NUM_FAVORITES ];
261
for ( i = NUM_FAVORITES-1; i>=0; --i )
262
entries[i] = tNEW( gServerFavoriteMenuEntries )( sg_holder.GetFavorite(i), edit_menu );
267
// delete menu entries
268
for ( i = NUM_FAVORITES-1; i>=0; --i )
271
// regenerate parent menu
272
sg_GenerateConnectionItems();
275
// ugly hack: functions clearing and filling the connection menu
276
static uMenu * sg_connectionMenu = 0;
277
static uMenuItem * sg_connectionMenuItemKeep = 0;
278
static void sg_ClearConnectionItems()
280
tASSERT( sg_connectionMenu );
282
// delete old connection items
283
for ( int i = sg_connectionMenu->NumItems()-1; i>=0; --i )
285
uMenuItem * item = sg_connectionMenu->Item(i);
286
if ( item != sg_connectionMenuItemKeep )
290
static void sg_GenerateConnectionItems()
292
tASSERT( sg_connectionMenu );
294
// delete old connection items
295
sg_ClearConnectionItems();
297
// create new connection items
298
for ( int i = NUM_FAVORITES-1; i>=0; --i )
300
gServerFavorite & fav = sg_holder.GetFavorite(i);
302
if (fav.name_ != "" && fav.name_ != "Empty" && fav.address_ != "")
304
tOutput fc; // Connect to "favn_name"
305
fc.SetTemplateParameter(1,tColoredString::RemoveColors(fav.name_) );
306
fc << "$bookmarks_menu_connect";
308
tNEW(uMenuItemFunctionInt)(sg_connectionMenu ,fc ,"$network_custjoin_connect_help" ,&sg_ConnectFavorite, i );
313
//!TODO for 3.0 or 3.1: phase out this legacy support
314
static tString sg_customServerName("");
315
static tConfItemLine sg_serverName_ci("CUSTOM_SERVER_NAME",sg_customServerName);
316
static int sg_clientPort = 4534;
317
static tConfItem<int> sg_cport("CLIENT_PORT",sg_clientPort);
319
//! transfer old custom server name to favorite
320
static void sg_TransferCustomServer()
322
if ( sg_customServerName != "" )
324
// add custom connect server to favorites
325
gServerInfoFavorite server( sg_customServerName, sg_clientPort );
326
gServerFavorites::AddFavorite( &server );
328
// clear custom connect server
329
sg_customServerName = "";
333
// *********************************************************************************************
337
// *********************************************************************************************
340
// *********************************************************************************************
342
void gServerFavorites::FavoritesMenu( void )
344
sg_TransferCustomServer();
346
uMenu net_menu("$bookmarks_menu");
347
sg_connectionMenu = & net_menu;
349
uMenuItemFunction edit(&net_menu,"$bookmarks_menu_edit", "$bookmarks_menu_edit_help",&sg_EditServers);
350
sg_connectionMenuItemKeep = & edit;
352
sg_GenerateConnectionItems();
354
sg_ClearConnectionItems();
356
sg_connectionMenuItemKeep = NULL;
357
sg_connectionMenu = NULL;
360
// *********************************************************************************************
362
// * CustomConnectMenu
364
// *********************************************************************************************
367
// *********************************************************************************************
369
void gServerFavorites::CustomConnectMenu( void )
371
sg_TransferCustomServer();
373
uMenu net_menu("$network_custjoin_text");
374
sg_connectionMenu = & net_menu;
376
gServerFavorite & fav = sg_holder.GetFavorite(-1);
378
// create menu entries
379
gCustomConnectEntries submenu( fav, &net_menu );
384
// *********************************************************************************************
388
// *********************************************************************************************
390
//! @param server the server to add to the favorites
391
//! @return true if successful, false if favorite list is full
393
// *********************************************************************************************
395
bool gServerFavorites::AddFavorite( nServerInfoBase const * server )
400
for ( int i = NUM_FAVORITES-1; i>=0; --i )
402
gServerFavorite & fav = sg_holder.GetFavorite(i);
404
if (fav.name_ == "" || fav.name_ == "Empty")
406
fav.name_ = tColoredString::RemoveColors(server->GetName());
407
fav.address_ = server->GetConnectionName();
408
fav.port_ = server->GetPort();
417
// *********************************************************************************************
421
// *********************************************************************************************
423
//! @param server server to check whether it is bookmarked
424
//! @return true if the server is in the list of favorites
426
// *********************************************************************************************
428
bool gServerFavorites::IsFavorite( nServerInfoBase const * server )
433
for ( int i = NUM_FAVORITES-1; i>=0; --i )
435
gServerFavorite & fav = sg_holder.GetFavorite(i);
437
if (fav.name_ != "" && fav.name_ != "Empty" && fav.address_ == server->GetConnectionName() && fav.port_ == static_cast< int >( server->GetPort() ) )