File: kmahjongglayout.cpp

package info (click to toggle)
kmahjongg 4:4.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,968 kB
  • ctags: 427
  • sloc: cpp: 3,945; makefile: 9; sh: 2
file content (129 lines) | stat: -rw-r--r-- 3,665 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
    kmahjongg, the classic mahjongg game for KDE
    Copyright (C) 2007 Mauricio Piacentini   <mauricio@tabuleiro.com>

    Kmahjongg is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "kmahjongglayout.h"
#include "BoardLayout.h"
#include <kstandarddirs.h>
#include <klocale.h>
#include <kconfig.h>
#include <kconfiggroup.h>
#include <QFile>
#include <QMap>
#include <KDebug>

class KMahjonggLayoutPrivate
{
public:
    KMahjonggLayoutPrivate()
    {
      board = new BoardLayout();
    }
    ~KMahjonggLayoutPrivate()
    {
      delete board;
    }

    BoardLayout * board;
    QMap<QString, QString> authorproperties;
    QString filename;
};

KMahjonggLayout::KMahjonggLayout()
    : d(new KMahjonggLayoutPrivate)
{
    static bool _inited = false;
    if (_inited)
        return;
    KGlobal::dirs()->addResourceType("kmahjongglayout", "data", QString::fromLatin1("kmahjongg/layouts/"));

    _inited = true;
}

KMahjonggLayout::~KMahjonggLayout() {
    delete d;
}

bool KMahjonggLayout::loadDefault()
{
    QString idx = "default.desktop";

    QString layoutPath = KStandardDirs::locate("kmahjongglayout", idx);
    kDebug() << "Inside LoadDefault(), located layout at" << layoutPath;
    if (layoutPath.isEmpty()) {
		return false;
    }
    return load(layoutPath);
}

#define kLayoutVersionFormat 1

bool KMahjonggLayout::load(const QString &file) {
    kDebug() << "Layout loading";

    QString layoutPath;
    kDebug() << "Attempting to load .desktop at" << file;

    // verify if it is a valid file first and if we can open it
    QFile bgfile(file);
    if (!bgfile.open(QIODevice::ReadOnly)) {
      return (false);
    }
    bgfile.close();

    KConfig bgconfig(file, KConfig::SimpleConfig);
    KConfigGroup group = bgconfig.group("KMahjonggLayout");

    d->authorproperties.insert("Name", group.readEntry("Name"));// Returns translated data
    d->authorproperties.insert("Author", group.readEntry("Author"));
    d->authorproperties.insert("Description", group.readEntry("Description"));
    d->authorproperties.insert("AuthorEmail", group.readEntry("AuthorEmail"));

    //Version control
    int bgversion = group.readEntry("VersionFormat",0);
    //Format is increased when we have incompatible changes, meaning that older clients are not able to use the remaining information safely
    if (bgversion > kLayoutVersionFormat) {
        return false;
    }

    QString layoutName = group.readEntry("FileName");

    layoutPath = KStandardDirs::locate("kmahjongglayout", layoutName);
    kDebug() << "Using layout at" << layoutPath;
    d->filename = layoutPath;

    if (layoutPath.isEmpty()) return (false);
    
    if (!d->board->loadBoardLayout(d->filename)) return (false);
    
    filename = file;

   return true;
}

BoardLayout * KMahjonggLayout::board() { 
  return d->board; 
}

QString KMahjonggLayout::path() const {
    return filename;
}

QString KMahjonggLayout::authorProperty(const QString &key) const {
    return d->authorproperties[key];
}