refactor: new FileTail::setWatchedFile

This commit is contained in:
sithlord48
2026-01-09 16:57:34 -05:00
committed by Nick Bolton
parent 2da14c3da3
commit 5e61a2b5ce
2 changed files with 23 additions and 6 deletions

View File

@ -1,5 +1,6 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2026 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
@ -14,11 +15,27 @@
namespace deskflow::gui {
FileTail::FileTail(const QString &filePath, QObject *parent)
: QObject(parent),
m_file(filePath),
m_watcher(new QFileSystemWatcher(this))
FileTail::FileTail(const QString &filePath, QObject *parent) : QObject(parent), 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)) {
qCritical() << "failed to open file for tail:" << filePath;
return;
@ -27,8 +44,6 @@ FileTail::FileTail(const QString &filePath, QObject *parent)
qDebug() << "starting file tail:" << filePath;
m_watcher->addPath(filePath);
m_lastPos = m_file.size();
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &FileTail::handleFileChanged);
}
void FileTail::handleFileChanged(const QString &)

View File

@ -1,5 +1,6 @@
/*
* Deskflow -- mouse and keyboard sharing utility
* SPDX-FileCopyrightText: (C) 2026 Deskflow Developers
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
*/
@ -19,6 +20,7 @@ class FileTail : public QObject
public:
explicit FileTail(const QString &filePath, QObject *parent = nullptr);
void setWatchedFile(const QString &filePath);
Q_SIGNALS:
void newLine(const QString &line);