blob: fae63aaafeb27bed36d63175577de6b5a077c7d3 [file] [log] [blame]
Edric Milaret627500d2015-03-27 16:41:40 -04001/***************************************************************************
2 * Copyright (C) 2011-2015 by Savoir-Faire Linux *
3 * 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
30
31CallWidget::CallWidget(QWidget *parent) :
32 NavWidget(Main ,parent),
33 ui(new Ui::CallWidget)
34{
35 ui->setupUi(this);
36
37 ui->holdButton->setCheckable(true);
38 ui->muteMicButton->setCheckable(true);
39 ui->muteSpeakerButton->setCheckable(true);
40 ui->callInvite->setVisible(false);
41
42 actualCall_ = nullptr;
43 videoRenderer_ = nullptr;
44
45 try {
46 callModel_ = CallModel::instance();
47
48 connect(callModel_, SIGNAL(incomingCall(Call*)),
49 this, SLOT(callIncoming(Call*)));
50 connect(callModel_, SIGNAL(callAdded(Call*,Call*)),
51 this, SLOT(addedCall(Call*, Call*)));
52 connect(callModel_, SIGNAL(callStateChanged(Call*, Call::State)),
53 this, SLOT(callStateChanged(Call*, Call::State)));
54
55 ui->callList->setModel(callModel_);
56
57 CategorizedHistoryModel::instance()->
58 addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
59
60 ui->historyList->setModel(CategorizedHistoryModel::instance());
61 ui->speakerSlider->setValue(Audio::Settings::instance()->playbackVolume());
62 ui->micSlider->setValue(Audio::Settings::instance()->captureVolume());
63
64 findRingAccount();
65
66 } catch (...) {
67 qDebug() << "INIT ERROR";
68 }
69}
70
71CallWidget::~CallWidget()
72{
73 delete ui;
74}
75
76void
77CallWidget::findRingAccount() {
78 auto a_count = AccountModel::instance()->rowCount();
79 auto found = false;
80 for (int i = 0; i < a_count; ++i) {
81 auto idx = AccountModel::instance()->index(i, 0);
82 auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
83 if ((Account::Protocol)protocol.toUInt() == Account::Protocol::RING) {
84 auto username = idx.data(static_cast<int>(Account::Role::Username));
85 ui->ringIdLabel->setText(
86 ui->ringIdLabel->text() + " " + username.toString());
87 found = true;
88 return;
89 }
90 }
91 if (!found) {
92 AccountModel::instance()->add("RING", Account::Protocol::RING);
93 AccountModel::instance()->save();
94 findRingAccount();
95 }
96}
97
98void
99CallWidget::callIncoming(Call *call)
100{
101 if (!call->account()->isAutoAnswer())
102 ui->callInvite->setVisible(true);
103 actualCall_ = call;
104}
105
106void
107CallWidget::on_acceptButton_clicked()
108{
109 if (actualCall_ != nullptr)
110 actualCall_->performAction(Call::Action::ACCEPT);
111 ui->callInvite->setVisible(false);
112}
113
114void
115CallWidget::on_refuseButton_clicked()
116{
117 if (actualCall_ == nullptr)
118 return;
119 actualCall_->performAction(Call::Action::REFUSE);
120 actualCall_ = nullptr;
121 ui->callInvite->setVisible(false);
122}
123
124void
125CallWidget::on_holdButton_toggled(bool checked)
126{
127 Q_UNUSED(checked)
128 if (actualCall_ == nullptr)
129 return;
130 actualCall_->performAction(Call::Action::HOLD);
131}
132
133void
134CallWidget::on_hangupButton_clicked()
135{
136 if (actualCall_ == nullptr)
137 return;
138 actualCall_->performAction(Call::Action::REFUSE);
139}
140
141void
142CallWidget::addedCall(Call* call, Call* parent) {
143 Q_UNUSED(parent);
144 if (call->direction() == Call::Direction::OUTGOING) {
145 actualCall_ = call;
146 }
147}
148
149void
150CallWidget::callStateChanged(Call* call, Call::State previousState) {
151 Q_UNUSED(previousState)
152 ui->callList->setCurrentIndex(callModel_->getIndex(actualCall_));
153 if (call->state() == Call::State::OVER) {
154 actualCall_ = nullptr;
155 ui->videoWidget->hide();
156 } else {
157 ui->videoWidget->show();
158 connect(actualCall_, SIGNAL(isOver(Call*)),
159 this, SLOT(callStateChanged(Call*,Call::State::OVER)));
160 ui->messageOutput->setModel(
161 IMConversationManager::instance()->getModel(actualCall_));
162 }
163}
164
165void
166CallWidget::on_callList_activated(const QModelIndex &index)
167{
168 Call *callSelected = callModel_->getCall(index);
169 if (actualCall_ != nullptr) {
170 if (callSelected == actualCall_)
171 return;
Edric Milaret72c406d2015-04-28 13:23:54 -0400172 ui->videoWidget->hide();
Edric Milaret627500d2015-03-27 16:41:40 -0400173 actualCall_->performAction(Call::Action::HOLD);
174 }
175 actualCall_ = callSelected;
176 actualCall_->performAction(Call::Action::HOLD);
Edric Milaret72c406d2015-04-28 13:23:54 -0400177 ui->videoWidget->show();
Edric Milaret627500d2015-03-27 16:41:40 -0400178}
179
180void
181CallWidget::on_muteSpeakerButton_toggled(bool checked)
182{
183 Audio::Settings::instance()->mutePlayback(checked);
184}
185
186void
187CallWidget::on_muteMicButton_toggled(bool checked)
188{
189 Audio::Settings::instance()->muteCapture(checked);
190}
191
192void
193CallWidget::on_speakerSlider_sliderMoved(int position)
194{
195 outputVolume_ = position;
196}
197
198void
199CallWidget::on_speakerSlider_sliderReleased()
200{
201 emit Audio::Settings::instance()->setPlaybackVolume(outputVolume_);
202}
203
204void
205CallWidget::on_micSlider_sliderMoved(int position)
206{
207 inputVolume_ = position;
208}
209
210void
211CallWidget::on_micSlider_sliderReleased()
212{
213 emit Audio::Settings::instance()->setCaptureVolume(inputVolume_);
214}
215
216void
217CallWidget::atExit() {
218
219}