2011-07-02 23:03:18 +00:00
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2011 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 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 Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
2011-07-03 19:45:08 +00:00
*****************************************************************************/
#include "carddeckstylereader.h"
2009-03-30 13:12:44 +00:00
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <fstream>
2009-04-14 21:00:22 +00:00
#include "game_defs.h"
2009-03-30 13:12:44 +00:00
using namespace std ;
2009-04-14 21:00:22 +00:00
CardDeckStyleReader :: CardDeckStyleReader ( ConfigFile * c , QWidget * w ) : myConfig ( c ), myW ( w ), fallBack ( 0 ), loadedSuccessfull ( 0 )
2009-03-30 13:12:44 +00:00
{
2011-02-11 20:02:08 +00:00
2009-03-30 13:12:44 +00:00
}
CardDeckStyleReader ::~ CardDeckStyleReader ()
{
}
2011-02-11 20:02:08 +00:00
void CardDeckStyleReader :: readStyleFile ( QString file )
{
2009-03-30 13:12:44 +00:00
//if style file failed --> default style fallback
2011-02-11 20:02:08 +00:00
if ( QFile ( file ). exists ()) {
2009-04-18 23:17:10 +00:00
currentFileName = file ;
2011-02-11 20:02:08 +00:00
} else {
currentFileName = QString :: fromUtf8 ( myConfig -> readConfigString ( "AppDataDir" ). c_str ()) + "gfx/cards/default/defaultdeckstyle.xml" ;
2009-03-30 13:12:44 +00:00
fallBack = 1 ;
}
2009-04-18 23:17:10 +00:00
QFile myFile ( currentFileName );
2009-04-18 23:55:09 +00:00
myFile . open ( QIODevice :: ReadOnly );
2009-04-18 23:17:10 +00:00
fileContent = myFile . readAll ();
2009-03-30 13:12:44 +00:00
QFileInfo info ( currentFileName );
2009-03-30 19:01:54 +00:00
currentDir = info . absolutePath () + "/" ;
2009-03-30 13:12:44 +00:00
2011-02-11 20:02:08 +00:00
//start reading the file and fill vars
2009-03-30 13:12:44 +00:00
string tempString1 ( "" );
2009-04-18 23:17:10 +00:00
2011-02-11 20:02:08 +00:00
TiXmlDocument doc ;
2009-04-18 23:17:10 +00:00
doc . Parse ( fileContent . constData ());
if ( doc . RootElement ()) {
2011-02-11 20:02:08 +00:00
TiXmlHandle docHandle ( & doc );
2009-03-30 13:12:44 +00:00
2011-02-11 20:02:08 +00:00
TiXmlElement * GameTableElement = docHandle . FirstChild ( "PokerTH" ). FirstChild ( "TableStyle" ). ToElement ();
if ( GameTableElement ) {
QMessageBox :: warning ( myW , tr ( "Card Deck Style Error" ),
tr ( "A game table style was selected instead of a card deck style. \n Please select a card deck style and try again!" ),
QMessageBox :: Ok );
} else {
2009-04-24 22:53:52 +00:00
2011-02-11 20:02:08 +00:00
TiXmlElement * itemsList = docHandle . FirstChild ( "PokerTH" ). FirstChild ( "CardDeck" ). FirstChild (). ToElement ();
for ( ; itemsList ; itemsList = itemsList -> NextSiblingElement ()) {
const char * tmpStr1 = itemsList -> Attribute ( "value" );
if ( tmpStr1 ) {
tempString1 = tmpStr1 ;
2009-04-24 22:53:52 +00:00
2011-02-11 20:02:08 +00:00
if ( itemsList -> ValueStr () == "StyleDescription" ) {
StyleDescription = QString :: fromUtf8 ( tempString1 . c_str ());
} else if ( itemsList -> ValueStr () == "StyleMaintainerName" ) {
StyleMaintainerName = QString :: fromUtf8 ( tempString1 . c_str ());
} else if ( itemsList -> ValueStr () == "StyleMaintainerEMail" ) {
StyleMaintainerEMail = QString :: fromUtf8 ( tempString1 . c_str ());
} else if ( itemsList -> ValueStr () == "StyleCreateDate" ) {
StyleCreateDate = QString :: fromUtf8 ( tempString1 . c_str ());
} else if ( itemsList -> ValueStr () == "PokerTHStyleFileVersion" ) {
PokerTHStyleFileVersion = QString :: fromUtf8 ( tempString1 . c_str ());
} else if ( itemsList -> ValueStr () == "Preview" ) {
Preview = currentDir + QString :: fromUtf8 ( tempString1 . c_str ());
}
}
}
//check if style items are left and show warning
leftItems . clear ();
2009-04-24 22:53:52 +00:00
2011-02-11 20:02:08 +00:00
if ( StyleDescription == "" ) {
leftItems << "StyleDescription" ;
}
if ( StyleMaintainerName == "" ) {
leftItems << "StyleMaintainerName" ;
}
if ( StyleMaintainerEMail == "" ) {
leftItems << "StyleMaintainerEMail" ;
}
if ( StyleCreateDate == "" ) {
leftItems << "StyleCreateDate" ;
}
if ( PokerTHStyleFileVersion == "" ) {
leftItems << "PokerTHStyleFileVersion" ;
}
2009-04-24 22:53:52 +00:00
2011-02-11 20:02:08 +00:00
//check if all files are there
cardsLeft . clear ();
int i ;
for ( i = 0 ; i < 52 ; i ++ ) {
QString cardString ( QString :: number ( i ) + ".png" );
if ( ! QDir ( currentDir ). exists ( cardString )) {
cardsLeft << cardString ;
}
}
if ( ! QDir ( currentDir ). exists ( "flipside.png" )) {
cardsLeft << "flipside.png" ;
}
2009-04-24 22:53:52 +00:00
2011-02-11 20:02:08 +00:00
// set loadedSuccessfull TRUE if everything works
if ( leftItems . isEmpty () && cardsLeft . isEmpty () && PokerTHStyleFileVersion != "" && PokerTHStyleFileVersion . toInt () == POKERTH_CD_STYLE_FILE_VERSION )
loadedSuccessfull = 1 ;
else
loadedSuccessfull = 0 ;
2009-04-24 22:53:52 +00:00
2011-02-11 20:02:08 +00:00
if ( ! leftItems . isEmpty () && myW != 0 ) showLeftItemsErrorMessage ( StyleDescription , leftItems , StyleMaintainerEMail );
else {
if ( ! cardsLeft . isEmpty () && myW != 0 ) showCardsLeftErrorMessage ( StyleDescription , cardsLeft , StyleMaintainerEMail );
//check for style file version
if ( PokerTHStyleFileVersion != "" && PokerTHStyleFileVersion . toInt () != POKERTH_CD_STYLE_FILE_VERSION ) {
QString EMail ;
if ( StyleMaintainerEMail != "NULL" ) EMail = StyleMaintainerEMail ;
QMessageBox :: warning ( myW , tr ( "Card Deck Style Error" ),
tr ( "Selected card deck style \" %1 \" seems to be outdated. \n The current PokerTH card deck style version is \" %2 \" , but this style has version \" %3 \" set. \n\n Please contact the card deck style builder %4." ). arg ( StyleDescription ). arg ( POKERTH_CD_STYLE_FILE_VERSION ). arg ( PokerTHStyleFileVersion ). arg ( EMail ),
QMessageBox :: Ok );
}
}
}
} else {
loadedSuccessfull = 0 ;
QMessageBox :: warning ( myW , tr ( "Card Deck Style Error" ),
tr ( "Cannot load card deck style file: %1 \n\n Please check the style file or choose another style!" ). arg ( currentFileName ),
QMessageBox :: Ok );
}
2009-04-10 18:31:31 +00:00
2009-03-30 13:12:44 +00:00
}
2009-04-10 18:31:31 +00:00
void CardDeckStyleReader :: showLeftItemsErrorMessage ( QString style , QStringList failedItems , QString email )
{
QString items = failedItems . join ( ", " );
2011-02-11 20:02:08 +00:00
QString EMail ;
if ( email != "NULL" ) EMail = email ;
2009-04-10 18:31:31 +00:00
2009-04-10 20:02:28 +00:00
QMessageBox :: warning ( myW , tr ( "Card Deck Style Error" ),
2011-02-11 20:02:08 +00:00
tr ( "Selected card deck style \" %1 \" seems to be incomplete or defective. \n\n The value(s) of \" %2 \" is/are missing. \n\n Please contact the card deck style builder %3." ). arg ( style ). arg ( items ). arg ( EMail ),
QMessageBox :: Ok );
2009-04-10 18:31:31 +00:00
}
void CardDeckStyleReader :: showCardsLeftErrorMessage ( QString style , QStringList failedItems , QString email )
2009-04-03 09:03:31 +00:00
{
QString items = failedItems . join ( ", " );
2011-02-11 20:02:08 +00:00
QString EMail ;
if ( email != "NULL" ) EMail = email ;
2009-04-03 09:03:31 +00:00
QMessageBox :: warning ( myW , tr ( "Card Deck Style Error" ),
2011-02-11 20:02:08 +00:00
tr ( "Selected card deck style \" %1 \" seems to be incomplete or defective. \n The card picture(s) \" %2 \" is/are not available. \n\n Please contact the card deck style builder %3." ). arg ( style ). arg ( items ). arg ( EMail ),
QMessageBox :: Ok );
2009-04-03 09:03:31 +00:00
}