v2.0.0
Loading...
Searching...
No Matches
fiff_file_sharer.cpp
Go to the documentation of this file.
1//=============================================================================================================
15
16//=============================================================================================================
17// INCLUDES
18//=============================================================================================================
19
20#include "fiff_file_sharer.h"
21
22//=============================================================================================================
23// QT INCLUDES
24//=============================================================================================================
25
26#include <QDir>
27#include <QDebug>
28
29//=============================================================================================================
30// USED NAMESPACES
31//=============================================================================================================
32
33using namespace FIFFLIB;
34
35//=============================================================================================================
36// DEFINE STATIC MEMBERS
37//=============================================================================================================
38
39const static char m_sDefaultDirectory[]("realtime_shared_files");
40const static char m_sDefaultFileName[]("realtime_file");
41
42//=============================================================================================================
43// DEFINE MEMBER METHODS
44//=============================================================================================================
45
47: FiffFileSharer(m_sDefaultDirectory)
48{
49}
50
51//=============================================================================================================
52
53FiffFileSharer::FiffFileSharer(const QString& sDirName)
54: m_sDirectory(QDir::currentPath() + "/" + sDirName)
55, m_iFileIndex(0)
56{
57}
58
59//=============================================================================================================
60
61void FiffFileSharer::copyRealtimeFile(const QString& sSourcePath)
62{
63 if (!initSharedDirectory())
64 return;
65
66 // Complete the copy under a temporary name and rename it, so the watcher never sees a half-written file.
67 const QString sFilePath(m_sDirectory + "/" + m_sDefaultFileName + QString::number(m_iFileIndex++) + "_raw.fif");
68 const QString sPartPath(sFilePath + ".part");
69 QFile::remove(sPartPath);
70 if (!QFile::copy(sSourcePath, sPartPath))
71 return;
72
73 QFile newFile(sPartPath);
74 if (newFile.open(QIODevice::ReadWrite)) {
75 FIFFLIB::FiffStream stream(&newFile);
76 stream.skipRawData(newFile.bytesAvailable());
77 stream.finish_writing_raw();
78 newFile.close();
79 }
80 QFile::remove(sFilePath);
81 QFile::rename(sPartPath, sFilePath);
82}
83
84//=============================================================================================================
85
87{
88 if (initSharedDirectory()) {
89 clearSharedDirectory();
90 m_fileWatcher.addPath(m_sDirectory);
91 connect(&m_fileWatcher, &QFileSystemWatcher::directoryChanged,
92 this, &FiffFileSharer::onDirectoryChanged, Qt::UniqueConnection);
93 } else {
94 qWarning() << "[FiffFileSharer::initWatcher] Unable to initilaize shared directory";
95 }
96}
97
98//=============================================================================================================
99
100void FiffFileSharer::clearSharedDirectory()
101{
102 QDir directory(m_sDirectory);
103 directory.setNameFilters(QStringList("*.*"));
104 directory.setFilter(QDir::Files);
105 for (auto& file : directory.entryList()) {
106 directory.remove(file);
107 }
108}
109
110//=============================================================================================================
111
112void FiffFileSharer::onDirectoryChanged(const QString& sPath)
113{
114 // Files appear complete (renamed into place), so every new one can be reported as soon as it is listed.
115 QString filePath(sPath + "/" + m_sDefaultFileName + QString::number(m_iFileIndex) + "_raw.fif");
116 while (QFile::exists(filePath)) {
117 emit newFileAtPath(filePath);
118 filePath = sPath + "/" + m_sDefaultFileName + QString::number(++m_iFileIndex) + "_raw.fif";
119 }
120}
121
122//=============================================================================================================
123
124bool FiffFileSharer::initSharedDirectory()
125{
126 QDir sharedDirectory(m_sDirectory);
127 if (!sharedDirectory.exists()) {
128 sharedDirectory.mkpath(".");
129 }
130
131 return sharedDirectory.exists();
132}
Hands finished FIFF recordings from one application to another through a watched directory.
FIFF file I/O, in-memory data structures and high-level readers/writers.
void newFileAtPath(const QString &sPath)
void copyRealtimeFile(const QString &sSourcePath)
FIFF tag-stream reader/writer: wraps a QIODevice and exposes typed read_* / write_* methods for every...