2020-08-11 22:49:51 +00:00
|
|
|
/*
|
|
|
|
* Squawk messenger.
|
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MESSAGEFEED_H
|
|
|
|
#define MESSAGEFEED_H
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QString>
|
|
|
|
|
2021-04-23 11:53:48 +00:00
|
|
|
#include <set>
|
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
#include <boost/multi_index_container.hpp>
|
|
|
|
#include <boost/multi_index/ordered_index.hpp>
|
|
|
|
#include <boost/multi_index/ranked_index.hpp>
|
|
|
|
#include <boost/multi_index/mem_fun.hpp>
|
|
|
|
|
|
|
|
#include <shared/message.h>
|
2020-08-20 21:32:30 +00:00
|
|
|
#include <shared/icons.h>
|
2020-08-11 22:49:51 +00:00
|
|
|
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
namespace Models {
|
|
|
|
class Element;
|
2021-01-14 11:22:02 +00:00
|
|
|
struct Attachment;
|
2021-05-24 22:06:05 +00:00
|
|
|
struct Edition;
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
class MessageFeed : public QAbstractListModel
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2021-04-30 20:07:00 +00:00
|
|
|
public:
|
|
|
|
enum SyncState {
|
|
|
|
incomplete,
|
|
|
|
syncing,
|
|
|
|
complete
|
|
|
|
};
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
MessageFeed(const Element* rosterItem, QObject *parent = nullptr);
|
2020-08-11 22:49:51 +00:00
|
|
|
~MessageFeed();
|
|
|
|
|
|
|
|
void addMessage(const Shared::Message& msg);
|
2021-04-19 21:49:24 +00:00
|
|
|
void changeMessage(const QString& id, const QMap<QString, QVariant>& data);
|
2020-08-11 22:49:51 +00:00
|
|
|
void removeMessage(const QString& id);
|
|
|
|
|
|
|
|
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override;
|
|
|
|
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
|
|
|
|
|
|
|
bool canFetchMore(const QModelIndex & parent) const override;
|
|
|
|
void fetchMore(const QModelIndex & parent) override;
|
2020-08-12 16:55:01 +00:00
|
|
|
QHash<int, QByteArray> roleNames() const override;
|
2021-05-04 14:09:41 +00:00
|
|
|
QModelIndex index(int row, int column, const QModelIndex & parent) const override;
|
2020-08-12 16:55:01 +00:00
|
|
|
|
2020-08-21 20:57:48 +00:00
|
|
|
void responseArchive(const std::list<Shared::Message> list, bool last);
|
2021-02-06 11:02:42 +00:00
|
|
|
void downloadAttachment(const QString& messageId);
|
2021-04-23 11:53:48 +00:00
|
|
|
bool registerUpload(const QString& messageId);
|
2021-04-28 20:26:19 +00:00
|
|
|
void reportLocalPathInvalid(const QString& messageId);
|
2020-08-11 22:49:51 +00:00
|
|
|
|
|
|
|
unsigned int unreadMessagesCount() const;
|
2021-04-18 12:49:20 +00:00
|
|
|
void fileProgress(const QString& messageId, qreal value, bool up);
|
|
|
|
void fileError(const QString& messageId, const QString& error, bool up);
|
|
|
|
void fileComplete(const QString& messageId, bool up);
|
2020-08-11 22:49:51 +00:00
|
|
|
|
2021-04-27 19:29:15 +00:00
|
|
|
void incrementObservers();
|
|
|
|
void decrementObservers();
|
2021-04-30 20:07:00 +00:00
|
|
|
SyncState getSyncState() const;
|
2021-04-27 19:29:15 +00:00
|
|
|
|
2021-05-16 21:52:59 +00:00
|
|
|
void requestLatestMessages(); //this method is used by Models::Contact to request latest messages after reconnection
|
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
signals:
|
|
|
|
void requestArchive(const QString& before);
|
2021-04-18 12:49:20 +00:00
|
|
|
void fileDownloadRequest(const QString& url);
|
2021-05-16 21:52:59 +00:00
|
|
|
void unreadMessagesCountChanged() const;
|
2021-04-27 19:29:15 +00:00
|
|
|
void newMessage(const Shared::Message& msg);
|
|
|
|
void unnoticedMessage(const Shared::Message& msg);
|
2021-04-28 20:26:19 +00:00
|
|
|
void localPathInvalid(const QString& path);
|
2021-04-30 20:07:00 +00:00
|
|
|
void syncStateChange(SyncState state);
|
2020-08-20 21:32:30 +00:00
|
|
|
|
2020-08-12 16:55:01 +00:00
|
|
|
public:
|
|
|
|
enum MessageRoles {
|
|
|
|
Text = Qt::UserRole + 1,
|
|
|
|
Sender,
|
|
|
|
Date,
|
|
|
|
DeliveryState,
|
|
|
|
Correction,
|
2020-08-20 21:32:30 +00:00
|
|
|
SentByMe,
|
2021-01-07 21:50:12 +00:00
|
|
|
Avatar,
|
|
|
|
Attach,
|
2021-02-01 22:55:15 +00:00
|
|
|
Id,
|
2021-04-22 22:41:32 +00:00
|
|
|
Error,
|
2021-01-07 21:50:12 +00:00
|
|
|
Bulk
|
|
|
|
};
|
|
|
|
|
2021-04-23 11:53:48 +00:00
|
|
|
protected:
|
|
|
|
bool sentByMe(const Shared::Message& msg) const;
|
|
|
|
Attachment fillAttach(const Shared::Message& msg) const;
|
2021-05-24 22:06:05 +00:00
|
|
|
Edition fillCorrection(const Shared::Message& msg) const;
|
2021-04-23 11:53:48 +00:00
|
|
|
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;
|
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
private:
|
|
|
|
//tags
|
|
|
|
struct id {};
|
|
|
|
struct time {};
|
|
|
|
|
|
|
|
typedef boost::multi_index_container<
|
|
|
|
Shared::Message*,
|
|
|
|
boost::multi_index::indexed_by<
|
|
|
|
boost::multi_index::ordered_unique<
|
|
|
|
boost::multi_index::tag<id>,
|
|
|
|
boost::multi_index::const_mem_fun<
|
|
|
|
Shared::Message,
|
|
|
|
QString,
|
|
|
|
&Shared::Message::getId
|
|
|
|
>
|
|
|
|
>,
|
|
|
|
boost::multi_index::ranked_non_unique<
|
|
|
|
boost::multi_index::tag<time>,
|
|
|
|
boost::multi_index::const_mem_fun<
|
|
|
|
Shared::Message,
|
|
|
|
QDateTime,
|
|
|
|
&Shared::Message::getTime
|
2020-08-12 16:55:01 +00:00
|
|
|
>,
|
|
|
|
std::greater<QDateTime>
|
2020-08-11 22:49:51 +00:00
|
|
|
>
|
|
|
|
>
|
|
|
|
> Storage;
|
|
|
|
|
|
|
|
typedef Storage::index<id>::type StorageById;
|
|
|
|
typedef Storage::index<time>::type StorageByTime;
|
|
|
|
Storage storage;
|
|
|
|
StorageById& indexById;
|
|
|
|
StorageByTime& indexByTime;
|
|
|
|
|
2020-08-20 21:32:30 +00:00
|
|
|
const Element* rosterItem;
|
2020-08-11 22:49:51 +00:00
|
|
|
SyncState syncState;
|
|
|
|
|
2021-01-14 11:22:02 +00:00
|
|
|
typedef std::map<QString, qreal> Progress;
|
2021-05-14 19:49:38 +00:00
|
|
|
typedef std::map<QString, QString> Err;
|
2021-01-14 11:22:02 +00:00
|
|
|
Progress uploads;
|
|
|
|
Progress downloads;
|
2021-05-14 19:49:38 +00:00
|
|
|
Err failedDownloads;
|
|
|
|
Err failedUploads;
|
2021-01-14 11:22:02 +00:00
|
|
|
|
2021-04-27 19:29:15 +00:00
|
|
|
std::set<QString>* unreadMessages;
|
|
|
|
uint16_t observersAmount;
|
|
|
|
|
2021-05-14 19:49:38 +00:00
|
|
|
|
2020-08-12 16:55:01 +00:00
|
|
|
static const QHash<int, QByteArray> roles;
|
2020-08-20 21:32:30 +00:00
|
|
|
};
|
2021-01-07 21:50:12 +00:00
|
|
|
|
2021-01-14 11:22:02 +00:00
|
|
|
enum AttachmentType {
|
2021-01-12 17:10:24 +00:00
|
|
|
none,
|
|
|
|
remote,
|
2021-01-14 11:22:02 +00:00
|
|
|
local,
|
2021-01-12 17:10:24 +00:00
|
|
|
downloading,
|
|
|
|
uploading,
|
2021-04-18 12:49:20 +00:00
|
|
|
errorDownload,
|
|
|
|
errorUpload,
|
2021-01-12 17:10:24 +00:00
|
|
|
ready
|
|
|
|
};
|
|
|
|
|
2021-01-14 11:22:02 +00:00
|
|
|
struct Attachment {
|
|
|
|
AttachmentType state;
|
2021-01-12 17:10:24 +00:00
|
|
|
qreal progress;
|
|
|
|
QString localPath;
|
2021-01-14 11:22:02 +00:00
|
|
|
QString remotePath;
|
2021-05-14 19:49:38 +00:00
|
|
|
QString error;
|
2021-01-12 17:10:24 +00:00
|
|
|
};
|
|
|
|
|
2021-05-24 22:06:05 +00:00
|
|
|
struct Edition {
|
|
|
|
bool corrected;
|
|
|
|
QString original;
|
|
|
|
QDateTime lastCorrection;
|
|
|
|
};
|
|
|
|
|
2021-01-07 21:50:12 +00:00
|
|
|
struct FeedItem {
|
2021-02-01 22:55:15 +00:00
|
|
|
QString id;
|
2021-01-07 21:50:12 +00:00
|
|
|
QString text;
|
|
|
|
QString sender;
|
|
|
|
QString avatar;
|
2021-04-22 22:41:32 +00:00
|
|
|
QString error;
|
2021-01-07 21:50:12 +00:00
|
|
|
bool sentByMe;
|
2021-05-24 22:06:05 +00:00
|
|
|
Edition correction;
|
2021-01-07 21:50:12 +00:00
|
|
|
QDateTime date;
|
|
|
|
Shared::Message::State state;
|
2021-01-14 11:22:02 +00:00
|
|
|
Attachment attach;
|
2021-01-07 21:50:12 +00:00
|
|
|
};
|
2020-08-11 22:49:51 +00:00
|
|
|
};
|
|
|
|
|
2021-01-14 11:22:02 +00:00
|
|
|
Q_DECLARE_METATYPE(Models::Attachment);
|
2021-05-24 22:06:05 +00:00
|
|
|
Q_DECLARE_METATYPE(Models::Edition);
|
2021-01-07 21:50:12 +00:00
|
|
|
Q_DECLARE_METATYPE(Models::FeedItem);
|
|
|
|
|
2020-08-11 22:49:51 +00:00
|
|
|
#endif // MESSAGEFEED_H
|