File indexing completed on 2024-05-12 04:04:48

0001 /*
0002  * Copyright (C) 1995 Paul Olav Tvete <paul@troll.no>
0003  * Copyright (C) 2000-2009 Stephan Kulow <coolo@kde.org>
0004  * Copyright (C) 2009 Parker Coates <coates@kde.org>
0005  *
0006  * License of original code:
0007  * -------------------------------------------------------------------------
0008  *   Permission to use, copy, modify, and distribute this software and its
0009  *   documentation for any purpose and without fee is hereby granted,
0010  *   provided that the above copyright notice appear in all copies and that
0011  *   both that copyright notice and this permission notice appear in
0012  *   supporting documentation.
0013  *
0014  *   This file is provided AS IS with no warranties of any kind.  The author
0015  *   shall have no liability with respect to the infringement of copyrights,
0016  *   trade secrets or any patents by this file or any part thereof.  In no
0017  *   event will the author be liable for any lost revenue or profits or
0018  *   other special, indirect and consequential damages.
0019  * -------------------------------------------------------------------------
0020  *
0021  * License of modifications/additions made after 2009-01-01:
0022  * -------------------------------------------------------------------------
0023  *   This program is free software; you can redistribute it and/or
0024  *   modify it under the terms of the GNU General Public License as
0025  *   published by the Free Software Foundation; either version 2 of
0026  *   the License, or (at your option) any later version.
0027  *
0028  *   This program is distributed in the hope that it will be useful,
0029  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0030  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0031  *   GNU General Public License for more details.
0032  *
0033  *   You should have received a copy of the GNU General Public License
0034  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0035  * -------------------------------------------------------------------------
0036  */
0037 
0038 #include "dealerinfo.h"
0039 
0040 // KF
0041 #include <KLocalizedString>
0042 
0043 DealerInfo::DealerInfo(const KLazyLocalizedString &untranslatedBaseName, int baseId)
0044     : m_baseName(untranslatedBaseName)
0045     , m_baseId(baseId)
0046 {
0047     DealerInfoList::self()->add(this);
0048 
0049     QString baseName = QString::fromUtf8(m_baseName.untranslatedText());
0050     for (int i = 0; i < baseName.size(); ++i) {
0051         QChar c = baseName.at(i);
0052         if (c.isLetterOrNumber())
0053             m_baseIdString += c.toLower();
0054     }
0055 }
0056 
0057 DealerInfo::~DealerInfo()
0058 {
0059 }
0060 
0061 QString DealerInfo::baseName() const
0062 {
0063     return m_baseName.toString();
0064 }
0065 
0066 KLazyLocalizedString DealerInfo::untranslatedBaseName() const
0067 {
0068     return m_baseName;
0069 }
0070 
0071 QString DealerInfo::baseIdString() const
0072 {
0073     return m_baseIdString;
0074 }
0075 
0076 int DealerInfo::baseId() const
0077 {
0078     return m_baseId;
0079 }
0080 
0081 void DealerInfo::addSubtype(int id, const KLazyLocalizedString &untranslatedName)
0082 {
0083     m_subtypes.insert(id, untranslatedName);
0084 }
0085 
0086 QList<int> DealerInfo::subtypeIds() const
0087 {
0088     return m_subtypes.keys();
0089 }
0090 
0091 QList<int> DealerInfo::distinctIds() const
0092 {
0093     if (m_subtypes.isEmpty())
0094         return QList<int>() << m_baseId;
0095     else
0096         return m_subtypes.keys();
0097 }
0098 
0099 bool DealerInfo::providesId(int id) const
0100 {
0101     return id == m_baseId || m_subtypes.contains(id);
0102 }
0103 
0104 QString DealerInfo::nameForId(int id) const
0105 {
0106     if (id == m_baseId)
0107         return baseName();
0108 
0109     QMap<int, KLazyLocalizedString>::const_iterator it = m_subtypes.find(id);
0110     if (it != m_subtypes.constEnd())
0111         return it.value().toString();
0112     else
0113         return QString();
0114 }
0115 
0116 class DealerInfoListPrivate
0117 {
0118 public:
0119     DealerInfoList instance;
0120 };
0121 
0122 Q_GLOBAL_STATIC(DealerInfoListPrivate, dilp)
0123 
0124 DealerInfoList *DealerInfoList::self()
0125 {
0126     return &(dilp->instance);
0127 }
0128 
0129 DealerInfoList::DealerInfoList()
0130 {
0131 }
0132 
0133 DealerInfoList::~DealerInfoList()
0134 {
0135 }
0136 
0137 void DealerInfoList::add(DealerInfo *di)
0138 {
0139     m_list.append(di);
0140 }
0141 
0142 const QList<DealerInfo *> DealerInfoList::games() const
0143 {
0144     return m_list;
0145 }
0146 
0147 int DealerInfoList::gameIdForFile(QXmlStreamReader &xml) const
0148 {
0149     QStringView gameType = xml.attributes().value(QStringLiteral("game-type"));
0150     for (const DealerInfo *di : games()) {
0151         if (di->baseIdString() == gameType) {
0152             return di->baseId();
0153             break;
0154         }
0155     }
0156     return -1;
0157 }