kmahjongg
kmahjongglayout.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "kmahjongglayout.h"
00021 #include "BoardLayout.h"
00022 #include <kstandarddirs.h>
00023 #include <klocale.h>
00024 #include <kconfig.h>
00025 #include <kconfiggroup.h>
00026 #include <QFile>
00027 #include <QMap>
00028 #include <KDebug>
00029
00030 class KMahjonggLayoutPrivate
00031 {
00032 public:
00033 KMahjonggLayoutPrivate()
00034 {
00035 board = new BoardLayout();
00036 }
00037 ~KMahjonggLayoutPrivate()
00038 {
00039 delete board;
00040 }
00041
00042 BoardLayout * board;
00043 QMap<QString, QString> authorproperties;
00044 QString filename;
00045 };
00046
00047 KMahjonggLayout::KMahjonggLayout()
00048 : d(new KMahjonggLayoutPrivate)
00049 {
00050 static bool _inited = false;
00051 if (_inited)
00052 return;
00053 KGlobal::dirs()->addResourceType("kmahjongglayout", "data", QString::fromLatin1("kmahjongg/layouts/"));
00054
00055 _inited = true;
00056 }
00057
00058 KMahjonggLayout::~KMahjonggLayout() {
00059 delete d;
00060 }
00061
00062 bool KMahjonggLayout::loadDefault()
00063 {
00064 QString idx = "default.desktop";
00065
00066 QString layoutPath = KStandardDirs::locate("kmahjongglayout", idx);
00067 kDebug() << "Inside LoadDefault(), located layout at" << layoutPath;
00068 if (layoutPath.isEmpty()) {
00069 return false;
00070 }
00071 return load(layoutPath);
00072 }
00073
00074 #define kLayoutVersionFormat 1
00075
00076 bool KMahjonggLayout::load(const QString &file) {
00077 kDebug() << "Layout loading";
00078
00079 QString layoutPath;
00080 kDebug() << "Attempting to load .desktop at" << file;
00081
00082
00083 QFile bgfile(file);
00084 if (!bgfile.open(QIODevice::ReadOnly)) {
00085 return (false);
00086 }
00087 bgfile.close();
00088
00089 KConfig bgconfig(file, KConfig::SimpleConfig);
00090 KConfigGroup group = bgconfig.group("KMahjonggLayout");
00091
00092 d->authorproperties.insert("Name", group.readEntry("Name"));
00093 d->authorproperties.insert("Author", group.readEntry("Author"));
00094 d->authorproperties.insert("Description", group.readEntry("Description"));
00095 d->authorproperties.insert("AuthorEmail", group.readEntry("AuthorEmail"));
00096
00097
00098 int bgversion = group.readEntry("VersionFormat",0);
00099
00100 if (bgversion > kLayoutVersionFormat) {
00101 return false;
00102 }
00103
00104 QString layoutName = group.readEntry("FileName");
00105
00106 layoutPath = KStandardDirs::locate("kmahjongglayout", layoutName);
00107 kDebug() << "Using layout at" << layoutPath;
00108 d->filename = layoutPath;
00109
00110 if (layoutPath.isEmpty()) return (false);
00111
00112 if (!d->board->loadBoardLayout(d->filename)) return (false);
00113
00114 filename = file;
00115
00116 return true;
00117 }
00118
00119 BoardLayout * KMahjonggLayout::board() {
00120 return d->board;
00121 }
00122
00123 QString KMahjonggLayout::path() const {
00124 return filename;
00125 }
00126
00127 QString KMahjonggLayout::authorProperty(const QString &key) const {
00128 return d->authorproperties[key];
00129 }