summary refs log tree commit diff
path: root/src/dialogs/AcceptCall.cpp
blob: 3d25ad82b9b77c04509aa365bd694b6ee3f38508 (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
#include <QComboBox>
#include <QLabel>
#include <QPushButton>
#include <QString>
#include <QVBoxLayout>

#include "ChatPage.h"
#include "Config.h"
#include "UserSettingsPage.h"
#include "Utils.h"
#include "WebRTCSession.h"
#include "dialogs/AcceptCall.h"
#include "ui/Avatar.h"

namespace dialogs {

AcceptCall::AcceptCall(const QString &caller,
                       const QString &displayName,
                       const QString &roomName,
                       const QString &avatarUrl,
                       bool isVideo,
                       QWidget *parent)
  : QWidget(parent)
{
        std::string errorMessage;
        WebRTCSession *session = &WebRTCSession::instance();
        if (!session->havePlugins(false, &errorMessage)) {
                emit ChatPage::instance()->showNotification(QString::fromStdString(errorMessage));
                emit close();
                return;
        }
        if (isVideo && !session->havePlugins(true, &errorMessage)) {
                emit ChatPage::instance()->showNotification(QString::fromStdString(errorMessage));
                emit close();
                return;
        }

        session->refreshDevices();
        microphones_ = session->getDeviceNames(
          false, ChatPage::instance()->userSettings()->microphone().toStdString());
        if (microphones_.empty()) {
                emit ChatPage::instance()->showNotification(
                  tr("Incoming call: No microphone found."));
                emit close();
                return;
        }
        if (isVideo)
                cameras_ = session->getDeviceNames(
                  true, ChatPage::instance()->userSettings()->camera().toStdString());

        setAutoFillBackground(true);
        setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
        setWindowModality(Qt::WindowModal);
        setAttribute(Qt::WA_DeleteOnClose, true);

        setMinimumWidth(conf::modals::MIN_WIDGET_WIDTH);
        setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

        auto layout = new QVBoxLayout(this);
        layout->setSpacing(conf::modals::WIDGET_SPACING);
        layout->setMargin(conf::modals::WIDGET_MARGIN);

        QFont f;
        f.setPointSizeF(f.pointSizeF());

        QFont labelFont;
        labelFont.setWeight(QFont::Medium);

        QLabel *displayNameLabel = nullptr;
        if (!displayName.isEmpty() && displayName != caller) {
                displayNameLabel = new QLabel(displayName, this);
                labelFont.setPointSizeF(f.pointSizeF() * 2);
                displayNameLabel->setFont(labelFont);
                displayNameLabel->setAlignment(Qt::AlignCenter);
        }

        QLabel *callerLabel = new QLabel(caller, this);
        labelFont.setPointSizeF(f.pointSizeF() * 1.2);
        callerLabel->setFont(labelFont);
        callerLabel->setAlignment(Qt::AlignCenter);

        auto avatar = new Avatar(this, QFontMetrics(f).height() * 6);
        if (!avatarUrl.isEmpty())
                avatar->setImage(avatarUrl);
        else
                avatar->setLetter(utils::firstChar(roomName));

        const int iconSize        = 22;
        QLabel *callTypeIndicator = new QLabel(this);
        callTypeIndicator->setPixmap(
          QIcon(isVideo ? ":/icons/icons/ui/video-call.png" : ":/icons/icons/ui/place-call.png")
            .pixmap(QSize(iconSize * 2, iconSize * 2)));

        QLabel *callTypeLabel = new QLabel(isVideo ? tr("Video Call") : tr("Voice Call"), this);
        labelFont.setPointSizeF(f.pointSizeF() * 1.1);
        callTypeLabel->setFont(labelFont);
        callTypeLabel->setAlignment(Qt::AlignCenter);

        auto buttonLayout = new QHBoxLayout;
        buttonLayout->setSpacing(18);
        acceptBtn_ = new QPushButton(tr("Accept"), this);
        acceptBtn_->setDefault(true);
        acceptBtn_->setIcon(
          QIcon(isVideo ? ":/icons/icons/ui/video-call.png" : ":/icons/icons/ui/place-call.png"));
        acceptBtn_->setIconSize(QSize(iconSize, iconSize));

        rejectBtn_ = new QPushButton(tr("Reject"), this);
        rejectBtn_->setIcon(QIcon(":/icons/icons/ui/end-call.png"));
        rejectBtn_->setIconSize(QSize(iconSize, iconSize));
        buttonLayout->addWidget(acceptBtn_);
        buttonLayout->addWidget(rejectBtn_);

        microphoneCombo_ = new QComboBox(this);
        for (const auto &m : microphones_)
                microphoneCombo_->addItem(QIcon(":/icons/icons/ui/microphone-unmute.png"),
                                          QString::fromStdString(m));

        if (!cameras_.empty()) {
                cameraCombo_ = new QComboBox(this);
                for (const auto &c : cameras_)
                        cameraCombo_->addItem(QIcon(":/icons/icons/ui/video-call.png"),
                                              QString::fromStdString(c));
        }

        if (displayNameLabel)
                layout->addWidget(displayNameLabel, 0, Qt::AlignCenter);
        layout->addWidget(callerLabel, 0, Qt::AlignCenter);
        layout->addWidget(avatar, 0, Qt::AlignCenter);
        layout->addWidget(callTypeIndicator, 0, Qt::AlignCenter);
        layout->addWidget(callTypeLabel, 0, Qt::AlignCenter);
        layout->addLayout(buttonLayout);
        layout->addWidget(microphoneCombo_);
        if (cameraCombo_)
                layout->addWidget(cameraCombo_);

        connect(acceptBtn_, &QPushButton::clicked, this, [this]() {
                ChatPage::instance()->userSettings()->setMicrophone(
                  QString::fromStdString(microphones_[microphoneCombo_->currentIndex()]));
                if (cameraCombo_) {
                        ChatPage::instance()->userSettings()->setCamera(
                          QString::fromStdString(cameras_[cameraCombo_->currentIndex()]));
                }
                emit accept();
                emit close();
        });
        connect(rejectBtn_, &QPushButton::clicked, this, [this]() {
                emit reject();
                emit close();
        });
}

}