blob: 100fa4a55d30ffe20e1d03babb52133ef84c0d2a [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"
Edric Milaret67007d12015-05-07 09:40:09 -040029#include "categorizedcontactmodel.h"
30#include "windowscontactbackend.h"
Edric Milaret627500d2015-03-27 16:41:40 -040031
Edric Milaret559bda52015-04-29 17:02:31 -040032#include "wizarddialog.h"
Edric Milaret627500d2015-03-27 16:41:40 -040033
34CallWidget::CallWidget(QWidget *parent) :
35 NavWidget(Main ,parent),
36 ui(new Ui::CallWidget)
37{
38 ui->setupUi(this);
39
40 ui->holdButton->setCheckable(true);
41 ui->muteMicButton->setCheckable(true);
42 ui->muteSpeakerButton->setCheckable(true);
43 ui->callInvite->setVisible(false);
44
45 actualCall_ = nullptr;
46 videoRenderer_ = nullptr;
47
48 try {
49 callModel_ = CallModel::instance();
50
51 connect(callModel_, SIGNAL(incomingCall(Call*)),
52 this, SLOT(callIncoming(Call*)));
53 connect(callModel_, SIGNAL(callAdded(Call*,Call*)),
54 this, SLOT(addedCall(Call*, Call*)));
55 connect(callModel_, SIGNAL(callStateChanged(Call*, Call::State)),
56 this, SLOT(callStateChanged(Call*, Call::State)));
57
Edric Milaret67007d12015-05-07 09:40:09 -040058 connect(AccountModel::instance()
59 , SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>))
60 , this
61 , SLOT(findRingAccount(QModelIndex, QModelIndex, QVector<int>)));
62
Edric Milaret627500d2015-03-27 16:41:40 -040063 ui->callList->setModel(callModel_);
64
65 CategorizedHistoryModel::instance()->
66 addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED);
67
Edric Milaret67007d12015-05-07 09:40:09 -040068 PersonModel::instance()->
69 addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED);
70
71 PersonModel::instance()->
72 addCollection<WindowsContactBackend>(LoadOptions::FORCE_ENABLED);
73
Edric Milaret627500d2015-03-27 16:41:40 -040074 ui->historyList->setModel(CategorizedHistoryModel::instance());
Edric Milaret67007d12015-05-07 09:40:09 -040075 CategorizedContactModel::instance()->setSortAlphabetical(false);
76 ui->contactView->setModel(CategorizedContactModel::instance());
77 ui->contactView->setHeaderHidden(true);
Edric Milaret627500d2015-03-27 16:41:40 -040078 ui->speakerSlider->setValue(Audio::Settings::instance()->playbackVolume());
79 ui->micSlider->setValue(Audio::Settings::instance()->captureVolume());
80
81 findRingAccount();
82
83 } catch (...) {
84 qDebug() << "INIT ERROR";
85 }
86}
87
88CallWidget::~CallWidget()
89{
90 delete ui;
91}
92
93void
Edric Milaret67007d12015-05-07 09:40:09 -040094CallWidget::findRingAccount(QModelIndex idx1, QModelIndex idx2, QVector<int> vec)
95{
96 Q_UNUSED(idx1)
97 Q_UNUSED(idx2)
98 Q_UNUSED(vec)
99
100 auto a_count = AccountModel::instance()->rowCount();
101 auto found = false;
102 for (int i = 0; i < a_count; ++i) {
103 auto idx = AccountModel::instance()->index(i, 0);
104 auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
105 if ((Account::Protocol)protocol.toUInt() == Account::Protocol::RING) {
106 auto username = idx.data(static_cast<int>(Account::Role::Username));
107 ui->ringIdLabel->setText(
108 "Your Ring ID: " + username.toString());
109 found = true;
110 return;
111 }
112 }
113 if (not found){
114 ui->ringIdLabel->setText("NO RING ACCOUNT FOUND");
115 }
116}
117
118void
119CallWidget::findRingAccount()
120{
Edric Milaret559bda52015-04-29 17:02:31 -0400121
Edric Milaret627500d2015-03-27 16:41:40 -0400122 auto a_count = AccountModel::instance()->rowCount();
123 auto found = false;
124 for (int i = 0; i < a_count; ++i) {
125 auto idx = AccountModel::instance()->index(i, 0);
126 auto protocol = idx.data(static_cast<int>(Account::Role::Proto));
127 if ((Account::Protocol)protocol.toUInt() == Account::Protocol::RING) {
128 auto username = idx.data(static_cast<int>(Account::Role::Username));
129 ui->ringIdLabel->setText(
130 ui->ringIdLabel->text() + " " + username.toString());
131 found = true;
132 return;
133 }
134 }
135 if (!found) {
Edric Milaret559bda52015-04-29 17:02:31 -0400136 WizardDialog *wizardDialog = new WizardDialog();
137 wizardDialog->exec();
138 delete wizardDialog;
Edric Milaret627500d2015-03-27 16:41:40 -0400139 }
140}
141
142void
143CallWidget::callIncoming(Call *call)
144{
145 if (!call->account()->isAutoAnswer())
146 ui->callInvite->setVisible(true);
147 actualCall_ = call;
148}
149
150void
151CallWidget::on_acceptButton_clicked()
152{
153 if (actualCall_ != nullptr)
154 actualCall_->performAction(Call::Action::ACCEPT);
155 ui->callInvite->setVisible(false);
156}
157
158void
159CallWidget::on_refuseButton_clicked()
160{
161 if (actualCall_ == nullptr)
162 return;
163 actualCall_->performAction(Call::Action::REFUSE);
164 actualCall_ = nullptr;
165 ui->callInvite->setVisible(false);
166}
167
168void
169CallWidget::on_holdButton_toggled(bool checked)
170{
171 Q_UNUSED(checked)
172 if (actualCall_ == nullptr)
173 return;
174 actualCall_->performAction(Call::Action::HOLD);
175}
176
177void
178CallWidget::on_hangupButton_clicked()
179{
180 if (actualCall_ == nullptr)
181 return;
182 actualCall_->performAction(Call::Action::REFUSE);
183}
184
185void
186CallWidget::addedCall(Call* call, Call* parent) {
187 Q_UNUSED(parent);
188 if (call->direction() == Call::Direction::OUTGOING) {
189 actualCall_ = call;
190 }
191}
192
193void
Edric Milaret67007d12015-05-07 09:40:09 -0400194CallWidget::callStateChanged(Call* call, Call::State previousState)
195{
Edric Milaret627500d2015-03-27 16:41:40 -0400196 Q_UNUSED(previousState)
Edric Milaretb5b49072015-05-08 11:56:07 -0400197 if (call == nullptr)
198 return;
Edric Milaret627500d2015-03-27 16:41:40 -0400199 ui->callList->setCurrentIndex(callModel_->getIndex(actualCall_));
200 if (call->state() == Call::State::OVER) {
201 actualCall_ = nullptr;
202 ui->videoWidget->hide();
Edric Milaret67007d12015-05-07 09:40:09 -0400203 } else if (call->state() == Call::State::HOLD) {
204 ui->videoWidget->hide();
Edric Milaretb5b49072015-05-08 11:56:07 -0400205 } else if (call->state() == Call::State::CURRENT){
Edric Milaret627500d2015-03-27 16:41:40 -0400206 ui->videoWidget->show();
Edric Milaret627500d2015-03-27 16:41:40 -0400207 ui->messageOutput->setModel(
208 IMConversationManager::instance()->getModel(actualCall_));
209 }
Edric Milaret67007d12015-05-07 09:40:09 -0400210 ui->callStateLabel->setText("Call State : " + state.at((int)call->state()));
Edric Milaret627500d2015-03-27 16:41:40 -0400211}
212
213void
214CallWidget::on_callList_activated(const QModelIndex &index)
215{
216 Call *callSelected = callModel_->getCall(index);
217 if (actualCall_ != nullptr) {
218 if (callSelected == actualCall_)
219 return;
Edric Milaret72c406d2015-04-28 13:23:54 -0400220 ui->videoWidget->hide();
Edric Milaret627500d2015-03-27 16:41:40 -0400221 actualCall_->performAction(Call::Action::HOLD);
222 }
223 actualCall_ = callSelected;
224 actualCall_->performAction(Call::Action::HOLD);
Edric Milaret72c406d2015-04-28 13:23:54 -0400225 ui->videoWidget->show();
Edric Milaret627500d2015-03-27 16:41:40 -0400226}
227
228void
229CallWidget::on_muteSpeakerButton_toggled(bool checked)
230{
231 Audio::Settings::instance()->mutePlayback(checked);
232}
233
234void
235CallWidget::on_muteMicButton_toggled(bool checked)
236{
237 Audio::Settings::instance()->muteCapture(checked);
238}
239
240void
241CallWidget::on_speakerSlider_sliderMoved(int position)
242{
243 outputVolume_ = position;
244}
245
246void
247CallWidget::on_speakerSlider_sliderReleased()
248{
249 emit Audio::Settings::instance()->setPlaybackVolume(outputVolume_);
250}
251
252void
253CallWidget::on_micSlider_sliderMoved(int position)
254{
255 inputVolume_ = position;
256}
257
258void
259CallWidget::on_micSlider_sliderReleased()
260{
261 emit Audio::Settings::instance()->setCaptureVolume(inputVolume_);
262}
263
264void
265CallWidget::atExit() {
266
267}
Edric Milaret67007d12015-05-07 09:40:09 -0400268
269void
270CallWidget::on_contactView_doubleClicked(const QModelIndex &index)
271{
272 auto contact = index.data((int)ContactMethod::Role::Uri);
273 QString uri;
274
275 if (contact.isValid()) {
276 uri = contact.toString();
277 } else {
278 auto var = index.data((int)Person::Role::Object);
279 if (var.isValid()) {
280 Person* person = var.value<Person*>();
281 if (person->phoneNumbers().size() > 0) {
282 uri = person->phoneNumbers().at(0)->uri();
283 }
284 }
285 }
286 if (not uri.isEmpty()) {
287 auto outCall = CallModel::instance()->dialingCall(uri);
288 outCall->setDialNumber(uri);
289 outCall->performAction(Call::Action::ACCEPT);
290 }
291}