• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • highscore
khighscore.cpp
1 /*
2  This file is part of the TDE 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  $Id$
22 */
23 
24 #include <config.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/file.h>
28 
29 #include <tdeapplication.h>
30 #include <ksimpleconfig.h>
31 #include <tdeglobal.h>
32 #include <kstdguiitem.h>
33 #include <tdelocale.h>
34 #include <tdemessagebox.h>
35 #include <kdebug.h>
36 #include <kstaticdeleter.h>
37 
38 #include "khighscore.h"
39 #include "tdeconfigrawbackend.h"
40 #include "tdefilelock.h"
41 
42 #define GROUP "KHighscore"
43 
44 class KHighscorePrivate
45 {
46 public:
47  KHighscorePrivate() {}
48 
49  TQString group;
50  bool global;
51 };
52 
53 KFileLock *KHighscore::_lock = 0;
54 KRawConfig *KHighscore::_config = 0;
55 static KStaticDeleter<KFileLock> lockSD;
56 static KStaticDeleter<KRawConfig> configSD;
57 
58 
59 KHighscore::KHighscore(TQObject* parent)
60  : TQObject(parent)
61 {
62  init(true);
63 }
64 
65 KHighscore::KHighscore(bool forceLocal, TQObject* parent)
66  : TQObject(parent)
67 {
68  init(forceLocal);
69 }
70 
71 void KHighscore::init(bool forceLocal)
72 {
73  d = new KHighscorePrivate;
74 #ifdef HIGHSCORE_DIRECTORY
75  d->global = !forceLocal;
76  if ( d->global && _lock==0 )
77  kdFatal(11002) << "KHighscore::init should be called before!!" << endl;
78 #else
79  d->global = false;
80  Q_UNUSED(forceLocal);
81 #endif
82  readCurrentConfig();
83 }
84 
85 bool KHighscore::isLocked() const
86 {
87  return (d->global ? _lock->isLocked() : true);
88 }
89 
90 void KHighscore::readCurrentConfig()
91 {
92  if ( d->global ) _config->reparseConfiguration();
93 }
94 
95 void KHighscore::init(const char *appname)
96 {
97 #ifdef HIGHSCORE_DIRECTORY
98  const TQString filename = TQString::fromLocal8Bit("%1/%2.scores")
99  .arg(HIGHSCORE_DIRECTORY).arg(appname);
100  int fd = open(filename.local8Bit(), O_RDWR);
101  if ( fd<0 ) kdFatal(11002) << "cannot open global highscore file \""
102  << filename << "\"" << endl;
103  lockSD.setObject(_lock, new KFileLock(fd));
104  configSD.setObject(_config, new KRawConfig(fd, true)); // read-only
105 
106  // drop the effective gid
107  int gid = getgid();
108  setregid(gid, gid);
109 #else
110  Q_UNUSED(appname);
111 #endif
112 }
113 
114 bool KHighscore::lockForWriting(TQWidget *widget)
115 {
116  if ( isLocked() ) return true;
117 
118  bool first = true;
119  for (;;) {
120  kdDebug(11002) << "try locking" << endl;
121  // lock the highscore file (it should exist)
122  int result = _lock->lock();
123  bool ok = ( result==0 );
124  kdDebug(11002) << "locking system-wide highscore file res="
125  << result << " (ok=" << ok << ")" << endl;
126  if (ok) {
127  readCurrentConfig();
128  _config->setReadOnly(false);
129  return true;
130  }
131 
132  if ( !first ) {
133  KGuiItem item = KStdGuiItem::cont();
134  item.setText(i18n("Retry"));
135  int res = KMessageBox::warningContinueCancel(widget, i18n("Cannot access the highscore file. Another user is probably currently writing to it."), TQString(), item, "ask_lock_global_highscore_file");
136  if ( res==KMessageBox::Cancel ) break;
137  } else sleep(1);
138  first = false;
139  }
140  return false;
141 }
142 
143 void KHighscore::writeAndUnlock()
144 {
145  if ( !d->global ) {
146  kapp->config()->sync();
147  return;
148  }
149  if ( !isLocked() ) return;
150 
151  kdDebug(11002) << "unlocking" << endl;
152  _config->sync(); // write config
153  _lock->unlock();
154  _config->setReadOnly(true);
155 }
156 
157 KHighscore::~KHighscore()
158 {
159  writeAndUnlock();
160  delete d;
161 }
162 
163 TDEConfig* KHighscore::config() const
164 {
165  return (d->global ? _config : kapp->config());
166 }
167 
168 void KHighscore::writeEntry(int entry, const TQString& key, const TQVariant& value)
169 {
170  Q_ASSERT( isLocked() );
171  TDEConfigGroupSaver cg(config(), group());
172  TQString confKey = TQString("%1_%2").arg(entry).arg(key);
173  cg.config()->writeEntry(confKey, value);
174 }
175 
176 void KHighscore::writeEntry(int entry, const TQString& key, int value)
177 {
178  Q_ASSERT( isLocked() );
179  TDEConfigGroupSaver cg(config(), group());
180  TQString confKey = TQString("%1_%2").arg(entry).arg(key);
181  cg.config()->writeEntry(confKey, value);
182 }
183 
184 void KHighscore::writeEntry(int entry, const TQString& key, const TQString &value)
185 {
186  Q_ASSERT (isLocked() );
187  TDEConfigGroupSaver cg(config(), group());
188  TQString confKey = TQString("%1_%2").arg(entry).arg(key);
189  cg.config()->writeEntry(confKey, value);
190 }
191 
192 TQVariant KHighscore::readPropertyEntry(int entry, const TQString& key, const TQVariant& pDefault) const
193 {
194  TDEConfigGroupSaver cg(config(), group());
195  TQString confKey = TQString("%1_%2").arg(entry).arg(key);
196  return cg.config()->readPropertyEntry(confKey, pDefault);
197 }
198 
199 TQString KHighscore::readEntry(int entry, const TQString& key, const TQString& pDefault) const
200 {
201  TDEConfigGroupSaver cg(config(), group());
202  TQString confKey = TQString("%1_%2").arg(entry).arg(key);
203  return cg.config()->readEntry(confKey, pDefault);
204 }
205 
206 int KHighscore::readNumEntry(int entry, const TQString& key, int pDefault) const
207 {
208  TDEConfigGroupSaver cg(config(), group());
209  TQString confKey = TQString("%1_%2").arg(entry).arg(key);
210  return cg.config()->readNumEntry(confKey, pDefault);
211 }
212 
213 bool KHighscore::hasEntry(int entry, const TQString& key) const
214 {
215  TDEConfigGroupSaver cg(config(), group());
216  TQString confKey = TQString("%1_%2").arg(entry).arg(key);
217  return cg.config()->hasKey(confKey);
218 }
219 
220 TQStringList KHighscore::readList(const TQString& key, int lastEntry) const
221 {
222  TQStringList list;
223  for (int i = 1; hasEntry(i, key) && ((lastEntry > 0) ? (i <= lastEntry) : true); i++) {
224  list.append(readEntry(i, key));
225  }
226  return list;
227 }
228 
229 void KHighscore::writeList(const TQString& key, const TQStringList& list)
230 {
231  for (int unsigned i = 1; i <= list.count(); i++) {
232  writeEntry(i, key, list[i - 1]);
233  }
234 }
235 
236 void KHighscore::setHighscoreGroup(const TQString& group)
237 {
238  d->group = group;
239 }
240 
241 const TQString& KHighscore::highscoreGroup() const
242 {
243  return d->group;
244 }
245 
246 TQString KHighscore::group() const
247 {
248  if ( highscoreGroup().isNull() )
249  return (d->global ? TQString() : GROUP);
250  return (d->global ? highscoreGroup()
251  : TQString("%1_%2").arg(GROUP).arg(highscoreGroup()));
252 }
253 
254 bool KHighscore::hasTable() const
255 { return config()->hasGroup(group()); }
256 
257 void KHighscore::sync()
258 {
259  writeAndUnlock();
260 }
261 
262 #include "khighscore.moc"
KHighscore::lockForWriting
bool lockForWriting(TQWidget *widget=0)
Definition: khighscore.cpp:114
KHighscore::sync
void sync()
Definition: khighscore.cpp:257
KHighscore::group
TQString group() const
Definition: khighscore.cpp:246
KHighscore::readEntry
TQString readEntry(int entry, const TQString &key, const TQString &pDefault=TQString()) const
Reads an entry from the highscore table.
Definition: khighscore.cpp:199
KHighscore::config
TDEConfig * config() const
Definition: khighscore.cpp:163
KHighscore::isLocked
bool isLocked() const
Definition: khighscore.cpp:85
KHighscore::readList
TQStringList readList(const TQString &key, int lastEntry=20) const
Reads a list of entries from the highscore table starting at 1 until lastEntry.
Definition: khighscore.cpp:220
KHighscore::writeList
void writeList(const TQString &key, const TQStringList &list)
Writes a list of entries to the highscore table.
Definition: khighscore.cpp:229
KHighscore::readPropertyEntry
TQVariant readPropertyEntry(int entry, const TQString &key, const TQVariant &pDefault) const
Read a TQVariant entry.
Definition: khighscore.cpp:192
KHighscore::writeEntry
void writeEntry(int entry, const TQString &key, const TQString &value)
Definition: khighscore.cpp:184
KHighscore::hasTable
bool hasTable() const
Definition: khighscore.cpp:254
KHighscore::highscoreGroup
const TQString & highscoreGroup() const
Definition: khighscore.cpp:241
KHighscore::KHighscore
KHighscore(TQObject *parent=0)
Definition: khighscore.cpp:59
KHighscore::writeAndUnlock
void writeAndUnlock()
Definition: khighscore.cpp:143
KHighscore::readNumEntry
int readNumEntry(int entry, const TQString &key, int pDefault=-1) const
Read a numeric value.
Definition: khighscore.cpp:206
KHighscore::init
static void init(const char *appname)
Definition: khighscore.cpp:95
KHighscore::readCurrentConfig
void readCurrentConfig()
Definition: khighscore.cpp:90
KHighscore::hasEntry
bool hasEntry(int entry, const TQString &key) const
Definition: khighscore.cpp:213
KHighscore::~KHighscore
~KHighscore()
Destructor.
Definition: khighscore.cpp:157
KHighscore::setHighscoreGroup
void setHighscoreGroup(const TQString &groupname=TQString())
Set the new highscore group.
Definition: khighscore.cpp:236

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.1
This website is maintained by Timothy Pearson.