Files
pokerth/src/gui/qt/joinnetworkgamedialog/joinnetworkgamedialogimpl.cpp
T

253 lines
9.5 KiB
C++
Raw Normal View History

2007-02-15 00:57:04 +00:00
/***************************************************************************
* Copyright (C) 2006 by FThauer FHammer *
* f.thauer@web.de *
* *
* 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. *
* *
* 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; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "joinnetworkgamedialogimpl.h"
2007-02-18 13:45:56 +00:00
#include "session.h"
2007-02-27 19:11:27 +00:00
#include "configfile.h"
2007-02-27 19:44:03 +00:00
#include "tinyxml.h"
2007-02-15 00:57:04 +00:00
2007-02-28 02:43:56 +00:00
using namespace std;
2007-05-13 13:32:06 +00:00
joinNetworkGameDialogImpl::joinNetworkGameDialogImpl(QWidget *parent, ConfigFile *c)
: QDialog(parent), myConfig(c)
2007-02-15 00:57:04 +00:00
{
2007-05-06 21:45:04 +00:00
bool toIntTrue;
setupUi(this);
2007-04-20 23:14:08 +00:00
2007-05-13 13:32:06 +00:00
spinBox_port->setValue(QString::fromUtf8(myConfig->readConfigString("ServerPort").c_str()).toInt(&toIntTrue, 10));
2007-02-15 00:57:04 +00:00
2007-02-28 02:43:56 +00:00
//Profile Name darf nicht mit einer Zahl beginnen --> XML konform
QRegExp rx("[A-Z|a-z]+[A-Z|a-z|\\d]*");
QValidator *validator = new QRegExpValidator(rx, this);
lineEdit_profileName->setValidator(validator);
2007-02-28 20:51:43 +00:00
pushButton_delete->setDisabled(TRUE);
2007-02-21 23:12:41 +00:00
lineEdit_ipAddress->setFocus();
2007-05-13 13:32:06 +00:00
myServerProfilesFile = myConfig->readConfigString("DataDir")+"serverprofiles.xml";
2007-02-21 23:12:41 +00:00
2007-02-27 22:53:03 +00:00
//Anlegen wenn noch nicht existiert!
2007-04-03 10:55:16 +00:00
QFile serverProfilesfile(QString::fromUtf8(myServerProfilesFile.c_str()));
2007-02-28 02:43:56 +00:00
if(!serverProfilesfile.exists()) {
2007-02-27 22:53:03 +00:00
2007-02-28 02:43:56 +00:00
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "");
doc.LinkEndChild( decl );
TiXmlElement * root = new TiXmlElement( "PokerTH" );
doc.LinkEndChild( root );
TiXmlElement * profiles = new TiXmlElement( "ServerProfiles" );
root->LinkEndChild( profiles );
2007-02-27 22:53:03 +00:00
doc.SaveFile( QString::fromUtf8(myServerProfilesFile.c_str()).toStdString() );
2007-02-28 02:43:56 +00:00
}
//Liste Füllen
fillServerProfileList();
2007-02-27 22:53:03 +00:00
2007-02-21 23:12:41 +00:00
// QShortcut *connectKey = new QShortcut(QKeySequence(Qt::Key_Enter), this);
// connect( connectKey, SIGNAL(activated() ), pushButton_connect, SLOT( click() ) );
2007-02-18 00:51:40 +00:00
connect( pushButton_connect, SIGNAL( clicked() ), this, SLOT( startClient() ) );
2007-02-25 21:11:14 +00:00
connect( pushButton_save, SIGNAL( clicked() ), this, SLOT( saveServerProfile() ) );
connect( pushButton_delete, SIGNAL( clicked() ), this, SLOT( deleteServerProfile() ) );
2007-02-21 23:12:41 +00:00
2007-02-28 11:16:29 +00:00
connect( treeWidget, SIGNAL( itemClicked ( QTreeWidgetItem*, int) ), this, SLOT( itemFillForm (QTreeWidgetItem*, int) ) );
2007-02-15 00:57:04 +00:00
}
2007-02-18 00:51:40 +00:00
void joinNetworkGameDialogImpl::startClient() {
2007-02-18 13:45:56 +00:00
// TODO: Check input values!
2007-02-18 21:12:57 +00:00
}
2007-02-21 23:12:41 +00:00
2007-02-28 02:43:56 +00:00
void joinNetworkGameDialogImpl::fillServerProfileList() {
2007-02-25 21:11:14 +00:00
2007-02-27 19:11:27 +00:00
2007-02-28 02:43:56 +00:00
treeWidget->clear();
TiXmlDocument doc(QString::fromUtf8(myServerProfilesFile.c_str()).toStdString());
2007-02-28 02:43:56 +00:00
if(!doc.LoadFile()) {
QMessageBox::warning(this, tr("Load Server-Profile-File Error"),
2007-04-03 10:55:16 +00:00
tr("Could not load server-profiles-file:\n"+QString::fromUtf8(myServerProfilesFile.c_str()).toAscii()),
2007-02-28 02:43:56 +00:00
QMessageBox::Close);
}
TiXmlHandle docHandle( &doc );
TiXmlElement* profile = docHandle.FirstChild( "PokerTH" ).FirstChild( "ServerProfiles" ).FirstChild().ToElement();
if ( profile ) {
for( profile; profile; profile = profile->NextSiblingElement()) {
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget,0);
item->setData(0, 0, profile->Attribute("Name"));
item->setData(1, 0, profile->Attribute("Address"));
item->setData(2, 0, profile->Attribute("Port"));
string isIpv6 = "no";
int tempInt = 0;
profile->QueryIntAttribute("IsIpv6", &tempInt );
if( tempInt == 1 ) { isIpv6 = "yes"; }
2007-04-03 10:55:16 +00:00
item->setData(3, 0, QString::fromUtf8(isIpv6.c_str()));
2007-02-28 02:43:56 +00:00
treeWidget->addTopLevelItem(item);
}
}
else { cout << "No Profiles Found \n"; }
treeWidget->resizeColumnToContents ( 0 );
treeWidget->resizeColumnToContents ( 1 );
treeWidget->resizeColumnToContents ( 2 );
treeWidget->resizeColumnToContents ( 3 );
}
2007-02-28 11:16:29 +00:00
void joinNetworkGameDialogImpl::itemFillForm (QTreeWidgetItem* item, int column) {
bool toIntTrue;
TiXmlDocument doc(myServerProfilesFile);
if(!doc.LoadFile()) {
QMessageBox::warning(this, tr("Load Server-Profile-File Error"),
2007-04-03 10:55:16 +00:00
tr("Could not load server-profiles-file:\n"+QString::fromUtf8(myServerProfilesFile.c_str()).toAscii()),
2007-02-28 11:16:29 +00:00
QMessageBox::Close);
}
TiXmlHandle docHandle( &doc );
TiXmlElement* profile = docHandle.FirstChild( "PokerTH" ).FirstChild( "ServerProfiles" ).FirstChild( item->data(0,0).toString().toStdString() ).ToElement();
if ( profile ) {
2007-04-03 10:55:16 +00:00
lineEdit_profileName->setText(QString::fromUtf8(profile->Attribute("Name")));
lineEdit_ipAddress->setText(QString::fromUtf8(profile->Attribute("Address")));
lineEdit_password->setText(QString::fromUtf8(profile->Attribute("Password")));
spinBox_port->setValue(QString::fromUtf8(profile->Attribute("Port")).toInt(&toIntTrue, 10));
checkBox_ipv6->setChecked(QString::fromUtf8(profile->Attribute("IsIpv6")).toInt(&toIntTrue, 10));
2007-02-28 11:16:29 +00:00
}
2007-02-28 20:51:43 +00:00
pushButton_delete->setEnabled(TRUE);
2007-02-28 11:16:29 +00:00
}
2007-02-28 02:43:56 +00:00
void joinNetworkGameDialogImpl::saveServerProfile() {
// bool toIntTrue;
TiXmlDocument doc(myServerProfilesFile);
if(!doc.LoadFile()) {
QMessageBox::warning(this, tr("Load Server-Profile-File Error"),
2007-04-03 10:55:16 +00:00
tr("Could not load server-profiles-file:\n"+QString::fromUtf8(myServerProfilesFile.c_str()).toAscii()),
2007-02-28 02:43:56 +00:00
QMessageBox::Close);
}
TiXmlHandle docHandle( &doc );
TiXmlElement* profiles = docHandle.FirstChild( "PokerTH" ).FirstChild( "ServerProfiles" ).ToElement();
if ( profiles ) {
TiXmlElement * testProfile = docHandle.FirstChild( "PokerTH" ).FirstChild( "ServerProfiles" ).FirstChild( lineEdit_profileName->text().toStdString() ).ToElement();
if( testProfile ) {
2007-03-01 21:30:40 +00:00
// Wenn der Name schon existiert --> Überschreiben?
QMessageBox msgBox(QMessageBox::Warning, tr("Save Server Profile Error"),
2007-03-01 22:23:38 +00:00
tr("A profile with the name: \""+lineEdit_profileName->text().toAscii()+"\" already exists.\nWould you like to overwrite ?"), QMessageBox::Yes | QMessageBox::No, this);
2007-03-01 21:30:40 +00:00
switch (msgBox.exec()) {
case QMessageBox::Yes: {
// yes was clicked
// remove the old
testProfile->Parent()->RemoveChild(testProfile);
// write the new
TiXmlElement * profile1 = new TiXmlElement( lineEdit_profileName->text().toStdString() );
profiles->LinkEndChild( profile1 );
profile1->SetAttribute("Name", lineEdit_profileName->text().toStdString());
profile1->SetAttribute("Address", lineEdit_ipAddress->text().toStdString());
profile1->SetAttribute("Password", lineEdit_password->text().toStdString());
profile1->SetAttribute("Port", spinBox_port->value());
profile1->SetAttribute("IsIpv6", checkBox_ipv6->isChecked());
}
break;
case QMessageBox::No:
// no was clicked
break;
default:
// should never be reached
break;
}
2007-02-28 02:43:56 +00:00
}
else {
// Wenn der Name nicht existiert --> speichern
2007-03-01 21:30:40 +00:00
TiXmlElement * profile2 = new TiXmlElement( lineEdit_profileName->text().toStdString() );
profiles->LinkEndChild( profile2 );
profile2->SetAttribute("Name", lineEdit_profileName->text().toStdString());
profile2->SetAttribute("Address", lineEdit_ipAddress->text().toStdString());
profile2->SetAttribute("Password", lineEdit_password->text().toStdString());
profile2->SetAttribute("Port", spinBox_port->value());
profile2->SetAttribute("IsIpv6", checkBox_ipv6->isChecked());
2007-02-28 02:43:56 +00:00
}
}
else { QMessageBox::warning(this, tr("Read Server-Profile List Error"),
2007-03-01 22:23:38 +00:00
tr("Could not read server-profiles list"),
2007-02-28 02:43:56 +00:00
QMessageBox::Close); }
if(!doc.SaveFile()) { QMessageBox::warning(this, tr("Save Server-Profile-File Error"),
2007-04-03 10:55:16 +00:00
tr("Could not save server-profiles-file:\n"+QString::fromUtf8(myServerProfilesFile.c_str()).toAscii()),
2007-02-28 02:43:56 +00:00
QMessageBox::Close); }
fillServerProfileList();
2007-02-25 21:11:14 +00:00
}
void joinNetworkGameDialogImpl::deleteServerProfile() {
2007-02-28 11:16:29 +00:00
TiXmlDocument doc(myServerProfilesFile);
if(!doc.LoadFile()) {
QMessageBox::warning(this, tr("Load Server-Profile-File Error"),
2007-04-03 10:55:16 +00:00
tr("Could not load server-profiles-file:\n"+QString::fromUtf8(myServerProfilesFile.c_str()).toAscii()),
2007-02-28 11:16:29 +00:00
QMessageBox::Close);
}
else {
TiXmlHandle docHandle( &doc );
TiXmlElement* profile = docHandle.FirstChild( "PokerTH" ).FirstChild( "ServerProfiles" ).FirstChild( treeWidget->currentItem()->data(0,0).toString().toStdString() ).ToElement();
2007-02-28 20:51:43 +00:00
if ( profile ) { profile->Parent()->RemoveChild(profile); }
if(!doc.SaveFile()) { QMessageBox::warning(this, tr("Save Server-Profile-File Error"),
2007-04-03 10:55:16 +00:00
tr("Could not save server-profiles-file:\n"+QString::fromUtf8(myServerProfilesFile.c_str()).toAscii()),
2007-02-28 20:51:43 +00:00
QMessageBox::Close); }
2007-02-28 11:16:29 +00:00
//Liste Füllen
fillServerProfileList();
}
2007-02-28 20:51:43 +00:00
pushButton_delete->setDisabled(TRUE);
2007-02-25 21:11:14 +00:00
}
2007-02-21 23:12:41 +00:00
void joinNetworkGameDialogImpl::keyPressEvent ( QKeyEvent * event ) {
// std::cout << "key" << event->key();
if (event->key() == 16777220) { pushButton_connect->click(); } //ENTER
}