• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkdegames/highscore

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
  • highscore
khighscore.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
4  Copyright (C) 2003 Nicolas Hadacek <hadacek@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "khighscore.h"
22 #include <config-highscore.h>
23 
24 #include <KConfig>
25 #include <KGlobal>
26 #include <KStandardGuiItem>
27 #include <KLocale>
28 #include <KMessageBox>
29 #include <KDebug>
30 #include <KLockFile>
31 #include <KConfigGroup>
32 
33 #include <QtCore/QFile>
34 
35 #include <unistd.h> // sleep
36 
37 #define GROUP "KHighscore"
38 
39 class KHighscore::KHighscorePrivate
40 {
41 public:
42  KHighscorePrivate() {}
43 
44  QString group;
45  bool global;
46 };
47 
48 class KHighscoreLockedConfig
49 {
50 public:
51  ~KHighscoreLockedConfig();
52  KLockFile *lock;
53  KConfig *config;
54 };
55 
56 KHighscoreLockedConfig::~KHighscoreLockedConfig()
57 {
58  delete lock;
59  delete config;
60 }
61 
62 K_GLOBAL_STATIC(KHighscoreLockedConfig, lockedConfig)
63 
64 KHighscore::KHighscore(bool forceLocal, QObject* parent)
65  : QObject(parent), d(new KHighscorePrivate)
66 {
67  init(forceLocal);
68 }
69 
70 void KHighscore::init(bool forceLocal)
71 {
72 #ifdef HIGHSCORE_DIRECTORY
73  d->global = !forceLocal;
74  if ( d->global && lockedConfig->lock==0 ) //If we're doing global highscores but not KFileLock has been set up yet
75  kFatal(11002) << "KHighscore::init should be called before!!";
76 #else
77  d->global = false;
78  Q_UNUSED(forceLocal);
79 #endif
80  readCurrentConfig();
81 }
82 
83 bool KHighscore::isLocked() const
84 {
85  return (d->global ? lockedConfig->lock->isLocked() : true);
86 }
87 
88 void KHighscore::readCurrentConfig()
89 {
90  if ( d->global ) lockedConfig->config->reparseConfiguration();
91 }
92 
93 void KHighscore::init(const char *appname)
94 {
95 #ifdef HIGHSCORE_DIRECTORY
96  const QString filename = QString::fromLocal8Bit("%1/%2.scores")
97  .arg(HIGHSCORE_DIRECTORY).arg(appname);
98  //int fd = fopen(filename.toLocal8Bit(), O_RDWR);
99  /*QFile file(filename);
100  if ( !file.open(QIODevice::ReadWrite) ) kFatal(11002) << "cannot open global highscore file \""
101  << filename << "\"";*/
102  /*if (!(QFile::permissions(filename) & QFile::WriteOwner)) kFatal(11002) << "cannot write to global highscore file \""
103  << filename << "\"";*/
104  kDebug() << "Global highscore file \"" << filename << "\"";
105  lockedConfig->lock = new KLockFile(filename);
106  lockedConfig->config = new KConfig(filename, KConfig::NoGlobals); // read-only (matt-?)
107 
108  // drop the effective gid
109 #ifdef __GNUC__
110  #warning not portable yet. Unix only. Actually it does not even work there yet.
111 #endif
112  int gid = getgid();
113  setregid(gid, gid);
114 #else
115  Q_UNUSED(appname);
116 #endif
117 }
118 
119 bool KHighscore::lockForWriting(QWidget *widget)
120 {
121  if ( isLocked() ) return true;
122 
123  bool first = true;
124  for (;;) {
125  kDebug(11002) << "try locking";
126  // lock the highscore file (it should exist)
127  int result = lockedConfig->lock->lock();
128  bool ok = ( result==0 );
129  kDebug(11002) << "locking system-wide highscore file res="
130  << result << " (ok=" << ok << ")";
131  if (ok) {
132  readCurrentConfig();
133  return true;
134  }
135 
136  if ( !first ) {
137  KGuiItem item = KStandardGuiItem::cont();
138  item.setText(i18n("Retry"));
139  int res = KMessageBox::warningContinueCancel(widget, i18n("Cannot access the highscore file. Another user is probably currently writing to it."), QString(), item, KStandardGuiItem::cancel(), QLatin1String( "ask_lock_global_highscore_file" ));
140  if ( res==KMessageBox::Cancel ) break;
141  } else sleep(1);
142  first = false;
143  }
144  return false;
145 }
146 
147 void KHighscore::writeAndUnlock()
148 {
149  if ( !d->global ) {
150  KGlobal::config()->sync();
151  return;
152  }
153  if ( !isLocked() ) return;
154 
155  kDebug(11002) << "unlocking";
156  lockedConfig->config->sync(); // write config
157  lockedConfig->lock->unlock();
158 }
159 
160 KHighscore::~KHighscore()
161 {
162  writeAndUnlock();
163  delete d;
164 }
165 
166 KConfig* KHighscore::config() const
167 {
168  return (d->global ? lockedConfig->config : static_cast<KConfig*>(KGlobal::config().data()));
169 }
170 
171 void KHighscore::writeEntry(int entry, const QString& key, const QVariant& value)
172 {
173  Q_ASSERT( isLocked() );
174  KConfigGroup cg(config(), group());
175  QString confKey = QString::fromLatin1( "%1_%2").arg(entry).arg(key);
176  cg.writeEntry(confKey, value);
177 }
178 
179 void KHighscore::writeEntry(int entry, const QString& key, int value)
180 {
181  Q_ASSERT( isLocked() );
182  KConfigGroup cg(config(), group());
183  QString confKey = QString::fromLatin1( "%1_%2").arg(entry).arg(key);
184  cg.writeEntry(confKey, value);
185 }
186 
187 void KHighscore::writeEntry(int entry, const QString& key, const QString &value)
188 {
189  Q_ASSERT (isLocked() );
190  KConfigGroup cg(config(), group());
191  QString confKey = QString::fromLatin1( "%1_%2").arg(entry).arg(key);
192  cg.writeEntry(confKey, value);
193 }
194 
195 QVariant KHighscore::readPropertyEntry(int entry, const QString& key, const QVariant& pDefault) const
196 {
197  KConfigGroup cg(config(), group());
198  QString confKey = QString::fromLatin1( "%1_%2").arg(entry).arg(key);
199  return cg.readEntry(confKey, pDefault);
200 }
201 
202 QString KHighscore::readEntry(int entry, const QString& key, const QString& pDefault) const
203 {
204  KConfigGroup cg(config(), group());
205  QString confKey = QString::fromLatin1( "%1_%2").arg(entry).arg(key);
206  return cg.readEntry(confKey, pDefault);
207 }
208 
209 int KHighscore::readNumEntry(int entry, const QString& key, int pDefault) const
210 {
211  KConfigGroup cg(config(), group());
212  QString confKey = QString::fromLatin1( "%1_%2").arg(entry).arg(key);
213  return cg.readEntry(confKey, pDefault);
214 }
215 
216 bool KHighscore::hasEntry(int entry, const QString& key) const
217 {
218  KConfigGroup cg(config(), group());
219  QString confKey = QString::fromLatin1( "%1_%2").arg(entry).arg(key);
220  return cg.hasKey(confKey);
221 }
222 
223 QStringList KHighscore::readList(const QString& key, int lastEntry) const
224 {
225  QStringList list;
226  for (int i = 1; hasEntry(i, key) && ((lastEntry > 0) ? (i <= lastEntry) : true); i++) {
227  list.append(readEntry(i, key));
228  }
229  return list;
230 }
231 
232 void KHighscore::writeList(const QString& key, const QStringList& list)
233 {
234  for (int i = 1; i <= list.count(); i++) {
235  writeEntry(i, key, list[i - 1]);
236  }
237 }
238 
239 void KHighscore::setHighscoreGroup(const QString& group)
240 {
241  d->group = group;
242 }
243 
244 QString KHighscore::highscoreGroup() const
245 {
246  return d->group;
247 }
248 
249 QStringList KHighscore::groupList() const
250 {
251  QStringList groupList = config()->groupList();
252  QStringList highscoreGroupList;
253  foreach (QString group, groupList) //krazy:exclude=foreach
254  {
255  if(group.contains( QLatin1String( "KHighscore")) ) //If it's one of _our_ groups (KHighscore or KHighscore_xxx)
256  {
257  if(group == QLatin1String( "KHighscore" ))
258  group.remove( QLatin1String( "KHighscore" )); //Set to blank
259  else
260  group.remove( QLatin1String( "KHighscore_" )); //Remove the KHighscore_ prefix
261  highscoreGroupList << group;
262  }
263  }
264 
265  return highscoreGroupList;
266 }
267 
268 QString KHighscore::group() const
269 {
270  if (highscoreGroup().isEmpty())
271  return (d->global ? QString() : QString::fromLatin1(GROUP));
272  return (d->global ?
273  highscoreGroup() :
274  QString::fromLatin1( "%1_%2").arg( QLatin1String( GROUP )).arg(highscoreGroup()));
275 }
276 
277 bool KHighscore::hasTable() const
278 {
279  return config()->hasGroup(group());
280 }
281 
282 #include "khighscore.moc"
KHighscore
Class for managing highscore tables.
Definition: khighscore.h:82
KHighscore::readList
QStringList readList(const QString &key, int lastEntry=20) const
Reads a list of entries from the highscore table starting at 1 until lastEntry.
Definition: khighscore.cpp:223
KHighscore::~KHighscore
~KHighscore()
Destructor.
Definition: khighscore.cpp:160
QWidget
KHighscore::setHighscoreGroup
void setHighscoreGroup(const QString &groupname=QLatin1String(""))
Set the new highscore group.
Definition: khighscore.cpp:239
KHighscore::isLocked
bool isLocked() const
Definition: khighscore.cpp:83
QString::remove
QString & remove(int position, int n)
KHighscore::readNumEntry
int readNumEntry(int entry, const QString &key, int pDefault=-1) const
Read a numeric value.
Definition: khighscore.cpp:209
KHighscore::group
QString group() const
Definition: khighscore.cpp:268
KHighscore::hasEntry
bool hasEntry(int entry, const QString &key) const
Definition: khighscore.cpp:216
QList::count
int count(const T &value) const
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
QList::append
void append(const T &value)
khighscore.h
QObject
KHighscore::init
static void init(const char *appname)
This method open the system-wide highscore file using the effective group id of the game executable (...
Definition: khighscore.cpp:93
KHighscore::groupList
QStringList groupList() const
Returns a list of group names without the KHighscore_ prexix.
Definition: khighscore.cpp:249
KHighscore::hasTable
bool hasTable() const
You can use this function to indicate whether KHighscore created a highscore table before and - if no...
Definition: khighscore.cpp:277
KHighscore::writeAndUnlock
void writeAndUnlock()
Effectively write and unlock the system-wide highscore file (.
Definition: khighscore.cpp:147
QString
QStringList
KHighscore::readPropertyEntry
QVariant readPropertyEntry(int entry, const QString &key, const QVariant &pDefault) const
Read a QVariant entry.
Definition: khighscore.cpp:195
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
KConfigGroup
KHighscore::highscoreGroup
QString highscoreGroup() const
Definition: khighscore.cpp:244
KHighscore::readEntry
QString readEntry(int entry, const QString &key, const QString &pDefault=QLatin1String("")) const
Reads an entry from the highscore table.
Definition: khighscore.cpp:202
QLatin1String
KHighscore::writeList
void writeList(const QString &key, const QStringList &list)
Writes a list of entries to the highscore table.
Definition: khighscore.cpp:232
KHighscore::readCurrentConfig
void readCurrentConfig()
Read the current state of the highscore file.
Definition: khighscore.cpp:88
QString::fromLatin1
QString fromLatin1(const char *str, int size)
GROUP
#define GROUP
Definition: khighscore.cpp:37
KHighscore::config
KConfig * config() const
Definition: khighscore.cpp:166
KHighscore::lockForWriting
bool lockForWriting(QWidget *widget=0)
Lock the system-wide highscore file for writing (does nothing and return true if the local file is us...
Definition: khighscore.cpp:119
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
KHighscore::writeEntry
void writeEntry(int entry, const QString &key, const QString &value)
Definition: khighscore.cpp:187
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:46 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames/highscore

Skip menu "libkdegames/highscore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal