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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "playersettings.h"
0008 #include "settings.h"
0009 
0010 #include <KConfig>
0011 #include <QDir>
0012 #include <QStandardPaths>
0013 
0014 PlayerSettings::PlayerSettings()
0015 {
0016     QStringList playersAvailable;
0017     QStringList playerFilesAvailable;
0018     QStringList playerNamesAvailable;
0019 
0020     const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::AppDataLocation, QStringLiteral("players"), QStandardPaths::LocateDirectory);
0021     for(const auto& dir: dirs) {
0022          const QStringList fileNames = QDir(dir).entryList({QStringLiteral("*.desktop")});
0023          for(const auto& file: fileNames) {
0024                 playersAvailable.append(file);
0025          }
0026     }
0027     
0028     playersAvailable.removeDuplicates();
0029     playersAvailable.sort();
0030     for(const auto& player: playersAvailable)
0031     {
0032         StructPlayerSettings settings;
0033         settings.strPlayerID = player;
0034 
0035         KConfig desktopFile(QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("players/") + settings.strPlayerID), KConfig::SimpleConfig);
0036 
0037         settings.strPlayerDesktopFilePath = desktopFile.name();
0038         settings.strPlayerName = desktopFile.group(QStringLiteral("KGameTheme")).readEntry<QString>("Name", QLatin1String(""));
0039         settings.strPlayerGraphicsFile = desktopFile.group(QStringLiteral("KGameTheme")).readEntry<QString>("FileName", QLatin1String(""));
0040         settings.enabled = false;
0041         
0042         m_playerSettings.insert(settings.strPlayerID, settings);
0043     }
0044     
0045     
0046     QStringList playersGroupList;
0047     QString strPlayerID;
0048     KConfig granatierConfig(QStringLiteral("granatierrc"), KConfig::NoGlobals );
0049     int nEnableCount = 0;
0050     if(granatierConfig.hasGroup(QStringLiteral("Player")))
0051     {
0052         playersGroupList = granatierConfig.group(QStringLiteral("Player")).groupList();
0053         playersGroupList.sort();
0054         for(const auto& player: playersGroupList)
0055         {
0056             strPlayerID = granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("PlayerID", QStringLiteral(""));
0057             if(m_playerSettings.contains(strPlayerID))
0058             {
0059                 m_playerSettings.find(strPlayerID).value().strPlayerName = granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("Name", QStringLiteral(""));
0060                 m_playerSettings.find(strPlayerID).value().enabled = granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<int>("Enabled", 0);
0061                 if(m_playerSettings.find(strPlayerID).value().enabled)
0062                 {
0063                     nEnableCount++;
0064                 }
0065                 
0066                 m_playerSettings.find(strPlayerID).value().keyUp = QKeySequence(granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("KeyUp", QStringLiteral("")));
0067                 m_playerSettings.find(strPlayerID).value().keyRight = QKeySequence(granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("KeyRight", QStringLiteral("")));
0068                 m_playerSettings.find(strPlayerID).value().keyDown = QKeySequence(granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("KeyDown", QStringLiteral("")));
0069                 m_playerSettings.find(strPlayerID).value().keyLeft = QKeySequence(granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("KeyLeft", QStringLiteral("")));
0070                 m_playerSettings.find(strPlayerID).value().keyPutBomb = QKeySequence(granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("KeyPutBomb", QStringLiteral("")));
0071             }
0072         }
0073     }
0074     
0075     auto settingsIterator = m_playerSettings.begin();
0076     while (nEnableCount < 2 && settingsIterator != m_playerSettings.end())
0077     {
0078         if(!(*settingsIterator).enabled)
0079         {
0080             (*settingsIterator).enabled = true;
0081             nEnableCount++;
0082         }
0083         ++settingsIterator;
0084     }
0085     
0086     int nFirstRun = granatierConfig.group(QStringLiteral("General")).readEntry<int>("FirstRun", 1);
0087     if(nFirstRun)
0088     {
0089         settingsIterator = m_playerSettings.begin();
0090         (*settingsIterator).keyUp = QKeySequence(Qt::Key_Up);
0091         (*settingsIterator).keyRight = QKeySequence(Qt::Key_Right);
0092         (*settingsIterator).keyDown = QKeySequence(Qt::Key_Down);
0093         (*settingsIterator).keyLeft = QKeySequence(Qt::Key_Left);
0094         (*settingsIterator).keyPutBomb = QKeySequence(Qt::Key_Return);
0095         ++settingsIterator;
0096         (*settingsIterator).keyUp = QKeySequence(Qt::Key_W);
0097         (*settingsIterator).keyRight = QKeySequence(Qt::Key_D);
0098         (*settingsIterator).keyDown = QKeySequence(Qt::Key_S);
0099         (*settingsIterator).keyLeft = QKeySequence(Qt::Key_A);
0100         (*settingsIterator).keyPutBomb = QKeySequence(Qt::Key_Q);
0101         ++settingsIterator;
0102         (*settingsIterator).keyUp = QKeySequence(Qt::Key_I);
0103         (*settingsIterator).keyRight = QKeySequence(Qt::Key_L);
0104         (*settingsIterator).keyDown = QKeySequence(Qt::Key_K);
0105         (*settingsIterator).keyLeft = QKeySequence(Qt::Key_J);
0106         (*settingsIterator).keyPutBomb = QKeySequence(Qt::Key_Space);
0107         m_pendingPlayerSettings = m_playerSettings;
0108         savePlayerSettings();
0109         granatierConfig.group(QStringLiteral("General")).writeEntry("FirstRun", 0);
0110     }
0111     
0112     m_pendingPlayerSettings = m_playerSettings;
0113 }
0114 
0115 PlayerSettings::~PlayerSettings()
0116 = default;
0117 
0118 const QStringList PlayerSettings::playerIDs() const
0119 {
0120     return m_playerSettings.keys();
0121 }
0122 
0123 const QString PlayerSettings::playerName(const QString& strPlayerID) const
0124 {
0125     return m_playerSettings.value(strPlayerID).strPlayerName;
0126 }
0127 
0128 const QString PlayerSettings::playerDesktopFilePath(const QString& strPlayerID) const
0129 {
0130   return m_playerSettings.value(strPlayerID).strPlayerDesktopFilePath;
0131 }
0132 
0133 const QString PlayerSettings::playerGraphicsFile(const QString& strPlayerID) const
0134 {
0135     return m_playerSettings.value(strPlayerID).strPlayerGraphicsFile;
0136 }
0137 
0138 bool PlayerSettings::enabled(const QString& strPlayerID) const
0139 {
0140     return m_playerSettings.value(strPlayerID).enabled;
0141 }
0142 
0143 const QKeySequence PlayerSettings::keyUp(const QString& strPlayerID) const
0144 {
0145     return m_playerSettings.value(strPlayerID).keyUp;
0146 }
0147 
0148 const QKeySequence PlayerSettings::keyRight(const QString& strPlayerID) const
0149 {
0150     return m_playerSettings.value(strPlayerID).keyRight;
0151 }
0152 
0153 const QKeySequence PlayerSettings::keyDown(const QString& strPlayerID) const
0154 {
0155     return m_playerSettings.value(strPlayerID).keyDown;
0156 }
0157 
0158 const QKeySequence PlayerSettings::keyLeft(const QString& strPlayerID) const
0159 {
0160     return m_playerSettings.value(strPlayerID).keyLeft;
0161 }
0162 
0163 const QKeySequence PlayerSettings::keyPutBomb(const QString& strPlayerID) const
0164 {
0165     return m_playerSettings.value(strPlayerID).keyPutBomb;
0166 }
0167 
0168 void PlayerSettings::savePlayerSettings()
0169 {
0170     m_playerSettings = m_pendingPlayerSettings;
0171     
0172     QStringList playersGroupList;
0173     QStringList strPlayerIDList;
0174     KConfig granatierConfig(QStringLiteral("granatierrc"), KConfig::NoGlobals );
0175     if(granatierConfig.hasGroup(QStringLiteral("Player")))
0176     {
0177         playersGroupList = granatierConfig.group(QStringLiteral("Player")).groupList();
0178         playersGroupList.sort();
0179         for(const auto& player: std::as_const(playersGroupList))
0180         {
0181             strPlayerIDList.append(granatierConfig.group(QStringLiteral("Player")).group(player).readEntry<QString>("PlayerID", QStringLiteral("")));
0182         }
0183         
0184         int nPlayersGroupIndex;
0185         int nIndex;
0186         auto player = m_playerSettings.constBegin();
0187         while ( player != m_playerSettings.constEnd())
0188         {
0189             playersGroupList.sort();
0190             nIndex = strPlayerIDList.indexOf( player.key());
0191             if(nIndex < 0)
0192             {
0193                 for(int j = 0; j < playersGroupList.count(); j++)
0194                 {
0195                     if(playersGroupList[j].toInt() > j+1)
0196                     {
0197                         playersGroupList.append(QStringLiteral("%1").arg(j+1));
0198                         strPlayerIDList.append( player.key());
0199                         nPlayersGroupIndex = j+1;
0200                         nIndex = nPlayersGroupIndex;
0201                         break;
0202                     }
0203                 }
0204                 if(nIndex < 0)
0205                 {
0206                     playersGroupList.append(QStringLiteral("%1").arg(playersGroupList.count() + 1));
0207                     strPlayerIDList.append( player.key());
0208                     nPlayersGroupIndex = playersGroupList.count();
0209                 }
0210             }
0211             else
0212             {
0213                 nPlayersGroupIndex = playersGroupList[nIndex].toInt();
0214             }
0215             
0216             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("PlayerID", (*player).strPlayerID);
0217             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("Name", (*player).strPlayerName);
0218             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("Enabled", ((*player).enabled ? 1 : 0));
0219             
0220             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyUp", (*player).keyUp.toString());
0221             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyRight", (*player).keyRight.toString());
0222             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyDown", (*player).keyDown.toString());
0223             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyLeft", (*player).keyLeft.toString());
0224             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyPutBomb", (*player).keyPutBomb.toString());
0225             
0226             ++player;
0227         }
0228     }
0229     else
0230     {
0231         int nPlayersGroupIndex = 1;
0232         for (const auto& player: m_playerSettings)
0233         {
0234             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("PlayerID", player.strPlayerID);
0235             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("Name", player.strPlayerName);
0236             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("Enabled", ( player.enabled ? 1 : 0));
0237             
0238             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyUp", player.keyUp.toString());
0239             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyRight", player.keyRight.toString());
0240             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyDown", player.keyDown.toString());
0241             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyLeft", player.keyLeft.toString());
0242             granatierConfig.group(QStringLiteral("Player")).group(QStringLiteral("%1").arg(nPlayersGroupIndex)).writeEntry("KeyPutBomb", player.keyPutBomb.toString());
0243             
0244             nPlayersGroupIndex++;
0245         }
0246     }
0247 }
0248 
0249 void PlayerSettings::discardUnsavedSettings()
0250 {
0251     m_pendingPlayerSettings = m_playerSettings;
0252 }
0253 
0254 void PlayerSettings::setPlayerName(const QString& strPlayerID, const QString& strName)
0255 {
0256     if( m_pendingPlayerSettings.contains(strPlayerID))
0257     {
0258         m_pendingPlayerSettings.find(strPlayerID).value().strPlayerName = strName;
0259         Settings::self()->setDummy(Settings::self()->dummy() + 3);
0260     }
0261 }
0262 
0263 void PlayerSettings::setEnabled(const QString& strPlayerID, const bool enabled)
0264 {
0265     if( m_pendingPlayerSettings.contains(strPlayerID))
0266     {
0267         m_pendingPlayerSettings.find(strPlayerID).value().enabled = enabled;
0268         Settings::self()->setDummy(Settings::self()->dummy() + 3);
0269     }
0270 }
0271 
0272 void PlayerSettings::setKeyUp(const QString& strPlayerID, const QKeySequence& key)
0273 {
0274     if( m_pendingPlayerSettings.contains(strPlayerID))
0275     {
0276         m_pendingPlayerSettings.find(strPlayerID).value().keyUp = key;
0277         Settings::self()->setDummy(Settings::self()->dummy() + 3);
0278     }
0279 }
0280 
0281 void PlayerSettings::setKeyRight(const QString& strPlayerID, const QKeySequence& key)
0282 {
0283     if( m_pendingPlayerSettings.contains(strPlayerID))
0284     {
0285         m_pendingPlayerSettings.find(strPlayerID).value().keyRight = key;
0286         Settings::self()->setDummy(Settings::self()->dummy() + 3);
0287     }
0288 }
0289 
0290 void PlayerSettings::setKeyDown(const QString& strPlayerID, const QKeySequence& key)
0291 {
0292     if( m_pendingPlayerSettings.contains(strPlayerID))
0293     {
0294         m_pendingPlayerSettings.find(strPlayerID).value().keyDown = key;
0295         Settings::self()->setDummy(Settings::self()->dummy() + 3);
0296     }
0297 }
0298 
0299 void PlayerSettings::setKeyLeft(const QString& strPlayerID, const QKeySequence& key)
0300 {
0301     if( m_pendingPlayerSettings.contains(strPlayerID))
0302     {
0303         m_pendingPlayerSettings.find(strPlayerID).value().keyLeft = key;
0304         Settings::self()->setDummy(Settings::self()->dummy() + 3);
0305     }
0306 }
0307 
0308 void PlayerSettings::setKeyPutBomb(const QString& strPlayerID, const QKeySequence& key)
0309 {
0310     if( m_pendingPlayerSettings.contains(strPlayerID))
0311     {
0312         m_pendingPlayerSettings.find(strPlayerID).value().keyPutBomb = key;
0313         Settings::self()->setDummy(Settings::self()->dummy() + 3);
0314     }
0315 }