summary refs log tree commit diff
path: root/src/TimelineView.cc
blob: 00a425571638932c214a61df1a84d6b1ce4b6934 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * nheko Copyright (C) 2017  Konstantinos Sideris <siderisk@auth.gr>
 *
 * 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/>.
 */

#include <QDebug>
#include <QScrollBar>
#include <QSettings>
#include <QtWidgets/QLabel>
#include <QtWidgets/QSpacerItem>

#include "TimelineItem.h"
#include "TimelineView.h"
#include "TimelineViewManager.h"

TimelineView::TimelineView(const QList<Event> &events, QWidget *parent)
    : QWidget(parent)
{
	init();
	addEvents(events);
}

TimelineView::TimelineView(QWidget *parent)
    : QWidget(parent)
{
	init();
}

void TimelineView::clear()
{
	for (const auto msg : scroll_layout_->children())
		msg->deleteLater();
}

void TimelineView::sliderRangeChanged(int min, int max)
{
	Q_UNUSED(min);
	scroll_area_->verticalScrollBar()->setValue(max);
}

int TimelineView::addEvents(const QList<Event> &events)
{
	QSettings settings;
	auto local_user = settings.value("auth/user_id").toString();

	int message_count = 0;

	for (const auto &event : events) {
		if (event.type() == "m.room.message") {
			auto msg_type = event.content().value("msgtype").toString();

			if (isPendingMessage(event, local_user)) {
				removePendingMessage(event);
				continue;
			}

			if (msg_type == "m.text" || msg_type == "m.notice") {
				auto with_sender = last_sender_ != event.sender();
				auto color = TimelineViewManager::getUserColor(event.sender());

				addHistoryItem(event, color, with_sender);
				last_sender_ = event.sender();

				message_count += 1;
			}
		}
	}

	return message_count;
}

void TimelineView::init()
{
	top_layout_ = new QVBoxLayout(this);
	top_layout_->setSpacing(0);
	top_layout_->setMargin(0);

	scroll_area_ = new QScrollArea(this);
	scroll_area_->setWidgetResizable(true);
	scroll_area_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

	scroll_widget_ = new QWidget();

	scroll_layout_ = new QVBoxLayout();
	scroll_layout_->addStretch(1);
	scroll_layout_->setSpacing(0);

	scroll_widget_->setLayout(scroll_layout_);

	scroll_area_->setWidget(scroll_widget_);

	top_layout_->addWidget(scroll_area_);

	setLayout(top_layout_);

	connect(scroll_area_->verticalScrollBar(),
		SIGNAL(rangeChanged(int, int)),
		this,
		SLOT(sliderRangeChanged(int, int)));
}

void TimelineView::addHistoryItem(const Event &event, const QString &color, bool with_sender)
{
	TimelineItem *item = new TimelineItem(event, with_sender, color, scroll_widget_);
	scroll_layout_->addWidget(item);
}

void TimelineView::updatePendingMessage(int txn_id, QString event_id)
{
	for (auto &msg : pending_msgs_) {
		if (msg.txn_id == txn_id) {
			msg.event_id = event_id;
			break;
		}
	}
}

bool TimelineView::isPendingMessage(const Event &event, const QString &userid)
{
	if (event.sender() != userid || event.type() != "m.room.message")
		return false;

	auto msgtype = event.content().value("msgtype").toString();
	auto body = event.content().value("body").toString();

	// FIXME: should contain more checks later on for other types of messages.
	if (msgtype != "m.text")
		return false;

	for (const auto &msg : pending_msgs_) {
		if (msg.event_id == event.eventId() || msg.body == body)
			return true;
	}

	return false;
}

void TimelineView::removePendingMessage(const Event &event)
{
	auto body = event.content().value("body").toString();

	for (auto it = pending_msgs_.begin(); it != pending_msgs_.end(); it++) {
		int index = std::distance(pending_msgs_.begin(), it);

		if (it->event_id == event.eventId() || it->body == body) {
			pending_msgs_.removeAt(index);
			break;
		}
	}
}

void TimelineView::addUserTextMessage(const QString &body, int txn_id)
{
	QSettings settings;
	auto user_id = settings.value("auth/user_id").toString();

	auto with_sender = last_sender_ != user_id;
	auto color = TimelineViewManager::getUserColor(user_id);

	TimelineItem *view_item;

	if (with_sender)
		view_item = new TimelineItem(user_id, color, body, scroll_widget_);
	else
		view_item = new TimelineItem(body, scroll_widget_);

	scroll_layout_->addWidget(view_item);

	last_sender_ = user_id;

	PendingMessage message(txn_id, body, "", view_item);

	pending_msgs_.push_back(message);
}

TimelineView::~TimelineView()
{
}