remoteconsole.cpp
1 //------------------------------------------------------------------------------
2 // remoteconsole.h
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2009 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #include "plugins/engineplugin.h"
25 #include "serverapi/rconprotocol.h"
26 #include "serverapi/server.h"
27 #include "widgets/serverconsole.h"
28 #include "remoteconsole.h"
29 #include "rconpassworddialog.h"
30 #include "strings.h"
31 #include "ui_remoteconsole.h"
32 
33 #include <QKeyEvent>
34 #include <QMessageBox>
35 #include <QScopedPointer>
36 #include <QString>
37 
38 DClass<RemoteConsole> : public Ui::RemoteConsole
39 {
40 public:
41  RConProtocol *protocol;
42  ServerConsole *serverConsole;
43  ServerPtr server;
44 };
45 
46 DPointered(RemoteConsole)
47 
48 RemoteConsole::RemoteConsole(QWidget *parent) : QMainWindow(parent)
49 {
50  d->protocol = NULL;
51 
52  // Prompt for connection info & password.
53  RconPasswordDialog *dlg = new RconPasswordDialog(this, true);
54  connect(dlg, SIGNAL(rejected()), this, SLOT(close()));
55  while(d->protocol == NULL)
56  {
57  int ret = dlg->exec();
58  if(ret == QDialog::Accepted)
59  {
60  QString address;
61  unsigned short port;
62  Strings::translateServerAddress(dlg->serverAddress(), address, port, QString("localhost:%1").arg(dlg->selectedEngine()->data()->defaultServerPort));
63 
64  d->server = dlg->selectedEngine()->server(QHostAddress(address), port);
65  if(!d->server->hasRcon())
66  {
67  QMessageBox::critical(this, tr("No RCon support"), tr("The source port selected has no RCon support."));
68  close();
69  return;
70  }
71  d->protocol = d->server->rcon();
72 
73  if(d->protocol != NULL)
74  {
75  setWindowIcon(d->server->icon());
76  standardInit();
77 
78  d->protocol->sendPassword(dlg->connectPassword());
79  }
80  }
81  else
82  break;
83  }
84  delete dlg;
85 }
86 
87 RemoteConsole::RemoteConsole(ServerPtr server, QWidget *parent)
88 : QMainWindow(parent)
89 {
90  d->protocol = server->rcon();
91  d->server = server;
92  standardInit();
93 
94  setWindowIcon(server->icon());
95  changeServerName(server->name());
96 
97  if (d->protocol != NULL)
98  {
99  showPasswordDialog();
100  }
101  else
102  {
103  QMessageBox::critical(parent, tr("RCon Failure"),
104  tr("Failed to connect RCon to server %1:%2").arg(
105  server->address().toString()).arg(server->port()));
106  }
107 }
108 
109 RemoteConsole::~RemoteConsole()
110 {
111 }
112 
113 void RemoteConsole::changeServerName(const QString &name)
114 {
115  setWindowTitle(name + tr(" - Remote Console"));
116 }
117 
118 void RemoteConsole::closeEvent(QCloseEvent *event)
119 {
120  if(d->protocol && d->protocol->isConnected())
121  d->protocol->disconnectFromServer();
122  event->accept();
123 }
124 
125 void RemoteConsole::invalidPassword()
126 {
127  QMessageBox::critical(this, tr("Invalid Password"), tr("The password you entered appears to be invalid."));
128  showPasswordDialog();
129 }
130 
132 {
133  return d->protocol != NULL;
134 }
135 
136 void RemoteConsole::disconnectFromServer()
137 {
138  if (d->protocol)
139  {
140  d->protocol->disconnectFromServer();
141  }
142  else
143  {
144  close();
145  }
146 }
147 
148 void RemoteConsole::showPasswordDialog()
149 {
150  // Prompt for password.
151  RconPasswordDialog *dlg = new RconPasswordDialog(this);
152  connect(dlg, SIGNAL(rejected()), this, SLOT(close()));
153  int ret = dlg->exec();
154  if(ret == QDialog::Accepted)
155  {
156  d->protocol->sendPassword(dlg->connectPassword());
157  }
158  delete dlg;
159 
160  // Set/Restore focus to the cmd line input.
161  d->serverConsole->setFocus();
162 }
163 
164 void RemoteConsole::standardInit()
165 {
166  d->setupUi(this);
167  d->serverConsole = new ServerConsole();
168  d->console->layout()->addWidget(d->serverConsole);
169 
170  // delete ourself on close
171  setAttribute(Qt::WA_DeleteOnClose);
172 
173  if(d->protocol == NULL)
174  return;
175 
176  // If we connected show the window
177  show();
178 
179  connect(d->actionDisconnect, SIGNAL(triggered()), this, SLOT(disconnectFromServer()));
180  connect(d->serverConsole, SIGNAL(messageSent(const QString &)), d->protocol, SLOT(sendCommand(const QString &)));
181  connect(d->protocol, SIGNAL(disconnected()), this, SLOT(close()));
182  connect(d->protocol, SIGNAL(messageReceived(const QString &)), d->serverConsole, SLOT(appendMessage(const QString &)));
183  connect(d->protocol, SIGNAL(invalidPassword()), this, SLOT(invalidPassword()));
184  connect(d->protocol, SIGNAL(playerListUpdated()), this, SLOT(updatePlayerList()));
185  connect(d->protocol, SIGNAL(serverNameChanged(const QString &)), this, SLOT(changeServerName(const QString &)));
186 }
187 
188 void RemoteConsole::updatePlayerList()
189 {
190  const QList<Player> &list = d->protocol->players();
191 
192  d->playerTable->setRowCount(list.size());
193  for(int i = 0; i < list.size(); ++i)
194  {
195  QString name = list[i].nameFormatted();
196  QTableWidgetItem* newItem = new QTableWidgetItem(name);
197  d->playerTable->setItem(i, 0, newItem);
198  }
199 }
static void translateServerAddress(const QString &addressString, QString &hostname, unsigned short &port, const QString &defaultAddress)
Translates string in format "hostname:port" to atomic values.
Definition: strings.cpp:392
Remote console protocol interface.
Definition: rconprotocol.h:79
virtual ServerPtr server(const QHostAddress &address, unsigned short port) const
Creates an instance of server object from this plugin.
bool isValid() const