blob: d186a108babe7a2d0c3f1fc63a3316610cec4ea1 [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
Edric Milaret2cf34292015-06-22 16:27:03 -040026#include "navstack.h"
27
Edric Milaretcdc978b2015-06-04 11:25:12 -040028#include "media/text.h"
29#include "media/textrecording.h"
30
Edric Milaret83b248c2015-06-02 11:42:23 -040031#include "imdelegate.h"
Edric Milaret2cf34292015-06-22 16:27:03 -040032#include "globalsystemtray.h"
Edric Milaret83b248c2015-06-02 11:42:23 -040033
Edric Milaretcdc978b2015-06-04 11:25:12 -040034InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) :
35 QWidget(parent),
36 ui(new Ui::InstantMessagingWidget)
37{
38 ui->setupUi(this);
39
Edric Milaret3aca8e32015-06-12 10:01:40 -040040 this->hide();
Edric Milaretcdc978b2015-06-04 11:25:12 -040041
Edric Milaret83b248c2015-06-02 11:42:23 -040042 imDelegate_ = new ImDelegate();
43 ui->messageOutput->setItemDelegate(imDelegate_);
Edric Milaretcdc978b2015-06-04 11:25:12 -040044 ui->messageOutput->setContextMenuPolicy(Qt::ActionsContextMenu);
Edric Milaret53ac6e52015-09-14 13:37:06 -040045 auto copyAction = new QAction(tr("Copy"), this);
Edric Milaretcdc978b2015-06-04 11:25:12 -040046 ui->messageOutput->addAction(copyAction);
47 connect(copyAction, &QAction::triggered, [=]() {
48 copyToClipboard();
49 });
Edric Milaret53ac6e52015-09-14 13:37:06 -040050 auto displayDate = new QAction(tr("Display date"), this);
Edric Milaret83b248c2015-06-02 11:42:23 -040051 displayDate->setCheckable(true);
52 ui->messageOutput->addAction(displayDate);
Edric Milaret53ac6e52015-09-14 13:37:06 -040053 auto displayAuthor = new QAction(tr("Display author"), this);
Edric Milaret83b248c2015-06-02 11:42:23 -040054 displayAuthor->setCheckable(true);
55 ui->messageOutput->addAction(displayAuthor);
56 auto lamdba = [=](){
57 int opts = 0;
58 displayAuthor->isChecked() ? opts |= ImDelegate::DisplayOptions::AUTHOR : opts;
59 displayDate->isChecked() ? opts |= ImDelegate::DisplayOptions::DATE : opts;
60 imDelegate_->setDisplayOptions(static_cast<ImDelegate::DisplayOptions>(opts));
61 };
62 connect(displayAuthor, &QAction::triggered, lamdba);
63 connect(displayDate, &QAction::triggered, lamdba);
Edric Milaretcdc978b2015-06-04 11:25:12 -040064}
65
66InstantMessagingWidget::~InstantMessagingWidget()
67{
68 delete ui;
Edric Milaret83b248c2015-06-02 11:42:23 -040069 delete imDelegate_;
Edric Milaretcdc978b2015-06-04 11:25:12 -040070}
71
72void
73InstantMessagingWidget::setMediaText(Call *call)
74{
75 if (call != nullptr) {
76 connect(call, SIGNAL(mediaAdded(Media::Media*)),
77 this, SLOT(mediaAdd(Media::Media*)));
78 Media::Text *textMedia = call->addOutgoingMedia<Media::Text>();
79 connect(ui->messageInput, &QLineEdit::returnPressed, [=]()
80 {
Edric Milaretc1bb1852015-07-17 12:05:24 -040081 QMap<QString, QString> messages;
82 messages["text/plain"] = ui->messageInput->text();
83 textMedia->send(messages);
Edric Milaretcdc978b2015-06-04 11:25:12 -040084 ui->messageInput->clear();
85 });
Edric Milaretcdc978b2015-06-04 11:25:12 -040086 } else {
87 ui->messageOutput->disconnect();
88 ui->messageInput->disconnect();
Edric Milaretcdc978b2015-06-04 11:25:12 -040089 }
90}
91
92void
93InstantMessagingWidget::mediaAdd(Media::Media *media)
94{
95 switch(media->type()) {
96 case Media::Media::Type::AUDIO:
97 break;
98 case Media::Media::Type::VIDEO:
99 break;
100 case Media::Media::Type::TEXT:
101 if (media->direction() == Media::Text::Direction::IN) {
102 ui->messageOutput->setModel(
103 static_cast<Media::Text*>(media)->recording()->
104 instantMessagingModel());
105 connect(ui->messageOutput->model(),
106 SIGNAL(rowsInserted(const QModelIndex&, int, int)),
107 ui->messageOutput, SLOT(scrollToBottom()));
Edric Milaret2cf34292015-06-22 16:27:03 -0400108 connect(static_cast<Media::Text*>(media),
109 SIGNAL(messageReceived(QMap<QString,QString>)),
110 this,
111 SLOT(onMsgReceived(QMap<QString,QString>)));
Edric Milaret3aca8e32015-06-12 10:01:40 -0400112 this->show();
Edric Milaretcdc978b2015-06-04 11:25:12 -0400113 }
114 break;
115 case Media::Media::Type::FILE:
116 break;
117 default:
118 break;
119 }
120}
121
122void
123InstantMessagingWidget::keyPressEvent(QKeyEvent *event)
124{
125 if (event->matches(QKeySequence::Copy)) {
126 copyToClipboard();
127 }
128}
129
130void
Edric Milaretbd0baf82015-06-22 14:58:06 -0400131InstantMessagingWidget::showEvent(QShowEvent *event)
132{
Edric Milaret023e8f12015-07-07 14:52:40 -0400133 Q_UNUSED(event)
Edric Milaretbd0baf82015-06-22 14:58:06 -0400134 ui->messageInput->setFocus();
135}
136
137void
Edric Milaretcdc978b2015-06-04 11:25:12 -0400138InstantMessagingWidget::copyToClipboard()
139{
140 auto idx = ui->messageOutput->currentIndex();
141 if (idx.isValid()) {
142 auto text = ui->messageOutput->model()->data(idx);
143
144 QApplication::clipboard()->setText(text.value<QString>());
145 }
146}
Edric Milaretd398dcb2015-06-05 17:02:14 -0400147
148void
149InstantMessagingWidget::on_sendButton_clicked()
150{
151 emit ui->messageInput->returnPressed();
152}
Edric Milaret2cf34292015-06-22 16:27:03 -0400153
154void
155InstantMessagingWidget::onMsgReceived(const QMap<QString,QString>& message)
156{
157 if (!QApplication::activeWindow()) {
158 GlobalSystemTray::instance().showMessage("Ring: Message Received", message["text/plain"]);
159 QApplication::alert(this, 5000);
160 }
161}