File indexing completed on 2022-09-27 13:16:00

0001 /*
0002     SPDX-FileCopyrightText: 2001-2004 Nicolas Hadacek <hadacek@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KEXTHIGHSCORE_INTERNAL_H
0008 #define KEXTHIGHSCORE_INTERNAL_H
0009 
0010 #include "kexthighscore.h"
0011 
0012 #include <ki18n_version.h>
0013 #include <KConfig>
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 #include <KHighscore>
0018 #include <KLazyLocalizedString>
0019 
0020 #include <QDateTime>
0021 #include <QTextStream>
0022 #include <QUrl>
0023 #include <QVector>
0024 
0025 class QDomNamedNodeMap;
0026 
0027 
0028 namespace KExtHighscore
0029 {
0030 
0031 class PlayerInfos;
0032 class Score;
0033 class Manager;
0034 
0035 
0036 //-----------------------------------------------------------------------------
0037 class RankItem : public Item
0038 {
0039  public:
0040     RankItem()
0041         : Item((uint)0, i18n("Rank"), Qt::AlignRight) {}
0042 
0043     QVariant read(uint i, const QVariant &value) const  override { Q_UNUSED(value); return i; }
0044     QString pretty(uint i, const QVariant &value) const override
0045         { Q_UNUSED(value); return QString::number(i+1); }
0046 };
0047 
0048 class NameItem : public Item
0049 {
0050  public:
0051     NameItem()
0052         : Item(QString(), i18n("Name"), Qt::AlignLeft) {
0053             setPrettySpecial(Anonymous);
0054     }
0055 };
0056 
0057 class DateItem : public Item
0058 {
0059  public:
0060     DateItem()
0061         : Item(QDateTime(), i18n("Date"), Qt::AlignRight) {
0062             setPrettyFormat(DateTime);
0063     }
0064 };
0065 
0066 class SuccessPercentageItem : public Item
0067 {
0068  public:
0069     SuccessPercentageItem()
0070         : Item((double)-1, i18n("Success"), Qt::AlignRight) {
0071             setPrettyFormat(Percentage);
0072             setPrettySpecial(NegativeNotDefined);
0073     }
0074 };
0075 
0076 //-----------------------------------------------------------------------------
0077 class ItemContainer
0078 {
0079  public:
0080     ItemContainer();
0081     ~ItemContainer();
0082 
0083     void setItem(Item *item);
0084     const Item *item() const { return _item; }
0085     Item *item() { return _item; }
0086 
0087     void setName(const QString &name) { _name = name; }
0088     QString name() const { return _name; }
0089 
0090     void setGroup(const QString &group) { _group = group; }
0091     bool isStored() const { return !_group.isNull(); }
0092 
0093     void setSubGroup(const QString &subGroup) { _subGroup = subGroup; }
0094     bool canHaveSubGroup() const { return !_subGroup.isNull(); }
0095 
0096     static const char ANONYMOUS[]; // name assigned to anonymous players
0097     static const KLazyLocalizedString ANONYMOUS_LABEL;
0098 
0099     QVariant read(uint i) const;
0100     QString pretty(uint i) const;
0101     void write(uint i, const QVariant &value) const;
0102     // for UInt QVariant (return new value)
0103     uint increment(uint i) const;
0104 
0105  private:
0106     Item    *_item;
0107     QString  _name, _group, _subGroup;
0108 
0109     QString entryName() const;
0110 
0111     ItemContainer(const ItemContainer &);
0112     ItemContainer &operator =(const ItemContainer &);
0113 };
0114 
0115 //-----------------------------------------------------------------------------
0116 /**
0117  * Manage a bunch of @ref Item which are saved under the same group
0118  * in KHighscores config file.
0119  */
0120 class ItemArray : public QVector<ItemContainer *>
0121 {
0122  public:
0123     ItemArray();
0124     virtual ~ItemArray();
0125 
0126     virtual uint nbEntries() const = 0;
0127 
0128     const ItemContainer *item(const QString &name) const;
0129     ItemContainer *item(const QString &name);
0130 
0131     void addItem(const QString &name, Item *, bool stored = true,
0132                  bool canHaveSubGroup = false);
0133     void setItem(const QString &name, Item *);
0134     int findIndex(const QString &name) const;
0135 
0136     void setGroup(const QString &group);
0137     void setSubGroup(const QString &subGroup);
0138 
0139     void read(uint k, Score &data) const;
0140     void write(uint k, const Score &data, uint maxNbLines) const;
0141 
0142     void exportToText(QTextStream &) const;
0143 
0144  private:
0145     QString _group, _subGroup;
0146 
0147     void _setItem(uint i, const QString &name, Item *, bool stored,
0148                   bool canHaveSubGroup);
0149 
0150     ItemArray(const ItemArray &);
0151     ItemArray &operator =(const ItemArray &);
0152 };
0153 
0154 //-----------------------------------------------------------------------------
0155 class ScoreInfos : public ItemArray
0156 {
0157  public:
0158     ScoreInfos(uint maxNbEntries, const PlayerInfos &infos);
0159 
0160     uint nbEntries() const override;
0161     uint maxNbEntries() const { return _maxNbEntries; }
0162 
0163  private:
0164     uint _maxNbEntries;
0165 };
0166 
0167 //-----------------------------------------------------------------------------
0168 class ConfigGroup : public KConfigGroup
0169 {
0170  public:
0171     explicit ConfigGroup(const QString &group = QLatin1String( "" ))
0172         : KConfigGroup(KSharedConfig::openConfig(), group) {}
0173 };
0174 
0175 //-----------------------------------------------------------------------------
0176 class PlayerInfos : public ItemArray
0177 {
0178  public:
0179     PlayerInfos();
0180 
0181     bool isNewPlayer() const { return _newPlayer; }
0182     bool isOldLocalPlayer() const { return _oldLocalPlayer; }
0183     uint nbEntries() const override;
0184     QString name() const { return item(QStringLiteral( "name" ))->read(_id).toString(); }
0185     bool isAnonymous() const;
0186     QString prettyName() const { return prettyName(_id); }
0187     QString prettyName(uint id) const { return item(QStringLiteral( "name" ))->pretty(id); }
0188     QString registeredName() const;
0189     QString comment() const { return item(QStringLiteral( "comment" ))->pretty(_id); }
0190     bool isWWEnabled() const;
0191     QString key() const;
0192     uint id() const { return _id; }
0193     uint oldLocalId() const { return _oldLocalId; }
0194 
0195     void createHistoItems(const QVector<uint> &scores, bool bound);
0196     QString histoName(int i) const;
0197     int histoSize() const;
0198     const QVector<uint> &histogram() const { return _histogram; }
0199 
0200     void submitScore(const Score &) const;
0201     // return true if the nickname is already used locally
0202     bool isNameUsed(const QString &name) const;
0203     void modifyName(const QString &newName) const;
0204     void modifySettings(const QString &newName, const QString &comment,
0205                         bool WWEnabled, const QString &newKey) const;
0206     void removeKey();
0207 
0208  private:
0209     bool _newPlayer, _bound, _oldLocalPlayer;
0210     uint _id, _oldLocalId;
0211     QVector<uint> _histogram;
0212 };
0213 
0214 //-----------------------------------------------------------------------------
0215 class ManagerPrivate
0216 {
0217  public:
0218     ManagerPrivate(uint nbGameTypes, Manager &manager);
0219     void init(uint maxNbentries);
0220     ~ManagerPrivate();
0221 
0222     bool modifySettings(const QString &newName, const QString &comment,
0223                         bool WWEnabled, QWidget *widget);
0224 
0225     void setGameType(uint type);
0226     void checkFirst();
0227     int submitLocal(const Score &score);
0228     int submitScore(const Score &score, QWidget *widget, bool askIfAnonymous);
0229     Score readScore(uint i) const;
0230 
0231     uint gameType() const        { return _gameType; }
0232     uint nbGameTypes() const     { return _nbGameTypes; }
0233     bool isWWHSAvailable() const { return !serverURL.isEmpty(); }
0234     ScoreInfos &scoreInfos()     { return *_scoreInfos; }
0235     PlayerInfos &playerInfos()   { return *_playerInfos; }
0236     KHighscore &hsConfig()       { return *_hsConfig; }
0237     enum QueryType { Submit, Register, Change, Players, Scores };
0238     QUrl queryUrl(QueryType type, const QString &newName = QLatin1String("")) const;
0239 
0240     void exportHighscores(QTextStream &);
0241 
0242     Manager &manager;
0243     QUrl     serverURL;
0244     QString  version;
0245     bool     showStatistics, showDrawGames, trackLostGames, trackDrawGames;
0246     Manager::ShowMode showMode;
0247 
0248  private:
0249     KHighscore   *_hsConfig;
0250     PlayerInfos  *_playerInfos;
0251     ScoreInfos   *_scoreInfos;
0252     bool          _first;
0253     const uint    _nbGameTypes;
0254     uint          _gameType;
0255 
0256     // return -1 if not a local best score
0257     int rank(const Score &score) const;
0258 
0259     bool submitWorldWide(const Score &score, QWidget *parent) const;
0260     static bool doQuery(const QUrl &url, QWidget *parent,
0261                         QDomNamedNodeMap *map = nullptr);
0262     static bool getFromQuery(const QDomNamedNodeMap &map, const QString &name,
0263                              QString &value, QWidget *parent);
0264     void convertToGlobal();
0265 };
0266 
0267 } // namespace
0268 
0269 #endif