blob: 7cf1893102f38d198b1756677ed6fc21cbaabc52 [file] [log] [blame]
Edric Milaretcdc978b2015-06-04 11:25:12 -04001/***************************************************************************
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 Milaret83b248c2015-06-02 11:42:23 -040029#include "imdelegate.h"
30
Edric Milaretcdc978b2015-06-04 11:25:12 -040031InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) :
32 QWidget(parent),
33 ui(new Ui::InstantMessagingWidget)
34{
35 ui->setupUi(this);
36
Edric Milaret3aca8e32015-06-12 10:01:40 -040037 this->hide();
Edric Milaretcdc978b2015-06-04 11:25:12 -040038
Edric Milaret83b248c2015-06-02 11:42:23 -040039 imDelegate_ = new ImDelegate();
40 ui->messageOutput->setItemDelegate(imDelegate_);
Edric Milaretcdc978b2015-06-04 11:25:12 -040041 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 Milaret83b248c2015-06-02 11:42:23 -040047 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 Milaretcdc978b2015-06-04 11:25:12 -040061}
62
63InstantMessagingWidget::~InstantMessagingWidget()
64{
65 delete ui;
Edric Milaret83b248c2015-06-02 11:42:23 -040066 delete imDelegate_;
Edric Milaretcdc978b2015-06-04 11:25:12 -040067}
68
69void
70InstantMessagingWidget::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 Milaretc1bb1852015-07-17 12:05:24 -040078 QMap<QString, QString> messages;
79 messages["text/plain"] = ui->messageInput->text();
80 textMedia->send(messages);
Edric Milaretcdc978b2015-06-04 11:25:12 -040081 ui->messageInput->clear();
82 });
Edric Milaretcdc978b2015-06-04 11:25:12 -040083 } else {
84 ui->messageOutput->disconnect();
85 ui->messageInput->disconnect();
Edric Milaretcdc978b2015-06-04 11:25:12 -040086 }
87}
88
89void
90InstantMessagingWidget::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 Milaret3aca8e32015-06-12 10:01:40 -0400105 this->show();
Edric Milaretcdc978b2015-06-04 11:25:12 -0400106 }
107 break;
108 case Media::Media::Type::FILE:
109 break;
110 default:
111 break;
112 }
113}
114
115void
116InstantMessagingWidget::keyPressEvent(QKeyEvent *event)
117{
118 if (event->matches(QKeySequence::Copy)) {
119 copyToClipboard();
120 }
121}
122
123void
Edric Milaretbd0baf82015-06-22 14:58:06 -0400124InstantMessagingWidget::showEvent(QShowEvent *event)
125{
Edric Milaret023e8f12015-07-07 14:52:40 -0400126 Q_UNUSED(event)
Edric Milaretbd0baf82015-06-22 14:58:06 -0400127 ui->messageInput->setFocus();
128}
129
130void
Edric Milaretcdc978b2015-06-04 11:25:12 -0400131InstantMessagingWidget::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 Milaretd398dcb2015-06-05 17:02:14 -0400140
141void
142InstantMessagingWidget::on_sendButton_clicked()
143{
144 emit ui->messageInput->returnPressed();
145}