QTfrontend/hwform.cpp
changeset 5998 e8f44e9433f0
parent 5907 64ccc6be0ec5
child 6009 14f6fc9869f2
equal deleted inserted replaced
5996:2c72fe81dd37 5998:e8f44e9433f0
    14  * You should have received a copy of the GNU General Public License
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
       
    19 #include <QDir>
    19 #include <QFile>
    20 #include <QFile>
    20 #include <QTextStream>
    21 #include <QTextStream>
    21 #include <QMessageBox>
    22 #include <QMessageBox>
    22 #include <QPushButton>
    23 #include <QPushButton>
    23 #include <QListWidget>
    24 #include <QListWidget>
   751 void HWForm::NetConnectOfficialServer()
   752 void HWForm::NetConnectOfficialServer()
   752 {
   753 {
   753     NetConnectServer("netserver.hedgewars.org", 46631);
   754     NetConnectServer("netserver.hedgewars.org", 46631);
   754 }
   755 }
   755 
   756 
   756 void HWForm::_NetConnect(const QString & hostName, quint16 port, const QString & nick)
   757 void HWForm::NetPassword(const QString & nick)
       
   758 {
       
   759     bool ok = false;
       
   760     int passLength = config->value("net/passwordlength", 0).toInt();
       
   761     QString hash = config->value("net/passwordhash", "").toString();
       
   762 
       
   763     // If the password is blank, ask the user to enter one in
       
   764     if (passLength == 0)
       
   765     {
       
   766         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);
       
   767 
       
   768         if (!ok) {
       
   769             ForcedDisconnect(tr("No password supplied."));
       
   770             return;
       
   771         }
       
   772 
       
   773         hash = QCryptographicHash::hash(password.toLatin1(), QCryptographicHash::Md5).toHex();
       
   774         config->setValue("net/passwordhash", hash);
       
   775         config->setValue("net/passwordlength", password.size());
       
   776         config->setNetPasswordLength(password.size());
       
   777     }
       
   778 
       
   779     hwnet->SendPasswordHash(hash);
       
   780 }
       
   781 
       
   782 void HWForm::NetNickTaken(const QString & nick)
       
   783 {
       
   784     bool ok = false;
       
   785     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);
       
   786 
       
   787     if (!ok || newNick.isEmpty()) {
       
   788             ForcedDisconnect(tr("No nickname supplied."));
       
   789         return;
       
   790     }
       
   791 
       
   792     hwnet->NewNick(newNick);
       
   793     config->setValue("net/nick", newNick);
       
   794     config->updNetNick();
       
   795 }
       
   796 
       
   797 void HWForm::NetAuthFailed()
       
   798 {
       
   799     // Set the password blank if case the user tries to join and enter his password again
       
   800     config->setValue("net/passwordlength", 0);
       
   801     config->setNetPasswordLength(0);
       
   802 }
       
   803 
       
   804 void HWForm::NetTeamAccepted(const QString & team)
       
   805 {
       
   806     ui.pageNetGame->pNetTeamsWidget->changeTeamStatus(team);
       
   807 }
       
   808 
       
   809 void HWForm::NetError(const QString & errmsg)
       
   810 {
       
   811     switch (ui.Pages->currentIndex())
       
   812     {
       
   813         case ID_PAGE_INGAME:
       
   814             ShowErrorMessage(errmsg);
       
   815             // no break
       
   816         case ID_PAGE_NETGAME:
       
   817             ui.pageNetGame->pChatWidget->addLine("Error",errmsg);
       
   818             break;
       
   819         default:
       
   820         ui.pageRoomsList->chatWidget->addLine("Error",errmsg);
       
   821     }
       
   822 }
       
   823 
       
   824 void HWForm::NetWarning(const QString & wrnmsg)
       
   825 {
       
   826     if (ui.Pages->currentIndex() == ID_PAGE_NETGAME || ui.Pages->currentIndex() == ID_PAGE_INGAME)
       
   827         ui.pageNetGame->pChatWidget->addLine("Warning",wrnmsg);
       
   828     else
       
   829         ui.pageRoomsList->chatWidget->addLine("Warning",wrnmsg);
       
   830 }
       
   831 
       
   832 void HWForm::_NetConnect(const QString & hostName, quint16 port, QString nick)
   757 {
   833 {
   758     if(hwnet) {
   834     if(hwnet) {
   759         hwnet->Disconnect();
   835         hwnet->Disconnect();
   760         delete hwnet;
   836         delete hwnet;
   761         hwnet=0;
   837         hwnet=0;
   762     }
   838     }
   763 
   839 
   764     ui.pageRoomsList->chatWidget->clear();
   840     ui.pageRoomsList->chatWidget->clear();
   765 
   841 
   766     hwnet = new HWNewNet(config, ui.pageNetGame->pGameCFG, ui.pageNetGame->pNetTeamsWidget);
   842     hwnet = new HWNewNet();
   767 
   843 
   768     GoToPage(ID_PAGE_CONNECTING);
   844     GoToPage(ID_PAGE_CONNECTING);
   769 
       
   770     connect(hwnet, SIGNAL(showMessage(const QString &)), this, SLOT(ShowErrorMessage(const QString &)), Qt::QueuedConnection);
       
   771 
   845 
   772     connect(hwnet, SIGNAL(AskForRunGame()), this, SLOT(CreateNetGame()));
   846     connect(hwnet, SIGNAL(AskForRunGame()), this, SLOT(CreateNetGame()));
   773     connect(hwnet, SIGNAL(Connected()), this, SLOT(NetConnected()));
   847     connect(hwnet, SIGNAL(Connected()), this, SLOT(NetConnected()));
       
   848     connect(hwnet, SIGNAL(Error(const QString&)), this, SLOT(NetError(const QString&)));
       
   849     connect(hwnet, SIGNAL(Warning(const QString&)), this, SLOT(NetWarning(const QString&)));
   774     connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
   850     connect(hwnet, SIGNAL(EnteredGame()), this, SLOT(NetGameEnter()));
   775     connect(hwnet, SIGNAL(LeftRoom()), this, SLOT(NetLeftRoom()));
   851     connect(hwnet, SIGNAL(LeftRoom(const QString&)), this, SLOT(NetLeftRoom(const QString&)));
   776     connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)));
   852     connect(hwnet, SIGNAL(AddNetTeam(const HWTeam&)), this, SLOT(AddNetTeam(const HWTeam&)));
       
   853     connect(hwnet, SIGNAL(RemoveNetTeam(const HWTeam&)), this, SLOT(RemoveNetTeam(const HWTeam&)));
       
   854     connect(hwnet, SIGNAL(TeamAccepted(const QString&)), this, SLOT(NetTeamAccepted(const QString&)));
       
   855     connect(hwnet, SIGNAL(AskForPassword(const QString&)), this, SLOT(NetPassword(const QString&)));
       
   856     connect(hwnet, SIGNAL(NickTaken(const QString&)), this, SLOT(NetNickTaken(const QString&)));
       
   857     connect(hwnet, SIGNAL(AuthFailed()), this, SLOT(NetAuthFailed()));
   777     //connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), hwnet, SLOT(partRoom()));
   858     //connect(ui.pageNetGame->BtnBack, SIGNAL(clicked()), hwnet, SLOT(partRoom()));
   778 
   859 
   779 // rooms list page stuff
   860 // rooms list page stuff
   780     connect(hwnet, SIGNAL(roomsList(const QStringList&)),
   861     connect(hwnet, SIGNAL(roomsList(const QStringList&)),
   781         ui.pageRoomsList, SLOT(setRoomsList(const QStringList&)));
   862         ui.pageRoomsList, SLOT(setRoomsList(const QStringList&)));
   877     connect(ui.pageAdmin, SIGNAL(setProtocol(int)), hwnet, SLOT(setLatestProtocolVar(int)));
   958     connect(ui.pageAdmin, SIGNAL(setProtocol(int)), hwnet, SLOT(setLatestProtocolVar(int)));
   878     connect(ui.pageAdmin, SIGNAL(askServerVars()), hwnet, SLOT(askServerVars()));
   959     connect(ui.pageAdmin, SIGNAL(askServerVars()), hwnet, SLOT(askServerVars()));
   879     connect(ui.pageAdmin->pbClearAccountsCache, SIGNAL(clicked()), hwnet, SLOT(clearAccountsCache()));
   960     connect(ui.pageAdmin->pbClearAccountsCache, SIGNAL(clicked()), hwnet, SLOT(clearAccountsCache()));
   880 
   961 
   881 // disconnect
   962 // disconnect
   882     connect(hwnet, SIGNAL(Disconnected()), this, SLOT(ForcedDisconnect()), Qt::QueuedConnection);
   963     connect(hwnet, SIGNAL(Disconnected(const QString&)), this, SLOT(ForcedDisconnect(const QString&)), Qt::QueuedConnection);
   883 
   964 
       
   965 // config stuff
       
   966     connect(hwnet, SIGNAL(paramChanged(const QString &, const QStringList &)), ui.pageNetGame->pGameCFG, SLOT(setParam(const QString &, const QStringList &)));
       
   967     connect(ui.pageNetGame->pGameCFG, SIGNAL(paramChanged(const QString &, const QStringList &)), hwnet, SLOT(onParamChanged(const QString &, const QStringList &)));
       
   968     connect(hwnet, SIGNAL(configAsked()), ui.pageNetGame->pGameCFG, SLOT(fullNetConfig()));
       
   969 
       
   970     while (nick.isEmpty()) {
       
   971         nick = QInputDialog::getText(this,
       
   972                  QObject::tr("Nickname"),
       
   973                  QObject::tr("Please enter your nickname"),
       
   974                  QLineEdit::Normal,
       
   975                  QDir::home().dirName());
       
   976         config->setValue("net/nick",nick);
       
   977         config->updNetNick();
       
   978     }
   884     hwnet->Connect(hostName, port, nick);
   979     hwnet->Connect(hostName, port, nick);
   885 }
   980 }
   886 
   981 
   887 void HWForm::NetConnect()
   982 void HWForm::NetConnect()
   888 {
   983 {
   926     NetConnectServer("localhost", pnetserver->getRunningPort());
  1021     NetConnectServer("localhost", pnetserver->getRunningPort());
   927 }
  1022 }
   928 
  1023 
   929 void HWForm::NetDisconnect()
  1024 void HWForm::NetDisconnect()
   930 {
  1025 {
   931     if(hwnet) {
       
   932         hwnet->Disconnect();
       
   933         delete hwnet;
       
   934         hwnet = 0;
       
   935     }
       
   936     if(pnetserver) {
  1026     if(pnetserver) {
   937         if (pRegisterServer)
  1027         if (pRegisterServer)
   938         {
  1028         {
   939             pRegisterServer->unregister();
  1029             pRegisterServer->unregister();
   940             pRegisterServer = 0;
  1030             pRegisterServer = 0;
   944         delete pnetserver;
  1034         delete pnetserver;
   945         pnetserver = 0;
  1035         pnetserver = 0;
   946     }
  1036     }
   947 }
  1037 }
   948 
  1038 
   949 void HWForm::ForcedDisconnect()
  1039 void HWForm::ForcedDisconnect(const QString & reason)
   950 {
  1040 {
   951     if(pnetserver) return; // we have server - let it care of all things
  1041     if(pnetserver) return; // we have server - let it care of all things
   952     if (hwnet) {
  1042     if (hwnet) {
   953         HWNewNet * tmp = hwnet;
       
   954         hwnet = 0;
       
   955         tmp->deleteLater();
       
   956         QMessageBox::warning(this, QMessageBox::tr("Network"),
  1043         QMessageBox::warning(this, QMessageBox::tr("Network"),
   957                 QMessageBox::tr("Connection to server is lost"));
  1044                 QMessageBox::tr("Connection to server is lost") + (reason.isEmpty()?"":("\n\n" + HWNewNet::tr("Quit reason: ") + '"' + reason +'"')));
   958 
  1045 
   959     }
  1046     }
   960     if (ui.Pages->currentIndex() != ID_PAGE_NET) GoBack();
  1047     if (ui.Pages->currentIndex() != ID_PAGE_NET) GoBack();
   961 }
  1048 }
   962 
  1049 
   972 }
  1059 }
   973 
  1060 
   974 void HWForm::AddNetTeam(const HWTeam& team)
  1061 void HWForm::AddNetTeam(const HWTeam& team)
   975 {
  1062 {
   976     ui.pageNetGame->pNetTeamsWidget->addTeam(team);
  1063     ui.pageNetGame->pNetTeamsWidget->addTeam(team);
       
  1064 }
       
  1065 
       
  1066 void HWForm::RemoveNetTeam(const HWTeam& team)
       
  1067 {
       
  1068     ui.pageNetGame->pNetTeamsWidget->removeNetTeam(team);
   977 }
  1069 }
   978 
  1070 
   979 void HWForm::StartMPGame()
  1071 void HWForm::StartMPGame()
   980 {
  1072 {
   981     QString ammo;
  1073     QString ammo;
  1139         sdli.StopMusic();
  1231         sdli.StopMusic();
  1140 }
  1232 }
  1141 
  1233 
  1142 void HWForm::NetGameChangeStatus(bool isMaster)
  1234 void HWForm::NetGameChangeStatus(bool isMaster)
  1143 {
  1235 {
       
  1236     ui.pageNetGame->pGameCFG->setEnabled(isMaster);
       
  1237     ui.pageNetGame->pNetTeamsWidget->setInteractivity(isMaster);
       
  1238 
  1144     if (isMaster)
  1239     if (isMaster)
  1145         NetGameMaster();
  1240         NetGameMaster();
  1146     else
  1241     else
  1147         NetGameSlave();
  1242         NetGameSlave();
  1148 }
  1243 }
  1199 void HWForm::selectFirstNetScheme()
  1294 void HWForm::selectFirstNetScheme()
  1200 {
  1295 {
  1201     ui.pageNetGame->pGameCFG->GameSchemes->setCurrentIndex(0);
  1296     ui.pageNetGame->pGameCFG->GameSchemes->setCurrentIndex(0);
  1202 }
  1297 }
  1203 
  1298 
  1204 void HWForm::NetLeftRoom()
  1299 void HWForm::NetLeftRoom(const QString & reason)
  1205 {
  1300 {
  1206     if (ui.Pages->currentIndex() == ID_PAGE_NETGAME || ui.Pages->currentIndex() == ID_PAGE_INGAME)
  1301     if (ui.Pages->currentIndex() == ID_PAGE_NETGAME || ui.Pages->currentIndex() == ID_PAGE_INGAME)
       
  1302     {
  1207         GoBack();
  1303         GoBack();
       
  1304         if (!reason.isEmpty())
       
  1305             ui.pageRoomsList->chatWidget->addLine("Notice",reason);
       
  1306     }
  1208     else
  1307     else
  1209         qWarning("Left room while not in room");
  1308         qWarning("Left room while not in room");
  1210 }
  1309 }
  1211 
  1310 
  1212 void HWForm::resizeEvent(QResizeEvent * event)
  1311 void HWForm::resizeEvent(QResizeEvent * event)