-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathaccountfolderscontroller.cpp
More file actions
381 lines (309 loc) · 16 KB
/
accountfolderscontroller.cpp
File metadata and controls
381 lines (309 loc) · 16 KB
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
/*
* Copyright (C) Lisa Reese <lisa.reese@kiteworks.com>
*
* 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 "accountfolderscontroller.h"
#include "accountfoldersview.h"
#include "accountmodalwidget.h"
#include "application.h"
#include "commonstrings.h"
#include "configfile.h"
#include "foldermodelcontroller.h"
#include "folderwizard.h"
#include "selectivesyncwidget.h"
#include "settingsdialog.h"
#include "guiutility.h"
#include "networkjobs.h"
#include "openfilemanager.h"
#include "theme.h"
#include <QMessageBox>
#include <QtWidgets/qabstractbutton.h>
namespace OCC {
Q_LOGGING_CATEGORY(lcAccountFoldersController, "gui.accountfolders.controller", QtInfoMsg)
AccountFoldersController::AccountFoldersController(AccountState *state, AccountFoldersView *view, QObject *parent)
: QObject{parent}
, _accountState(state)
, _view(view)
{
if (!_view || !_accountState || !_accountState->account())
return;
_accountId = _accountState->account()->uuid();
buildMenuActions();
FolderModelController *modelController = new FolderModelController(_accountId, this);
connect(modelController, &FolderModelController::currentFolderChanged, this, &AccountFoldersController::onFolderChanged);
QStandardItemModel *model = modelController->itemModel();
_view->setItemModels(model, modelController->selectionModel());
connect(_view, &AccountFoldersView::addFolderTriggered, this, &AccountFoldersController::onAddFolder);
FolderMan *folderMan = FolderMan::instance();
modelController->connectSignals(folderMan);
connect(folderMan, &FolderMan::unsyncedSpaceCountChanged, this, &AccountFoldersController::onUnsyncedSpaceCountChanged);
}
void AccountFoldersController::onUnsyncedSpaceCountChanged(const QUuid &accountId, int unsyncedSpaceCount, int totalSpaceCount)
{
if (accountId != _accountId)
return;
_view->enableAddFolder(unsyncedSpaceCount > 0);
// sometimes we have folders that are related to a space which has been removed server side. Because of this,
// sometimes the total "synced" count can be larger than the space count, so min the diff to get something reasonable
int reasonableCount = qMin(totalSpaceCount - unsyncedSpaceCount, totalSpaceCount);
_view->setSyncedFolderCount(reasonableCount, totalSpaceCount);
}
void AccountFoldersController::onFolderChanged(OCC::Folder *folder)
{
if (_currentFolder == folder)
return;
_currentFolder = folder;
}
void AccountFoldersController::onAddFolder()
{
if (!_accountState || !_accountState->account()) {
return;
}
FolderWizard *folderWizard = new FolderWizard(_accountState->account(), nullptr);
folderWizard->setAttribute(Qt::WA_DeleteOnClose);
connect(folderWizard, &FolderWizard::folderWizardAccepted, this, &AccountFoldersController::onFolderWizardAccepted);
emit requestShowModalWidget(folderWizard);
}
void AccountFoldersController::onFolderWizardAccepted(OCC::FolderMan::SyncConnectionDescription result)
{
if (!_accountState) {
return;
}
// The gui should not allow users to selectively choose any sync lists if vfs is enabled, but this kind of check was
// originally in play here so...keep it just in case.
if (result.useVirtualFiles && !result.selectiveSyncBlackList.empty()) {
result.selectiveSyncBlackList.clear();
}
// Refactoring todo: turn this into a signal/requestAddFolder
FolderMan::instance()->addFolderFromGui(_accountState, result);
}
void AccountFoldersController::buildMenuActions()
{
QList<QAction *> itemActions;
// show in finder
_showInSystemFolder = new QAction(CommonStrings::showInFileBrowser(), this);
_showInSystemFolder->setObjectName("showInFileSystemAction");
itemActions.push_back(_showInSystemFolder);
connect(_showInSystemFolder, &QAction::triggered, this, &AccountFoldersController::onShowInSystemFolder);
if (_accountState->account()->capabilities().privateLinkPropertyAvailable()) {
_showInBrowser = new QAction(CommonStrings::showInWebBrowser(), this);
_showInBrowser->setObjectName("showInBrowserAction");
itemActions.push_back(_showInBrowser);
connect(_showInBrowser, &QAction::triggered, this, &AccountFoldersController::onShowInBrowser);
}
QAction *separator = new QAction(this);
// I don't think this is particularly useful but shouldn't hurt
separator->setObjectName("sparatorAction");
separator->setSeparator(true);
itemActions.push_back(separator);
_forceSync = new QAction(tr("Sync now"), this);
_forceSync->setObjectName("forceSyncAction");
itemActions.push_back(_forceSync);
connect(_forceSync, &QAction::triggered, this, &AccountFoldersController::onForceSync);
_pauseSync = new QAction(this);
_pauseSync->setObjectName("pauseSyncAction");
itemActions.push_back(_pauseSync);
connect(_pauseSync, &QAction::triggered, this, &AccountFoldersController::onTogglePauseSync);
_chooseSync = new QAction(tr("Manage subfolder sync"), this);
_chooseSync->setObjectName("selectiveSyncAction");
Vfs::Mode mode = VfsPluginManager::instance().bestAvailableVfsMode();
if (mode == Vfs::WindowsCfApi) {
// need extra checks:
if (Theme::instance()->forceVirtualFilesOption()) {
// get rid of choose sync action as it can never be enabled
// yes this is a slightly strange way to do this but think of the alternative code...blech.
delete _chooseSync;
_chooseSync = nullptr;
} else {
_enableVfs = new QAction(this);
_enableVfs->setObjectName("enableVfsAction");
itemActions.push_back(_enableVfs);
connect(_enableVfs, &QAction::triggered, this, &AccountFoldersController::onEnableVfs);
}
}
if (_chooseSync) {
itemActions.push_back(_chooseSync);
connect(_chooseSync, &QAction::triggered, this, &AccountFoldersController::onChooseSync);
}
//: This shows as 'Remove folder sync' or 'Remove Space sync'
QString removeSyncString = tr("Remove %1 sync").arg(CommonStrings::space());
_removeSync = new QAction(removeSyncString, this);
_removeSync->setObjectName("removeFolderSyncAction");
itemActions.push_back(_removeSync);
connect(_removeSync, &QAction::triggered, this, &AccountFoldersController::onRemoveSync);
connect(_view, &AccountFoldersView::requestActionsUpdate, this, &AccountFoldersController::updateActions);
_view->setFolderActions(itemActions);
}
void AccountFoldersController::onEnableVfs()
{
if (!_currentFolder || VfsPluginManager::instance().bestAvailableVfsMode() != Vfs::WindowsCfApi)
return;
if (!_currentFolder->virtualFilesEnabled()) {
qCInfo(lcAccountFoldersController) << "Enabling vfs support for folder" << _currentFolder->path();
// Change the folder vfs mode and load the plugin
_currentFolder->setVirtualFilesEnabled(true);
} else {
QMessageBox msgBox(QMessageBox::Question, tr("Disable virtual file support?"),
tr("This action will disable virtual file support. As a consequence contents of folders that "
"are currently marked as 'available online only' will be downloaded."
"\n\n"
"The only advantage of disabling virtual file support is that the selective sync feature "
"will become available again."
"\n\n"
"This action will abort any currently running synchronization."),
QMessageBox::Yes | QMessageBox::No, ocApp()->gui()->settingsDialog());
msgBox.setObjectName("confirmDisableVfsDialog");
msgBox.button(QMessageBox::Yes)->setText(tr("Disable support"));
msgBox.button(QMessageBox::Yes)->setObjectName("disableVfsButton");
msgBox.button(QMessageBox::No)->setText(tr("Cancel"));
msgBox.button(QMessageBox::No)->setObjectName("cancelDisableVfsButton");
int result = msgBox.exec();
if (result == QDialog::Rejected)
return;
qCInfo(lcAccountFoldersController) << "Disabling vfs support for folder" << _currentFolder->path();
// Also wipes virtual files, schedules remote discovery
_currentFolder->setVirtualFilesEnabled(false);
}
}
void AccountFoldersController::updateActions()
{
// the checks for _currentFolder are twofold:
// obvs we don't want to try to access values that aren't there
// furthermore, we do need to disable if the current folder selection
// is empty so it's actually a key part of the evaluation for action state here
// so doing a general if(!_curentFolder) return; handling is not correct
_showInSystemFolder->setEnabled(_currentFolder && QFileInfo::exists(_currentFolder->path()));
if (_showInBrowser)
_showInBrowser->setEnabled(_currentFolder && _currentFolder->isConnected() && _currentFolder->isAvailable());
_forceSync->setEnabled(_currentFolder && _currentFolder->canSync());
_forceSync->setText(_currentFolder && _currentFolder->isSyncRunning() ? tr("Restart sync") : tr("Sync now"));
_pauseSync->setText(_currentFolder && _currentFolder->syncPaused() ? tr("Resume sync") : tr("Pause sync"));
// this is a bit nuanced: we want to enable pause if the folder is disconnected as this is always a safe operation. if it *is* connected
// we only want to enabled the action if the folder is available. If the folder is not available pause makes no sense as there is nothing to sync to
// as the space is missing, server side.
_pauseSync->setEnabled(_currentFolder && (!_currentFolder->isConnected() || _currentFolder->isAvailable()));
_removeSync->setEnabled(_currentFolder);
if (_enableVfs) {
_enableVfs->setText(_currentFolder && _currentFolder->virtualFilesEnabled() ? tr("Deactivate virtual files") : tr("Activate virtual files"));
// we can't block existence of the action when the folder path doesn't support vfs, so disable it here just in case
// todo: DC-219 hopefully this check can go away
QString pathError = Vfs::pathSupportDetail(_currentFolder->path(), Vfs::WindowsCfApi);
_enableVfs->setEnabled(pathError.isEmpty() && _currentFolder->canSync());
}
if (_chooseSync)
_chooseSync->setEnabled(_currentFolder && _currentFolder->isConnected() && _currentFolder->isAvailable() && !_currentFolder->virtualFilesEnabled());
}
void OCC::AccountFoldersController::onShowInSystemFolder()
{
if (!_currentFolder)
return;
if (QFileInfo::exists(_currentFolder->path()))
showInFileManager(_currentFolder->path());
}
void AccountFoldersController::onShowInBrowser()
{
if (!_currentFolder)
return;
QString path = _currentFolder->remotePathTrailingSlash();
QUrl davUrl = _currentFolder->webDavUrl();
fetchPrivateLinkUrl(_accountState->account(), davUrl, path, this, [](const QUrl &url) { Utility::openBrowser(url, nullptr); });
}
void AccountFoldersController::onForceSync()
{
if (!_currentFolder)
return;
if (NetworkInformation::instance()->isMetered() && ConfigFile().pauseSyncWhenMetered()) {
QMessageBox messageBox(QMessageBox::Question, tr("Internet connection is metered"),
tr("Synchronization is paused because the Internet connection is a metered connection"
"<p>Do you really want to force a Synchronization now?"),
QMessageBox::Yes | QMessageBox::No, ocApp()->gui()->settingsDialog());
messageBox.setObjectName("confirmForceSyncWhenMeteredDialog");
messageBox.button(QMessageBox::No)->setObjectName("cancelForceSyncWhenMeteredButton");
messageBox.button(QMessageBox::Yes)->setObjectName("forceSyncWhenMeteredButton");
int result = messageBox.exec();
if (result == QMessageBox::No)
return;
}
FolderMan::instance()->forceFolderSync(_currentFolder);
}
void AccountFoldersController::onTogglePauseSync()
{
if (!_currentFolder)
return;
bool currentlyPaused = _currentFolder->syncPaused();
if (!currentlyPaused) {
if (_currentFolder->isSyncRunning()) {
QMessageBox msgbox(QMessageBox::Question, tr("Sync Running"), tr("The sync operation is running.<br/>Do you want to stop it?"),
QMessageBox::Yes | QMessageBox::No, ocApp()->gui()->settingsDialog());
msgbox.setDefaultButton(QMessageBox::No);
msgbox.setObjectName("confirmPauseRunningSyncDialog");
msgbox.button(QMessageBox::Yes)->setObjectName("pauseRunningSyncButton");
msgbox.button(QMessageBox::No)->setObjectName("cancelPausRunningSyncButton");
int result = msgbox.exec();
if (result == QMessageBox::No)
return;
_currentFolder->slotTerminateSync(tr("Sync paused by user"));
}
}
// todo: what is this really for? if we are pausing do we actually need it or only on resume?
// if we do need it, it should probably move to the setSyncPaused function so it never gets missed
_currentFolder->slotNextSyncFullLocalDiscovery(); // ensure we don't forget about local errors
_currentFolder->setSyncPaused(!currentlyPaused);
}
void AccountFoldersController::onRemoveSync()
{
if (!_currentFolder)
return;
qCInfo(lcAccountFoldersController) << "Remove Folder " << _currentFolder->path();
QString shortGuiLocalPath = _currentFolder->shortGuiLocalPath();
QMessageBox msgBox(QMessageBox::Question, tr("Confirm Folder Sync Connection Removal"),
tr("<p>Do you really want to stop syncing the folder <i>%1</i>?</p>"
"<p><b>Note:</b> This will <b>not</b> delete any files.</p>")
.arg(shortGuiLocalPath),
QMessageBox::Yes | QMessageBox::No, ocApp()->gui()->settingsDialog());
msgBox.button(QMessageBox::Yes)->setText(tr("Remove Folder Sync Connection"));
msgBox.button(QMessageBox::No)->setText(tr("Cancel"));
msgBox.setObjectName("confirmRemoveFolderSyncDialog");
msgBox.button(QMessageBox::Yes)->setObjectName("removeFolderSyncButton");
msgBox.button(QMessageBox::No)->setObjectName("cancelRemoveFolderSyncButton");
int result = msgBox.exec();
if (result == QMessageBox::No)
return;
// todo: make it a signal
FolderMan::instance()->removeFolderFromGui(_currentFolder);
}
void AccountFoldersController::onChooseSync()
{
if (!_currentFolder || !_accountState || !_accountState->account()) {
return;
}
bool ok;
QSet<QString> selectiveSyncList = _currentFolder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, ok);
if (!ok)
return;
// this widget gets reparented to the layout in the AccountModalWidget so should be cleaned up there
auto *selectiveSync = new SelectiveSyncWidget(_accountState->account(), nullptr);
selectiveSync->setDavUrl(_currentFolder->webDavUrl());
selectiveSync->setFolderInfo(_currentFolder->remotePath(), _currentFolder->displayName(), selectiveSyncList);
// the account modal widget is auto-deleted by the handler, currently the AccountView, when it emits "finished"
// as with so many of these async impls the details are way too complicated but that's a topic for a future refactoring
auto *modalWidget = new AccountModalWidget(tr("Choose what to sync"), selectiveSync, nullptr);
modalWidget->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
connect(modalWidget, &AccountModalWidget::accepted, this, [selectiveSync, this] {
if (!_currentFolder)
return;
_currentFolder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, selectiveSync->createBlackList());
FolderMan::instance()->forceFolderSync(_currentFolder);
});
emit requestAccountModalWidget(modalWidget);
}
}