/*************************************************************************** Description : KPuzzle - A KDE Jigsaw Puzzle Game Version : 0.2 Copyright : (C) 2000-2001 by Michael Wand EMail : mwand@gmx.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. * * * ***************************************************************************/ #include "main.h" #include "highscore.h" #include #include #include #include #include #include #include #include #include #include #include #define HIGHSCORE_FILE_NAME "/.kpuzzle-highscores" /* Constructor with the additional parameter ys (the number which must * be shown in the "Your Score" field). The "Your Score" field will be * invisible if ys == 0. * Additionally, if ys != 0, the initializeDialog() function will prompt * for the player's name if necessary. * This function only creates the widget structure for the dialog, * all data is initialized by the initializeDialog() function. */ CHighscoreDialog::CHighscoreDialog(QWidget* parent,int ys) : QDialog(parent,"Highscores",true) { setCaption(i18n("Highscores")); /* Internal vars. */ _yourScore = ys; _list = new QPtrList; _list->setAutoDelete(true); /* Box layout */ _vbox = new QVBoxLayout(this,30,20); /* This is the "Your Result" label, which is shown only if * _yourScore != 0. */ _yourResult = NULL; if (_yourScore) { _yourResult = new QLabel(this); _vbox->addWidget(_yourResult); } /* The list widget. */ _listBox = new QListBox(this); _listBox->setEnabled(false); _listBox->setMinimumSize(QSize(80,100)); _vbox->addWidget(_listBox); _ok = new QPushButton(i18n("OK"),this); _ok->setDefault(true); _vbox->addWidget(_ok); connect(_ok,SIGNAL(clicked()),this,SLOT(accept())); _vbox->activate(); } /* Initializes all dialog data. Prompts for the player's name if * he has earned a place in the highscore list. */ void CHighscoreDialog::initializeDialog() { char temp[40]; char newEntry = readList(); if (_yourScore) { if (newEntry != -1) { QString n = nameDialog(); if (n != "") { _list->insert(newEntry,new CListElement(_yourScore,n)); sprintf(temp,i18n("%1 %2").arg(_yourScore).arg((const char*) n)); _list->removeLast(); } else sprintf(temp,i18n("%1 - No Highscore entry").arg(_yourScore)); } else { sprintf(temp,i18n("You are not in the Highscores.")); } _yourResult->setText(temp); } QPtrListIterator it(*_list); it.toFirst(); while(it.current()) { char* s; s = (char*) malloc(30); memset(s,0,30); sprintf(s,i18n("%1 %2").arg(it.current()->_score).arg((const char*) it.current()->_name)); _listBox->insertItem(new QListBoxText(s)); ++it; free(s); } } /* Destructor */ CHighscoreDialog::~CHighscoreDialog() { writeList(); delete _ok; delete _listBox; if (_yourScore) delete _yourResult; delete _list; } /* Show a dialog which prompts the user for his name, and * return this name. */ QString CHighscoreDialog::nameDialog() { bool OK; QString ret = QInputDialog::getText(i18n("Your name"),i18n("Enter your name for highscore list"), QLineEdit::Normal,i18n("My name"),&OK); if (!OK) return QString::null; else return ret; } /* unused XXX */ void CHighscoreDialog::getHighscoreFile(QString& name) { name = QDir::homeDirPath(); } /* Read the highscore list from a file, return the position * of the player in the list (with respect to _yourScore). * The filename is QDir::homeDirPath() + HIGHSCORE_FILE_NAME. */ char CHighscoreDialog::readList() { char name[50]; QString filename; int score; int readChars; char place = -1; /* Place of player in highscores */ char counter = 0; filename = QDir::homeDirPath() + HIGHSCORE_FILE_NAME; FILE* f = fopen(filename,"r"); if (!f) return 0; do { readChars = fscanf(f,"%d %s",&score,name); if (readChars != 2) break; char* spc; while ((spc = strchr(name,'\\')) != NULL) { *spc = ' '; } _list->append(new CListElement(score,name)); if (place == -1 && score < _yourScore) place = counter; counter++; } while (counter < MAX_HIGHSCORES - 1); if (place == -1 && counter < MAX_HIGHSCORES - 1) place = counter; fclose(f); return place; } /* Writes the highscore list to a file. */ bool CHighscoreDialog::writeList() { QString filename; filename = QDir::homeDirPath() + HIGHSCORE_FILE_NAME; FILE* f = fopen(filename,"w"); if (!f) return false; QPtrListIterator it(*_list); for (it.toFirst();it.current();++it) { char name[21]; strcpy(name,it.current()->_name); char* spc; while ((spc = strchr(name,' ')) != NULL) { *spc = '\\'; } fprintf(f,"%i %s\n",it.current()->_score,name); } fclose(f); return true; }