File indexing completed on 2024-04-14 03:59:52

0001 /*
0002     SPDX-FileCopyrightText: 2007 Mauricio Piacentini <mauricio@tabuleiro.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 // own
0008 #include "kmahjongglayout.h"
0009 
0010 // Qt
0011 #include <QFile>
0012 #include <QStandardPaths>
0013 
0014 // KF
0015 #include <KConfig>
0016 #include <KConfigGroup>
0017 
0018 // KMahjongg
0019 #include "boardlayout.h"
0020 #include "kmahjongg_debug.h"
0021 
0022 namespace {
0023 constexpr int kLayoutVersionFormat = 1;
0024 }
0025 
0026 KMahjonggLayout::KMahjonggLayout()
0027     : m_board(new BoardLayout())
0028 {
0029     static bool s_inited = false;
0030     if (s_inited) {
0031         return;
0032     }
0033 
0034     s_inited = true;
0035 }
0036 
0037 KMahjonggLayout::~KMahjonggLayout() = default;
0038 
0039 bool KMahjonggLayout::loadDefault()
0040 {
0041     const QString subdir = QStringLiteral("/layouts/");
0042     const QString layoutFileName = QStringLiteral("default.desktop");
0043 
0044     const QString layoutPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, subdir + layoutFileName);
0045     qCDebug(KMAHJONGG_LOG) << "Inside LoadDefault(), located layout at " << layoutPath;
0046     if (layoutPath.isEmpty()) {
0047         return false;
0048     }
0049     return load(layoutPath);
0050 }
0051 
0052 bool KMahjonggLayout::load(const QString & file)
0053 {
0054     // verify if it is a valid file first and if we can open it
0055     QFile bgfile(file);
0056     if (!bgfile.open(QIODevice::ReadOnly)) {
0057         m_name.clear();
0058         m_description.clear();
0059         m_authorName.clear();
0060         m_authorEmailAddress.clear();
0061         return false;
0062     }
0063     bgfile.close();
0064 
0065     KConfig bgconfig(file, KConfig::SimpleConfig);
0066     KConfigGroup group = bgconfig.group(QStringLiteral("KMahjonggLayout"));
0067 
0068     m_name = group.readEntry("Name"); // Returns translated data
0069     m_description = group.readEntry("Description");
0070     m_authorName = group.readEntry("Author");
0071     m_authorEmailAddress = group.readEntry("AuthorEmail");
0072 
0073     // Version control
0074     const int bgversion = group.readEntry("VersionFormat", 0);
0075     // Format is increased when we have incompatible changes, meaning that older clients
0076     // are not able to use the remaining information safely
0077     if (bgversion > kLayoutVersionFormat) {
0078         return false;
0079     }
0080 
0081     const QString subdir = QStringLiteral("/layouts/");
0082     const QString layoutFileName = group.readEntry("FileName");
0083 
0084     const QString layoutPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, subdir + layoutFileName);
0085     qCDebug(KMAHJONGG_LOG) << "Using layout at" << layoutPath;
0086 
0087     if (layoutPath.isEmpty()) {
0088         return false;
0089     }
0090 
0091     if (!m_board->loadBoardLayout(layoutPath)) {
0092         return false;
0093     }
0094 
0095     m_fileName = file;
0096 
0097     return true;
0098 }
0099 
0100 BoardLayout * KMahjonggLayout::board() const
0101 {
0102     return m_board.get();
0103 }
0104 
0105 QString KMahjonggLayout::path() const
0106 {
0107     return m_fileName;
0108 }
0109 
0110 QString KMahjonggLayout::name() const
0111 {
0112     return m_name;
0113 }
0114 
0115 QString KMahjonggLayout::description() const
0116 {
0117     return m_description;
0118 }
0119 
0120 QString KMahjonggLayout::authorName() const
0121 {
0122     return m_authorName;
0123 }
0124 
0125 QString KMahjonggLayout::authorEmailAddress() const
0126 {
0127     return m_authorEmailAddress;
0128 }