Overte C++ Documentation
ScriptsModel.h
1 //
2 // ScriptsModel.h
3 // interface/src
4 //
5 // Created by Ryan Huffman on 05/12/14.
6 // Copyright 2014 High Fidelity, Inc.
7 // Copyright 2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
16 
17 #ifndef hifi_ScriptsModel_h
18 #define hifi_ScriptsModel_h
19 
20 #include <QAbstractItemModel>
21 #include <QDir>
22 #include <QNetworkReply>
23 #include <QFileSystemWatcher>
24 
25 class TreeNodeFolder;
26 
27 enum ScriptOrigin {
28  SCRIPT_ORIGIN_LOCAL,
29  SCRIPT_ORIGIN_DEFAULT
30 };
31 
32 enum TreeNodeType {
33  TREE_NODE_TYPE_SCRIPT,
34  TREE_NODE_TYPE_FOLDER
35 };
36 
37 class TreeNodeBase {
38 public:
39  TreeNodeFolder* getParent() const { return _parent; }
40  void setParent(TreeNodeFolder* parent) { _parent = parent; }
41  TreeNodeType getType() { return _type; }
42  const QString& getName() { return _name; };
43  virtual ~TreeNodeBase() = default;
44 
45 private:
46  TreeNodeFolder* _parent;
47  TreeNodeType _type;
48 
49 protected:
50  QString _name;
51  TreeNodeBase(TreeNodeFolder* parent, const QString& name, TreeNodeType type);
52 };
53 
54 class TreeNodeScript : public TreeNodeBase {
55 public:
56  TreeNodeScript(const QString& localPath, const QString& fullPath, ScriptOrigin origin);
57  const QString& getLocalPath() { return _localPath; }
58  const QString& getFullPath() { return _fullPath; };
59  ScriptOrigin getOrigin() { return _origin; };
60 
61 private:
62  QString _localPath;
63  QString _fullPath;
64  ScriptOrigin _origin;
65 };
66 
67 class TreeNodeFolder : public TreeNodeBase {
68 public:
69  TreeNodeFolder(const QString& foldername, TreeNodeFolder* parent);
70 };
71 
72 /*@jsdoc
73  * Information on the scripts that are in the default scripts directory of the Interface installation. This is provided as a
74  * property of {@link ScriptDiscoveryService}.
75  *
76  * <p>The information provided reflects the subdirectory structure. Methods and signals are per QT's
77  * <a href="http://doc.qt.io/qt-5/qabstractitemmodel.html">QAbstractItemModel</a> class, with the following details:</p>
78  * <ul>
79  * <li>A single column of data: <code>columnCount(index)</code> returns <code>1</code>. </li>
80  * <li>Data is provided for the following roles:
81  * <table>
82  * <thead>
83  * <tr><th>Role</th><th>Value</th><th>Description</th></tr>
84  * </thead>
85  * <tbody>
86  * <tr><td>Display</td><td><code>0</code></td><td>The directory or script file name.</td></tr>
87  * <tr><td>Path</td><td><code>256</code></td><td>The path and filename of the data item if it is a script,
88  * <code>undefined</code> if it is a directory.</td></tr>
89  * </tbody>
90  * </table>
91  * </li>
92  * <li>Use <code>null</code> for the root directory's index.</li>
93  * </ul>
94  *
95  * @class ScriptsModel
96  * @hideconstructor
97  *
98  * @hifi-interface
99  * @hifi-client-entity
100  * @hifi-avatar
101  *
102  * @example <caption>List the first 2 levels of the scripts directory.</caption>
103  * var MAX_DIRECTORY_LEVEL = 1;
104  * var DISPLAY_ROLE = 0;
105  * var PATH_ROLE = 256;
106  *
107  * function printDirectory(parentIndex, directoryLevel, indent) {
108  * var numRows = ScriptDiscoveryService.scriptsModel.rowCount(parentIndex);
109  * for (var i = 0; i < numRows; i++) {
110  * var rowIndex = ScriptDiscoveryService.scriptsModel.index(i, 0, parentIndex);
111  *
112  * var name = ScriptDiscoveryService.scriptsModel.data(rowIndex, DISPLAY_ROLE);
113  * var hasChildren = ScriptDiscoveryService.scriptsModel.hasChildren(rowIndex);
114  * var path = hasChildren ? "" : ScriptDiscoveryService.scriptsModel.data(rowIndex, PATH_ROLE);
115  *
116  * print(indent + "- " + name + (hasChildren ? "" : " - " + path));
117  *
118  * if (hasChildren && directoryLevel < MAX_DIRECTORY_LEVEL) {
119  * printDirectory(rowIndex, directoryLevel + 1, indent + " ");
120  * }
121  * }
122  * }
123  *
124  * print("Scripts:");
125  * printDirectory(null, 0, ""); // null index for the root directory.
126  */
128 class ScriptsModel : public QAbstractItemModel {
129  Q_OBJECT
130 public:
131  ScriptsModel(QObject* parent = NULL);
132  ~ScriptsModel();
133 
134  // No JSDoc because the particulars of the parent class is provided in the @class description.
135  QModelIndex index(int row, int column, const QModelIndex& parent) const override;
136 
137  // No JSDoc because the particulars of the parent class is provided in the @class description.
138  QModelIndex parent(const QModelIndex& child) const override;
139 
140  // No JSDoc because the particulars of the parent class is provided in the @class description.
141  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
142 
143  // No JSDoc because the particulars of the parent class is provided in the @class description.
144  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
145 
146  // No JSDoc because the particulars of the parent class is provided in the @class description.
147  int columnCount(const QModelIndex& parent = QModelIndex()) const override;
148 
149  // Not exposed in the API because no conversion between TreeNodeBase and ScriptValue is provided.
150  TreeNodeBase* getTreeNodeFromIndex(const QModelIndex& index) const;
151 
152  // Not exposed in the API because no conversion between TreeNodeBase and ScriptValue is provided.
153  QList<TreeNodeBase*> getFolderNodes(TreeNodeFolder* parent) const;
154 
155  enum Role {
156  ScriptPath = Qt::UserRole,
157  };
158 
159 protected slots:
160 
161  /*@jsdoc
162  * @function ScriptsModel.updateScriptsLocation
163  * @param {string} newPath - New path.
164  * @deprecated This method is deprecated and will be removed from the API.
165  */
166  void updateScriptsLocation(const QString& newPath);
167 
168  /*@jsdoc
169  * @function ScriptsModel.downloadFinished
170  * @deprecated This method is deprecated and will be removed from the API.
171  */
172  void downloadFinished();
173 
174  /*@jsdoc
175  * @function ScriptsModel.reloadLocalFiles
176  * @deprecated This method is deprecated and will be removed from the API.
177  */
178  void reloadLocalFiles();
179 
180  /*@jsdoc
181  * @function ScriptsModel.reloadDefaultFiles
182  * @deprecated This method is deprecated and will be removed from the API.
183  */
184  void reloadDefaultFiles();
185 
186 protected:
187  void requestDefaultFiles(QString marker = QString());
188  bool parseXML(QByteArray xmlFile);
189  void rebuildTree();
190 
191 private:
192  friend class ScriptEngines;
193  bool _loadingScripts;
194  QDir _localDirectory;
195  QFileSystemWatcher _fsWatcher;
196  QList<TreeNodeBase*> _treeNodes;
197 };
198 
199 #endif // hifi_ScriptsModel_h
200 
Provides the ScriptDiscoveryService scripting interface.
Definition: ScriptEngines.h:59
Provides script file information available from the ScriptDiscoveryService scripting interface.
Definition: ScriptsModel.h:128