CDirectoryMonitorComp.h
Go to the documentation of this file.
1 #ifndef ihotfgui_CDirectoryMonitorComp_included
2 #define ihotfgui_CDirectoryMonitorComp_included
3 
4 
5 // Qt includes
6 #include <QtCore/QFileSystemWatcher>
7 #include <QtCore/QThread>
8 #include <QtCore/QDir>
9 #include <QtCore/QSet>
10 #include <QtCore/QTextStream>
11 #include <QtCore/QMutex>
12 
13 // ACF includes
15 #include <icomp/CComponentBase.h>
16 #include <ilog/TLoggerCompWrap.h>
17 #include <iprm/IParamsSet.h>
18 #include <ifile/IFileNameParam.h>
19 
20 // AcfSln includes
25 
26 
27 namespace ihotfgui
28 {
29 
30 
35  public QThread,
37  virtual public ihotf::IDirectoryMonitor
38 {
39  Q_OBJECT
40 
41 public:
43  typedef QThread BaseClass2;
44 
46  {
51  };
52 
53  I_BEGIN_COMPONENT(CDirectoryMonitorComp);
54  I_REGISTER_INTERFACE(ihotf::IDirectoryMonitor);
55  I_ASSIGN(m_paramsSetCompPtr, "ParamsSet", "Default parameter set for the directory monitoring", false, "ParamsSet");
56  I_ASSIGN_TO(m_paramsSetModelCompPtr, m_paramsSetCompPtr, false);
57  I_ASSIGN(m_monitoringSessionManagerCompPtr, "SessionManager", "Provider of previous monitoring sessions", false, "SessionManager");
58  I_ASSIGN(m_fileSystemChangeStorageCompPtr, "FileSystemChangeStorage", "File storage", true, "FileSystemChangeStorage");
59  I_ASSIGN(m_traceFileFolderCompPtr, "TraceFileFolder", "Location for the trace file", false, "TraceFileFolder");
60  I_ASSIGN(m_directoryPathIdAttrPtr, "DirParamId", "Parameter ID of the path to be observed in the parameter set", true, "DirParamId");
61  I_ASSIGN(m_directoryMonitorParamsIdAttrPtr, "DirectoryMonitorParamsId", "ID of the directory observing parameters in the parameter set", true, "DirectoryMonitorParamsId");
62  I_ASSIGN(m_enableTraceParamIdAttrPtr, "EnableTraceParamId", "ID of the trace enable parameter in the parameter set", true, "EnableTrace");
63  I_ASSIGN(m_autoStartAttrPtr, "AutoStart", "If enabled, start the directory monitoring after initialization", false, false);
64  I_END_COMPONENT;
65 
67 
68  // reimplemented (ihotf::IDirectoryMonitor)
69  virtual bool IsRunning() const;
70  virtual bool StartObserving(const iprm::IParamsSet* paramsSetPtr = NULL);
71  virtual void StopObserving();
72 
73 protected:
74  // reimplemented (icomp::CComponentBase)
75  virtual void OnComponentCreated();
76  virtual void OnComponentDestroyed();
77 
78  // reimplemented (QThread)
79  virtual void run();
80 
81 private Q_SLOTS:
85  void OnFolderChanged(const istd::IChangeable::ChangeSet& changeSet);
86 
90  void OnDirectoryChangeNotification(const QString& directory);
91 
92 Q_SIGNALS:
96  void FolderChanged(const istd::IChangeable::ChangeSet& changeSet);
97 
98 private:
99  struct FileAccessInfo
100  {
101  FileAccessInfo()
102  :fileSize(0)
103  {
104  }
105 
106  QDateTime checkTimeStamp;
107  QDateTime lastAccessTimeStamp;
108  qint64 fileSize;
109  };
110 
111  typedef QMap<QString, FileAccessInfo> FileAccessMap;
112 
113 
114  void SetFolderPath(const QString& folderPath);
115  void StartObserverThread();
116  void StopObserverThread();
117  void ResetFiles();
118  bool ConnectToParameterModel(const iprm::IParamsSet& paramsSet);
119  void DisconnectFromParameterModel();
120  void UpdateMonitoringSession() const;
121  bool HasFileAccess(const QString& filePath, FileAccessInfo& lastFileAccessInfo) const;
122  bool CheckFileAccess(const QString& filePath) const;
123  QDateTime GetLastAccessTime(const QFileInfo& fileInfo) const;
124  void UpdateNonAccessedFiles(FileAccessMap& accessMap, QStringList& fileList);
125  void EnableTracing(bool tracingEnabled);
126  void WriteTraceMessage(const QString& traceMessage) const;
127 
128 private:
129  struct FileSystemChanges
130  {
131  QStringList addedFiles;
132  QStringList removedFiles;
133  QStringList modifiedFiles;
134  QStringList attributeChangedFiles;
135  };
136 
140  class MonitoringParamsObserver: public imod::TSingleModelObserverBase<ihotf::IDirectoryMonitorParams>
141  {
142  public:
144 
145  MonitoringParamsObserver(CDirectoryMonitorComp& parent);
146 
147  // reimplemented (imod::IObserver)
148  virtual void AfterUpdate(imod::IModel* modelPtr, const istd::IChangeable::ChangeSet& changeSet);
149 
150  private:
151  CDirectoryMonitorComp& m_parent;
152  };
153 
157  class DirectoryParamsObserver: public imod::TSingleModelObserverBase<ifile::IFileNameParam>
158  {
159  public:
161 
162  DirectoryParamsObserver(CDirectoryMonitorComp& parent);
163 
164  // reimplemented (imod::IObserver)
165  virtual void AfterUpdate(imod::IModel* modelPtr, const istd::IChangeable::ChangeSet& changeSet);
166 
167  private:
168  CDirectoryMonitorComp& m_parent;
169  };
170 
171  ihotf::IMonitoringSession::FileItems m_directoryFiles;
172  FileAccessMap m_nonAccessedAddedFiles;
173  FileAccessMap m_nonAccessedModifiedFiles;
174 
175  bool m_finishThread;
176 
177  FileSystemChanges m_folderChanges;
178 
179  QDir m_currentDirectory;
180 
181  int m_directoryPendingChangesCounter;
182 
183  QFileSystemWatcher m_directoryWatcher;
184 
185  // Model shadows:
186  double m_poolingFrequency;
187  QStringList m_fileFilterExpressions;
188  int m_observingItemTypes;
189  int m_observingChanges;
190  int m_lastModificationMinDifference;
191  int m_folderDepth;
192  int m_timestampMode;
193 
194  MonitoringParamsObserver m_monitoringParamsObserver;
195  DirectoryParamsObserver m_directoryParamsObserver;
196 
197  // Component attributes
198  I_REF(iprm::IParamsSet, m_paramsSetCompPtr);
199  I_REF(imod::IModel, m_paramsSetModelCompPtr);
200  I_REF(ihotf::IMonitoringSessionManager, m_monitoringSessionManagerCompPtr);
201  I_REF(ihotf::IFileSystemChangeStorage, m_fileSystemChangeStorageCompPtr);
202  I_REF(ifile::IFileNameParam, m_traceFileFolderCompPtr);
203  I_ATTR(QByteArray, m_directoryPathIdAttrPtr);
204  I_ATTR(QByteArray, m_directoryMonitorParamsIdAttrPtr);
205  I_ATTR(bool, m_autoStartAttrPtr);
206  I_ATTR(QByteArray, m_enableTraceParamIdAttrPtr);
207 
208  bool m_lockChanges;
209 
210  int m_processingFlags;
211 
212  mutable QMutex m_mutex;
213 
214  bool m_isTraceEnabled;
215  mutable QMutex m_traceFileMutex;
216  istd::TDelPtr<QFile> m_traceFilePtr;
217  mutable QTextStream m_traceStream;
218 };
219 
220 
221 } // namespace ihotfgui
222 
223 
224 #endif // !ihotfgui_CDirectoryMonitorComp_included
225 
226 
void FolderChanged(const istd::IChangeable::ChangeSet &changeSet)
Signal is emitted from observing thread to notifiy the main thread about changes in a directory...
virtual bool IsRunning() const
Checks if the folder monitoring is active.
Common interface for a monitoring session's manager.
Component for monitoring of the file folder.
virtual void StopObserving()
Stop observing process.
QMap< QString, QDateTime > FileItems
Map absolute file path to modification time.
For the case that, the number of file descriptors was running out the manual folder observation will ...
virtual bool StartObserving(const iprm::IParamsSet *paramsSetPtr=NULL)
Start observing process.
Common interface for a storage of files.
ilog::CLoggerComponentBase BaseClass
Interface for a folder monitor.

© 2007-2017 Witold Gantzke and Kirill Lepskiy