instantmessagingwidget.cpp 4.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/***************************************************************************
 * Copyright (C) 2015 by Savoir-Faire Linux                                *
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "instantmessagingwidget.h"
#include "ui_instantmessagingwidget.h"

#include <QApplication>
#include <QClipboard>
#include <QMenu>

#include "media/text.h"
#include "media/textrecording.h"

Edric Milaret's avatar
Edric Milaret committed
29 30
#include "imdelegate.h"

31 32 33 34 35 36
InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InstantMessagingWidget)
{
    ui->setupUi(this);

37
    this->hide();
38

Edric Milaret's avatar
Edric Milaret committed
39 40
    imDelegate_ = new ImDelegate();
    ui->messageOutput->setItemDelegate(imDelegate_);
41
    ui->messageOutput->setContextMenuPolicy(Qt::ActionsContextMenu);
42
    auto copyAction = new QAction(tr("Copy"), this);
43 44 45 46
    ui->messageOutput->addAction(copyAction);
    connect(copyAction, &QAction::triggered, [=]() {
        copyToClipboard();
    });
47
    auto displayDate = new QAction(tr("Display date"), this);
Edric Milaret's avatar
Edric Milaret committed
48 49
    displayDate->setCheckable(true);
    ui->messageOutput->addAction(displayDate);
50
    auto displayAuthor = new QAction(tr("Display author"), this);
Edric Milaret's avatar
Edric Milaret committed
51 52 53 54 55 56 57 58 59 60
    displayAuthor->setCheckable(true);
    ui->messageOutput->addAction(displayAuthor);
    auto lamdba = [=](){
        int opts = 0;
        displayAuthor->isChecked() ? opts |= ImDelegate::DisplayOptions::AUTHOR : opts;
        displayDate->isChecked() ? opts |= ImDelegate::DisplayOptions::DATE : opts;
        imDelegate_->setDisplayOptions(static_cast<ImDelegate::DisplayOptions>(opts));
    };
    connect(displayAuthor, &QAction::triggered, lamdba);
    connect(displayDate, &QAction::triggered, lamdba);
61 62 63 64 65
}

InstantMessagingWidget::~InstantMessagingWidget()
{
    delete ui;
Edric Milaret's avatar
Edric Milaret committed
66
    delete imDelegate_;
67 68 69 70 71 72 73 74 75 76 77
}

void
InstantMessagingWidget::setMediaText(Call *call)
{
    if (call != nullptr) {
        connect(call, SIGNAL(mediaAdded(Media::Media*)),
                this, SLOT(mediaAdd(Media::Media*)));
        Media::Text *textMedia = call->addOutgoingMedia<Media::Text>();
        connect(ui->messageInput, &QLineEdit::returnPressed, [=]()
        {
78 79 80
            QMap<QString, QString> messages;
            messages["text/plain"] = ui->messageInput->text();
            textMedia->send(messages);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            ui->messageInput->clear();
        });
    } else {
        ui->messageOutput->disconnect();
        ui->messageInput->disconnect();
    }
}

void
InstantMessagingWidget::mediaAdd(Media::Media *media)
{
    switch(media->type()) {
    case Media::Media::Type::AUDIO:
        break;
    case Media::Media::Type::VIDEO:
        break;
    case Media::Media::Type::TEXT:
        if (media->direction() == Media::Text::Direction::IN) {
            ui->messageOutput->setModel(
                        static_cast<Media::Text*>(media)->recording()->
                        instantMessagingModel());
            connect(ui->messageOutput->model(),
                    SIGNAL(rowsInserted(const QModelIndex&, int, int)),
                    ui->messageOutput, SLOT(scrollToBottom()));
105
            this->show();
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        }
        break;
    case Media::Media::Type::FILE:
        break;
    default:
        break;
    }
}

void
InstantMessagingWidget::keyPressEvent(QKeyEvent *event)
{
    if (event->matches(QKeySequence::Copy)) {
        copyToClipboard();
    }
}

123 124 125
void
InstantMessagingWidget::showEvent(QShowEvent *event)
{
126
    Q_UNUSED(event)
127 128 129
    ui->messageInput->setFocus();
}

130 131 132 133 134 135 136 137 138 139
void
InstantMessagingWidget::copyToClipboard()
{
    auto idx = ui->messageOutput->currentIndex();
    if (idx.isValid()) {
        auto text = ui->messageOutput->model()->data(idx);

        QApplication::clipboard()->setText(text.value<QString>());
    }
}
Edric Milaret's avatar
Edric Milaret committed
140 141 142 143 144 145

void
InstantMessagingWidget::on_sendButton_clicked()
{
    emit ui->messageInput->returnPressed();
}