folder.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (C) by Duncan Mac-Vicar P. <duncan@kde.org>
  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 <QDebug>
  15. #include <QTimer>
  16. #include <QUrl>
  17. #include "mirall/folder.h"
  18. #include "mirall/folderwatcher.h"
  19. #include "mirall/mirallconfigfile.h"
  20. #include "mirall/syncresult.h"
  21. #define DEFAULT_POLL_INTERVAL_SEC 15000
  22. namespace Mirall {
  23. Folder::Folder(const QString &alias, const QString &path, const QString& secondPath, QObject *parent)
  24. : QObject(parent),
  25. _errorCount(0),
  26. _path(path),
  27. _secondPath(secondPath),
  28. _pollTimer(new QTimer(this)),
  29. _alias(alias),
  30. _onlyOnlineEnabled(false),
  31. _onlyThisLANEnabled(false),
  32. _online(false),
  33. _enabled(true)
  34. {
  35. qsrand(QTime::currentTime().msec());
  36. _pollTimer->setSingleShot(true);
  37. int polltime = DEFAULT_POLL_INTERVAL_SEC - 2000+ (int)( 4000.0*qrand()/(RAND_MAX+1.0));
  38. _pollTimer->setInterval( polltime );
  39. QObject::connect(_pollTimer, SIGNAL(timeout()), this, SLOT(slotPollTimerTimeout()));
  40. _pollTimer->start();
  41. #ifdef USE_WATCHER
  42. _watcher = new Mirall::FolderWatcher(path, this);
  43. MirallConfigFile cfg;
  44. _watcher->setIgnoreListFile( cfg.excludeFile() );
  45. QObject::connect(_watcher, SIGNAL(folderChanged(const QStringList &)),
  46. SLOT(slotChanged(const QStringList &)));
  47. #endif
  48. QObject::connect(this, SIGNAL(syncStarted()),
  49. SLOT(slotSyncStarted()));
  50. QObject::connect(this, SIGNAL(syncFinished(const SyncResult &)),
  51. SLOT(slotSyncFinished(const SyncResult &)));
  52. _online = _networkMgr.isOnline();
  53. QObject::connect(&_networkMgr, SIGNAL(onlineStateChanged(bool)), SLOT(slotOnlineChanged(bool)));
  54. _syncResult = SyncResult( SyncResult::NotYetStarted );
  55. }
  56. Folder::~Folder()
  57. {
  58. }
  59. QString Folder::alias() const
  60. {
  61. return _alias;
  62. }
  63. QString Folder::path() const
  64. {
  65. return _path;
  66. }
  67. QString Folder::secondPath() const
  68. {
  69. return _secondPath;
  70. }
  71. bool Folder::syncEnabled() const
  72. {
  73. return _enabled;
  74. }
  75. void Folder::setSyncEnabled( bool doit )
  76. {
  77. _enabled = doit;
  78. #ifdef USE_WATCHER
  79. _watcher->setEventsEnabled( doit );
  80. #endif
  81. if( doit && ! _pollTimer->isActive() ) {
  82. _pollTimer->start();
  83. }
  84. qDebug() << "setSyncEnabled - ############################ " << doit;
  85. if( doit ) {
  86. // undefined until next sync
  87. _syncResult.setStatus( SyncResult::NotYetStarted);
  88. evaluateSync( QStringList() );
  89. } else {
  90. // disabled.
  91. _syncResult.setStatus( SyncResult::Disabled );
  92. }
  93. }
  94. bool Folder::onlyOnlineEnabled() const
  95. {
  96. return _onlyOnlineEnabled;
  97. }
  98. void Folder::setOnlyOnlineEnabled(bool enabled)
  99. {
  100. _onlyOnlineEnabled = enabled;
  101. }
  102. bool Folder::onlyThisLANEnabled() const
  103. {
  104. return _onlyThisLANEnabled;
  105. }
  106. void Folder::setOnlyThisLANEnabled(bool enabled)
  107. {
  108. _onlyThisLANEnabled = enabled;
  109. }
  110. int Folder::pollInterval() const
  111. {
  112. return _pollTimer->interval();
  113. }
  114. void Folder::setPollInterval(int milliseconds)
  115. {
  116. _pollTimer->setInterval( milliseconds );
  117. }
  118. int Folder::errorCount()
  119. {
  120. return _errorCount;
  121. }
  122. void Folder::resetErrorCount()
  123. {
  124. _errorCount = 0;
  125. }
  126. void Folder::incrementErrorCount()
  127. {
  128. // if the error count gets higher than three, the interval timer
  129. // of the watcher is doubled.
  130. _errorCount++;
  131. if( _errorCount > 1 ) {
  132. #ifdef USE_WATCHER
  133. int interval = _watcher->eventInterval();
  134. int newInt = 2*interval;
  135. qDebug() << "Set new watcher interval to " << newInt;
  136. _watcher->setEventInterval( newInt );
  137. #endif
  138. _errorCount = 0;
  139. }
  140. }
  141. SyncResult Folder::syncResult() const
  142. {
  143. return _syncResult;
  144. }
  145. void Folder::evaluateSync(const QStringList &pathList)
  146. {
  147. if( !_enabled ) {
  148. qDebug() << "*" << alias() << "sync skipped, disabled!";
  149. return;
  150. }
  151. if (!_online && onlyOnlineEnabled()) {
  152. qDebug() << "*" << alias() << "sync skipped, not online";
  153. return;
  154. }
  155. // stop the poll timer here. Its started again in the slot of
  156. // sync finished.
  157. qDebug() << "* " << alias() << "Poll timer disabled";
  158. _pollTimer->stop();
  159. _syncResult.setStatus( SyncResult::NotYetStarted );
  160. emit scheduleToSync( alias() );
  161. }
  162. void Folder::startSync( const QStringList &pathList )
  163. {
  164. _syncResult = SyncResult( SyncResult::SyncRunning );
  165. emit syncStateChange();
  166. }
  167. void Folder::slotPollTimerTimeout()
  168. {
  169. qDebug() << "* Polling" << alias() << "for changes. Ignoring all pending events until now";
  170. #ifdef USE_WATCHER
  171. _watcher->clearPendingEvents();
  172. #endif
  173. evaluateSync(QStringList());
  174. }
  175. void Folder::slotOnlineChanged(bool online)
  176. {
  177. qDebug() << "* " << alias() << "is" << (online ? "now online" : "no longer online");
  178. _online = online;
  179. }
  180. void Folder::slotChanged(const QStringList &pathList)
  181. {
  182. qDebug() << "** Changed was notified on " << pathList;
  183. evaluateSync(pathList);
  184. }
  185. void Folder::slotSyncStarted()
  186. {
  187. // disable events until syncing is done
  188. #ifdef USE_WATCHER
  189. _watcher->setEventsEnabled(false);
  190. #endif
  191. }
  192. void Folder::slotSyncFinished(const SyncResult &result)
  193. {
  194. #ifdef USE_WATCHER
  195. _watcher->setEventsEnabled(true);
  196. #endif
  197. _syncResult = result;
  198. emit syncStateChange();
  199. // reenable the poll timer if folder is sync enabled
  200. if( syncEnabled() ) {
  201. qDebug() << "* " << alias() << "Poll timer enabled with " << _pollTimer->interval() << "milliseconds";
  202. _pollTimer->start();
  203. } else {
  204. qDebug() << "* Not enabling poll timer for " << alias();
  205. _pollTimer->stop();
  206. }
  207. }
  208. void Folder::setBackend( const QString& b )
  209. {
  210. _backend = b;
  211. }
  212. QString Folder::backend() const
  213. {
  214. return _backend;
  215. }
  216. } // namespace Mirall