instantmessagingwidget.cpp 5.32 KB
Newer Older
1
/***************************************************************************
Edric Milaret's avatar
Edric Milaret committed
2
 * Copyright (C) 2015 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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>

26 27
#include "navstack.h"

28 29 30
#include "media/text.h"
#include "media/textrecording.h"

Edric Milaret's avatar
Edric Milaret committed
31
#include "imdelegate.h"
32
#include "globalsystemtray.h"
Edric Milaret's avatar
Edric Milaret committed
33

34 35 36 37 38 39
InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::InstantMessagingWidget)
{
    ui->setupUi(this);

40
    this->hide();
41

Edric Milaret's avatar
Edric Milaret committed
42 43
    imDelegate_ = new ImDelegate();
    ui->messageOutput->setItemDelegate(imDelegate_);
44
    ui->messageOutput->setContextMenuPolicy(Qt::ActionsContextMenu);
45
    auto copyAction = new QAction(tr("Copy"), this);
46 47 48 49
    ui->messageOutput->addAction(copyAction);
    connect(copyAction, &QAction::triggered, [=]() {
        copyToClipboard();
    });
50
    auto displayDate = new QAction(tr("Display date"), this);
Edric Milaret's avatar
Edric Milaret committed
51 52
    displayDate->setCheckable(true);
    ui->messageOutput->addAction(displayDate);
53
    auto displayAuthor = new QAction(tr("Display author"), this);
Edric Milaret's avatar
Edric Milaret committed
54 55 56 57 58 59 60 61 62 63
    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);
64 65 66 67 68
}

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

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, [=]()
        {
81 82 83
            QMap<QString, QString> messages;
            messages["text/plain"] = ui->messageInput->text();
            textMedia->send(messages);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
            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()));
108 109 110 111
            connect(static_cast<Media::Text*>(media),
                    SIGNAL(messageReceived(QMap<QString,QString>)),
                    this,
                    SLOT(onMsgReceived(QMap<QString,QString>)));
112
            this->show();
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        }
        break;
    case Media::Media::Type::FILE:
        break;
    default:
        break;
    }
}

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

130 131 132
void
InstantMessagingWidget::showEvent(QShowEvent *event)
{
133
    Q_UNUSED(event)
134 135 136
    ui->messageInput->setFocus();
}

137 138 139 140 141 142 143 144 145 146
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
147 148 149 150 151 152

void
InstantMessagingWidget::on_sendButton_clicked()
{
    emit ui->messageInput->returnPressed();
}
153 154 155 156 157 158 159 160 161

void
InstantMessagingWidget::onMsgReceived(const QMap<QString,QString>& message)
{
    if (!QApplication::activeWindow()) {
        GlobalSystemTray::instance().showMessage("Ring: Message Received", message["text/plain"]);
        QApplication::alert(this, 5000);
    }
}