CAutoPersistenceComp.h
Go to the documentation of this file.
1 #ifndef ifile_CAutoPersistenceComp_included
2 #define ifile_CAutoPersistenceComp_included
3 
4 
5 // Qt includes
6 #include <QtCore/QObject>
7 #include <QtCore/QTimer>
8 #include <QtCore/QFuture>
9 #include <QtCore/QFileSystemWatcher>
10 
11 // ACF includes
12 #include <istd/IChangeable.h>
13 #include <imod/IModel.h>
15 #include <ilog/TLoggerCompWrap.h>
17 #include <ifile/IFilePersistence.h>
18 #include <ifile/IFileNameParam.h>
20 
21 #if QT_VERSION >= 0x050000
22 #include <QtCore/QLockFile>
23 #endif
24 
25 namespace ifile
26 {
27 
28 
45  public QObject,
48 {
49  Q_OBJECT
50 
51 public:
54 
55  enum ModelIds
56  {
61 
66 
71  };
72 
73  I_BEGIN_COMPONENT(CAutoPersistenceComp);
74  I_ASSIGN(m_objectCompPtr, "Object", "Object will be restored and stored", true, "Object");
75  I_ASSIGN_TO(m_objectModelCompPtr, m_objectCompPtr, false);
76  I_ASSIGN_TO(m_serializeableObjectCompPtr, m_objectCompPtr, false);
77  I_ASSIGN(m_fileLoaderCompPtr, "FileLoader", "File loader used to store and restore object", true, "FileLoader");
78  I_ASSIGN(m_filePathCompPtr, "FilePath", "File path where object data will be stored if non anonymous loader is used", false, "FilePath");
79  I_ASSIGN_TO(m_filePathModelCompPtr, m_filePathCompPtr, false);
80  I_ASSIGN(m_restoreOnBeginAttrPtr, "RestoreOnBegin", "Flag indicating that object should be restored on begin", true, true);
81  I_ASSIGN(m_storeOnBeginAttrPtr, "StoreOnBegin", "Flag indicating that object should be stored on begin if the storage file doesn't exist", true, false);
82  I_ASSIGN(m_storeOnEndAttrPtr, "StoreOnEnd", "Flag indicating that object should be stored on end", true, true);
83  I_ASSIGN(m_storeOnChangeAttrPtr, "StoreOnChange", "Flag indicating that object should be stored on each data change", true, false);
84  I_ASSIGN(m_storeIntervalAttrPtr, "StoreInterval", "Time interval in seconds for automatic object storing", false, 10);
85  I_ASSIGN(m_reloadOnFileChangeAttrPtr, "AutoReload", "Update data model if the file was changed", true, false);
86  I_ASSIGN(m_runtimeStatusCompPtr, "RuntimeStatus", "Application's runtime status", false, "RuntimeStatus");
87  I_ASSIGN_TO(m_runtimeStatusModelCompPtr, m_runtimeStatusCompPtr, false);
88  I_ASSIGN(m_staleLockTimeAttrPtr, "StaleLockTime", "Time in seconds after which a lock file is considered stale.", true, 30.0);
89  I_ASSIGN(m_tryLockTimeoutAttrPtr, "TryLockTimeout", "Lock function will wait for at most TryLockTimeout seconds for the lock file to become available."
90  "\nNOTE: Lock will wait forever until the lock file can be locked when set to negative", true, 0.0);
91  I_ASSIGN(m_enableLockForLoadAttrPtr, "EnableLockForRead", "When enabled lock is also set when reading from file."
92  "\nNOTE: On NTFS file systems, ownership and permissions checking is disabled (in QT) by default for performance reasons."
93  "\nEnable this flag when you are known what you are doing.", true, false);
94  I_END_COMPONENT;
95 
100 
101 protected:
105  void SaveObjectSnapshot();
106 
110  virtual bool LoadObject(const QString& filePath);
114  virtual bool StoreObject(const istd::IChangeable& object);
115 
116  // reimplemented (icomp::CComponentBase)
117  virtual void OnComponentCreated();
118  virtual void OnComponentDestroyed();
119 
120  // reimplemented (imod::CMultiModelDispatcherBase)
121  virtual void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet& changeSet);
122 
123 private Q_SLOTS:
127  void OnTimeout();
128 
129  void OnFileContentsChanged(const QString& path);
130 
131 private:
135  bool TryStartIntervalStore();
136 
137  bool LockFile(const QString& filePath, bool store) const;
138  void UnlockFile() const;
139 
140 private:
141  I_REF(ibase::IRuntimeStatusProvider, m_runtimeStatusCompPtr);
142  I_REF(imod::IModel, m_runtimeStatusModelCompPtr);
143 
147  I_REF(istd::IChangeable, m_objectCompPtr);
148  I_REF(iser::ISerializable, m_serializeableObjectCompPtr);
149  I_REF(imod::IModel, m_objectModelCompPtr);
150 
154  I_REF(ifile::IFilePersistence, m_fileLoaderCompPtr);
155 
159  I_REF(ifile::IFileNameParam, m_filePathCompPtr);
160  I_REF(imod::IModel, m_filePathModelCompPtr);
161 
165  I_ATTR(bool, m_restoreOnBeginAttrPtr);
166 
171  I_ATTR(bool, m_storeOnBeginAttrPtr);
172 
176  I_ATTR(bool, m_storeOnEndAttrPtr);
177 
181  I_ATTR(bool, m_storeOnChangeAttrPtr);
182 
187  I_ATTR(double, m_storeIntervalAttrPtr);
188 
192  I_ATTR(bool, m_reloadOnFileChangeAttrPtr);
193 
199  I_ATTR(double, m_staleLockTimeAttrPtr);
200 
206  I_ATTR(double, m_tryLockTimeoutAttrPtr);
207 
213  I_ATTR(bool, m_enableLockForLoadAttrPtr);
214 
215  iser::CMemoryWriteArchive m_lastStoredObjectState;
216 
220  bool m_isObjectChanged;
221 
225  bool m_isLoadedFromFile;
226 
227  QTimer m_storingTimer;
228 
229  QFuture<void> m_storingFuture;
230 
234  mutable istd::TDelPtr<istd::IChangeable> m_objectShadowPtr;
235 
236  QFileSystemWatcher m_fileWatcher;
237 
238  QMutex m_loadSaveMutex;
239 
240  mutable bool m_blockLoadingOnFileChanges;
241 
242 #if QT_VERSION >= 0x050000
243  mutable istd::TDelPtr<QLockFile> m_lockFilePtr;
244 #endif
245 };
246 
247 
248 } // namespace ifile
249 
250 
251 #endif // !ifile_CAutoPersistenceComp_included
252 
253 
virtual void OnModelChanged(int modelId, const istd::IChangeable::ChangeSet &changeSet)
virtual void OnComponentCreated()
Generic implementation of a data model changes notifier.
Provider of the run-time status.
Common class for all classes which objects can be archived or restored from archive.
Definition: ISerializable.h:23
virtual void OnComponentDestroyed()
imod::CMultiModelDispatcherBase BaseClass2
Wrapper provider of log-functionality for component based implementations.
Common interface for any file system item name (e.g directory, file or URL path)
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
virtual bool LoadObject(const QString &filePath)
Load working object data from some file.
CAutoPersistenceComp()
Default constructor.
Common interface for model objects, that supports Model/Observer design pattern.
Definition: IModel.h:23
Set of change flags (its IDs).
Definition: IChangeable.h:30
The component for automatic object persistence.
Interface providing loading and saving of data objects.
ilog::CLoggerComponentBase BaseClass
Implementation of archive using memory buffer to store the persistent objects.
void SaveObjectSnapshot()
Store object copy in a separate thread.
virtual bool StoreObject(const istd::IChangeable &object)
Store some object data to working file.

© 2007-2017 Witold Gantzke and Kirill Lepskiy