29 #include <tdeapplication.h>
30 #include <ksimpleconfig.h>
31 #include <tdeglobal.h>
32 #include <kstdguiitem.h>
33 #include <tdelocale.h>
34 #include <tdemessagebox.h>
36 #include <kstaticdeleter.h>
38 #include "khighscore.h"
39 #include "tdeconfigrawbackend.h"
40 #include "tdefilelock.h"
42 #define GROUP "KHighscore"
44 class KHighscorePrivate
47 KHighscorePrivate() {}
53 KFileLock *KHighscore::_lock = 0;
54 KRawConfig *KHighscore::_config = 0;
55 static KStaticDeleter<KFileLock> lockSD;
56 static KStaticDeleter<KRawConfig> configSD;
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;
87 return (d->global ? _lock->isLocked() :
true);
92 if ( d->global ) _config->reparseConfiguration();
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));
120 kdDebug(11002) <<
"try locking" << endl;
122 int result = _lock->lock();
123 bool ok = ( result==0 );
124 kdDebug(11002) <<
"locking system-wide highscore file res="
125 << result <<
" (ok=" << ok <<
")" << endl;
128 _config->setReadOnly(
false);
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;
146 kapp->config()->sync();
151 kdDebug(11002) <<
"unlocking" << endl;
154 _config->setReadOnly(
true);
165 return (d->global ? _config : kapp->config());
172 TQString confKey = TQString(
"%1_%2").arg(entry).arg(key);
173 cg.config()->writeEntry(confKey, value);
180 TQString confKey = TQString(
"%1_%2").arg(entry).arg(key);
181 cg.config()->writeEntry(confKey, value);
188 TQString confKey = TQString(
"%1_%2").arg(entry).arg(key);
189 cg.config()->writeEntry(confKey, value);
195 TQString confKey = TQString(
"%1_%2").arg(entry).arg(key);
196 return cg.config()->readPropertyEntry(confKey, pDefault);
202 TQString confKey = TQString(
"%1_%2").arg(entry).arg(key);
203 return cg.config()->readEntry(confKey, pDefault);
209 TQString confKey = TQString(
"%1_%2").arg(entry).arg(key);
210 return cg.config()->readNumEntry(confKey, pDefault);
216 TQString confKey = TQString(
"%1_%2").arg(entry).arg(key);
217 return cg.config()->hasKey(confKey);
223 for (
int i = 1;
hasEntry(i, key) && ((lastEntry > 0) ? (i <= lastEntry) :
true); i++) {
231 for (
int unsigned i = 1; i <= list.count(); i++) {
249 return (d->global ? TQString() : GROUP);
262 #include "khighscore.moc"
bool lockForWriting(TQWidget *widget=0)
TQString readEntry(int entry, const TQString &key, const TQString &pDefault=TQString()) const
Reads an entry from the highscore table.
TDEConfig * config() const
TQStringList readList(const TQString &key, int lastEntry=20) const
Reads a list of entries from the highscore table starting at 1 until lastEntry.
void writeList(const TQString &key, const TQStringList &list)
Writes a list of entries to the highscore table.
TQVariant readPropertyEntry(int entry, const TQString &key, const TQVariant &pDefault) const
Read a TQVariant entry.
void writeEntry(int entry, const TQString &key, const TQString &value)
const TQString & highscoreGroup() const
KHighscore(TQObject *parent=0)
int readNumEntry(int entry, const TQString &key, int pDefault=-1) const
Read a numeric value.
static void init(const char *appname)
bool hasEntry(int entry, const TQString &key) const
void setHighscoreGroup(const TQString &groupname=TQString())
Set the new highscore group.