callwidget.cpp 9.68 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1
/***************************************************************************
2
 * Copyright (C) 2015 by Savoir-Faire Linux                                *
Edric Milaret's avatar
Edric Milaret committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * 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 "callwidget.h"
#include "ui_callwidget.h"

22
#include <memory>
Edric Milaret's avatar
Edric Milaret committed
23 24 25 26 27 28 29

#include "imconversationmanager.h"
#include "instantmessagingmodel.h"
#include "audio/settings.h"
#include "personmodel.h"
#include "fallbackpersoncollection.h"
#include "accountmodel.h"
30 31
#include "categorizedcontactmodel.h"
#include "windowscontactbackend.h"
32
#include "historydelegate.h"
Edric Milaret's avatar
Edric Milaret committed
33
#include "contactdelegate.h"
Edric Milaret's avatar
Edric Milaret committed
34

35
#include "wizarddialog.h"
Edric Milaret's avatar
Edric Milaret committed
36 37 38 39 40 41 42 43 44 45 46 47

CallWidget::CallWidget(QWidget *parent) :
    NavWidget(Main ,parent),
    ui(new Ui::CallWidget)
{
    ui->setupUi(this);

    ui->holdButton->setCheckable(true);
    ui->muteMicButton->setCheckable(true);
    ui->muteSpeakerButton->setCheckable(true);
    ui->callInvite->setVisible(false);

48
    setActualCall(nullptr);
Edric Milaret's avatar
Edric Milaret committed
49 50 51 52 53 54 55 56 57 58 59 60
    videoRenderer_ = nullptr;

    try {
        callModel_ = CallModel::instance();

        connect(callModel_, SIGNAL(incomingCall(Call*)),
                this, SLOT(callIncoming(Call*)));
        connect(callModel_, SIGNAL(callAdded(Call*,Call*)),
                this, SLOT(addedCall(Call*, Call*)));
        connect(callModel_, SIGNAL(callStateChanged(Call*, Call::State)),
                this, SLOT(callStateChanged(Call*, Call::State)));

61 62 63 64 65
        connect(AccountModel::instance()
                , SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>))
                , this
                , SLOT(findRingAccount(QModelIndex, QModelIndex, QVector<int>)));

Edric Milaret's avatar
Edric Milaret committed
66 67 68 69 70
        ui->callList->setModel(callModel_);

        CategorizedHistoryModel::instance()->
                addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);

71 72 73 74 75 76
        PersonModel::instance()->
                addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);

        PersonModel::instance()->
                addCollection<WindowsContactBackend>(LoadOptions::FORCE_ENABLED);

77 78 79 80 81 82 83 84 85
        auto historyModel = std::unique_ptr<QSortFilterProxyModel>(new QSortFilterProxyModel());
        historyModel->setSourceModel(CategorizedHistoryModel::instance());
        historyModel->setSortRole(static_cast<int>(Call::Role::Date));
        historyModel->sort(0,Qt::DescendingOrder);
        ui->historyList->setModel(historyModel.get());
        ui->historyList->setHeaderHidden(true);
        ui->historyList->setItemDelegate(new HistoryDelegate());
        historyModel.release();

86 87
        CategorizedContactModel::instance()->setSortAlphabetical(false);
        ui->contactView->setModel(CategorizedContactModel::instance());
Edric Milaret's avatar
Edric Milaret committed
88
        ui->contactView->setItemDelegate(new ContactDelegate());
89

Edric Milaret's avatar
Edric Milaret committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        ui->speakerSlider->setValue(Audio::Settings::instance()->playbackVolume());
        ui->micSlider->setValue(Audio::Settings::instance()->captureVolume());

        findRingAccount();

    } catch (...) {
        qDebug() << "INIT ERROR";
    }
}

CallWidget::~CallWidget()
{
    delete ui;
}

void
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
CallWidget::findRingAccount(QModelIndex idx1, QModelIndex idx2, QVector<int> vec)
{
    Q_UNUSED(idx1)
    Q_UNUSED(idx2)
    Q_UNUSED(vec)

    auto a_count = AccountModel::instance()->rowCount();
    auto found = false;
    for (int i = 0; i < a_count; ++i) {
        auto idx = AccountModel::instance()->index(i, 0);
        auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
        if ((Account::Protocol)protocol.toUInt() == Account::Protocol::RING) {
            auto username = idx.data(static_cast<int>(Account::Role::Username));
            ui->ringIdLabel->setText(
                        "Your Ring ID: " + username.toString());
            found = true;
            return;
        }
    }
    if (not found){
        ui->ringIdLabel->setText("NO RING ACCOUNT FOUND");
    }
}

void
CallWidget::findRingAccount()
{
133

Edric Milaret's avatar
Edric Milaret committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147
    auto a_count = AccountModel::instance()->rowCount();
    auto found = false;
    for (int i = 0; i < a_count; ++i) {
        auto idx = AccountModel::instance()->index(i, 0);
        auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
        if ((Account::Protocol)protocol.toUInt() == Account::Protocol::RING) {
            auto username = idx.data(static_cast<int>(Account::Role::Username));
            ui->ringIdLabel->setText(
                        ui->ringIdLabel->text() + " " + username.toString());
            found = true;
            return;
        }
    }
    if (!found) {
148 149 150
        WizardDialog *wizardDialog = new WizardDialog();
        wizardDialog->exec();
        delete wizardDialog;
Edric Milaret's avatar
Edric Milaret committed
151 152 153 154 155 156 157 158
    }
}

void
CallWidget::callIncoming(Call *call)
{
    if (!call->account()->isAutoAnswer())
        ui->callInvite->setVisible(true);
159
    setActualCall(call);
Edric Milaret's avatar
Edric Milaret committed
160 161 162 163 164 165
}

void
CallWidget::on_acceptButton_clicked()
{
    if (actualCall_ != nullptr)
166
        actualCall_->performAction(Call::Action::ACCEPT);
Edric Milaret's avatar
Edric Milaret committed
167 168 169 170 171 172 173 174 175
    ui->callInvite->setVisible(false);
}

void
CallWidget::on_refuseButton_clicked()
{
    if (actualCall_ == nullptr)
        return;
    actualCall_->performAction(Call::Action::REFUSE);
176
    setActualCall(nullptr);
Edric Milaret's avatar
Edric Milaret committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    ui->callInvite->setVisible(false);
}

void
CallWidget::on_holdButton_toggled(bool checked)
{
    Q_UNUSED(checked)
    if (actualCall_ == nullptr)
        return;
    actualCall_->performAction(Call::Action::HOLD);
}

void
CallWidget::on_hangupButton_clicked()
{
    if (actualCall_ == nullptr)
        return;
    actualCall_->performAction(Call::Action::REFUSE);
}

void
CallWidget::addedCall(Call* call, Call* parent) {
    Q_UNUSED(parent);
    if (call->direction() == Call::Direction::OUTGOING) {
201
        setActualCall(call);
Edric Milaret's avatar
Edric Milaret committed
202 203 204 205
    }
}

void
206 207
CallWidget::callStateChanged(Call* call, Call::State previousState)
{
Edric Milaret's avatar
Edric Milaret committed
208
    Q_UNUSED(previousState)
209 210
    if (call == nullptr)
        return;
Edric Milaret's avatar
Edric Milaret committed
211
    ui->callList->setCurrentIndex(callModel_->getIndex(actualCall_));
212 213
    if (call->state() == Call::State::OVER || call->state() == Call::State::ERROR) {
        setActualCall(nullptr);
Edric Milaret's avatar
Edric Milaret committed
214
        ui->videoWidget->hide();
215 216
    } else if (call->state() == Call::State::HOLD) {
        ui->videoWidget->hide();
217
    } else if (call->state() == Call::State::CURRENT) {
Edric Milaret's avatar
Edric Milaret committed
218 219 220 221
        ui->videoWidget->show();
        ui->messageOutput->setModel(
                    IMConversationManager::instance()->getModel(actualCall_));
    }
222
    ui->callStateLabel->setText("Call State : " + state.at((int)call->state()));
Edric Milaret's avatar
Edric Milaret committed
223 224 225 226 227 228 229 230 231
}

void
CallWidget::on_callList_activated(const QModelIndex &index)
{
    Call *callSelected = callModel_->getCall(index);
    if (actualCall_ != nullptr) {
        if (callSelected == actualCall_)
            return;
Edric Milaret's avatar
Edric Milaret committed
232
        ui->videoWidget->hide();
Edric Milaret's avatar
Edric Milaret committed
233 234
        actualCall_->performAction(Call::Action::HOLD);
    }
235
    setActualCall(callSelected);
Edric Milaret's avatar
Edric Milaret committed
236
    actualCall_->performAction(Call::Action::HOLD);
Edric Milaret's avatar
Edric Milaret committed
237
    ui->videoWidget->show();
Edric Milaret's avatar
Edric Milaret committed
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
}

void
CallWidget::on_muteSpeakerButton_toggled(bool checked)
{
    Audio::Settings::instance()->mutePlayback(checked);
}

void
CallWidget::on_muteMicButton_toggled(bool checked)
{
    Audio::Settings::instance()->muteCapture(checked);
}

void
CallWidget::on_speakerSlider_sliderMoved(int position)
{
    outputVolume_ = position;
}

void
CallWidget::on_speakerSlider_sliderReleased()
{
    emit Audio::Settings::instance()->setPlaybackVolume(outputVolume_);
}

void
CallWidget::on_micSlider_sliderMoved(int position)
{
    inputVolume_ = position;
}

void
CallWidget::on_micSlider_sliderReleased()
{
    emit Audio::Settings::instance()->setCaptureVolume(inputVolume_);
}

void
CallWidget::atExit() {

}
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

void
CallWidget::on_contactView_doubleClicked(const QModelIndex &index)
{
    auto contact =  index.data((int)ContactMethod::Role::Uri);
    QString uri;

    if (contact.isValid()) {
        uri = contact.toString();
    } else {
        auto var = index.data((int)Person::Role::Object);
        if (var.isValid()) {
            Person* person = var.value<Person*>();
            if (person->phoneNumbers().size() > 0) {
                uri = person->phoneNumbers().at(0)->uri();
            }
        }
    }
    if (not uri.isEmpty()) {
        auto outCall = CallModel::instance()->dialingCall(uri);
        outCall->setDialNumber(uri);
        outCall->performAction(Call::Action::ACCEPT);
    }
}
304

305 306
void
CallWidget::on_historyList_doubleClicked(const QModelIndex &index)
307 308 309 310 311 312 313 314
{
    QString number = index.model()->data(index, static_cast<int>(Call::Role::Number)).toString();
    if (not number.isEmpty()) {
        auto outCall = CallModel::instance()->dialingCall(number);
        outCall->setDialNumber(number);
        outCall->performAction(Call::Action::ACCEPT);
    }
}
315 316 317 318 319 320 321 322

void
CallWidget::setActualCall(Call* value)
{
    actualCall_ = value;
    ui->holdButton->setEnabled(actualCall_ != nullptr);
    ui->hangupButton->setEnabled(actualCall_ != nullptr);
}