folderman.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. */
  14. #include "folderman.h"
  15. #include "configfile.h"
  16. #include "folder.h"
  17. #include "syncresult.h"
  18. #include "theme.h"
  19. #include "socketapi.h"
  20. #include "account.h"
  21. #include "accountstate.h"
  22. #include "accountmanager.h"
  23. #include "filesystem.h"
  24. #include "lockwatcher.h"
  25. #include "common/asserts.h"
  26. #include <pushnotifications.h>
  27. #include <syncengine.h>
  28. #ifdef Q_OS_MAC
  29. #include <CoreServices/CoreServices.h>
  30. #endif
  31. #include <QMessageBox>
  32. #include <QtCore>
  33. #include <QMutableSetIterator>
  34. #include <QSet>
  35. #include <QNetworkProxy>
  36. static const char versionC[] = "version";
  37. static const int maxFoldersVersion = 1;
  38. namespace OCC {
  39. Q_LOGGING_CATEGORY(lcFolderMan, "nextcloud.gui.folder.manager", QtInfoMsg)
  40. FolderMan *FolderMan::_instance = nullptr;
  41. FolderMan::FolderMan(QObject *parent)
  42. : QObject(parent)
  43. , _lockWatcher(new LockWatcher)
  44. , _navigationPaneHelper(this)
  45. {
  46. ASSERT(!_instance);
  47. _instance = this;
  48. _socketApi.reset(new SocketApi);
  49. ConfigFile cfg;
  50. std::chrono::milliseconds polltime = cfg.remotePollInterval();
  51. qCInfo(lcFolderMan) << "setting remote poll timer interval to" << polltime.count() << "msec";
  52. _etagPollTimer.setInterval(polltime.count());
  53. QObject::connect(&_etagPollTimer, &QTimer::timeout, this, &FolderMan::slotEtagPollTimerTimeout);
  54. _etagPollTimer.start();
  55. _startScheduledSyncTimer.setSingleShot(true);
  56. connect(&_startScheduledSyncTimer, &QTimer::timeout,
  57. this, &FolderMan::slotStartScheduledFolderSync);
  58. _timeScheduler.setInterval(5000);
  59. _timeScheduler.setSingleShot(false);
  60. connect(&_timeScheduler, &QTimer::timeout,
  61. this, &FolderMan::slotScheduleFolderByTime);
  62. _timeScheduler.start();
  63. connect(AccountManager::instance(), &AccountManager::removeAccountFolders,
  64. this, &FolderMan::slotRemoveFoldersForAccount);
  65. connect(AccountManager::instance(), &AccountManager::accountSyncConnectionRemoved,
  66. this, &FolderMan::slotAccountRemoved);
  67. connect(_lockWatcher.data(), &LockWatcher::fileUnlocked,
  68. this, &FolderMan::slotWatchedFileUnlocked);
  69. connect(this, &FolderMan::folderListChanged, this, &FolderMan::slotSetupPushNotifications);
  70. }
  71. FolderMan *FolderMan::instance()
  72. {
  73. return _instance;
  74. }
  75. FolderMan::~FolderMan()
  76. {
  77. qDeleteAll(_folderMap);
  78. _instance = nullptr;
  79. }
  80. const OCC::Folder::Map &FolderMan::map() const
  81. {
  82. return _folderMap;
  83. }
  84. void FolderMan::unloadFolder(Folder *f)
  85. {
  86. if (!f) {
  87. return;
  88. }
  89. _socketApi->slotUnregisterPath(f->alias());
  90. _folderMap.remove(f->alias());
  91. disconnect(f, &Folder::syncStarted,
  92. this, &FolderMan::slotFolderSyncStarted);
  93. disconnect(f, &Folder::syncFinished,
  94. this, &FolderMan::slotFolderSyncFinished);
  95. disconnect(f, &Folder::syncStateChange,
  96. this, &FolderMan::slotForwardFolderSyncStateChange);
  97. disconnect(f, &Folder::syncPausedChanged,
  98. this, &FolderMan::slotFolderSyncPaused);
  99. disconnect(&f->syncEngine().syncFileStatusTracker(), &SyncFileStatusTracker::fileStatusChanged,
  100. _socketApi.data(), &SocketApi::broadcastStatusPushMessage);
  101. disconnect(f, &Folder::watchedFileChangedExternally,
  102. &f->syncEngine().syncFileStatusTracker(), &SyncFileStatusTracker::slotPathTouched);
  103. }
  104. int FolderMan::unloadAndDeleteAllFolders()
  105. {
  106. int cnt = 0;
  107. // clear the list of existing folders.
  108. Folder::MapIterator i(_folderMap);
  109. while (i.hasNext()) {
  110. i.next();
  111. Folder *f = i.value();
  112. unloadFolder(f);
  113. delete f;
  114. cnt++;
  115. }
  116. ASSERT(_folderMap.isEmpty());
  117. _lastSyncFolder = nullptr;
  118. _currentSyncFolder = nullptr;
  119. _scheduledFolders.clear();
  120. emit folderListChanged(_folderMap);
  121. emit scheduleQueueChanged();
  122. return cnt;
  123. }
  124. void FolderMan::registerFolderWithSocketApi(Folder *folder)
  125. {
  126. if (!folder)
  127. return;
  128. if (!QDir(folder->path()).exists())
  129. return;
  130. // register the folder with the socket API
  131. if (folder->canSync())
  132. _socketApi->slotRegisterPath(folder->alias());
  133. }
  134. int FolderMan::setupFolders()
  135. {
  136. unloadAndDeleteAllFolders();
  137. QStringList skipSettingsKeys;
  138. backwardMigrationSettingsKeys(&skipSettingsKeys, &skipSettingsKeys);
  139. auto settings = ConfigFile::settingsWithGroup(QLatin1String("Accounts"));
  140. const auto accountsWithSettings = settings->childGroups();
  141. if (accountsWithSettings.isEmpty()) {
  142. int r = setupFoldersMigration();
  143. if (r > 0) {
  144. AccountManager::instance()->save(false); // don't save credentials, they had not been loaded from keychain
  145. }
  146. return r;
  147. }
  148. qCInfo(lcFolderMan) << "Setup folders from settings file";
  149. for (const auto &account : AccountManager::instance()->accounts()) {
  150. const auto id = account->account()->id();
  151. if (!accountsWithSettings.contains(id)) {
  152. continue;
  153. }
  154. settings->beginGroup(id);
  155. // The "backwardsCompatible" flag here is related to migrating old
  156. // database locations
  157. auto process = [&](const QString &groupName, bool backwardsCompatible, bool foldersWithPlaceholders) {
  158. settings->beginGroup(groupName);
  159. if (skipSettingsKeys.contains(settings->group())) {
  160. // Should not happen: bad container keys should have been deleted
  161. qCWarning(lcFolderMan) << "Folder structure" << groupName << "is too new, ignoring";
  162. } else {
  163. setupFoldersHelper(*settings, account, skipSettingsKeys, backwardsCompatible, foldersWithPlaceholders);
  164. }
  165. settings->endGroup();
  166. };
  167. process(QStringLiteral("Folders"), true, false);
  168. // See Folder::saveToSettings for details about why these exists.
  169. process(QStringLiteral("Multifolders"), false, false);
  170. process(QStringLiteral("FoldersWithPlaceholders"), false, true);
  171. settings->endGroup(); // <account>
  172. }
  173. emit folderListChanged(_folderMap);
  174. return _folderMap.size();
  175. }
  176. void FolderMan::setupFoldersHelper(QSettings &settings, AccountStatePtr account, const QStringList &ignoreKeys, bool backwardsCompatible, bool foldersWithPlaceholders)
  177. {
  178. for (const auto &folderAlias : settings.childGroups()) {
  179. // Skip folders with too-new version
  180. settings.beginGroup(folderAlias);
  181. if (ignoreKeys.contains(settings.group())) {
  182. qCInfo(lcFolderMan) << "Folder" << folderAlias << "is too new, ignoring";
  183. _additionalBlockedFolderAliases.insert(folderAlias);
  184. settings.endGroup();
  185. continue;
  186. }
  187. settings.endGroup();
  188. FolderDefinition folderDefinition;
  189. settings.beginGroup(folderAlias);
  190. if (FolderDefinition::load(settings, folderAlias, &folderDefinition)) {
  191. auto defaultJournalPath = folderDefinition.defaultJournalPath(account->account());
  192. // Migration: Old settings don't have journalPath
  193. if (folderDefinition.journalPath.isEmpty()) {
  194. folderDefinition.journalPath = defaultJournalPath;
  195. }
  196. // Migration #2: journalPath might be absolute (in DataAppDir most likely) move it back to the root of local tree
  197. if (folderDefinition.journalPath.at(0) != QChar('.')) {
  198. QFile oldJournal(folderDefinition.journalPath);
  199. QFile oldJournalShm(folderDefinition.journalPath + QStringLiteral("-shm"));
  200. QFile oldJournalWal(folderDefinition.journalPath + QStringLiteral("-wal"));
  201. folderDefinition.journalPath = defaultJournalPath;
  202. socketApi()->slotUnregisterPath(folderAlias);
  203. auto settings = account->settings();
  204. auto journalFileMoveSuccess = true;
  205. // Due to db logic can't be sure which of these file exist.
  206. if (oldJournal.exists()) {
  207. journalFileMoveSuccess &= oldJournal.rename(folderDefinition.localPath + "/" + folderDefinition.journalPath);
  208. }
  209. if (oldJournalShm.exists()) {
  210. journalFileMoveSuccess &= oldJournalShm.rename(folderDefinition.localPath + "/" + folderDefinition.journalPath + QStringLiteral("-shm"));
  211. }
  212. if (oldJournalWal.exists()) {
  213. journalFileMoveSuccess &= oldJournalWal.rename(folderDefinition.localPath + "/" + folderDefinition.journalPath + QStringLiteral("-wal"));
  214. }
  215. if (!journalFileMoveSuccess) {
  216. qCWarning(lcFolderMan) << "Wasn't able to move 3.0 syncjournal database files to new location. One-time loss off sync settings possible.";
  217. } else {
  218. qCInfo(lcFolderMan) << "Successfully migrated syncjournal database.";
  219. }
  220. auto vfs = createVfsFromPlugin(folderDefinition.virtualFilesMode);
  221. if (!vfs && folderDefinition.virtualFilesMode != Vfs::Off) {
  222. qCWarning(lcFolderMan) << "Could not load plugin for mode" << folderDefinition.virtualFilesMode;
  223. }
  224. Folder *f = addFolderInternal(folderDefinition, account.data(), std::move(vfs));
  225. f->saveToSettings();
  226. continue;
  227. }
  228. // Migration: ._ files sometimes can't be created.
  229. // So if the configured journalPath has a dot-underscore ("._sync_*.db")
  230. // but the current default doesn't have the underscore, switch to the
  231. // new default if no db exists yet.
  232. if (folderDefinition.journalPath.startsWith("._sync_")
  233. && defaultJournalPath.startsWith(".sync_")
  234. && !QFile::exists(folderDefinition.absoluteJournalPath())) {
  235. folderDefinition.journalPath = defaultJournalPath;
  236. }
  237. // Migration: If an old db is found, move it to the new name.
  238. if (backwardsCompatible) {
  239. SyncJournalDb::maybeMigrateDb(folderDefinition.localPath, folderDefinition.absoluteJournalPath());
  240. }
  241. auto vfs = createVfsFromPlugin(folderDefinition.virtualFilesMode);
  242. if (!vfs) {
  243. // TODO: Must do better error handling
  244. qFatal("Could not load plugin");
  245. }
  246. Folder *f = addFolderInternal(std::move(folderDefinition), account.data(), std::move(vfs));
  247. if (f) {
  248. // Migrate the old "usePlaceholders" setting to the root folder pin state
  249. if (settings.value(QLatin1String(versionC), 1).toInt() == 1
  250. && settings.value(QLatin1String("usePlaceholders"), false).toBool()) {
  251. qCInfo(lcFolderMan) << "Migrate: From usePlaceholders to PinState::OnlineOnly";
  252. f->setRootPinState(PinState::OnlineOnly);
  253. }
  254. // Migration: Mark folders that shall be saved in a backwards-compatible way
  255. if (backwardsCompatible)
  256. f->setSaveBackwardsCompatible(true);
  257. if (foldersWithPlaceholders)
  258. f->setSaveInFoldersWithPlaceholders();
  259. scheduleFolder(f);
  260. emit folderSyncStateChange(f);
  261. }
  262. }
  263. settings.endGroup();
  264. }
  265. }
  266. int FolderMan::setupFoldersMigration()
  267. {
  268. ConfigFile cfg;
  269. QDir storageDir(cfg.configPath());
  270. _folderConfigPath = cfg.configPath() + QLatin1String("folders");
  271. qCInfo(lcFolderMan) << "Setup folders from " << _folderConfigPath << "(migration)";
  272. QDir dir(_folderConfigPath);
  273. //We need to include hidden files just in case the alias starts with '.'
  274. dir.setFilter(QDir::Files | QDir::Hidden);
  275. const auto list = dir.entryList();
  276. // Normally there should be only one account when migrating.
  277. AccountState *accountState = AccountManager::instance()->accounts().value(0).data();
  278. for (const auto &alias : list) {
  279. Folder *f = setupFolderFromOldConfigFile(alias, accountState);
  280. if (f) {
  281. scheduleFolder(f);
  282. emit folderSyncStateChange(f);
  283. }
  284. }
  285. emit folderListChanged(_folderMap);
  286. // return the number of valid folders.
  287. return _folderMap.size();
  288. }
  289. void FolderMan::backwardMigrationSettingsKeys(QStringList *deleteKeys, QStringList *ignoreKeys)
  290. {
  291. auto settings = ConfigFile::settingsWithGroup(QLatin1String("Accounts"));
  292. auto processSubgroup = [&](const QString &name) {
  293. settings->beginGroup(name);
  294. const int foldersVersion = settings->value(QLatin1String(versionC), 1).toInt();
  295. if (foldersVersion <= maxFoldersVersion) {
  296. foreach (const auto &folderAlias, settings->childGroups()) {
  297. settings->beginGroup(folderAlias);
  298. const int folderVersion = settings->value(QLatin1String(versionC), 1).toInt();
  299. if (folderVersion > FolderDefinition::maxSettingsVersion()) {
  300. ignoreKeys->append(settings->group());
  301. }
  302. settings->endGroup();
  303. }
  304. } else {
  305. deleteKeys->append(settings->group());
  306. }
  307. settings->endGroup();
  308. };
  309. for (const auto &accountId : settings->childGroups()) {
  310. settings->beginGroup(accountId);
  311. processSubgroup("Folders");
  312. processSubgroup("Multifolders");
  313. processSubgroup("FoldersWithPlaceholders");
  314. settings->endGroup();
  315. }
  316. }
  317. bool FolderMan::ensureJournalGone(const QString &journalDbFile)
  318. {
  319. // remove the old journal file
  320. while (QFile::exists(journalDbFile) && !QFile::remove(journalDbFile)) {
  321. qCWarning(lcFolderMan) << "Could not remove old db file at" << journalDbFile;
  322. int ret = QMessageBox::warning(nullptr, tr("Could not reset folder state"),
  323. tr("An old sync journal \"%1\" was found, "
  324. "but could not be removed. Please make sure "
  325. "that no application is currently using it.")
  326. .arg(QDir::fromNativeSeparators(QDir::cleanPath(journalDbFile))),
  327. QMessageBox::Retry | QMessageBox::Abort);
  328. if (ret == QMessageBox::Abort) {
  329. return false;
  330. }
  331. }
  332. return true;
  333. }
  334. #define SLASH_TAG QLatin1String("__SLASH__")
  335. #define BSLASH_TAG QLatin1String("__BSLASH__")
  336. #define QMARK_TAG QLatin1String("__QMARK__")
  337. #define PERCENT_TAG QLatin1String("__PERCENT__")
  338. #define STAR_TAG QLatin1String("__STAR__")
  339. #define COLON_TAG QLatin1String("__COLON__")
  340. #define PIPE_TAG QLatin1String("__PIPE__")
  341. #define QUOTE_TAG QLatin1String("__QUOTE__")
  342. #define LT_TAG QLatin1String("__LESS_THAN__")
  343. #define GT_TAG QLatin1String("__GREATER_THAN__")
  344. #define PAR_O_TAG QLatin1String("__PAR_OPEN__")
  345. #define PAR_C_TAG QLatin1String("__PAR_CLOSE__")
  346. QString FolderMan::escapeAlias(const QString &alias)
  347. {
  348. QString a(alias);
  349. a.replace(QLatin1Char('/'), SLASH_TAG);
  350. a.replace(QLatin1Char('\\'), BSLASH_TAG);
  351. a.replace(QLatin1Char('?'), QMARK_TAG);
  352. a.replace(QLatin1Char('%'), PERCENT_TAG);
  353. a.replace(QLatin1Char('*'), STAR_TAG);
  354. a.replace(QLatin1Char(':'), COLON_TAG);
  355. a.replace(QLatin1Char('|'), PIPE_TAG);
  356. a.replace(QLatin1Char('"'), QUOTE_TAG);
  357. a.replace(QLatin1Char('<'), LT_TAG);
  358. a.replace(QLatin1Char('>'), GT_TAG);
  359. a.replace(QLatin1Char('['), PAR_O_TAG);
  360. a.replace(QLatin1Char(']'), PAR_C_TAG);
  361. return a;
  362. }
  363. SocketApi *FolderMan::socketApi()
  364. {
  365. return this->_socketApi.data();
  366. }
  367. QString FolderMan::unescapeAlias(const QString &alias)
  368. {
  369. QString a(alias);
  370. a.replace(SLASH_TAG, QLatin1String("/"));
  371. a.replace(BSLASH_TAG, QLatin1String("\\"));
  372. a.replace(QMARK_TAG, QLatin1String("?"));
  373. a.replace(PERCENT_TAG, QLatin1String("%"));
  374. a.replace(STAR_TAG, QLatin1String("*"));
  375. a.replace(COLON_TAG, QLatin1String(":"));
  376. a.replace(PIPE_TAG, QLatin1String("|"));
  377. a.replace(QUOTE_TAG, QLatin1String("\""));
  378. a.replace(LT_TAG, QLatin1String("<"));
  379. a.replace(GT_TAG, QLatin1String(">"));
  380. a.replace(PAR_O_TAG, QLatin1String("["));
  381. a.replace(PAR_C_TAG, QLatin1String("]"));
  382. return a;
  383. }
  384. // filename is the name of the file only, it does not include
  385. // the configuration directory path
  386. // WARNING: Do not remove this code, it is used for predefined/automated deployments (2016)
  387. Folder *FolderMan::setupFolderFromOldConfigFile(const QString &file, AccountState *accountState)
  388. {
  389. Folder *folder = nullptr;
  390. qCInfo(lcFolderMan) << " ` -> setting up:" << file;
  391. QString escapedAlias(file);
  392. // check the unescaped variant (for the case when the filename comes out
  393. // of the directory listing). If the file does not exist, escape the
  394. // file and try again.
  395. QFileInfo cfgFile(_folderConfigPath, file);
  396. if (!cfgFile.exists()) {
  397. // try the escaped variant.
  398. escapedAlias = escapeAlias(file);
  399. cfgFile.setFile(_folderConfigPath, escapedAlias);
  400. }
  401. if (!cfgFile.isReadable()) {
  402. qCWarning(lcFolderMan) << "Cannot read folder definition for alias " << cfgFile.filePath();
  403. return folder;
  404. }
  405. QSettings settings(_folderConfigPath + QLatin1Char('/') + escapedAlias, QSettings::IniFormat);
  406. qCInfo(lcFolderMan) << " -> file path: " << settings.fileName();
  407. // Check if the filename is equal to the group setting. If not, use the group
  408. // name as an alias.
  409. QStringList groups = settings.childGroups();
  410. if (!groups.contains(escapedAlias) && groups.count() > 0) {
  411. escapedAlias = groups.first();
  412. }
  413. settings.beginGroup(escapedAlias); // read the group with the same name as the file which is the folder alias
  414. QString path = settings.value(QLatin1String("localPath")).toString();
  415. QString backend = settings.value(QLatin1String("backend")).toString();
  416. QString targetPath = settings.value(QLatin1String("targetPath")).toString();
  417. bool paused = settings.value(QLatin1String("paused"), false).toBool();
  418. // QString connection = settings.value( QLatin1String("connection") ).toString();
  419. QString alias = unescapeAlias(escapedAlias);
  420. if (backend.isEmpty() || backend != QLatin1String("owncloud")) {
  421. qCWarning(lcFolderMan) << "obsolete configuration of type" << backend;
  422. return nullptr;
  423. }
  424. // cut off the leading slash, oCUrl always has a trailing.
  425. if (targetPath.startsWith(QLatin1Char('/'))) {
  426. targetPath.remove(0, 1);
  427. }
  428. if (!accountState) {
  429. qCCritical(lcFolderMan) << "can't create folder without an account";
  430. return nullptr;
  431. }
  432. FolderDefinition folderDefinition;
  433. folderDefinition.alias = alias;
  434. folderDefinition.localPath = path;
  435. folderDefinition.targetPath = targetPath;
  436. folderDefinition.paused = paused;
  437. folderDefinition.ignoreHiddenFiles = ignoreHiddenFiles();
  438. folder = addFolderInternal(folderDefinition, accountState, std::make_unique<VfsOff>());
  439. if (folder) {
  440. QStringList blackList = settings.value(QLatin1String("blackList")).toStringList();
  441. if (!blackList.empty()) {
  442. //migrate settings
  443. folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, blackList);
  444. settings.remove(QLatin1String("blackList"));
  445. // FIXME: If you remove this codepath, you need to provide another way to do
  446. // this via theme.h or the normal FolderMan::setupFolders
  447. }
  448. folder->saveToSettings();
  449. }
  450. qCInfo(lcFolderMan) << "Migrated!" << folder;
  451. settings.sync();
  452. return folder;
  453. }
  454. void FolderMan::slotFolderSyncPaused(Folder *f, bool paused)
  455. {
  456. if (!f) {
  457. qCCritical(lcFolderMan) << "slotFolderSyncPaused called with empty folder";
  458. return;
  459. }
  460. if (!paused) {
  461. _disabledFolders.remove(f);
  462. scheduleFolder(f);
  463. } else {
  464. _disabledFolders.insert(f);
  465. }
  466. }
  467. void FolderMan::slotFolderCanSyncChanged()
  468. {
  469. auto *f = qobject_cast<Folder *>(sender());
  470. ASSERT(f);
  471. if (f->canSync()) {
  472. _socketApi->slotRegisterPath(f->alias());
  473. } else {
  474. _socketApi->slotUnregisterPath(f->alias());
  475. }
  476. }
  477. Folder *FolderMan::folder(const QString &alias)
  478. {
  479. if (!alias.isEmpty()) {
  480. if (_folderMap.contains(alias)) {
  481. return _folderMap[alias];
  482. }
  483. }
  484. return nullptr;
  485. }
  486. void FolderMan::scheduleAllFolders()
  487. {
  488. for (Folder *f : _folderMap.values()) {
  489. if (f && f->canSync()) {
  490. scheduleFolder(f);
  491. }
  492. }
  493. }
  494. void FolderMan::slotScheduleAppRestart()
  495. {
  496. _appRestartRequired = true;
  497. qCInfo(lcFolderMan) << "Application restart requested!";
  498. }
  499. void FolderMan::slotSyncOnceFileUnlocks(const QString &path)
  500. {
  501. _lockWatcher->addFile(path);
  502. }
  503. /*
  504. * if a folder wants to be synced, it calls this slot and is added
  505. * to the queue. The slot to actually start a sync is called afterwards.
  506. */
  507. void FolderMan::scheduleFolder(Folder *f)
  508. {
  509. if (!f) {
  510. qCCritical(lcFolderMan) << "slotScheduleSync called with null folder";
  511. return;
  512. }
  513. auto alias = f->alias();
  514. qCInfo(lcFolderMan) << "Schedule folder " << alias << " to sync!";
  515. if (!_scheduledFolders.contains(f)) {
  516. if (!f->canSync()) {
  517. qCInfo(lcFolderMan) << "Folder is not ready to sync, not scheduled!";
  518. _socketApi->slotUpdateFolderView(f);
  519. return;
  520. }
  521. f->prepareToSync();
  522. emit folderSyncStateChange(f);
  523. _scheduledFolders.enqueue(f);
  524. emit scheduleQueueChanged();
  525. } else {
  526. qCInfo(lcFolderMan) << "Sync for folder " << alias << " already scheduled, do not enqueue!";
  527. }
  528. startScheduledSyncSoon();
  529. }
  530. void FolderMan::scheduleFolderNext(Folder *f)
  531. {
  532. auto alias = f->alias();
  533. qCInfo(lcFolderMan) << "Schedule folder " << alias << " to sync! Front-of-queue.";
  534. if (!f->canSync()) {
  535. qCInfo(lcFolderMan) << "Folder is not ready to sync, not scheduled!";
  536. return;
  537. }
  538. _scheduledFolders.removeAll(f);
  539. f->prepareToSync();
  540. emit folderSyncStateChange(f);
  541. _scheduledFolders.prepend(f);
  542. emit scheduleQueueChanged();
  543. startScheduledSyncSoon();
  544. }
  545. void FolderMan::slotScheduleETagJob(const QString & /*alias*/, RequestEtagJob *job)
  546. {
  547. QObject::connect(job, &QObject::destroyed, this, &FolderMan::slotEtagJobDestroyed);
  548. QMetaObject::invokeMethod(this, "slotRunOneEtagJob", Qt::QueuedConnection);
  549. // maybe: add to queue
  550. }
  551. void FolderMan::slotEtagJobDestroyed(QObject * /*o*/)
  552. {
  553. // _currentEtagJob is automatically cleared
  554. // maybe: remove from queue
  555. QMetaObject::invokeMethod(this, "slotRunOneEtagJob", Qt::QueuedConnection);
  556. }
  557. void FolderMan::slotRunOneEtagJob()
  558. {
  559. if (_currentEtagJob.isNull()) {
  560. Folder *folder = nullptr;
  561. for (Folder *f : qAsConst(_folderMap)) {
  562. if (f->etagJob()) {
  563. // Caveat: always grabs the first folder with a job, but we think this is Ok for now and avoids us having a seperate queue.
  564. _currentEtagJob = f->etagJob();
  565. folder = f;
  566. break;
  567. }
  568. }
  569. if (_currentEtagJob.isNull()) {
  570. //qCDebug(lcFolderMan) << "No more remote ETag check jobs to schedule.";
  571. /* now it might be a good time to check for restarting... */
  572. if (!isAnySyncRunning() && _appRestartRequired) {
  573. restartApplication();
  574. }
  575. } else {
  576. qCDebug(lcFolderMan) << "Scheduling" << folder->remoteUrl().toString() << "to check remote ETag";
  577. _currentEtagJob->start(); // on destroy/end it will continue the queue via slotEtagJobDestroyed
  578. }
  579. }
  580. }
  581. void FolderMan::slotAccountStateChanged()
  582. {
  583. auto *accountState = qobject_cast<AccountState *>(sender());
  584. if (!accountState) {
  585. return;
  586. }
  587. QString accountName = accountState->account()->displayName();
  588. if (accountState->isConnected()) {
  589. qCInfo(lcFolderMan) << "Account" << accountName << "connected, scheduling its folders";
  590. for (Folder *f : _folderMap.values()) {
  591. if (f
  592. && f->canSync()
  593. && f->accountState() == accountState) {
  594. scheduleFolder(f);
  595. }
  596. }
  597. } else {
  598. qCInfo(lcFolderMan) << "Account" << accountName << "disconnected or paused, "
  599. "terminating or descheduling sync folders";
  600. foreach (Folder *f, _folderMap.values()) {
  601. if (f
  602. && f->isSyncRunning()
  603. && f->accountState() == accountState) {
  604. f->slotTerminateSync();
  605. }
  606. }
  607. QMutableListIterator<Folder *> it(_scheduledFolders);
  608. while (it.hasNext()) {
  609. Folder *f = it.next();
  610. if (f->accountState() == accountState) {
  611. it.remove();
  612. }
  613. }
  614. emit scheduleQueueChanged();
  615. }
  616. }
  617. // only enable or disable foldermans will schedule and do syncs.
  618. // this is not the same as Pause and Resume of folders.
  619. void FolderMan::setSyncEnabled(bool enabled)
  620. {
  621. if (!_syncEnabled && enabled && !_scheduledFolders.isEmpty()) {
  622. // We have things in our queue that were waiting for the connection to come back on.
  623. startScheduledSyncSoon();
  624. }
  625. _syncEnabled = enabled;
  626. // force a redraw in case the network connect status changed
  627. emit(folderSyncStateChange(nullptr));
  628. }
  629. void FolderMan::startScheduledSyncSoon()
  630. {
  631. if (_startScheduledSyncTimer.isActive()) {
  632. return;
  633. }
  634. if (_scheduledFolders.empty()) {
  635. return;
  636. }
  637. if (isAnySyncRunning()) {
  638. return;
  639. }
  640. qint64 msDelay = 100; // 100ms minimum delay
  641. qint64 msSinceLastSync = 0;
  642. // Require a pause based on the duration of the last sync run.
  643. if (Folder *lastFolder = _lastSyncFolder) {
  644. msSinceLastSync = lastFolder->msecSinceLastSync().count();
  645. // 1s -> 1.5s pause
  646. // 10s -> 5s pause
  647. // 1min -> 12s pause
  648. // 1h -> 90s pause
  649. qint64 pause = qSqrt(lastFolder->msecLastSyncDuration().count()) / 20.0 * 1000.0;
  650. msDelay = qMax(msDelay, pause);
  651. }
  652. // Delays beyond one minute seem too big, particularly since there
  653. // could be things later in the queue that shouldn't be punished by a
  654. // long delay!
  655. msDelay = qMin(msDelay, 60 * 1000ll);
  656. // Time since the last sync run counts against the delay
  657. msDelay = qMax(1ll, msDelay - msSinceLastSync);
  658. qCInfo(lcFolderMan) << "Starting the next scheduled sync in" << (msDelay / 1000) << "seconds";
  659. _startScheduledSyncTimer.start(msDelay);
  660. }
  661. /*
  662. * slot to start folder syncs.
  663. * It is either called from the slot where folders enqueue themselves for
  664. * syncing or after a folder sync was finished.
  665. */
  666. void FolderMan::slotStartScheduledFolderSync()
  667. {
  668. if (isAnySyncRunning()) {
  669. for (auto f : _folderMap) {
  670. if (f->isSyncRunning())
  671. qCInfo(lcFolderMan) << "Currently folder " << f->remoteUrl().toString() << " is running, wait for finish!";
  672. }
  673. return;
  674. }
  675. if (!_syncEnabled) {
  676. qCInfo(lcFolderMan) << "FolderMan: Syncing is disabled, no scheduling.";
  677. return;
  678. }
  679. qCDebug(lcFolderMan) << "folderQueue size: " << _scheduledFolders.count();
  680. if (_scheduledFolders.isEmpty()) {
  681. return;
  682. }
  683. // Find the first folder in the queue that can be synced.
  684. Folder *folder = nullptr;
  685. while (!_scheduledFolders.isEmpty()) {
  686. Folder *g = _scheduledFolders.dequeue();
  687. if (g->canSync()) {
  688. folder = g;
  689. break;
  690. }
  691. }
  692. emit scheduleQueueChanged();
  693. // Start syncing this folder!
  694. if (folder) {
  695. // Safe to call several times, and necessary to try again if
  696. // the folder path didn't exist previously.
  697. folder->registerFolderWatcher();
  698. registerFolderWithSocketApi(folder);
  699. _currentSyncFolder = folder;
  700. folder->startSync(QStringList());
  701. }
  702. }
  703. bool FolderMan::pushNotificationsFilesReady(Account *account)
  704. {
  705. const auto pushNotifications = account->pushNotifications();
  706. const auto pushFilesAvailable = account->capabilities().availablePushNotifications() & PushNotificationType::Files;
  707. return pushFilesAvailable && pushNotifications && pushNotifications->isReady();
  708. }
  709. void FolderMan::slotEtagPollTimerTimeout()
  710. {
  711. qCInfo(lcFolderMan) << "Etag poll timer timeout";
  712. const auto folderMapValues = _folderMap.values();
  713. qCInfo(lcFolderMan) << "Folders to sync:" << folderMapValues.size();
  714. QList<Folder *> foldersToRun;
  715. // Some folders need not to be checked because they use the push notifications
  716. std::copy_if(folderMapValues.begin(), folderMapValues.end(), std::back_inserter(foldersToRun), [this](Folder *folder) -> bool {
  717. const auto account = folder->accountState()->account();
  718. return !pushNotificationsFilesReady(account.data());
  719. });
  720. qCInfo(lcFolderMan) << "Number of folders that don't use push notifications:" << foldersToRun.size();
  721. runEtagJobsIfPossible(foldersToRun);
  722. }
  723. void FolderMan::runEtagJobsIfPossible(const QList<Folder *> &folderMap)
  724. {
  725. for (auto folder : folderMap) {
  726. runEtagJobIfPossible(folder);
  727. }
  728. }
  729. void FolderMan::runEtagJobIfPossible(Folder *folder)
  730. {
  731. const ConfigFile cfg;
  732. const auto polltime = cfg.remotePollInterval();
  733. qCInfo(lcFolderMan) << "Run etag job on folder" << folder;
  734. if (!folder) {
  735. return;
  736. }
  737. if (folder->isSyncRunning()) {
  738. qCInfo(lcFolderMan) << "Can not run etag job: Sync is running";
  739. return;
  740. }
  741. if (_scheduledFolders.contains(folder)) {
  742. qCInfo(lcFolderMan) << "Can not run etag job: Folder is alreday scheduled";
  743. return;
  744. }
  745. if (_disabledFolders.contains(folder)) {
  746. qCInfo(lcFolderMan) << "Can not run etag job: Folder is disabled";
  747. return;
  748. }
  749. if (folder->etagJob() || folder->isBusy() || !folder->canSync()) {
  750. qCInfo(lcFolderMan) << "Can not run etag job: Folder is busy";
  751. return;
  752. }
  753. // When not using push notifications, make sure polltime is reached
  754. if (!pushNotificationsFilesReady(folder->accountState()->account().data())) {
  755. if (folder->msecSinceLastSync() < polltime) {
  756. qCInfo(lcFolderMan) << "Can not run etag job: Polltime not reached";
  757. return;
  758. }
  759. }
  760. QMetaObject::invokeMethod(folder, "slotRunEtagJob", Qt::QueuedConnection);
  761. }
  762. void FolderMan::slotAccountRemoved(AccountState *accountState)
  763. {
  764. for (const auto &folder : qAsConst(_folderMap)) {
  765. if (folder->accountState() == accountState) {
  766. folder->onAssociatedAccountRemoved();
  767. }
  768. }
  769. }
  770. void FolderMan::slotRemoveFoldersForAccount(AccountState *accountState)
  771. {
  772. QVarLengthArray<Folder *, 16> foldersToRemove;
  773. Folder::MapIterator i(_folderMap);
  774. while (i.hasNext()) {
  775. i.next();
  776. Folder *folder = i.value();
  777. if (folder->accountState() == accountState) {
  778. foldersToRemove.append(folder);
  779. }
  780. }
  781. for (const auto &f : qAsConst(foldersToRemove)) {
  782. removeFolder(f);
  783. }
  784. emit folderListChanged(_folderMap);
  785. }
  786. void FolderMan::slotForwardFolderSyncStateChange()
  787. {
  788. if (auto *f = qobject_cast<Folder *>(sender())) {
  789. emit folderSyncStateChange(f);
  790. }
  791. }
  792. void FolderMan::slotServerVersionChanged(Account *account)
  793. {
  794. // Pause folders if the server version is unsupported
  795. if (account->serverVersionUnsupported()) {
  796. qCWarning(lcFolderMan) << "The server version is unsupported:" << account->serverVersion()
  797. << "pausing all folders on the account";
  798. for (auto &f : qAsConst(_folderMap)) {
  799. if (f->accountState()->account().data() == account) {
  800. f->setSyncPaused(true);
  801. }
  802. }
  803. }
  804. }
  805. void FolderMan::slotWatchedFileUnlocked(const QString &path)
  806. {
  807. if (Folder *f = folderForPath(path)) {
  808. // Treat this equivalently to the file being reported by the file watcher
  809. f->slotWatchedPathChanged(path, Folder::ChangeReason::UnLock);
  810. }
  811. }
  812. void FolderMan::slotScheduleFolderByTime()
  813. {
  814. for (const auto &f : qAsConst(_folderMap)) {
  815. // Never schedule if syncing is disabled or when we're currently
  816. // querying the server for etags
  817. if (!f->canSync() || f->etagJob()) {
  818. continue;
  819. }
  820. auto msecsSinceSync = f->msecSinceLastSync();
  821. // Possibly it's just time for a new sync run
  822. bool forceSyncIntervalExpired = msecsSinceSync > ConfigFile().forceSyncInterval();
  823. if (forceSyncIntervalExpired) {
  824. qCInfo(lcFolderMan) << "Scheduling folder" << f->alias()
  825. << "because it has been" << msecsSinceSync.count() << "ms "
  826. << "since the last sync";
  827. scheduleFolder(f);
  828. continue;
  829. }
  830. // Retry a couple of times after failure; or regularly if requested
  831. bool syncAgain =
  832. (f->consecutiveFailingSyncs() > 0 && f->consecutiveFailingSyncs() < 3)
  833. || f->syncEngine().isAnotherSyncNeeded() == DelayedFollowUp;
  834. auto syncAgainDelay = std::chrono::seconds(10); // 10s for the first retry-after-fail
  835. if (f->consecutiveFailingSyncs() > 1)
  836. syncAgainDelay = std::chrono::seconds(60); // 60s for each further attempt
  837. if (syncAgain && msecsSinceSync > syncAgainDelay) {
  838. qCInfo(lcFolderMan) << "Scheduling folder" << f->alias()
  839. << ", the last" << f->consecutiveFailingSyncs() << "syncs failed"
  840. << ", anotherSyncNeeded" << f->syncEngine().isAnotherSyncNeeded()
  841. << ", last status:" << f->syncResult().statusString()
  842. << ", time since last sync:" << msecsSinceSync.count();
  843. scheduleFolder(f);
  844. continue;
  845. }
  846. // Do we want to retry failing syncs or another-sync-needed runs more often?
  847. }
  848. }
  849. bool FolderMan::isAnySyncRunning() const
  850. {
  851. if (_currentSyncFolder)
  852. return true;
  853. for (auto f : _folderMap) {
  854. if (f->isSyncRunning())
  855. return true;
  856. }
  857. return false;
  858. }
  859. void FolderMan::slotFolderSyncStarted()
  860. {
  861. auto f = qobject_cast<Folder *>(sender());
  862. ASSERT(f);
  863. if (!f)
  864. return;
  865. qCInfo(lcFolderMan, ">========== Sync started for folder [%s] of account [%s] with remote [%s]",
  866. qPrintable(f->shortGuiLocalPath()),
  867. qPrintable(f->accountState()->account()->displayName()),
  868. qPrintable(f->remoteUrl().toString()));
  869. }
  870. /*
  871. * a folder indicates that its syncing is finished.
  872. * Start the next sync after the system had some milliseconds to breath.
  873. * This delay is particularly useful to avoid late file change notifications
  874. * (that we caused ourselves by syncing) from triggering another spurious sync.
  875. */
  876. void FolderMan::slotFolderSyncFinished(const SyncResult &)
  877. {
  878. auto f = qobject_cast<Folder *>(sender());
  879. ASSERT(f);
  880. if (!f)
  881. return;
  882. qCInfo(lcFolderMan, "<========== Sync finished for folder [%s] of account [%s] with remote [%s]",
  883. qPrintable(f->shortGuiLocalPath()),
  884. qPrintable(f->accountState()->account()->displayName()),
  885. qPrintable(f->remoteUrl().toString()));
  886. if (f == _currentSyncFolder) {
  887. _lastSyncFolder = _currentSyncFolder;
  888. _currentSyncFolder = nullptr;
  889. }
  890. if (!isAnySyncRunning())
  891. startScheduledSyncSoon();
  892. }
  893. Folder *FolderMan::addFolder(AccountState *accountState, const FolderDefinition &folderDefinition)
  894. {
  895. // Choose a db filename
  896. auto definition = folderDefinition;
  897. definition.journalPath = definition.defaultJournalPath(accountState->account());
  898. if (!ensureJournalGone(definition.absoluteJournalPath())) {
  899. return nullptr;
  900. }
  901. auto vfs = createVfsFromPlugin(folderDefinition.virtualFilesMode);
  902. if (!vfs) {
  903. qCWarning(lcFolderMan) << "Could not load plugin for mode" << folderDefinition.virtualFilesMode;
  904. return nullptr;
  905. }
  906. auto folder = addFolderInternal(definition, accountState, std::move(vfs));
  907. // Migration: The first account that's configured for a local folder shall
  908. // be saved in a backwards-compatible way.
  909. const auto folderList = FolderMan::instance()->map();
  910. const auto oneAccountOnly = std::none_of(folderList.cbegin(), folderList.cend(), [folder](const auto *other) {
  911. return other != folder && other->cleanPath() == folder->cleanPath();
  912. });
  913. folder->setSaveBackwardsCompatible(oneAccountOnly);
  914. if (folder) {
  915. folder->setSaveBackwardsCompatible(oneAccountOnly);
  916. folder->saveToSettings();
  917. emit folderSyncStateChange(folder);
  918. emit folderListChanged(_folderMap);
  919. }
  920. _navigationPaneHelper.scheduleUpdateCloudStorageRegistry();
  921. return folder;
  922. }
  923. Folder *FolderMan::addFolderInternal(
  924. FolderDefinition folderDefinition,
  925. AccountState *accountState,
  926. std::unique_ptr<Vfs> vfs)
  927. {
  928. auto alias = folderDefinition.alias;
  929. int count = 0;
  930. while (folderDefinition.alias.isEmpty()
  931. || _folderMap.contains(folderDefinition.alias)
  932. || _additionalBlockedFolderAliases.contains(folderDefinition.alias)) {
  933. // There is already a folder configured with this name and folder names need to be unique
  934. folderDefinition.alias = alias + QString::number(++count);
  935. }
  936. auto folder = new Folder(folderDefinition, accountState, std::move(vfs), this);
  937. if (_navigationPaneHelper.showInExplorerNavigationPane() && folderDefinition.navigationPaneClsid.isNull()) {
  938. folder->setNavigationPaneClsid(QUuid::createUuid());
  939. folder->saveToSettings();
  940. }
  941. qCInfo(lcFolderMan) << "Adding folder to Folder Map " << folder << folder->alias();
  942. _folderMap[folder->alias()] = folder;
  943. if (folder->syncPaused()) {
  944. _disabledFolders.insert(folder);
  945. }
  946. // See matching disconnects in unloadFolder().
  947. connect(folder, &Folder::syncStarted, this, &FolderMan::slotFolderSyncStarted);
  948. connect(folder, &Folder::syncFinished, this, &FolderMan::slotFolderSyncFinished);
  949. connect(folder, &Folder::syncStateChange, this, &FolderMan::slotForwardFolderSyncStateChange);
  950. connect(folder, &Folder::syncPausedChanged, this, &FolderMan::slotFolderSyncPaused);
  951. connect(folder, &Folder::canSyncChanged, this, &FolderMan::slotFolderCanSyncChanged);
  952. connect(&folder->syncEngine().syncFileStatusTracker(), &SyncFileStatusTracker::fileStatusChanged,
  953. _socketApi.data(), &SocketApi::broadcastStatusPushMessage);
  954. connect(folder, &Folder::watchedFileChangedExternally,
  955. &folder->syncEngine().syncFileStatusTracker(), &SyncFileStatusTracker::slotPathTouched);
  956. folder->registerFolderWatcher();
  957. registerFolderWithSocketApi(folder);
  958. return folder;
  959. }
  960. Folder *FolderMan::folderForPath(const QString &path)
  961. {
  962. QString absolutePath = QDir::cleanPath(path) + QLatin1Char('/');
  963. const auto folders = this->map().values();
  964. const auto it = std::find_if(folders.cbegin(), folders.cend(), [absolutePath](const auto *folder) {
  965. const QString folderPath = folder->cleanPath() + QLatin1Char('/');
  966. return absolutePath.startsWith(folderPath, (Utility::isWindows() || Utility::isMac()) ? Qt::CaseInsensitive : Qt::CaseSensitive);
  967. });
  968. return it != folders.cend() ? *it : nullptr;
  969. }
  970. QStringList FolderMan::findFileInLocalFolders(const QString &relPath, const AccountPtr acc)
  971. {
  972. QStringList re;
  973. // We'll be comparing against Folder::remotePath which always starts with /
  974. QString serverPath = relPath;
  975. if (!serverPath.startsWith('/'))
  976. serverPath.prepend('/');
  977. for (Folder *folder : this->map().values()) {
  978. if (acc && folder->accountState()->account() != acc) {
  979. continue;
  980. }
  981. if (!serverPath.startsWith(folder->remotePath()))
  982. continue;
  983. QString path = folder->cleanPath() + '/';
  984. path += serverPath.midRef(folder->remotePathTrailingSlash().length());
  985. if (QFile::exists(path)) {
  986. re.append(path);
  987. }
  988. }
  989. return re;
  990. }
  991. void FolderMan::removeFolder(Folder *f)
  992. {
  993. if (!f) {
  994. qCCritical(lcFolderMan) << "Can not remove null folder";
  995. return;
  996. }
  997. qCInfo(lcFolderMan) << "Removing " << f->alias();
  998. const bool currentlyRunning = f->isSyncRunning();
  999. if (currentlyRunning) {
  1000. // abort the sync now
  1001. f->slotTerminateSync();
  1002. }
  1003. if (_scheduledFolders.removeAll(f) > 0) {
  1004. emit scheduleQueueChanged();
  1005. }
  1006. f->setSyncPaused(true);
  1007. f->wipeForRemoval();
  1008. // remove the folder configuration
  1009. f->removeFromSettings();
  1010. unloadFolder(f);
  1011. if (currentlyRunning) {
  1012. // We want to schedule the next folder once this is done
  1013. connect(f, &Folder::syncFinished,
  1014. this, &FolderMan::slotFolderSyncFinished);
  1015. // Let the folder delete itself when done.
  1016. connect(f, &Folder::syncFinished, f, &QObject::deleteLater);
  1017. } else {
  1018. delete f;
  1019. }
  1020. _navigationPaneHelper.scheduleUpdateCloudStorageRegistry();
  1021. emit folderListChanged(_folderMap);
  1022. }
  1023. QString FolderMan::getBackupName(QString fullPathName) const
  1024. {
  1025. if (fullPathName.endsWith("/"))
  1026. fullPathName.chop(1);
  1027. if (fullPathName.isEmpty())
  1028. return QString();
  1029. QString newName = fullPathName + tr(" (backup)");
  1030. QFileInfo fi(newName);
  1031. int cnt = 2;
  1032. do {
  1033. if (fi.exists()) {
  1034. newName = fullPathName + tr(" (backup %1)").arg(cnt++);
  1035. fi.setFile(newName);
  1036. }
  1037. } while (fi.exists());
  1038. return newName;
  1039. }
  1040. bool FolderMan::startFromScratch(const QString &localFolder)
  1041. {
  1042. if (localFolder.isEmpty()) {
  1043. return false;
  1044. }
  1045. QFileInfo fi(localFolder);
  1046. QDir parentDir(fi.dir());
  1047. QString folderName = fi.fileName();
  1048. // Adjust for case where localFolder ends with a /
  1049. if (fi.isDir()) {
  1050. folderName = parentDir.dirName();
  1051. parentDir.cdUp();
  1052. }
  1053. if (fi.exists()) {
  1054. // It exists, but is empty -> just reuse it.
  1055. if (fi.isDir() && fi.dir().count() == 0) {
  1056. qCDebug(lcFolderMan) << "startFromScratch: Directory is empty!";
  1057. return true;
  1058. }
  1059. // Disconnect the socket api from the database to avoid that locking of the
  1060. // db file does not allow to move this dir.
  1061. Folder *f = folderForPath(localFolder);
  1062. if (f) {
  1063. if (localFolder.startsWith(f->path())) {
  1064. _socketApi->slotUnregisterPath(f->alias());
  1065. }
  1066. f->journalDb()->close();
  1067. f->slotTerminateSync(); // Normally it should not be running, but viel hilft viel
  1068. }
  1069. // Make a backup of the folder/file.
  1070. QString newName = getBackupName(parentDir.absoluteFilePath(folderName));
  1071. QString renameError;
  1072. if (!FileSystem::rename(fi.absoluteFilePath(), newName, &renameError)) {
  1073. qCWarning(lcFolderMan) << "startFromScratch: Could not rename" << fi.absoluteFilePath()
  1074. << "to" << newName << "error:" << renameError;
  1075. return false;
  1076. }
  1077. }
  1078. if (!parentDir.mkdir(fi.absoluteFilePath())) {
  1079. qCWarning(lcFolderMan) << "startFromScratch: Could not mkdir" << fi.absoluteFilePath();
  1080. return false;
  1081. }
  1082. return true;
  1083. }
  1084. void FolderMan::slotWipeFolderForAccount(AccountState *accountState)
  1085. {
  1086. QVarLengthArray<Folder *, 16> foldersToRemove;
  1087. Folder::MapIterator i(_folderMap);
  1088. while (i.hasNext()) {
  1089. i.next();
  1090. Folder *folder = i.value();
  1091. if (folder->accountState() == accountState) {
  1092. foldersToRemove.append(folder);
  1093. }
  1094. }
  1095. bool success = false;
  1096. for (const auto &f : qAsConst(foldersToRemove)) {
  1097. if (!f) {
  1098. qCCritical(lcFolderMan) << "Can not remove null folder";
  1099. return;
  1100. }
  1101. qCInfo(lcFolderMan) << "Removing " << f->alias();
  1102. const bool currentlyRunning = (_currentSyncFolder == f);
  1103. if (currentlyRunning) {
  1104. // abort the sync now
  1105. _currentSyncFolder->slotTerminateSync();
  1106. }
  1107. if (_scheduledFolders.removeAll(f) > 0) {
  1108. emit scheduleQueueChanged();
  1109. }
  1110. // wipe database
  1111. f->wipeForRemoval();
  1112. // wipe data
  1113. QDir userFolder(f->path());
  1114. if (userFolder.exists()) {
  1115. success = userFolder.removeRecursively();
  1116. if (!success) {
  1117. qCWarning(lcFolderMan) << "Failed to remove existing folder " << f->path();
  1118. } else {
  1119. qCInfo(lcFolderMan) << "wipe: Removed file " << f->path();
  1120. }
  1121. } else {
  1122. success = true;
  1123. qCWarning(lcFolderMan) << "folder does not exist, can not remove.";
  1124. }
  1125. f->setSyncPaused(true);
  1126. // remove the folder configuration
  1127. f->removeFromSettings();
  1128. unloadFolder(f);
  1129. if (currentlyRunning) {
  1130. delete f;
  1131. }
  1132. _navigationPaneHelper.scheduleUpdateCloudStorageRegistry();
  1133. }
  1134. emit folderListChanged(_folderMap);
  1135. emit wipeDone(accountState, success);
  1136. }
  1137. void FolderMan::setDirtyProxy()
  1138. {
  1139. for (const Folder *f : _folderMap.values()) {
  1140. if (f) {
  1141. if (f->accountState() && f->accountState()->account()
  1142. && f->accountState()->account()->networkAccessManager()) {
  1143. // Need to do this so we do not use the old determined system proxy
  1144. f->accountState()->account()->networkAccessManager()->setProxy(
  1145. QNetworkProxy(QNetworkProxy::DefaultProxy));
  1146. }
  1147. }
  1148. }
  1149. }
  1150. void FolderMan::setDirtyNetworkLimits()
  1151. {
  1152. for (Folder *f : _folderMap.values()) {
  1153. // set only in busy folders. Otherwise they read the config anyway.
  1154. if (f && f->isBusy()) {
  1155. f->setDirtyNetworkLimits();
  1156. }
  1157. }
  1158. }
  1159. void FolderMan::trayOverallStatus(const QList<Folder *> &folders,
  1160. SyncResult::Status *status, bool *unresolvedConflicts)
  1161. {
  1162. *status = SyncResult::Undefined;
  1163. *unresolvedConflicts = false;
  1164. int cnt = folders.count();
  1165. // if one folder: show the state of the one folder.
  1166. // if more folders:
  1167. // if one of them has an error -> show error
  1168. // if one is paused, but others ok, show ok
  1169. // do not show "problem" in the tray
  1170. //
  1171. if (cnt == 1) {
  1172. Folder *folder = folders.at(0);
  1173. if (folder) {
  1174. auto syncResult = folder->syncResult();
  1175. if (folder->syncPaused()) {
  1176. *status = SyncResult::Paused;
  1177. } else {
  1178. SyncResult::Status syncStatus = syncResult.status();
  1179. switch (syncStatus) {
  1180. case SyncResult::Undefined:
  1181. *status = SyncResult::Error;
  1182. break;
  1183. case SyncResult::Problem: // don't show the problem icon in tray.
  1184. *status = SyncResult::Success;
  1185. break;
  1186. default:
  1187. *status = syncStatus;
  1188. break;
  1189. }
  1190. }
  1191. *unresolvedConflicts = syncResult.hasUnresolvedConflicts();
  1192. }
  1193. } else {
  1194. int errorsSeen = 0;
  1195. int goodSeen = 0;
  1196. int abortOrPausedSeen = 0;
  1197. int runSeen = 0;
  1198. int various = 0;
  1199. for (const Folder *folder : qAsConst(folders)) {
  1200. SyncResult folderResult = folder->syncResult();
  1201. if (folder->syncPaused()) {
  1202. abortOrPausedSeen++;
  1203. } else {
  1204. SyncResult::Status syncStatus = folderResult.status();
  1205. switch (syncStatus) {
  1206. case SyncResult::Undefined:
  1207. case SyncResult::NotYetStarted:
  1208. various++;
  1209. break;
  1210. case SyncResult::SyncPrepare:
  1211. case SyncResult::SyncRunning:
  1212. runSeen++;
  1213. break;
  1214. case SyncResult::Problem: // don't show the problem icon in tray.
  1215. case SyncResult::Success:
  1216. goodSeen++;
  1217. break;
  1218. case SyncResult::Error:
  1219. case SyncResult::SetupError:
  1220. errorsSeen++;
  1221. break;
  1222. case SyncResult::SyncAbortRequested:
  1223. case SyncResult::Paused:
  1224. abortOrPausedSeen++;
  1225. // no default case on purpose, check compiler warnings
  1226. }
  1227. }
  1228. if (folderResult.hasUnresolvedConflicts())
  1229. *unresolvedConflicts = true;
  1230. }
  1231. if (errorsSeen > 0) {
  1232. *status = SyncResult::Error;
  1233. } else if (abortOrPausedSeen > 0 && abortOrPausedSeen == cnt) {
  1234. // only if all folders are paused
  1235. *status = SyncResult::Paused;
  1236. } else if (runSeen > 0) {
  1237. *status = SyncResult::SyncRunning;
  1238. } else if (goodSeen > 0) {
  1239. *status = SyncResult::Success;
  1240. }
  1241. }
  1242. }
  1243. QString FolderMan::trayTooltipStatusString(
  1244. SyncResult::Status syncStatus, bool hasUnresolvedConflicts, bool paused)
  1245. {
  1246. QString folderMessage;
  1247. switch (syncStatus) {
  1248. case SyncResult::Undefined:
  1249. folderMessage = tr("Undefined State.");
  1250. break;
  1251. case SyncResult::NotYetStarted:
  1252. folderMessage = tr("Waiting to start syncing.");
  1253. break;
  1254. case SyncResult::SyncPrepare:
  1255. folderMessage = tr("Preparing for sync.");
  1256. break;
  1257. case SyncResult::SyncRunning:
  1258. folderMessage = tr("Sync is running.");
  1259. break;
  1260. case SyncResult::Success:
  1261. case SyncResult::Problem:
  1262. if (hasUnresolvedConflicts) {
  1263. folderMessage = tr("Sync finished with unresolved conflicts.");
  1264. } else {
  1265. folderMessage = tr("Last Sync was successful.");
  1266. }
  1267. break;
  1268. case SyncResult::Error:
  1269. break;
  1270. case SyncResult::SetupError:
  1271. folderMessage = tr("Setup Error.");
  1272. break;
  1273. case SyncResult::SyncAbortRequested:
  1274. folderMessage = tr("User Abort.");
  1275. break;
  1276. case SyncResult::Paused:
  1277. folderMessage = tr("Sync is paused.");
  1278. break;
  1279. // no default case on purpose, check compiler warnings
  1280. }
  1281. if (paused) {
  1282. // sync is disabled.
  1283. folderMessage = tr("%1 (Sync is paused)").arg(folderMessage);
  1284. }
  1285. return folderMessage;
  1286. }
  1287. static QString checkPathValidityRecursive(const QString &path)
  1288. {
  1289. if (path.isEmpty()) {
  1290. return FolderMan::tr("No valid folder selected!");
  1291. }
  1292. #ifdef Q_OS_WIN
  1293. Utility::NtfsPermissionLookupRAII ntfs_perm;
  1294. #endif
  1295. const QFileInfo selFile(path);
  1296. if (!selFile.exists()) {
  1297. QString parentPath = selFile.dir().path();
  1298. if (parentPath != path)
  1299. return checkPathValidityRecursive(parentPath);
  1300. return FolderMan::tr("The selected path does not exist!");
  1301. }
  1302. if (!selFile.isDir()) {
  1303. return FolderMan::tr("The selected path is not a folder!");
  1304. }
  1305. if (!selFile.isWritable()) {
  1306. return FolderMan::tr("You have no permission to write to the selected folder!");
  1307. }
  1308. return QString();
  1309. }
  1310. // QFileInfo::canonicalPath returns an empty string if the file does not exist.
  1311. // This function also works with files that does not exist and resolve the symlinks in the
  1312. // parent directories.
  1313. static QString canonicalPath(const QString &path)
  1314. {
  1315. QFileInfo selFile(path);
  1316. if (!selFile.exists()) {
  1317. const auto parentPath = selFile.dir().path();
  1318. // It's possible for the parentPath to match the path
  1319. // (possibly we've arrived at a non-existant drive root on Windows)
  1320. // and recursing would be fatal.
  1321. if (parentPath == path) {
  1322. return path;
  1323. }
  1324. return canonicalPath(parentPath) + '/' + selFile.fileName();
  1325. }
  1326. return selFile.canonicalFilePath();
  1327. }
  1328. QString FolderMan::checkPathValidityForNewFolder(const QString &path, const QUrl &serverUrl) const
  1329. {
  1330. QString recursiveValidity = checkPathValidityRecursive(path);
  1331. if (!recursiveValidity.isEmpty()) {
  1332. qCDebug(lcFolderMan) << path << recursiveValidity;
  1333. return recursiveValidity;
  1334. }
  1335. // check if the local directory isn't used yet in another ownCloud sync
  1336. Qt::CaseSensitivity cs = Qt::CaseSensitive;
  1337. if (Utility::fsCasePreserving()) {
  1338. cs = Qt::CaseInsensitive;
  1339. }
  1340. const QString userDir = QDir::cleanPath(canonicalPath(path)) + '/';
  1341. for (auto i = _folderMap.constBegin(); i != _folderMap.constEnd(); ++i) {
  1342. auto *f = static_cast<Folder *>(i.value());
  1343. QString folderDir = QDir::cleanPath(canonicalPath(f->path())) + '/';
  1344. bool differentPaths = QString::compare(folderDir, userDir, cs) != 0;
  1345. if (differentPaths && folderDir.startsWith(userDir, cs)) {
  1346. return tr("The local folder %1 already contains a folder used in a folder sync connection. "
  1347. "Please pick another one!")
  1348. .arg(QDir::toNativeSeparators(path));
  1349. }
  1350. if (differentPaths && userDir.startsWith(folderDir, cs)) {
  1351. return tr("The local folder %1 is already contained in a folder used in a folder sync connection. "
  1352. "Please pick another one!")
  1353. .arg(QDir::toNativeSeparators(path));
  1354. }
  1355. // if both pathes are equal, the server url needs to be different
  1356. // otherwise it would mean that a new connection from the same local folder
  1357. // to the same account is added which is not wanted. The account must differ.
  1358. if (serverUrl.isValid() && !differentPaths) {
  1359. QUrl folderUrl = f->accountState()->account()->url();
  1360. QString user = f->accountState()->account()->credentials()->user();
  1361. folderUrl.setUserName(user);
  1362. if (serverUrl == folderUrl) {
  1363. return tr("There is already a sync from the server to this local folder. "
  1364. "Please pick another local folder!");
  1365. }
  1366. }
  1367. }
  1368. return QString();
  1369. }
  1370. QString FolderMan::findGoodPathForNewSyncFolder(const QString &basePath, const QUrl &serverUrl) const
  1371. {
  1372. QString folder = basePath;
  1373. // If the parent folder is a sync folder or contained in one, we can't
  1374. // possibly find a valid sync folder inside it.
  1375. // Example: Someone syncs their home directory. Then ~/foobar is not
  1376. // going to be an acceptable sync folder path for any value of foobar.
  1377. QString parentFolder = QFileInfo(folder).dir().canonicalPath();
  1378. if (FolderMan::instance()->folderForPath(parentFolder)) {
  1379. // Any path with that parent is going to be unacceptable,
  1380. // so just keep it as-is.
  1381. return basePath;
  1382. }
  1383. int attempt = 1;
  1384. forever {
  1385. const bool isGood =
  1386. !QFileInfo(folder).exists()
  1387. && FolderMan::instance()->checkPathValidityForNewFolder(folder, serverUrl).isEmpty();
  1388. if (isGood) {
  1389. break;
  1390. }
  1391. // Count attempts and give up eventually
  1392. attempt++;
  1393. if (attempt > 100) {
  1394. return basePath;
  1395. }
  1396. folder = basePath + QString::number(attempt);
  1397. }
  1398. return folder;
  1399. }
  1400. bool FolderMan::ignoreHiddenFiles() const
  1401. {
  1402. if (_folderMap.empty()) {
  1403. // Currently no folders in the manager -> return default
  1404. return false;
  1405. }
  1406. // Since the hiddenFiles settings is the same for all folders, just return the settings of the first folder
  1407. return _folderMap.begin().value()->ignoreHiddenFiles();
  1408. }
  1409. void FolderMan::setIgnoreHiddenFiles(bool ignore)
  1410. {
  1411. // Note that the setting will revert to 'true' if all folders
  1412. // are deleted...
  1413. for (Folder *folder : qAsConst(_folderMap)) {
  1414. folder->setIgnoreHiddenFiles(ignore);
  1415. folder->saveToSettings();
  1416. }
  1417. }
  1418. QQueue<Folder *> FolderMan::scheduleQueue() const
  1419. {
  1420. return _scheduledFolders;
  1421. }
  1422. Folder *FolderMan::currentSyncFolder() const
  1423. {
  1424. return _currentSyncFolder;
  1425. }
  1426. void FolderMan::restartApplication()
  1427. {
  1428. if (Utility::isLinux()) {
  1429. // restart:
  1430. qCInfo(lcFolderMan) << "Restarting application NOW, PID" << qApp->applicationPid() << "is ending.";
  1431. qApp->quit();
  1432. QStringList args = qApp->arguments();
  1433. QString prg = args.takeFirst();
  1434. QProcess::startDetached(prg, args);
  1435. } else {
  1436. qCDebug(lcFolderMan) << "On this platform we do not restart.";
  1437. }
  1438. }
  1439. void FolderMan::slotSetupPushNotifications(const Folder::Map &folderMap)
  1440. {
  1441. for (auto folder : folderMap) {
  1442. const auto account = folder->accountState()->account();
  1443. // See if the account already provides the PushNotifications object and if yes connect to it.
  1444. // If we can't connect at this point, the signals will be connected in slotPushNotificationsReady()
  1445. // after the PushNotification object emitted the ready signal
  1446. slotConnectToPushNotifications(account.data());
  1447. connect(account.data(), &Account::pushNotificationsReady, this, &FolderMan::slotConnectToPushNotifications, Qt::UniqueConnection);
  1448. }
  1449. }
  1450. void FolderMan::slotProcessFilesPushNotification(Account *account)
  1451. {
  1452. qCInfo(lcFolderMan) << "Got files push notification for account" << account;
  1453. for (auto folder : _folderMap) {
  1454. // Just run on the folders that belong to this account
  1455. if (folder->accountState()->account() != account) {
  1456. continue;
  1457. }
  1458. qCInfo(lcFolderMan) << "Schedule folder" << folder << "for sync";
  1459. scheduleFolder(folder);
  1460. }
  1461. }
  1462. void FolderMan::slotConnectToPushNotifications(Account *account)
  1463. {
  1464. const auto pushNotifications = account->pushNotifications();
  1465. if (pushNotificationsFilesReady(account)) {
  1466. qCInfo(lcFolderMan) << "Push notifications ready";
  1467. connect(pushNotifications, &PushNotifications::filesChanged, this, &FolderMan::slotProcessFilesPushNotification, Qt::UniqueConnection);
  1468. }
  1469. }
  1470. } // namespace OCC