FlightGear next
LocalAircraftCache.hxx
Go to the documentation of this file.
1// Written by James Turner, started October 2017
2
3//
4// Copyright (C) 2017 James Turner <zakalawe@mac.com>
5//
6// This program is free software; you can redistribute it and/or
7// modify it under the terms of the GNU General Public License as
8// published by the Free Software Foundation; either version 2 of the
9// License, or (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful, but
12// WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20#ifndef LOCALAIRCRAFTCACHE_HXX
21#define LOCALAIRCRAFTCACHE_HXX
22
23#include <memory>
24#include <QObject>
25#include <QPixmap>
26#include <QDateTime>
27#include <QUrl>
28#include <QSharedPointer>
29#include <QDir>
30#include <QVariant>
31
32#include <simgear/props/props.hxx>
33
34class QDataStream;
35struct AircraftItem;
36class SGPropertyNode;
37
38typedef QSharedPointer<AircraftItem> AircraftItemPtr;
39
41{
42 AircraftItem() = default;
43
44 bool initFromFile(QDir dir, QString filePath);
45
46 // the file-name without -set.xml suffix
47 QString baseName() const;
48
49 QString name() const;
50
51 QString description() const;
52
53 void fromDataStream(QDataStream& ds);
54
55 void toDataStream(QDataStream& ds) const;
56
57 int indexOfVariant(QUrl uri) const;
58
59 bool excluded = false;
60 QString path;
61
62 QString authors; // legacy authors data only
63 int ratings[4] = {0, 0, 0, 0};
64 QString variantOf;
65 QDateTime pathModTime;
66 QList<AircraftItemPtr> variants;
67 bool usesHeliports = false;
68 bool usesSeaports = false;
69 QList<QUrl> previews;
70 bool isPrimary = false;
72 QString minFGVersion;
73 QStringList tags;
74 bool needsMaintenance = false;
78 QVariant status(int variant);
79
80
81private:
82 struct LocalizedStrings {
83 QString locale;
84 QMap<QByteArray, QString> strings;
85 };
86
87 friend QDataStream& operator<<(QDataStream&, const LocalizedStrings&);
88 friend QDataStream& operator>>(QDataStream&, LocalizedStrings&);
89
90 using LocalizedStringsVec = QVector<LocalizedStrings>;
91
92 // store all localized strings. We need this to avoid rebuilding
93 // the cache when switching languages.
94 LocalizedStringsVec _localized;
95
96 // the resolved values for our strings, based on QLocale
97 // if we support dynamic switching of language, this would need to
98 // be flushed and re-computed
99 QMap<QByteArray, QString> _currentStrings;
100
101 void doLocalizeStrings();
102
103 void readLocalizedStrings(SGPropertyNode_ptr simNode);
104};
105
106class LocalAircraftCache : public QObject
107{
108 Q_OBJECT
109public:
111
112 static LocalAircraftCache* instance();
113 static void reset();
114
115
116 void setPaths(QStringList paths);
117 QStringList paths() const;
118
119 void scanDirs();
120
121
127 static bool isCandidateAircraftPath(QString path);
128
129 int itemCount() const;
130
131 QVector<AircraftItemPtr> allItems() const;
132
133 AircraftItemPtr itemAt(int index) const;
134
135 AircraftItemPtr findItemWithUri(QUrl aircraftUri) const;
136 int findIndexWithUri(QUrl aircraftUri) const;
137
139
140 QVariant aircraftStatus(AircraftItemPtr item) const;
141
149
159
160 Q_ENUMS(PackageStatus)
161 Q_ENUMS(AircraftStatus)
162
163 // rating order is FDM, Systems, Cockpit, External model
164 static int ratingFromProperties(SGPropertyNode* node, int ratingIndex);
165
171
178 ParseSetXMLResult readAircraftProperties(const SGPath& path, SGPropertyNode_ptr props);
179signals:
180
183
184 void cleared();
185 void addedItems(int count);
186public slots:
187
188private slots:
189 void onScanResults();
190
191 void onScanFinished();
192
193private:
194 explicit LocalAircraftCache();
195
196 void abandonCurrentScan();
197
199 std::unique_ptr<AircraftCachePrivate> d;
200};
201
202#endif // LOCALAIRCRAFTCACHE_HXX
QSharedPointer< AircraftItem > AircraftItemPtr
ParseSetXMLResult readAircraftProperties(const SGPath &path, SGPropertyNode_ptr props)
readAircraftProperties - helper to parse a -set.xml, but with the correct path setup (root,...
AircraftItemPtr itemAt(int index) const
static int ratingFromProperties(SGPropertyNode *node, int ratingIndex)
static bool isCandidateAircraftPath(QString path)
@helper to determine if a particular path is likely to contain aircraft or not.
QStringList paths() const
@ Retry
aircraft scan in progress, try again later
void addedItems(int count)
AircraftItemPtr primaryItemFor(AircraftItemPtr item) const
QVector< AircraftItemPtr > allItems() const
int findIndexWithUri(QUrl aircraftUri) const
void setPaths(QStringList paths)
static LocalAircraftCache * instance()
AircraftItemPtr findItemWithUri(QUrl aircraftUri) const
QVariant aircraftStatus(AircraftItemPtr item) const
static int status
int indexOfVariant(QUrl uri) const
QList< QUrl > previews
QString name() const
friend QDataStream & operator<<(QDataStream &, const LocalizedStrings &)
friend QDataStream & operator>>(QDataStream &, LocalizedStrings &)
bool initFromFile(QDir dir, QString filePath)
void toDataStream(QDataStream &ds) const
QList< AircraftItemPtr > variants
AircraftItem()=default
void fromDataStream(QDataStream &ds)
QString description() const
QString baseName() const