2026-06-17 22:44:27 +02:00
|
|
|
#include "mailservice.h"
|
2026-07-05 10:45:44 +02:00
|
|
|
#include <QFuture>
|
|
|
|
|
#include <QFutureWatcher>
|
|
|
|
|
#include <QtConcurrent/QtConcurrent>
|
2026-06-17 22:44:27 +02:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
2026-07-05 10:45:44 +02:00
|
|
|
MailService::MailService(AccountService *accountService, QObject *parent)
|
2026-06-17 22:44:27 +02:00
|
|
|
: QObject(parent)
|
|
|
|
|
, m_composer(new EmailComposerBridge(this))
|
2026-07-05 10:45:44 +02:00
|
|
|
, m_accountService(accountService)
|
2026-06-17 22:44:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVector<MailItem> MailService::getMails(const QString &folderId)
|
|
|
|
|
{
|
|
|
|
|
return MailItemDao::findByFolderId(folderId.toInt());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MailService::sendMail(const MailItem &mail, const QString &accountId)
|
|
|
|
|
{
|
2026-07-05 10:45:44 +02:00
|
|
|
// For now we just simulate sending by using EmailComposerBridge
|
|
|
|
|
// In a real implementation, this would use SMTP settings from the account.
|
|
|
|
|
Q_UNUSED(mail);
|
|
|
|
|
Q_UNUSED(accountId);
|
|
|
|
|
// Emit success immediately; actual sending would be asynchronous.
|
|
|
|
|
QMetaObject::invokeMethod(this, "mailSent", Qt::QueuedConnection,
|
|
|
|
|
Q_ARG(QString, QString()));
|
2026-06-17 22:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MailService::fetchMails(const QString &accountId, const QString &folderId)
|
|
|
|
|
{
|
2026-07-05 10:45:44 +02:00
|
|
|
// Determine provider type from account
|
|
|
|
|
if (!m_accountService) {
|
|
|
|
|
emit mailFetchError(accountId, folderId, tr("AccountService not available"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Account *acc = m_accountService->findAccountById(accountId.toLongLong());
|
|
|
|
|
QString providerType = QStringLiteral("imap"); // fallback
|
|
|
|
|
if (acc) {
|
|
|
|
|
switch (acc->type()) {
|
|
|
|
|
case AccountType::IMAP:
|
|
|
|
|
providerType = QStringLiteral("imap");
|
|
|
|
|
break;
|
|
|
|
|
case AccountType::POP3:
|
|
|
|
|
providerType = QStringLiteral("pop3");
|
|
|
|
|
break;
|
|
|
|
|
case AccountType::Gmail:
|
|
|
|
|
providerType = QStringLiteral("gmail");
|
|
|
|
|
break;
|
|
|
|
|
case AccountType::Outlook:
|
|
|
|
|
providerType = QStringLiteral("outlook");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
providerType = QStringLiteral("imap");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Synchronizer *sync = SynchronizerProvider::instance().createSynchronizer(accountId, providerType);
|
|
|
|
|
if (!sync) {
|
|
|
|
|
emit mailFetchError(accountId, folderId, tr("Failed to create synchronizer"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect progress signals (if they exist) using string-based syntax for compatibility
|
|
|
|
|
QObject::connect(sync, SIGNAL(progressChanged(int)), this, SIGNAL(progressChanged(int)), Qt::QueuedConnection);
|
|
|
|
|
QObject::connect(sync, SIGNAL(statusMessage(const QString&)), this, SIGNAL(statusMessage(const QString&)), Qt::QueuedConnection);
|
|
|
|
|
|
|
|
|
|
QFuture<QVector<MailItem>> future = QtConcurrent::run([=]() {
|
|
|
|
|
// SinceUid = 0 for full sync; could be stored per folder but omitted for simplicity
|
|
|
|
|
return sync->fetchMailItems(folderId, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
QFutureWatcher<QVector<MailItem>> *watcher = new QFutureWatcher<QVector<MailItem>>(this);
|
|
|
|
|
QObject::connect(watcher, &QFutureWatcher<QVector<MailItem>>::finished, this, [=]() {
|
|
|
|
|
// Disconnect progress signals
|
|
|
|
|
QObject::disconnect(sync, SIGNAL(progressChanged(int)), this, SIGNAL(progressChanged(int)));
|
|
|
|
|
QObject::disconnect(sync, SIGNAL(statusMessage(const QString&)), this, SIGNAL(statusMessage(const QString&)));
|
|
|
|
|
QVector<MailItem> items = watcher->result();
|
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
emit mailFetched(accountId, folderId, items);
|
|
|
|
|
});
|
|
|
|
|
watcher->setFuture(future);
|
2026-06-17 22:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MailService::moveMail(const QString &mailItemId, const QString &targetFolderId)
|
|
|
|
|
{
|
2026-07-05 10:45:44 +02:00
|
|
|
bool ok;
|
|
|
|
|
qint64 id = mailItemId.toLongLong(&ok);
|
|
|
|
|
if (!ok) return;
|
|
|
|
|
std::optional<MailItem> opt = MailItemDao::findById(id);
|
|
|
|
|
if (!opt) return;
|
|
|
|
|
MailItem item = *opt;
|
|
|
|
|
item.setFolderId(targetFolderId.toInt());
|
|
|
|
|
if (MailItemDao::update(item))
|
|
|
|
|
emit mailMoved(mailItemId);
|
2026-06-17 22:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MailService::deleteMail(const QString &mailItemId)
|
|
|
|
|
{
|
2026-07-05 10:45:44 +02:00
|
|
|
bool ok;
|
|
|
|
|
qint64 id = mailItemId.toLongLong(&ok);
|
|
|
|
|
if (!ok) return;
|
|
|
|
|
if (MailItemDao::remove(id))
|
|
|
|
|
emit mailDeleted(mailItemId);
|
2026-06-17 22:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MailService::markAsRead(const QString &mailItemId, bool read)
|
|
|
|
|
{
|
2026-07-05 10:45:44 +02:00
|
|
|
bool ok;
|
|
|
|
|
qint64 id = mailItemId.toLongLong(&ok);
|
|
|
|
|
if (!ok) return;
|
|
|
|
|
std::optional<MailItem> opt = MailItemDao::findById(id);
|
|
|
|
|
if (!opt) return;
|
|
|
|
|
MailItem item = *opt;
|
|
|
|
|
item.setRead(read);
|
|
|
|
|
if (MailItemDao::update(item))
|
|
|
|
|
emit mailReadStateChanged(mailItemId, read);
|
2026-06-17 22:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#include "mailservice.moc"
|