File: syncthingdownloadmodel.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (280 lines) | stat: -rw-r--r-- 10,178 bytes parent folder | download
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
#include "./syncthingdownloadmodel.h"

#include <syncthingconnector/syncthingconnection.h>
#include <syncthingconnector/utils.h>

#include <QStringBuilder>

using namespace std;
using namespace CppUtilities;

namespace Data {

SyncthingDownloadModel::PendingDir::PendingDir(const SyncthingDir *syncthingDir, unsigned int pendingItems)
    : syncthingDir(syncthingDir)
    , pendingItems(pendingItems)
{
}

bool SyncthingDownloadModel::PendingDir::operator==(const SyncthingDir *dir) const
{
    return syncthingDir == dir;
}

SyncthingDownloadModel::SyncthingDownloadModel(SyncthingConnection &connection, QObject *parent)
    : SyncthingModel(connection, parent)
    , m_dirs(connection.dirInfo())
    , m_unknownIcon(
          QIcon::fromTheme(QStringLiteral("text-x-generic"), QIcon(QStringLiteral(":/icons/hicolor/scalable/mimetypes/text-x-generic.svg"))))
    , m_pendingDownloads(0)
{
    connect(&m_connection, &SyncthingConnection::downloadProgressChanged, this, &SyncthingDownloadModel::downloadProgressChanged);
}

QHash<int, QByteArray> SyncthingDownloadModel::roleNames() const
{
    const static QHash<int, QByteArray> roles{
        { Qt::DisplayRole, "name" },
        { Qt::DecorationRole, "fileIcon" },
        { ItemPercentage, "percentage" },
        { ItemProgressLabel, "progressLabel" },
        { ItemPath, "path" },
    };
    return roles;
}

/*!
 * \brief Returns the directory info for the specified \a index. The returned object is not persistent.
 */
const SyncthingDir *SyncthingDownloadModel::dirInfo(const QModelIndex &index) const
{
    return (index.parent().isValid()
            ? dirInfo(index.parent())
            : (static_cast<size_t>(index.row()) < m_pendingDirs.size() ? m_pendingDirs[static_cast<size_t>(index.row())].syncthingDir : nullptr));
}

const SyncthingItemDownloadProgress *SyncthingDownloadModel::progressInfo(const QModelIndex &index) const
{
    if (index.parent().isValid() && static_cast<size_t>(index.parent().row()) < m_pendingDirs.size()
        && static_cast<size_t>(index.row()) < m_pendingDirs[static_cast<size_t>(index.parent().row())].syncthingDir->downloadingItems.size()) {
        return &(m_pendingDirs[static_cast<size_t>(index.parent().row())].syncthingDir->downloadingItems[static_cast<size_t>(index.row())]);
    } else {
        return nullptr;
    }
}

QModelIndex SyncthingDownloadModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        // top-level: all pending dir labels/IDs
        if (row < rowCount(parent)) {
            return createIndex(row, column, static_cast<quintptr>(-1));
        }
    } else if (!parent.parent().isValid()) {
        // dir-level: pending downloads
        if (row < rowCount(parent)) {
            return createIndex(row, column, static_cast<quintptr>(parent.row()));
        }
    }
    return QModelIndex();
}

QModelIndex SyncthingDownloadModel::parent(const QModelIndex &child) const
{
    return child.internalId() != static_cast<quintptr>(-1) ? index(static_cast<int>(child.internalId()), 0, QModelIndex()) : QModelIndex();
}

QVariant SyncthingDownloadModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    switch (orientation) {
    case Qt::Horizontal:
        switch (role) {
        case Qt::DisplayRole:
            switch (section) {
            case 0:
                return tr("Dir/item");
            case 1:
                return tr("Progress");
            }
            break;
        default:;
        }
        break;
    default:;
    }
    return QVariant();
}

QVariant SyncthingDownloadModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (index.parent().isValid()) {
        // downloading items (of dir)
        if (static_cast<size_t>(index.parent().row()) >= m_pendingDirs.size()) {
            return QVariant();
        }
        const SyncthingDir &dir = *m_pendingDirs[static_cast<size_t>(index.parent().row())].syncthingDir;
        if (static_cast<size_t>(index.row()) < dir.downloadingItems.size()) {
            const SyncthingItemDownloadProgress &progress = dir.downloadingItems[static_cast<size_t>(index.row())];
            switch (role) {
            case Qt::DisplayRole:
            case Qt::EditRole:
                switch (index.column()) {
                case 0: // file names
                    return progress.relativePath;
                case 1: // progress information
                    return progress.label;
                }
                break;
            case Qt::ToolTipRole:
                break;
            case Qt::DecorationRole:
                switch (index.column()) {
                case 0: // file icon
                    return progress.fileInfo.exists() ? m_fileIconProvider.icon(progress.fileInfo) : m_unknownIcon;
                default:;
                }
                break;
            case ItemPercentage:
                return progress.downloadPercentage;
            case ItemProgressLabel:
                return progress.label;
            case ItemPath:
                return dir.path + progress.relativePath;
            default:;
            }
        }
        return QVariant();
    }

    if (static_cast<size_t>(index.row()) >= m_pendingDirs.size()) {
        return QVariant();
    }

    // dir IDs and overall dir progress
    const SyncthingDir &dir = *m_pendingDirs[static_cast<size_t>(index.row())].syncthingDir;
    switch (role) {
    case Qt::DisplayRole:
    case Qt::EditRole:
        switch (index.column()) {
        case 0:
            return QVariant(
                (dir.label.isEmpty() ? dir.id : dir.label) % QChar(' ') % QChar('(') % QString::number(dir.downloadingItems.size()) % QChar(')'));
        case 1:
            return dir.downloadLabel;
        }
        break;
    case Qt::TextAlignmentRole:
        switch (index.column()) {
        case 0:
            break;
        case 1:
            return static_cast<int>(Qt::AlignRight | Qt::AlignVCenter);
        }
        break;
    case ItemPercentage:
        return dir.downloadPercentage;
    case ItemProgressLabel:
        return dir.downloadLabel;
    case ItemPath:
        return dir.path;
    default:;
    }
    return QVariant();
}

bool SyncthingDownloadModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(index)
    Q_UNUSED(value)
    Q_UNUSED(role)
    return false;
}

int SyncthingDownloadModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        return static_cast<int>(m_pendingDirs.size());
    } else if (!parent.parent().isValid() && parent.row() >= 0 && static_cast<size_t>(parent.row()) < m_pendingDirs.size()) {
        return static_cast<int>(m_pendingDirs[static_cast<size_t>(parent.row())].pendingItems);
    } else {
        return 0;
    }
}

void SyncthingDownloadModel::handleConfigInvalidated()
{
    beginResetModel();
    m_pendingDirs.clear();
    endResetModel();
}

void SyncthingDownloadModel::handleNewConfigAvailable()
{
    m_pendingDirs.reserve(m_connection.dirInfo().size());
}

void SyncthingDownloadModel::downloadProgressChanged()
{
    int row = 0;
    // iterate through all directories ...
    for (const SyncthingDir &dirInfo : m_connection.dirInfo()) {
        // ... and check whether the directory has been pending before
        const auto pendingIterator = find(m_pendingDirs.begin(), m_pendingDirs.end(), &dirInfo);

        // check whether the directory has downloading items ("is pending")
        if (dirInfo.downloadingItems.empty()) {
            // check whether the non-pending directory was pending before
            if (pendingIterator != m_pendingDirs.end()) {
                // remove the row for the previously pending directory
                beginRemoveRows(QModelIndex(), row, row);
                m_pendingDirs.erase(pendingIterator);
                endRemoveRows();
            }

        } else {
            // check whether the pending directory was pending before
            if (pendingIterator != m_pendingDirs.end()) {
                // get parent index and number of previous/new pending items
                const auto parentIndex(index(row, 0));
                const auto pendingItemCount(pendingIterator->pendingItems);
                const auto newPendingItemCount(dirInfo.downloadingItems.size());

                // insert/remove rows missing/surplus rows
                if (pendingItemCount < newPendingItemCount) {
                    beginInsertRows(parentIndex, static_cast<int>(pendingItemCount), static_cast<int>(newPendingItemCount - 1));
                    pendingIterator->pendingItems = newPendingItemCount;
                    endInsertRows();
                } else if (pendingItemCount > newPendingItemCount) {
                    beginRemoveRows(parentIndex, static_cast<int>(newPendingItemCount), static_cast<int>(pendingItemCount - 1));
                    pendingIterator->pendingItems = newPendingItemCount;
                    endRemoveRows();
                }

                // update the present items
                if (newPendingItemCount) {
                    static const QVector<int> roles({ Qt::DisplayRole, Qt::EditRole, Qt::DecorationRole, Qt::ForegroundRole, Qt::ToolTipRole,
                        ItemPercentage, ItemProgressLabel, ItemPath });
                    emit dataChanged(parentIndex, index(row, 1), roles);
                    emit dataChanged(index(0, 0, parentIndex), index(static_cast<int>(newPendingItemCount - 1), 1, parentIndex), roles);
                }

            } else {
                // add the new directory
                beginInsertRows(QModelIndex(), row, row);
                m_pendingDirs.emplace_back(&dirInfo, 0);
                endInsertRows();
                // add new pending items
                beginInsertRows(index(row, row), 0, static_cast<int>(dirInfo.downloadingItems.size() - 1));
                m_pendingDirs.back().pendingItems = dirInfo.downloadingItems.size();
                endInsertRows();
            }
            ++row;
        }
    }
}

} // namespace Data