blob: fefa346282415baf853f12b4221e586b34810b45 [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)
197 ui->callList->setCurrentIndex(callModel_->getIndex(actualCall_));
198 if (call->state() == Call::State::OVER) {
199 actualCall_ = nullptr;
200 ui->videoWidget->hide();
Edric Milaret67007d12015-05-07 09:40:09 -0400201 } else if (call->state() == Call::State::HOLD) {
202 ui->videoWidget->hide();
Edric Milaret627500d2015-03-27 16:41:40 -0400203 } else {
204 ui->videoWidget->show();
Edric Milaret627500d2015-03-27 16:41:40 -0400205 ui->messageOutput->setModel(
206 IMConversationManager::instance()->getModel(actualCall_));
207 }
Edric Milaret67007d12015-05-07 09:40:09 -0400208 ui->callStateLabel->setText("Call State : " + state.at((int)call->state()));
Edric Milaret627500d2015-03-27 16:41:40 -0400209}
210
211void
212CallWidget::on_callList_activated(const QModelIndex &index)
213{
214 Call *callSelected = callModel_->getCall(index);
215 if (actualCall_ != nullptr) {
216 if (callSelected == actualCall_)
217 return;
Edric Milaret72c406d2015-04-28 13:23:54 -0400218 ui->videoWidget->hide();
Edric Milaret627500d2015-03-27 16:41:40 -0400219 actualCall_->performAction(Call::Action::HOLD);
220 }
221 actualCall_ = callSelected;
222 actualCall_->performAction(Call::Action::HOLD);
Edric Milaret72c406d2015-04-28 13:23:54 -0400223 ui->videoWidget->show();
Edric Milaret627500d2015-03-27 16:41:40 -0400224}
225
226void
227CallWidget::on_muteSpeakerButton_toggled(bool checked)
228{
229 Audio::Settings::instance()->mutePlayback(checked);
230}
231
232void
233CallWidget::on_muteMicButton_toggled(bool checked)
234{
235 Audio::Settings::instance()->muteCapture(checked);
236}
237
238void
239CallWidget::on_speakerSlider_sliderMoved(int position)
240{
241 outputVolume_ = position;
242}
243
244void
245CallWidget::on_speakerSlider_sliderReleased()
246{
247 emit Audio::Settings::instance()->setPlaybackVolume(outputVolume_);
248}
249
250void
251CallWidget::on_micSlider_sliderMoved(int position)
252{
253 inputVolume_ = position;
254}
255
256void
257CallWidget::on_micSlider_sliderReleased()
258{
259 emit Audio::Settings::instance()->setCaptureVolume(inputVolume_);
260}
261
262void
263CallWidget::atExit() {
264
265}
Edric Milaret67007d12015-05-07 09:40:09 -0400266
267void
268CallWidget::on_contactView_doubleClicked(const QModelIndex &index)
269{
270 auto contact = index.data((int)ContactMethod::Role::Uri);
271 QString uri;
272
273 if (contact.isValid()) {
274 uri = contact.toString();
275 } else {
276 auto var = index.data((int)Person::Role::Object);
277 if (var.isValid()) {
278 Person* person = var.value<Person*>();
279 if (person->phoneNumbers().size() > 0) {
280 uri = person->phoneNumbers().at(0)->uri();
281 }
282 }
283 }
284 if (not uri.isEmpty()) {
285 auto outCall = CallModel::instance()->dialingCall(uri);
286 outCall->setDialNumber(uri);
287 outCall->performAction(Call::Action::ACCEPT);
288 }
289}