kexthighscore_internal.h
00001 /* 00002 This file is part of the TDE games library 00003 Copyright (C) 2001-2004 Nicolas Hadacek (hadacek@kde.org) 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #ifndef KEXTHIGHSCORE_INTERNAL_H 00021 #define KEXTHIGHSCORE_INTERNAL_H 00022 00023 #include <tdeapplication.h> 00024 #include <tdeconfig.h> 00025 #include <tdelocale.h> 00026 #include <kurl.h> 00027 00028 #include "khighscore.h" 00029 #include "kexthighscore.h" 00030 00031 class TQTextStream; 00032 class TQTabWidget; 00033 class TQDomNamedNodeMap; 00034 00035 00036 namespace KExtHighscore 00037 { 00038 00039 class PlayerInfos; 00040 class Score; 00041 class Manager; 00042 00043 00044 //----------------------------------------------------------------------------- 00045 class RankItem : public Item 00046 { 00047 public: 00048 RankItem() 00049 : Item((uint)0, i18n("Rank"), TQt::AlignRight) {} 00050 00051 TQVariant read(uint rank, const TQVariant &) const { return rank; } 00052 TQString pretty(uint rank, const TQVariant &) const 00053 { return TQString::number(rank+1); } 00054 }; 00055 00056 class NameItem : public Item 00057 { 00058 public: 00059 NameItem() 00060 : Item(TQString(), i18n("Name"), TQt::AlignLeft) { 00061 setPrettySpecial(Anonymous); 00062 } 00063 }; 00064 00065 class DateItem : public Item 00066 { 00067 public: 00068 DateItem() 00069 : Item(TQDateTime(), i18n("Date"), TQt::AlignRight) { 00070 setPrettyFormat(DateTime); 00071 } 00072 }; 00073 00074 class SuccessPercentageItem : public Item 00075 { 00076 public: 00077 SuccessPercentageItem() 00078 : Item((double)-1, i18n("Success"), TQt::AlignRight) { 00079 setPrettyFormat(Percentage); 00080 setPrettySpecial(NegativeNotDefined); 00081 } 00082 }; 00083 00084 //----------------------------------------------------------------------------- 00085 class ItemContainer 00086 { 00087 public: 00088 ItemContainer(); 00089 ~ItemContainer(); 00090 00091 void setItem(Item *item); 00092 const Item *item() const { return _item; } 00093 Item *item() { return _item; } 00094 00095 void setName(const TQString &name) { _name = name; } 00096 TQString name() const { return _name; } 00097 00098 void setGroup(const TQString &group) { _group = group; } 00099 bool isStored() const { return !_group.isNull(); } 00100 00101 void setSubGroup(const TQString &subGroup) { _subGroup = subGroup; } 00102 bool canHaveSubGroup() const { return !_subGroup.isNull(); } 00103 00104 static const char ANONYMOUS[]; // name assigned to anonymous players 00105 static const char ANONYMOUS_LABEL[]; 00106 00107 TQVariant read(uint i) const; 00108 TQString pretty(uint i) const; 00109 void write(uint i, const TQVariant &value) const; 00110 // for UInt TQVariant (return new value) 00111 uint increment(uint i) const; 00112 00113 private: 00114 Item *_item; 00115 TQString _name, _group, _subGroup; 00116 00117 TQString entryName() const; 00118 00119 ItemContainer(const ItemContainer &); 00120 ItemContainer &operator =(const ItemContainer &); 00121 }; 00122 00123 //----------------------------------------------------------------------------- 00128 class ItemArray : public TQMemArray<ItemContainer *> 00129 { 00130 public: 00131 ItemArray(); 00132 virtual ~ItemArray(); 00133 00134 virtual uint nbEntries() const = 0; 00135 00136 const ItemContainer *item(const TQString &name) const; 00137 ItemContainer *item(const TQString &name); 00138 00139 void addItem(const TQString &name, Item *, bool stored = true, 00140 bool canHaveSubGroup = false); 00141 void setItem(const TQString &name, Item *); 00142 int findIndex(const TQString &name) const; 00143 00144 void setGroup(const TQString &group); 00145 void setSubGroup(const TQString &subGroup); 00146 00147 void read(uint k, Score &data) const; 00148 void write(uint k, const Score &data, uint maxNbLines) const; 00149 00150 void exportToText(TQTextStream &) const; 00151 00152 private: 00153 TQString _group, _subGroup; 00154 00155 void _setItem(uint i, const TQString &name, Item *, bool stored, 00156 bool canHaveSubGroup); 00157 00158 ItemArray(const ItemArray &); 00159 ItemArray &operator =(const ItemArray &); 00160 }; 00161 00162 //----------------------------------------------------------------------------- 00163 class ScoreInfos : public ItemArray 00164 { 00165 public: 00166 ScoreInfos(uint maxNbEntries, const PlayerInfos &infos); 00167 00168 uint nbEntries() const; 00169 uint maxNbEntries() const { return _maxNbEntries; } 00170 00171 private: 00172 uint _maxNbEntries; 00173 }; 00174 00175 //----------------------------------------------------------------------------- 00176 class ConfigGroup : public TDEConfigGroupSaver 00177 { 00178 public: 00179 ConfigGroup(const TQString &group = TQString()) 00180 : TDEConfigGroupSaver(kapp->config(), group) {} 00181 }; 00182 00183 //----------------------------------------------------------------------------- 00184 class PlayerInfos : public ItemArray 00185 { 00186 public: 00187 PlayerInfos(); 00188 00189 bool isNewPlayer() const { return _newPlayer; } 00190 bool isOldLocalPlayer() const { return _oldLocalPlayer; } 00191 uint nbEntries() const; 00192 TQString name() const { return item("name")->read(_id).toString(); } 00193 bool isAnonymous() const; 00194 TQString prettyName() const { return prettyName(_id); } 00195 TQString prettyName(uint id) const { return item("name")->pretty(id); } 00196 TQString registeredName() const; 00197 TQString comment() const { return item("comment")->pretty(_id); } 00198 bool isWWEnabled() const; 00199 TQString key() const; 00200 uint id() const { return _id; } 00201 uint oldLocalId() const { return _oldLocalId; } 00202 00203 void createHistoItems(const TQMemArray<uint> &scores, bool bound); 00204 TQString histoName(uint i) const; 00205 uint histoSize() const; 00206 const TQMemArray<uint> &histogram() const { return _histogram; } 00207 00208 void submitScore(const Score &) const; 00209 // return true if the nickname is already used locally 00210 bool isNameUsed(const TQString &name) const; 00211 void modifyName(const TQString &newName) const; 00212 void modifySettings(const TQString &newName, const TQString &comment, 00213 bool WWEnabled, const TQString &newKey) const; 00214 void removeKey(); 00215 00216 private: 00217 bool _newPlayer, _bound, _oldLocalPlayer; 00218 uint _id, _oldLocalId; 00219 TQMemArray<uint> _histogram; 00220 }; 00221 00222 //----------------------------------------------------------------------------- 00223 class ManagerPrivate 00224 { 00225 public: 00226 ManagerPrivate(uint nbGameTypes, Manager &manager); 00227 void init(uint maxNbentries); 00228 ~ManagerPrivate(); 00229 00230 bool modifySettings(const TQString &newName, const TQString &comment, 00231 bool WWEnabled, TQWidget *widget); 00232 00233 void setGameType(uint type); 00234 void checkFirst(); 00235 int submitLocal(const Score &score); 00236 int submitScore(const Score &score, TQWidget *widget, bool askIfAnonymous); 00237 Score readScore(uint i) const; 00238 00239 uint gameType() const { return _gameType; } 00240 uint nbGameTypes() const { return _nbGameTypes; } 00241 bool isWWHSAvailable() const { return !serverURL.isEmpty(); } 00242 ScoreInfos &scoreInfos() { return *_scoreInfos; } 00243 PlayerInfos &playerInfos() { return *_playerInfos; } 00244 KHighscore &hsConfig() { return *_hsConfig; } 00245 enum QueryType { Submit, Register, Change, Players, Scores }; 00246 KURL queryURL(QueryType type, const TQString &newName=TQString()) const; 00247 00248 void exportHighscores(TQTextStream &); 00249 00250 Manager &manager; 00251 KURL serverURL; 00252 TQString version; 00253 bool showStatistics, showDrawGames, trackLostGames, trackDrawGames; 00254 Manager::ShowMode showMode; 00255 00256 private: 00257 KHighscore *_hsConfig; 00258 PlayerInfos *_playerInfos; 00259 ScoreInfos *_scoreInfos; 00260 bool _first; 00261 const uint _nbGameTypes; 00262 uint _gameType; 00263 00264 // return -1 if not a local best score 00265 int rank(const Score &score) const; 00266 00267 bool submitWorldWide(const Score &score, TQWidget *parent) const; 00268 static bool doQuery(const KURL &url, TQWidget *parent, 00269 TQDomNamedNodeMap *map = 0); 00270 static bool getFromQuery(const TQDomNamedNodeMap &map, const TQString &name, 00271 TQString &value, TQWidget *parent); 00272 void convertToGlobal(); 00273 }; 00274 00275 } // namespace 00276 00277 #endif