-
Notifications
You must be signed in to change notification settings - Fork 671
/
Copy pathfolderstatusmodel.cpp
467 lines (415 loc) · 18.1 KB
/
folderstatusmodel.cpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Copyright (C) by Klaas Freitag <freitag@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "folderstatusmodel.h"
#include "account.h"
#include "accountstate.h"
#include "common/asserts.h"
#include "folderman.h"
#include "gui/quotainfo.h"
#include "theme.h"
#include "resources/resources.h"
#include "libsync/graphapi/space.h"
#include "libsync/graphapi/spacesmanager.h"
#include <QApplication>
#include <QDir>
#include <QFileIconProvider>
#include <QRandomGenerator>
#include <QVarLengthArray>
#include <set>
using namespace std::chrono_literals;
Q_DECLARE_METATYPE(QPersistentModelIndex)
namespace OCC {
Q_LOGGING_CATEGORY(lcFolderStatus, "gui.folder.model", QtInfoMsg)
namespace {
// minimum delay between progress updates
constexpr auto progressUpdateTimeOutC = 1s;
QString statusIconName(Folder *f)
{
auto status = f->syncResult();
if (!f->accountState()->isConnected()) {
status.setStatus(SyncResult::Status::Offline);
} else if (f->syncPaused() || f->accountState()->state() == AccountState::PausedDueToMetered) {
status.setStatus(SyncResult::Status::Paused);
}
return Theme::instance()->syncStateIconName(status);
}
class SubFolderInfo
{
public:
class Progress
{
public:
Progress() { }
bool isNull() const { return _progressString.isEmpty() && _overallSyncString.isEmpty(); }
QString _progressString;
QString _overallSyncString;
float _overallPercent = 0;
};
SubFolderInfo(Folder *folder)
: _folder(folder)
{
}
Folder *const _folder;
Progress _progress;
std::chrono::steady_clock::time_point _lastProgressUpdated = std::chrono::steady_clock::now();
ProgressInfo::Status _lastProgressUpdateStatus = ProgressInfo::None;
};
void computeProgress(const ProgressInfo &progress, SubFolderInfo::Progress *pi)
{
// find the single item to display: This is going to be the bigger item, or the last completed
// item if no items are in progress.
SyncFileItem curItem = progress._lastCompletedItem;
qint64 curItemProgress = -1; // -1 means finished
qint64 biggerItemSize = 0;
quint64 estimatedUpBw = 0;
quint64 estimatedDownBw = 0;
QStringList allFilenames;
for (const auto &citm : progress._currentItems) {
if (curItemProgress == -1 || (ProgressInfo::isSizeDependent(citm._item) && biggerItemSize < citm._item._size)) {
curItemProgress = citm._progress.completed();
curItem = citm._item;
biggerItemSize = citm._item._size;
}
if (citm._item._direction != SyncFileItem::Up) {
estimatedDownBw += progress.fileProgress(citm._item).estimatedBandwidth;
} else {
estimatedUpBw += progress.fileProgress(citm._item).estimatedBandwidth;
}
allFilenames.append(QApplication::translate("FolderStatus", "'%1'").arg(citm._item._file));
}
if (curItemProgress == -1) {
curItemProgress = curItem._size;
}
const QString itemFileName = curItem._file;
const QString kindString = Progress::asActionString(curItem);
QString fileProgressString;
if (ProgressInfo::isSizeDependent(curItem)) {
// quint64 estimatedBw = progress.fileProgress(curItem).estimatedBandwidth;
if (estimatedUpBw || estimatedDownBw) {
/*
//: Example text: "uploading foobar.png (1MB of 2MB) time left 2 minutes at a rate of 24Kb/s"
fileProgressString = tr("%1 %2 (%3 of %4) %5 left at a rate of %6/s")
.arg(kindString, itemFileName, s1, s2,
Utility::durationToDescriptiveString(progress.fileProgress(curItem).estimatedEta),
Utility::octetsToString(estimatedBw) );
*/
//: Example text: "Syncing 'foo.txt', 'bar.txt'"
fileProgressString = QApplication::translate("FolderStatus", "Syncing %1").arg(allFilenames.join(QStringLiteral(", ")));
if (estimatedDownBw > 0) {
fileProgressString.append(QApplication::translate("FolderStatus", ", ⬇️ %1/s").arg(Utility::octetsToString(estimatedDownBw)));
}
if (estimatedUpBw > 0) {
fileProgressString.append(QApplication::translate("FolderStatus", ", ⬆️ %1/s").arg(Utility::octetsToString(estimatedUpBw)));
}
} else {
//: Example text: "uploading foobar.png (2MB of 2MB)"
fileProgressString = QApplication::translate("FolderStatus", "%1 %2 (%3 of %4)")
.arg(kindString, itemFileName, Utility::octetsToString(curItemProgress), Utility::octetsToString(curItem._size));
}
} else if (!kindString.isEmpty()) {
//: Example text: "uploading foobar.png"
fileProgressString = QApplication::translate("FolderStatus", "%1 %2").arg(kindString, itemFileName);
}
pi->_progressString = fileProgressString;
// overall progress
qint64 completedSize = progress.completedSize();
qint64 completedFile = progress.completedFiles();
qint64 currentFile = progress.currentFile();
qint64 totalSize = qMax(completedSize, progress.totalSize());
qint64 totalFileCount = qMax(currentFile, progress.totalFiles());
QString overallSyncString;
if (totalSize > 0) {
const QString s1 = Utility::octetsToString(completedSize);
const QString s2 = Utility::octetsToString(totalSize);
if (progress.trustEta()) {
//: Example text: "5 minutes left, 12 MB of 345 MB, file 6 of 7"
overallSyncString = QApplication::translate("FolderStatus", "%5 left, %1 of %2, file %3 of %4")
.arg(s1, s2)
.arg(currentFile)
.arg(totalFileCount)
.arg(Utility::durationToDescriptiveString1(std::chrono::milliseconds(progress.totalProgress().estimatedEta)));
} else {
//: Example text: "12 MB of 345 MB, file 6 of 7"
overallSyncString = QApplication::translate("FolderStatus", "%1 of %2, file %3 of %4").arg(s1, s2).arg(currentFile).arg(totalFileCount);
}
} else if (totalFileCount > 0) {
// Don't attempt to estimate the time left if there is no kb to transfer.
overallSyncString = QApplication::translate("FolderStatus", "file %1 of %2").arg(currentFile).arg(totalFileCount);
}
pi->_overallSyncString = overallSyncString;
int overallPercent = 0;
if (totalFileCount > 0) {
// Add one 'byte' for each file so the percentage is moving when deleting or renaming files
overallPercent = qRound(double(completedSize + completedFile) / double(totalSize + totalFileCount) * 100.0);
}
pi->_overallPercent = qBound(0, overallPercent, 100);
}
}
FolderStatusModel::FolderStatusModel(QObject *parent)
: QAbstractListModel(parent)
, _accountState(nullptr)
{
}
FolderStatusModel::~FolderStatusModel() { }
void FolderStatusModel::setAccountState(const AccountStatePtr &accountState)
{
beginResetModel();
_folders.clear();
if (_accountState != accountState) {
Q_ASSERT(!_accountState);
_accountState = accountState;
connect(FolderMan::instance(), &FolderMan::folderSyncStateChange, this, &FolderStatusModel::slotFolderSyncStateChange);
if (accountState->supportsSpaces()) {
connect(accountState->account()->spacesManager(), &GraphApi::SpacesManager::updated, this,
[this] { Q_EMIT dataChanged(index(0, 0), index(rowCount() - 1, 0)); });
connect(accountState->account()->spacesManager(), &GraphApi::SpacesManager::spaceChanged, this, [this](auto *space) {
for (int i = 0; i < rowCount(); ++i) {
auto *f = _folders[i]->_folder;
if (f->accountState()->supportsSpaces() && f->spaceId() == space->drive().getId()) {
Q_EMIT dataChanged(index(i, 0), index(i, 0));
break;
}
}
});
}
}
for (const auto &f : FolderMan::instance()->folders()) {
if (!accountState)
break;
if (f->accountState() != accountState)
continue;
_folders.push_back(std::make_unique<SubFolderInfo>(f));
connect(ProgressDispatcher::instance(), &ProgressDispatcher::progressInfo, this, [f, this](Folder *folder, const ProgressInfo &progress) {
if (folder == f) {
slotSetProgress(progress, f);
}
});
}
endResetModel();
}
QVariant FolderStatusModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::EditRole)
return QVariant();
const auto &folderInfo = _folders.at(index.row());
auto f = folderInfo->_folder;
if (!f)
return QVariant();
const auto getSpace = [&]() -> GraphApi::Space * {
if (_accountState->supportsSpaces()) {
return _accountState->account()->spacesManager()->space(f->spaceId());
}
return nullptr;
};
switch (static_cast<Roles>(role)) {
case Roles::Subtitle: {
if (auto *space = getSpace()) {
if (!space->drive().getDescription().isEmpty()) {
return space->drive().getDescription();
}
}
return tr("Local folder: %1").arg(f->shortGuiLocalPath());
}
case Roles::FolderErrorMsg: {
auto errors = f->syncResult().errorStrings();
const auto legacyError = FolderMan::instance()->unsupportedConfiguration(f->path());
if (!legacyError) {
errors.append(legacyError.error());
}
if (f->syncResult().hasUnresolvedConflicts()) {
errors.append(tr("There are unresolved conflicts."));
}
if (f->isReady() && f->virtualFilesEnabled() && f->vfs().mode() == Vfs::Mode::WithSuffix) {
errors.append({
tr("The suffix VFS plugin is deprecated and will be removed in the 7.0 release.\n"
"Please use the context menu and select \"Disable virtual file support\" to ensure future access to your synced files.\n"
"You are going to lose access to your sync folder if you do not do so!"),
});
}
return errors;
}
case Roles::DisplayName: {
if (auto *space = getSpace()) {
return space->displayName();
}
return f->displayName();
}
case Roles::FolderImageUrl:
if (f->accountState()->supportsSpaces()) {
// TODO: the url hast random parts to enforce a reload
return QStringLiteral("image://space/%1/%2").arg(QString::number(QRandomGenerator::global()->generate()), f->spaceId());
}
return QStringLiteral("image://ownCloud/core/folder-sync");
case Roles::FolderStatusUrl:
return QStringLiteral("image://ownCloud/core/states/%1").arg(statusIconName(f));
case Roles::SyncProgressItemString:
return folderInfo->_progress._progressString;
case Roles::SyncProgressOverallPercent:
return folderInfo->_progress._overallPercent / 100.0;
case Roles::SyncProgressOverallString:
return folderInfo->_progress._overallSyncString;
case Roles::Priority:
// everything will be sorted in descending order, multiply the priority by 100 and prefer A over Z by appling a negative factor
return QVariant::fromValue(f->priority() * 100 - (f->displayName().isEmpty() ? 0 : static_cast<int64_t>(f->displayName().at(0).toLower().unicode())));
case Roles::Quota: {
qint64 used{};
qint64 total{};
if (_accountState->supportsSpaces()) {
if (auto spacesManager = f->accountState()->account()->spacesManager()) {
const auto *space = spacesManager->space(f->spaceId());
if (space) {
const auto quota = space->drive().getQuota();
if (quota.isValid()) {
used = quota.getUsed();
total = quota.getTotal();
}
}
}
} else {
used = f->accountState()->quotaInfo()->lastQuotaUsedBytes();
total = f->accountState()->quotaInfo()->lastQuotaTotalBytes();
}
if (total <= 0) {
return {};
}
return tr("%1 of %2 used").arg(Utility::octetsToString(used), Utility::octetsToString(total));
}
case Roles::ToolTip: {
if (_accountState->isConnected()) {
return tr("The status of %1 is %2").arg(f->displayName(), Utility::enumToDisplayName(f->syncResult().status()));
} else {
return tr("The account %1 is currently not connected.").arg(f->accountState()->account()->displayName());
}
}
case Roles::Folder:
return QVariant::fromValue(f);
}
return {};
}
Folder *FolderStatusModel::folder(const QModelIndex &index) const
{
Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
return _folders.at(index.row())->_folder;
}
int FolderStatusModel::rowCount(const QModelIndex &parent) const
{
Q_ASSERT(!parent.isValid());
return static_cast<int>(_folders.size());
}
QHash<int, QByteArray> FolderStatusModel::roleNames() const
{
return {
{static_cast<int>(Roles::DisplayName), "displayName"},
{static_cast<int>(Roles::Subtitle), "subtitle"},
{static_cast<int>(Roles::FolderImageUrl), "imageUrl"},
{static_cast<int>(Roles::FolderStatusUrl), "statusUrl"},
{static_cast<int>(Roles::SyncProgressOverallPercent), "progress"},
{static_cast<int>(Roles::SyncProgressOverallString), "overallText"},
{static_cast<int>(Roles::SyncProgressItemString), "itemText"},
{static_cast<int>(Roles::FolderErrorMsg), "errorMsg"},
{static_cast<int>(Roles::Quota), "quota"},
{static_cast<int>(Roles::ToolTip), "toolTip"},
{static_cast<int>(Roles::Folder), "folder"},
};
}
void FolderStatusModel::slotUpdateFolderState(Folder *folder)
{
if (!folder)
return;
for (int i = 0; i < _folders.size(); ++i) {
if (_folders.at(i)->_folder == folder) {
Q_EMIT dataChanged(index(i, 0), index(i, 0));
}
}
}
void FolderStatusModel::slotSetProgress(const ProgressInfo &progress, Folder *f)
{
if (!qobject_cast<QWidget *>(QObject::parent())->isVisible()) {
return; // for https://github.com/owncloud/client/issues/2648#issuecomment-71377909
}
const int folderIndex = indexOf(f);
if (folderIndex < 0) {
return;
}
const auto &folder = _folders.at(folderIndex);
auto *pi = &folder->_progress;
// depending on the use of virtual files or small files this slot might be called very often.
// throttle the model updates to prevent an needlessly high cpu usage used on ui updates.
if (folder->_lastProgressUpdateStatus != progress.status() || (std::chrono::steady_clock::now() - folder->_lastProgressUpdated > progressUpdateTimeOutC)) {
folder->_lastProgressUpdateStatus = progress.status();
switch (progress.status()) {
case ProgressInfo::None:
Q_UNREACHABLE();
break;
case ProgressInfo::Discovery:
if (!progress._currentDiscoveredRemoteFolder.isEmpty()) {
pi->_overallSyncString = tr("Checking for changes in remote '%1'").arg(progress._currentDiscoveredRemoteFolder);
} else if (!progress._currentDiscoveredLocalFolder.isEmpty()) {
pi->_overallSyncString = tr("Checking for changes in local '%1'").arg(progress._currentDiscoveredLocalFolder);
}
break;
case ProgressInfo::Reconcile:
pi->_overallSyncString = tr("Reconciling changes");
break;
case ProgressInfo::Propagation:
Q_FALLTHROUGH();
case ProgressInfo::Done:
computeProgress(progress, pi);
}
Q_EMIT dataChanged(index(folderIndex, 0), index(folderIndex, 0));
folder->_lastProgressUpdated = std::chrono::steady_clock::now();
}
}
int FolderStatusModel::indexOf(Folder *f) const
{
const auto found = std::find_if(_folders.cbegin(), _folders.cend(), [f](const auto &it) { return it->_folder == f; });
if (found == _folders.cend()) {
return -1;
}
return std::distance(_folders.cbegin(), found);
}
void FolderStatusModel::slotFolderSyncStateChange(Folder *f)
{
if (!f) {
return;
}
const int folderIndex = indexOf(f);
if (folderIndex < 0) {
return;
}
auto &pi = _folders.at(folderIndex)->_progress;
SyncResult::Status state = f->syncResult().status();
if (!f->canSync()) {
// Reset progress info.
pi = {};
} else if (state == SyncResult::NotYetStarted) {
pi = {};
pi._overallSyncString = tr("Queued");
} else if (state == SyncResult::SyncPrepare) {
pi = {};
pi._overallSyncString = Utility::enumToDisplayName(SyncResult::SyncPrepare);
} else if (state == SyncResult::Problem || state == SyncResult::Success || state == SyncResult::Error) {
// Reset the progress info after a sync.
pi = {};
}
// update the icon etc. now
slotUpdateFolderState(f);
}
void FolderStatusModel::resetFolders()
{
setAccountState(_accountState);
}
} // namespace OCC