1
0
forked from blue/squawk

file progress events delivery methonds

This commit is contained in:
Blue 2021-02-27 15:21:27 +03:00
parent 85555da81f
commit a0348b8fd2
7 changed files with 97 additions and 19 deletions

View File

@ -30,6 +30,7 @@ Models::Element::Element(Type p_type, const Models::Account* acc, const QString&
feed(new MessageFeed(this)) feed(new MessageFeed(this))
{ {
connect(feed, &MessageFeed::requestArchive, this, &Element::requestArchive); connect(feed, &MessageFeed::requestArchive, this, &Element::requestArchive);
connect(feed, &MessageFeed::fileLocalPathRequest, this, &Element::fileLocalPathRequest);
QMap<QString, QVariant>::const_iterator itr = data.find("avatarState"); QMap<QString, QVariant>::const_iterator itr = data.find("avatarState");
if (itr != data.end()) { if (itr != data.end()) {
@ -154,3 +155,9 @@ bool Models::Element::isRoom() const
{ {
return type != contact; return type != contact;
} }
void Models::Element::fileProgress(const QString& messageId, qreal value)
{
feed->fileProgress(messageId, value);
}

View File

@ -43,9 +43,11 @@ public:
unsigned int getMessagesCount() const; unsigned int getMessagesCount() const;
void responseArchive(const std::list<Shared::Message> list, bool last); void responseArchive(const std::list<Shared::Message> list, bool last);
bool isRoom() const; bool isRoom() const;
void fileProgress(const QString& messageId, qreal value);
signals: signals:
void requestArchive(const QString& before); void requestArchive(const QString& before);
void fileLocalPathRequest(const QString& messageId, const QString& url);
protected: protected:
void setJid(const QString& p_jid); void setJid(const QString& p_jid);

View File

@ -299,10 +299,46 @@ Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) c
void Models::MessageFeed::downloadAttachment(const QString& messageId) void Models::MessageFeed::downloadAttachment(const QString& messageId)
{ {
qDebug() << "request to download attachment of the message" << messageId; QModelIndex ind = modelIndexById(messageId);
if (ind.isValid()) {
std::pair<Progress::iterator, bool> progressPair = downloads.insert(std::make_pair(messageId, 0));
if (!progressPair.second) { //Only to take action if we weren't already downloading it
Shared::Message* msg = static_cast<Shared::Message*>(ind.internalPointer());
emit dataChanged(ind, ind);
emit fileLocalPathRequest(messageId, msg->getOutOfBandUrl());
} else {
qDebug() << "Attachment download for message with id" << messageId << "is already in progress, skipping";
}
} else {
qDebug() << "An attempt to download an attachment for the message that doesn't exist. ID:" << messageId;
}
} }
void Models::MessageFeed::uploadAttachment(const QString& messageId) void Models::MessageFeed::uploadAttachment(const QString& messageId)
{ {
qDebug() << "request to upload attachment of the message" << messageId; qDebug() << "request to upload attachment of the message" << messageId;
} }
void Models::MessageFeed::fileProgress(const QString& messageId, qreal value)
{
Progress::iterator itr = downloads.find(messageId);
if (itr != downloads.end()) {
itr->second = value;
QModelIndex ind = modelIndexById(messageId);
emit dataChanged(ind, ind);
}
}
QModelIndex Models::MessageFeed::modelIndexById(const QString& id) const
{
StorageById::const_iterator itr = indexById.find(id);
if (itr != indexById.end()) {
Shared::Message* msg = *itr;
StorageByTime::const_iterator tItr = indexByTime.upper_bound(msg->getTime());
int position = indexByTime.rank(tItr);
return createIndex(position, 0, msg);
} else {
return QModelIndex();
}
}

View File

@ -59,14 +59,17 @@ public:
void uploadAttachment(const QString& messageId); void uploadAttachment(const QString& messageId);
unsigned int unreadMessagesCount() const; unsigned int unreadMessagesCount() const;
void fileProgress(const QString& messageId, qreal value);
signals: signals:
void requestArchive(const QString& before); void requestArchive(const QString& before);
void requestStateChange(bool requesting); void requestStateChange(bool requesting);
void fileLocalPathRequest(const QString& messageId, const QString& url);
protected: protected:
bool sentByMe(const Shared::Message& msg) const; bool sentByMe(const Shared::Message& msg) const;
Attachment fillAttach(const Shared::Message& msg) const; Attachment fillAttach(const Shared::Message& msg) const;
QModelIndex modelIndexById(const QString& id) const;
public: public:
enum MessageRoles { enum MessageRoles {

View File

@ -27,7 +27,8 @@ Models::Roster::Roster(QObject* parent):
root(new Item(Item::root, {{"name", "root"}})), root(new Item(Item::root, {{"name", "root"}})),
accounts(), accounts(),
groups(), groups(),
contacts() contacts(),
requestedFiles()
{ {
connect(accountsModel, &Accounts::dataChanged, this, &Roster::onAccountDataChanged); connect(accountsModel, &Accounts::dataChanged, this, &Roster::onAccountDataChanged);
connect(root, &Item::childChanged, this, &Roster::onChildChanged); connect(root, &Item::childChanged, this, &Roster::onChildChanged);
@ -447,6 +448,7 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
if (itr == contacts.end()) { if (itr == contacts.end()) {
contact = new Contact(acc, jid, data); contact = new Contact(acc, jid, data);
connect(contact, &Contact::requestArchive, this, &Roster::onElementRequestArchive); connect(contact, &Contact::requestArchive, this, &Roster::onElementRequestArchive);
connect(contact, &Contact::fileLocalPathRequest, this, &Roster::onElementFileLocalPathRequest);
contacts.insert(std::make_pair(id, contact)); contacts.insert(std::make_pair(id, contact));
} else { } else {
contact = itr->second; contact = itr->second;
@ -806,6 +808,7 @@ void Models::Roster::addRoom(const QString& account, const QString jid, const QM
Room* room = new Room(acc, jid, data); Room* room = new Room(acc, jid, data);
connect(room, &Contact::requestArchive, this, &Roster::onElementRequestArchive); connect(room, &Contact::requestArchive, this, &Roster::onElementRequestArchive);
connect(room, &Contact::fileLocalPathRequest, this, &Roster::onElementFileLocalPathRequest);
rooms.insert(std::make_pair(id, room)); rooms.insert(std::make_pair(id, room));
acc->appendChild(room); acc->appendChild(room);
} }
@ -978,3 +981,41 @@ void Models::Roster::responseArchive(const QString& account, const QString& jid,
} }
} }
} }
void Models::Roster::onElementFileLocalPathRequest(const QString& messageId, const QString& url)
{
Element* el = static_cast<Element*>(sender());
std::map<QString, std::set<Models::Roster::ElId>>::iterator itr = requestedFiles.find(messageId);
bool created = false;
if (itr == requestedFiles.end()) {
itr = requestedFiles.insert(std::make_pair(messageId, std::set<Models::Roster::ElId>())).first;
created = true;
}
itr->second.insert(Models::Roster::ElId(el->getAccountName(), el->getJid()));
if (created) {
emit fileLocalPathRequest(messageId, url);
}
}
void Models::Roster::fileProgress(const QString& messageId, qreal value)
{
std::map<QString, std::set<Models::Roster::ElId>>::const_iterator itr = requestedFiles.find(messageId);
if (itr == requestedFiles.end()) {
qDebug() << "fileProgress in UI but there is nobody waiting for that id:" << messageId << ", skipping";
return;
} else {
const std::set<Models::Roster::ElId>& convs = itr->second;
for (const Models::Roster::ElId& id : convs) {
std::map<ElId, Contact*>::const_iterator cItr = contacts.find(id);
if (cItr != contacts.end()) {
cItr->second->fileProgress(messageId, value);
} else {
std::map<ElId, Room*>::const_iterator rItr = rooms.find(id);
if (rItr != rooms.end()) {
rItr->second->fileProgress(messageId, value);
}
}
}
}
}

View File

@ -81,11 +81,13 @@ public:
QModelIndex getAccountIndex(const QString& name); QModelIndex getAccountIndex(const QString& name);
QModelIndex getGroupIndex(const QString& account, const QString& name); QModelIndex getGroupIndex(const QString& account, const QString& name);
void responseArchive(const QString& account, const QString& jid, const std::list<Shared::Message>& list, bool last); void responseArchive(const QString& account, const QString& jid, const std::list<Shared::Message>& list, bool last);
void fileProgress(const QString& messageId, qreal value);
Accounts* accountsModel; Accounts* accountsModel;
signals: signals:
void requestArchive(const QString& account, const QString& jid, const QString& before); void requestArchive(const QString& account, const QString& jid, const QString& before);
void fileLocalPathRequest(const QString& messageId, const QString& url);
private: private:
Item* root; Item* root;
@ -93,6 +95,7 @@ private:
std::map<ElId, Group*> groups; std::map<ElId, Group*> groups;
std::map<ElId, Contact*> contacts; std::map<ElId, Contact*> contacts;
std::map<ElId, Room*> rooms; std::map<ElId, Room*> rooms;
std::map<QString, std::set<Models::Roster::ElId>> requestedFiles;
private slots: private slots:
void onAccountDataChanged(const QModelIndex& tl, const QModelIndex& br, const QVector<int>& roles); void onAccountDataChanged(const QModelIndex& tl, const QModelIndex& br, const QVector<int>& roles);
@ -104,6 +107,7 @@ private slots:
void onChildIsAboutToBeMoved(Item* source, int first, int last, Item* destination, int newIndex); void onChildIsAboutToBeMoved(Item* source, int first, int last, Item* destination, int newIndex);
void onChildMoved(); void onChildMoved();
void onElementRequestArchive(const QString& before); void onElementRequestArchive(const QString& before);
void onElementFileLocalPathRequest(const QString& messageId, const QString& url);
public: public:
class ElId { class ElId {

View File

@ -63,6 +63,7 @@ Squawk::Squawk(QWidget *parent) :
connect(rosterModel.accountsModel, &Models::Accounts::sizeChanged, this, &Squawk::onAccountsSizeChanged); connect(rosterModel.accountsModel, &Models::Accounts::sizeChanged, this, &Squawk::onAccountsSizeChanged);
connect(&rosterModel, &Models::Roster::requestArchive, this, &Squawk::onConversationRequestArchive); connect(&rosterModel, &Models::Roster::requestArchive, this, &Squawk::onConversationRequestArchive);
connect(&rosterModel, &Models::Roster::fileLocalPathRequest, this, &Squawk::fileLocalPathRequest);
connect(contextMenu, &QMenu::aboutToHide, this, &Squawk::onContextAboutToHide); connect(contextMenu, &QMenu::aboutToHide, this, &Squawk::onContextAboutToHide);
//m_ui->mainToolBar->addWidget(m_ui->comboBox); //m_ui->mainToolBar->addWidget(m_ui->comboBox);
@ -405,23 +406,7 @@ void Squawk::onConversationDownloadFile(const QString& messageId, const QString&
void Squawk::fileProgress(const QString& messageId, qreal value) void Squawk::fileProgress(const QString& messageId, qreal value)
{ {
std::map<QString, std::set<Models::Roster::ElId>>::const_iterator itr = requestedFiles.find(messageId); rosterModel.fileProgress(messageId, value);
if (itr == requestedFiles.end()) {
qDebug() << "fileProgress in UI Squawk but there is nobody waiting for that id" << messageId << ", skipping";
return;
} else {
const std::set<Models::Roster::ElId>& convs = itr->second;
for (std::set<Models::Roster::ElId>::const_iterator cItr = convs.begin(), cEnd = convs.end(); cItr != cEnd; ++cItr) {
const Models::Roster::ElId& id = *cItr;
Conversations::const_iterator c = conversations.find(id);
if (c != conversations.end()) {
c->second->responseFileProgress(messageId, value);
}
if (currentConversation != 0 && currentConversation->getId() == id) {
currentConversation->responseFileProgress(messageId, value);
}
}
}
} }
void Squawk::fileError(const QString& messageId, const QString& error) void Squawk::fileError(const QString& messageId, const QString& error)