summaryrefslogtreecommitdiffstats
path: root/lskat/lskat/networkdlg.cpp
blob: 1bf3f18f34d88701c90210b4a60fd36b8f4a63c1 (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
/***************************************************************************
                          networkdlg.cpp  -  description
                             -------------------
    copyright            : (C) 2004 by Jakub Stachowski
    email                : qbast@go2.pl
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include "networkdlg.h"
#include <tqspinbox.h>
#include <tqlineedit.h>
#include <tqcombobox.h>
#include <tqbuttongroup.h>
#include <tqwidgetstack.h>

extern const char* LSKAT_SERVICE;

// Create the dialog 
NetworkDlg::NetworkDlg( TQWidget *tqparent, const char *name )
    : NetworkDlgBase( tqparent, name, TRUE )
{
  browser = new DNSSD::ServiceBrowser(TQString::tqfromLatin1(LSKAT_SERVICE));
  connect(browser,TQT_SIGNAL(finished()),TQT_SLOT(gamesFound()));
  browser->startBrowse();
}

NetworkDlg::~NetworkDlg()
{
  delete browser;
}

void NetworkDlg::SetHost(const TQString& host)
{
  hostname->setText(host);
}

void NetworkDlg::SetName(const TQString& name)
{
  serverName->setText(name);
}
TQString NetworkDlg::QueryName() const 
{
  return serverName->text();
}

void NetworkDlg::SetPort(unsigned short port)
{
  this->port->setValue(port);
}

void NetworkDlg::gamesFound()
{
  bool autoselect=false;
  if (!clientName->count() && group->selectedId()==1) autoselect=true;
  clientName->clear();
  TQStringList names;
  TQValueList<DNSSD::RemoteService::Ptr>::ConstIterator itEnd = browser->services().end();
  for (TQValueList<DNSSD::RemoteService::Ptr>::ConstIterator it = browser->services().begin();
    it!=itEnd; ++it) names << (*it)->serviceName();
  clientName->insertStringList(names);
  if (autoselect && clientName->count()) gameSelected(0);
}

void NetworkDlg::gameSelected(int nr)
{
 if (nr>=browser->services().count() || nr<0) return;
 DNSSD::RemoteService::Ptr srv = browser->services()[nr];
 if (!srv->isResolved() && !srv->resolve()) return;
 hostname->setText(srv->hostName());
 port->setValue(srv->port());
}

unsigned short NetworkDlg::QueryPort() const
{
  return port->value();
}

TQString NetworkDlg::QueryHost() const
{
  return hostname->text();
}

void NetworkDlg::toggleServerClient()
{
  stack->raiseWidget(group->selectedId());
  if (group->selectedId()==1) {
    gameSelected(clientName->currentItem());
    hostname->setEnabled(true);
    }
    else {
      hostname->setText(TQString());
      hostname->setEnabled(false);
    }
}

#include "networkdlg.moc"