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 | |
Edric Milaret | 83b248c | 2015-06-02 11:42:23 -0400 | [diff] [blame] | 29 | #include "imdelegate.h" |
| 30 | |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 31 | InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) : |
| 32 | QWidget(parent), |
| 33 | ui(new Ui::InstantMessagingWidget) |
| 34 | { |
| 35 | ui->setupUi(this); |
| 36 | |
Edric Milaret | 3aca8e3 | 2015-06-12 10:01:40 -0400 | [diff] [blame] | 37 | this->hide(); |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 38 | |
Edric Milaret | 83b248c | 2015-06-02 11:42:23 -0400 | [diff] [blame] | 39 | imDelegate_ = new ImDelegate(); |
| 40 | ui->messageOutput->setItemDelegate(imDelegate_); |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 41 | ui->messageOutput->setContextMenuPolicy(Qt::ActionsContextMenu); |
| 42 | auto copyAction = new QAction("Copy", this); |
| 43 | ui->messageOutput->addAction(copyAction); |
| 44 | connect(copyAction, &QAction::triggered, [=]() { |
| 45 | copyToClipboard(); |
| 46 | }); |
Edric Milaret | 83b248c | 2015-06-02 11:42:23 -0400 | [diff] [blame] | 47 | auto displayDate = new QAction("Display date", this); |
| 48 | displayDate->setCheckable(true); |
| 49 | ui->messageOutput->addAction(displayDate); |
| 50 | auto displayAuthor = new QAction("Display author", this); |
| 51 | displayAuthor->setCheckable(true); |
| 52 | ui->messageOutput->addAction(displayAuthor); |
| 53 | auto lamdba = [=](){ |
| 54 | int opts = 0; |
| 55 | displayAuthor->isChecked() ? opts |= ImDelegate::DisplayOptions::AUTHOR : opts; |
| 56 | displayDate->isChecked() ? opts |= ImDelegate::DisplayOptions::DATE : opts; |
| 57 | imDelegate_->setDisplayOptions(static_cast<ImDelegate::DisplayOptions>(opts)); |
| 58 | }; |
| 59 | connect(displayAuthor, &QAction::triggered, lamdba); |
| 60 | connect(displayDate, &QAction::triggered, lamdba); |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | InstantMessagingWidget::~InstantMessagingWidget() |
| 64 | { |
| 65 | delete ui; |
Edric Milaret | 83b248c | 2015-06-02 11:42:23 -0400 | [diff] [blame] | 66 | delete imDelegate_; |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void |
| 70 | InstantMessagingWidget::setMediaText(Call *call) |
| 71 | { |
| 72 | if (call != nullptr) { |
| 73 | connect(call, SIGNAL(mediaAdded(Media::Media*)), |
| 74 | this, SLOT(mediaAdd(Media::Media*))); |
| 75 | Media::Text *textMedia = call->addOutgoingMedia<Media::Text>(); |
| 76 | connect(ui->messageInput, &QLineEdit::returnPressed, [=]() |
| 77 | { |
Edric Milaret | c1bb185 | 2015-07-17 12:05:24 -0400 | [diff] [blame] | 78 | QMap<QString, QString> messages; |
| 79 | messages["text/plain"] = ui->messageInput->text(); |
| 80 | textMedia->send(messages); |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 81 | ui->messageInput->clear(); |
| 82 | }); |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 83 | } else { |
| 84 | ui->messageOutput->disconnect(); |
| 85 | ui->messageInput->disconnect(); |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | InstantMessagingWidget::mediaAdd(Media::Media *media) |
| 91 | { |
| 92 | switch(media->type()) { |
| 93 | case Media::Media::Type::AUDIO: |
| 94 | break; |
| 95 | case Media::Media::Type::VIDEO: |
| 96 | break; |
| 97 | case Media::Media::Type::TEXT: |
| 98 | if (media->direction() == Media::Text::Direction::IN) { |
| 99 | ui->messageOutput->setModel( |
| 100 | static_cast<Media::Text*>(media)->recording()-> |
| 101 | instantMessagingModel()); |
| 102 | connect(ui->messageOutput->model(), |
| 103 | SIGNAL(rowsInserted(const QModelIndex&, int, int)), |
| 104 | ui->messageOutput, SLOT(scrollToBottom())); |
Edric Milaret | 3aca8e3 | 2015-06-12 10:01:40 -0400 | [diff] [blame] | 105 | this->show(); |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 106 | } |
| 107 | break; |
| 108 | case Media::Media::Type::FILE: |
| 109 | break; |
| 110 | default: |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | InstantMessagingWidget::keyPressEvent(QKeyEvent *event) |
| 117 | { |
| 118 | if (event->matches(QKeySequence::Copy)) { |
| 119 | copyToClipboard(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
Edric Milaret | bd0baf8 | 2015-06-22 14:58:06 -0400 | [diff] [blame] | 124 | InstantMessagingWidget::showEvent(QShowEvent *event) |
| 125 | { |
Edric Milaret | 023e8f1 | 2015-07-07 14:52:40 -0400 | [diff] [blame] | 126 | Q_UNUSED(event) |
Edric Milaret | bd0baf8 | 2015-06-22 14:58:06 -0400 | [diff] [blame] | 127 | ui->messageInput->setFocus(); |
| 128 | } |
| 129 | |
| 130 | void |
Edric Milaret | cdc978b | 2015-06-04 11:25:12 -0400 | [diff] [blame] | 131 | InstantMessagingWidget::copyToClipboard() |
| 132 | { |
| 133 | auto idx = ui->messageOutput->currentIndex(); |
| 134 | if (idx.isValid()) { |
| 135 | auto text = ui->messageOutput->model()->data(idx); |
| 136 | |
| 137 | QApplication::clipboard()->setText(text.value<QString>()); |
| 138 | } |
| 139 | } |
Edric Milaret | d398dcb | 2015-06-05 17:02:14 -0400 | [diff] [blame] | 140 | |
| 141 | void |
| 142 | InstantMessagingWidget::on_sendButton_clicked() |
| 143 | { |
| 144 | emit ui->messageInput->returnPressed(); |
| 145 | } |