Skip to content

Commit 7f4beb4

Browse files
authored
[DC-150] refactor Account shared pointer (#12311)
* new point * switch shared pointer to qpointer in connection validater and added nullptr checks * removed accessor for nam on abstract core job refactored CheckServerJobFactory to take a raw account pointer. Also removed the parent arg to the factory as it's fairly useless adjusted the parenting of the one-off nam created in the factory to be sure it is parented by the job, so it is cleaned up along with the job. minimal update to oauth to deal with the new factory interface * lots of small cleanups/updates/removing accessors that have no business existing. * removed the oc10 related folderwizardremotepath and updated deps also cleaned up some additional accessors in the abstract network job * updated AccountBasedOAuth and request auth impls to use *account * comments and fixed typo * removed dep on account from activity data and updated uses * small cleanups, removed some accessors, including those to get the account, and got rid of account member for DiscoverySingleDirectoryJob as it was never used * changed folder wizard and related class dependencies from the account state to the spacesmanager as that was all that was needed eliminated more accessors to get the account, replaced more accountptr with just account*. identified cases where the account dep can be removed completely with correct refactoring to make jobs only run in controllers/managers (where it's reasonable). removed no longer used deprecated function from SpacesManager * fixed forward declaration * fixed the long standing issue in folderstatusmodel with the convoluted setAccountStatus function. Now the account status handling is much safer and clearer. * make gcc happy and added todo's to really fix this mess * did an interim refactor of the account storage in socketapi. Instead of a qset of accountptr instances I changed it to a hash of uuid -> QPointer<account> I think we can actually replace this with a qset of defaultSyncRoot since the roots are unique to the account, and that is all the class needs to function, but don't want to get too far off track with this pr so do that another time * more random conversions from shared to raw or q pointer * updated the syncEngine account pointer * AbstractNetworkJob now uses QPointer for the account (as do all subclasses now) * made account member in AbstractNetworkJob private to ensure it's not used directly by subclasses updated fetchServerSettings and discovery classes * updated propagator classes * finished the bulk of the updates. vfs is still to be updated so currently vfs tests don't pass - I am pushing this so I can pull the vfs changes * merged the vfs changes in, updated the accountPtr to raw pointer in vfs - still need to turn it into a qpointer and check for nulls but it works now (local tests passed too) so should be no problem. * removed accountfwd.h and all associated refs to AccountPtr - it's now gone. also did some basic cleanup on some includes as well as updating to #pragma once where I ran into it * missed a nullptr check that crashed on quit immediately after adding account
1 parent 48f41ff commit 7f4beb4

File tree

111 files changed

+733
-1210
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+733
-1210
lines changed

src/common/vfs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ const VfsPluginManager &VfsPluginManager::instance()
276276
return *_instance;
277277
}
278278

279-
VfsSetupParams::VfsSetupParams(const AccountPtr &account, const QUrl &baseUrl, bool groupInSidebar, SyncEngine *syncEngine)
279+
VfsSetupParams::VfsSetupParams(Account *account, const QUrl &baseUrl, bool groupInSidebar, SyncEngine *syncEngine)
280280
: account(account)
281281
, _baseUrl(baseUrl)
282282
, _groupInSidebar(groupInSidebar)

src/common/vfs.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,38 @@
1313
*/
1414
#pragma once
1515

16+
// if this is included, to support a qpointer on the account, all hell breaks loose wrt endless "cannot open include file: 'owncloudlib.h'
17+
// No such file or directory" which is fundamental to the libsync exports
18+
// so unfortunately we can't make a qpointer for the account here - do the update described below asap to get rid of the dependency
19+
// #include "libsync/account.h"
1620
#include "assert.h"
1721
#include "csync/csync.h"
18-
#include "libsync/accountfwd.h"
1922
#include "ocsynclib.h"
2023
#include "pinstate.h"
2124
#include "result.h"
2225
#include "syncfilestatus.h"
2326
#include "utility.h"
2427

2528
#include <QObject>
26-
#include <QSharedPointer>
29+
#include <QPointer>
2730
#include <QUrl>
2831
#include <QVersionNumber>
2932

3033
#include <memory>
3134

35+
3236
namespace OCC {
3337

3438
class Account;
39+
3540
class SyncJournalDb;
3641
class SyncFileItem;
3742
class SyncEngine;
3843

3944
/** Collection of parameters for initializing a Vfs instance. */
4045
struct OCSYNC_EXPORT VfsSetupParams
4146
{
42-
explicit VfsSetupParams(const AccountPtr &account, const QUrl &baseUrl, bool groupInSidebar, SyncEngine *syncEngine);
47+
explicit VfsSetupParams(Account *account, const QUrl &baseUrl, bool groupInSidebar, SyncEngine *syncEngine);
4348
/** The full path to the folder on the local filesystem
4449
*
4550
* Always ends with /.
@@ -53,7 +58,7 @@ struct OCSYNC_EXPORT VfsSetupParams
5358
QString remotePath;
5459

5560
/// Account url, credentials etc. for network calls
56-
AccountPtr account;
61+
Account *account;
5762

5863
/** Access to the sync folder's database.
5964
*

src/gui/accountmanager.cpp

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,50 +109,53 @@ bool AccountManager::restore()
109109
if (auto acc = loadAccountHelper(*settings)) {
110110
acc->_groupIndex = accountIndex;
111111
if (auto accState = AccountState::loadFromSettings(acc, *settings)) {
112-
addAccountState(std::move(accState));
112+
addAccountState(accState);
113113
}
114114
}
115115
settings->endGroup();
116116
}
117117

118118
return true;
119119
}
120-
121-
AccountPtr AccountManager::createAccount(const NewAccountModel &model)
120+
121+
Account *AccountManager::createAccount(const NewAccountModel &model)
122122
{
123-
auto newAccountPtr = Account::create(QUuid::createUuid(), model.davUser(), model.effectiveUserInfoUrl());
123+
auto account = new Account(QUuid::createUuid(), model.davUser(), model.effectiveUserInfoUrl());
124124

125-
newAccountPtr->setDavDisplayName(model.displayName());
125+
account->setDavDisplayName(model.displayName());
126126

127-
Credentials *creds = new Credentials(model.authToken(), model.refreshToken(), newAccountPtr.get());
128-
newAccountPtr->setCredentials(creds);
127+
Credentials *creds = new Credentials(model.authToken(), model.refreshToken(), account);
128+
account->setCredentials(creds);
129129

130-
newAccountPtr->addApprovedCerts(model.trustedCertificates());
130+
account->addApprovedCerts(model.trustedCertificates());
131131
QString syncRoot = model.defaultSyncRoot();
132132
if (!syncRoot.isEmpty()) {
133-
newAccountPtr->setDefaultSyncRoot(syncRoot);
133+
account->setDefaultSyncRoot(syncRoot);
134134
if (!QFileInfo::exists(syncRoot)) {
135135
OC_ASSERT(QDir().mkpath(syncRoot));
136136
}
137-
Utility::markDirectoryAsSyncRoot(syncRoot, newAccountPtr->uuid());
137+
Utility::markDirectoryAsSyncRoot(syncRoot, account->uuid());
138138
}
139139

140-
newAccountPtr->setCapabilities(model.capabilities());
140+
account->setCapabilities(model.capabilities());
141141

142-
return newAccountPtr;
142+
return account;
143143
}
144144

145145
void AccountManager::save()
146146
{
147147
for (const auto &acc : std::as_const(_accounts)) {
148-
saveAccount(acc->account().data());
148+
saveAccount(acc->account());
149149
}
150150

151151
qCInfo(lcAccountManager) << "Saved all account settings";
152152
}
153153

154154
void AccountManager::saveAccount(Account *account)
155155
{
156+
if (!account)
157+
return;
158+
156159
qCDebug(lcAccountManager) << "Saving account" << account->url().toString();
157160
auto settings = ConfigFile::settingsWithGroup(accountsC());
158161
settings->beginGroup(account->groupIndex());
@@ -206,9 +209,9 @@ QStringList AccountManager::accountNames() const
206209
bool AccountManager::accountForLoginExists(const QUrl &url, const QString &davUser) const
207210
{
208211
for (const auto &state : _accounts) {
209-
if (state) {
210-
AccountPtr account = state->account();
211-
if (account && account->url() == url && account->davUser() == davUser) {
212+
if (state && state->account()) {
213+
Account *account = state->account();
214+
if (account->url() == url && account->davUser() == davUser) {
212215
return true;
213216
}
214217
}
@@ -221,18 +224,19 @@ const QList<AccountState *> AccountManager::accounts() const
221224
return _accounts.values();
222225
}
223226

224-
AccountPtr AccountManager::loadAccountHelper(QSettings &settings)
227+
Account *AccountManager::loadAccountHelper(QSettings &settings)
225228
{
226229
QVariant urlConfig = settings.value(urlC());
227230
if (!urlConfig.isValid()) {
228231
// No URL probably means a corrupted entry in the account settings
229232
qCWarning(lcAccountManager) << "No URL for account " << settings.group();
230-
return AccountPtr();
233+
return nullptr;
231234
}
235+
232236
QString user = settings.value(davUserC()).toString();
233237
if (user.isEmpty()) {
234238
qCWarning(lcAccountManager) << "No user name provided for account " << settings.group();
235-
return AccountPtr();
239+
return nullptr;
236240
}
237241
QUrl url = urlConfig.toUrl();
238242

@@ -260,7 +264,7 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings)
260264
if (key.contains(uid.toString(QUuid::WithoutBraces)))
261265
credsGroup->remove(key);
262266
}
263-
return AccountPtr();
267+
return nullptr;
264268
}
265269

266270
// 7.0 change - this special handling can be removed in 8.0
@@ -275,13 +279,13 @@ AccountPtr AccountManager::loadAccountHelper(QSettings &settings)
275279
}
276280

277281

278-
auto acc = Account::create(uid, user, url);
282+
auto acc = new Account(uid, user, url);
279283

280284
acc->setDavDisplayName(settings.value(davUserDisplyNameC()).toString());
281285
acc->setCapabilities(caps);
282286
acc->setDefaultSyncRoot(settings.value(defaultSyncRootC()).toString());
283287

284-
acc->setCredentials(new Credentials(acc.get()));
288+
acc->setCredentials(new Credentials(acc));
285289

286290
// now the server cert, it is in the general group
287291
settings.beginGroup("General");
@@ -321,7 +325,7 @@ AccountState *AccountManager::accountState(const QUuid uuid)
321325
return _accounts.value(uuid);
322326
}
323327

324-
AccountState *AccountManager::addAccount(const AccountPtr &newAccount)
328+
AccountState *AccountManager::addAccount(Account *newAccount)
325329
{
326330
auto id = newAccount->groupIndex();
327331
if (id.isEmpty() || !isAccountIndexAvailable(id)) {
@@ -330,7 +334,7 @@ AccountState *AccountManager::addAccount(const AccountPtr &newAccount)
330334
newAccount->_groupIndex = id;
331335

332336

333-
return addAccountState(AccountState::fromNewAccount(newAccount));
337+
return addAccountState(new AccountState(newAccount));
334338
}
335339

336340
void AccountManager::deleteAccount(AccountState *account)
@@ -412,23 +416,22 @@ QString AccountManager::generateFreeAccountIndex() const
412416
}
413417
}
414418

415-
AccountState *AccountManager::addAccountState(std::unique_ptr<AccountState> &&accountState)
419+
AccountState *AccountManager::addAccountState(AccountState *accountState)
416420
{
417-
AccountState *statePtr = accountState.release();
418-
if (!statePtr) // just bail. I have no idea why this is happening but fine. it's null and not usable
419-
return statePtr;
421+
if (!accountState || !accountState->account()) // just bail. I have no idea why this is happening but fine. it's null and not usable
422+
return nullptr;
423+
424+
_accounts.insert(accountState->account()->uuid(), accountState);
420425

421-
_accounts.insert(statePtr->account()->uuid(), statePtr);
426+
Account *acc = accountState->account();
422427

423-
auto *rawAccount = statePtr->account().get();
424428
// this slot can't be connected until the account state exists because saveAccount uses the state
425-
connect(rawAccount, &Account::wantsAccountSaved, this, [rawAccount, this] { saveAccount(rawAccount); });
429+
connect(acc, &Account::wantsAccountSaved, this, [acc, this] { saveAccount(acc); });
426430

427-
Q_EMIT accountAdded(statePtr);
431+
Q_EMIT accountAdded(accountState);
428432
Q_EMIT accountsChanged();
433+
accountState->checkConnectivity();
429434

430-
statePtr->checkConnectivity();
431-
432-
return statePtr;
435+
return accountState;
433436
}
434437
}

src/gui/accountmanager.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class OWNCLOUDGUI_EXPORT AccountManager : public QObject
5353
* @param model contains the data used to set up the new account (usually collected from the new account wizard)
5454
* @return the AccountPtr associated with the new account. for now.
5555
*/
56-
AccountPtr createAccount(const NewAccountModel &model);
56+
Account *createAccount(const NewAccountModel &model);
5757

5858
/**
5959
* Creates account objects from a given settings file.
@@ -68,7 +68,7 @@ class OWNCLOUDGUI_EXPORT AccountManager : public QObject
6868
* Typically called from the wizard
6969
*/
7070
// todo: #28 - this should not be public and should not be called directly by any third party
71-
AccountState *addAccount(const AccountPtr &newAccount);
71+
AccountState *addAccount(Account *newAccount);
7272

7373
/**
7474
* remove all accounts
@@ -119,15 +119,16 @@ public Q_SLOTS:
119119
private:
120120
// saving and loading Account to settings
121121
void saveAccountHelper(Account *account, QSettings &settings, bool saveCredentials = true);
122-
AccountPtr loadAccountHelper(QSettings &settings);
122+
Account *loadAccountHelper(QSettings &settings);
123123

124124
bool isAccountIndexAvailable(const QString &index) const;
125125
QString generateFreeAccountIndex() const;
126126

127127
// Adds an account to the tracked list, emitting accountAdded()
128-
AccountState *addAccountState(std::unique_ptr<AccountState> &&accountState);
128+
AccountState *addAccountState(AccountState *accountState);
129129

130130
AccountManager() {}
131+
131132
QMap<QUuid, AccountState *> _accounts;
132133
};
133134
}

0 commit comments

Comments
 (0)