FolderMan: refactor some function to take a Folder* instead of an alias

I want to remove this dependency of alias in the code because we might get
rid of it entierly later
This commit is contained in:
Olivier Goffart 2015-04-28 15:13:39 +02:00
parent e4694a6db8
commit e02f1a222e
9 changed files with 81 additions and 117 deletions

View file

@ -236,7 +236,7 @@ void AccountSettings::slotRemoveCurrentFolder()
}
FolderMan *folderMan = FolderMan::instance();
folderMan->slotRemoveFolder( alias );
folderMan->slotRemoveFolder( folderMan->folder(alias) );
_model->removeRow(row);
// single folder fix to show add-button and hide remove-button
@ -349,7 +349,7 @@ void AccountSettings::slotEnableCurrentFolder()
f->slotTerminateSync();
}
f->setSyncPaused(!currentlyPaused); // toggle the pause setting
folderMan->slotSetFolderPaused( alias, !currentlyPaused );
folderMan->slotSetFolderPaused( f, !currentlyPaused );
// keep state for the icon setting.
if( currentlyPaused ) _wasDisabledBefore = true;
@ -366,7 +366,7 @@ void AccountSettings::slotSyncCurrentFolderNow()
QString alias = _model->data( selected, FolderStatusDelegate::FolderAliasRole ).toString();
FolderMan *folderMan = FolderMan::instance();
folderMan->slotScheduleSync(alias);
folderMan->slotScheduleSync(folderMan->folder(alias));
}
void AccountSettings::slotUpdateFolderState( Folder *folder )

View file

@ -292,7 +292,7 @@ void Folder::slotRunEtagJob()
<< _syncResult.statusString();
}
_forceSyncOnPollTimeout = false;
emit scheduleToSync(alias());
emit scheduleToSync(this);
} else {
// Do the ordinary etag check for the root folder and only schedule a real
@ -315,7 +315,7 @@ void Folder::etagRetreived(const QString& etag)
if (_lastEtag != etag) {
_lastEtag = etag;
emit scheduleToSync(alias());
emit scheduleToSync(this);
}
}
@ -540,7 +540,7 @@ void Folder::slotWatchedPathChanged(const QString& path)
// When no sync is running or it's in the prepare phase, we can
// always schedule a new sync.
if (! _engine || _syncResult.status() == SyncResult::SyncPrepare) {
emit scheduleToSync(alias());
emit scheduleToSync(this);
return;
}
@ -562,7 +562,7 @@ void Folder::slotWatchedPathChanged(const QString& path)
#endif
if (! ownChange) {
emit scheduleToSync(alias());
emit scheduleToSync(this);
}
}

View file

@ -172,7 +172,7 @@ signals:
void syncStateChange();
void syncStarted();
void syncFinished(const SyncResult &result);
void scheduleToSync( const QString& );
void scheduleToSync(Folder*);
public slots:

View file

@ -96,21 +96,20 @@ OCC::Folder::Map FolderMan::map()
return _folderMap;
}
void FolderMan::unloadFolder( const QString& alias )
void FolderMan::unloadFolder( Folder *f )
{
Folder* f = folder(alias);
if( !f ) {
return;
}
if( _socketApi ) {
_socketApi->slotUnregisterPath(alias);
_socketApi->slotUnregisterPath(f->alias());
}
if( _folderWatchers.contains(alias)) {
_folderWatchers.remove(alias);
if( _folderWatchers.contains(f->alias())) {
_folderWatchers.remove(f->alias());
}
_folderMap.remove( alias );
_folderMap.remove( f->alias() );
}
int FolderMan::unloadAndDeleteAllFolders()
@ -122,12 +121,12 @@ int FolderMan::unloadAndDeleteAllFolders()
while (i.hasNext()) {
i.next();
Folder* f = i.value();
unloadFolder(i.key());
unloadFolder(f);
delete f;
cnt++;
}
_lastSyncFolder.clear();
_currentSyncFolder.clear();
_lastSyncFolder = 0;
_currentSyncFolder = 0;
_scheduleQueue.clear();
Q_ASSERT(_folderMap.count() == 0);
@ -209,8 +208,8 @@ int FolderMan::setupFolders()
if (FolderDefinition::load(*settings, folderAlias, &folderDefinition)) {
Folder* f = addFolderInternal(account.data(), folderDefinition);
if (f) {
slotScheduleSync(f->alias());
emit folderSyncStateChange(f->alias());
slotScheduleSync(f);
emit folderSyncStateChange(f);
}
}
}
@ -240,8 +239,8 @@ int FolderMan::setupFoldersMigration()
foreach ( const QString& alias, list ) {
Folder *f = setupFolderFromConfigFile( alias );
if( f ) {
slotScheduleSync(alias);
emit( folderSyncStateChange( f->alias() ) );
slotScheduleSync(f);
emit( folderSyncStateChange( f ) );
}
}
@ -402,7 +401,7 @@ Folder* FolderMan::setupFolderFromConfigFile(const QString &file) {
}
/* Use a signal mapper to connect the signals to the alias */
connect(folder, SIGNAL(scheduleToSync(const QString&)), SLOT(slotScheduleSync(const QString&)));
connect(folder, SIGNAL(scheduleToSync(Folder*)), SLOT(slotScheduleSync(Folder*)));
connect(folder, SIGNAL(syncStateChange()), _folderChangeSignalMapper, SLOT(map()));
connect(folder, SIGNAL(syncStarted()), SLOT(slotFolderSyncStarted()));
connect(folder, SIGNAL(syncFinished(SyncResult)), SLOT(slotFolderSyncFinished(SyncResult)));
@ -413,15 +412,14 @@ Folder* FolderMan::setupFolderFromConfigFile(const QString &file) {
return folder;
}
void FolderMan::slotSetFolderPaused( const QString& alias, bool paused )
void FolderMan::slotSetFolderPaused( Folder *f, bool paused )
{
Folder *f = folder(alias);
if( !f ) {
qDebug() << "!! Can not enable alias " << alias << ", can not be found in folderMap.";
qWarning() << "!! slotSetFolderPaused called with empty folder";
return;
}
slotScheduleSync(alias);
slotScheduleSync(f);
if (!paused) {
_disabledFolders.remove(f);
@ -429,7 +427,7 @@ void FolderMan::slotSetFolderPaused( const QString& alias, bool paused )
_disabledFolders.insert(f);
}
f->setSyncPaused(paused);
emit folderSyncStateChange(alias);
emit folderSyncStateChange(f);
}
// this really terminates the current sync process
@ -437,7 +435,7 @@ void FolderMan::slotSetFolderPaused( const QString& alias, bool paused )
// csync still remains in a stable state, regardless of that.
void FolderMan::terminateSyncProcess()
{
Folder *f = folder(_currentSyncFolder);
Folder *f = _currentSyncFolder;
if( f ) {
// This will, indirectly and eventually, call slotFolderSyncFinished
// and thereby clear _currentSyncFolder.
@ -455,17 +453,11 @@ Folder *FolderMan::folder( const QString& alias )
return 0;
}
SyncResult FolderMan::syncResult( const QString& alias )
{
Folder *f = folder( alias );
return f ? f->syncResult() : SyncResult();
}
void FolderMan::slotScheduleAllFolders()
{
foreach( Folder *f, _folderMap.values() ) {
if (f && ! f->syncPaused()) {
slotScheduleSync( f->alias() );
slotScheduleSync( f );
}
}
}
@ -474,13 +466,13 @@ void FolderMan::slotScheduleAllFolders()
* if a folder wants to be synced, it calls this slot and is added
* to the queue. The slot to actually start a sync is called afterwards.
*/
void FolderMan::slotScheduleSync( const QString& alias )
void FolderMan::slotScheduleSync( Folder *f )
{
Folder* f = folder(alias);
if( !f ) {
qDebug() << "Not scheduling sync for empty or unknown folder" << alias;
qWarning() << "slotScheduleSync called with null folder";
return;
}
auto alias = f->alias();
if( _socketApi ) {
// We want the SocketAPI to already now update so that it can show the EVAL icon
@ -491,7 +483,7 @@ void FolderMan::slotScheduleSync( const QString& alias )
qDebug() << "Schedule folder " << alias << " to sync!";
if( ! _scheduleQueue.contains(alias) ) {
if( ! _scheduleQueue.contains(f) ) {
if( !f->syncPaused() ) {
f->prepareToSync();
} else {
@ -501,7 +493,7 @@ void FolderMan::slotScheduleSync( const QString& alias )
}
return;
}
_scheduleQueue.enqueue(alias);
_scheduleQueue.enqueue(f);
} else {
qDebug() << " II> Sync for folder " << alias << " already scheduled, do not enqueue!";
}
@ -554,7 +546,7 @@ void FolderMan::setSyncEnabled( bool enabled )
}
_syncEnabled = enabled;
// force a redraw in case the network connect status changed
emit( folderSyncStateChange(QString::null) );
emit( folderSyncStateChange(0) );
}
void FolderMan::startScheduledSyncSoon(qint64 msMinimumDelay)
@ -565,7 +557,7 @@ void FolderMan::startScheduledSyncSoon(qint64 msMinimumDelay)
if (_scheduleQueue.empty()) {
return;
}
if (! _currentSyncFolder.isEmpty()) {
if (_currentSyncFolder) {
return;
}
@ -573,7 +565,7 @@ void FolderMan::startScheduledSyncSoon(qint64 msMinimumDelay)
qint64 msSinceLastSync = 0;
// Require a pause based on the duration of the last sync run.
if (Folder* lastFolder = folder(_lastSyncFolder)) {
if (Folder* lastFolder = _lastSyncFolder) {
msSinceLastSync = lastFolder->msecSinceLastSync();
// 1s -> 1.5s pause
@ -585,7 +577,7 @@ void FolderMan::startScheduledSyncSoon(qint64 msMinimumDelay)
}
// Punish consecutive follow-up syncs with longer delays.
if (Folder* nextFolder = folder(_scheduleQueue.head())) {
if (Folder* nextFolder = _scheduleQueue.head()) {
int followUps = nextFolder->consecutiveFollowUpSyncs();
if (followUps >= 2) {
// This is okay due to the 1min maximum delay limit below.
@ -616,8 +608,8 @@ void FolderMan::startScheduledSyncSoon(qint64 msMinimumDelay)
*/
void FolderMan::slotStartScheduledFolderSync()
{
if( !_currentSyncFolder.isEmpty() ) {
qDebug() << "Currently folder " << _currentSyncFolder << " is running, wait for finish!";
if( _currentSyncFolder ) {
qDebug() << "Currently folder " << _currentSyncFolder->alias() << " is running, wait for finish!";
return;
}
@ -632,16 +624,12 @@ void FolderMan::slotStartScheduledFolderSync()
}
// Try to start the top scheduled sync.
const QString alias = _scheduleQueue.dequeue();
Folder *f = folder(alias);
if( !f ) {
qDebug() << "FolderMan: Not syncing queued folder" << alias << ": not in folder map anymore";
return;
}
Folder *f = _scheduleQueue.dequeue();
Q_ASSERT(f);
// Start syncing this folder!
if( !f->syncPaused() ) {
_currentSyncFolder = alias;
_currentSyncFolder = f;
f->startSync( QStringList() );
@ -660,66 +648,46 @@ void FolderMan::slotEtagPollTimerTimeout()
ConfigFile cfg;
int polltime = cfg.remotePollInterval();
QSet<QString> folderAliases = _folderMap.keys().toSet();
QMutableSetIterator<QString> i(folderAliases);
while (i.hasNext()) {
QString alias = i.next();
if (_currentSyncFolder == alias) {
i.remove();
foreach (Folder *f, _folderMap) {
if (_currentSyncFolder == f) {
continue;
}
if (_scheduleQueue.contains(alias)) {
i.remove();
if (_scheduleQueue.contains(f)) {
continue;
}
Folder *f = _folderMap.value(alias);
if (f && _disabledFolders.contains(f)) {
i.remove();
continue;
}
if (f && (f->etagJob() || f->isBusy() || f->syncPaused())) {
i.remove();
continue;
}
if (f && f->msecSinceLastSync() < polltime) {
i.remove();
continue;
}
}
if (folderAliases.isEmpty()) {
qDebug() << Q_FUNC_INFO << "No folders need to check for the remote ETag";
} else {
qDebug() << Q_FUNC_INFO << "The following folders need to check for the remote ETag:" << folderAliases;
i = folderAliases; // reset
while (i.hasNext()) {
QString alias = i.next();
QMetaObject::invokeMethod(_folderMap.value(alias), "slotRunEtagJob", Qt::QueuedConnection);
}
QMetaObject::invokeMethod(f, "slotRunEtagJob", Qt::QueuedConnection);
}
}
void FolderMan::slotRemoveFoldersForAccount(AccountState* accountState)
{
QStringList foldersToRemove;
QVarLengthArray<Folder *, 16> foldersToRemove;
Folder::MapIterator i(_folderMap);
while (i.hasNext()) {
i.next();
Folder* folder = i.value();
if (folder->accountState() == accountState) {
foldersToRemove.append(folder->alias());
foldersToRemove.append(folder);
}
}
qDebug() << "Account was removed, removing associated folders:" << foldersToRemove;
foreach (const QString& alias, foldersToRemove) {
slotRemoveFolder(alias);
foreach (const auto &f, foldersToRemove) {
slotRemoveFolder(f);
}
}
void FolderMan::slotFolderSyncStarted( )
{
qDebug() << ">===================================== sync started for " << _currentSyncFolder;
qDebug() << ">===================================== sync started for " << _currentSyncFolder->alias();
}
/*
@ -730,10 +698,10 @@ void FolderMan::slotFolderSyncStarted( )
*/
void FolderMan::slotFolderSyncFinished( const SyncResult& )
{
qDebug() << "<===================================== sync finished for " << _currentSyncFolder;
qDebug() << "<===================================== sync finished for " << _currentSyncFolder->alias();
_lastSyncFolder = _currentSyncFolder;
_currentSyncFolder.clear();
_currentSyncFolder = 0;
startScheduledSyncSoon();
}
@ -761,7 +729,7 @@ Folder* FolderMan::addFolderInternal(AccountState* accountState, const FolderDef
}
/* Use a signal mapper to connect the signals to the alias */
connect(folder, SIGNAL(scheduleToSync(const QString&)), SLOT(slotScheduleSync(const QString&)));
connect(folder, SIGNAL(scheduleToSync(Folder*)), SLOT(slotScheduleSync(Folder*)));
connect(folder, SIGNAL(syncStateChange()), _folderChangeSignalMapper, SLOT(map()));
connect(folder, SIGNAL(syncStarted()), SLOT(slotFolderSyncStarted()));
connect(folder, SIGNAL(syncFinished(SyncResult)), SLOT(slotFolderSyncFinished(SyncResult)));
@ -788,17 +756,16 @@ Folder *FolderMan::folderForPath(const QString &path)
return 0;
}
void FolderMan::slotRemoveFolder( const QString& alias )
void FolderMan::slotRemoveFolder( Folder *f )
{
Folder *f = folder(alias);
if( !f ) {
qDebug() << "!! Can not remove " << alias << ", not in folderMap.";
qWarning() << "!! Can not remove null folder";
return;
}
qDebug() << "Removing " << alias;
qDebug() << "Removing " << f->alias();
const bool currentlyRunning = (_currentSyncFolder == alias);
const bool currentlyRunning = (_currentSyncFolder == f);
if( currentlyRunning ) {
// let the folder delete itself when done and
// abort the sync now
@ -806,7 +773,7 @@ void FolderMan::slotRemoveFolder( const QString& alias )
terminateSyncProcess();
}
_scheduleQueue.removeAll(alias);
_scheduleQueue.removeAll(f);
f->wipe();
f->setSyncPaused(true);
@ -814,7 +781,7 @@ void FolderMan::slotRemoveFolder( const QString& alias )
// remove the folder configuration
f->removeFromSettings();
unloadFolder( alias );
unloadFolder( f);
if( !currentlyRunning ) {
delete f;
}

View file

@ -55,9 +55,6 @@ public:
/** Returns the folder by alias or NULL if no folder with the alias exists. */
Folder *folder( const QString& );
/** Returns the last sync result by alias */
SyncResult syncResult( const QString& );
/** Creates a folder for a specific configuration, identified by alias. */
Folder* setupFolderFromConfigFile(const QString & );
@ -86,18 +83,17 @@ public:
signals:
/**
* signal to indicate a folder named by alias has changed its sync state.
* Get the state via the Folder Map or the syncResult and syncState methods.
* signal to indicate a folder has changed its sync state.
*
* Attention: The alias string may be zero. Do a general update of the state than.
* Attention: The folder may be zero. Do a general update of the state than.
*/
void folderSyncStateChange( const QString & );
void folderSyncStateChange(Folder*);
void folderListLoaded(const Folder::Map &);
public slots:
void slotRemoveFolder( const QString& );
void slotSetFolderPaused(const QString&, bool paused);
void slotRemoveFolder( Folder* );
void slotSetFolderPaused(Folder *, bool paused);
void slotFolderSyncStarted();
void slotFolderSyncFinished( const SyncResult& );
@ -122,7 +118,7 @@ public slots:
void setDirtyNetworkLimits();
// slot to add a folder to the syncing queue
void slotScheduleSync( const QString & );
void slotScheduleSync(Folder*);
// slot to scheule an ETag job
void slotScheduleETagJob ( const QString &alias, RequestEtagJob *job);
void slotEtagJobDestroyed (QObject*);
@ -142,7 +138,7 @@ private:
Folder* addFolderInternal(AccountState* accountState, const FolderDefinition& folderDefinition);
/* unloads a folder object, does not delete it */
void unloadFolder( const QString& alias );
void unloadFolder( Folder * );
/** Will start a sync after a bit of delay. */
void startScheduledSyncSoon(qint64 msMinimumDelay = 0);
@ -158,8 +154,8 @@ private:
Folder::Map _folderMap;
QString _folderConfigPath;
QSignalMapper *_folderChangeSignalMapper;
QString _currentSyncFolder;
QString _lastSyncFolder;
Folder *_currentSyncFolder = 0;
QPointer<Folder> _lastSyncFolder;
bool _syncEnabled;
QTimer _etagPollTimer;
QPointer<RequestEtagJob> _currentEtagJob; // alias of Folder running the current RequestEtagJob
@ -168,7 +164,7 @@ private:
QPointer<SocketApi> _socketApi;
/** The aliases of folders that shall be synced. */
QQueue<QString> _scheduleQueue;
QQueue<Folder*> _scheduleQueue;
/** When the timer expires one of the scheduled syncs will be started. */
QTimer _startScheduledSyncTimer;

View file

@ -486,7 +486,7 @@ void FolderStatusModel::slotApplySelectiveSync()
foreach(const auto &it, changes) {
folder->journalDb()->avoidReadFromDbOnNextSync(it);
}
folderMan->slotScheduleSync(folder->alias());
folderMan->slotScheduleSync(folder);
}
}

View file

@ -84,8 +84,8 @@ ownCloudGui::ownCloudGui(Application *parent) :
SLOT(slotUpdateProgress(QString,ProgressInfo)) );
FolderMan *folderMan = FolderMan::instance();
connect( folderMan, SIGNAL(folderSyncStateChange(QString)),
this,SLOT(slotSyncStateChange(QString)));
connect( folderMan, SIGNAL(folderSyncStateChange(Folder*)),
this,SLOT(slotSyncStateChange(Folder*)));
connect( Logger::instance(), SIGNAL(guiLog(QString,QString)),
SLOT(slotShowTrayMessage(QString,QString)));
@ -169,18 +169,17 @@ void ownCloudGui::slotTrayClicked( QSystemTrayIcon::ActivationReason reason )
#endif
}
void ownCloudGui::slotSyncStateChange( const QString& alias )
void ownCloudGui::slotSyncStateChange( Folder* folder )
{
FolderMan *folderMan = FolderMan::instance();
const SyncResult& result = folderMan->syncResult( alias );
slotComputeOverallSyncStatus();
if( alias.isEmpty() ) {
if( !folder ) {
return; // Valid, just a general GUI redraw was needed.
}
qDebug() << "Sync state changed for folder " << alias << ": " << result.statusString();
auto result = folder->syncResult();
qDebug() << "Sync state changed for folder " << folder->alias() << ": " << result.statusString();
if (result.status() == SyncResult::Success || result.status() == SyncResult::Error) {
Logger::instance()->enterNextLogFile();

View file

@ -27,6 +27,8 @@
namespace OCC {
class Folder;
class SettingsDialog;
class SettingsDialogMac;
class Application;
@ -62,7 +64,7 @@ public slots:
void slotShowSettings();
void slotShowSyncProtocol();
void slotShutdown();
void slotSyncStateChange( const QString& alias );
void slotSyncStateChange(Folder*);
void slotTrayClicked( QSystemTrayIcon::ActivationReason reason );
void slotToggleLogBrowser();
void slotOpenOwnCloud();

View file

@ -413,7 +413,7 @@ void SelectiveSyncDialog::accept()
_folder->journalDb()->avoidReadFromDbOnNextSync(it);
}
folderMan->slotScheduleSync(_folder->alias());
folderMan->slotScheduleSync(_folder);
}
QDialog::accept();
}