1 | /* |
2 | kmahjongg, the classic mahjongg game for KDE |
3 | Copyright (C) 2007 Mauricio Piacentini <mauricio@tabuleiro.com> |
4 | |
5 | Kmahjongg is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
9 | |
10 | This program 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 |
13 | GNU General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "kmahjongglayout.h" |
21 | #include "BoardLayout.h" |
22 | #include <kstandarddirs.h> |
23 | #include <klocale.h> |
24 | #include <kconfig.h> |
25 | #include <kconfiggroup.h> |
26 | #include <QFile> |
27 | #include <QMap> |
28 | #include <KDebug> |
29 | |
30 | class KMahjonggLayoutPrivate |
31 | { |
32 | public: |
33 | KMahjonggLayoutPrivate() |
34 | { |
35 | board = new BoardLayout(); |
36 | } |
37 | ~KMahjonggLayoutPrivate() |
38 | { |
39 | delete board; |
40 | } |
41 | |
42 | BoardLayout * board; |
43 | QMap<QString, QString> authorproperties; |
44 | QString filename; |
45 | }; |
46 | |
47 | KMahjonggLayout::KMahjonggLayout() |
48 | : d(new KMahjonggLayoutPrivate) |
49 | { |
50 | static bool _inited = false; |
51 | if (_inited) |
52 | return; |
53 | KGlobal::dirs()->addResourceType("kmahjongglayout" , "data" , QString::fromLatin1("kmahjongg/layouts/" )); |
54 | |
55 | _inited = true; |
56 | } |
57 | |
58 | KMahjonggLayout::~KMahjonggLayout() { |
59 | delete d; |
60 | } |
61 | |
62 | bool KMahjonggLayout::loadDefault() |
63 | { |
64 | QString idx = "default.desktop" ; |
65 | |
66 | QString layoutPath = KStandardDirs::locate("kmahjongglayout" , idx); |
67 | kDebug() << "Inside LoadDefault(), located layout at" << layoutPath; |
68 | if (layoutPath.isEmpty()) { |
69 | return false; |
70 | } |
71 | return load(layoutPath); |
72 | } |
73 | |
74 | #define kLayoutVersionFormat 1 |
75 | |
76 | bool KMahjonggLayout::load(const QString &file) { |
77 | kDebug() << "Layout loading" ; |
78 | |
79 | QString layoutPath; |
80 | kDebug() << "Attempting to load .desktop at" << file; |
81 | |
82 | // verify if it is a valid file first and if we can open it |
83 | QFile bgfile(file); |
84 | if (!bgfile.open(QIODevice::ReadOnly)) { |
85 | return (false); |
86 | } |
87 | bgfile.close(); |
88 | |
89 | KConfig bgconfig(file, KConfig::SimpleConfig); |
90 | KConfigGroup group = bgconfig.group("KMahjonggLayout" ); |
91 | |
92 | d->authorproperties.insert("Name" , group.readEntry("Name" ));// Returns translated data |
93 | d->authorproperties.insert("Author" , group.readEntry("Author" )); |
94 | d->authorproperties.insert("Description" , group.readEntry("Description" )); |
95 | d->authorproperties.insert("AuthorEmail" , group.readEntry("AuthorEmail" )); |
96 | |
97 | //Version control |
98 | int bgversion = group.readEntry("VersionFormat" ,0); |
99 | //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely |
100 | if (bgversion > kLayoutVersionFormat) { |
101 | return false; |
102 | } |
103 | |
104 | QString layoutName = group.readEntry("FileName" ); |
105 | |
106 | layoutPath = KStandardDirs::locate("kmahjongglayout" , layoutName); |
107 | kDebug() << "Using layout at" << layoutPath; |
108 | d->filename = layoutPath; |
109 | |
110 | if (layoutPath.isEmpty()) return (false); |
111 | |
112 | if (!d->board->loadBoardLayout(d->filename)) return (false); |
113 | |
114 | filename = file; |
115 | |
116 | return true; |
117 | } |
118 | |
119 | BoardLayout * KMahjonggLayout::board() { |
120 | return d->board; |
121 | } |
122 | |
123 | QString KMahjonggLayout::path() const { |
124 | return filename; |
125 | } |
126 | |
127 | QString KMahjonggLayout::authorProperty(const QString &key) const { |
128 | return d->authorproperties[key]; |
129 | } |
130 | |