summaryrefslogtreecommitdiffstats
path: root/atlantik/client/atlantik.cpp
blob: f1e24645f38caaaebeed9d60a1cebee25f13e0a2 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
// Copyright (c) 2002-2004 Rob Kaper <cap@capsi.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation.
//
// This program 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 this program; see the file COPYING.  If not, write to
// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.

#include <errno.h>

#include <tqcolor.h>
#include <tqdatetime.h>
#include <tqlineedit.h>
#include <tqscrollview.h>
#include <tqpopupmenu.h>

#include <kaboutapplication.h>
#include <kaction.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kconfig.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <knotifyclient.h>
#include <knotifydialog.h>
#include <kstatusbar.h>
#include <kstdgameaction.h>
#include <kstdaction.h>
#include <ktoolbar.h>

#include <kdeversion.h>
#undef KDE_3_2_FEATURES
#if defined(KDE_MAKE_VERSION)
#if KDE_VERSION >= KDE_MAKE_VERSION(3,2,0)
	#define KDE_3_2_FEATURES
#endif
#endif

#include <kdebug.h>

#include <atlantic_core.h>
#include <auction.h>
#include <estate.h>
#include <player.h>
#include <trade.h>
#include "atlantik.moc"

#include <atlantik_network.h>

#include <board.h>
#include <trade_widget.h>

#include "eventlogwidget.h"
#include "main.h"
#include "selectserver_widget.h"
#include "selectgame_widget.h"
#include "selectconfiguration_widget.h"

LogTextEdit::LogTextEdit( TQWidget *parent, const char *name ) : TQTextEdit( parent, name )
{
#ifdef KDE_3_2_FEATURES
	m_clear = KStdAction::clear( this, TQT_SLOT( clear() ), 0 );
#else
	m_clear = new KAction( i18n("Clear"), "clear", NULL, this, TQT_SLOT( clear() ), static_cast<KActionCollection *>(0), "clear" );
#endif
	m_selectAll = KStdAction::selectAll( this, TQT_SLOT( selectAll() ), 0 );
	m_copy = KStdAction::copy( this, TQT_SLOT( copy() ), 0 );
}

LogTextEdit::~LogTextEdit()
{
	delete m_clear;
	delete m_selectAll;
	delete m_copy;
}

TQPopupMenu *LogTextEdit::createPopupMenu( const TQPoint & )
{
	TQPopupMenu *rmbMenu = new TQPopupMenu( this );
	m_clear->plug( rmbMenu );
	rmbMenu->insertSeparator();
	m_copy->setEnabled( hasSelectedText() );
	m_copy->plug( rmbMenu );
	m_selectAll->plug( rmbMenu );

	return rmbMenu;
}

Atlantik::Atlantik ()
 :	KMainWindow (),
 	m_runningGame( false )
{
	// Read application configuration
	readConfig();

	// Toolbar: Game
//	KStdGameAction::gameNew(this, TQT_SLOT(slotNewGame()), actionCollection(), "game_new");
        m_showEventLog = new KAction(i18n("Show Event &Log")/*, "atlantik_showeventlog"*/, CTRL+Key_L, this, TQT_SLOT(showEventLog()), actionCollection(), "showeventlog");
	KStdGameAction::quit(kapp, TQT_SLOT(closeAllWindows()), actionCollection(), "game_quit");

	// Toolbar: Settings
	KStdAction::preferences(this, TQT_SLOT(slotConfigure()), actionCollection());
	KStdAction::configureNotifications(this, TQT_SLOT(configureNotifications()), actionCollection());

	// Initialize pointers to 0L
	m_configDialog = 0;
	m_board = 0;
	m_eventLogWidget = 0;
	m_selectServer = 0;
	m_selectGame = 0;
	m_selectConfiguration = 0;
	m_atlantikNetwork = 0;

	// Game and network core
	m_atlanticCore = new AtlanticCore(this, "atlanticCore");
	connect(m_atlanticCore, TQT_SIGNAL(createGUI(Player *)), this, TQT_SLOT(newPlayer(Player *)));
	connect(m_atlanticCore, TQT_SIGNAL(removeGUI(Player *)), this, TQT_SLOT(removeGUI(Player *)));
	connect(m_atlanticCore, TQT_SIGNAL(createGUI(Trade *)), this, TQT_SLOT(newTrade(Trade *)));
	connect(m_atlanticCore, TQT_SIGNAL(removeGUI(Trade *)), this, TQT_SLOT(removeGUI(Trade *)));

	initEventLog();
	initNetworkObject();

	// Menu,toolbar: Move
	m_roll = KStdGameAction::roll(this, TQT_SIGNAL(rollDice()), actionCollection());
	m_roll->setEnabled(false);
	m_buyEstate = new KAction(i18n("&Buy"), "atlantik_buy_estate", CTRL+Key_B, this, TQT_SIGNAL(buyEstate()), actionCollection(), "buy_estate");
	m_buyEstate->setEnabled(false);
	m_auctionEstate = new KAction(i18n("&Auction"), "auction", CTRL+Key_A, this, TQT_SIGNAL(auctionEstate()), actionCollection(), "auction");
	m_auctionEstate->setEnabled(false);
	m_endTurn = KStdGameAction::endTurn(this, TQT_SIGNAL(endTurn()), actionCollection());
	m_endTurn->setEnabled(false);
        m_jailCard = new KAction(i18n("Use Card to Leave Jail")/*, "atlantik_move_jail_card"*/, 0, this, TQT_SIGNAL(jailCard()), actionCollection(), "move_jailcard");
	m_jailCard->setEnabled(false);
	m_jailPay = new KAction(i18n("&Pay to Leave Jail"), "jail_pay", CTRL+Key_P, this, TQT_SIGNAL(jailPay()), actionCollection(), "move_jailpay");
	m_jailPay->setEnabled(false);
        m_jailRoll = new KAction(i18n("Roll to Leave &Jail")/*, "atlantik_move_jail_roll"*/, CTRL+Key_J, this, TQT_SIGNAL(jailRoll()), actionCollection(), "move_jailroll");
	m_jailRoll->setEnabled(false);

	// Mix code and XML into GUI
	KMainWindow::createGUI();
	applyMainWindowSettings( KGlobal::config(), "AtlantikMainWindow" );
	KMainWindow::statusBar()->insertItem("Atlantik " ATLANTIK_VERSION_STRING, 0);
	KMainWindow::statusBar()->insertItem(TQString::null, 1);
	connect(statusBar(), TQT_SIGNAL(released(int)), this, TQT_SLOT(statusBarClick(int)));

	// Main widget, containing all others
 	m_mainWidget = new TQWidget(this, "main");
	m_mainWidget->show();
	m_mainLayout = new TQGridLayout(m_mainWidget, 3, 2);
	setCentralWidget(m_mainWidget);

	// Vertical view area for portfolios.
	m_portfolioScroll = new TQScrollView(m_mainWidget, "pfScroll");
	m_mainLayout->addWidget( m_portfolioScroll, 0, 0 );
	m_portfolioScroll->setHScrollBarMode( TQScrollView::AlwaysOff );
	m_portfolioScroll->setResizePolicy( TQScrollView::AutoOneFit );
	m_portfolioScroll->setFixedHeight( 200 );
	m_portfolioScroll->hide();

	m_portfolioWidget = new TQWidget( m_portfolioScroll->viewport(), "pfWidget" );
	m_portfolioScroll->addChild( m_portfolioWidget );
	m_portfolioWidget->show();

	m_portfolioLayout = new TQVBoxLayout(m_portfolioWidget);
	m_portfolioViews.setAutoDelete(true);

	// Nice label
//	m_portfolioLabel = new TQLabel(i18n("Players"), m_portfolioWidget, "pfLabel");
//	m_portfolioLayout->addWidget(m_portfolioLabel);
//	m_portfolioLabel->show();

	// Text view for chat and status messages from server.
	m_serverMsgs = new LogTextEdit(m_mainWidget, "serverMsgs");
	m_serverMsgs->setTextFormat(TQTextEdit::PlainText);
	m_serverMsgs->setReadOnly(true);
	m_serverMsgs->setHScrollBarMode(TQScrollView::AlwaysOff);
	m_serverMsgs->setMinimumWidth(200);
	m_mainLayout->addWidget(m_serverMsgs, 1, 0);

	// LineEdit to enter commands and chat messages.
	m_input = new TQLineEdit(m_mainWidget, "input");
	m_mainLayout->addWidget(m_input, 2, 0);

	m_serverMsgs->setFocusProxy(m_input);

	connect(m_input, TQT_SIGNAL(returnPressed()), this, TQT_SLOT(slotSendMsg()));

	// Set stretching where we want it.
	m_mainLayout->setRowStretch(1, 1); // make m_board+m_serverMsgs stretch vertically, not the rest
	m_mainLayout->setColStretch(1, 1); // make m_board stretch horizontally, not the rest

	// Check command-line args to see if we need to connect or show Monopigator window
	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

	TQCString host = args->getOption("host");	
	TQCString port = args->getOption("port");	
	if (!host.isNull() && !port.isNull())
		m_atlantikNetwork->serverConnect(host, port.toInt());
	else
		showSelectServer();
}

void Atlantik::readConfig()
{
	// Read configuration settings
	KConfig *config = kapp->config();

	// General configuration
	config->setGroup("General");
	m_config.chatTimestamps = config->readBoolEntry("ChatTimeStamps", false);

	// Personalization configuration
	config->setGroup("Personalization");
	m_config.playerName = config->readEntry("PlayerName", "Atlantik");
	m_config.playerImage = config->readEntry("PlayerImage", "cube.png");

	// Board configuration
	config->setGroup("Board");
	m_config.indicateUnowned = config->readBoolEntry("IndicateUnowned", true);
	m_config.highliteUnowned = config->readBoolEntry("HighliteUnowned", false);
	m_config.darkenMortgaged = config->readBoolEntry("DarkenMortgaged", true);
	m_config.animateTokens = config->readBoolEntry("AnimateToken", false);
	m_config.quartzEffects = config->readBoolEntry("QuartzEffects", true);

	// Meta server configuation
	config->setGroup("Monopigator");
	m_config.connectOnStart = config->readBoolEntry("ConnectOnStart", false);
	m_config.hideDevelopmentServers = config->readBoolEntry("HideDevelopmentServers", true);

	// Portfolio colors
	config->setGroup("WM");
	TQColor activeDefault(204, 204, 204), inactiveDefault(153, 153, 153);
	m_config.activeColor = config->readColorEntry("activeBackground", &activeDefault);
	m_config.inactiveColor = config->readColorEntry("inactiveBlend", &inactiveDefault);
}

void Atlantik::newPlayer(Player *player)
{
	initBoard();
	m_board->addToken(player);
	addPortfolioView(player);

	// Player::changed() is not connected until later this method, so
	// we'd better force an update.
	playerChanged(player);

	connect(player, TQT_SIGNAL(changed(Player *)), this, TQT_SLOT(playerChanged(Player *)));
	connect(player, TQT_SIGNAL(gainedTurn()), this, TQT_SLOT(gainedTurn()));
	connect(player, TQT_SIGNAL(changed(Player *)), m_board, TQT_SLOT(playerChanged(Player *)));

	KNotifyClient::event(winId(), "newplayer");
}

void Atlantik::newEstate(Estate *estate)
{
	initBoard();
	m_board->addEstateView(estate, m_config.indicateUnowned, m_config.highliteUnowned, m_config.darkenMortgaged, m_config.quartzEffects);
}

void Atlantik::newTrade(Trade *trade)
{
	TradeDisplay *tradeDisplay = new TradeDisplay(trade, m_atlanticCore, 0, "tradeDisplay");
	m_tradeGUIMap[trade] = tradeDisplay;
	tradeDisplay->show();
}

void Atlantik::newAuction(Auction *auction)
{
	initBoard();
	m_board->addAuctionWidget(auction);
}

void Atlantik::removeGUI(Player *player)
{
	// Find and remove portfolioview
	PortfolioView *portfolioView = findPortfolioView(player);
	if (portfolioView)
		m_portfolioViews.remove(portfolioView);

	if (m_board)
		m_board->removeToken(player);
}

void Atlantik::removeGUI(Trade *trade)
{
	if (TradeDisplay *tradeDisplay = m_tradeGUIMap[trade])
		delete tradeDisplay;
}

void Atlantik::showSelectServer()
{
	if (m_selectServer)
		return;

	m_selectServer = new SelectServer(m_config.connectOnStart, m_config.hideDevelopmentServers, m_mainWidget, "selectServer");
	m_mainLayout->addMultiCellWidget(m_selectServer, 0, 2, 1, 1);
	m_selectServer->show();

	if (m_selectGame)
	{
		delete m_selectGame;
		m_selectGame = 0;
	}

	m_atlanticCore->reset(true);
	initNetworkObject();

	connect(m_selectServer, TQT_SIGNAL(serverConnect(const TQString, int)), m_atlantikNetwork, TQT_SLOT(serverConnect(const TQString, int)));
	connect(m_selectServer, TQT_SIGNAL(msgStatus(const TQString &)), this, TQT_SLOT(slotMsgStatus(const TQString &)));

	m_selectServer->slotRefresh( m_config.connectOnStart );
}

void Atlantik::showSelectGame()
{
	if (m_selectGame)
		return;

	m_selectGame = new SelectGame(m_atlanticCore, m_mainWidget, "selectGame");
	m_atlanticCore->emitGames();

	m_mainLayout->addMultiCellWidget(m_selectGame, 0, 2, 1, 1);
	m_selectGame->show();

	// Reset core and GUI
	if (m_board)
	{
		m_board->hide();
		m_board->reset();
//		delete m_board;
//		m_board = 0;

		// m_portfolioViews.clear();
		m_atlanticCore->reset();
	}

	if (m_selectServer)
	{
		delete m_selectServer;
		m_selectServer = 0;
	}
	if (m_selectConfiguration)
	{
		delete m_selectConfiguration;
		m_selectConfiguration = 0;
	}

	connect(m_selectGame, TQT_SIGNAL(joinGame(int)), m_atlantikNetwork, TQT_SLOT(joinGame(int)));
	connect(m_selectGame, TQT_SIGNAL(newGame(const TQString &)), m_atlantikNetwork, TQT_SLOT(newGame(const TQString &)));
	connect(m_selectGame, TQT_SIGNAL(leaveServer()), this, TQT_SLOT(showSelectServer()));
	connect(m_selectGame, TQT_SIGNAL(msgStatus(const TQString &)), this, TQT_SLOT(slotMsgStatus(const TQString &)));
}

void Atlantik::showSelectConfiguration()
{
	if (m_selectConfiguration)
		return;

	if (m_selectGame)
	{
		delete m_selectGame;
		m_selectGame = 0;
	}

	m_selectConfiguration = new SelectConfiguration(m_atlanticCore, m_mainWidget, "selectConfiguration");
	m_mainLayout->addMultiCellWidget(m_selectConfiguration, 0, 2, 1, 1);
	m_selectConfiguration->show();

	connect(m_atlanticCore, TQT_SIGNAL(createGUI(ConfigOption *)), m_selectConfiguration, TQT_SLOT(addConfigOption(ConfigOption *)));
	connect(m_atlantikNetwork, TQT_SIGNAL(gameOption(TQString, TQString, TQString, TQString, TQString)), m_selectConfiguration, TQT_SLOT(gameOption(TQString, TQString, TQString, TQString, TQString)));
	connect(m_atlantikNetwork, TQT_SIGNAL(gameInit()), m_selectConfiguration, TQT_SLOT(initGame()));
	connect(m_selectConfiguration, TQT_SIGNAL(startGame()), m_atlantikNetwork, TQT_SLOT(startGame()));
	connect(m_selectConfiguration, TQT_SIGNAL(leaveGame()), m_atlantikNetwork, TQT_SLOT(leaveGame()));
	connect(m_selectConfiguration, TQT_SIGNAL(changeOption(int, const TQString &)), m_atlantikNetwork, TQT_SLOT(changeOption(int, const TQString &)));
	connect(m_selectConfiguration, TQT_SIGNAL(buttonCommand(TQString)), m_atlantikNetwork, TQT_SLOT(writeData(TQString)));
	connect(m_selectConfiguration, TQT_SIGNAL(iconSelected(const TQString &)), m_atlantikNetwork, TQT_SLOT(setImage(const TQString &)));
	connect(m_selectConfiguration, TQT_SIGNAL(statusMessage(const TQString &)), this, TQT_SLOT(slotMsgStatus(const TQString &)));
}

void Atlantik::initBoard()
{
	if (m_board)
		return;

	m_board = new AtlantikBoard(m_atlanticCore, 40, AtlantikBoard::Play, m_mainWidget, "board");
	m_board->setViewProperties(m_config.indicateUnowned, m_config.highliteUnowned, m_config.darkenMortgaged, m_config.quartzEffects, m_config.animateTokens);

	connect(m_atlantikNetwork, TQT_SIGNAL(displayDetails(TQString, bool, bool, Estate *)), m_board, TQT_SLOT(insertDetails(TQString, bool, bool, Estate *)));
	connect(m_atlantikNetwork, TQT_SIGNAL(addCommandButton(TQString, TQString, bool)), m_board, TQT_SLOT(displayButton(TQString, TQString, bool)));
	connect(m_atlantikNetwork, TQT_SIGNAL(addCloseButton()), m_board, TQT_SLOT(addCloseButton()));
	connect(m_board, TQT_SIGNAL(tokenConfirmation(Estate *)), m_atlantikNetwork, TQT_SLOT(tokenConfirmation(Estate *)));
	connect(m_board, TQT_SIGNAL(buttonCommand(TQString)), m_atlantikNetwork, TQT_SLOT(writeData(TQString)));
}

void Atlantik::showBoard()
{
	if (m_selectGame)
	{
		delete m_selectGame;
		m_selectGame = 0;
	}

	if (m_selectConfiguration)
	{
		delete m_selectConfiguration;
		m_selectConfiguration = 0;
	}

	if (!m_board)
		initBoard();

	m_runningGame = true;

	m_mainLayout->addMultiCellWidget(m_board, 0, 2, 1, 1);
	m_board->displayDefault();
	m_board->show();

	PortfolioView *portfolioView = 0;
	for (TQPtrListIterator<PortfolioView> it(m_portfolioViews); *it; ++it)
		if ((portfolioView = dynamic_cast<PortfolioView*>(*it)))
			portfolioView->buildPortfolio();
}

void Atlantik::freezeBoard()
{
	if (!m_board)
		showBoard();

	m_runningGame = false;
	// TODO: m_board->freeze();
}

void Atlantik::slotNetworkConnected()
{
}

void Atlantik::slotNetworkError(int errnum)
{
	TQString errMsg(i18n("Error connecting: "));
	
	switch (m_atlantikNetwork->status())
	{
		case IO_ConnectError:
			if (errnum == ECONNREFUSED)
				errMsg.append(i18n("connection refused by host."));
			else
				errMsg.append(i18n("could not connect to host."));
			break;

		case IO_LookupError:
			errMsg.append(i18n("host not found."));
			break;

		default:
			errMsg.append(i18n("unknown error."));
	}

	serverMsgsAppend(errMsg);

	// Re-init network object
	initNetworkObject();
}

void Atlantik::networkClosed(int status)
{
	switch( status )
	{
	case KBufferedIO::involuntary:
		slotMsgStatus( i18n("Connection with server %1:%2 lost.").arg(m_atlantikNetwork->host()).arg(m_atlantikNetwork->port()), TQString("connect_no") );
		showSelectServer();
		break;
	default:
		if ( !m_atlantikNetwork->host().isEmpty() )
			slotMsgStatus( i18n("Disconnected from %1:%2.").arg(m_atlantikNetwork->host()).arg(m_atlantikNetwork->port()), TQString("connect_no") );
		break;
	}
}

void Atlantik::slotConfigure()
{
	if (m_configDialog == 0)
		m_configDialog = new ConfigDialog(this);
	m_configDialog->show();
	
	connect(m_configDialog, TQT_SIGNAL(okClicked()), this, TQT_SLOT(slotUpdateConfig()));
}

void Atlantik::showEventLog()
{
	if (!m_eventLogWidget)
		m_eventLogWidget = new EventLogWidget(m_eventLog, 0);
	m_eventLogWidget->show();
}

void Atlantik::configureNotifications()
{
	KNotifyDialog::configure(this);
}

void Atlantik::slotUpdateConfig()
{
	KConfig *config=kapp->config();
	bool optBool, configChanged = false;
	TQString optStr;

	optBool = m_configDialog->chatTimestamps();
	if (m_config.chatTimestamps != optBool)
	{
		m_config.chatTimestamps = optBool;
		configChanged = true;
	}

	optStr = m_configDialog->playerName();
	if (m_config.playerName != optStr)
	{
		m_config.playerName = optStr;
		m_atlantikNetwork->setName(optStr);
	}

	optStr = m_configDialog->playerImage();
	if (m_config.playerImage != optStr)
	{
		m_config.playerImage = optStr;
		m_atlantikNetwork->setImage(optStr);
	}

	optBool = m_configDialog->indicateUnowned();
	if (m_config.indicateUnowned != optBool)
	{
		m_config.indicateUnowned = optBool;
		configChanged = true;
	}

	optBool = m_configDialog->highliteUnowned();
	if (m_config.highliteUnowned != optBool)
	{
		m_config.highliteUnowned = optBool;
		configChanged = true;
	}

	optBool = m_configDialog->darkenMortgaged();
	if (m_config.darkenMortgaged != optBool)
	{
		m_config.darkenMortgaged = optBool;
		configChanged = true;
	}

	optBool = m_configDialog->animateToken();
	if (m_config.animateTokens != optBool)
	{
		m_config.animateTokens = optBool;
		configChanged = true;
	}

	optBool = m_configDialog->quartzEffects();
	if (m_config.quartzEffects != optBool)
	{
		m_config.quartzEffects = optBool;
		configChanged = true;
	}

	optBool = m_configDialog->connectOnStart();
	if (m_config.connectOnStart != optBool)
	{
		m_config.connectOnStart = optBool;
		configChanged = true;
	}

	optBool = m_configDialog->hideDevelopmentServers();
	if (m_config.hideDevelopmentServers != optBool)
	{
		m_config.hideDevelopmentServers = optBool;
		if (m_selectServer)
			m_selectServer->setHideDevelopmentServers(optBool);

		configChanged = true;
	}

	config->setGroup("General");
	config->writeEntry("ChatTimeStamps", m_config.chatTimestamps);

	config->setGroup("Personalization");
	config->writeEntry("PlayerName", m_config.playerName);
	config->writeEntry("PlayerImage", m_config.playerImage);

	config->setGroup("Board");
	config->writeEntry("IndicateUnowned", m_config.indicateUnowned);
	config->writeEntry("HighliteUnowned", m_config.highliteUnowned);
	config->writeEntry("DarkenMortgaged", m_config.darkenMortgaged);
	config->writeEntry("AnimateToken", m_config.animateTokens);
	config->writeEntry("QuartzEffects", m_config.quartzEffects);

	config->setGroup("Monopigator");
	config->writeEntry("ConnectOnStart", m_config.connectOnStart);
	config->writeEntry("HideDevelopmentServers", m_config.hideDevelopmentServers);

	config->sync();

	if (configChanged && m_board)
		m_board->setViewProperties(m_config.indicateUnowned, m_config.highliteUnowned, m_config.darkenMortgaged, m_config.quartzEffects, m_config.animateTokens);
}

void Atlantik::slotSendMsg()
{
	m_atlantikNetwork->cmdChat(m_input->text());
	m_input->setText(TQString::null);
}

void Atlantik::slotMsgInfo(TQString msg)
{
	serverMsgsAppend(msg);
}

void Atlantik::slotMsgError(TQString msg)
{
	serverMsgsAppend("Error: " + msg);
}

void Atlantik::slotMsgStatus(const TQString &message, const TQString &icon)
{
	KMainWindow::statusBar()->changeItem(message, 1);
	m_eventLog->addEvent(message, icon);
}

void Atlantik::slotMsgChat(TQString player, TQString msg)
{
	if (m_config.chatTimestamps)
	{
		TQTime time = TQTime::currentTime();
		serverMsgsAppend(TQString("[%1] %2: %3").arg(time.toString("hh:mm")).arg(player).arg(msg));
	}
	else
		serverMsgsAppend(player + ": " + msg);
	KNotifyClient::event(winId(), "chat");
}

void Atlantik::serverMsgsAppend(TQString msg)
{
	// Use append, not setText(old+new) because that one doesn't wrap
	m_serverMsgs->append(msg);
	m_serverMsgs->ensureVisible(0, m_serverMsgs->contentsHeight());
}

void Atlantik::playerChanged(Player *player)
{
	PortfolioView *portfolioView = findPortfolioView(player);
	if (!portfolioView)
		portfolioView = addPortfolioView(player);

	Player *playerSelf = m_atlanticCore->playerSelf();
	if (player == playerSelf)
	{
		// We changed ourselves..
		PortfolioView *portfolioView = 0;
		for (TQPtrListIterator<PortfolioView> it(m_portfolioViews); *it; ++it)
			if ((portfolioView = dynamic_cast<PortfolioView*>(*it)))
			{
				// Clear all portfolios if we're not in game
				if ( !player->game() )
					portfolioView->clearPortfolio();

				// Show players in our game, hide the rest
				Player *pTmp = portfolioView->player();
				if (pTmp->game() == playerSelf->game())
					portfolioView->show();
				else
					portfolioView->hide();
			}
		if (!player->game())
			showSelectGame();
		else
		{
			if ( !m_board || m_board->isHidden() )
				showSelectConfiguration();
		}

		m_roll->setEnabled(player->canRoll());
		m_buyEstate->setEnabled(player->canBuy());
		m_auctionEstate->setEnabled(player->canAuction());

		// TODO: Should be more finetuned, but monopd doesn't send can_endturn can_payjail can_jailroll yet
		m_endTurn->setEnabled(player->hasTurn() && !(player->canRoll() || player->canBuy() || player->inJail()));
		m_jailCard->setEnabled(player->canUseCard());
		m_jailPay->setEnabled(player->hasTurn() && player->inJail());
		m_jailRoll->setEnabled(player->hasTurn() && player->inJail());
	}
	else
	{
		// Another player changed, check if we need to show or hide
		// his/her portfolioView.
		if (playerSelf)
		{
			if (player->game() == playerSelf->game())
				portfolioView->show();
			else
				portfolioView->hide();
		}
		else if ( !player->game() )
			portfolioView->hide();
	}
}

void Atlantik::gainedTurn()
{
	KNotifyClient::event(winId(), "gainedturn", i18n("It is your turn now.") );
}

void Atlantik::initEventLog()
{
	m_eventLog = new EventLog();
}

void Atlantik::initNetworkObject()
{
	if (m_atlantikNetwork)
	{
		m_atlantikNetwork->reset();
		return;
	}

	m_atlantikNetwork = new AtlantikNetwork(m_atlanticCore);
	connect(m_atlantikNetwork, TQT_SIGNAL(msgInfo(TQString)), this, TQT_SLOT(slotMsgInfo(TQString)));
	connect(m_atlantikNetwork, TQT_SIGNAL(msgError(TQString)), this, TQT_SLOT(slotMsgError(TQString)));
	connect(m_atlantikNetwork, TQT_SIGNAL(msgStatus(const TQString &, const TQString &)), this, TQT_SLOT(slotMsgStatus(const TQString &, const TQString &)));
	connect(m_atlantikNetwork, TQT_SIGNAL(msgChat(TQString, TQString)), this, TQT_SLOT(slotMsgChat(TQString, TQString)));

	connect(m_atlantikNetwork, TQT_SIGNAL(connectionSuccess()), this, TQT_SLOT(slotNetworkConnected()));
	connect(m_atlantikNetwork, TQT_SIGNAL(connectionFailed(int)), this, TQT_SLOT(slotNetworkError(int)));
	connect(m_atlantikNetwork, TQT_SIGNAL(closed(int)), this, TQT_SLOT(networkClosed(int)));

	connect(m_atlantikNetwork, TQT_SIGNAL(receivedHandshake()), this, TQT_SLOT(sendHandshake()));

	connect(m_atlantikNetwork, TQT_SIGNAL(gameConfig()), this, TQT_SLOT(showSelectConfiguration()));
	connect(m_atlantikNetwork, TQT_SIGNAL(gameInit()), this, TQT_SLOT(initBoard()));
	connect(m_atlantikNetwork, TQT_SIGNAL(gameRun()), this, TQT_SLOT(showBoard()));
	connect(m_atlantikNetwork, TQT_SIGNAL(gameEnd()), this, TQT_SLOT(freezeBoard()));

	connect(m_atlantikNetwork, TQT_SIGNAL(newEstate(Estate *)), this, TQT_SLOT(newEstate(Estate *)));
	connect(m_atlantikNetwork, TQT_SIGNAL(newAuction(Auction *)), this, TQT_SLOT(newAuction(Auction *)));

	connect(m_atlantikNetwork, TQT_SIGNAL(clientCookie(TQString)), this, TQT_SLOT(clientCookie(TQString)));
	connect(m_atlantikNetwork, TQT_SIGNAL(networkEvent(const TQString &, const TQString &)), m_eventLog, TQT_SLOT(addEvent(const TQString &, const TQString &)));

	connect(this, TQT_SIGNAL(rollDice()), m_atlantikNetwork, TQT_SLOT(rollDice()));
	connect(this, TQT_SIGNAL(buyEstate()), m_atlantikNetwork, TQT_SLOT(buyEstate()));
	connect(this, TQT_SIGNAL(auctionEstate()), m_atlantikNetwork, TQT_SLOT(auctionEstate()));
	connect(this, TQT_SIGNAL(endTurn()), m_atlantikNetwork, TQT_SLOT(endTurn()));
	connect(this, TQT_SIGNAL(jailCard()), m_atlantikNetwork, TQT_SLOT(jailCard()));
	connect(this, TQT_SIGNAL(jailPay()), m_atlantikNetwork, TQT_SLOT(jailPay()));
	connect(this, TQT_SIGNAL(jailRoll()), m_atlantikNetwork, TQT_SLOT(jailRoll()));
}

void Atlantik::clientCookie(TQString cookie)
{
	KConfig *config = kapp->config();

	if (cookie.isNull())
	{
		if (config->hasGroup("Reconnection"))
			config->deleteGroup("Reconnection", true);
	}
	else if (m_atlantikNetwork)
	{
		config->setGroup("Reconnection");
		config->writeEntry("Host", m_atlantikNetwork->host());
		config->writeEntry("Port", m_atlantikNetwork->port());
		config->writeEntry("Cookie", cookie);
	}
	else
		return;

	config->sync();
}

void Atlantik::sendHandshake()
{
	m_atlantikNetwork->setName(m_config.playerName);
	m_atlantikNetwork->setImage(m_config.playerImage);

	// Check command-line args to see if we need to auto-join
	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

	TQCString game = args->getOption("game");	
	if (!game.isNull())
		m_atlantikNetwork->joinGame(game.toInt());
}

void Atlantik::statusBarClick(int item)
{
	if ( item == 0 )
	{
		KAboutApplication dialog(kapp->aboutData(), this);
		dialog.exec();
	}
	else if ( item == 1)
		showEventLog();
}

PortfolioView *Atlantik::addPortfolioView(Player *player)
{
	PortfolioView *portfolioView = new PortfolioView(m_atlanticCore, player, m_config.activeColor, m_config.inactiveColor, m_portfolioWidget);
	m_portfolioViews.append(portfolioView);
	if ( m_portfolioViews.count() > 0 && m_portfolioScroll->isHidden() )
		m_portfolioScroll->show();

	connect(player, TQT_SIGNAL(changed(Player *)), portfolioView, TQT_SLOT(playerChanged()));
	connect(portfolioView, TQT_SIGNAL(newTrade(Player *)), m_atlantikNetwork, TQT_SLOT(newTrade(Player *)));
	connect(portfolioView, TQT_SIGNAL(kickPlayer(Player *)), m_atlantikNetwork, TQT_SLOT(kickPlayer(Player *)));
	connect(portfolioView, TQT_SIGNAL(estateClicked(Estate *)), m_board, TQT_SLOT(prependEstateDetails(Estate *)));

	m_portfolioLayout->addWidget(portfolioView);
	portfolioView->show();

	return portfolioView;
}

PortfolioView *Atlantik::findPortfolioView(Player *player)
{
	PortfolioView *portfolioView = 0;
	for (TQPtrListIterator<PortfolioView> it(m_portfolioViews); (portfolioView = *it) ; ++it)
		if (player == portfolioView->player())
			return portfolioView;

	return 0;
}

void Atlantik::closeEvent(TQCloseEvent *e)
{
	Game *gameSelf = m_atlanticCore->gameSelf();
	Player *playerSelf = m_atlanticCore->playerSelf();

	int result = KMessageBox::Continue;
	if ( gameSelf && !playerSelf->isBankrupt() && m_runningGame )
		result = KMessageBox::warningContinueCancel( this, i18n("You are currently part of an active game. Are you sure you want to close Atlantik? If you do, you forfeit the game."), i18n("Close & Forfeit?"), i18n("Close && Forfeit") );

	if ( result == KMessageBox::Continue )
	{
		if ( m_atlantikNetwork )
			m_atlantikNetwork->leaveGame();

		saveMainWindowSettings(kapp->config(), "AtlantikMainWindow");
		KMainWindow::closeEvent(e);
	}
}