Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * Copyright (C) 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 "instantmessagingwidget.h" |
| 20 | #include "ui_instantmessagingwidget.h" |
| 21 | |
| 22 | #include <QApplication> |
| 23 | #include <QClipboard> |
| 24 | #include <QMenu> |
| 25 | |
| 26 | #include "media/text.h" |
| 27 | #include "media/textrecording.h" |
| 28 | |
| 29 | InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) : |
| 30 | QWidget(parent), |
| 31 | ui(new Ui::InstantMessagingWidget) |
| 32 | { |
| 33 | ui->setupUi(this); |
| 34 | |
| 35 | ui->messageInput->hide(); |
| 36 | ui->messageOutput->hide(); |
| 37 | |
| 38 | ui->messageOutput->setContextMenuPolicy(Qt::ActionsContextMenu); |
| 39 | auto copyAction = new QAction("Copy", this); |
| 40 | ui->messageOutput->addAction(copyAction); |
| 41 | connect(copyAction, &QAction::triggered, [=]() { |
| 42 | copyToClipboard(); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | InstantMessagingWidget::~InstantMessagingWidget() |
| 47 | { |
| 48 | delete ui; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | InstantMessagingWidget::setMediaText(Call *call) |
| 53 | { |
| 54 | if (call != nullptr) { |
| 55 | connect(call, SIGNAL(mediaAdded(Media::Media*)), |
| 56 | this, SLOT(mediaAdd(Media::Media*))); |
| 57 | Media::Text *textMedia = call->addOutgoingMedia<Media::Text>(); |
| 58 | connect(ui->messageInput, &QLineEdit::returnPressed, [=]() |
| 59 | { |
| 60 | textMedia->send(ui->messageInput->text()); |
| 61 | ui->messageInput->clear(); |
| 62 | }); |
| 63 | ui->messageInput->show(); |
| 64 | } else { |
| 65 | ui->messageOutput->disconnect(); |
| 66 | ui->messageInput->disconnect(); |
| 67 | ui->messageOutput->hide(); |
| 68 | ui->messageInput->hide(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | InstantMessagingWidget::mediaAdd(Media::Media *media) |
| 74 | { |
| 75 | switch(media->type()) { |
| 76 | case Media::Media::Type::AUDIO: |
| 77 | break; |
| 78 | case Media::Media::Type::VIDEO: |
| 79 | break; |
| 80 | case Media::Media::Type::TEXT: |
| 81 | if (media->direction() == Media::Text::Direction::IN) { |
| 82 | ui->messageOutput->setModel( |
| 83 | static_cast<Media::Text*>(media)->recording()-> |
| 84 | instantMessagingModel()); |
| 85 | connect(ui->messageOutput->model(), |
| 86 | SIGNAL(rowsInserted(const QModelIndex&, int, int)), |
| 87 | ui->messageOutput, SLOT(scrollToBottom())); |
| 88 | ui->messageOutput->show(); |
| 89 | } |
| 90 | break; |
| 91 | case Media::Media::Type::FILE: |
| 92 | break; |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | InstantMessagingWidget::keyPressEvent(QKeyEvent *event) |
| 100 | { |
| 101 | if (event->matches(QKeySequence::Copy)) { |
| 102 | copyToClipboard(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | InstantMessagingWidget::copyToClipboard() |
| 108 | { |
| 109 | auto idx = ui->messageOutput->currentIndex(); |
| 110 | if (idx.isValid()) { |
| 111 | auto text = ui->messageOutput->model()->data(idx); |
| 112 | |
| 113 | QApplication::clipboard()->setText(text.value<QString>()); |
| 114 | } |
| 115 | } |