Skip to content

Commit

Permalink
Working on messages features
Browse files Browse the repository at this point in the history
  • Loading branch information
curoviyxru committed Sep 1, 2023
1 parent d87fe0b commit d86ef5d
Show file tree
Hide file tree
Showing 12 changed files with 500 additions and 51 deletions.
52 changes: 47 additions & 5 deletions dialogsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,25 @@ void DialogsModel::messagesGetDialogsResponse(TgObject data, TgLongVariant messa
}
}

dialogsRows.append(createRow(lastDialog, lastPeer, lastMessage));
TgObject fromId = lastMessage["from_id"].toMap();
TgObject messageSender;

if (TgClient::isUser(fromId)) for (qint32 j = 0; j < usersList.size(); ++j) {
TgObject peer = usersList[j].toMap();
if (TgClient::peersEqual(peer, fromId)) {
messageSender = peer;
break;
}
}
if (TgClient::isChat(fromId)) for (qint32 j = 0; j < chatsList.size(); ++j) {
TgObject peer = chatsList[j].toMap();
if (TgClient::peersEqual(peer, fromId)) {
messageSender = peer;
break;
}
}

dialogsRows.append(createRow(lastDialog, lastPeer, lastMessage, messageSender));
}

beginInsertRows(QModelIndex(), _dialogs.size(), _dialogs.size() + dialogsRows.size() - 1);
Expand Down Expand Up @@ -208,11 +226,11 @@ QColor userColor(qint64 id)
return QColor::fromHsl(id % 360, 160, 160);
}

TgObject DialogsModel::createRow(TgObject dialog, TgObject peer, TgObject message)
TgObject DialogsModel::createRow(TgObject dialog, TgObject peer, TgObject message, TgObject messageSender)
{
TgObject row;

TgObject inputPeer = TgClient::toInputPeer(peer);
TgObject inputPeer = peer;
row["inputPeer"] = inputPeer;

inputPeer["read_inbox_max_id"] = dialog["read_inbox_max_id"];
Expand All @@ -224,7 +242,7 @@ TgObject DialogsModel::createRow(TgObject dialog, TgObject peer, TgObject messag
row["peerBytes"] = array;

if (TgClient::isUser(peer)) {
row["title"] = peer["first_name"].toString() + " " + peer["last_name"].toString();
row["title"] = QString(peer["first_name"].toString() + " " + peer["last_name"].toString());
} else {
row["title"] = peer["title"].toString();
}
Expand All @@ -234,7 +252,31 @@ TgObject DialogsModel::createRow(TgObject dialog, TgObject peer, TgObject messag
row["avatarLoaded"] = false;

row["messageTime"] = QDateTime::fromTime_t(qMax(message["date"].toInt(), message["edit_date"].toInt())).toString("hh:mm");
row["messageText"] = message["message"].toString().replace('\n', " ");

QString messageSenderName;

if (TgClient::commonPeerType(messageSender) == 0) {
//This means that it is a channel feed or personal messages.
//Authorized user isn't returned by API, so we have to deal with it.
if (message["out"].toBool()) {
messageSenderName = "You";
}
//else messageSender = peer;
}

if (TgClient::isUser(messageSender)) {
messageSenderName = messageSender["first_name"].toString();
} else {
messageSenderName = messageSender["title"].toString();
}

if (!messageSenderName.isEmpty()) {
messageSenderName += ": ";
}

messageSenderName += message["message"].toString().replace('\n', " ");

row["messageText"] = messageSenderName;

TgObject photo = peer["photo"].toMap();
if (GETID(photo)) {
Expand Down
2 changes: 1 addition & 1 deletion dialogsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DialogsModel : public QAbstractListModel
bool canFetchMore(const QModelIndex &parent) const;
void fetchMore(const QModelIndex &parent);

TgObject createRow(TgObject dialog, TgObject peer, TgObject message);
TgObject createRow(TgObject dialog, TgObject peer, TgObject message, TgObject messageSender);

signals:

Expand Down
31 changes: 10 additions & 21 deletions kutegramquick.pro
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
# Add more folders to ship with the application, here
QT += core declarative network xml
DEFINES += QT_USE_FAST_CONCATENATION QT_USE_FAST_OPERATOR_PLUS
VERSION = 1.0.0
CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT

# TODO Migrate to QRC
folder_01.source = qml
folder_01.target = .
folder_02.source = img
folder_02.target = .
DEPLOYMENTFOLDERS = folder_01 folder_02

# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =

symbian:TARGET.UID3 = 0xE607720E

# Smart Installer package's UID
# This UID is from the protected range and therefore the package will
# fail to install if self-signed. By default qmake uses the unprotected
# range value if unprotected UID is defined for the application and
# 0x2002CCCF value if protected UID is given to the application
#symbian:DEPLOYMENT.installer_header = 0x2002CCCF

# Allow network access on Symbian
symbian:TARGET.CAPABILITY += NetworkServices

# If your application uses the Qt Mobility libraries, uncomment the following
# lines and add the respective components to the MOBILITY variable.
# CONFIG += mobility
# MOBILITY +=
symbian:TARGET.CAPABILITY += NetworkServices ReadUserData WriteUserData

# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp \
dialogsmodel.cpp \
messagesmodel.cpp
Expand All @@ -34,10 +24,6 @@ HEADERS += \
dialogsmodel.h \
messagesmodel.h

# Please do not modify the following two lines. Required for deployment.
include(qmlapplicationviewer/qmlapplicationviewer.pri)
qtcAddDeployment()

OTHER_FILES += \
qtc_packaging/debian_harmattan/rules \
qtc_packaging/debian_harmattan/README \
Expand All @@ -47,3 +33,6 @@ OTHER_FILES += \
qtc_packaging/debian_harmattan/changelog

include(libkg/libkg.pri)

include(qmlapplicationviewer/qmlapplicationviewer.pri)
qtcAddDeployment()
2 changes: 1 addition & 1 deletion libkg
Submodule libkg updated 3 files
+14 −0 tgc_utils.cpp
+1 −0 tgclient.h
+1 −1 tgtransport.cpp
9 changes: 8 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <QtGui/QApplication>
#include <QApplication>
#include "qmlapplicationviewer.h"

#include <QFontDatabase>
Expand All @@ -10,6 +10,11 @@

int main(int argc, char *argv[])
{
//TODO OpenGL acceleration
#if QT_VERSION < 0x050000
//QApplication::setGraphicsSystem("opengl");
#endif

QApplication app(argc, argv);

QApplication::setApplicationVersion("1.0.0");
Expand All @@ -18,8 +23,10 @@ int main(int argc, char *argv[])
QApplication::setOrganizationDomain("kg.crx.moe");

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
#if QT_VERSION < 0x050000
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForCStrings(codec);
#endif
QTextCodec::setCodecForLocale(codec);

TgClient::registerQML();
Expand Down
76 changes: 67 additions & 9 deletions messagesmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
#include <QMutexLocker>
#include <QColor>
#include <QDateTime>
#include <QUrl>
#include <QDomDocument>

MessagesModel::MessagesModel(QObject *parent)
: QAbstractListModel(parent)
, mutex(QMutex::Recursive)
, _client(0)
, _userId(0)
, _peer()
, _inputPeer()
, _upRequestId(0)
, _downRequestId(0)
, _upOffset(0)
Expand All @@ -34,6 +37,7 @@ void MessagesModel::resetState()
}

_peer = TgObject();
_inputPeer = TgObject();
_upRequestId = 0;
_downRequestId = 0;
_upOffset = 0;
Expand Down Expand Up @@ -75,6 +79,7 @@ void MessagesModel::setPeer(QByteArray bytes)
peerStream >> peer;

_peer = peer;
_inputPeer = TgClient::toInputPeer(peer);

_upOffset = _downOffset = qMax(peer["read_inbox_max_id"].toInt(), peer["read_outbox_max_id"].toInt());
fetchMoreUpwards();
Expand Down Expand Up @@ -104,36 +109,46 @@ QVariant MessagesModel::data(const QModelIndex &index, int role) const
TgObject curr = _history[index.row()];
TgObject prev = _history[index.row() - 1];

return (curr["grouped_id"] == prev["grouped_id"]
|| curr["date"].toInt() - prev["date"].toInt() < 300)
&& curr["sender"] == prev["sender"];
if (!TgClient::peersEqual(curr["sender"].toMap(), prev["sender"].toMap())) {
return false;
}

if (curr["grouped_id"].toLongLong() == prev["grouped_id"].toLongLong()) {
return true;
}

if (!TgClient::isChannel(_peer) && curr["date"].toInt() - prev["date"].toInt() < 300) {
return true;
}

return false;
}

return _history[index.row()][roleNames()[role]];
}

bool MessagesModel::canFetchMore(const QModelIndex &parent) const
{
return _client && _userId.toLongLong() && TgClient::commonPeerType(_peer) != 0 && !_downRequestId.toLongLong() && _downOffset != -1;
return _client && _userId.toLongLong() && TgClient::commonPeerType(_inputPeer) != 0 && !_downRequestId.toLongLong() && _downOffset != -1;
}

void MessagesModel::fetchMore(const QModelIndex &parent)
{
QMutexLocker lock(&mutex);

_downRequestId = _client->messagesGetHistory(_peer, _downOffset, 0, -20, 20);
_downRequestId = _client->messagesGetHistory(_inputPeer, _downOffset, 0, -20, 20);
}

bool MessagesModel::canFetchMoreUpwards() const
{
return _client && _userId.toLongLong() && TgClient::commonPeerType(_peer) != 0 && !_upRequestId.toLongLong() && _upOffset != -1;
return _client && _userId.toLongLong() && TgClient::commonPeerType(_inputPeer) != 0 && !_upRequestId.toLongLong() && _upOffset != -1;
}

void MessagesModel::fetchMoreUpwards()
{
QMutexLocker lock(&mutex);

_upRequestId = _client->messagesGetHistory(_peer, _upOffset, 0, 0, 20);
_upRequestId = _client->messagesGetHistory(_inputPeer, _upOffset, 0, 0, 20);
}

void MessagesModel::authorized(TgLongVariant userId)
Expand Down Expand Up @@ -197,6 +212,9 @@ void MessagesModel::handleHistoryResponse(TgObject data, TgLongVariant messageId
break;
}
}
if (TgClient::commonPeerType(fromId) == 0) {
sender = _peer;
}

messagesRows.append(createRow(message, sender));
}
Expand Down Expand Up @@ -251,6 +269,11 @@ void MessagesModel::handleHistoryResponseUpwards(TgObject data, TgLongVariant me
break;
}
}
if (TgClient::commonPeerType(fromId) == 0) {
//This means that it is a channel feed or personal messages.
//Authorized user is returned by API, so we don't need to put it manually.
sender = _peer;
}

messagesRows.append(createRow(message, sender));
}
Expand All @@ -277,16 +300,51 @@ TgObject MessagesModel::createRow(TgObject message, TgObject sender)
TgObject row;

if (TgClient::isUser(sender)) {
row["senderName"] = sender["first_name"].toString() + " " + sender["last_name"].toString();
row["senderName"] = QString(sender["first_name"].toString() + " " + sender["last_name"].toString());
} else {
row["senderName"] = sender["title"].toString();
}

row["date"] = message["date"];
row["grouped_id"] = message["grouped_id"];
row["messageTime"] = QDateTime::fromTime_t(qMax(message["date"].toInt(), message["edit_date"].toInt())).toString("hh:mm");
row["messageText"] = message["message"];
row["messageText"] = message["message"].toString();
row["sender"] = TgClient::toInputPeer(sender);

return row;
}

void MessagesModel::linkActivated(QString link, qint32 listIndex)
{
QMutexLocker lock(&mutex);

QUrl url(link);

if (url.scheme() == "kutegram") {
if (url.host() == "spoiler") {
TgObject listItem = _history[listIndex];
QDomDocument dom;
QString error;
int errorLine = 0, errorColumn = 0;
dom.setContent(listItem["messageText"].toString(), false, &error, &errorLine, &errorColumn);
//TODO remove this
kgDebug() << error << errorLine << errorColumn;
kgDebug() << listItem["messageText"].toString();

QDomNodeList list = dom.elementsByTagName("a");
for (qint32 i = 0; i < list.count(); ++i) {
QDomElement node = list.at(i).toElement();
if (node.attribute("href") == link) {
node.removeAttribute("href");
node.removeAttribute("style");
break;
}
}
listItem["messageText"] = dom.toString(-1);
_history[listIndex] = listItem;

emit dataChanged(index(listIndex), index(listIndex));
}
}
// TODO else openUrl(url);
}
3 changes: 3 additions & 0 deletions messagesmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MessagesModel : public QAbstractListModel
TgLongVariant _userId;

TgObject _peer;
TgObject _inputPeer;

TgLongVariant _upRequestId;
TgLongVariant _downRequestId;
Expand Down Expand Up @@ -64,6 +65,8 @@ public slots:

bool canFetchMoreUpwards() const;
void fetchMoreUpwards();

void linkActivated(QString link, qint32 index);
};

#endif // MESSAGESMODEL_H
Loading

0 comments on commit d86ef5d

Please sign in to comment.