Showing the changes to interface/src/ScriptsModel.cpp (1 of 4 files). Show all
Unified · Split
| @@ -11,6 +11,7 @@ · 10 unmodified lines | ||
| 11 | 11 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 12 | 12 | // |
| 13 | 13 | |
| 14 | +#include <typeinfo> | |
| 14 | 15 | #include <QUrl> |
| 15 | 16 | #include <QUrlQuery> |
| 16 | 17 | #include <QXmlStreamReader> |
| @@ -31,18 +32,30 @@ static const QString IS_TRUNCATED_NAME = "IsTruncated"; · 14 unmodified lines | ||
| 31 | 32 | static const QString CONTAINER_NAME = "Contents"; |
| 32 | 33 | static const QString KEY_NAME = "Key"; |
| 33 | 34 | |
| 34 | −ScriptItem::ScriptItem(const QString& filename, const QString& fullPath) : | |
| 35 | +TreeNodeBase::TreeNodeBase(TreeNodeFolder* parent, TreeNodeType type) : | |
| 36 | + _parent(parent), | |
| 37 | + _type(type) { | |
| 38 | +}; | |
| 39 | + | |
| 40 | +TreeNodeScript::TreeNodeScript(const QString& filename, const QString& fullPath, ScriptOrigin origin) : | |
| 41 | + TreeNodeBase(NULL, TREE_NODE_TYPE_SCRIPT), | |
| 35 | 42 | _filename(filename), |
| 36 | − _fullPath(fullPath) { | |
| 43 | + _fullPath(fullPath), | |
| 44 | + _origin(origin) | |
| 45 | +{ | |
| 46 | +}; | |
| 47 | + | |
| 48 | +TreeNodeFolder::TreeNodeFolder(const QString& foldername, TreeNodeFolder* parent) : | |
| 49 | + TreeNodeBase(parent, TREE_NODE_TYPE_FOLDER), | |
| 50 | + _foldername(foldername) { | |
| 37 | 51 | }; |
| 38 | 52 | |
| 39 | 53 | ScriptsModel::ScriptsModel(QObject* parent) : |
| 40 | − QAbstractListModel(parent), | |
| 54 | + QAbstractItemModel(parent), | |
| 41 | 55 | _loadingScripts(false), |
| 42 | 56 | _localDirectory(), |
| 43 | 57 | _fsWatcher(), |
| 44 | − _localFiles(), | |
| 45 | − _remoteFiles() | |
| 58 | + _treeNodes() | |
| 46 | 59 | { |
| 47 | 60 | |
| 48 | 61 | _localDirectory.setFilter(QDir::Files | QDir::Readable); |
| @@ -57,23 +70,31 @@ ScriptsModel::ScriptsModel(QObject* parent) : · 8 unmodified lines | ||
| 57 | 70 | reloadRemoteFiles(); |
| 58 | 71 | } |
| 59 | 72 | |
| 73 | +QModelIndex ScriptsModel::index(int row, int column, const QModelIndex& parent) const { | |
| 74 | + return QModelIndex(); | |
| 75 | +} | |
| 76 | + | |
| 77 | +QModelIndex ScriptsModel::parent(const QModelIndex& child) const { | |
| 78 | + return QModelIndex(); | |
| 79 | +} | |
| 80 | + | |
| 60 | 81 | QVariant ScriptsModel::data(const QModelIndex& index, int role) const { |
| 61 | − const QList<ScriptItem*>* files = NULL; | |
| 82 | + /*const QList<TreeNodeScript*>* files = NULL; | |
| 62 | 83 | int row = 0; |
| 63 | 84 | bool isLocal = index.row() < _localFiles.size(); |
| 64 | − if (isLocal) { | |
| 65 | − files = &_localFiles; | |
| 66 | − row = index.row(); | |
| 67 | − } else { | |
| 68 | − files = &_remoteFiles; | |
| 69 | − row = index.row() - _localFiles.size(); | |
| 70 | − } | |
| 85 | + if (isLocal) { | |
| 86 | + files = &_localFiles; | |
| 87 | + row = index.row(); | |
| 88 | + } else { | |
| 89 | + files = &_remoteFiles; | |
| 90 | + row = index.row() - _localFiles.size(); | |
| 91 | + } | |
| 71 | 92 | |
| 72 | 93 | if (role == Qt::DisplayRole) { |
| 73 | 94 | return QVariant((*files)[row]->getFilename() + (isLocal ? " (local)" : "")); |
| 74 | 95 | } else if (role == ScriptPath) { |
| 75 | 96 | return QVariant((*files)[row]->getFullPath()); |
| 76 | − } | |
| 97 | + }*/ | |
| 77 | 98 | return QVariant(); |
| 78 | 99 | } |
| 79 | 100 | |
| @@ -81,9 +102,14 @@ int ScriptsModel::rowCount(const QModelIndex& parent) const { · 1 unmodified line | ||
| 81 | 102 | if (parent.isValid()) { |
| 82 | 103 | return 0; |
| 83 | 104 | } |
| 84 | − return _localFiles.length() + _remoteFiles.length(); | |
| 105 | + return 0;// _localFiles.length() + _remoteFiles.length(); | |
| 85 | 106 | } |
| 86 | 107 | |
| 108 | +int ScriptsModel::columnCount(const QModelIndex& parent) const { | |
| 109 | + return 1; | |
| 110 | +} | |
| 111 | + | |
| 112 | + | |
| 87 | 113 | void ScriptsModel::updateScriptsLocation(const QString& newPath) { |
| 88 | 114 | _fsWatcher.removePath(_localDirectory.absolutePath()); |
| 89 | 115 | |
| @@ -93,7 +119,7 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) { · 3 unmodified lines | ||
| 93 | 119 | if (!_localDirectory.absolutePath().isEmpty()) { |
| 94 | 120 | _fsWatcher.addPath(_localDirectory.absolutePath()); |
| 95 | 121 | } |
| 96 | − } | |
| 122 | + } | |
| 97 | 123 | |
| 98 | 124 | reloadLocalFiles(); |
| 99 | 125 | } |
| @@ -101,8 +127,12 @@ void ScriptsModel::updateScriptsLocation(const QString& newPath) { · 1 unmodified line | ||
| 101 | 127 | void ScriptsModel::reloadRemoteFiles() { |
| 102 | 128 | if (!_loadingScripts) { |
| 103 | 129 | _loadingScripts = true; |
| 104 | − while (!_remoteFiles.isEmpty()) { | |
| 105 | − delete _remoteFiles.takeFirst(); | |
| 130 | + for (int i = _treeNodes.size() - 1; i >= 0; i--) { | |
| 131 | + TreeNodeBase* node = _treeNodes.at(i); | |
| 132 | + if (typeid(*node) == typeid(TreeNodeScript) && static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_REMOTE) { | |
| 133 | + delete node; | |
| 134 | + _treeNodes.removeAt(i); | |
| 135 | + } | |
| 106 | 136 | } |
| 107 | 137 | requestRemoteFiles(); |
| 108 | 138 | } |
| @@ -121,7 +151,6 @@ void ScriptsModel::requestRemoteFiles(QString marker) { · 12 unmodified lines | ||
| 121 | 151 | QNetworkRequest request(url); |
| 122 | 152 | QNetworkReply* reply = networkAccessManager.get(request); |
| 123 | 153 | connect(reply, SIGNAL(finished()), SLOT(downloadFinished())); |
| 124 | − | |
| 125 | 154 | } |
| 126 | 155 | |
| 127 | 156 | void ScriptsModel::downloadFinished() { |
| @@ -170,7 +199,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 42 unmodified lines | ||
| 170 | 199 | xml.readNext(); |
| 171 | 200 | lastKey = xml.text().toString(); |
| 172 | 201 | if (jsRegex.exactMatch(xml.text().toString())) { |
| 173 | − _remoteFiles.append(new ScriptItem(lastKey.mid(MODELS_LOCATION.length()), S3_URL + "/" + lastKey)); | |
| 202 | + _treeNodes.append(new TreeNodeScript(lastKey.mid(MODELS_LOCATION.length()), S3_URL + "/" + lastKey, SCRIPT_ORIGIN_REMOTE)); | |
| 174 | 203 | } |
| 175 | 204 | } |
| 176 | 205 | xml.readNext(); |
| @@ -178,7 +207,7 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 1 unmodified line | ||
| 178 | 207 | } |
| 179 | 208 | xml.readNext(); |
| 180 | 209 | } |
| 181 | − | |
| 210 | + rebuildTree(); | |
| 182 | 211 | endResetModel(); |
| 183 | 212 | |
| 184 | 213 | // Error handling |
| @@ -198,8 +227,15 @@ bool ScriptsModel::parseXML(QByteArray xmlFile) { · 13 unmodified lines | ||
| 198 | 227 | void ScriptsModel::reloadLocalFiles() { |
| 199 | 228 | beginResetModel(); |
| 200 | 229 | |
| 201 | − while (!_localFiles.isEmpty()) { | |
| 202 | − delete _localFiles.takeFirst(); | |
| 230 | + for (int i = _treeNodes.size() - 1; i >= 0; i--) { | |
| 231 | + TreeNodeBase* node = _treeNodes.at(i); | |
| 232 | + qDebug() << "deleting local " << i << " " << typeid(*node).name(); | |
| 233 | + if (node->getType() == TREE_NODE_TYPE_SCRIPT && | |
| 234 | + static_cast<TreeNodeScript*>(node)->getOrigin() == SCRIPT_ORIGIN_LOCAL) | |
| 235 | + { | |
| 236 | + delete node; | |
| 237 | + _treeNodes.removeAt(i); | |
| 238 | + } | |
| 203 | 239 | } |
| 204 | 240 | |
| 205 | 241 | _localDirectory.refresh(); |
| @@ -207,8 +243,35 @@ void ScriptsModel::reloadLocalFiles() { · 1 unmodified line | ||
| 207 | 243 | const QFileInfoList localFiles = _localDirectory.entryInfoList(); |
| 208 | 244 | for (int i = 0; i < localFiles.size(); i++) { |
| 209 | 245 | QFileInfo file = localFiles[i]; |
| 210 | − _localFiles.append(new ScriptItem(file.fileName(), file.absoluteFilePath())); | |
| 246 | + _treeNodes.append(new TreeNodeScript(file.fileName(), file.absoluteFilePath(), SCRIPT_ORIGIN_LOCAL)); | |
| 211 | 247 | } |
| 212 | − | |
| 248 | + rebuildTree(); | |
| 213 | 249 | endResetModel(); |
| 214 | 250 | } |
| 251 | + | |
| 252 | +void ScriptsModel::rebuildTree() { | |
| 253 | + for (int i = _treeNodes.size() - 1; i >= 0; i--) { | |
| 254 | + | |
| 255 | + if (_treeNodes.at(i)->getType() == TREE_NODE_TYPE_FOLDER) { | |
| 256 | + delete _treeNodes.at(i); | |
| 257 | + _treeNodes.removeAt(i); | |
| 258 | + } | |
| 259 | + } | |
| 260 | + QHash<QString, TreeNodeFolder*> folders; | |
| 261 | + for (int i = 0; i < _treeNodes.size(); i++) { | |
| 262 | + TreeNodeBase* node = _treeNodes.at(i); | |
| 263 | + qDebug() << "blup" << i << typeid(*node).name(); | |
| 264 | + if (typeid(*node) == typeid(TreeNodeScript)) { | |
| 265 | + TreeNodeScript* script = static_cast<TreeNodeScript*>(node); | |
| 266 | + TreeNodeFolder* parent = NULL; | |
| 267 | + QString hash; | |
| 268 | + QStringList pathList = script->getFilename().split(tr("/")); | |
| 269 | + QStringList::const_iterator pathIterator; | |
| 270 | + for (pathIterator = pathList.constBegin(); pathIterator != pathList.constEnd(); ++pathIterator) { | |
| 271 | + hash.append("/" + *pathIterator); | |
| 272 | + qDebug() << hash; | |
| 273 | + } | |
| 274 | + } | |
| 275 | + } | |
| 276 | + folders.clear(); | |
| 277 | +} | |