summary refs log tree commit diff
path: root/src/dialogs/RoomSettings.cpp
blob: 4a7ea4fc3c377b96955550c483f622d479ca395b (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include "Avatar.h"
#include "Config.h"
#include "FlatButton.h"
#include "MatrixClient.h"
#include "Painter.h"
#include "TextField.h"
#include "Utils.h"
#include "dialogs/RoomSettings.hpp"

#include <QComboBox>
#include <QLabel>
#include <QPainter>
#include <QPixmap>
#include <QSharedPointer>
#include <QStyleOption>
#include <QVBoxLayout>

using namespace dialogs;

EditModal::EditModal(const QString &roomId, QWidget *parent)
  : QWidget(parent)
  , roomId_{roomId}
{
        setMinimumWidth(360);
        setAutoFillBackground(true);
        setAttribute(Qt::WA_DeleteOnClose, true);
        setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        setWindowModality(Qt::WindowModal);

        auto layout = new QVBoxLayout(this);

        applyBtn_ = new FlatButton(tr("APPLY"), this);
        applyBtn_->setFontSize(conf::btn::fontSize);
        cancelBtn_ = new FlatButton(tr("CANCEL"), this);
        cancelBtn_->setFontSize(conf::btn::fontSize);

        auto btnLayout = new QHBoxLayout;
        btnLayout->setContentsMargins(5, 20, 5, 5);
        btnLayout->addWidget(applyBtn_);
        btnLayout->addWidget(cancelBtn_);

        nameInput_ = new TextField(this);
        nameInput_->setLabel(tr("Name"));
        topicInput_ = new TextField(this);
        topicInput_->setLabel(tr("Topic"));

        QFont font;
        font.setPixelSize(conf::modals::errorFont);

        errorField_ = new QLabel(this);
        errorField_->setFont(font);
        errorField_->setWordWrap(true);
        errorField_->hide();

        layout->addWidget(nameInput_);
        layout->addWidget(topicInput_);
        layout->addLayout(btnLayout, 1);

        auto labelLayout = new QHBoxLayout;
        labelLayout->setAlignment(Qt::AlignHCenter);
        labelLayout->addWidget(errorField_);
        layout->addLayout(labelLayout);

        connect(applyBtn_, &QPushButton::clicked, [this]() {
                // Check if the values are changed from the originals.
                auto newName  = nameInput_->text().trimmed();
                auto newTopic = topicInput_->text().trimmed();

                errorField_->hide();

                if (newName == initialName_ && newTopic == initialTopic_) {
                        close();
                        return;
                }

                using namespace mtx::events;

                if (newName != initialName_ && !newName.isEmpty()) {
                        state::Name body;
                        body.name = newName.toStdString();

                        auto proxy =
                          http::client()->sendStateEvent<state::Name, EventType::RoomName>(body,
                                                                                           roomId_);
                        connect(proxy.get(),
                                &StateEventProxy::stateEventSent,
                                this,
                                [this, proxy, newName]() {
                                        proxy->deleteLater();
                                        errorField_->hide();
                                        emit nameChanged(newName);
                                        close();
                                });

                        connect(proxy.get(),
                                &StateEventProxy::stateEventError,
                                this,
                                [this, proxy, newName](const QString &msg) {
                                        proxy->deleteLater();
                                        errorField_->setText(msg);
                                        errorField_->show();
                                });
                }

                if (newTopic != initialTopic_ && !newTopic.isEmpty()) {
                        state::Topic body;
                        body.topic = newTopic.toStdString();

                        auto proxy =
                          http::client()->sendStateEvent<state::Topic, EventType::RoomTopic>(
                            body, roomId_);
                        connect(proxy.get(),
                                &StateEventProxy::stateEventSent,
                                this,
                                [this, proxy, newTopic]() {
                                        proxy->deleteLater();
                                        errorField_->hide();
                                        close();
                                });

                        connect(proxy.get(),
                                &StateEventProxy::stateEventError,
                                this,
                                [this, proxy, newTopic](const QString &msg) {
                                        proxy->deleteLater();
                                        errorField_->setText(msg);
                                        errorField_->show();
                                });
                }
        });
        connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close);
}

void
EditModal::setFields(const QString &roomName, const QString &roomTopic)
{
        initialName_  = roomName;
        initialTopic_ = roomTopic;

        nameInput_->setText(roomName);
        topicInput_->setText(roomTopic);
}

TopSection::TopSection(const RoomInfo &info, const QImage &img, QWidget *parent)
  : QWidget{parent}
  , info_{std::move(info)}
{
        textColor_ = palette().color(QPalette::Text);
        avatar_    = utils::scaleImageToPixmap(img, AvatarSize);

        QSizePolicy policy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        setSizePolicy(policy);
}

void
TopSection::setRoomName(const QString &name)
{
        info_.name = name.toStdString();
        update();
}

QSize
TopSection::sizeHint() const
{
        QFont font;
        font.setPixelSize(18);
        return QSize(340, AvatarSize + QFontMetrics(font).ascent());
}

RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
  : QFrame(parent)
  , room_id_{std::move(room_id)}
{
        setMaximumWidth(420);

        try {
                info_ = cache::client()->singleRoomInfo(room_id_.toStdString());

                setAvatar(QImage::fromData(cache::client()->image(info_.avatar_url)));
        } catch (const lmdb::error &e) {
                qWarning() << "failed to retrieve room info from cache" << room_id;
        }

        constexpr int SettingsMargin = 2;

        auto layout = new QVBoxLayout(this);
        layout->setSpacing(15);
        layout->setMargin(20);

        saveBtn_ = new FlatButton("SAVE", this);
        saveBtn_->setFontSize(conf::btn::fontSize);
        cancelBtn_ = new FlatButton(tr("CANCEL"), this);
        cancelBtn_->setFontSize(conf::btn::fontSize);

        auto btnLayout = new QHBoxLayout();
        btnLayout->setSpacing(0);
        btnLayout->setMargin(0);
        btnLayout->addStretch(1);
        btnLayout->addWidget(saveBtn_);
        btnLayout->addWidget(cancelBtn_);

        auto notifOptionLayout_ = new QHBoxLayout;
        notifOptionLayout_->setMargin(SettingsMargin);
        auto notifLabel = new QLabel(tr("Notifications"), this);
        auto notifCombo = new QComboBox(this);
        notifCombo->setDisabled(true);
        notifCombo->addItem(tr("Muted"));
        notifCombo->addItem(tr("Mentions only"));
        notifCombo->addItem(tr("All messages"));
        notifLabel->setStyleSheet("font-size: 15px;");

        notifOptionLayout_->addWidget(notifLabel);
        notifOptionLayout_->addWidget(notifCombo, 0, Qt::AlignBottom | Qt::AlignRight);

        auto accessOptionLayout = new QHBoxLayout();
        accessOptionLayout->setMargin(SettingsMargin);
        auto accessLabel = new QLabel(tr("Room access"), this);
        accessCombo      = new QComboBox(this);
        accessCombo->addItem(tr("Anyone and guests"));
        accessCombo->addItem(tr("Anyone"));
        accessCombo->addItem(tr("Invited users"));
        accessCombo->setDisabled(true);
        accessLabel->setStyleSheet("font-size: 15px;");

        if (info_.join_rule == JoinRule::Public) {
                if (info_.guest_access) {
                        accessCombo->setCurrentIndex(0);
                } else {
                        accessCombo->setCurrentIndex(1);
                }
        } else {
                accessCombo->setCurrentIndex(2);
        }

        accessOptionLayout->addWidget(accessLabel);
        accessOptionLayout->addWidget(accessCombo);

        QFont font;
        font.setPixelSize(18);
        font.setWeight(70);

        auto menuLabel = new QLabel("Room Settings", this);
        menuLabel->setFont(font);

        constexpr int buttonSize = 36;
        constexpr int iconSize   = buttonSize / 2;

        QIcon editIcon;
        editIcon.addFile(":/icons/icons/ui/edit.svg");
        editFieldsBtn_ = new FlatButton(this);
        editFieldsBtn_->setFixedSize(buttonSize, buttonSize);
        editFieldsBtn_->setCornerRadius(iconSize);
        editFieldsBtn_->setIcon(editIcon);
        editFieldsBtn_->setIcon(editIcon);
        editFieldsBtn_->setIconSize(QSize(iconSize, iconSize));

        connect(editFieldsBtn_, &QPushButton::clicked, this, [this]() {
                auto modal = new EditModal(room_id_, this->parentWidget());
                modal->setFields(QString::fromStdString(info_.name),
                                 QString::fromStdString(info_.topic));
                modal->show();
                connect(modal, &EditModal::nameChanged, this, [this](const QString &newName) {
                        topSection_->setRoomName(newName);
                });
        });

        topSection_ = new TopSection(info_, avatarImg_, this);

        auto editLayout = new QHBoxLayout;
        editLayout->setMargin(0);
        editLayout->addWidget(topSection_);
        editLayout->addWidget(editFieldsBtn_, 0, Qt::AlignRight | Qt::AlignTop);

        layout->addWidget(menuLabel);
        layout->addLayout(editLayout);
        layout->addLayout(notifOptionLayout_);
        layout->addLayout(accessOptionLayout);
        layout->addLayout(btnLayout);

        connect(cancelBtn_, &QPushButton::clicked, this, &RoomSettings::closing);
        connect(saveBtn_, &QPushButton::clicked, this, &RoomSettings::saveSettings);
}

void
RoomSettings::saveSettings()
{
        // TODO: Save access changes to the room
        if (accessCombo->currentIndex() < 2) {
                if (info_.join_rule != JoinRule::Public) {
                        // Make join_rule Public
                }
                if (accessCombo->currentIndex() == 0) {
                        if (!info_.guest_access) {
                                // Make guest_access CanJoin
                        }
                }
        } else {
                if (info_.join_rule != JoinRule::Invite) {
                        // Make join_rule invite
                }
                if (info_.guest_access) {
                        // Make guest_access forbidden
                }
        }
        closing();
}

void
RoomSettings::paintEvent(QPaintEvent *)
{
        QStyleOption opt;
        opt.init(this);
        QPainter p(this);
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void
TopSection::paintEvent(QPaintEvent *)
{
        Painter p(this);
        PainterHighQualityEnabler hq(p);

        constexpr int textStartX     = AvatarSize + 5 * Padding;
        const int availableTextWidth = width() - textStartX;

        constexpr int nameFont    = 15;
        constexpr int membersFont = 14;

        p.save();
        p.setPen(textColor());
        p.translate(textStartX, 2 * Padding);

        // Draw the name.
        QFont font;
        font.setPixelSize(membersFont);
        const auto members = QString("%1 members").arg(info_.member_count);

        font.setPixelSize(nameFont);
        const auto name = QFontMetrics(font).elidedText(
          QString::fromStdString(info_.name), Qt::ElideRight, availableTextWidth - 4 * Padding);

        font.setWeight(60);
        p.setFont(font);
        p.drawTextLeft(0, 0, name);

        // Draw the number of members
        p.translate(0, QFontMetrics(p.font()).ascent() + 2 * Padding);

        font.setPixelSize(membersFont);
        font.setWeight(50);
        p.setFont(font);
        p.drawTextLeft(0, 0, members);
        p.restore();

        if (avatar_.isNull()) {
                font.setPixelSize(AvatarSize / 2);
                font.setWeight(60);
                p.setFont(font);

                p.translate(Padding, Padding);
                p.drawLetterAvatar(utils::firstChar(name),
                                   QColor("white"),
                                   QColor("black"),
                                   AvatarSize + Padding,
                                   AvatarSize + Padding,
                                   AvatarSize);
        } else {
                QRect avatarRegion(Padding, Padding, AvatarSize, AvatarSize);

                QPainterPath pp;
                pp.addEllipse(avatarRegion.center(), AvatarSize, AvatarSize);

                p.setClipPath(pp);
                p.drawPixmap(avatarRegion, avatar_);
        }
}