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

@ -86,7 +86,7 @@ void Models::MessageFeed::changeMessage(const QString& id, const QMap<QString, Q
}
Shared::Message* msg = *itr;
QVector<int> changeRoles = detectChanges(*msg, data);
std::set<MessageRoles> changeRoles = detectChanges(*msg, data);
QModelIndex index = modelIndexByTime(id, msg->getTime());
Shared::Message::Change functor(data);
bool success = indexById.modify(itr, functor);
@ -96,55 +96,69 @@ void Models::MessageFeed::changeMessage(const QString& id, const QMap<QString, Q
}
if (functor.hasIdBeenModified()) {
changeRoles.push_back(MessageRoles::Id);
changeRoles.insert(MessageRoles::Id);
}
//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);
bool attachOrError = changeRoles.count(MessageRoles::Attach) > 0 || changeRoles.count(MessageRoles::Error);
if (dItr != downloads.end()) {
if (dItr->second == 1) {
if (attachOrError) {
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 {
dItr = uploads.find(id);
if (dItr != uploads.end()) {
if (dItr->second == 1) {
if (attachOrError) {
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);
}
emit dataChanged(index, index, cr);
}
QVector<int> Models::MessageFeed::detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const
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();
QMap<QString, QVariant>::const_iterator itr = data.find("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");
bool att = false;
if (itr != data.end() && itr.value().toString() != msg.getOutOfBandUrl()) {
roles.push_back(MessageRoles::Attach);
roles.insert(MessageRoles::Attach);
att = true;
}
if (!att) {
itr = data.find("attachPath");
if (itr != data.end() && itr.value().toString() != msg.getAttachPath()) {
roles.push_back(MessageRoles::Attach);
roles.insert(MessageRoles::Attach);
}
}
if (state == Shared::Message::State::error) {
itr = data.find("errorText");
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
}
if (!msg.getEdited() || msg.getLastModified() < correctionDate) {
roles.push_back(MessageRoles::Text);
roles.push_back(MessageRoles::Correction);
roles.insert(MessageRoles::Text);
roles.insert(MessageRoles::Correction);
}
}
@ -410,6 +424,11 @@ void Models::MessageFeed::uploadAttachment(const QString& 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)
{
Progress* pr = 0;