Commit 3aec112f authored by Martin Klapetek's avatar Martin Klapetek Committed by Alexander Akulich
Browse files

[declarative] Allow the view requesting more history to be fetched


Reviewed-by: Alexander Akulich's avatarAlexander Akulich <akulichalexander@gmail.com>
parent 7e824024
......@@ -180,16 +180,36 @@ void MessagesModel::setAccount(const Tp::AccountPtr &account)
void MessagesModel::onHistoryFetched(const QList<KTp::Message> &messages)
{
if (!messages.isEmpty()) {
QList<KTp::Message> messagesToAdd;
// Make sure we're not adding duplicated messages to the model
if (!d->messages.isEmpty()) {
int i = 0;
for (i = 0; i < messages.size(); i++) {
if (messages.at(i) == d->messages.at(0).message) {
break;
}
}
messagesToAdd = messages.mid(0, i);
} else {
messagesToAdd = messages;
}
if (!messagesToAdd.isEmpty()) {
//Add all messages before the ones already present in the channel
beginInsertRows(QModelIndex(), 0, messages.count() - 1);
for(int i=messages.size()-1;i>=0;i--) {
d->messages.prepend(messages[i]);
beginInsertRows(QModelIndex(), 0, messagesToAdd.count() - 1);
for (int i = messagesToAdd.size() - 1; i >= 0; i--) {
d->messages.prepend(messagesToAdd[i]);
}
endInsertRows();
}
d->logsLoaded = true;
// Emit changed for the first message after the prepended
// logs, to make sure the bubble shape is updated
// through PreviousMessageTypeRole
QModelIndex index = createIndex(messagesToAdd.count(), 0);
Q_EMIT dataChanged(index, index);
Q_EMIT lastMessageChanged();
}
......@@ -473,3 +493,19 @@ QDateTime MessagesModel::lastMessageDateTime() const
return data(index, MessagesModel::TimeRole).toDateTime();
}
void MessagesModel::fetchMoreHistory()
{
if (d->messages.isEmpty() || !d->logsLoaded) {
return;
}
d->logsLoaded = false;
const KTp::Message message = d->messages.at(0).message;
const QString token = message.token().isEmpty() ? message.time().toString(Qt::ISODate) + message.mainMessagePart()
: message.token();
d->logManager->setScrollbackLength(10);
d->logManager->fetchHistory(rowCount() + 10, token);
}
......@@ -88,12 +88,14 @@ class MessagesModel : public QAbstractListModel
QString lastMessage() const;
QDateTime lastMessageDateTime() const;
Q_SIGNALS:
void visibleToUserChanged(bool visible);
void unreadCountChanged(int unreadMesssagesCount);
void lastMessageChanged();
public Q_SLOTS:
void fetchMoreHistory();
void sendNewMessage(const QString &message);
private Q_SLOTS:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment