File: userlistdock.cpp

package info (click to toggle)
quaternion 0.0.97.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,800 kB
  • sloc: cpp: 8,380; xml: 172; sh: 5; makefile: 2
file content (200 lines) | stat: -rw-r--r-- 6,490 bytes parent folder | download
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
/**************************************************************************
 *                                                                        *
 * SPDX-FileCopyrightText: 2015 Felix Rohrbach <kde@fxrh.de>              *
 *                                                                        *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *                                                                        *
 **************************************************************************/

#include "userlistdock.h"

#include <QtWidgets/QTableView>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMenu>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QInputDialog>
#include <QtGui/QGuiApplication>

#include <Quotient/connection.h>
#include <Quotient/events/roompowerlevelsevent.h>
#include <Quotient/room.h>
#include <Quotient/user.h>

#include "models/userlistmodel.h"
#include "quaternionroom.h"

UserListDock::UserListDock(QWidget* parent)
    : QDockWidget(tr("Users"), parent)
{
    setObjectName(QStringLiteral("UsersDock"));

    m_box = new QVBoxLayout();

    m_box->addSpacing(1);
    m_filterline = new QLineEdit(this);
    m_filterline->setPlaceholderText(tr("Search"));
    m_filterline->setDisabled(true);
    m_box->addWidget(m_filterline);

    m_view = new QTableView(this);
    m_view->setShowGrid(false);
    // Derive the member icon size from that of the default icon used when
    // the member doesn't have an avatar
    const auto iconExtent = m_view->fontMetrics().height() * 3 / 2;
    m_view->setIconSize(
        QIcon::fromTheme("user-available", QIcon(":/irc-channel-joined"))
            .actualSize({ iconExtent, iconExtent }));
    m_view->horizontalHeader()->setStretchLastSection(true);
    m_view->horizontalHeader()->setVisible(false);
    m_view->verticalHeader()->setVisible(false);
    m_box->addWidget(m_view);

    m_widget = new QWidget(this);
    m_widget->setLayout(m_box);
    setWidget(m_widget);

    connect(m_view, &QTableView::activated,
            this, &UserListDock::requestUserMention);
    connect( m_view, &QTableView::pressed, this, [this] {
        if (QGuiApplication::mouseButtons() & Qt::MiddleButton)
            startChatSelected();
    });

    m_model = new UserListModel(m_view);
    m_view->setModel(m_model);

    connect( m_model, &UserListModel::membersChanged,
             this, &UserListDock::refreshTitle );
    connect( m_model, &QAbstractListModel::modelReset,
             this, &UserListDock::refreshTitle );
    connect(m_filterline, &QLineEdit::textEdited,
             m_model, &UserListModel::filter);

    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &QWidget::customContextMenuRequested,
            this, &UserListDock::showContextMenu);
}

void UserListDock::setRoom(QuaternionRoom* room)
{
    if (m_currentRoom)
        m_currentRoom->setCachedUserFilter(m_filterline->text());
    m_currentRoom = room;
    m_model->setRoom(room);
    m_filterline->setEnabled(room);
    m_filterline->setText(room ? room->cachedUserFilter() : "");
    m_model->filter(m_filterline->text());
}

void UserListDock::refreshTitle()
{
    setWindowTitle(tr("Users") +
        (!m_currentRoom ? QString() :
         ' ' + (m_model->rowCount() == m_currentRoom->joinedCount() ?
                    QStringLiteral("(%L1)").arg(m_currentRoom->joinedCount()) :
                    tr("(%L1 out of %L2)", "%found out of %total users")
                    .arg(m_model->rowCount()).arg(m_currentRoom->joinedCount())))
    );
}

void UserListDock::showContextMenu(QPoint pos)
{
    if (getSelectedUser().isEmpty())
        return;

    auto* contextMenu = new QMenu(this);

    contextMenu->addAction(QIcon::fromTheme("contact-new"),
        tr("Open direct chat"), this, &UserListDock::startChatSelected);
    contextMenu->addAction(tr("Mention user"), this,
        &UserListDock::requestUserMention);
    QAction* ignoreAction =
        contextMenu->addAction(QIcon::fromTheme("mail-thread-ignored"),
            tr("Ignore user"), this, &UserListDock::ignoreUser);
    ignoreAction->setCheckable(true);
    contextMenu->addSeparator();

    const auto* plEvt =
        m_currentRoom->currentState().get<Quotient::RoomPowerLevelsEvent>();
    const int userPl =
        plEvt ? plEvt->powerLevelForUser(m_currentRoom->localMember().id()) : 0;

    if (!plEvt || userPl >= plEvt->kick()) {
        contextMenu->addAction(QIcon::fromTheme("im-ban-kick-user"),
            tr("Kick user"), this,&UserListDock::kickUser);
    }
    if (!plEvt || userPl >= plEvt->ban()) {
        contextMenu->addAction(QIcon::fromTheme("im-ban-user"),
            tr("Ban user"), this, &UserListDock::banUser);
    }

    contextMenu->popup(mapToGlobal(pos));
    ignoreAction->setChecked(isIgnored());
}

void UserListDock::startChatSelected()
{
    if (auto userId = getSelectedUser(); !userId.isEmpty())
        m_currentRoom->connection()->requestDirectChat(userId);
}

void UserListDock::requestUserMention()
{
    if (auto userId = getSelectedUser(); !userId.isEmpty())
        emit userMentionRequested(userId);
}

void UserListDock::kickUser()
{
    if (auto userId = getSelectedUser(); !userId.isEmpty())
    {
        bool ok;
        const auto reason = QInputDialog::getText(this,
                tr("Kick %1").arg(userId), tr("Reason"),
                QLineEdit::Normal, nullptr, &ok);
        if (ok) {
            m_currentRoom->kickMember(userId, reason);
        }
    }
}

void UserListDock::banUser()
{
    if (auto userId = getSelectedUser(); !userId.isEmpty())
    {
        bool ok;
        const auto reason = QInputDialog::getText(this,
                tr("Ban %1").arg(userId), tr("Reason"),
                QLineEdit::Normal, nullptr, &ok);
        if (ok) {
            m_currentRoom->ban(userId, reason);
        }
    }
}

void UserListDock::ignoreUser()
{
    if (auto* user = m_currentRoom->connection()->user(getSelectedUser())) {
        if (!user->isIgnored())
            user->ignore();
        else
            user->unmarkIgnore();
    }
}

bool UserListDock::isIgnored()
{
    if (auto memberId = getSelectedUser(); !memberId.isEmpty())
        return m_currentRoom->connection()->isIgnored(memberId);
    return false;
}

QString UserListDock::getSelectedUser() const
{
    auto index = m_view->currentIndex();
    if (!index.isValid())
        return {};
    const auto member = m_model->userAt(index);
    Q_ASSERT(!member.isEmpty());
    return member.id();
}