diff options
| author | Danilo M. <danix@danix.xyz> | 2026-09-24 16:43:00 +0200 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-09-24 16:43:00 +0200 |
| commit | 4ded387d3bb926982ea00f40ab01ece7ffdeaf94 (patch) | |
| tree | 2c760df5da32851408ef7e6291aa7c16680ef12e /src/calendarsync.cpp | |
| parent | 55eb08f9e03cc56c4d0e26a6c88e6ecc7222e1f3 (diff) | |
| download | qtmaildir-4ded387d3bb926982ea00f40ab01ece7ffdeaf94.tar.gz qtmaildir-4ded387d3bb926982ea00f40ab01ece7ffdeaf94.zip | |
feat: debounce the calendar sync command after writes
Diffstat (limited to 'src/calendarsync.cpp')
| -rw-r--r-- | src/calendarsync.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/calendarsync.cpp b/src/calendarsync.cpp new file mode 100644 index 0000000..2fc555f --- /dev/null +++ b/src/calendarsync.cpp @@ -0,0 +1,71 @@ +/* + * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs + * Copyright (C) 2026 Danilo M. <danix@danix.xyz> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "calendarsync.h" + +CalendarSync::CalendarSync(const QString &command, int delayMs, QObject *parent) + : QObject(parent), m_command(command) +{ + m_timer.setSingleShot(true); + m_timer.setInterval(delayMs); + connect(&m_timer, &QTimer::timeout, this, &CalendarSync::run); + m_process.setProcessChannelMode(QProcess::MergedChannels); + connect(&m_process, &QProcess::readyRead, this, + [this]() { m_output += m_process.readAll(); }); + connect(&m_process, &QProcess::finished, this, + [this](int code, QProcess::ExitStatus status) { + emit finished(status == QProcess::NormalExit && code == 0, + QString::fromLocal8Bit(m_output)); + if (m_again) { + m_again = false; + m_timer.start(); + } + }); + connect(&m_process, &QProcess::errorOccurred, this, + [this](QProcess::ProcessError error) { + if (error == QProcess::FailedToStart) + emit finished(false, m_process.errorString()); + }); +} + +void CalendarSync::schedule() +{ + if (m_command.isEmpty()) + return; + if (isRunning()) + m_again = true; + else + m_timer.start(); // restarting is the debounce +} + +bool CalendarSync::isRunning() const +{ + return m_process.state() != QProcess::NotRunning; +} + +void CalendarSync::run() +{ + // splitCommand, not a shell: quoting works and nothing is interpreted. + QStringList parts = QProcess::splitCommand(m_command); + if (parts.isEmpty()) + return; + const QString program = parts.takeFirst(); + m_output.clear(); + emit started(); + m_process.start(program, parts); +} |
