aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/calendarsync.cpp71
-rw-r--r--src/calendarsync.h54
-rw-r--r--tests/CMakeLists.txt1
-rw-r--r--tests/test_calendarsync.cpp94
5 files changed, 221 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;
+};
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 6be4a22..6721340 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -75,6 +75,7 @@ target_compile_definitions(test_contactstore PRIVATE
add_qtmaildir_test(repeatrule)
add_qtmaildir_test(calendarstore)
add_qtmaildir_test(calendarwriter)
+add_qtmaildir_test(calendarsync)
add_qtmaildir_test(signatures)
add_qtmaildir_test(busyindicator)
add_qtmaildir_test(tagstrip)
diff --git a/tests/test_calendarsync.cpp b/tests/test_calendarsync.cpp
new file mode 100644
index 0000000..e5cdee5
--- /dev/null
+++ b/tests/test_calendarsync.cpp
@@ -0,0 +1,94 @@
+/*
+ * 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 <QtTest>
+#include <QFile>
+#include <QSignalSpy>
+#include <QTemporaryDir>
+
+#include "calendarsync.h"
+
+namespace {
+
+/// A script that appends a line to `log` each run and exits with `code`.
+QString script(const QTemporaryDir &dir, int code)
+{
+ const QString path = dir.filePath(QStringLiteral("sync.sh"));
+ QFile f(path);
+ f.open(QIODevice::WriteOnly);
+ f.write(QStringLiteral("#!/bin/sh\necho run >> %1\necho output\nexit %2\n")
+ .arg(dir.filePath(QStringLiteral("log"))).arg(code).toUtf8());
+ f.close();
+ f.setPermissions(f.permissions() | QFileDevice::ExeOwner);
+ return path;
+}
+
+int runs(const QTemporaryDir &dir)
+{
+ QFile f(dir.filePath(QStringLiteral("log")));
+ return f.open(QIODevice::ReadOnly) ? f.readAll().count('\n') : 0;
+}
+
+} // namespace
+
+class TestCalendarSync : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void aBurstOfWritesRunsOnce();
+ void reportsFailureWithTheOutput();
+ void anEmptyCommandNeverRuns();
+};
+
+void TestCalendarSync::aBurstOfWritesRunsOnce()
+{
+ QTemporaryDir dir;
+ CalendarSync sync(script(dir, 0), 50);
+ QSignalSpy finished(&sync, &CalendarSync::finished);
+ sync.schedule();
+ sync.schedule();
+ sync.schedule();
+ QVERIFY(finished.wait(5000));
+ QCOMPARE(finished.first().at(0).toBool(), true);
+ QTest::qWait(200); // nothing else is queued, so nothing else may run
+ QCOMPARE(runs(dir), 1);
+}
+
+void TestCalendarSync::reportsFailureWithTheOutput()
+{
+ QTemporaryDir dir;
+ CalendarSync sync(script(dir, 3), 0);
+ QSignalSpy finished(&sync, &CalendarSync::finished);
+ sync.schedule();
+ QVERIFY(finished.wait(5000));
+ QCOMPARE(finished.first().at(0).toBool(), false);
+ QVERIFY(finished.first().at(1).toString().contains(QStringLiteral("output")));
+}
+
+void TestCalendarSync::anEmptyCommandNeverRuns()
+{
+ CalendarSync sync(QString(), 0);
+ QSignalSpy started(&sync, &CalendarSync::started);
+ sync.schedule();
+ QTest::qWait(100);
+ QCOMPARE(started.count(), 0);
+}
+
+QTEST_MAIN(TestCalendarSync)
+#include "test_calendarsync.moc"