Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

CrewDatabase.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2016-2017 Syping
  4. *
  5. * This program 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 3 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, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "StandardPaths.h"
  19. #include "CrewDatabase.h"
  20. #include "config.h"
  21. #include <QStringBuilder>
  22. #include <QMutexLocker>
  23. #include <QDebug>
  24. #include <QFile>
  25. #include <QDir>
  26. CrewDatabase::CrewDatabase(QObject *parent) : QObject(parent)
  27. {
  28. QDir dir;
  29. dir.mkpath(StandardPaths::dataLocation());
  30. dir.setPath(StandardPaths::dataLocation());
  31. QString dirPath = dir.absolutePath();
  32. QString defaultConfPath = dirPath % "/crews.ini";
  33. QSettings confPathSettings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  34. confPathSettings.beginGroup("Database");
  35. QString confPathFile = confPathSettings.value("Crews", defaultConfPath).toString();
  36. confPathSettings.endGroup();
  37. crewDB = new QSettings(confPathFile, QSettings::IniFormat);
  38. crewDB->beginGroup("Crews");
  39. addProcess = false;
  40. }
  41. CrewDatabase::~CrewDatabase()
  42. {
  43. crewDB->endGroup();
  44. delete crewDB;
  45. }
  46. QStringList CrewDatabase::getCrews()
  47. {
  48. QMutexLocker locker(&mutex);
  49. #ifdef GTA5SYNC_DEBUG
  50. qDebug() << "getCrews";
  51. #endif
  52. return getCrews_p();
  53. }
  54. QStringList CrewDatabase::getCrews_p()
  55. {
  56. #ifdef GTA5SYNC_DEBUG
  57. qDebug() << "getCrews_p";
  58. #endif
  59. QStringList compatibleCrewList = getCompatibleCrews_p();
  60. crewDB->endGroup();
  61. crewDB->beginGroup("CrewList");
  62. QStringList crewIDs = crewDB->value("IDs", QStringList()).toStringList();
  63. crewIDs += compatibleCrewList;
  64. crewIDs.removeDuplicates();
  65. crewDB->endGroup();
  66. crewDB->beginGroup("Crews");
  67. return crewIDs;
  68. }
  69. QStringList CrewDatabase::getCompatibleCrews()
  70. {
  71. QMutexLocker locker(&mutex);
  72. #ifdef GTA5SYNC_DEBUG
  73. qDebug() << "getCompatibleCrews";
  74. #endif
  75. return getCompatibleCrews_p();
  76. }
  77. QStringList CrewDatabase::getCompatibleCrews_p()
  78. {
  79. #ifdef GTA5SYNC_DEBUG
  80. qDebug() << "getCompatibleCrews_p";
  81. #endif
  82. return crewDB->childKeys();
  83. }
  84. QString CrewDatabase::getCrewName(QString crewID)
  85. {
  86. QMutexLocker locker(&mutex);
  87. #ifdef GTA5SYNC_DEBUG
  88. qDebug() << "getCrewName" << crewID;
  89. #endif
  90. QString crewStr = crewDB->value(crewID, crewID).toString();
  91. if (crewID == "0") crewStr = tr("No Crew", "");
  92. return crewStr;
  93. }
  94. QString CrewDatabase::getCrewName(int crewID)
  95. {
  96. QMutexLocker locker(&mutex);
  97. #ifdef GTA5SYNC_DEBUG
  98. qDebug() << "getCrewName" << crewID;
  99. #endif
  100. QString crewStr = crewDB->value(QString::number(crewID), crewID).toString();
  101. if (crewID == 0) crewStr = tr("No Crew", "");
  102. return crewStr;
  103. }
  104. void CrewDatabase::setCrewName(int crewID, QString crewName)
  105. {
  106. QMutexLocker locker(&mutex);
  107. #ifdef GTA5SYNC_DEBUG
  108. qDebug() << "setCrewName" << crewID << crewName;
  109. #endif
  110. crewDB->setValue(QString::number(crewID), crewName);
  111. }
  112. void CrewDatabase::addCrew(int crewID)
  113. {
  114. QMutexLocker locker(&mutex);
  115. #ifdef GTA5SYNC_DEBUG
  116. qDebug() << "addCrew" << crewID;
  117. #endif
  118. QStringList crews = getCrews_p();
  119. crews += QString::number(crewID);
  120. crews.removeDuplicates();
  121. crewDB->endGroup();
  122. crewDB->beginGroup("CrewList");
  123. crewDB->setValue("IDs", crews);
  124. crewDB->endGroup();
  125. crewDB->beginGroup("Crews");
  126. }
  127. bool CrewDatabase::isCompatibleCrew(QString crewNID)
  128. {
  129. QMutexLocker locker(&mutex);
  130. #ifdef GTA5SYNC_DEBUG
  131. qDebug() << "isCompatibleCrew" << crewNID;
  132. #endif
  133. return crewDB->contains(crewNID);
  134. }
  135. bool CrewDatabase::isCompatibleCrew(int crewID)
  136. {
  137. QMutexLocker locker(&mutex);
  138. #ifdef GTA5SYNC_DEBUG
  139. qDebug() << "isCompatibleCrew" << crewID;
  140. #endif
  141. return crewDB->contains(QString::number(crewID));
  142. }
  143. void CrewDatabase::setAddingCrews(bool addingCrews)
  144. {
  145. QMutexLocker locker(&mutex);
  146. #ifdef GTA5SYNC_DEBUG
  147. qDebug() << "setAddingCrews" << addingCrews;
  148. #endif
  149. addProcess = addingCrews;
  150. }
  151. bool CrewDatabase::isAddingCrews()
  152. {
  153. QMutexLocker locker(&mutex);
  154. #ifdef GTA5SYNC_DEBUG
  155. qDebug() << "isAddingCrews";
  156. #endif
  157. return addProcess;
  158. }