summary refs log tree commit diff
path: root/src/timeline/TimelineViewManager.cc
blob: dda71d2f51ad6d297d14ee8b3cdceb1a01becde5 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
 * 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 <random>

#include <QApplication>
#include <QFileInfo>
#include <QSettings>

#include "Cache.h"
#include "Logging.hpp"
#include "timeline/TimelineView.h"
#include "timeline/TimelineViewManager.h"
#include "timeline/widgets/AudioItem.h"
#include "timeline/widgets/FileItem.h"
#include "timeline/widgets/ImageItem.h"
#include "timeline/widgets/VideoItem.h"

TimelineViewManager::TimelineViewManager(QWidget *parent)
  : QStackedWidget(parent)
{
        setStyleSheet("border: none;");
}

void
TimelineViewManager::removeTimelineEvent(const QString &room_id, const QString &event_id)
{
        auto view = views_[room_id];

        if (view)
                view->removeEvent(event_id);
}

void
TimelineViewManager::queueTextMessage(const QString &msg)
{
        if (active_room_.isEmpty())
                return;

        auto room_id = active_room_;
        auto view    = views_[room_id];

        view->addUserMessage(mtx::events::MessageType::Text, msg);
}

void
TimelineViewManager::queueEmoteMessage(const QString &msg)
{
        if (active_room_.isEmpty())
                return;

        auto room_id = active_room_;
        auto view    = views_[room_id];

        view->addUserMessage(mtx::events::MessageType::Emote, msg);
}

void
TimelineViewManager::queueImageMessage(const QString &roomid,
                                       const QString &filename,
                                       const QString &url,
                                       const QString &mime,
                                       uint64_t size)
{
        if (!timelineViewExists(roomid)) {
                nhlog::ui()->warn("Cannot send m.image message to a non-managed view");
                return;
        }

        auto view = views_[roomid];

        view->addUserMessage<ImageItem, mtx::events::MessageType::Image>(url, filename, mime, size);
}

void
TimelineViewManager::queueFileMessage(const QString &roomid,
                                      const QString &filename,
                                      const QString &url,
                                      const QString &mime,
                                      uint64_t size)
{
        if (!timelineViewExists(roomid)) {
                nhlog::ui()->warn("cannot send m.file message to a non-managed view");
                return;
        }

        auto view = views_[roomid];

        view->addUserMessage<FileItem, mtx::events::MessageType::File>(url, filename, mime, size);
}

void
TimelineViewManager::queueAudioMessage(const QString &roomid,
                                       const QString &filename,
                                       const QString &url,
                                       const QString &mime,
                                       uint64_t size)
{
        if (!timelineViewExists(roomid)) {
                nhlog::ui()->warn("cannot send m.audio message to a non-managed view");
                return;
        }

        auto view = views_[roomid];

        view->addUserMessage<AudioItem, mtx::events::MessageType::Audio>(url, filename, mime, size);
}

void
TimelineViewManager::queueVideoMessage(const QString &roomid,
                                       const QString &filename,
                                       const QString &url,
                                       const QString &mime,
                                       uint64_t size)
{
        if (!timelineViewExists(roomid)) {
                nhlog::ui()->warn("cannot send m.video message to a non-managed view");
                return;
        }

        auto view = views_[roomid];

        view->addUserMessage<VideoItem, mtx::events::MessageType::Video>(url, filename, mime, size);
}

void
TimelineViewManager::initialize(const mtx::responses::Rooms &rooms)
{
        for (auto it = rooms.join.cbegin(); it != rooms.join.cend(); ++it) {
                addRoom(it->second, QString::fromStdString(it->first));
        }

        sync(rooms);
}

void
TimelineViewManager::initWithMessages(const std::map<QString, mtx::responses::Timeline> &msgs)
{
        for (auto it = msgs.cbegin(); it != msgs.cend(); ++it) {
                if (timelineViewExists(it->first))
                        return;

                // Create a history view with the room events.
                TimelineView *view = new TimelineView(it->second, it->first);
                views_.emplace(it->first, QSharedPointer<TimelineView>(view));

                connect(view,
                        &TimelineView::updateLastTimelineMessage,
                        this,
                        &TimelineViewManager::updateRoomsLastMessage);

                // Add the view in the widget stack.
                addWidget(view);
        }
}

void
TimelineViewManager::initialize(const std::vector<std::string> &rooms)
{
        for (const auto &roomid : rooms)
                addRoom(QString::fromStdString(roomid));
}

void
TimelineViewManager::addRoom(const mtx::responses::JoinedRoom &room, const QString &room_id)
{
        if (timelineViewExists(room_id))
                return;

        // Create a history view with the room events.
        TimelineView *view = new TimelineView(room.timeline, room_id);
        views_.emplace(room_id, QSharedPointer<TimelineView>(view));

        connect(view,
                &TimelineView::updateLastTimelineMessage,
                this,
                &TimelineViewManager::updateRoomsLastMessage);

        // Add the view in the widget stack.
        addWidget(view);
}

void
TimelineViewManager::addRoom(const QString &room_id)
{
        if (timelineViewExists(room_id))
                return;

        // Create a history view without any events.
        TimelineView *view = new TimelineView(room_id);
        views_.emplace(room_id, QSharedPointer<TimelineView>(view));

        connect(view,
                &TimelineView::updateLastTimelineMessage,
                this,
                &TimelineViewManager::updateRoomsLastMessage);

        // Add the view in the widget stack.
        addWidget(view);
}

void
TimelineViewManager::sync(const mtx::responses::Rooms &rooms)
{
        for (const auto &room : rooms.join) {
                auto roomid = QString::fromStdString(room.first);

                if (!timelineViewExists(roomid)) {
                        nhlog::ui()->warn("ignoring event from unknown room: {}",
                                          roomid.toStdString());
                        continue;
                }

                auto view = views_.at(roomid);

                view->addEvents(room.second.timeline);
        }
}

void
TimelineViewManager::setHistoryView(const QString &room_id)
{
        if (!timelineViewExists(room_id)) {
                nhlog::ui()->warn("room from RoomList is not present in ViewManager: {}",
                                  room_id.toStdString());
                return;
        }

        active_room_ = room_id;
        auto view    = views_.at(room_id);

        setCurrentWidget(view.data());

        view->fetchHistory();
        view->scrollDown();
}

QString
TimelineViewManager::chooseRandomColor()
{
        std::random_device random_device;
        std::mt19937 engine{random_device()};
        std::uniform_real_distribution<float> dist(0, 1);

        float hue        = dist(engine);
        float saturation = 0.9;
        float value      = 0.7;

        int hue_i = hue * 6;

        float f = hue * 6 - hue_i;

        float p = value * (1 - saturation);
        float q = value * (1 - f * saturation);
        float t = value * (1 - (1 - f) * saturation);

        float r = 0;
        float g = 0;
        float b = 0;

        if (hue_i == 0) {
                r = value;
                g = t;
                b = p;
        } else if (hue_i == 1) {
                r = q;
                g = value;
                b = p;
        } else if (hue_i == 2) {
                r = p;
                g = value;
                b = t;
        } else if (hue_i == 3) {
                r = p;
                g = q;
                b = value;
        } else if (hue_i == 4) {
                r = t;
                g = p;
                b = value;
        } else if (hue_i == 5) {
                r = value;
                g = p;
                b = q;
        }

        int ri = r * 256;
        int gi = g * 256;
        int bi = b * 256;

        QColor color(ri, gi, bi);

        return color.name();
}

bool
TimelineViewManager::hasLoaded() const
{
        return std::all_of(views_.cbegin(), views_.cend(), [](const auto &view) {
                return view.second->hasLoaded();
        });
}