First attemtps to upload files, debug, reused of once uploaded or downloaded files

This commit is contained in:
Blue 2021-04-23 14:53:48 +03:00
parent d936c0302d
commit 8310708c92
7 changed files with 59 additions and 32 deletions

View File

@ -253,7 +253,9 @@ void Core::MessageHandler::performSending(Shared::Message data)
{ {
QString jid = data.getPenPalJid(); QString jid = data.getPenPalJid();
QString id = data.getId(); QString id = data.getId();
QString oob = data.getOutOfBandUrl();
RosterItem* ri = acc->rh->getRosterItem(jid); RosterItem* ri = acc->rh->getRosterItem(jid);
QMap<QString, QVariant> changes;
if (acc->state == Shared::ConnectionState::connected) { if (acc->state == Shared::ConnectionState::connected) {
QXmppMessage msg(acc->getFullJid(), data.getTo(), data.getBody(), data.getThread()); QXmppMessage msg(acc->getFullJid(), data.getTo(), data.getBody(), data.getThread());
@ -262,7 +264,7 @@ void Core::MessageHandler::performSending(Shared::Message data)
#endif #endif
msg.setId(id); msg.setId(id);
msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible msg.setType(static_cast<QXmppMessage::Type>(data.getType())); //it is safe here, my type is compatible
msg.setOutOfBandUrl(data.getOutOfBandUrl()); msg.setOutOfBandUrl(oob);
msg.setReceiptRequested(true); msg.setReceiptRequested(true);
bool sent = acc->client.sendPacket(msg); bool sent = acc->client.sendPacket(msg);
@ -286,10 +288,16 @@ void Core::MessageHandler::performSending(Shared::Message data)
data.setErrorText("You are is offline or reconnecting"); data.setErrorText("You are is offline or reconnecting");
} }
emit acc->changeMessage(jid, id, { Shared::Message::State mstate = data.getState();
{"state", static_cast<uint>(data.getState())}, changes.insert("state", static_cast<uint>(mstate));
{"errorText", data.getErrorText()} if (mstate == Shared::Message::State::error) {
}); changes.insert("errorText", data.getErrorText());
}
if (oob.size() > 0) {
changes.insert("outOfBandUrl", oob);
}
emit acc->changeMessage(jid, id, changes);
} }
void Core::MessageHandler::prepareUpload(const Shared::Message& data) void Core::MessageHandler::prepareUpload(const Shared::Message& data)

View File

@ -373,7 +373,7 @@ QString Core::NetworkAccess::getFileRemoteUrl(const QString& path)
QString p; QString p;
try { try {
QString p = storage.getUrl(path); p = storage.getUrl(path);
} catch (const Archive::NotFound& err) { } catch (const Archive::NotFound& err) {
} catch (...) { } catch (...) {

View File

@ -46,6 +46,7 @@ public:
delivered, delivered,
error error
}; };
static const State StateHighest = State::error; static const State StateHighest = State::error;
static const State StateLowest = State::pending; static const State StateLowest = State::pending;

View File

@ -86,7 +86,7 @@ void Models::MessageFeed::changeMessage(const QString& id, const QMap<QString, Q
} }
Shared::Message* msg = *itr; Shared::Message* msg = *itr;
QVector<int> changeRoles = detectChanges(*msg, data); std::set<MessageRoles> changeRoles = detectChanges(*msg, data);
QModelIndex index = modelIndexByTime(id, msg->getTime()); QModelIndex index = modelIndexByTime(id, msg->getTime());
Shared::Message::Change functor(data); Shared::Message::Change functor(data);
bool success = indexById.modify(itr, functor); bool success = indexById.modify(itr, functor);
@ -96,55 +96,69 @@ void Models::MessageFeed::changeMessage(const QString& id, const QMap<QString, Q
} }
if (functor.hasIdBeenModified()) { if (functor.hasIdBeenModified()) {
changeRoles.push_back(MessageRoles::Id); changeRoles.insert(MessageRoles::Id);
} }
//change message is a final event in download/upload event train //change message is a final event in download/upload event train
//only after changeMessage we can consider the download is done //only after changeMessage we can consider the download is done
Progress::const_iterator dItr = downloads.find(id); Progress::const_iterator dItr = downloads.find(id);
bool attachOrError = changeRoles.count(MessageRoles::Attach) > 0 || changeRoles.count(MessageRoles::Error);
if (dItr != downloads.end()) { if (dItr != downloads.end()) {
if (dItr->second == 1) { if (attachOrError) {
downloads.erase(dItr); downloads.erase(dItr);
} else if (changeRoles.count(MessageRoles::Id) > 0) {
qreal progress = dItr->second;
downloads.erase(dItr);
downloads.insert(std::make_pair(msg->getId(), progress));
} }
} else { } else {
dItr = uploads.find(id); dItr = uploads.find(id);
if (dItr != uploads.end()) { if (dItr != uploads.end()) {
if (dItr->second == 1) { if (attachOrError) {
uploads.erase(dItr); uploads.erase(dItr);
} else if (changeRoles.count(MessageRoles::Id) > 0) {
qreal progress = dItr->second;
uploads.erase(dItr);
uploads.insert(std::make_pair(msg->getId(), progress));
} }
} }
} }
emit dataChanged(index, index, changeRoles); QVector<int> cr;
for (MessageRoles role : changeRoles) {
cr.push_back(role);
} }
QVector<int> Models::MessageFeed::detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const emit dataChanged(index, index, cr);
}
std::set<Models::MessageFeed::MessageRoles> Models::MessageFeed::detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const
{ {
QVector<int> roles; std::set<MessageRoles> roles;
Shared::Message::State state = msg.getState(); Shared::Message::State state = msg.getState();
QMap<QString, QVariant>::const_iterator itr = data.find("state"); QMap<QString, QVariant>::const_iterator itr = data.find("state");
if (itr != data.end() && static_cast<Shared::Message::State>(itr.value().toUInt()) != state) { if (itr != data.end() && static_cast<Shared::Message::State>(itr.value().toUInt()) != state) {
roles.push_back(MessageRoles::DeliveryState); roles.insert(MessageRoles::DeliveryState);
} }
itr = data.find("outOfBandUrl"); itr = data.find("outOfBandUrl");
bool att = false; bool att = false;
if (itr != data.end() && itr.value().toString() != msg.getOutOfBandUrl()) { if (itr != data.end() && itr.value().toString() != msg.getOutOfBandUrl()) {
roles.push_back(MessageRoles::Attach); roles.insert(MessageRoles::Attach);
att = true; att = true;
} }
if (!att) { if (!att) {
itr = data.find("attachPath"); itr = data.find("attachPath");
if (itr != data.end() && itr.value().toString() != msg.getAttachPath()) { if (itr != data.end() && itr.value().toString() != msg.getAttachPath()) {
roles.push_back(MessageRoles::Attach); roles.insert(MessageRoles::Attach);
} }
} }
if (state == Shared::Message::State::error) { if (state == Shared::Message::State::error) {
itr = data.find("errorText"); itr = data.find("errorText");
if (itr != data.end()) { if (itr != data.end()) {
roles.push_back(MessageRoles::Error); roles.insert(MessageRoles::Error);
} }
} }
@ -158,8 +172,8 @@ QVector<int> Models::MessageFeed::detectChanges(const Shared::Message& msg, cons
correctionDate = QDateTime::currentDateTimeUtc(); //in case there is no information about time of this correction it's applied correctionDate = QDateTime::currentDateTimeUtc(); //in case there is no information about time of this correction it's applied
} }
if (!msg.getEdited() || msg.getLastModified() < correctionDate) { if (!msg.getEdited() || msg.getLastModified() < correctionDate) {
roles.push_back(MessageRoles::Text); roles.insert(MessageRoles::Text);
roles.push_back(MessageRoles::Correction); roles.insert(MessageRoles::Correction);
} }
} }
@ -410,6 +424,11 @@ 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;
} }
bool Models::MessageFeed::registerUpload(const QString& messageId)
{
return uploads.insert(std::make_pair(messageId, 0)).second;
}
void Models::MessageFeed::fileProgress(const QString& messageId, qreal value, bool up) void Models::MessageFeed::fileProgress(const QString& messageId, qreal value, bool up)
{ {
Progress* pr = 0; Progress* pr = 0;

View File

@ -23,6 +23,8 @@
#include <QDateTime> #include <QDateTime>
#include <QString> #include <QString>
#include <set>
#include <boost/multi_index_container.hpp> #include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/ranked_index.hpp> #include <boost/multi_index/ranked_index.hpp>
@ -57,6 +59,7 @@ public:
void responseArchive(const std::list<Shared::Message> list, bool last); void responseArchive(const std::list<Shared::Message> list, bool last);
void downloadAttachment(const QString& messageId); void downloadAttachment(const QString& messageId);
void uploadAttachment(const QString& messageId); void uploadAttachment(const QString& messageId);
bool registerUpload(const QString& messageId);
unsigned int unreadMessagesCount() const; unsigned int unreadMessagesCount() const;
void fileProgress(const QString& messageId, qreal value, bool up); void fileProgress(const QString& messageId, qreal value, bool up);
@ -68,13 +71,6 @@ signals:
void requestStateChange(bool requesting); void requestStateChange(bool requesting);
void fileDownloadRequest(const QString& url); void fileDownloadRequest(const QString& url);
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;
QVector<int> detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const;
public: public:
enum MessageRoles { enum MessageRoles {
Text = Qt::UserRole + 1, Text = Qt::UserRole + 1,
@ -90,6 +86,13 @@ public:
Bulk Bulk
}; };
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;
std::set<MessageRoles> detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const;
private: private:
enum SyncState { enum SyncState {
incomplete, incomplete,

View File

@ -33,6 +33,7 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el,
QWidget(parent), QWidget(parent),
isMuc(muc), isMuc(muc),
account(acc), account(acc),
element(el),
palJid(pJid), palJid(pJid),
activePalResource(pRes), activePalResource(pRes),
m_ui(new Ui::Conversation()), m_ui(new Ui::Conversation()),
@ -162,11 +163,6 @@ QString Conversation::getJid() const
return palJid; return palJid;
} }
void Conversation::changeMessage(const QString& id, const QMap<QString, QVariant>& data)
{
// line->changeMessage(id, data);
}
KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {} KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {}
bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event) bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
@ -220,6 +216,7 @@ void Conversation::onEnterPressed()
for (Badge* badge : filesToAttach) { for (Badge* badge : filesToAttach) {
Shared::Message msg = createMessage(); Shared::Message msg = createMessage();
msg.setAttachPath(badge->id); msg.setAttachPath(badge->id);
element->feed->registerUpload(msg.getId());
emit sendMessage(msg); emit sendMessage(msg);
} }
clearAttachedFiles(); clearAttachedFiles();

View File

@ -73,7 +73,6 @@ public:
void fileError(const QString& messageId, const QString& error); void fileError(const QString& messageId, const QString& error);
void responseFileProgress(const QString& messageId, qreal progress); void responseFileProgress(const QString& messageId, qreal progress);
virtual void setAvatar(const QString& path); virtual void setAvatar(const QString& path);
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
void setFeedFrames(bool top, bool right, bool bottom, bool left); void setFeedFrames(bool top, bool right, bool bottom, bool left);
signals: signals: