Paso 2: Implementar sistema de mensajería/eventos con Event Bus y actualizar synchronizers para publicar eventos
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QMutex>
|
||||
#include <QMap>
|
||||
#include <QVector>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include "events.h"
|
||||
|
||||
class EventBus : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static EventBus& instance();
|
||||
~EventBus() = default;
|
||||
|
||||
// Suscribirse a un tipo de evento específico
|
||||
template <typename EventType>
|
||||
void subscribe(std::function<void(const EventType&)> callback)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
// Usamos typeid para identificar el tipo de evento
|
||||
auto typeId = typeid(EventType).hash_code();
|
||||
m_subscribers[typeId].push_back(
|
||||
[callback](const std::shared_ptr<void>& eventPtr) {
|
||||
const EventType* event = static_cast<const EventType*>(eventPtr.get());
|
||||
if (event) {
|
||||
callback(*event);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Publicar un evento
|
||||
template <typename EventType>
|
||||
void publish(const EventType& event)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
auto typeId = typeid(EventType).hash_code();
|
||||
|
||||
// Crear un shared_ptr para manejar la vida del evento
|
||||
auto eventPtr = std::make_shared<EventType>(event);
|
||||
|
||||
// Notificar a todos los suscriptores de este tipo
|
||||
if (m_subscribers.contains(typeId)) {
|
||||
for (auto& callback : m_subscribers[typeId]) {
|
||||
callback(eventPtr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Desuscribirse (opcional, para evitar memory leaks en casos complejos)
|
||||
// Nota: En una implementación real, necesitaríamos guardar tokens de suscripción
|
||||
void unsubscribeAll()
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
m_subscribers.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
EventBus() = default;
|
||||
Q_DISABLE_COPY(EventBus)
|
||||
|
||||
QMutex m_mutex;
|
||||
// Mapeo de typeid -> vector de callbacks
|
||||
QMap<size_t, QVector<std::function<void(const std::shared_ptr<void>&)>>> m_subscribers;
|
||||
};
|
||||
|
||||
// Macros de conveniencia para usar el event bus
|
||||
#define EVENT_BUS EventBus::instance()
|
||||
#define SUBSCRIBE(EventType, callback) EVENT_BUS.subscribe<EventType>(callback)
|
||||
#define PUBLISH(event) EVENT_BUS.publish(event)
|
||||
@@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
#include <QDateTime>
|
||||
#include <QVector>
|
||||
#include "../models/account.h"
|
||||
#include "../models/folder.h"
|
||||
#include "../core/mailitem.h"
|
||||
|
||||
namespace WinoMail {
|
||||
namespace Events {
|
||||
|
||||
// Base event class
|
||||
struct BaseEvent {
|
||||
virtual ~BaseEvent() = default;
|
||||
QString eventId; // Unique identifier for the event
|
||||
QDateTime timestamp; // When the event occurred
|
||||
};
|
||||
|
||||
// Mail item events
|
||||
struct MailItemAddedEvent : public BaseEvent {
|
||||
MailItem item;
|
||||
};
|
||||
|
||||
struct MailItemRemovedEvent : public BaseEvent {
|
||||
QString itemUid; // UID of the removed item
|
||||
int folderId;
|
||||
};
|
||||
|
||||
struct MailItemUpdatedEvent : public BaseEvent {
|
||||
MailItem item;
|
||||
QVector<QString> changedFields; // Which fields were modified
|
||||
};
|
||||
|
||||
// Folder events
|
||||
struct FolderAddedEvent : public BaseEvent {
|
||||
Folder folder;
|
||||
};
|
||||
|
||||
struct FolderRemovedEvent : public BaseEvent {
|
||||
String folderId;
|
||||
};
|
||||
|
||||
struct FolderUpdatedEvent : public BaseEvent {
|
||||
Folder folder;
|
||||
QVector<QString> changedFields;
|
||||
};
|
||||
|
||||
// Account events
|
||||
struct AccountAddedEvent : public BaseEvent {
|
||||
Account account;
|
||||
};
|
||||
|
||||
struct AccountRemovedEvent : public BaseEvent {
|
||||
int accountId;
|
||||
};
|
||||
|
||||
struct AccountUpdatedEvent : public BaseEvent {
|
||||
Account account;
|
||||
QVector<QString> changedFields;
|
||||
};
|
||||
|
||||
// Sync events
|
||||
struct SyncStartedEvent : public BaseEvent {
|
||||
int accountId;
|
||||
String folderId; // Optional, empty for full account sync
|
||||
};
|
||||
|
||||
struct SyncFinishedEvent : public BaseEvent {
|
||||
int accountId;
|
||||
String folderId; // Optional, empty for full account sync
|
||||
bool success;
|
||||
QString errorMessage; // If success is false
|
||||
};
|
||||
|
||||
// Connection events
|
||||
struct AccountConnectedEvent : public BaseEvent {
|
||||
int accountId;
|
||||
QString provider; // "outlook", "gmail", "imap"
|
||||
};
|
||||
|
||||
struct AccountDisconnectedEvent : public BaseEvent {
|
||||
int accountId;
|
||||
QString provider;
|
||||
QString reason; // Optional reason for disconnection
|
||||
};
|
||||
|
||||
// Error events
|
||||
struct ErrorEvent : public BaseEvent {
|
||||
QString message;
|
||||
QString source; // Which component generated the error
|
||||
bool isCritical; // Whether this error requires user attention
|
||||
};
|
||||
|
||||
} // namespace Events
|
||||
} // namespace WinoMail
|
||||
Reference in New Issue
Block a user