many many netclient/frondent changes (just the beginning though):
* better separation of concerns
* trying to reduce segfaulst due to netclient being deconstructed while still in use
* no extra popup for quit reason anymore
* errors/warnings will be displayed in chat instead of popups in most cases
* NOTE: auto-kick for ignored players is currently _disabled_ will fix that soon
--- a/QTfrontend/chatwidget.cpp Sat Sep 24 00:00:57 2011 +0400
+++ b/QTfrontend/chatwidget.cpp Fri Sep 23 22:42:30 2011 +0200
@@ -111,6 +111,9 @@
.UserAction .nick { color: #ffa0ff; }\
.FriendAction { color: #ff00ff; }\
.FriendAction .nick { color: #ff30ff; }\
+.Error { color: #ff0000 }\
+.Warning { color: #ff8000 }\
+.Notice { color: #fefefe }\
";
HWChatWidget::HWChatWidget(QWidget* parent, QSettings * gameSettings, SDLInteraction * sdli, bool notify) :
@@ -340,9 +343,6 @@
isFriend = friendsList.contains(nick, Qt::CaseInsensitive);
}
- if (chatStrings.size() > 250)
- chatStrings.removeFirst();
-
QString formattedStr = Qt::escape(str.mid(1));
// make hedgewars.org urls actual links
formattedStr = formattedStr.replace(URLREGEXP, "<a href=\"http://\\3\">\\3</a>");
@@ -367,9 +367,17 @@
cssClass = "FriendChat";
}
- formattedStr = QString("<span class=\"%2\">%1</span>").arg(formattedStr).arg(cssClass);
+ addLine(cssClass,formattedStr);
+}
- chatStrings.append(formattedStr);
+void HWChatWidget::addLine(const QString& cssClass, QString line)
+{
+ if (chatStrings.size() > 250)
+ chatStrings.removeFirst();
+
+ line = QString("<span class=\"%2\">%1</span>").arg(line).arg(cssClass);
+
+ chatStrings.append(line);
chatText->setHtml(chatStrings.join("<br>"));
--- a/QTfrontend/chatwidget.h Sat Sep 24 00:00:57 2011 +0400
+++ b/QTfrontend/chatwidget.h Fri Sep 23 22:42:30 2011 +0200
@@ -61,6 +61,7 @@
void saveLists(const QString & nick);
void setShowReady(bool s);
void setShowFollow(bool enabled);
+ void addLine(const QString & cssClass, QString line);
static const char* STYLE;
QStringList ignoreList, friendsList;
--- a/QTfrontend/hwform.cpp Sat Sep 24 00:00:57 2011 +0400
+++ b/QTfrontend/hwform.cpp Fri Sep 23 22:42:30 2011 +0200
@@ -16,6 +16,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
+#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QMessageBox>
@@ -753,7 +754,82 @@
NetConnectServer("netserver.hedgewars.org", 46631);
}
-void HWForm::_NetConnect(const QString & hostName, quint16 port, const QString & nick)
+void HWForm::NetPassword(const QString & nick)
+{
+ bool ok = false;
+ int passLength = config->value("net/passwordlength", 0).toInt();
+ QString hash = config->value("net/passwordhash", "").toString();
+
+ // If the password is blank, ask the user to enter one in
+ if (passLength == 0)
+ {
+ QString password = QInputDialog::getText(this, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(nick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok);
+
+ if (!ok) {
+ ForcedDisconnect(tr("No password supplied."));
+ return;
+ }
+
+ hash = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5).toHex();
+ config->setValue("net/passwordhash", hash);
+ config->setValue("net/passwordlength", password.size());
+ config->setNetPasswordLength(password.size());
+ }
+
+ hwnet->SendPasswordHash(hash);
+}
+
+void HWForm::NetNickTaken(const QString & nick)
+{
+ bool ok = false;
+ QString newNick = QInputDialog::getText(this, tr("Nickname"), tr("Some one already uses\n your nickname %1\non the server.\nPlease pick another nickname:").arg(nick), QLineEdit::Normal, nick, &ok);
+
+ if (!ok || newNick.isEmpty()) {
+ ForcedDisconnect(tr("No nickname supplied."));
+ return;
+ }
+
+ hwnet->NewNick(newNick);
+ config->setValue("net/nick", newNick);
+ config->updNetNick();
+}
+
+void HWForm::NetAuthFailed()
+{
+ // Set the password blank if case the user tries to join and enter his password again
+ config->setValue("net/passwordlength", 0);
+ config->setNetPasswordLength(0);
+}
+
+void HWForm::NetTeamAccepted(const QString & team)
+{
+ ui.pageNetGame->pNetTeamsWidget->changeTeamStatus(team);
+}
+
+void HWForm::NetError(const QString & errmsg)
+{
+ switch (ui.Pages->currentIndex())
+ {
+ case ID_PAGE_INGAME:
+ ShowErrorMessage(errmsg);
+ // no break
+ case ID_PAGE_NETGAME:
+ ui.pageNetGame->pChatWidget->addLine("Error",errmsg);
+ break;
+ default:
+ ui.pageRoomsList->chatWidget->addLine("Error",errmsg);
+ }
+}
+
+void HWForm::NetWarning(const QString & wrnmsg)
+{
+ if (ui.Pages->currentIndex() == ID_PAGE_NETGAME || ui.Pages->currentIndex() == ID_PAGE_INGAME)
+ ui.pageNetGame->pChatWidget->addLine("Warning",wrnmsg);
+ else
+ ui.pageRoomsList->chatWidget->addLine("Warning",wrnmsg);
+}
+
+void HWForm::_NetConnect(const QString & hostName, quint16 port, QString nick)
{
if(hwnet) {
hwnet->Disconnect();
@@ -763,17 +839,22 @@
ui.pageRoomsList->chatWidget->clear();
- hwnet = new HWNewNet(config, ui.pageNetGame->pGameCFG, ui.pageNetGame->pNetTeamsWidget);
+ hwnet = new HWNewNet();
GoToPage(ID_PAGE_CONNECTING);
- connect(hwnet, SIGNAL(showMessage(const QString &)), this, SLOT(ShowErrorMessage(const QString &)), Qt::QueuedConnection);
-
connect(hwnet, SIGNAL(AskForRunGame()), this, SLOT(CreateNetGame()));
connect(hwnet, SIGNAL(Connected()), this, SLOT(NetConnected()));
+ connect(hwnet, SIGNAL(Error(const QString&)), this, SLOT(NetError(const QString&)));
+ connect(hwnet, SIGNAL(Warning(const QString&)), this, SLOT(NetWarning(const QString&)));
connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
- connect(hwnet, SIGNAL(LeftRoom()), this, SLOT(NetLeftRoom()));
+ connect(hwnet, SIGNAL(LeftRoom(const QString&)), this, SLOT(NetLeftRoom(const QString&)));
connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)));
+ connect(hwnet, SIGNAL(RemoveNetTeam(const HWTeam&)), this, SLOT(RemoveNetTeam(const HWTeam&)));
+ connect(hwnet, SIGNAL(TeamAccepted(const QString&)), this, SLOT(NetTeamAccepted(const QString&)));
+ connect(hwnet, SIGNAL(AskForPassword(const QString&)), this, SLOT(NetPassword(const QString&)));
+ connect(hwnet, SIGNAL(NickTaken(const QString&)), this, SLOT(NetNickTaken(const QString&)));
+ connect(hwnet, SIGNAL(AuthFailed()), this, SLOT(NetAuthFailed()));
//connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), hwnet, SLOT(partRoom()));
// rooms list page stuff
@@ -879,8 +960,22 @@
connect(ui.pageAdmin->pbClearAccountsCache, SIGNAL(clicked()), hwnet, SLOT(clearAccountsCache()));
// disconnect
- connect(hwnet, SIGNAL(Disconnected()), this, SLOT(ForcedDisconnect()), Qt::QueuedConnection);
+ connect(hwnet, SIGNAL(Disconnected(const QString&)), this, SLOT(ForcedDisconnect(const QString&)), Qt::QueuedConnection);
+
+// config stuff
+ connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
+ connect(ui.pageNetGame->pGameCFG, SIGNAL(paramChanged(const QString &, const QStringList &)), hwnet, SLOT(onParamChanged(const QString &, const QStringList &)));
+ connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
+ while (nick.isEmpty()) {
+ nick = QInputDialog::getText(this,
+ QObject::tr("Nickname"),
+ QObject::tr("Please enter your nickname"),
+ QLineEdit::Normal,
+ QDir::home().dirName());
+ config->setValue("net/nick",nick);
+ config->updNetNick();
+ }
hwnet->Connect(hostName, port, nick);
}
@@ -928,11 +1023,6 @@
void HWForm::NetDisconnect()
{
- if(hwnet) {
- hwnet->Disconnect();
- delete hwnet;
- hwnet = 0;
- }
if(pnetserver) {
if (pRegisterServer)
{
@@ -946,15 +1036,12 @@
}
}
-void HWForm::ForcedDisconnect()
+void HWForm::ForcedDisconnect(const QString & reason)
{
if(pnetserver) return; // we have server - let it care of all things
if (hwnet) {
- HWNewNet * tmp = hwnet;
- hwnet = 0;
- tmp->deleteLater();
QMessageBox::warning(this, QMessageBox::tr("Network"),
- QMessageBox::tr("Connection to server is lost"));
+ QMessageBox::tr("Connection to server is lost") + (reason.isEmpty()?"":("\n\n" + HWNewNet::tr("Quit reason: ") + '"' + reason +'"')));
}
if (ui.Pages->currentIndex() != ID_PAGE_NET) GoBack();
@@ -976,6 +1063,11 @@
ui.pageNetGame->pNetTeamsWidget->addTeam(team);
}
+void HWForm::RemoveNetTeam(const HWTeam& team)
+{
+ ui.pageNetGame->pNetTeamsWidget->removeNetTeam(team);
+}
+
void HWForm::StartMPGame()
{
QString ammo;
@@ -1141,6 +1233,9 @@
void HWForm::NetGameChangeStatus(bool isMaster)
{
+ ui.pageNetGame->pGameCFG->setEnabled(isMaster);
+ ui.pageNetGame->pNetTeamsWidget->setInteractivity(isMaster);
+
if (isMaster)
NetGameMaster();
else
@@ -1201,10 +1296,14 @@
ui.pageNetGame->pGameCFG->GameSchemes->setCurrentIndex(0);
}
-void HWForm::NetLeftRoom()
+void HWForm::NetLeftRoom(const QString & reason)
{
if (ui.Pages->currentIndex() == ID_PAGE_NETGAME || ui.Pages->currentIndex() == ID_PAGE_INGAME)
+ {
GoBack();
+ if (!reason.isEmpty())
+ ui.pageRoomsList->chatWidget->addLine("Notice",reason);
+ }
else
qWarning("Left room while not in room");
}
--- a/QTfrontend/hwform.h Sat Sep 24 00:00:57 2011 +0400
+++ b/QTfrontend/hwform.h Fri Sep 23 22:42:30 2011 +0200
@@ -97,11 +97,18 @@
void NetStartServer();
void NetDisconnect();
void NetConnected();
+ void NetError(const QString & errmsg);
+ void NetWarning(const QString & wrnmsg);
void NetGameEnter();
+ void NetPassword(const QString & nick);
+ void NetNickTaken(const QString & nick);
+ void NetAuthFailed();
+ void NetTeamAccepted(const QString& team);
void AddNetTeam(const HWTeam& team);
+ void RemoveNetTeam(const HWTeam& team);
void StartMPGame();
void GameStateChanged(GameState gameState);
- void ForcedDisconnect();
+ void ForcedDisconnect(const QString & reason);
void ShowErrorMessage(const QString &);
void GetRecord(bool isDemo, const QByteArray & record);
void CreateNetGame();
@@ -115,13 +122,13 @@
void NetGameSlave();
void AsyncNetServerStart();
- void NetLeftRoom();
+ void NetLeftRoom(const QString & reason);
void selectFirstNetScheme();
void saveDemoWithCustomName();
private:
- void _NetConnect(const QString & hostName, quint16 port, const QString & nick);
+ void _NetConnect(const QString & hostName, quint16 port, QString nick);
void UpdateTeamsLists(const QStringList* editable_teams=0);
void CreateGame(GameCFGWidget * gamecfg, TeamSelWidget* pTeamSelWidget, QString ammo);
void closeEvent(QCloseEvent *event);
--- a/QTfrontend/newnetclient.cpp Sat Sep 24 00:00:57 2011 +0400
+++ b/QTfrontend/newnetclient.cpp Fri Sep 23 22:42:30 2011 +0200
@@ -24,10 +24,7 @@
#include "hwconsts.h"
#include "newnetclient.h"
#include "proto.h"
-#include "gameuiconfig.h"
#include "game.h"
-#include "gamecfgwidget.h"
-#include "teamselect.h"
#include "misc.h"
/* only to get the ignoreList from the chat widget */
#include "hwform.h"
@@ -36,10 +33,7 @@
char delimeter='\n';
-HWNewNet::HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget) :
- config(config),
- m_pGameCFGWidget(pGameCFGWidget),
- m_pTeamSelWidget(pTeamSelWidget),
+HWNewNet::HWNewNet() :
isChief(false),
m_game_connected(false),
loginStep(0),
@@ -51,11 +45,6 @@
connect(&NetSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnect()));
connect(&NetSocket, SIGNAL(error(QAbstractSocket::SocketError)), this,
SLOT(displayError(QAbstractSocket::SocketError)));
-
-// config stuff
- connect(this, SIGNAL(paramChanged(const QString &, const QStringList &)), pGameCFGWidget, SLOT(setParam(const QString &, const QStringList &)));
- connect(pGameCFGWidget, SIGNAL(paramChanged(const QString &, const QStringList &)), this, SLOT(onParamChanged(const QString &, const QStringList &)));
- connect(this, SIGNAL(configAsked()), pGameCFGWidget, SLOT(fullNetConfig()));
}
HWNewNet::~HWNewNet()
@@ -63,7 +52,7 @@
if (m_game_connected)
{
RawSendNet(QString("QUIT%1%2").arg(delimeter).arg("User quit"));
- emit Disconnected();
+ emit Disconnected(tr("User quit"));
}
NetSocket.flush();
}
@@ -71,15 +60,6 @@
void HWNewNet::Connect(const QString & hostName, quint16 port, const QString & nick)
{
mynick = nick;
- while (mynick.isEmpty()) {
- mynick = QInputDialog::getText(m_pGameCFGWidget,
- QObject::tr("Nickname"),
- QObject::tr("Please enter your nickname"),
- QLineEdit::Normal,
- QDir::home().dirName());
- config->setValue("net/nick",mynick);
- config->updNetNick();
- }
myhost = hostName + QString(":%1").arg(port);
NetSocket.connectToHost(hostName, port);
}
@@ -147,6 +127,11 @@
RawSendNet(QString("REMOVE_TEAM") + delimeter + team.TeamName);
}
+void HWNewNet::NewNick(const QString & nick)
+{
+ RawSendNet(QString("NICK%1%2").arg(delimeter).arg(nick));
+}
+
void HWNewNet::ToggleReady()
{
RawSendNet(QString("TOGGLE_READY"));
@@ -166,7 +151,7 @@
void HWNewNet::RawSendNet(const QByteArray & buf)
{
- qDebug() << "Client: " << QString(buf).split("\n");
+ qDebug() << "Client: " << QString(buf).split("\n");
NetSocket.write(buf);
NetSocket.write("\n\n", 2);
}
@@ -191,29 +176,33 @@
void HWNewNet::OnDisconnect()
{
- if(m_game_connected) emit Disconnected();
+ if(m_game_connected) emit Disconnected("");
m_game_connected = false;
}
void HWNewNet::displayError(QAbstractSocket::SocketError socketError)
{
- emit Disconnected();
m_game_connected = false;
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
- emit showMessage(tr("The host was not found. Please check the host name and port settings."));
+ emit Disconnected(tr("The host was not found. Please check the host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
- emit showMessage(tr("Connection refused"));
+ emit Disconnected(tr("Connection refused"));
break;
default:
- emit showMessage(NetSocket.errorString());
+ emit Disconnected(NetSocket.errorString());
}
}
+void HWNewNet::SendPasswordHash(const QString & hash)
+{
+ RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash));
+}
+
void HWNewNet::ParseCmd(const QStringList & lst)
{
qDebug() << "Server: " << lst;
@@ -235,17 +224,17 @@
if (lst[0] == "ERROR") {
if (lst.size() == 2)
- emit showMessage("Error: " + lst[1]);
+ emit Error(lst[1]);
else
- emit showMessage("Unknown error");
+ emit Error("Unknown error");
return;
}
if (lst[0] == "WARNING") {
if (lst.size() == 2)
- emit showMessage("Warning: " + lst[1]);
+ emit Warning(lst[1]);
else
- emit showMessage("Unknown warning");
+ emit Warning("Unknown warning");
return;
}
@@ -383,21 +372,19 @@
qWarning("Net: Bad REMOVETEAM message");
return;
}
- m_pTeamSelWidget->removeNetTeam(HWTeam(lst[1]));
+ emit RemoveNetTeam(HWTeam(lst[1]));
return;
}
if(lst[0] == "ROOMABANDONED") {
netClientState = 2;
- emit showMessage(HWNewNet::tr("Room destroyed"));
- emit LeftRoom();
+ emit LeftRoom(tr("Room destroyed"));
return;
}
if(lst[0] == "KICKED") {
netClientState = 2;
- emit showMessage(HWNewNet::tr("You got kicked"));
- emit LeftRoom();
+ emit LeftRoom(tr("You got kicked"));
return;
}
@@ -418,11 +405,14 @@
if (isChief)
emit configAsked();
}
+ // TODO reactivate
+/*
if (lst[i] != mynick && isChief && config->Form->ui.pageRoomsList->chatWidget->ignoreList.contains(lst[i], Qt::CaseInsensitive) && !config->Form->ui.pageRoomsList->chatWidget->friendsList.contains(lst[i], Qt::CaseInsensitive))
{
kickPlayer(lst[i]);
}
else
+*/
{
emit nickAdded(lst[i], isChief && (lst[i] != mynick));
emit chatStringFromNet(tr("%1 *** %2 has joined the room").arg('\x03').arg(lst[i]));
@@ -498,28 +488,7 @@
}
if (lst[0] == "ASKPASSWORD") {
- bool ok = false;
- int passLength = config->value("net/passwordlength", 0).toInt();
- QString hash = config->value("net/passwordhash", "").toString();
-
- // If the password is blank, ask the user to enter one in
- if (passLength == 0)
- {
- QString password = QInputDialog::getText(m_pGameCFGWidget, tr("Password"), tr("Your nickname %1 is\nregistered on Hedgewars.org\nPlease provide your password below\nor pick another nickname in game config:").arg(mynick), QLineEdit::Password, passLength==0?NULL:QString(passLength,'\0'), &ok);
-
- if (!ok) {
- Disconnect();
- emit Disconnected();
- return;
- }
-
- hash = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5).toHex();
- config->setValue("net/passwordhash", hash);
- config->setValue("net/passwordlength", password.size());
- config->setNetPasswordLength(password.size());
- }
-
- RawSendNet(QString("PASSWORD%1%2").arg(delimeter).arg(hash));
+ emit AskForPassword(mynick);
return;
}
@@ -549,7 +518,7 @@
qWarning("Net: Bad TEAM_ACCEPTED message");
return;
}
- m_pTeamSelWidget->changeTeamStatus(lst[1]);
+ emit TeamAccepted(lst[1]);
return;
}
@@ -616,11 +585,11 @@
}
if (lst[1] == "Authentication failed")
{
- // Set the password blank if case the user tries to join and enter his password again
- config->setValue("net/passwordlength", 0);
- config->setNetPasswordLength(0);
+ emit AuthFailed();
}
- emit showMessage(HWNewNet::tr("Quit reason: ") + lst[1]);
+ m_game_connected = false;
+ Disconnect();
+ emit Disconnected(lst[1]);
return;
}
@@ -633,15 +602,11 @@
if (lst[0] == "ROOM_CONTROL_ACCESS") {
if (lst.size() < 2)
{
- qWarning("Net: Bad BYE message");
+ qWarning("Net: Bad ROOM_CONTROL_ACCESS message");
return;
}
- bool b = lst[1] != "0";
- m_pGameCFGWidget->setEnabled(b);
- m_pTeamSelWidget->setInteractivity(b);
- isChief = b;
+ isChief = (lst[1] != "0");
emit roomMaster(isChief);
-
return;
}
@@ -825,21 +790,7 @@
{
case 0:
{
- bool ok = false;
- QString newNick = QInputDialog::getText(m_pGameCFGWidget, tr("Nickname"), tr("Some one already uses\n your nickname %1\non the server.\nPlease pick another nickname:").arg(mynick), QLineEdit::Normal, mynick, &ok);
-
- if (!ok || newNick.isEmpty()) {
- Disconnect();
- emit Disconnected();
- return;
- }
-
- config->setValue("net/nick", newNick);
- config->updNetNick();
- mynick = newNick;
-
- RawSendNet(QString("NICK%1%2").arg(delimeter).arg(newNick));
-
+ emit NickTaken(mynick);
break;
}
}
--- a/QTfrontend/newnetclient.h Sat Sep 24 00:00:57 2011 +0400
+++ b/QTfrontend/newnetclient.h Fri Sep 23 22:42:30 2011 +0200
@@ -39,10 +39,12 @@
Q_OBJECT
public:
- HWNewNet(GameUIConfig * config, GameCFGWidget* pGameCFGWidget, TeamSelWidget* pTeamSelWidget);
+ HWNewNet();
~HWNewNet();
void Connect(const QString & hostName, quint16 port, const QString & nick);
void Disconnect();
+ void SendPasswordHash(const QString & hash);
+ void NewNick(const QString & nick);
bool isRoomChief();
bool isInRoom();
int getClientState();
@@ -51,10 +53,6 @@
QString getHost();
private:
- GameUIConfig* config;
- GameCFGWidget* m_pGameCFGWidget;
- TeamSelWidget* m_pTeamSelWidget;
-
bool isChief;
QString mynick;
QString myroom;
@@ -95,9 +93,14 @@
signals:
void AskForRunGame();
void Connected();
- void Disconnected();
+ void Disconnected(const QString & reason);
+ void Error(const QString & errmsg);
+ void Warning(const QString & wrnmsg);
+ void AskForPassword(const QString & nick);
+ void NickTaken(const QString & nick);
+ void AuthFailed();
void EnteredGame();
- void LeftRoom();
+ void LeftRoom(const QString & reason);
void nickAdded(const QString& nick, bool notifyNick);
void nickRemoved(const QString& nick);
void nickAddedLobby(const QString& nick, bool notifyNick);
@@ -110,7 +113,9 @@
void paramChanged(const QString & param, const QStringList & value);
void configAsked();
+ void TeamAccepted(const QString&);
void AddNetTeam(const HWTeam&);
+ void RemoveNetTeam(const HWTeam&);
void hhnumChanged(const HWTeam&);
void teamColorChanged(const HWTeam&);
void chatStringLobby(const QString&);
@@ -127,7 +132,6 @@
void setReadyStatus(const QString & nick, bool isReady);
void setMyReadyStatus(bool isReady);
- void showMessage(const QString &);
public slots:
void ToggleReady();