refactor: new FileTail::setWatchedFile
This commit is contained in:
parent
2da14c3da3
commit
5e61a2b5ce
2 changed files with 23 additions and 6 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Deskflow -- mouse and keyboard sharing utility
|
* Deskflow -- mouse and keyboard sharing utility
|
||||||
|
* SPDX-FileCopyrightText: (C) 2026 Deskflow Developers
|
||||||
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
|
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
|
||||||
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
||||||
*/
|
*/
|
||||||
|
|
@ -14,11 +15,27 @@
|
||||||
|
|
||||||
namespace deskflow::gui {
|
namespace deskflow::gui {
|
||||||
|
|
||||||
FileTail::FileTail(const QString &filePath, QObject *parent)
|
FileTail::FileTail(const QString &filePath, QObject *parent) : QObject(parent), m_watcher(new QFileSystemWatcher(this))
|
||||||
: QObject(parent),
|
|
||||||
m_file(filePath),
|
|
||||||
m_watcher(new QFileSystemWatcher(this))
|
|
||||||
{
|
{
|
||||||
|
setWatchedFile(filePath);
|
||||||
|
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &FileTail::handleFileChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileTail::setWatchedFile(const QString &filePath)
|
||||||
|
{
|
||||||
|
if (filePath.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (m_file.fileName() == filePath)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!m_file.fileName().isEmpty()) {
|
||||||
|
m_watcher->removePath(m_file.fileName());
|
||||||
|
m_file.close();
|
||||||
|
m_file.setFileName(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_file.setFileName(filePath);
|
||||||
if (!m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (!m_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
qCritical() << "failed to open file for tail:" << filePath;
|
qCritical() << "failed to open file for tail:" << filePath;
|
||||||
return;
|
return;
|
||||||
|
|
@ -27,8 +44,6 @@ FileTail::FileTail(const QString &filePath, QObject *parent)
|
||||||
qDebug() << "starting file tail:" << filePath;
|
qDebug() << "starting file tail:" << filePath;
|
||||||
m_watcher->addPath(filePath);
|
m_watcher->addPath(filePath);
|
||||||
m_lastPos = m_file.size();
|
m_lastPos = m_file.size();
|
||||||
|
|
||||||
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &FileTail::handleFileChanged);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileTail::handleFileChanged(const QString &)
|
void FileTail::handleFileChanged(const QString &)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Deskflow -- mouse and keyboard sharing utility
|
* Deskflow -- mouse and keyboard sharing utility
|
||||||
|
* SPDX-FileCopyrightText: (C) 2026 Deskflow Developers
|
||||||
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
|
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
|
||||||
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
||||||
*/
|
*/
|
||||||
|
|
@ -19,6 +20,7 @@ class FileTail : public QObject
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit FileTail(const QString &filePath, QObject *parent = nullptr);
|
explicit FileTail(const QString &filePath, QObject *parent = nullptr);
|
||||||
|
void setWatchedFile(const QString &filePath);
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void newLine(const QString &line);
|
void newLine(const QString &line);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue