Browse Source

Keep channel download commands out of the shared message delegate

master
evgeny 2 days ago
parent
commit
a281165450
  1. 51
      tools/chatgui/src/messagedelegate.cpp
  2. 2
      tools/chatgui/src/messagedelegate.h

51
tools/chatgui/src/messagedelegate.cpp

@ -24,14 +24,6 @@
#include <QDebug>
#include <QImageReader>
extern "C" {
#include "../transport/gui_bridge.h"
#include "chat/chat_core.h"
#include "../../lib/u_async.h"
#include "../../lib/mem.h"
void chat_core_attachment_download_trampoline(void* arg);
}
MessageDelegate::MessageDelegate(QObject *parent)
: QStyledItemDelegate(parent) {}
@ -96,16 +88,7 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
}
if (dlState == 1) return false; /* скачивается */
if (chId.isEmpty() || msgId <= 0) return false;
struct attachment_dl_req* req = (struct attachment_dl_req*)
u_malloc(sizeof(struct attachment_dl_req));
if (!req) return false;
memset(req, 0, sizeof(*req));
req->inst = gui_bridge_get_inst();
strncpy(req->channel_id, chId.toUtf8().constData(), sizeof(req->channel_id) - 1);
req->msg_id = msgId;
gui_bridge_post_uasync_fn(chat_core_attachment_download_trampoline, req);
return true;
return m_downloadCb && m_downloadCb(index);
}
if (L.isImage) {
@ -123,16 +106,7 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
}
if (dlState == 1) return false; /* скачивается */
if (chId.isEmpty() || msgId <= 0) return false;
struct attachment_dl_req* req = (struct attachment_dl_req*)
u_malloc(sizeof(struct attachment_dl_req));
if (!req) return false;
memset(req, 0, sizeof(*req));
req->inst = gui_bridge_get_inst();
strncpy(req->channel_id, chId.toUtf8().constData(), sizeof(req->channel_id) - 1);
req->msg_id = msgId;
gui_bridge_post_uasync_fn(chat_core_attachment_download_trampoline, req);
return true;
return m_downloadCb && m_downloadCb(index);
}
if (L.isFile) {
@ -151,16 +125,7 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
}
if (dlState == 1) return false; /* downloading — no action */
if (chId.isEmpty() || msgId <= 0) return false;
struct attachment_dl_req* req = (struct attachment_dl_req*)
u_malloc(sizeof(struct attachment_dl_req));
if (!req) return false;
memset(req, 0, sizeof(*req));
req->inst = gui_bridge_get_inst();
strncpy(req->channel_id, chId.toUtf8().constData(), sizeof(req->channel_id) - 1);
req->msg_id = msgId;
gui_bridge_post_uasync_fn(chat_core_attachment_download_trampoline, req);
return true;
return m_downloadCb && m_downloadCb(index);
}
if (!L.isVoice) return false;
@ -184,15 +149,7 @@ bool MessageDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
/* not downloaded (or file evicted): start download (play will start on completion) */
bool fileMissing = filePath.isEmpty() || !QFile::exists(filePath);
if (fileMissing && dlState != 1) {
if (chId.isEmpty() || msgId <= 0) return false;
struct attachment_dl_req* req = (struct attachment_dl_req*)
u_malloc(sizeof(struct attachment_dl_req));
if (!req) return false;
memset(req, 0, sizeof(*req));
req->inst = gui_bridge_get_inst();
strncpy(req->channel_id, chId.toUtf8().constData(), sizeof(req->channel_id) - 1);
req->msg_id = msgId;
gui_bridge_post_uasync_fn(chat_core_attachment_download_trampoline, req);
if (!m_downloadCb || !m_downloadCb(index)) return false;
model->setData(index, true, MsgVoiceDownloadPendingRole);
return true;
}

2
tools/chatgui/src/messagedelegate.h

@ -60,6 +60,7 @@ public:
void setVideoOpenCallback(std::function<void(const QString&)> cb) { m_videoOpenCb = std::move(cb); }
void setImageOpenCallback(std::function<void(const QString&)> cb) { m_imageOpenCb = std::move(cb); }
void setDownloadCallback(std::function<bool(const QModelIndex&)> cb) { m_downloadCb = std::move(cb); }
private:
struct Layout {
@ -102,6 +103,7 @@ private:
std::function<void(const QString&)> m_videoOpenCb;
std::function<void(const QString&)> m_imageOpenCb;
std::function<bool(const QModelIndex&)> m_downloadCb;
Layout calcLayout(const QStyleOptionViewItem &option,
const QModelIndex &index) const;

Loading…
Cancel
Save