2026-05-24 00:02:19 +02:00
|
|
|
#include "emailmanager.h"
|
2026-05-17 03:08:16 +02:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QStandardPaths>
|
|
|
|
|
#include "../db/dao/mailitemdao.h"
|
2026-05-24 00:02:19 +02:00
|
|
|
#include <QFile>
|
2026-05-24 21:01:05 +02:00
|
|
|
#include <QMessageBox>
|
2026-05-17 03:08:16 +02:00
|
|
|
|
|
|
|
|
EmailManager::EmailManager(QObject *parent)
|
|
|
|
|
: QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MailItem EmailManager::getMailItemById(qint64 id) const
|
|
|
|
|
{
|
|
|
|
|
// Use the DAO to fetch the MailItem by id
|
2026-05-24 00:02:19 +02:00
|
|
|
auto optItem = MailItemDao::findById(id);
|
2026-05-17 03:08:16 +02:00
|
|
|
if (optItem.has_value()) {
|
|
|
|
|
return optItem.value();
|
|
|
|
|
}
|
|
|
|
|
// Return an empty MailItem if not found
|
|
|
|
|
return MailItem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString EmailManager::getStorageDirectory() const
|
|
|
|
|
{
|
|
|
|
|
// Define where .eml files are stored (e.g., in the application data directory)
|
|
|
|
|
QString storagePath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
|
QDir dir(storagePath);
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
|
dir.mkpath("."); // create if it doesn't exist
|
|
|
|
|
}
|
|
|
|
|
// Assuming .eml files are stored directly in this directory
|
|
|
|
|
return storagePath;
|
2026-05-24 00:02:19 +02:00
|
|
|
}
|
|
|
|
|
|
2026-05-24 21:01:05 +02:00
|
|
|
QString EmailManager::getEmailHtmlById(qint64 id) const
|
|
|
|
|
{
|
|
|
|
|
// Get the MailItem by id
|
|
|
|
|
MailItem item = getMailItemById(id);
|
|
|
|
|
if (item.id() == 0) {
|
|
|
|
|
return "<html><body><h2>Error: Email not found</h2></body></html>";
|
|
|
|
|
}
|
|
|
|
|
// Construct the file path: storage directory + fileId + .eml
|
|
|
|
|
QString storageDir = getStorageDirectory();
|
|
|
|
|
QString fileName = item.fileId();
|
|
|
|
|
if (fileName.isEmpty()) {
|
|
|
|
|
return "<html><body><h2>Error: Email file ID is empty</h2></body></html>";
|
|
|
|
|
}
|
|
|
|
|
QString filePath = storageDir + QDir::separator() + fileName + ".eml";
|
|
|
|
|
// Convert the .eml file to HTML
|
|
|
|
|
return convertEmlToHtml(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 00:02:19 +02:00
|
|
|
QString EmailManager::convertEmlToHtml(const QString& emlFilePath) const
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement actual MIME to HTML conversion using gmime or similar
|
|
|
|
|
// For now, return a placeholder indicating the feature is not yet implemented
|
|
|
|
|
QFile file(emlFilePath);
|
|
|
|
|
if (!file.exists()) {
|
|
|
|
|
return "<html><body><h2>Error: Email file not found</h2></body></html>";
|
|
|
|
|
}
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
|
|
|
return "<html><body><h2>Error: Cannot open email file</h2></body></html>";
|
|
|
|
|
}
|
|
|
|
|
QByteArray data = file.readAll();
|
|
|
|
|
file.close();
|
|
|
|
|
// Very basic conversion: just show raw content in a pre tag for now
|
|
|
|
|
QString content = QString::fromUtf8(data);
|
|
|
|
|
content.replace("&", "&").replace("<", "<").replace(">", ">");
|
|
|
|
|
return QString("<html><body style='font-family: monospace;'><h2>Email Content (raw)</h2><pre>%1</pre></body></html>")
|
|
|
|
|
.arg(content);
|
2026-05-24 21:01:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool EmailManager::sendEmail(const QString& to, const QString& subject, const QString& body)
|
|
|
|
|
{
|
|
|
|
|
// TODO: Implement actual email sending
|
|
|
|
|
// For now, just show a message and return true
|
|
|
|
|
QMessageBox::information(nullptr, "Send Email",
|
|
|
|
|
QString("To: %1\nSubject: %2\nBody: %3").arg(to, subject, body));
|
|
|
|
|
return true;
|
2026-05-17 03:08:16 +02:00
|
|
|
}
|