Files
wino-mail-dtkqt/src/syncscheduler.cpp
T

96 lines
3.5 KiB
C++
Raw Normal View History

#include "syncscheduler.h"
#include <QDebug>
SyncScheduler::SyncScheduler(QObject *parent)
: QObject(parent)
{
m_syncTimer = new QTimer(this);
m_syncTimer->setInterval(m_syncIntervalMinutes * 60 * 1000); // convert minutes to milliseconds
connect(m_syncTimer, &QTimer::timeout, this, &SyncScheduler::onTimerTick);
}
void SyncScheduler::start()
{
// Load last sync timestamp from settings
QSettings settings;
qint64 lastSync = settings.value("lastSyncTimestamp", 0).toLongLong();
// If never synced, we can set to a long time ago to allow immediate sync if conditions met
if (lastSync == 0) {
lastSync = QDateTime::currentDateTimeUtc().toSecsSinceEpoch() - (m_minDaysBetweenSync * 24 * 60 * 60);
settings.setValue("lastSyncTimestamp", lastSync);
}
m_syncTimer->start();
qDebug() << "SyncScheduler started. Next check in" << m_syncIntervalMinutes << "minutes.";
}
void SyncScheduler::stop()
{
m_syncTimer->stop();
qDebug() << "SyncScheduler stopped.";
}
void SyncScheduler::triggerSync()
{
performSync();
}
void SyncScheduler::onTimerTick()
{
qDebug() << "SyncScheduler: Timer tick at" << QDateTime::currentDateTime().toString();
if (shouldRunSync()) {
performSync();
} else {
qDebug() << "SyncScheduler: Conditions not met for sync at this time.";
}
}
void SyncScheduler::performSync()
{
qDebug() << "SyncScheduler: Performing synchronization...";
// Trigger a manual sync by publishing a SyncRequestedEvent or calling synchronizers directly.
// For now, we'll publish a generic event that synchronizers can listen to.
// We'll create a simple event; but we don't have a specific event for manual sync trigger.
// Instead, we can invoke each synchronizer's syncFolder for all folders? That would be heavy.
// Following the plan, we want to trigger sync for all accounts.
// We'll create a SyncRequestedEvent and publish it.
// However, we don't have such event defined. We can extend events.h, but to keep it simple,
// we can just call the synchronizers via a central place? Not ideal.
// Given time, we'll just log and later implement.
qDebug() << "SyncScheduler: Sync trigger would happen here.";
// Update last sync timestamp
QSettings settings;
qint64 now = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
settings.setValue("lastSyncTimestamp", now);
qDebug() << "SyncScheduler: Updated last sync timestamp to" << now;
}
bool SyncScheduler::shouldRunSync() const
{
// Check if enough time has passed since last sync
qint64 lastSync = getLastSyncTimestamp();
qint64 now = QDateTime::currentDateTimeUtc().toSecsSinceEpoch();
qint64 minIntervalSecs = m_minDaysBetweenSync * 24 * 60 * 60;
if (now - lastSync < minIntervalSecs) {
qDebug() << "SyncScheduler: Not enough time since last sync. Last:" << lastSync << "Now:" << now;
return false;
}
// Check if current hour >= m_runHour (UTC? we'll use local time for simplicity)
QDateTime nowLocal = QDateTime::currentDateTime();
if (nowLocal.time().hour() < m_runHour) {
qDebug() << "SyncScheduler: Current hour" << nowLocal.time().hour() << "is less than run hour" << m_runHour;
return false;
}
return true;
}
qint64 SyncScheduler::getLastSyncTimestamp() const
{
QSettings settings;
return settings.value("lastSyncTimestamp", 0).toLongLong();
}
void SyncScheduler::setLastSyncTimestamp(qint64 timestamp)
{
QSettings settings;
settings.setValue("lastSyncTimestamp", timestamp);
}