summaryrefslogtreecommitdiffstats
path: root/libkdegames/highscore/kexthighscore_internal.h
blob: ff70c5cdb567d2239873036dc737b458e7a67f62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
    This file is part of the KDE games library
    Copyright (C) 2001-2004 Nicolas Hadacek (hadacek@kde.org)

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License version 2 as published by the Free Software Foundation.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef KEXTHIGHSCORE_INTERNAL_H
#define KEXTHIGHSCORE_INTERNAL_H

#include <kapplication.h>
#include <kconfig.h>
#include <klocale.h>
#include <kurl.h>

#include "khighscore.h"
#include "kexthighscore.h"

class TQTextStream;
class TQTabWidget;
class TQDomNamedNodeMap;


namespace KExtHighscore
{

class PlayerInfos;
class Score;
class Manager;


//-----------------------------------------------------------------------------
class RankItem : public Item
{
 public:
    RankItem()
        : Item((uint)0, i18n("Rank"), Qt::AlignRight) {}

    TQVariant read(uint rank, const TQVariant &) const  { return rank; }
    TQString pretty(uint rank, const TQVariant &) const
        { return TQString::number(rank+1); }
};

class NameItem : public Item
{
 public:
    NameItem()
        : Item(TQString::null, i18n("Name"), Qt::AlignLeft) {
            setPrettySpecial(Anonymous);
    }
};

class DateItem : public Item
{
 public:
    DateItem()
        : Item(TQDateTime(), i18n("Date"), Qt::AlignRight) {
            setPrettyFormat(DateTime);
    }
};

class SuccessPercentageItem : public Item
{
 public:
    SuccessPercentageItem()
        : Item((double)-1, i18n("Success"), Qt::AlignRight) {
            setPrettyFormat(Percentage);
            setPrettySpecial(NegativeNotDefined);
    }
};

//-----------------------------------------------------------------------------
class ItemContainer
{
 public:
    ItemContainer();
    ~ItemContainer();

    void setItem(Item *item);
    const Item *item() const { return _item; }
    Item *item() { return _item; }

    void setName(const TQString &name) { _name = name; }
    TQString name() const { return _name; }

    void setGroup(const TQString &group) { _group = group; }
    bool isStored() const { return !_group.isNull(); }

    void setSubGroup(const TQString &subGroup) { _subGroup = subGroup; }
    bool canHaveSubGroup() const { return !_subGroup.isNull(); }

    static const char ANONYMOUS[]; // name assigned to anonymous players
    static const char ANONYMOUS_LABEL[];

    TQVariant read(uint i) const;
    TQString pretty(uint i) const;
    void write(uint i, const TQVariant &value) const;
    // for UInt TQVariant (return new value)
    uint increment(uint i) const;

 private:
    Item    *_item;
    TQString  _name, _group, _subGroup;

    TQString entryName() const;

    ItemContainer(const ItemContainer &);
    ItemContainer &operator =(const ItemContainer &);
};

//-----------------------------------------------------------------------------
/**
 * Manage a bunch of @ref Item which are saved under the same group
 * in KHighscores config file.
 */
class ItemArray : public TQMemArray<ItemContainer *>
{
 public:
    ItemArray();
    virtual ~ItemArray();

    virtual uint nbEntries() const = 0;

    const ItemContainer *item(const TQString &name) const;
    ItemContainer *item(const TQString &name);

    void addItem(const TQString &name, Item *, bool stored = true,
                 bool canHaveSubGroup = false);
    void setItem(const TQString &name, Item *);
    int findIndex(const TQString &name) const;

    void setGroup(const TQString &group);
    void setSubGroup(const TQString &subGroup);

    void read(uint k, Score &data) const;
    void write(uint k, const Score &data, uint maxNbLines) const;

    void exportToText(TQTextStream &) const;

 private:
    TQString _group, _subGroup;

    void _setItem(uint i, const TQString &name, Item *, bool stored,
                  bool canHaveSubGroup);

    ItemArray(const ItemArray &);
    ItemArray &operator =(const ItemArray &);
};

//-----------------------------------------------------------------------------
class ScoreInfos : public ItemArray
{
 public:
    ScoreInfos(uint maxNbEntries, const PlayerInfos &infos);

    uint nbEntries() const;
    uint maxNbEntries() const { return _maxNbEntries; }

 private:
    uint _maxNbEntries;
};

//-----------------------------------------------------------------------------
class ConfigGroup : public KConfigGroupSaver
{
 public:
    ConfigGroup(const TQString &group = TQString::null)
        : KConfigGroupSaver(kapp->config(), group) {}
};

//-----------------------------------------------------------------------------
class PlayerInfos : public ItemArray
{
 public:
    PlayerInfos();

    bool isNewPlayer() const { return _newPlayer; }
    bool isOldLocalPlayer() const { return _oldLocalPlayer; }
    uint nbEntries() const;
    TQString name() const { return item("name")->read(_id).toString(); }
    bool isAnonymous() const;
    TQString prettyName() const { return prettyName(_id); }
    TQString prettyName(uint id) const { return item("name")->pretty(id); }
    TQString registeredName() const;
    TQString comment() const { return item("comment")->pretty(_id); }
    bool isWWEnabled() const;
    TQString key() const;
    uint id() const { return _id; }
    uint oldLocalId() const { return _oldLocalId; }

    void createHistoItems(const TQMemArray<uint> &scores, bool bound);
    TQString histoName(uint i) const;
    uint histoSize() const;
    const TQMemArray<uint> &histogram() const { return _histogram; }

    void submitScore(const Score &) const;
    // return true if the nickname is already used locally
    bool isNameUsed(const TQString &name) const;
    void modifyName(const TQString &newName) const;
    void modifySettings(const TQString &newName, const TQString &comment,
                        bool WWEnabled, const TQString &newKey) const;
    void removeKey();

 private:
    bool _newPlayer, _bound, _oldLocalPlayer;
    uint _id, _oldLocalId;
    TQMemArray<uint> _histogram;
};

//-----------------------------------------------------------------------------
class ManagerPrivate
{
 public:
    ManagerPrivate(uint nbGameTypes, Manager &manager);
    void init(uint maxNbentries);
    ~ManagerPrivate();

    bool modifySettings(const TQString &newName, const TQString &comment,
                        bool WWEnabled, TQWidget *widget);

    void setGameType(uint type);
    void checkFirst();
    int submitLocal(const Score &score);
    int submitScore(const Score &score, TQWidget *widget, bool askIfAnonymous);
    Score readScore(uint i) const;

    uint gameType() const        { return _gameType; }
    uint nbGameTypes() const     { return _nbGameTypes; }
    bool isWWHSAvailable() const { return !serverURL.isEmpty(); }
    ScoreInfos &scoreInfos()     { return *_scoreInfos; }
    PlayerInfos &playerInfos()   { return *_playerInfos; }
    KHighscore &hsConfig()       { return *_hsConfig; }
    enum QueryType { Submit, Register, Change, Players, Scores };
    KURL queryURL(QueryType type, const TQString &newName=TQString::null) const;

    void exportHighscores(TQTextStream &);

    Manager &manager;
    KURL     serverURL;
    TQString  version;
    bool     showStatistics, showDrawGames, trackLostGames, trackDrawGames;
    Manager::ShowMode showMode;

 private:
    KHighscore   *_hsConfig;
    PlayerInfos  *_playerInfos;
    ScoreInfos   *_scoreInfos;
    bool          _first;
    const uint    _nbGameTypes;
    uint          _gameType;

    // return -1 if not a local best score
    int rank(const Score &score) const;

    bool submitWorldWide(const Score &score, TQWidget *parent) const;
    static bool doQuery(const KURL &url, TQWidget *parent,
                        TQDomNamedNodeMap *map = 0);
    static bool getFromQuery(const TQDomNamedNodeMap &map, const TQString &name,
                             TQString &value, TQWidget *parent);
    void convertToGlobal();
};

} // namespace

#endif