A basic model for external resources. * * This model manages a list of resources. As such, external users of such resources do not own them, * and the resource's lifetime is contingent on the model's lifetime. * * TODO: Make the resources unique pointers accessible through weak pointers. */
| 21 | * TODO: Make the resources unique pointers accessible through weak pointers. |
| 22 | */ |
| 23 | class ResourceFolderModel : public QAbstractListModel { |
| 24 | Q_OBJECT |
| 25 | public: |
| 26 | ResourceFolderModel(QDir, QObject* parent = nullptr); |
| 27 | ~ResourceFolderModel() override; |
| 28 | |
| 29 | /** Starts watching the paths for changes. |
| 30 | * |
| 31 | * Returns whether starting to watch all the paths was successful. |
| 32 | * If one or more fails, it returns false. |
| 33 | */ |
| 34 | bool startWatching(const QStringList paths); |
| 35 | |
| 36 | /** Stops watching the paths for changes. |
| 37 | * |
| 38 | * Returns whether stopping to watch all the paths was successful. |
| 39 | * If one or more fails, it returns false. |
| 40 | */ |
| 41 | bool stopWatching(const QStringList paths); |
| 42 | |
| 43 | /* Helper methods for subclasses, using a predetermined list of paths. */ |
| 44 | virtual bool startWatching() { return startWatching({ m_dir.absolutePath() }); }; |
| 45 | virtual bool stopWatching() { return stopWatching({ m_dir.absolutePath() }); }; |
| 46 | |
| 47 | /** Given a path in the system, install that resource, moving it to its place in the |
| 48 | * instance file hierarchy. |
| 49 | * |
| 50 | * Returns whether the installation was succcessful. |
| 51 | */ |
| 52 | virtual bool installResource(QString path); |
| 53 | |
| 54 | /** Uninstall (i.e. remove all data about it) a resource, given its file name. |
| 55 | * |
| 56 | * Returns whether the removal was successful. |
| 57 | */ |
| 58 | virtual bool uninstallResource(QString file_name); |
| 59 | virtual bool deleteResources(const QModelIndexList&); |
| 60 | |
| 61 | /** Applies the given 'action' to the resources in 'indexes'. |
| 62 | * |
| 63 | * Returns whether the action was successfully applied to all resources. |
| 64 | */ |
| 65 | virtual bool setResourceEnabled(const QModelIndexList& indexes, EnableAction action); |
| 66 | |
| 67 | /** Creates a new update task and start it. Returns false if no update was done, like when an update is already underway. */ |
| 68 | virtual bool update(); |
| 69 | |
| 70 | /** Creates a new parse task, if needed, for 'res' and start it.*/ |
| 71 | virtual void resolveResource(Resource* res); |
| 72 | |
| 73 | [[nodiscard]] size_t size() const { return m_resources.size(); }; |
| 74 | [[nodiscard]] bool empty() const { return size() == 0; } |
| 75 | [[nodiscard]] Resource& at(int index) { return *m_resources.at(index); } |
| 76 | [[nodiscard]] Resource const& at(int index) const { return *m_resources.at(index); } |
| 77 | [[nodiscard]] QList<Resource::Ptr> const& all() const { return m_resources; } |
| 78 | |
| 79 | [[nodiscard]] QDir const& dir() const { return m_dir; } |
| 80 |
no test coverage detected