blob: 6dafd94e2e0681e9cef29605b652bc2f7986e705 [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 {
78 textMedia->send(ui->messageInput->text());
79 ui->messageInput->clear();
80 });
Edric Milaretcdc978b2015-06-04 11:25:12 -040081 } else {
82 ui->messageOutput->disconnect();
83 ui->messageInput->disconnect();
Edric Milaretcdc978b2015-06-04 11:25:12 -040084 }
85}
86
87void
88InstantMessagingWidget::mediaAdd(Media::Media *media)
89{
90 switch(media->type()) {
91 case Media::Media::Type::AUDIO:
92 break;
93 case Media::Media::Type::VIDEO:
94 break;
95 case Media::Media::Type::TEXT:
96 if (media->direction() == Media::Text::Direction::IN) {
97 ui->messageOutput->setModel(
98 static_cast<Media::Text*>(media)->recording()->
99 instantMessagingModel());
100 connect(ui->messageOutput->model(),
101 SIGNAL(rowsInserted(const QModelIndex&, int, int)),
102 ui->messageOutput, SLOT(scrollToBottom()));
Edric Milaret3aca8e32015-06-12 10:01:40 -0400103 this->show();
Edric Milaretcdc978b2015-06-04 11:25:12 -0400104 }
105 break;
106 case Media::Media::Type::FILE:
107 break;
108 default:
109 break;
110 }
111}
112
113void
114InstantMessagingWidget::keyPressEvent(QKeyEvent *event)
115{
116 if (event->matches(QKeySequence::Copy)) {
117 copyToClipboard();
118 }
119}
120
121void
Edric Milaretbd0baf82015-06-22 14:58:06 -0400122InstantMessagingWidget::showEvent(QShowEvent *event)
123{
Edric Milaret023e8f12015-07-07 14:52:40 -0400124 Q_UNUSED(event)
Edric Milaretbd0baf82015-06-22 14:58:06 -0400125 ui->messageInput->setFocus();
126}
127
128void
Edric Milaretcdc978b2015-06-04 11:25:12 -0400129InstantMessagingWidget::copyToClipboard()
130{
131 auto idx = ui->messageOutput->currentIndex();
132 if (idx.isValid()) {
133 auto text = ui->messageOutput->model()->data(idx);
134
135 QApplication::clipboard()->setText(text.value<QString>());
136 }
137}
Edric Milaretd398dcb2015-06-05 17:02:14 -0400138
139void
140InstantMessagingWidget::on_sendButton_clicked()
141{
142 emit ui->messageInput->returnPressed();
143}