nextcloud-desktop/src/gui/folderman.h

367 lines
11 KiB
C
Raw Normal View History

/*
* Copyright (C) by Klaas Freitag <freitag@owncloud.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.
*/
#ifndef FOLDERMAN_H
#define FOLDERMAN_H
#include <QObject>
#include <QQueue>
#include <QList>
#include "folder.h"
#include "folderwatcher.h"
#include "navigationpanehelper.h"
#include "syncfileitem.h"
class TestFolderMan;
2014-11-10 00:34:07 +03:00
namespace OCC {
class Application;
class SyncResult;
class SocketApi;
class LockWatcher;
2015-06-29 19:56:09 +03:00
/**
* @brief The FolderMan class
* @ingroup gui
*
* The FolderMan knows about all loaded folders and is responsible for
* scheduling them when necessary.
*
* A folder is scheduled if:
* - The configured force-sync-interval has expired
* (_timeScheduler and slotScheduleFolderByTime())
*
* - A folder watcher receives a notification about a file change
* (_folderWatchers and Folder::slotWatchedPathChanged())
*
* - The folder etag on the server has changed
* (_etagPollTimer)
*
* - The locks of a monitored file are released
* (_lockWatcher and slotWatchedFileUnlocked())
*
* - There was a sync error or a follow-up sync is requested
* (_timeScheduler and slotScheduleFolderByTime()
* and Folder::slotSyncFinished())
*/
class FolderMan : public QObject
{
Q_OBJECT
public:
~FolderMan();
2017-05-17 11:55:42 +03:00
static FolderMan *instance();
int setupFolders();
2015-04-24 11:18:33 +03:00
int setupFoldersMigration();
/**
* Returns a list of keys that can't be read because they are from
* future versions.
*/
static void backwardMigrationSettingsKeys(QStringList *deleteKeys, QStringList *ignoreKeys);
2020-07-23 13:40:58 +03:00
const Folder::Map &map() const;
2015-04-24 11:18:33 +03:00
/** Adds a folder for an account, ensures the journal is gone and saves it in the settings.
*/
2017-05-17 11:55:42 +03:00
Folder *addFolder(AccountState *accountState, const FolderDefinition &folderDefinition);
/** Removes a folder */
2017-05-17 11:55:42 +03:00
void removeFolder(Folder *);
/** Returns the folder which the file or directory stored in path is in */
2017-05-17 11:55:42 +03:00
Folder *folderForPath(const QString &path);
/**
* returns a list of local files that exist on the local harddisk for an
* incoming relative server path. The method checks with all existing sync
* folders.
*/
2017-05-17 11:55:42 +03:00
QStringList findFileInLocalFolders(const QString &relPath, const AccountPtr acc);
/** Returns the folder by alias or \c nullptr if no folder with the alias exists. */
2017-05-17 11:55:42 +03:00
Folder *folder(const QString &);
/**
* Migrate accounts from owncloud < 2.0
* Creates a folder for a specific configuration, identified by alias.
*/
2017-05-17 11:55:42 +03:00
Folder *setupFolderFromOldConfigFile(const QString &, AccountState *account);
/**
2016-09-02 16:51:13 +03:00
* Ensures that a given directory does not contain a sync journal file.
*
2013-07-24 16:16:52 +04:00
* @returns false if the journal could not be removed, true otherwise.
*/
2017-05-17 11:55:42 +03:00
static bool ensureJournalGone(const QString &journalDbFile);
2013-07-24 16:16:52 +04:00
/** Creates a new and empty local directory. */
2017-05-17 11:55:42 +03:00
bool startFromScratch(const QString &);
/// Produce text for use in the tray tooltip
static QString trayTooltipStatusString(SyncResult::Status syncStatus, bool hasUnresolvedConflicts, bool paused);
/// Compute status summarizing multiple folders
static void trayOverallStatus(const QList<Folder *> &folders,
SyncResult::Status *status, bool *unresolvedConflicts);
2014-08-11 17:09:17 +04:00
// Escaping of the alias which is used in QSettings AND the file
// system, thus need to be escaped.
2017-05-17 11:55:42 +03:00
static QString escapeAlias(const QString &);
static QString unescapeAlias(const QString &);
2014-08-11 17:09:17 +04:00
SocketApi *socketApi();
NavigationPaneHelper &navigationPaneHelper() { return _navigationPaneHelper; }
/**
* Check if @a path is a valid path for a new folder considering the already sync'ed items.
* Make sure that this folder, or any subfolder is not sync'ed already.
*
* Note that different accounts are allowed to sync to the same folder.
*
* @returns an empty string if it is allowed, or an error if it is not allowed
*/
QString checkPathValidityForNewFolder(const QString &path, const QUrl &serverUrl = QUrl()) const;
/**
* Attempts to find a non-existing, acceptable path for creating a new sync folder.
*
* Uses \a basePath as the baseline. It'll return this path if it's acceptable.
*
* Note that this can fail. If someone syncs ~ and \a basePath is ~/ownCloud, no
* subfolder of ~ would be a good candidate. When that happens \a basePath
* is returned.
*/
QString findGoodPathForNewSyncFolder(const QString &basePath, const QUrl &serverUrl) const;
/**
* While ignoring hidden files can theoretically be switched per folder,
* it's currently a global setting that users can only change for all folders
* at once.
* These helper functions can be removed once it's properly per-folder.
*/
bool ignoreHiddenFiles() const;
void setIgnoreHiddenFiles(bool ignore);
/**
* Access to the current queue of scheduled folders.
*/
2017-05-17 11:55:42 +03:00
QQueue<Folder *> scheduleQueue() const;
/**
* Access to the currently syncing folder.
*
* Note: This is only the folder that's currently syncing *as-scheduled*. There
* may be externally-managed syncs such as from placeholder hydrations.
*
* See also isAnySyncRunning()
*/
2017-05-17 11:55:42 +03:00
Folder *currentSyncFolder() const;
/**
* Returns true if any folder is currently syncing.
*
* This might be a FolderMan-scheduled sync, or a externally
* managed sync like a placeholder hydration.
*/
bool isAnySyncRunning() const;
/** Removes all folders */
int unloadAndDeleteAllFolders();
/**
* If enabled is set to false, no new folders will start to sync.
* The current one will finish.
*/
2017-05-17 11:55:42 +03:00
void setSyncEnabled(bool);
/** Queues a folder for syncing. */
2017-05-17 11:55:42 +03:00
void scheduleFolder(Folder *);
/** Puts a folder in the very front of the queue. */
2017-05-17 11:55:42 +03:00
void scheduleFolderNext(Folder *);
/** Queues all folders for syncing. */
void scheduleAllFolders();
void setDirtyProxy();
void setDirtyNetworkLimits();
signals:
/**
* signal to indicate a folder has changed its sync state.
*
* Attention: The folder may be zero. Do a general update of the state then.
*/
2017-05-17 11:55:42 +03:00
void folderSyncStateChange(Folder *);
/**
* Indicates when the schedule queue changes.
*/
void scheduleQueueChanged();
/**
* Emitted whenever the list of configured folders changes.
*/
void folderListChanged(const Folder::Map &);
2013-08-27 12:02:59 +04:00
/**
* Emitted once slotRemoveFoldersForAccount is done wiping
*/
void wipeDone(AccountState *account, bool success);
public slots:
/**
* Schedules folders of newly connected accounts, terminates and
* de-schedules folders of disconnected accounts.
*/
void slotAccountStateChanged();
/**
* restart the client as soon as it is possible, ie. no folders syncing.
*/
void slotScheduleAppRestart();
/**
* Triggers a sync run once the lock on the given file is removed.
*
* Automatically detemines the folder that's responsible for the file.
* See slotWatchedFileUnlocked().
*/
2017-05-17 11:55:42 +03:00
void slotSyncOnceFileUnlocks(const QString &path);
// slot to schedule an ETag job (from Folder only)
2017-05-17 11:55:42 +03:00
void slotScheduleETagJob(const QString &alias, RequestEtagJob *job);
/** Wipe folder */
void slotWipeFolderForAccount(AccountState *accountState);
private slots:
void slotFolderSyncPaused(Folder *, bool paused);
void slotFolderCanSyncChanged();
void slotFolderSyncStarted();
2017-05-17 11:55:42 +03:00
void slotFolderSyncFinished(const SyncResult &);
void slotRunOneEtagJob();
2017-05-17 11:55:42 +03:00
void slotEtagJobDestroyed(QObject *);
// slot to take the next folder from queue and start syncing.
void slotStartScheduledFolderSync();
void slotEtagPollTimerTimeout();
2015-04-24 11:18:33 +03:00
void slotAccountRemoved(AccountState *accountState);
2017-05-17 11:55:42 +03:00
void slotRemoveFoldersForAccount(AccountState *accountState);
// Wraps the Folder::syncStateChange() signal into the
// FolderMan::folderSyncStateChange(Folder*) signal.
void slotForwardFolderSyncStateChange();
2017-05-17 11:55:42 +03:00
void slotServerVersionChanged(Account *account);
/**
* A file whose locks were being monitored has become unlocked.
*
* This schedules the folder for synchronization that contains
* the file with the given path.
*/
2017-05-17 11:55:42 +03:00
void slotWatchedFileUnlocked(const QString &path);
/**
* Schedules folders whose time to sync has come.
*
* Either because a long time has passed since the last sync or
* because of previous failures.
*/
void slotScheduleFolderByTime();
void slotSetupPushNotifications(const Folder::Map &);
void slotProcessFilesPushNotification(Account *account);
void slotConnectToPushNotifications(Account *account);
private:
/** Adds a new folder, does not add it to the account settings and
* does not set an account on the new folder.
2015-04-24 11:18:33 +03:00
*/
2017-05-17 11:55:42 +03:00
Folder *addFolderInternal(FolderDefinition folderDefinition,
AccountState *accountState, std::unique_ptr<Vfs> vfs);
2015-04-24 11:18:33 +03:00
/* unloads a folder object, does not delete it */
2017-05-17 11:55:42 +03:00
void unloadFolder(Folder *);
/** Will start a sync after a bit of delay. */
void startScheduledSyncSoon();
// finds all folder configuration files
// and create the folders
2017-05-17 11:55:42 +03:00
QString getBackupName(QString fullPathName) const;
// makes the folder known to the socket api
void registerFolderWithSocketApi(Folder *folder);
// restarts the application (Linux only)
void restartApplication();
void setupFoldersHelper(QSettings &settings, AccountStatePtr account, const QStringList &ignoreKeys, bool backwardsCompatible, bool foldersWithPlaceholders);
void runEtagJobsIfPossible(const QList<Folder *> &folderMap);
void runEtagJobIfPossible(Folder *folder);
bool pushNotificationsFilesReady(Account *account);
2017-05-17 11:55:42 +03:00
QSet<Folder *> _disabledFolders;
Folder::Map _folderMap;
QString _folderConfigPath;
Folder *_currentSyncFolder = nullptr;
QPointer<Folder> _lastSyncFolder;
bool _syncEnabled = true;
/// Folder aliases from the settings that weren't read
QSet<QString> _additionalBlockedFolderAliases;
/// Starts regular etag query jobs
QTimer _etagPollTimer;
/// The currently running etag query
QPointer<RequestEtagJob> _currentEtagJob;
/// Watches files that couldn't be synced due to locks
QScopedPointer<LockWatcher> _lockWatcher;
/// Occasionally schedules folders
QTimer _timeScheduler;
/// Scheduled folders that should be synced as soon as possible
2017-05-17 11:55:42 +03:00
QQueue<Folder *> _scheduledFolders;
/// Picks the next scheduled folder and starts the sync
2017-05-17 11:55:42 +03:00
QTimer _startScheduledSyncTimer;
QScopedPointer<SocketApi> _socketApi;
NavigationPaneHelper _navigationPaneHelper;
bool _appRestartRequired = false;
static FolderMan *_instance;
explicit FolderMan(QObject *parent = nullptr);
2014-11-10 00:34:07 +03:00
friend class OCC::Application;
friend class ::TestFolderMan;
};
2014-11-10 00:34:07 +03:00
} // namespace OCC
#endif // FOLDERMAN_H