1 /***************************************************************************
2     Copyright (C) 2006-2009 Robby Stephenson <robby@periapsis.org>
3 
4  ***************************************************************************/
5 
6 /***************************************************************************
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or         *
9  *   modify it under the terms of the GNU General Public License as        *
10  *   published by the Free Software Foundation; either version 2 of        *
11  *   the License or (at your option) version 3 or any later version        *
12  *   accepted by the membership of KDE e.V. (or its successor approved     *
13  *   by the membership of KDE e.V.), which shall act as a proxy            *
14  *   defined in Section 14 of version 3 of the license.                    *
15  *                                                                         *
16  *   This program is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
23  *                                                                         *
24  ***************************************************************************/
25 
26 #include "gamecollection.h"
27 #include "../tellico_debug.h"
28 
29 #include <KLocalizedString>
30 
31 namespace {
32   static const char* game_general = I18N_NOOP("General");
33   static const char* game_personal = I18N_NOOP("Personal");
34 }
35 
36 using Tellico::Data::GameCollection;
37 
GameCollection(bool addDefaultFields_,const QString & title_)38 GameCollection::GameCollection(bool addDefaultFields_, const QString& title_)
39    : Collection(title_.isEmpty() ? i18n("My Games") : title_) {
40   setDefaultGroupField(QStringLiteral("platform"));
41   if(addDefaultFields_) {
42     addFields(defaultFields());
43   }
44 }
45 
defaultFields()46 Tellico::Data::FieldList GameCollection::defaultFields() {
47   FieldList list;
48   FieldPtr field;
49 
50   list.append(Field::createDefaultField(Field::TitleField));
51 
52   QStringList platform;
53   platform << platformName(XboxSeriesX) << platformName(XboxOne) << platformName(Xbox360) << platformName(Xbox)
54            << platformName(PlayStation5) << platformName(PlayStation4) << platformName(PlayStation3) << platformName(PlayStation2) << platformName(PlayStation)
55            << platformName(PlayStationPortable) << platformName(PlayStationVita)
56            << platformName(NintendoSwitch) << platformName(NintendoWiiU)
57            << platformName(NintendoWii)  << platformName(Nintendo3DS) << platformName(NintendoDS)
58            << platformName(Nintendo64)  << platformName(SuperNintendo) << platformName(Nintendo)
59            << platformName(NintendoGameCube) << platformName(Dreamcast)
60            << platformName(Windows) << platformName(MacOS) << platformName(Linux);
61   field = new Field(QStringLiteral("platform"), i18n("Platform"), platform);
62   field->setCategory(i18n(game_general));
63   field->setFlags(Field::AllowGrouped);
64   list.append(field);
65 
66   field = new Field(QStringLiteral("genre"), i18n("Genre"));
67   field->setCategory(i18n(game_general));
68   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
69   field->setFormatType(FieldFormat::FormatPlain);
70   list.append(field);
71 
72   field = new Field(QStringLiteral("year"), i18n("Release Year"), Field::Number);
73   field->setCategory(i18n(game_general));
74   field->setFlags(Field::AllowGrouped);
75   list.append(field);
76 
77   field = new Field(QStringLiteral("publisher"), i18nc("Games - Publisher", "Publisher"));
78   field->setCategory(i18n(game_general));
79   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
80   field->setFormatType(FieldFormat::FormatPlain);
81   list.append(field);
82 
83   field = new Field(QStringLiteral("developer"), i18n("Developer"));
84   field->setCategory(i18n(game_general));
85   field->setFlags(Field::AllowCompletion | Field::AllowMultiple | Field::AllowGrouped);
86   field->setFormatType(FieldFormat::FormatPlain);
87   list.append(field);
88 
89   QStringList cert = i18nc("Video game ratings - "
90                            "Unrated, Adults Only, Mature, Teen, Everyone 10+, Everyone, Early Childhood, Pending",
91                            "Unrated, Adults Only, Mature, Teen, Everyone 10+, Everyone, Early Childhood, Pending")
92 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
93                      .split(QRegularExpression(QLatin1String("\\s*,\\s*")), QString::SkipEmptyParts);
94 #else
95                      .split(QRegularExpression(QLatin1String("\\s*,\\s*")), Qt::SkipEmptyParts);
96 #endif
97   field = new Field(QStringLiteral("certification"), i18n("ESRB Rating"), cert);
98   field->setCategory(i18n(game_general));
99   field->setFlags(Field::AllowGrouped);
100   list.append(field);
101 
102   field = new Field(QStringLiteral("description"), i18n("Description"), Field::Para);
103   list.append(field);
104 
105   field = new Field(QStringLiteral("rating"), i18n("Personal Rating"), Field::Rating);
106   field->setCategory(i18n(game_personal));
107   field->setFlags(Field::AllowGrouped);
108   list.append(field);
109 
110   field = new Field(QStringLiteral("completed"), i18n("Completed"), Field::Bool);
111   field->setCategory(i18n(game_personal));
112   list.append(field);
113 
114   field = new Field(QStringLiteral("pur_date"), i18n("Purchase Date"));
115   field->setCategory(i18n(game_personal));
116   field->setFormatType(FieldFormat::FormatDate);
117   list.append(field);
118 
119   field = new Field(QStringLiteral("gift"), i18n("Gift"), Field::Bool);
120   field->setCategory(i18n(game_personal));
121   list.append(field);
122 
123   field = new Field(QStringLiteral("pur_price"), i18n("Purchase Price"));
124   field->setCategory(i18n(game_personal));
125   list.append(field);
126 
127   field = new Field(QStringLiteral("loaned"), i18n("Loaned"), Field::Bool);
128   field->setCategory(i18n(game_personal));
129   list.append(field);
130 
131   field = new Field(QStringLiteral("cover"), i18n("Cover"), Field::Image);
132   list.append(field);
133 
134   field = new Field(QStringLiteral("comments"), i18n("Comments"), Field::Para);
135   field->setCategory(i18n(game_personal));
136   list.append(field);
137 
138   list.append(Field::createDefaultField(Field::IDField));
139   list.append(Field::createDefaultField(Field::CreatedDateField));
140   list.append(Field::createDefaultField(Field::ModifiedDateField));
141 
142   return list;
143 }
144 
145 
normalizePlatform(const QString & platformName_)146 QString GameCollection::normalizePlatform(const QString& platformName_) {
147   if(platformName_.isEmpty()) return QString();
148   GamePlatform platform = guessPlatform(platformName_);
149   if(platform == UnknownPlatform) {
150     QString platformName = platformName_;
151     if(platformName.startsWith(QStringLiteral("Microsoft"))) {
152       platformName = platformName.mid(sizeof("Microsoft"));
153     } else if(platformName.startsWith(QStringLiteral("Sony Playstation"))) {
154       // default video game collection has no space between 'PlayStation' and #
155       platformName = QStringLiteral("PlayStation") + platformName.mid(sizeof("Sony Playstation"));
156     }
157     myDebug() << "No default name for" << platformName_ << "; return" << platformName;
158     return platformName;
159   }
160   return platformName(platform);
161 }
162 
guessPlatform(const QString & name_)163 Tellico::Data::GameCollection::GamePlatform GameCollection::guessPlatform(const QString& name_) {
164   // try to be smart about guessing the platform from its name
165   if(name_.contains(QStringLiteral("PlayStation"), Qt::CaseInsensitive)) {
166     if(name_.contains(QStringLiteral("Vita"))) {
167       return PlayStationVita;
168     } else if(name_.contains(QStringLiteral("Portable"))) {
169       return PlayStationPortable;
170     } else if(name_.contains(QStringLiteral("5"))) {
171       return PlayStation5;
172     } else if(name_.contains(QStringLiteral("4"))) {
173       return PlayStation4;
174     } else if(name_.contains(QStringLiteral("3"))) {
175       return PlayStation3;
176     } else if(name_.contains(QStringLiteral("2"))) {
177       return PlayStation2;
178     } else {
179       return PlayStation;
180     }
181   } else if(name_.contains(QStringLiteral("PSP"))) {
182     return PlayStationPortable;
183   } else if(name_.contains(QStringLiteral("Xbox"), Qt::CaseInsensitive)) {
184     if(name_.contains(QStringLiteral("One"))) {
185       return XboxOne;
186     } else if(name_.contains(QStringLiteral("360"))) {
187       return Xbox360;
188     } else if(name_.endsWith(QStringLiteral("X"))) {
189       return XboxSeriesX;
190     } else {
191       return Xbox;
192     }
193   } else if(name_.contains(QStringLiteral("Switch"))) {
194     return NintendoSwitch;
195   } else if(name_.contains(QStringLiteral("Wii"))) {
196     if(name_.contains(QStringLiteral("U"))) {
197       return NintendoWiiU;
198     } else {
199       return NintendoWii;
200     }
201   } else if(name_.contains(QStringLiteral("PC")) ||
202             name_.contains(QStringLiteral("Windows"))) {
203     return Windows;
204   } else if(name_.contains(QStringLiteral("Mac"))) {
205     return MacOS;
206   } else if(name_.contains(QStringLiteral("3DS"))) {
207     return Nintendo3DS;
208   } else if(name_.contains(QStringLiteral("DS"))) {
209     return NintendoDS;
210   } else if(name_ == QStringLiteral("Nintendo 64")) {
211     return Nintendo64;
212   } else if(name_.contains(QStringLiteral("GameCube"), Qt::CaseInsensitive)) {
213     return NintendoGameCube;
214   } else if(name_.contains(QStringLiteral("Advance"))) {
215     return GameBoyAdvance;
216   } else if(name_.contains(QStringLiteral("Game Boy Color")) || name_.contains(QStringLiteral("GameBoy Color"), Qt::CaseInsensitive)) {
217     return GameBoyColor;
218   } else if(name_.contains(QStringLiteral("Game Boy")) || name_.contains(QStringLiteral("GameBoy"), Qt::CaseInsensitive)) {
219     return GameBoy;
220   } else if(name_.contains(QStringLiteral("SNES")) || name_.contains(QStringLiteral("Super Nintendo"))) {
221     return SuperNintendo;
222     // only return Nintendo if equal or includes Original or Entertainment
223     // could be platforms like "Nintendo Virtual Boy"
224   } else if(name_ == QStringLiteral("Nintendo")
225             || name_ == QStringLiteral("NES")
226             || name_.contains(QStringLiteral("Nintendo Entertainment"))) {
227     return Nintendo;
228   } else if(name_.contains(QStringLiteral("Genesis"))) {
229     return Genesis;
230   } else if(name_.contains(QStringLiteral("Dreamcast"))) {
231     return Dreamcast;
232   } else if(name_.contains(QStringLiteral("Linux"))) {
233     return Linux;
234   } else if(name_.contains(QStringLiteral("ios"), Qt::CaseInsensitive)) {
235     return iOS;
236   } else if(name_.contains(QStringLiteral("Android"))) {
237     return Android;
238   }
239 //  myDebug() << "No platform guess for" << name_;
240   return UnknownPlatform;
241 }
242 
platformName(GamePlatform platform_)243 QString GameCollection::platformName(GamePlatform platform_) {
244   switch(platform_) {
245     case Linux:               return i18n("Linux");
246     case MacOS:               return i18n("Mac OS");
247     case Windows:             return i18nc("Windows Platform", "Windows");
248     case iOS:                 return i18nc("iOS Platform", "iOS");
249     case Android:             return i18nc("Android Platform", "Android");
250     case Xbox:                return i18n("Xbox");
251     case Xbox360:             return i18n("Xbox 360");
252     case XboxOne:             return i18n("Xbox One");
253     case XboxSeriesX:         return i18n("Xbox Series X");
254     case PlayStation:         return i18n("PlayStation");
255     case PlayStation2:        return i18n("PlayStation2");
256     case PlayStation3:        return i18n("PlayStation3");
257     case PlayStation4:        return i18n("PlayStation4");
258     case PlayStation5:        return i18n("PlayStation5");
259     case PlayStationPortable: return i18nc("PlayStation Portable", "PSP");
260     case PlayStationVita:     return i18n("PlayStation Vita");
261     case GameBoy:             return i18n("Game Boy");
262     case GameBoyColor:        return i18n("Game Boy Color");
263     case GameBoyAdvance:      return i18n("Game Boy Advance");
264     case Nintendo:            return i18n("Nintendo");
265     case SuperNintendo:       return i18n("Super Nintendo");
266     case Nintendo64:          return i18n("Nintendo 64");
267     case NintendoGameCube:    return i18n("GameCube");
268     case NintendoWii:         return i18n("Nintendo Wii");
269     case NintendoWiiU:        return i18n("Nintendo WiiU");
270     case NintendoSwitch:      return i18n("Nintendo Switch");
271     case NintendoDS:          return i18n("Nintendo DS");
272     case Nintendo3DS:         return i18n("Nintendo 3DS");
273     case Genesis:             return i18nc("Sega Genesis", "Genesis");
274     case Dreamcast:           return i18n("Dreamcast");
275     case UnknownPlatform:     break;
276     case LastPlatform:        break;
277   }
278   myDebug() << "Failed to return platform name for" << platform_;
279   return QString();
280 }
281