00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "highscoresmodel.h"
00021
00022 #include <core/gluonobject.h>
00023 #include <core/gdlhandler.h>
00024 #include <core/gluon_global.h>
00025
00026 #include <QDebug>
00027 #include <QDir>
00028 #include <QTextStream>
00029
00030 using namespace GluonCore;
00031 using namespace GluonPlayer;
00032
00033 static const char serviceURI[] = "gamingfreedom.org";
00034
00035 HighScoresModel::HighScoresModel( QString gameId, QObject* parent )
00036 : QAbstractTableModel( parent )
00037 , m_gameId(gameId), rootNode( new GluonObject( "HighScores" ) )
00038 {
00039 loadData();
00040 }
00041
00042 HighScoresModel::~HighScoresModel()
00043 {
00044 saveData();
00045 }
00046
00047 QVariant HighScoresModel::data( const QModelIndex& index, int role ) const
00048 {
00049 if( role == Qt::DisplayRole || role == Qt::EditRole )
00050 {
00051 switch( index.column() )
00052 {
00053 case NameColumn:
00054 return rootNode->child( index.row() )->name();
00055 break;
00056 case HighScoreColumn:
00057 return rootNode->child( index.row() )->property( "HighScore" );
00058 break;
00059 case LevelColumn:
00060 return rootNode->child( index.row() )->property( "Level" );
00061 break;
00062 }
00063 }
00064 return QVariant();
00065 }
00066
00067 int HighScoresModel::columnCount( const QModelIndex& parent ) const
00068 {
00069 Q_UNUSED( parent );
00070 return 3;
00071 }
00072
00073 int HighScoresModel::rowCount( const QModelIndex& parent ) const
00074 {
00075 Q_UNUSED( parent );
00076 return rootNode->children().count();
00077 }
00078
00079 QVariant HighScoresModel::headerData( int section, Qt::Orientation orientation, int role ) const
00080 {
00081 if( role == Qt::DisplayRole && orientation == Qt::Horizontal )
00082 {
00083 switch( section )
00084 {
00085 case NameColumn:
00086 return QString( "Name" );
00087 break;
00088 case HighScoreColumn:
00089 return QString( "High Score" );
00090 break;
00091 case LevelColumn:
00092 return QString( "Level" );
00093 break;
00094 }
00095 }
00096
00097 return QVariant();
00098 }
00099
00100 void HighScoresModel::loadData()
00101 {
00102 QDir gluonDir = QDir::home();
00103 gluonDir.mkpath( ".gluon/" + QString( serviceURI ) );
00104 gluonDir.cd( ".gluon/" + QString( serviceURI ) );
00105 QString filename = gluonDir.absoluteFilePath( "highscores.gdl" );
00106
00107 QFile dataFile( filename );
00108 while( 1 )
00109 {
00110 if( !dataFile.open( QIODevice::ReadOnly ) )
00111 {
00112 if( !QFile::exists( filename ) )
00113 {
00114 qDebug() << "Cannot find the file " << filename << ", creating new";
00115 dataFile.close();
00116 saveData();
00117 continue;
00118 }
00119 else
00120 {
00121 qDebug() << "Cannot open the file " << filename;
00122 return;
00123 }
00124 }
00125 else
00126 {
00127 break;
00128 }
00129 }
00130
00131 QTextStream highScoresReader( &dataFile );
00132 QString fileContents = highScoresReader.readAll();
00133 dataFile.close();
00134
00135 if( fileContents.isEmpty() )
00136 {
00137 qDebug() << "Something is wrong with the high scores file";
00138 return;
00139 }
00140
00141 QList<GluonObject*> highScores = GluonCore::GDLHandler::instance()->parseGDL( fileContents, 0 );
00142 rootNode = highScores.at( 0 );
00143 }
00144
00145 void HighScoresModel::saveData()
00146 {
00147 qDebug() << "Saving high scores Data";
00148 QDir gluonDir = QDir::home();
00149 gluonDir.mkpath( ".gluon/" + QString( serviceURI ) );
00150 gluonDir.cd( ".gluon/" + QString( serviceURI ) );
00151 QString filename = gluonDir.absoluteFilePath( "highscores.gdl" );
00152
00153 QFile dataFile( filename );
00154 if( !dataFile.open( QIODevice::WriteOnly ) )
00155 {
00156 qDebug() << "Cannot open the high scores file";
00157 return;
00158 }
00159
00160 QList<const GluonObject*> highScores;
00161 highScores.append( rootNode );
00162 QTextStream dataWriter( &dataFile );
00163 dataWriter << GluonCore::GDLHandler::instance()->serializeGDL( highScores );
00164 dataFile.close();
00165 qDebug() << "Saved";
00166 }
00167
00168 #include "highscoresmodel.moc"