im: add copy/paste features

Also move the im code to live in his own class

Refs #74690

Change-Id: Ifa5b45a82f9b6b885e09d0207b5f15a4675b7409
diff --git a/instantmessagingwidget.cpp b/instantmessagingwidget.cpp
new file mode 100644
index 0000000..11b435a
--- /dev/null
+++ b/instantmessagingwidget.cpp
@@ -0,0 +1,115 @@
+/***************************************************************************
+ * 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"
+
+InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) :
+    QWidget(parent),
+    ui(new Ui::InstantMessagingWidget)
+{
+    ui->setupUi(this);
+
+    ui->messageInput->hide();
+    ui->messageOutput->hide();
+
+    ui->messageOutput->setContextMenuPolicy(Qt::ActionsContextMenu);
+    auto copyAction = new QAction("Copy", this);
+    ui->messageOutput->addAction(copyAction);
+    connect(copyAction, &QAction::triggered, [=]() {
+        copyToClipboard();
+    });
+}
+
+InstantMessagingWidget::~InstantMessagingWidget()
+{
+    delete ui;
+}
+
+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, [=]()
+        {
+            textMedia->send(ui->messageInput->text());
+            ui->messageInput->clear();
+        });
+        ui->messageInput->show();
+    } else {
+        ui->messageOutput->disconnect();
+        ui->messageInput->disconnect();
+        ui->messageOutput->hide();
+        ui->messageInput->hide();
+    }
+}
+
+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()));
+            ui->messageOutput->show();
+        }
+        break;
+    case Media::Media::Type::FILE:
+        break;
+    default:
+        break;
+    }
+}
+
+void
+InstantMessagingWidget::keyPressEvent(QKeyEvent *event)
+{
+    if (event->matches(QKeySequence::Copy)) {
+        copyToClipboard();
+    }
+}
+
+void
+InstantMessagingWidget::copyToClipboard()
+{
+    auto idx = ui->messageOutput->currentIndex();
+    if (idx.isValid()) {
+        auto text = ui->messageOutput->model()->data(idx);
+
+        QApplication::clipboard()->setText(text.value<QString>());
+    }
+}