From 48e498be251760e34def4f42a28940136c72ee80 Mon Sep 17 00:00:00 2001 From: blue Date: Tue, 20 Apr 2021 00:49:24 +0300 Subject: [PATCH] some debug, message changing in messageFeed --- core/networkaccess.cpp | 4 +-- shared/message.cpp | 20 +++++++++++ shared/message.h | 12 +++++++ ui/models/element.cpp | 2 +- ui/models/messagefeed.cpp | 68 +++++++++++++++++++++++++++++++---- ui/models/messagefeed.h | 3 +- ui/squawk.cpp | 23 +----------- ui/utils/comboboxdelegate.cpp | 2 +- ui/utils/messagedelegate.cpp | 6 +++- ui/utils/messagedelegate.h | 2 ++ 10 files changed, 107 insertions(+), 35 deletions(-) diff --git a/core/networkaccess.cpp b/core/networkaccess.cpp index 60d7cb9..f178068 100644 --- a/core/networkaccess.cpp +++ b/core/networkaccess.cpp @@ -479,11 +479,11 @@ QString Core::NetworkAccess::checkFileName(const QString& name, const QString& p suffix += "." + (*sItr); } QString postfix(""); - QFileInfo proposedName(path + realName + suffix); + QFileInfo proposedName(path + "/" + realName + suffix); int counter = 0; while (proposedName.exists()) { QString count = QString("(") + std::to_string(++counter).c_str() + ")"; - proposedName = QFileInfo(path + realName + count + suffix); + proposedName = QFileInfo(path + "/" + realName + count + suffix); } return proposedName.absoluteFilePath(); diff --git a/shared/message.cpp b/shared/message.cpp index 3f23d59..6728bbe 100644 --- a/shared/message.cpp +++ b/shared/message.cpp @@ -456,3 +456,23 @@ void Shared::Message::setAttachPath(const QString& path) { attachPath = path; } + +Shared::Message::Change::Change(const QMap& _data): + data(_data), + idModified(false) {} + +void Shared::Message::Change::operator()(Shared::Message& msg) +{ + idModified = msg.change(data); +} + +void Shared::Message::Change::operator()(Shared::Message* msg) +{ + idModified = msg->change(data); +} + +bool Shared::Message::Change::hasIdBeenModified() const +{ + return idModified; +} + diff --git a/shared/message.h b/shared/message.h index 2082101..c2766b3 100644 --- a/shared/message.h +++ b/shared/message.h @@ -49,6 +49,18 @@ public: static const State StateHighest = State::error; static const State StateLowest = State::pending; + struct Change //change functor, stores in idModified if ID has been modified during change + { + Change(const QMap& _data); + void operator() (Message& msg); + void operator() (Message* msg); + bool hasIdBeenModified() const; + + private: + const QMap& data; + bool idModified; + }; + Message(Type p_type); Message(); diff --git a/ui/models/element.cpp b/ui/models/element.cpp index d372e8d..659a30f 100644 --- a/ui/models/element.cpp +++ b/ui/models/element.cpp @@ -143,7 +143,7 @@ void Models::Element::addMessage(const Shared::Message& data) void Models::Element::changeMessage(const QString& id, const QMap& data) { - + feed->changeMessage(id, data); } void Models::Element::responseArchive(const std::list list, bool last) diff --git a/ui/models/messagefeed.cpp b/ui/models/messagefeed.cpp index 99951d5..7c82900 100644 --- a/ui/models/messagefeed.cpp +++ b/ui/models/messagefeed.cpp @@ -76,8 +76,44 @@ void Models::MessageFeed::addMessage(const Shared::Message& msg) endInsertRows(); } -void Models::MessageFeed::changeMessage(const QString& id, const Shared::Message& msg) +void Models::MessageFeed::changeMessage(const QString& id, const QMap& data) { + StorageById::iterator itr = indexById.find(id); + if (itr == indexById.end()) { + qDebug() << "received a command to change a message, but the message couldn't be found, skipping"; + return; + } + + Shared::Message* msg = *itr; + QModelIndex index = modelIndexByTime(id, msg->getTime()); + Shared::Message::Change functor(data); + bool success = indexById.modify(itr, functor); + if (!success) { + qDebug() << "received a command to change a message, but something went wrong modifying message in the feed, throwing error"; + throw 872; + } + + if (functor.hasIdBeenModified()) { + + } + + //change message is a final event in download/upload event train + //only after changeMessage we can consider the download is done + Progress::const_iterator dItr = downloads.find(id); + if (dItr != downloads.end()) { + if (dItr->second == 1) { + downloads.erase(dItr); + } + } else { + dItr = uploads.find(id); + if (dItr != uploads.end()) { + if (dItr->second == 1) { + uploads.erase(dItr); + } + } + } + + emit dataChanged(index, index); } void Models::MessageFeed::removeMessage(const QString& id) @@ -338,7 +374,7 @@ void Models::MessageFeed::fileProgress(const QString& messageId, qreal value, bo void Models::MessageFeed::fileComplete(const QString& messageId, bool up) { - //TODO + fileProgress(messageId, 1, up); } void Models::MessageFeed::fileError(const QString& messageId, const QString& error, bool up) @@ -352,11 +388,29 @@ 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(); + return modelIndexByTime(id, msg->getTime()); } + + return QModelIndex(); } +QModelIndex Models::MessageFeed::modelIndexByTime(const QString& id, const QDateTime& time) const +{ + StorageByTime::const_iterator tItr = indexByTime.upper_bound(time); + StorageByTime::const_iterator tBeg = indexByTime.begin(); + bool found = false; + while (tItr != tBeg) { + if (id == (*tItr)->getId()) { + found = true; + break; + } + --tItr; + } + + if (found) { + int position = indexByTime.rank(tItr); + return createIndex(position, 0, *tItr); + } + + return QModelIndex(); +} diff --git a/ui/models/messagefeed.h b/ui/models/messagefeed.h index f4c2c28..e8fb712 100644 --- a/ui/models/messagefeed.h +++ b/ui/models/messagefeed.h @@ -44,7 +44,7 @@ public: ~MessageFeed(); void addMessage(const Shared::Message& msg); - void changeMessage(const QString& id, const Shared::Message& msg); + void changeMessage(const QString& id, const QMap& data); void removeMessage(const QString& id); QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override; @@ -72,6 +72,7 @@ protected: bool sentByMe(const Shared::Message& msg) const; Attachment fillAttach(const Shared::Message& msg) const; QModelIndex modelIndexById(const QString& id) const; + QModelIndex modelIndexByTime(const QString& id, const QDateTime& time) const; public: enum MessageRoles { diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 22c1051..05f8c87 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -444,28 +444,7 @@ void Squawk::accountMessage(const QString& account, const Shared::Message& data) void Squawk::changeMessage(const QString& account, const QString& jid, const QString& id, const QMap& data) { - Models::Roster::ElId eid({account, jid}); - bool found = false; - - if (currentConversation != 0 && currentConversation->getId() == eid) { - currentConversation->changeMessage(id, data); - QApplication::alert(this); - found = true; - } - - Conversations::iterator itr = conversations.find(eid); - if (itr != conversations.end()) { - Conversation* conv = itr->second; - conv->changeMessage(id, data); - if (!found && conv->isMinimized()) { - rosterModel.changeMessage(account, jid, id, data); - } - found = true; - } - - if (!found) { - rosterModel.changeMessage(account, jid, id, data); - } + rosterModel.changeMessage(account, jid, id, data); } void Squawk::notify(const QString& account, const Shared::Message& msg) diff --git a/ui/utils/comboboxdelegate.cpp b/ui/utils/comboboxdelegate.cpp index 7153405..4c96c79 100644 --- a/ui/utils/comboboxdelegate.cpp +++ b/ui/utils/comboboxdelegate.cpp @@ -37,7 +37,7 @@ QWidget* ComboboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewI { QComboBox *cb = new QComboBox(parent); - for (const std::pair pair : entries) { + for (const std::pair& pair : entries) { cb->addItem(pair.second, pair.first); } diff --git a/ui/utils/messagedelegate.cpp b/ui/utils/messagedelegate.cpp index ff5b1fb..83eaa42 100644 --- a/ui/utils/messagedelegate.cpp +++ b/ui/utils/messagedelegate.cpp @@ -179,6 +179,9 @@ QSize MessageDelegate::sizeHint(const QStyleOptionViewItem& option, const QModel break; case Models::ready: break; + case Models::errorDownload: + case Models::errorUpload: + break; } messageSize.rheight() += nickMetrics.lineSpacing(); @@ -306,10 +309,11 @@ QProgressBar * MessageDelegate::getBar(const Models::FeedItem& data) const if (result == 0) { result = new QProgressBar(); + result->setRange(0, 100); bars->insert(std::make_pair(data.id, result)); } - result->setValue(data.attach.progress); + result->setValue(data.attach.progress * 100); return result; } diff --git a/ui/utils/messagedelegate.h b/ui/utils/messagedelegate.h index 42c8ed5..56f9ebf 100644 --- a/ui/utils/messagedelegate.h +++ b/ui/utils/messagedelegate.h @@ -85,6 +85,8 @@ private: std::map* bars; std::set* idsToKeep; bool clearingWidgets; + + }; #endif // MESSAGEDELEGATE_H