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 | |
| parent | 55eb08f9e03cc56c4d0e26a6c88e6ecc7222e1f3 (diff) | |
| download | qtmaildir-4ded387d3bb926982ea00f40ab01ece7ffdeaf94.tar.gz qtmaildir-4ded387d3bb926982ea00f40ab01ece7ffdeaf94.zip | |
feat: debounce the calendar sync command after writes
Diffstat (limited to 'src')
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/calendarsync.cpp | 71 | ||||
| -rw-r--r-- | src/calendarsync.h | 54 |
3 files changed, 126 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e70039e..8f32fcc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -47,6 +47,7 @@ add_library(qtmaildir_lib STATIC repeatrule.cpp calendarstore.cpp calendarwriter.cpp + calendarsync.cpp ) target_include_directories(qtmaildir_lib 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); +} diff --git a/src/calendarsync.h b/src/calendarsync.h new file mode 100644 index 0000000..c406f79 --- /dev/null +++ b/src/calendarsync.h @@ -0,0 +1,54 @@ +/* + * 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. + */ + +#pragma once + +#include <QObject> +#include <QProcess> +#include <QTimer> + +/// Runs calendar_sync_command a short delay after the last write. +/// +/// The calendar's counterpart to MailSync, and deliberately smaller: no lock +/// monitoring and no status file, because the command itself takes the +/// flock that cron shares. A write arriving while a run is in progress is not +/// dropped: it queues exactly one more run for when this one finishes, since +/// the running sync may have listed the vdir before the write landed. +class CalendarSync : public QObject +{ + Q_OBJECT + +public: + CalendarSync(const QString &command, int delayMs, QObject *parent = nullptr); + + void schedule(); + bool isRunning() const; + +signals: + void started(); + void finished(bool ok, const QString &output); + +private: + void run(); + + QString m_command; + QTimer m_timer; + QProcess m_process; + QByteArray m_output; + bool m_again = false; +}; |
