| 1 | // Written by James Turner, started October 2017 |
| 2 | |
| 3 | // |
| 4 | // Copyright (C) 2017 James Turner <[email protected]> |
| 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 | |
| 34 | class QDataStream; |
| 35 | struct AircraftItem; |
| 36 | class SGPropertyNode; |
| 37 | |
| 38 | typedef QSharedPointer<AircraftItem> AircraftItemPtr; |
| 39 | |
| 40 | struct AircraftItem |
| 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; |
| 71 | QString thumbnailPath; |
| 72 | QString minFGVersion; |
| 73 | QStringList tags; |
| 74 | bool needsMaintenance = false; |
| 75 | QUrl homepageUrl; |
| 76 | QUrl wikipediaUrl; |
| 77 | QUrl supportUrl; |
| 78 | QVariant status(int variant); |
| 79 | |
| 80 | |
| 81 | private: |
| 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 | |
| 106 | class LocalAircraftCache : public QObject |
| 107 | { |
| 108 | Q_OBJECT |
| 109 | public: |
| 110 | ~LocalAircraftCache(); |
| 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 | |
| 122 | /** |
| 123 | * @helper to determine if a particular path is likely to contain |
| 124 | * aircraft or not. Checks for -set.xml files one level down in the tree. |
| 125 | * |
| 126 | */ |
| 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 | |
| 138 | AircraftItemPtr primaryItemFor(AircraftItemPtr item) const; |
| 139 | |
| 140 | QVariant aircraftStatus(AircraftItemPtr item) const; |
| 141 | |
| 142 | enum AircraftStatus |
| 143 | { |
| 144 | AircraftOk = 0, |
| 145 | AircraftUnmaintained, |
| 146 | AircraftNeedsNewerSimulator, |
| 147 | AircraftNeedsOlderSimulator // won't ever occur for the moment |
| 148 | }; |
| 149 | |
| 150 | enum PackageStatus { |
| 151 | PackageNotInstalled = 0, |
| 152 | PackageInstalled, |
| 153 | PackageUpdateAvailable, |
| 154 | PackageQueued, |
| 155 | PackageDownloading, |
| 156 | NotPackaged, |
| 157 | PackageInstallFailed |
| 158 | }; |
| 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 | |
| 166 | enum class ParseSetXMLResult { |
| 167 | Ok, |
| 168 | Failed, |
| 169 | Retry ///< aircraft scan in progress, try again later |
| 170 | }; |
| 171 | /** |
| 172 | * @brief readAircraftProperties - helper to parse a -set.xml, but with the correct |
| 173 | * path setup (root, aircradft dirs, current aircraft dir) |
| 174 | * @param path : full path to the -set.xml file |
| 175 | * @param props : property node to be populated |
| 176 | * @return status indication |
| 177 | */ |
| 178 | ParseSetXMLResult readAircraftProperties(const SGPath& path, SGPropertyNode_ptr props); |
| 179 | signals: |
| 180 | |
| 181 | void scanStarted(); |
| 182 | void scanCompleted(); |
| 183 | |
| 184 | void cleared(); |
| 185 | void addedItems(int count); |
| 186 | public slots: |
| 187 | |
| 188 | private slots: |
| 189 | void onScanResults(); |
| 190 | |
| 191 | void onScanFinished(); |
| 192 | |
| 193 | private: |
| 194 | explicit LocalAircraftCache(); |
| 195 | |
| 196 | void abandonCurrentScan(); |
| 197 | |
| 198 | class AircraftCachePrivate; |
| 199 | std::unique_ptr<AircraftCachePrivate> d; |
| 200 | }; |
| 201 | |
| 202 | #endif // LOCALAIRCRAFTCACHE_HXX |