Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 1 | /*************************************************************************** |
Edric Milaret | 4bba46d | 2015-04-29 16:33:38 -0400 | [diff] [blame] | 2 | * Copyright (C) 2015 by Savoir-Faire Linux * |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 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" |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 29 | #include "categorizedcontactmodel.h" |
| 30 | #include "windowscontactbackend.h" |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 31 | |
Edric Milaret | 559bda5 | 2015-04-29 17:02:31 -0400 | [diff] [blame] | 32 | #include "wizarddialog.h" |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 33 | |
| 34 | CallWidget::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 Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 58 | connect(AccountModel::instance() |
| 59 | , SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)) |
| 60 | , this |
| 61 | , SLOT(findRingAccount(QModelIndex, QModelIndex, QVector<int>))); |
| 62 | |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 63 | ui->callList->setModel(callModel_); |
| 64 | |
| 65 | CategorizedHistoryModel::instance()-> |
| 66 | addCollection<MinimalHistoryBackend>(LoadOptions::FORCE_ENABLED); |
| 67 | |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 68 | PersonModel::instance()-> |
| 69 | addCollection<FallbackPersonCollection>(LoadOptions::FORCE_ENABLED); |
| 70 | |
| 71 | PersonModel::instance()-> |
| 72 | addCollection<WindowsContactBackend>(LoadOptions::FORCE_ENABLED); |
| 73 | |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 74 | ui->historyList->setModel(CategorizedHistoryModel::instance()); |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 75 | CategorizedContactModel::instance()->setSortAlphabetical(false); |
| 76 | ui->contactView->setModel(CategorizedContactModel::instance()); |
| 77 | ui->contactView->setHeaderHidden(true); |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 78 | 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 | |
| 88 | CallWidget::~CallWidget() |
| 89 | { |
| 90 | delete ui; |
| 91 | } |
| 92 | |
| 93 | void |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 94 | CallWidget::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 | |
| 118 | void |
| 119 | CallWidget::findRingAccount() |
| 120 | { |
Edric Milaret | 559bda5 | 2015-04-29 17:02:31 -0400 | [diff] [blame] | 121 | |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 122 | 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 Milaret | 559bda5 | 2015-04-29 17:02:31 -0400 | [diff] [blame] | 136 | WizardDialog *wizardDialog = new WizardDialog(); |
| 137 | wizardDialog->exec(); |
| 138 | delete wizardDialog; |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | CallWidget::callIncoming(Call *call) |
| 144 | { |
| 145 | if (!call->account()->isAutoAnswer()) |
| 146 | ui->callInvite->setVisible(true); |
| 147 | actualCall_ = call; |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | CallWidget::on_acceptButton_clicked() |
| 152 | { |
| 153 | if (actualCall_ != nullptr) |
| 154 | actualCall_->performAction(Call::Action::ACCEPT); |
| 155 | ui->callInvite->setVisible(false); |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | CallWidget::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 | |
| 168 | void |
| 169 | CallWidget::on_holdButton_toggled(bool checked) |
| 170 | { |
| 171 | Q_UNUSED(checked) |
| 172 | if (actualCall_ == nullptr) |
| 173 | return; |
| 174 | actualCall_->performAction(Call::Action::HOLD); |
| 175 | } |
| 176 | |
| 177 | void |
| 178 | CallWidget::on_hangupButton_clicked() |
| 179 | { |
| 180 | if (actualCall_ == nullptr) |
| 181 | return; |
| 182 | actualCall_->performAction(Call::Action::REFUSE); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | CallWidget::addedCall(Call* call, Call* parent) { |
| 187 | Q_UNUSED(parent); |
| 188 | if (call->direction() == Call::Direction::OUTGOING) { |
| 189 | actualCall_ = call; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 194 | CallWidget::callStateChanged(Call* call, Call::State previousState) |
| 195 | { |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 196 | Q_UNUSED(previousState) |
Edric Milaret | b5b4907 | 2015-05-08 11:56:07 -0400 | [diff] [blame^] | 197 | if (call == nullptr) |
| 198 | return; |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 199 | ui->callList->setCurrentIndex(callModel_->getIndex(actualCall_)); |
| 200 | if (call->state() == Call::State::OVER) { |
| 201 | actualCall_ = nullptr; |
| 202 | ui->videoWidget->hide(); |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 203 | } else if (call->state() == Call::State::HOLD) { |
| 204 | ui->videoWidget->hide(); |
Edric Milaret | b5b4907 | 2015-05-08 11:56:07 -0400 | [diff] [blame^] | 205 | } else if (call->state() == Call::State::CURRENT){ |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 206 | ui->videoWidget->show(); |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 207 | ui->messageOutput->setModel( |
| 208 | IMConversationManager::instance()->getModel(actualCall_)); |
| 209 | } |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 210 | ui->callStateLabel->setText("Call State : " + state.at((int)call->state())); |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
| 214 | CallWidget::on_callList_activated(const QModelIndex &index) |
| 215 | { |
| 216 | Call *callSelected = callModel_->getCall(index); |
| 217 | if (actualCall_ != nullptr) { |
| 218 | if (callSelected == actualCall_) |
| 219 | return; |
Edric Milaret | 72c406d | 2015-04-28 13:23:54 -0400 | [diff] [blame] | 220 | ui->videoWidget->hide(); |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 221 | actualCall_->performAction(Call::Action::HOLD); |
| 222 | } |
| 223 | actualCall_ = callSelected; |
| 224 | actualCall_->performAction(Call::Action::HOLD); |
Edric Milaret | 72c406d | 2015-04-28 13:23:54 -0400 | [diff] [blame] | 225 | ui->videoWidget->show(); |
Edric Milaret | 627500d | 2015-03-27 16:41:40 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void |
| 229 | CallWidget::on_muteSpeakerButton_toggled(bool checked) |
| 230 | { |
| 231 | Audio::Settings::instance()->mutePlayback(checked); |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | CallWidget::on_muteMicButton_toggled(bool checked) |
| 236 | { |
| 237 | Audio::Settings::instance()->muteCapture(checked); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | CallWidget::on_speakerSlider_sliderMoved(int position) |
| 242 | { |
| 243 | outputVolume_ = position; |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | CallWidget::on_speakerSlider_sliderReleased() |
| 248 | { |
| 249 | emit Audio::Settings::instance()->setPlaybackVolume(outputVolume_); |
| 250 | } |
| 251 | |
| 252 | void |
| 253 | CallWidget::on_micSlider_sliderMoved(int position) |
| 254 | { |
| 255 | inputVolume_ = position; |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | CallWidget::on_micSlider_sliderReleased() |
| 260 | { |
| 261 | emit Audio::Settings::instance()->setCaptureVolume(inputVolume_); |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | CallWidget::atExit() { |
| 266 | |
| 267 | } |
Edric Milaret | 67007d1 | 2015-05-07 09:40:09 -0400 | [diff] [blame] | 268 | |
| 269 | void |
| 270 | CallWidget::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 | } |