forked from blue/squawk
handled a case when user removes downloaded file, minor optimizations on message changing
This commit is contained in:
parent
b44873d587
commit
50190f3eac
23 changed files with 136 additions and 40 deletions
|
@ -33,6 +33,7 @@ Models::Element::Element(Type p_type, const Models::Account* acc, const QString&
|
|||
connect(feed, &MessageFeed::fileDownloadRequest, this, &Element::fileDownloadRequest);
|
||||
connect(feed, &MessageFeed::unreadMessagesCountChanged, this, &Element::onFeedUnreadMessagesCountChanged);
|
||||
connect(feed, &MessageFeed::unnoticedMessage, this, &Element::onFeedUnnoticedMessage);
|
||||
connect(feed, &MessageFeed::localPathInvalid, this, &Element::localPathInvalid);
|
||||
|
||||
QMap<QString, QVariant>::const_iterator itr = data.find("avatarState");
|
||||
if (itr != data.end()) {
|
||||
|
|
|
@ -51,6 +51,7 @@ signals:
|
|||
void requestArchive(const QString& before);
|
||||
void fileDownloadRequest(const QString& url);
|
||||
void unnoticedMessage(const QString& account, const Shared::Message& msg);
|
||||
void localPathInvalid(const QString& path);
|
||||
|
||||
protected:
|
||||
void setJid(const QString& p_jid);
|
||||
|
|
|
@ -116,37 +116,39 @@ void Models::MessageFeed::changeMessage(const QString& id, const QMap<QString, Q
|
|||
}
|
||||
}
|
||||
|
||||
//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 (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 (changeRoles.size() > 0) {
|
||||
//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 (attachOrError) {
|
||||
uploads.erase(dItr);
|
||||
downloads.erase(dItr);
|
||||
} else if (changeRoles.count(MessageRoles::Id) > 0) {
|
||||
qreal progress = dItr->second;
|
||||
uploads.erase(dItr);
|
||||
uploads.insert(std::make_pair(msg->getId(), progress));
|
||||
downloads.erase(dItr);
|
||||
downloads.insert(std::make_pair(msg->getId(), progress));
|
||||
}
|
||||
} else {
|
||||
dItr = uploads.find(id);
|
||||
if (dItr != uploads.end()) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QVector<int> cr;
|
||||
for (MessageRoles role : changeRoles) {
|
||||
cr.push_back(role);
|
||||
}
|
||||
|
||||
emit dataChanged(index, index, cr);
|
||||
}
|
||||
|
||||
QVector<int> cr;
|
||||
for (MessageRoles role : changeRoles) {
|
||||
cr.push_back(role);
|
||||
}
|
||||
|
||||
emit dataChanged(index, index, cr);
|
||||
}
|
||||
|
||||
std::set<Models::MessageFeed::MessageRoles> Models::MessageFeed::detectChanges(const Shared::Message& msg, const QMap<QString, QVariant>& data) const
|
||||
|
@ -174,13 +176,13 @@ std::set<Models::MessageFeed::MessageRoles> Models::MessageFeed::detectChanges(c
|
|||
|
||||
if (state == Shared::Message::State::error) {
|
||||
itr = data.find("errorText");
|
||||
if (itr != data.end()) {
|
||||
if (itr != data.end() && itr.value().toString() != msg.getErrorText()) {
|
||||
roles.insert(MessageRoles::Error);
|
||||
}
|
||||
}
|
||||
|
||||
itr = data.find("body");
|
||||
if (itr != data.end()) {
|
||||
if (itr != data.end() && itr.value().toString() != msg.getBody()) {
|
||||
QMap<QString, QVariant>::const_iterator dItr = data.find("stamp");
|
||||
QDateTime correctionDate;
|
||||
if (dItr != data.end()) {
|
||||
|
@ -522,3 +524,22 @@ QModelIndex Models::MessageFeed::modelIndexByTime(const QString& id, const QDate
|
|||
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
void Models::MessageFeed::reportLocalPathInvalid(const QString& messageId)
|
||||
{
|
||||
StorageById::iterator itr = indexById.find(messageId);
|
||||
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;
|
||||
|
||||
emit localPathInvalid(msg->getAttachPath());
|
||||
|
||||
//gonna change the message in current model right away, to prevent spam on each attemt to draw element
|
||||
QModelIndex index = modelIndexByTime(messageId, msg->getTime());
|
||||
msg->setAttachPath("");
|
||||
|
||||
emit dataChanged(index, index, {MessageRoles::Attach});
|
||||
}
|
||||
|
|
|
@ -60,6 +60,7 @@ public:
|
|||
void downloadAttachment(const QString& messageId);
|
||||
void uploadAttachment(const QString& messageId);
|
||||
bool registerUpload(const QString& messageId);
|
||||
void reportLocalPathInvalid(const QString& messageId);
|
||||
|
||||
unsigned int unreadMessagesCount() const;
|
||||
void fileProgress(const QString& messageId, qreal value, bool up);
|
||||
|
@ -76,6 +77,7 @@ signals:
|
|||
void unreadMessagesCountChanged();
|
||||
void newMessage(const Shared::Message& msg);
|
||||
void unnoticedMessage(const Shared::Message& msg);
|
||||
void localPathInvalid(const QString& path);
|
||||
|
||||
public:
|
||||
enum MessageRoles {
|
||||
|
|
|
@ -449,6 +449,7 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons
|
|||
connect(contact, &Contact::requestArchive, this, &Roster::onElementRequestArchive);
|
||||
connect(contact, &Contact::fileDownloadRequest, this, &Roster::fileDownloadRequest);
|
||||
connect(contact, &Contact::unnoticedMessage, this, &Roster::unnoticedMessage);
|
||||
connect(contact, &Contact::localPathInvalid, this, &Roster::localPathInvalid);
|
||||
contacts.insert(std::make_pair(id, contact));
|
||||
} else {
|
||||
contact = itr->second;
|
||||
|
@ -787,6 +788,7 @@ void Models::Roster::addRoom(const QString& account, const QString jid, const QM
|
|||
connect(room, &Contact::requestArchive, this, &Roster::onElementRequestArchive);
|
||||
connect(room, &Contact::fileDownloadRequest, this, &Roster::fileDownloadRequest);
|
||||
connect(room, &Contact::unnoticedMessage, this, &Roster::unnoticedMessage);
|
||||
connect(room, &Contact::localPathInvalid, this, &Roster::localPathInvalid);
|
||||
rooms.insert(std::make_pair(id, room));
|
||||
acc->appendChild(room);
|
||||
}
|
||||
|
|
|
@ -93,6 +93,7 @@ signals:
|
|||
void requestArchive(const QString& account, const QString& jid, const QString& before);
|
||||
void fileDownloadRequest(const QString& url);
|
||||
void unnoticedMessage(const QString& account, const Shared::Message& msg);
|
||||
void localPathInvalid(const QString& path);
|
||||
|
||||
private:
|
||||
Element* getElement(const ElId& id);
|
||||
|
|
|
@ -64,6 +64,7 @@ Squawk::Squawk(QWidget *parent) :
|
|||
connect(rosterModel.accountsModel, &Models::Accounts::sizeChanged, this, &Squawk::onAccountsSizeChanged);
|
||||
connect(&rosterModel, &Models::Roster::requestArchive, this, &Squawk::onRequestArchive);
|
||||
connect(&rosterModel, &Models::Roster::fileDownloadRequest, this, &Squawk::fileDownloadRequest);
|
||||
connect(&rosterModel, &Models::Roster::localPathInvalid, this, &Squawk::localPathInvalid);
|
||||
connect(contextMenu, &QMenu::aboutToHide, this, &Squawk::onContextAboutToHide);
|
||||
//m_ui->mainToolBar->addWidget(m_ui->comboBox);
|
||||
|
||||
|
|
|
@ -79,6 +79,7 @@ signals:
|
|||
void requestVCard(const QString& account, const QString& jid);
|
||||
void uploadVCard(const QString& account, const Shared::VCard& card);
|
||||
void responsePassword(const QString& account, const QString& password);
|
||||
void localPathInvalid(const QString& path);
|
||||
|
||||
public slots:
|
||||
void readSettings();
|
||||
|
|
|
@ -304,6 +304,7 @@ void FeedView::setItemDelegate(QAbstractItemDelegate* delegate)
|
|||
if (specialDelegate) {
|
||||
MessageDelegate* del = static_cast<MessageDelegate*>(itemDelegate());
|
||||
disconnect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
|
||||
disconnect(del, &MessageDelegate::invalidPath, this, &FeedView::onMessageInvalidPath);
|
||||
}
|
||||
|
||||
QAbstractItemView::setItemDelegate(delegate);
|
||||
|
@ -312,6 +313,7 @@ void FeedView::setItemDelegate(QAbstractItemDelegate* delegate)
|
|||
if (del) {
|
||||
specialDelegate = true;
|
||||
connect(del, &MessageDelegate::buttonPushed, this, &FeedView::onMessageButtonPushed);
|
||||
connect(del, &MessageDelegate::invalidPath, this, &FeedView::onMessageInvalidPath);
|
||||
} else {
|
||||
specialDelegate = false;
|
||||
}
|
||||
|
@ -341,3 +343,12 @@ void FeedView::onMessageButtonPushed(const QString& messageId, bool download)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FeedView::onMessageInvalidPath(const QString& messageId)
|
||||
{
|
||||
if (specialModel) {
|
||||
Models::MessageFeed* feed = static_cast<Models::MessageFeed*>(model());
|
||||
feed->reportLocalPathInvalid(messageId);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,7 @@ protected slots:
|
|||
void verticalScrollbarValueChanged(int value) override;
|
||||
void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles) override;
|
||||
void onMessageButtonPushed(const QString& messageId, bool download);
|
||||
void onMessageInvalidPath(const QString& messageId);
|
||||
|
||||
protected:
|
||||
int verticalOffset() const override;
|
||||
|
|
|
@ -310,7 +310,11 @@ void MessageDelegate::paintPreview(const Models::FeedItem& data, QPainter* paint
|
|||
start = option.rect.topLeft();
|
||||
}
|
||||
QImage img(data.attach.localPath);
|
||||
painter->drawImage(QRect(start, size), img);
|
||||
if (img.isNull()) {
|
||||
emit invalidPath(data.id);
|
||||
} else {
|
||||
painter->drawImage(QRect(start, size), img);
|
||||
}
|
||||
|
||||
option.rect.adjust(0, size.height() + textMargin, 0, 0);
|
||||
}
|
||||
|
|
|
@ -55,6 +55,7 @@ public:
|
|||
|
||||
signals:
|
||||
void buttonPushed(const QString& messageId, bool download) const;
|
||||
void invalidPath(const QString& messageId) const;
|
||||
|
||||
protected:
|
||||
void paintButton(QPushButton* btn, QPainter* painter, bool sentByMe, QStyleOptionViewItem& option) const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue