callwidget.cpp 22.8 KB
Newer Older
Edric Milaret's avatar
Edric Milaret committed
1
/***************************************************************************
Nicolas Jager's avatar
Nicolas Jager committed
2
 * Copyright (C) 2015-2016 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 23
#include <QClipboard>

24
#include <memory>
Edric Milaret's avatar
Edric Milaret committed
25

26 27 28
//ERROR is defined in windows.h
#include "utils.h"
#undef ERROR
Edric Milaret's avatar
Edric Milaret committed
29
#undef interface
30

Edric Milaret's avatar
Edric Milaret committed
31 32
#include "audio/settings.h"
#include "personmodel.h"
33
#include "person.h"
Edric Milaret's avatar
Edric Milaret committed
34
#include "fallbackpersoncollection.h"
35
#include "categorizedcontactmodel.h"
Edric Milaret's avatar
Edric Milaret committed
36
#include "localhistorycollection.h"
37 38
#include "media/text.h"
#include "media/recording.h"
39
#include "media/recordingmodel.h"
40
#include "recentmodel.h"
41
#include "contactmethod.h"
Edric Milaret's avatar
Edric Milaret committed
42
#include "globalinstances.h"
43

44
#include "wizarddialog.h"
Edric Milaret's avatar
Edric Milaret committed
45
#include "windowscontactbackend.h"
46
#include "contactpicker.h"
47
#include "contactmethodpicker.h"
48
#include "globalsystemtray.h"
49 50 51
#include "historydelegate.h"
#include "contactdelegate.h"
#include "smartlistdelegate.h"
52
#include "imdelegate.h"
Edric Milaret's avatar
Edric Milaret committed
53
#include "pixbufmanipulator.h"
Edric Milaret's avatar
Edric Milaret committed
54

55
CallWidget::CallWidget(QWidget* parent) :
56
    NavWidget(END ,parent),
Edric Milaret's avatar
Edric Milaret committed
57
    ui(new Ui::CallWidget),
58 59
    menu_(new QMenu()),
    imDelegate_(new ImDelegate())
Edric Milaret's avatar
Edric Milaret committed
60
{
61 62
    setMouseTracking(true);

Edric Milaret's avatar
Edric Milaret committed
63 64
    ui->setupUi(this);

Nicolas Jager's avatar
Nicolas Jager committed
65 66 67
    welcomePageAnim_ = new QPropertyAnimation(ui->welcomePage, "pos", this);
    messagingPageAnim_ = new QPropertyAnimation(ui->messagingPage, "pos", this);

68
    setActualCall(nullptr);
Edric Milaret's avatar
Edric Milaret committed
69 70
    videoRenderer_ = nullptr;

71 72 73
    connect(ui->videoWidget, SIGNAL(setChatVisibility(bool)),
            ui->instantMessagingWidget, SLOT(setVisible(bool)));

74 75 76 77
    QPixmap logo(":/images/logo-ring-standard-coul.png");
    ui->ringLogo->setPixmap(logo.scaledToHeight(100, Qt::SmoothTransformation));
    ui->ringLogo->setAlignment(Qt::AlignHCenter);

Edric Milaret's avatar
Edric Milaret committed
78 79
    GlobalInstances::setPixmapManipulator(std::unique_ptr<Interfaces::PixbufManipulator>(new Interfaces::PixbufManipulator()));

Edric Milaret's avatar
Edric Milaret committed
80
    try {
Edric Milaret's avatar
Edric Milaret committed
81
        callModel_ = &CallModel::instance();
Edric Milaret's avatar
Edric Milaret committed
82 83 84 85 86 87 88 89

        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)));

Edric Milaret's avatar
Edric Milaret committed
90
        connect(&AccountModel::instance()
91 92 93 94
                , SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>))
                , this
                , SLOT(findRingAccount(QModelIndex, QModelIndex, QVector<int>)));

95 96
        RecentModel::instance().peopleProxy()->setFilterRole(static_cast<int>(Ring::Role::Name));
        RecentModel::instance().peopleProxy()->setFilterCaseSensitivity(Qt::CaseInsensitive);
Edric Milaret's avatar
Edric Milaret committed
97
        ui->smartList->setModel(RecentModel::instance().peopleProxy());
98 99 100 101
        connect(ui->smartList->selectionModel(),
                SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
                this,
                SLOT(smartListSelectionChanged(QItemSelection,QItemSelection)));
102 103 104

        connect(ui->smartList, &QTreeView::entered, this, &CallWidget::on_entered);

105
        smartListDelegate_ = new SmartListDelegate();
106
        ui->smartList->setSmartListItemDelegate(smartListDelegate_);
Edric Milaret's avatar
Edric Milaret committed
107

Edric Milaret's avatar
Edric Milaret committed
108
        PersonModel::instance().
109
                addCollection<WindowsContactBackend>(LoadOptions::FORCE_ENABLED);
Edric Milaret's avatar
Edric Milaret committed
110

Edric Milaret's avatar
Edric Milaret committed
111 112 113
        CategorizedContactModel::instance().setSortAlphabetical(false);
        CategorizedContactModel::instance().setUnreachableHidden(true);
        ui->contactView->setModel(&CategorizedContactModel::instance());
114 115
        contactDelegate_ = new ContactDelegate();
        ui->contactView->setItemDelegate(contactDelegate_);
116

Edric Milaret's avatar
Edric Milaret committed
117
        CategorizedHistoryModel::instance().
118
                addCollection<LocalHistoryCollection>(LoadOptions::FORCE_ENABLED);
119

Edric Milaret's avatar
Edric Milaret committed
120 121
        ui->historyList->setModel(CategorizedHistoryModel::SortedProxy::instance().model());
        CategorizedHistoryModel::SortedProxy::instance().model()->sort(0, Qt::DescendingOrder);
122
        ui->historyList->setHeaderHidden(true);
Edric Milaret's avatar
Edric Milaret committed
123 124
        historyDelegate_ = new HistoryDelegate();
        ui->historyList->setItemDelegate(historyDelegate_);
125

Edric Milaret's avatar
Edric Milaret committed
126 127
        connect(CategorizedHistoryModel::SortedProxy::instance().model(), &QSortFilterProxyModel::layoutChanged, [=]() {
            auto idx = CategorizedHistoryModel::SortedProxy::instance().model()->index(0,0);
128 129 130 131
            if (idx.isValid())
                ui->historyList->setExpanded(idx, true);
        });

132 133 134 135 136 137 138 139 140
        ui->historyList->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(ui->historyList, &QListView::customContextMenuRequested, [=](const QPoint& pos){
            if (ui->historyList->currentIndex().parent().isValid()) {
                QPoint globalPos = ui->historyList->mapToGlobal(pos);
                QMenu menu;

                ContactMethod* contactMethod = ui->historyList->currentIndex()
                        .data(static_cast<int>(Call::Role::ContactMethod)).value<ContactMethod*>();

141
                auto copyAction = new QAction(tr("Copy number"), this);
142 143 144 145
                menu.addAction(copyAction);
                connect(copyAction, &QAction::triggered, [=]() {
                    QApplication::clipboard()->setText(contactMethod->uri());
                });
146
                if (not contactMethod->contact() || contactMethod->contact()->isPlaceHolder()) {
147
                    auto addExisting = new QAction(tr("Add to contact"), this);
148 149
                    menu.addAction(addExisting);
                    connect(addExisting, &QAction::triggered, [=]() {
150
                        ContactPicker contactPicker(contactMethod);
151
                        contactPicker.move(globalPos.x(), globalPos.y() - (contactPicker.height()/2));
152
                        contactPicker.exec();
153 154 155 156 157
                    });
                }
                menu.exec(globalPos);
            }
        });
Edric Milaret's avatar
Edric Milaret committed
158

Edric Milaret's avatar
Edric Milaret committed
159
        findRingAccount();
Edric Milaret's avatar
Edric Milaret committed
160 161
        setupOutOfCallIM();
        setupSmartListMenu();
Edric Milaret's avatar
Edric Milaret committed
162

Edric Milaret's avatar
Edric Milaret committed
163 164
    } catch (const std::exception& e) {
        qDebug() << "INIT ERROR" << e.what();
Edric Milaret's avatar
Edric Milaret committed
165 166 167 168 169 170
    }
}

CallWidget::~CallWidget()
{
    delete ui;
171
    delete menu_;
Edric Milaret's avatar
Edric Milaret committed
172
    delete contactDelegate_;
173
    delete imDelegate_;
Nicolas Jager's avatar
Nicolas Jager committed
174 175
    delete welcomePageAnim_;
    delete messagingPageAnim_;
Edric Milaret's avatar
Edric Milaret committed
176 177
}

Edric Milaret's avatar
Edric Milaret committed
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
void
CallWidget::setupOutOfCallIM()
{
    ui->listMessageView->setItemDelegate(imDelegate_);
    ui->listMessageView->setContextMenuPolicy(Qt::ActionsContextMenu);

    auto copyAction = new QAction(tr("Copy"), this);
    ui->listMessageView->addAction(copyAction);
    connect(copyAction, &QAction::triggered, [=]() {
        auto idx = ui->listMessageView->currentIndex();
        if (idx.isValid()) {
            auto text = ui->listMessageView->model()->data(idx);
            QApplication::clipboard()->setText(text.value<QString>());
        }
    });
    auto displayDate = new QAction(tr("Display date"), this);
    displayDate->setCheckable(true);
    ui->listMessageView->addAction(displayDate);
    auto displayAuthor = new QAction(tr("Display author"), this);
    displayAuthor->setCheckable(true);
    ui->listMessageView->addAction(displayAuthor);
    auto lamdba = [=](){
        int opts = 0;
        displayAuthor->isChecked() ? opts |= ImDelegate::DisplayOptions::AUTHOR : opts;
        displayDate->isChecked() ? opts |= ImDelegate::DisplayOptions::DATE : opts;
        imDelegate_->setDisplayOptions(static_cast<ImDelegate::DisplayOptions>(opts));
    };
    connect(displayAuthor, &QAction::triggered, lamdba);
    connect(displayDate, &QAction::triggered, lamdba);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

    connect(&::Media::RecordingModel::instance(),
            SIGNAL(newTextMessage(::Media::TextRecording*, ContactMethod*)),
            this,
            SLOT(onIncomingMessage(::Media::TextRecording*, ContactMethod*)));
}

void
CallWidget::onIncomingMessage(::Media::TextRecording* t, ContactMethod* cm) {
    Q_UNUSED(cm)

    if (!QApplication::focusWidget()) {
        auto idx = t->instantTextMessagingModel()->
                index(t->instantTextMessagingModel()->rowCount()-1, 0);
        GlobalSystemTray::instance()
                .showMessage("Ring",
                             QString(tr("Message incoming from %1")).arg(
                                 idx.data((int)Media::TextRecording::Role::AuthorDisplayname).toString()));
        QApplication::alert(this, 5000);
    }
Edric Milaret's avatar
Edric Milaret committed
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
}

void
CallWidget::setupSmartListMenu() {
    ui->smartList->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->smartList, &QListView::customContextMenuRequested, [=](const QPoint& pos){
        auto idx = ui->smartList->currentIndex();
        if (not idx.isValid())
            return;
        QPoint globalPos = ui->smartList->mapToGlobal(pos);
        QMenu menu;

        ContactMethod* contactMethod = RecentModel::instance()
                .getContactMethods(RecentModel::instance().peopleProxy()->mapToSource(idx)).at(0);
        if (not contactMethod)
            return;
        auto copyAction = new QAction(tr("Copy number"), this);
        menu.addAction(copyAction);
        connect(copyAction, &QAction::triggered, [=]() {
            QApplication::clipboard()->setText(contactMethod->uri());
        });
        if (not contactMethod->contact() || contactMethod->contact()->isPlaceHolder()) {
            auto addExisting = new QAction(tr("Add to contact"), this);
            menu.addAction(addExisting);
            connect(addExisting, &QAction::triggered, [=]() {
                ContactPicker contactPicker(contactMethod);
                contactPicker.move(globalPos.x(), globalPos.y() - (contactPicker.height()/2));
                contactPicker.exec();
            });
        }
        menu.exec(globalPos);
    });
}

Edric Milaret's avatar
Edric Milaret committed
261
void
262 263 264 265 266 267
CallWidget::findRingAccount(QModelIndex idx1, QModelIndex idx2, QVector<int> vec)
{
    Q_UNUSED(idx1)
    Q_UNUSED(idx2)
    Q_UNUSED(vec)

Edric Milaret's avatar
Edric Milaret committed
268
    auto a_count = AccountModel::instance().rowCount();
269 270
    auto found = false;
    for (int i = 0; i < a_count; ++i) {
Edric Milaret's avatar
Edric Milaret committed
271
        auto idx = AccountModel::instance().index(i, 0);
272 273 274
        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));
275
            ui->ringIdLabel->setText(username.toString());
276 277 278 279
            found = true;
            return;
        }
    }
280
    if (not found) {
281
        ui->ringIdLabel->setText(tr("NO RING ACCOUNT FOUND"));
282 283 284 285 286 287
    }
}

void
CallWidget::findRingAccount()
{
288

Edric Milaret's avatar
Edric Milaret committed
289
    auto a_count = AccountModel::instance().rowCount();
Edric Milaret's avatar
Edric Milaret committed
290 291
    auto found = false;
    for (int i = 0; i < a_count; ++i) {
Edric Milaret's avatar
Edric Milaret committed
292
        auto idx = AccountModel::instance().index(i, 0);
Edric Milaret's avatar
Edric Milaret committed
293 294
        auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
        if ((Account::Protocol)protocol.toUInt() == Account::Protocol::RING) {
Edric Milaret's avatar
Edric Milaret committed
295
            auto account = AccountModel::instance().getAccountByModelIndex(idx);
296 297 298
            if (account->displayName().isEmpty())
                account->displayName() = account->alias();
            auto username = account->username();
299
            ui->ringIdLabel->setText(username);
Edric Milaret's avatar
Edric Milaret committed
300 301 302 303 304
            found = true;
            return;
        }
    }
    if (!found) {
305
        ui->ringIdLabel->setText(tr("NO RING ACCOUNT FOUND"));
306 307 308
        WizardDialog *wizardDialog = new WizardDialog();
        wizardDialog->exec();
        delete wizardDialog;
Edric Milaret's avatar
Edric Milaret committed
309 310 311 312
    }
}

void
313
CallWidget::callIncoming(Call* call)
Edric Milaret's avatar
Edric Milaret committed
314
{
315 316 317 318
    if (!QApplication::focusWidget()) {
        GlobalSystemTray::instance()
                .showMessage("Ring",
                             QString(tr("Call incoming from %1")).arg(call->formattedName()));
319 320 321
        QApplication::alert(this, 5000);
    }

Edric Milaret's avatar
Edric Milaret committed
322
    if (!call->account()->isAutoAnswer()) {
323
        ui->callerIdLabel->setText(QString(tr("%1", "%1 is the name of the caller"))
Edric Milaret's avatar
Edric Milaret committed
324
                                   .arg(call->formattedName()));
325
        ui->stackedWidget->setCurrentWidget(ui->callInvitePage);
Edric Milaret's avatar
Edric Milaret committed
326
    }
327
    setActualCall(call);
Edric Milaret's avatar
Edric Milaret committed
328 329 330 331 332 333
}

void
CallWidget::on_acceptButton_clicked()
{
    if (actualCall_ != nullptr)
334
        actualCall_->performAction(Call::Action::ACCEPT);
335
    ui->stackedWidget->setCurrentWidget(ui->videoPage);
Edric Milaret's avatar
Edric Milaret committed
336 337 338 339 340 341 342 343
}

void
CallWidget::on_refuseButton_clicked()
{
    if (actualCall_ == nullptr)
        return;
    actualCall_->performAction(Call::Action::REFUSE);
344
    setActualCall(nullptr);
345
    ui->stackedWidget->setCurrentWidget(ui->welcomePage);
Edric Milaret's avatar
Edric Milaret committed
346 347 348
}

void
349 350
CallWidget::addedCall(Call* call, Call* parent)
{
Edric Milaret's avatar
Edric Milaret committed
351 352
    Q_UNUSED(parent);
    if (call->direction() == Call::Direction::OUTGOING) {
353
        setActualCall(call);
Nicolas Jager's avatar
Nicolas Jager committed
354
        ui->stackedWidget->setCurrentWidget(ui->outboundCallPage);
Edric Milaret's avatar
Edric Milaret committed
355 356 357 358
    }
}

void
359 360
CallWidget::callStateChanged(Call* call, Call::State previousState)
{
Edric Milaret's avatar
Edric Milaret committed
361
    Q_UNUSED(previousState)
362 363
    if (call == nullptr
            || call != actualCall_)
364
        return;
365

366
    if (call->state() == Call::State::OVER) {
367
        setActualCall(nullptr);
368
        ui->instantMessagingWidget->setMediaText(nullptr);
369
        ui->stackedWidget->setCurrentWidget(ui->welcomePage);
Edric Milaret's avatar
Edric Milaret committed
370 371 372 373 374 375
        //TODO : Link this so that recentModel get selected correctly
        //        auto onHoldCall = callModel_->getActiveCalls().first();
        //        if (onHoldCall != nullptr && onHoldCall->state() == Call::State::HOLD) {
        //            setActualCall(onHoldCall);
        //            onHoldCall->performAction(Call::Action::HOLD);
        //        }
376
    } else if (call->state() == Call::State::CURRENT) {
377
        ui->instantMessagingWidget->setMediaText(actualCall_);
378
        ui->stackedWidget->setCurrentWidget(ui->videoPage);
Edric Milaret's avatar
Edric Milaret committed
379 380 381 382
    }
}

void
383 384
CallWidget::atExit()
{
Edric Milaret's avatar
Edric Milaret committed
385
}
386 387

void
388
CallWidget::on_contactView_doubleClicked(const QModelIndex& index)
389
{
390 391 392
    if (not index.isValid())
        return;

393
    ContactMethod* uri = nullptr;
394

Edric Milaret's avatar
Edric Milaret committed
395 396 397 398
    auto var = index.child(0,0).data(
                static_cast<int>(Person::Role::Object));
    if (var.isValid()) {
        Person* person = var.value<Person*>();
399 400 401 402 403 404 405 406 407 408
        if (person->phoneNumbers().size() == 1) {
            uri = person->phoneNumbers().at(0);
        } else if (person->phoneNumbers().size() > 1) {
            ContactMethodPicker dlg(person->phoneNumbers());
            auto pos = QCursor::pos();
            dlg.move(pos.x(), pos.y());
            if (dlg.exec())
                uri = dlg.getSelected();
        }
        if (uri) {
Edric Milaret's avatar
Edric Milaret committed
409
            auto outCall = CallModel::instance().dialingCall(uri);
410
            outCall->performAction(Call::Action::ACCEPT);
411 412
            ui->mainTabMenu->setCurrentIndex(0);
            ui->smartList->setFocus();
413 414 415
        }
    }
}
416

417
void
418
CallWidget::on_historyList_doubleClicked(const QModelIndex& index)
419
{
Edric Milaret's avatar
Edric Milaret committed
420 421 422
    if (not index.isValid())
        return;

423 424
    auto number = index.data(static_cast<int>(Call::Role::ContactMethod)).value<ContactMethod*>();
    if (number) {
Edric Milaret's avatar
Edric Milaret committed
425
        auto outCall = CallModel::instance().dialingCall(number);
426
        outCall->performAction(Call::Action::ACCEPT);
427 428
        ui->mainTabMenu->setCurrentIndex(0);
        ui->smartList->setFocus();
429 430
    }
}
431 432 433 434 435

void
CallWidget::setActualCall(Call* value)
{
    actualCall_ = value;
Edric Milaret's avatar
Edric Milaret committed
436 437
    CallModel::instance().selectCall(value);
    ui->videoWidget->pushRenderer(value);
438
}
439

440 441
void
CallWidget::on_cancelButton_clicked()
442 443 444 445
{
    if (actualCall_)
        actualCall_->performAction(Call::Action::REFUSE);
}
446 447

void
448
CallWidget::on_smartList_doubleClicked(const QModelIndex& index)
449
{
Edric Milaret's avatar
Edric Milaret committed
450 451
    auto realIndex = RecentModel::instance().peopleProxy()->mapToSource(index);
    if (RecentModel::instance().hasActiveCall(realIndex))
452 453 454 455 456 457 458 459 460 461
        return;

    ContactMethod* m = nullptr;
    if (auto cm = realIndex.data((int)Call::Role::ContactMethod).value<ContactMethod*>()) {
        m = cm;
    } else {
        if (auto person =  realIndex.data((int)Person::Role::Object).value<Person*>()) {
            m = person->phoneNumbers().first();
        }
    }
Edric Milaret's avatar
Edric Milaret committed
462 463
    if (m && !RecentModel::instance().index(0, 0, realIndex).isValid()) {
        Call* c = CallModel::instance().dialingCall(m);
464 465 466 467 468
        c->performAction(Call::Action::ACCEPT);
    }
}

void
469
CallWidget::smartListSelectionChanged(const QItemSelection& newSel, const QItemSelection& oldSel) {
470 471 472

    Q_UNUSED(oldSel)

473 474 475 476 477
    if (newSel.indexes().empty())
    {
        ui->stackedWidget->setCurrentWidget(ui->welcomePage);
        return;
    }
478
    auto newIdx = newSel.indexes().first();
Edric Milaret's avatar
Edric Milaret committed
479
    if (not newIdx.isValid())
480 481
        return;

482 483
    auto nodeIdx = RecentModel::instance().peopleProxy()->mapToSource(newIdx);
    auto newIdxCall = RecentModel::instance().getActiveCall(nodeIdx);
484

Edric Milaret's avatar
Edric Milaret committed
485
    if (newIdxCall && newIdxCall != actualCall_) {
486 487
        setActualCall(newIdxCall);
        ui->stackedWidget->setCurrentWidget(ui->videoPage);
Edric Milaret's avatar
Edric Milaret committed
488 489 490
    } else if (newIdxCall == nullptr){
        setActualCall(nullptr);
        showIMOutOfCall();
491 492 493 494 495 496 497 498 499
    } else {
        setActualCall(nullptr);
        ui->stackedWidget->setCurrentWidget(ui->welcomePage);
    }
}

void
CallWidget::placeCall()
{
500
    if (ui->ringContactLineEdit->text().isEmpty())
501
        return;
502
    Call* c = CallModel::instance().dialingCall(PhoneDirectoryModel::instance().getNumber(ui->ringContactLineEdit->text()));
503
    c->performAction(Call::Action::ACCEPT);
504
    ui->ringContactLineEdit->clear();
505 506 507
}

void
508
CallWidget::settingsButton_clicked()
509 510 511 512 513
{
    emit NavigationRequested(ScreenEnum::ConfScreen);
}

void
514
CallWidget::contactButton_clicked(bool checked)
515 516 517 518 519
{
    ui->mainTabMenu->setCurrentIndex(checked ? 1 : 0);
}

void
520
CallWidget::historicButton_clicked(bool checked)
521 522 523
{
    ui->mainTabMenu->setCurrentIndex(checked ? 2 : 0);
}
524 525 526 527

void
CallWidget::on_ringContactLineEdit_returnPressed()
{
528 529 530 531
    auto cm = PhoneDirectoryModel::instance().getNumber(ui->ringContactLineEdit->text());
    time_t currentTime;
    ::time(&currentTime);
    cm->setLastUsed(currentTime);
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
}

void
CallWidget::on_btnCall_clicked()
{
    placeCall();
}

void
CallWidget::on_btnvideo_clicked()
{
    if (not highLightedIndex_.isValid())
        return;

    on_smartList_doubleClicked(highLightedIndex_);
}
548 549

void
Edric Milaret's avatar
Edric Milaret committed
550
CallWidget::showIMOutOfCall()
551 552 553 554 555 556 557 558 559
{
    if (not highLightedIndex_.isValid())
        return;

    ui->smartList->selectionModel()->select(highLightedIndex_, QItemSelectionModel::ClearAndSelect);

    ui->contactMethodComboBox->clear();

    auto nodeIdx = RecentModel::instance().peopleProxy()->mapToSource(highLightedIndex_);
Edric Milaret's avatar
Edric Milaret committed
560 561
    ui->imNameLabel->setText(QString(tr("Conversation with %1", "%1 is the contact name"))
                             .arg(nodeIdx.data(static_cast<int>(Ring::Role::Name)).toString()));
562
    auto cmVector = RecentModel::instance().getContactMethods(nodeIdx);
Edric Milaret's avatar
Edric Milaret committed
563
    ui->contactMethodComboBox->setEnabled(cmVector.size() > 1);
564
    foreach (const ContactMethod* cm, cmVector) {
Edric Milaret's avatar
Edric Milaret committed
565
        ui->contactMethodComboBox->addItem(cm->uri());
566
    }
Nicolas Jager's avatar
Nicolas Jager committed
567
    slideToRight(messagingPageAnim_, ui->messagingPage);
568 569 570 571 572
}

void
CallWidget::on_sendButton_clicked()
{
Edric Milaret's avatar
Edric Milaret committed
573
    if (ui->messageEdit->text().trimmed().isEmpty())
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
        return;
    auto number = ui->contactMethodComboBox->currentText();
    if (auto cm = PhoneDirectoryModel::instance().getNumber(number)) {
        QMap<QString, QString> msg;
        msg["text/plain"] = ui->messageEdit->text();
        cm->sendOfflineTextMessage(msg);
        ui->messageEdit->clear();
    } else {
        qWarning() << "Contact Method not found for " << number;
    }
}

void
CallWidget::on_messageEdit_returnPressed()
{
    on_sendButton_clicked();
}

void
CallWidget::on_contactMethodComboBox_currentIndexChanged(const QString& number)
{
    auto cm = PhoneDirectoryModel::instance().getNumber(number);
    if (auto txtRecording = cm->textRecording()) {
        ui->listMessageView->setModel(txtRecording->instantMessagingModel());
        disconnect(imConnection_);
        imConnection_ = connect(txtRecording,
Edric Milaret's avatar
Edric Milaret committed
600 601 602
                                SIGNAL(messageInserted(QMap<QString,QString>,ContactMethod*,Media::Media::Direction)),
                                this,
                                SLOT(slotAccountMessageReceived(QMap<QString,QString>,ContactMethod*,Media::Media::Direction)));
603 604 605 606 607 608 609 610
        auto messagesPresent = txtRecording->instantMessagingModel()->rowCount() > 0;
        if (messagesPresent) {
            ui->listMessageView->scrollToBottom();
            txtRecording->setAllRead();
        }
        ui->listMessageView->setVisible(messagesPresent);
        ui->noMessagesLabel->setVisible(!messagesPresent);
        if (not messagesPresent) {
Edric Milaret's avatar
Edric Milaret committed
611 612 613 614 615 616
            imVisibleConnection_ = connect(txtRecording->instantMessagingModel(),
                                           &QAbstractItemModel::rowsInserted,
                                           [this, txtRecording]() {
                if (imVisibleConnection_)
                    disconnect(imVisibleConnection_);
                auto messagesPresent = txtRecording->instantMessagingModel()->rowCount() > 0;
617 618 619 620
                ui->listMessageView->setVisible(messagesPresent);
                ui->noMessagesLabel->setVisible(!messagesPresent);
            });
        }
621 622 623 624 625 626 627 628 629 630 631 632
    }
}

void
CallWidget::slotAccountMessageReceived(const QMap<QString,QString> message,
                                       ContactMethod* cm,
                                       Media::Media::Direction dir)
{
    Q_UNUSED(message)
    Q_UNUSED(dir)

    ui->listMessageView->scrollToBottom();
633
    cm->textRecording()->setAllRead();
634
}
635 636 637 638 639 640

void
CallWidget::on_ringContactLineEdit_textEdited(const QString& text)
{
    RecentModel::instance().peopleProxy()->setFilterWildcard(text);
}
Edric Milaret's avatar
Edric Milaret committed
641

Nicolas Jager's avatar
Nicolas Jager committed
642 643
void
CallWidget::on_imBackButton_clicked()
Edric Milaret's avatar
Edric Milaret committed
644
{
Nicolas Jager's avatar
Nicolas Jager committed
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
    slideToLeft(welcomePageAnim_, ui->welcomePage);
}

void
CallWidget::slideToLeft(QPropertyAnimation* anim, QWidget* widget)
{
    ui->stackedWidget->setCurrentWidget(widget);
    anim->setDuration(animDuration_);
    anim->setStartValue(QPoint(widget->width(), widget->y()));
    anim->setEndValue(QPoint(widget->x(), widget->y()));
    anim->setEasingCurve(QEasingCurve::OutQuad);
    anim->start();
}

void
CallWidget::slideToRight(QPropertyAnimation* anim, QWidget* widget)
{
    ui->stackedWidget->setCurrentWidget(widget);
    anim->setDuration(animDuration_);
    anim->setStartValue(QPoint(-widget->width(), widget->y()));
    anim->setEndValue(QPoint(widget->x(), widget->y()));
    anim->setEasingCurve(QEasingCurve::OutQuad);
    anim->start();
Edric Milaret's avatar
Edric Milaret committed
668
}