diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index 7fc58bc..3b5d9ca 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -16,6 +16,7 @@ CONFIG += sailfishapp sailfishapp_i18n SOURCES += src/harbour-fernschreiber.cpp \ src/chatlistmodel.cpp \ + src/chatmodel.cpp \ src/tdlibreceiver.cpp \ src/tdlibwrapper.cpp @@ -76,6 +77,7 @@ INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \ HEADERS += \ src/chatlistmodel.h \ + src/chatmodel.h \ src/tdlibreceiver.h \ src/tdlibsecrets.h \ src/tdlibwrapper.h diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index ff91940..828a286 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -101,6 +101,7 @@ Page { isChannel = chatGroupInformation.is_channel; updateGroupStatusText(); } + tdLibWrapper.getChatHistory(chatInformation.id); chatPage.loading = false; } diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp new file mode 100644 index 0000000..ef9c066 --- /dev/null +++ b/src/chatmodel.cpp @@ -0,0 +1,37 @@ +#include "chatmodel.h" + +ChatModel::ChatModel(TDLibWrapper *tdLibWrapper) +{ + this->tdLibWrapper = tdLibWrapper; +} + +ChatModel::~ChatModel() +{ + +} + +int ChatModel::rowCount(const QModelIndex &) const +{ + return messages.size(); +} + +QVariant ChatModel::data(const QModelIndex &index, int role) const +{ + if(index.isValid() && role == Qt::DisplayRole) { + return QVariant(messages.value(index.row())); + } + return QVariant(); +} + +bool ChatModel::insertRows(int row, int count, const QModelIndex &parent) +{ + qDebug() << "[ChatModel] Inserting at " << row << ", row count: " << count; + beginInsertRows(parent, row, row + count - 1); + this->messages.insert(row, this->messagesToBeAdded); + this->messageIndexMap.clear(); + for (int i = 0; i < this->messages.size(); i++) { + this->messageIndexMap.insert(this->messages.at(i).toMap().value("id").toString(), i); + } + endInsertRows(); + return true; +} diff --git a/src/chatmodel.h b/src/chatmodel.h new file mode 100644 index 0000000..ba035a6 --- /dev/null +++ b/src/chatmodel.h @@ -0,0 +1,29 @@ +#ifndef CHATMODEL_H +#define CHATMODEL_H + +#include +#include +#include +#include "tdlibwrapper.h" + +class ChatModel : public QAbstractListModel +{ + Q_OBJECT +public: + ChatModel(TDLibWrapper *tdLibWrapper); + ~ChatModel() override; + + virtual int rowCount(const QModelIndex&) const override; + virtual QVariant data(const QModelIndex &index, int role) const override; + virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + +private: + + TDLibWrapper *tdLibWrapper; + QVariantList messages; + QVariantList messagesToBeAdded; + QVariantMap messageIndexMap; + QMutex chatListMutex; +}; + +#endif // CHATMODEL_H diff --git a/src/harbour-fernschreiber.cpp b/src/harbour-fernschreiber.cpp index 7ce5eb6..886dfa5 100644 --- a/src/harbour-fernschreiber.cpp +++ b/src/harbour-fernschreiber.cpp @@ -31,6 +31,7 @@ #include "tdlibwrapper.h" #include "chatlistmodel.h" +#include "chatmodel.h" int main(int argc, char *argv[]) { @@ -46,6 +47,9 @@ int main(int argc, char *argv[]) ChatListModel chatListModel(tdLibWrapper); context->setContextProperty("chatListModel", &chatListModel); + ChatModel chatModel(tdLibWrapper); + context->setContextProperty("chatModel", &chatModel); + view->setSource(SailfishApp::pathTo("qml/harbour-fernschreiber.qml")); view->show(); return app->exec(); diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index e5df4e6..d2f35b9 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -69,6 +69,7 @@ void TDLibReceiver::processReceivedDocument(const QJsonDocument &receivedJsonDoc if (objectTypeName == "updateBasicGroup") { this->processUpdateBasicGroup(receivedInformation); } if (objectTypeName == "updateSupergroup") { this->processUpdateSuperGroup(receivedInformation); } if (objectTypeName == "updateChatOnlineMemberCount") { this->processChatOnlineMemberCountUpdated(receivedInformation); } + if (objectTypeName == "messages") { this->processMessages(receivedInformation); } } void TDLibReceiver::processUpdateOption(const QVariantMap &receivedInformation) @@ -190,3 +191,9 @@ void TDLibReceiver::processChatOnlineMemberCountUpdated(const QVariantMap &recei qDebug() << "[TDLibReceiver] Online member count updated for chat " << chatId; emit chatOnlineMemberCountUpdated(chatId, receivedInformation.value("online_member_count").toInt()); } + +void TDLibReceiver::processMessages(const QVariantMap &receivedInformation) +{ + qDebug() << "[TDLibReceiver] Received new messages..." << receivedInformation.value("total_count").toString(); + emit messagesReceived(receivedInformation.value("messages").toList()); +} diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index a7e9219..d8791d3 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -52,6 +52,7 @@ signals: void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount); + void messagesReceived(const QVariantList &messages); private: void *tdLibClient; @@ -74,6 +75,7 @@ private: void processUpdateBasicGroup(const QVariantMap &receivedInformation); void processUpdateSuperGroup(const QVariantMap &receivedInformation); void processChatOnlineMemberCountUpdated(const QVariantMap &receivedInformation); + void processMessages(const QVariantMap &receivedInformation); }; #endif // TDLIBRECEIVER_H diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 74eed1a..9afd2d6 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -52,6 +52,7 @@ TDLibWrapper::TDLibWrapper(QObject *parent) : QObject(parent) connect(this->tdLibReceiver, SIGNAL(basicGroupUpdated(QString, QVariantMap)), this, SLOT(handleBasicGroupUpdated(QString, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(superGroupUpdated(QString, QVariantMap)), this, SLOT(handleSuperGroupUpdated(QString, QVariantMap))); connect(this->tdLibReceiver, SIGNAL(chatOnlineMemberCountUpdated(QString, int)), this, SLOT(handleChatOnlineMemberCountUpdated(QString, int))); + connect(this->tdLibReceiver, SIGNAL(messagesReceived(QVariantList)), this, SLOT(handleMessagesReceived(QVariantList))); this->tdLibReceiver->start(); @@ -152,6 +153,19 @@ void TDLibWrapper::closeChat(const QString &chatId) this->sendRequest(requestObject); } +void TDLibWrapper::getChatHistory(const QString &chatId, const qlonglong &fromMessageId, const int &offset, const int &limit, const bool &onlyLocal) +{ + qDebug() << "[TDLibWrapper] Retrieving chat history " << chatId << fromMessageId << offset << limit << onlyLocal; + QVariantMap requestObject; + requestObject.insert("@type", "getChatHistory"); + requestObject.insert("chat_id", chatId); + requestObject.insert("from_message_id", fromMessageId); + requestObject.insert("offset", offset); + requestObject.insert("limit", limit); + requestObject.insert("only_local", onlyLocal); + this->sendRequest(requestObject); +} + QVariantMap TDLibWrapper::getUserInformation() { return this->userInformation; @@ -358,6 +372,11 @@ void TDLibWrapper::handleChatOnlineMemberCountUpdated(const QString &chatId, con emit chatOnlineMemberCountUpdated(chatId, onlineMemberCount); } +void TDLibWrapper::handleMessagesReceived(const QVariantList &messages) +{ + emit messagesReceived(messages); +} + void TDLibWrapper::setInitialParameters() { qDebug() << "[TDLibWrapper] Sending initial parameters to TD Lib"; diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index e9654ca..d9515fa 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -76,6 +76,7 @@ public: Q_INVOKABLE void downloadFile(const QString &fileId); Q_INVOKABLE void openChat(const QString &chatId); Q_INVOKABLE void closeChat(const QString &chatId); + Q_INVOKABLE void getChatHistory(const QString &chatId, const qlonglong &fromMessageId = 0, const int &offset = 0, const int &limit = 50, const bool &onlyLocal = false); signals: void versionDetected(const QString &version); @@ -94,6 +95,7 @@ signals: void basicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void superGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void chatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount); + void messagesReceived(const QVariantList &messages); public slots: void handleVersionDetected(const QString &version); @@ -112,6 +114,7 @@ public slots: void handleBasicGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void handleSuperGroupUpdated(const QString &groupId, const QVariantMap &groupInformation); void handleChatOnlineMemberCountUpdated(const QString &chatId, const int &onlineMemberCount); + void handleMessagesReceived(const QVariantList &messages); private: void *tdLibClient;