aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/projectexplorer/devicesupport/devicefilesystemmodel.cpp
blob: 3648a48ddc3a1328d3f6f7a24788830752e08737 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "devicefilesystemmodel.h"

#include <projectexplorer/devicesupport/idevice.h>
#include <utils/futuresynchronizer.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/utilsicons.h>

#include <QFutureWatcher>
#include <QIcon>
#include <QList>
#include <QScopeGuard>
#include <QSet>

using namespace Utils;

namespace ProjectExplorer {
namespace Internal {

enum class FileType {
    File,
    Dir,
//    Link,
    Other
};


class RemoteDirNode;
class RemoteFileNode
{
public:
    virtual ~RemoteFileNode() = default;

    FilePath m_filePath;
    FileType m_fileType = FileType::File;
    RemoteDirNode *m_parent = nullptr;
};

class RemoteDirNode : public RemoteFileNode
{
public:
    RemoteDirNode() { m_fileType = FileType::Dir; }
    ~RemoteDirNode() { qDeleteAll(m_children); }

    enum { Initial, Fetching, Finished } m_state = Initial;
    QList<RemoteFileNode *> m_children;
};

static RemoteFileNode *indexToFileNode(const QModelIndex &index)
{
    return static_cast<RemoteFileNode *>(index.internalPointer());
}

static RemoteDirNode *indexToDirNode(const QModelIndex &index)
{
    RemoteFileNode * const fileNode = indexToFileNode(index);
    QTC_CHECK(fileNode);
    return dynamic_cast<RemoteDirNode *>(fileNode);
}

using ResultType = QList<QPair<FilePath, FileType>>;

class DeviceFileSystemModelPrivate
{
public:
    IDevice::ConstPtr m_device;
    std::unique_ptr<RemoteDirNode> m_rootNode;
    QSet<QFutureWatcher<ResultType> *> m_watchers;
    FutureSynchronizer m_futureSynchronizer;
};

} // namespace Internal

using namespace Internal;

DeviceFileSystemModel::DeviceFileSystemModel(QObject *parent)
    : QAbstractItemModel(parent), d(new DeviceFileSystemModelPrivate)
{
    d->m_futureSynchronizer.setCancelOnWait(true);
}

DeviceFileSystemModel::~DeviceFileSystemModel()
{
    qDeleteAll(d->m_watchers);
    delete d;
}

void DeviceFileSystemModel::setDevice(const IDevice::ConstPtr &device)
{
    d->m_device = device;
}

bool DeviceFileSystemModel::canFetchMore(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        return !d->m_rootNode.get();
    }

    RemoteDirNode * const dirNode = indexToDirNode(parent);
    if (!dirNode)
        return false;
    if (dirNode->m_state == RemoteDirNode::Initial)
        return true;
    return false;
}

void DeviceFileSystemModel::fetchMore(const QModelIndex &parent)
{
    if (!parent.isValid()) {
        beginInsertRows(QModelIndex(), 0, 0);
        QTC_CHECK(!d->m_rootNode);
        d->m_rootNode.reset(new RemoteDirNode);
        d->m_rootNode->m_filePath = d->m_device->rootPath();
        endInsertRows();
        return;
    }
    RemoteDirNode * const dirNode = indexToDirNode(parent);
    if (!dirNode)
        return;
    if (dirNode->m_state != RemoteDirNode::Initial)
        return;
    collectEntries(dirNode->m_filePath, dirNode);
    dirNode->m_state = RemoteDirNode::Fetching;
}

bool DeviceFileSystemModel::hasChildren(const QModelIndex &parent) const
{
    if (!parent.isValid())
        return true;

    RemoteDirNode * const dirNode = indexToDirNode(parent);
    if (!dirNode)
        return false;
    if (dirNode->m_state == RemoteDirNode::Initial)
        return true;
    return dirNode->m_children.size();
}

int DeviceFileSystemModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 2; // type + name
}

QVariant DeviceFileSystemModel::data(const QModelIndex &index, int role) const
{
    const RemoteFileNode * const node = indexToFileNode(index);
    QTC_ASSERT(node, return QVariant());
    if (index.column() == 0 && role == Qt::DecorationRole) {
        if (node->m_fileType == FileType::File)
            return Utils::Icons::UNKNOWN_FILE.icon();
        if (node->m_fileType == FileType::Dir)
            return Utils::Icons::DIR.icon();
        return Utils::Icons::HELP.icon(); // Shows a question mark.
    }
    if (index.column() == 1) {
        if (role == Qt::DisplayRole) {
            if (node->m_filePath == d->m_device->rootPath())
                return QString("/");
            return node->m_filePath.fileName();
        }
        if (role == PathRole)
            return node->m_filePath.toString();
    }
    return QVariant();
}

Qt::ItemFlags DeviceFileSystemModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::NoItemFlags;
    return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

QVariant DeviceFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation != Qt::Horizontal)
        return QVariant();
    if (role != Qt::DisplayRole)
        return QVariant();
    if (section == 0)
        return tr("File Type");
    if (section == 1)
        return tr("File Name");
    return QVariant();
}

QModelIndex DeviceFileSystemModel::index(int row, int column, const QModelIndex &parent) const
{
    if (row < 0 || row >= rowCount(parent) || column < 0 || column >= columnCount(parent))
        return QModelIndex();
    if (!d->m_rootNode)
        return QModelIndex();
    if (!parent.isValid())
        return createIndex(row, column, d->m_rootNode.get());
    const RemoteDirNode * const parentNode = indexToDirNode(parent);
    QTC_ASSERT(parentNode, return QModelIndex());
    QTC_ASSERT(row < parentNode->m_children.count(), return QModelIndex());
    RemoteFileNode * const childNode = parentNode->m_children.at(row);
    return createIndex(row, column, childNode);
}

QModelIndex DeviceFileSystemModel::parent(const QModelIndex &child) const
{
    if (!child.isValid()) // Don't assert on this, since the model tester tries it.
        return QModelIndex();

    const RemoteFileNode * const childNode = indexToFileNode(child);
    QTC_ASSERT(childNode, return QModelIndex());
    if (childNode == d->m_rootNode.get())
        return QModelIndex();
    RemoteDirNode * const parentNode = childNode->m_parent;
    if (parentNode == d->m_rootNode.get())
        return createIndex(0, 0, d->m_rootNode.get());
    const RemoteDirNode * const grandParentNode = parentNode->m_parent;
    QTC_ASSERT(grandParentNode, return QModelIndex());
    return createIndex(grandParentNode->m_children.indexOf(parentNode), 0, parentNode);
}

static FileType fileType(const FilePath &path)
{
    if (path.isDir())
        return FileType::Dir;
    if (path.isFile())
        return FileType::File;
    return FileType::Other;
}

static void dirEntries(QFutureInterface<ResultType> &futureInterface, const FilePath &dir)
{
    const FilePaths entries = dir.dirEntries(QDir::NoFilter);
    ResultType result;
    for (const FilePath &entry : entries) {
        if (futureInterface.isCanceled())
            return;
        result.append(qMakePair(entry, fileType(entry)));
    }
    futureInterface.reportResult(result);
}

int DeviceFileSystemModel::rowCount(const QModelIndex &parent) const
{
    if (!d->m_rootNode)
        return 0;
    if (!parent.isValid())
        return 1;
    if (parent.column() != 0)
        return 0;
    RemoteDirNode * const dirNode = indexToDirNode(parent);
    if (!dirNode)
        return 0;
    return dirNode->m_children.count();
}

void DeviceFileSystemModel::collectEntries(const FilePath &filePath, RemoteDirNode *parentNode)
{
    // Destructor of this will delete working watchers, as they are children of this.
    QFutureWatcher<ResultType> *watcher = new QFutureWatcher<ResultType>(this);
    auto future = runAsync(dirEntries, filePath);
    d->m_futureSynchronizer.addFuture(future);
    connect(watcher, &QFutureWatcher<ResultType>::finished, this, [this, watcher, parentNode] {
        auto cleanup = qScopeGuard([watcher, this] {
            d->m_watchers.remove(watcher);
            watcher->deleteLater();
        });

        QTC_ASSERT(parentNode->m_state == RemoteDirNode::Fetching, return);
        parentNode->m_state = RemoteDirNode::Finished;

        const ResultType entries = watcher->result();
        if (entries.isEmpty())
            return;

        const int row = parentNode->m_parent
                ? parentNode->m_parent->m_children.indexOf(parentNode) : 0;
        const QModelIndex parentIndex = createIndex(row, 0, parentNode);
        beginInsertRows(parentIndex, 0, entries.count() - 1);

        for (const QPair<FilePath, FileType> &entry : entries) {
            RemoteFileNode *childNode = nullptr;
            if (entry.second == FileType::Dir)
                childNode = new RemoteDirNode;
            else
                childNode = new RemoteFileNode;
            childNode->m_filePath = entry.first;
            childNode->m_fileType = entry.second;
            childNode->m_parent = parentNode;
            parentNode->m_children.append(childNode);
        }
        endInsertRows();
    });
    d->m_watchers.insert(watcher);
    watcher->setFuture(future);
}

} // namespace ProjectExplorer