blob: 3203714fca0cbc19ba1c5245d33f430f6276a7c7 [file] [log] [blame]
Edric Milaret627500d2015-03-27 16:41:40 -04001/***************************************************************************
Edric Milaret4bba46d2015-04-29 16:33:38 -04002 * Copyright (C) 2015 by Savoir-Faire Linux *
Edric Milaret627500d2015-03-27 16:41:40 -04003 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
19#include "callwidget.h"
20#include "ui_callwidget.h"
21
22
23#include "imconversationmanager.h"
24#include "instantmessagingmodel.h"
25#include "audio/settings.h"
26#include "personmodel.h"
27#include "fallbackpersoncollection.h"
28#include "accountmodel.h"
29
Edric Milaret559bda52015-04-29 17:02:31 -040030#include "wizarddialog.h"
Edric Milaret627500d2015-03-27 16:41:40 -040031
32CallWidget::CallWidget(QWidget *parent) :
33 NavWidget(Main ,parent),
34 ui(new Ui::CallWidget)
35{
36 ui->setupUi(this);
37
38 ui->holdButton->setCheckable(true);
39 ui->muteMicButton->setCheckable(true);
40 ui->muteSpeakerButton->setCheckable(true);
41 ui->callInvite->setVisible(false);
42
43 actualCall_ = nullptr;
44 videoRenderer_ = nullptr;
45
46 try {
47 callModel_ = CallModel::instance();
48
49 connect(callModel_, SIGNAL(incomingCall(Call*)),
50 this, SLOT(callIncoming(Call*)));
51 connect(callModel_, SIGNAL(callAdded(Call*,Call*)),
52 this, SLOT(addedCall(Call*, Call*)));
53 connect(callModel_, SIGNAL(callStateChanged(Call*, Call::State)),
54 this, SLOT(callStateChanged(Call*, Call::State)));
55
56 ui->callList->setModel(callModel_);
57
58 CategorizedHistoryModel::instance()->
59 addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
60
61 ui->historyList->setModel(CategorizedHistoryModel::instance());
62 ui->speakerSlider->setValue(Audio::Settings::instance()->playbackVolume());
63 ui->micSlider->setValue(Audio::Settings::instance()->captureVolume());
64
65 findRingAccount();
66
67 } catch (...) {
68 qDebug() << "INIT ERROR";
69 }
70}
71
72CallWidget::~CallWidget()
73{
74 delete ui;
75}
76
77void
78CallWidget::findRingAccount() {
Edric Milaret559bda52015-04-29 17:02:31 -040079
Edric Milaret627500d2015-03-27 16:41:40 -040080 auto a_count = AccountModel::instance()->rowCount();
81 auto found = false;
82 for (int i = 0; i < a_count; ++i) {
83 auto idx = AccountModel::instance()->index(i, 0);
84 auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
85 if ((Account::Protocol)protocol.toUInt() == Account::Protocol::RING) {
86 auto username = idx.data(static_cast<int>(Account::Role::Username));
87 ui->ringIdLabel->setText(
88 ui->ringIdLabel->text() + " " + username.toString());
89 found = true;
90 return;
91 }
92 }
93 if (!found) {
Edric Milaret559bda52015-04-29 17:02:31 -040094 WizardDialog *wizardDialog = new WizardDialog();
95 wizardDialog->exec();
96 delete wizardDialog;
Edric Milaret627500d2015-03-27 16:41:40 -040097 findRingAccount();
98 }
99}
100
101void
102CallWidget::callIncoming(Call *call)
103{
104 if (!call->account()->isAutoAnswer())
105 ui->callInvite->setVisible(true);
106 actualCall_ = call;
107}
108
109void
110CallWidget::on_acceptButton_clicked()
111{
112 if (actualCall_ != nullptr)
113 actualCall_->performAction(Call::Action::ACCEPT);
114 ui->callInvite->setVisible(false);
115}
116
117void
118CallWidget::on_refuseButton_clicked()
119{
120 if (actualCall_ == nullptr)
121 return;
122 actualCall_->performAction(Call::Action::REFUSE);
123 actualCall_ = nullptr;
124 ui->callInvite->setVisible(false);
125}
126
127void
128CallWidget::on_holdButton_toggled(bool checked)
129{
130 Q_UNUSED(checked)
131 if (actualCall_ == nullptr)
132 return;
133 actualCall_->performAction(Call::Action::HOLD);
134}
135
136void
137CallWidget::on_hangupButton_clicked()
138{
139 if (actualCall_ == nullptr)
140 return;
141 actualCall_->performAction(Call::Action::REFUSE);
142}
143
144void
145CallWidget::addedCall(Call* call, Call* parent) {
146 Q_UNUSED(parent);
147 if (call->direction() == Call::Direction::OUTGOING) {
148 actualCall_ = call;
149 }
150}
151
152void
153CallWidget::callStateChanged(Call* call, Call::State previousState) {
154 Q_UNUSED(previousState)
155 ui->callList->setCurrentIndex(callModel_->getIndex(actualCall_));
156 if (call->state() == Call::State::OVER) {
157 actualCall_ = nullptr;
158 ui->videoWidget->hide();
159 } else {
160 ui->videoWidget->show();
161 connect(actualCall_, SIGNAL(isOver(Call*)),
162 this, SLOT(callStateChanged(Call*,Call::State::OVER)));
163 ui->messageOutput->setModel(
164 IMConversationManager::instance()->getModel(actualCall_));
165 }
166}
167
168void
169CallWidget::on_callList_activated(const QModelIndex &index)
170{
171 Call *callSelected = callModel_->getCall(index);
172 if (actualCall_ != nullptr) {
173 if (callSelected == actualCall_)
174 return;
Edric Milaret72c406d2015-04-28 13:23:54 -0400175 ui->videoWidget->hide();
Edric Milaret627500d2015-03-27 16:41:40 -0400176 actualCall_->performAction(Call::Action::HOLD);
177 }
178 actualCall_ = callSelected;
179 actualCall_->performAction(Call::Action::HOLD);
Edric Milaret72c406d2015-04-28 13:23:54 -0400180 ui->videoWidget->show();
Edric Milaret627500d2015-03-27 16:41:40 -0400181}
182
183void
184CallWidget::on_muteSpeakerButton_toggled(bool checked)
185{
186 Audio::Settings::instance()->mutePlayback(checked);
187}
188
189void
190CallWidget::on_muteMicButton_toggled(bool checked)
191{
192 Audio::Settings::instance()->muteCapture(checked);
193}
194
195void
196CallWidget::on_speakerSlider_sliderMoved(int position)
197{
198 outputVolume_ = position;
199}
200
201void
202CallWidget::on_speakerSlider_sliderReleased()
203{
204 emit Audio::Settings::instance()->setPlaybackVolume(outputVolume_);
205}
206
207void
208CallWidget::on_micSlider_sliderMoved(int position)
209{
210 inputVolume_ = position;
211}
212
213void
214CallWidget::on_micSlider_sliderReleased()
215{
216 emit Audio::Settings::instance()->setCaptureVolume(inputVolume_);
217}
218
219void
220CallWidget::atExit() {
221
222}