From a0348b8fd2a0eb34e7bc575f6da842e30fbca767 Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 27 Feb 2021 15:21:27 +0300 Subject: [PATCH] file progress events delivery methonds --- ui/models/element.cpp | 7 +++++++ ui/models/element.h | 2 ++ ui/models/messagefeed.cpp | 38 +++++++++++++++++++++++++++++++++- ui/models/messagefeed.h | 3 +++ ui/models/roster.cpp | 43 ++++++++++++++++++++++++++++++++++++++- ui/models/roster.h | 4 ++++ ui/squawk.cpp | 19 ++--------------- 7 files changed, 97 insertions(+), 19 deletions(-) diff --git a/ui/models/element.cpp b/ui/models/element.cpp index 20df389..7b0e70f 100644 --- a/ui/models/element.cpp +++ b/ui/models/element.cpp @@ -30,6 +30,7 @@ Models::Element::Element(Type p_type, const Models::Account* acc, const QString& feed(new MessageFeed(this)) { connect(feed, &MessageFeed::requestArchive, this, &Element::requestArchive); + connect(feed, &MessageFeed::fileLocalPathRequest, this, &Element::fileLocalPathRequest); QMap::const_iterator itr = data.find("avatarState"); if (itr != data.end()) { @@ -154,3 +155,9 @@ bool Models::Element::isRoom() const { return type != contact; } + +void Models::Element::fileProgress(const QString& messageId, qreal value) +{ + feed->fileProgress(messageId, value); +} + diff --git a/ui/models/element.h b/ui/models/element.h index 047a645..f537f9b 100644 --- a/ui/models/element.h +++ b/ui/models/element.h @@ -43,9 +43,11 @@ public: unsigned int getMessagesCount() const; void responseArchive(const std::list list, bool last); bool isRoom() const; + void fileProgress(const QString& messageId, qreal value); signals: void requestArchive(const QString& before); + void fileLocalPathRequest(const QString& messageId, const QString& url); protected: void setJid(const QString& p_jid); diff --git a/ui/models/messagefeed.cpp b/ui/models/messagefeed.cpp index 7d167cd..55451fb 100644 --- a/ui/models/messagefeed.cpp +++ b/ui/models/messagefeed.cpp @@ -299,10 +299,46 @@ Models::Attachment Models::MessageFeed::fillAttach(const Shared::Message& msg) c 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 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(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) { 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(); + } +} + diff --git a/ui/models/messagefeed.h b/ui/models/messagefeed.h index 9a58c45..e8995d5 100644 --- a/ui/models/messagefeed.h +++ b/ui/models/messagefeed.h @@ -59,14 +59,17 @@ public: void uploadAttachment(const QString& messageId); unsigned int unreadMessagesCount() const; + void fileProgress(const QString& messageId, qreal value); signals: void requestArchive(const QString& before); void requestStateChange(bool requesting); + void fileLocalPathRequest(const QString& messageId, const QString& url); protected: bool sentByMe(const Shared::Message& msg) const; Attachment fillAttach(const Shared::Message& msg) const; + QModelIndex modelIndexById(const QString& id) const; public: enum MessageRoles { diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 95515b3..a7bc74e 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -27,7 +27,8 @@ Models::Roster::Roster(QObject* parent): root(new Item(Item::root, {{"name", "root"}})), accounts(), groups(), - contacts() + contacts(), + requestedFiles() { connect(accountsModel, &Accounts::dataChanged, this, &Roster::onAccountDataChanged); 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()) { contact = new Contact(acc, jid, data); connect(contact, &Contact::requestArchive, this, &Roster::onElementRequestArchive); + connect(contact, &Contact::fileLocalPathRequest, this, &Roster::onElementFileLocalPathRequest); contacts.insert(std::make_pair(id, contact)); } else { 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); connect(room, &Contact::requestArchive, this, &Roster::onElementRequestArchive); + connect(room, &Contact::fileLocalPathRequest, this, &Roster::onElementFileLocalPathRequest); rooms.insert(std::make_pair(id, 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(sender()); + std::map>::iterator itr = requestedFiles.find(messageId); + bool created = false; + if (itr == requestedFiles.end()) { + itr = requestedFiles.insert(std::make_pair(messageId, std::set())).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>::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& convs = itr->second; + for (const Models::Roster::ElId& id : convs) { + std::map::const_iterator cItr = contacts.find(id); + if (cItr != contacts.end()) { + cItr->second->fileProgress(messageId, value); + } else { + std::map::const_iterator rItr = rooms.find(id); + if (rItr != rooms.end()) { + rItr->second->fileProgress(messageId, value); + } + } + } + } +} + diff --git a/ui/models/roster.h b/ui/models/roster.h index f43d9a9..1f398d8 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -81,11 +81,13 @@ public: QModelIndex getAccountIndex(const QString& name); QModelIndex getGroupIndex(const QString& account, const QString& name); void responseArchive(const QString& account, const QString& jid, const std::list& list, bool last); + void fileProgress(const QString& messageId, qreal value); Accounts* accountsModel; signals: void requestArchive(const QString& account, const QString& jid, const QString& before); + void fileLocalPathRequest(const QString& messageId, const QString& url); private: Item* root; @@ -93,6 +95,7 @@ private: std::map groups; std::map contacts; std::map rooms; + std::map> requestedFiles; private slots: void onAccountDataChanged(const QModelIndex& tl, const QModelIndex& br, const QVector& roles); @@ -104,6 +107,7 @@ private slots: void onChildIsAboutToBeMoved(Item* source, int first, int last, Item* destination, int newIndex); void onChildMoved(); void onElementRequestArchive(const QString& before); + void onElementFileLocalPathRequest(const QString& messageId, const QString& url); public: class ElId { diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 52e1d9f..55bfe63 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -63,6 +63,7 @@ Squawk::Squawk(QWidget *parent) : connect(rosterModel.accountsModel, &Models::Accounts::sizeChanged, this, &Squawk::onAccountsSizeChanged); connect(&rosterModel, &Models::Roster::requestArchive, this, &Squawk::onConversationRequestArchive); + connect(&rosterModel, &Models::Roster::fileLocalPathRequest, this, &Squawk::fileLocalPathRequest); connect(contextMenu, &QMenu::aboutToHide, this, &Squawk::onContextAboutToHide); //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) { - std::map>::const_iterator itr = requestedFiles.find(messageId); - if (itr == requestedFiles.end()) { - qDebug() << "fileProgress in UI Squawk but there is nobody waiting for that id" << messageId << ", skipping"; - return; - } else { - const std::set& convs = itr->second; - for (std::set::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); - } - } - } + rosterModel.fileProgress(messageId, value); } void Squawk::fileError(const QString& messageId, const QString& error)