2019-04-08 21:40:49 +00:00
|
|
|
/*
|
2019-08-14 14:54:46 +00:00
|
|
|
* Squawk messenger.
|
2019-04-08 21:40:49 +00:00
|
|
|
* Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
*
|
|
|
|
* 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 "conversation.h"
|
|
|
|
#include "ui_conversation.h"
|
2019-04-09 15:04:08 +00:00
|
|
|
#include <QDebug>
|
2019-04-12 15:22:10 +00:00
|
|
|
#include <QScrollBar>
|
2019-05-15 17:36:37 +00:00
|
|
|
#include <QTimer>
|
2019-06-19 14:15:20 +00:00
|
|
|
#include <QGraphicsDropShadowEffect>
|
2019-06-21 19:33:38 +00:00
|
|
|
#include <QFileDialog>
|
2019-04-09 15:04:08 +00:00
|
|
|
|
2019-08-29 14:19:35 +00:00
|
|
|
Conversation::Conversation(bool muc, const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent):
|
2019-04-09 15:04:08 +00:00
|
|
|
QWidget(parent),
|
2019-08-29 14:19:35 +00:00
|
|
|
isMuc(muc),
|
2019-08-23 09:51:33 +00:00
|
|
|
myJid(mJid),
|
|
|
|
myResource(mRes),
|
|
|
|
palJid(pJid),
|
|
|
|
activePalResource(pRes),
|
|
|
|
account(acc),
|
2019-08-29 14:19:35 +00:00
|
|
|
line(new MessageLine(muc)),
|
2019-04-13 20:38:20 +00:00
|
|
|
m_ui(new Ui::Conversation()),
|
2019-04-12 15:22:10 +00:00
|
|
|
ker(),
|
2019-04-13 20:38:20 +00:00
|
|
|
thread(),
|
2019-08-23 09:51:33 +00:00
|
|
|
statusIcon(0),
|
|
|
|
statusLabel(0),
|
2019-04-14 20:37:02 +00:00
|
|
|
scroll(down),
|
2019-05-15 17:36:37 +00:00
|
|
|
manualSliderChange(false),
|
|
|
|
requestingHistory(false),
|
|
|
|
everShown(false)
|
2019-04-09 15:04:08 +00:00
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
|
|
|
m_ui->splitter->setSizes({300, 0});
|
|
|
|
m_ui->splitter->setStretchFactor(1, 0);
|
|
|
|
|
2019-08-23 09:51:33 +00:00
|
|
|
statusIcon = m_ui->statusIcon;
|
|
|
|
statusLabel = m_ui->statusLabel;
|
2019-04-09 15:04:08 +00:00
|
|
|
|
2019-04-09 22:01:25 +00:00
|
|
|
connect(&ker, SIGNAL(enterPressed()), this, SLOT(onEnterPressed()));
|
2019-04-12 05:51:36 +00:00
|
|
|
connect(m_ui->sendButton, SIGNAL(clicked(bool)), this, SLOT(onEnterPressed()));
|
2019-08-23 09:51:33 +00:00
|
|
|
connect(line, SIGNAL(resize(int)), this, SLOT(onMessagesResize(int)));
|
2019-06-21 19:33:38 +00:00
|
|
|
//connect(m_ui->attachButton, SIGNAL(clicked(bool)), this, SLOT(onAttach()));
|
2019-04-09 22:01:25 +00:00
|
|
|
|
2019-04-10 15:22:52 +00:00
|
|
|
m_ui->messageEditor->installEventFilter(&ker);
|
2019-04-12 15:22:10 +00:00
|
|
|
|
2019-04-14 20:37:02 +00:00
|
|
|
QScrollBar* vs = m_ui->scrollArea->verticalScrollBar();
|
2019-04-11 14:58:59 +00:00
|
|
|
m_ui->scrollArea->setWidget(line);
|
2019-04-14 20:37:02 +00:00
|
|
|
vs->setBackgroundRole(QPalette::Base);
|
|
|
|
vs->setAutoFillBackground(true);
|
|
|
|
connect(vs, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChanged(int)));
|
2019-06-19 14:15:20 +00:00
|
|
|
|
|
|
|
applyVisualEffects();
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Conversation::~Conversation()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:15:20 +00:00
|
|
|
void Conversation::applyVisualEffects()
|
|
|
|
{
|
|
|
|
QGraphicsDropShadowEffect *e1 = new QGraphicsDropShadowEffect;
|
|
|
|
e1->setBlurRadius(10);
|
|
|
|
e1->setXOffset(0);
|
|
|
|
e1->setYOffset(-2);
|
|
|
|
e1->setColor(Qt::black);
|
|
|
|
m_ui->bl->setGraphicsEffect(e1);
|
|
|
|
|
|
|
|
QGraphicsDropShadowEffect *e2 = new QGraphicsDropShadowEffect;
|
|
|
|
e2->setBlurRadius(7);
|
|
|
|
e2->setXOffset(0);
|
|
|
|
e2->setYOffset(2);
|
|
|
|
e2->setColor(Qt::black);
|
|
|
|
m_ui->ul->setGraphicsEffect(e2);
|
|
|
|
|
|
|
|
QGraphicsDropShadowEffect *e3 = new QGraphicsDropShadowEffect;
|
|
|
|
e3->setBlurRadius(10);
|
|
|
|
e3->setXOffset(0);
|
|
|
|
e3->setYOffset(2);
|
|
|
|
e3->setColor(Qt::black);
|
|
|
|
m_ui->ut->setGraphicsEffect(e3);
|
|
|
|
}
|
|
|
|
|
2019-04-09 15:04:08 +00:00
|
|
|
void Conversation::setName(const QString& name)
|
|
|
|
{
|
2019-06-18 15:08:03 +00:00
|
|
|
m_ui->nameLabel->setText(name);
|
2019-06-21 19:33:38 +00:00
|
|
|
setWindowTitle(name);
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Conversation::getAccount() const
|
|
|
|
{
|
2019-08-23 09:51:33 +00:00
|
|
|
return account;
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Conversation::getJid() const
|
|
|
|
{
|
2019-08-23 09:51:33 +00:00
|
|
|
return palJid;
|
2019-04-09 15:04:08 +00:00
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
|
2019-04-11 14:58:59 +00:00
|
|
|
void Conversation::addMessage(const Shared::Message& data)
|
2019-04-09 22:01:25 +00:00
|
|
|
{
|
2019-04-12 15:22:10 +00:00
|
|
|
int pos = m_ui->scrollArea->verticalScrollBar()->sliderPosition();
|
|
|
|
int max = m_ui->scrollArea->verticalScrollBar()->maximum();
|
2019-05-15 17:36:37 +00:00
|
|
|
|
2019-04-13 20:38:20 +00:00
|
|
|
MessageLine::Position place = line->message(data);
|
|
|
|
if (place == MessageLine::invalid) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 15:22:52 +00:00
|
|
|
KeyEnterReceiver::KeyEnterReceiver(QObject* parent): QObject(parent), ownEvent(false) {}
|
|
|
|
|
2019-04-09 22:01:25 +00:00
|
|
|
bool KeyEnterReceiver::eventFilter(QObject* obj, QEvent* event)
|
|
|
|
{
|
2019-04-10 15:22:52 +00:00
|
|
|
QEvent::Type type = event->type();
|
|
|
|
if (type == QEvent::KeyPress) {
|
2019-04-09 22:01:25 +00:00
|
|
|
QKeyEvent* key = static_cast<QKeyEvent*>(event);
|
2019-04-10 15:22:52 +00:00
|
|
|
int k = key->key();
|
|
|
|
if (k == Qt::Key_Enter || k == Qt::Key_Return) {
|
|
|
|
Qt::KeyboardModifiers mod = key->modifiers();
|
|
|
|
if (mod & Qt::ControlModifier) {
|
|
|
|
mod = mod & ~Qt::ControlModifier;
|
|
|
|
QKeyEvent* nEvent = new QKeyEvent(event->type(), k, mod, key->text(), key->isAutoRepeat(), key->count());
|
|
|
|
QCoreApplication::postEvent(obj, nEvent);
|
|
|
|
ownEvent = true;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (ownEvent) {
|
|
|
|
ownEvent = false;
|
|
|
|
} else {
|
|
|
|
emit enterPressed();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 15:22:52 +00:00
|
|
|
return QObject::eventFilter(obj, event);
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-12 15:22:10 +00:00
|
|
|
QString Conversation::getPalResource() const
|
|
|
|
{
|
|
|
|
return activePalResource;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::setPalResource(const QString& res)
|
|
|
|
{
|
|
|
|
activePalResource = res;
|
|
|
|
}
|
|
|
|
|
2019-04-09 22:01:25 +00:00
|
|
|
void Conversation::onEnterPressed()
|
|
|
|
{
|
2019-04-11 14:58:59 +00:00
|
|
|
QString body(m_ui->messageEditor->toPlainText());
|
2019-04-12 15:22:10 +00:00
|
|
|
|
|
|
|
if (body.size() > 0) {
|
|
|
|
m_ui->messageEditor->clear();
|
2019-08-28 11:40:55 +00:00
|
|
|
handleSendMessage(body);
|
2019-04-12 15:22:10 +00:00
|
|
|
}
|
2019-04-09 22:01:25 +00:00
|
|
|
}
|
2019-04-13 20:38:20 +00:00
|
|
|
|
|
|
|
void Conversation::onMessagesResize(int amount)
|
|
|
|
{
|
2019-04-14 20:37:02 +00:00
|
|
|
manualSliderChange = true;
|
2019-04-13 20:38:20 +00:00
|
|
|
switch (scroll) {
|
|
|
|
case down:
|
|
|
|
m_ui->scrollArea->verticalScrollBar()->setValue(m_ui->scrollArea->verticalScrollBar()->maximum());
|
|
|
|
break;
|
2019-05-15 17:36:37 +00:00
|
|
|
case keep: {
|
|
|
|
int max = m_ui->scrollArea->verticalScrollBar()->maximum();
|
|
|
|
int value = m_ui->scrollArea->verticalScrollBar()->value() + amount;
|
|
|
|
m_ui->scrollArea->verticalScrollBar()->setValue(value);
|
|
|
|
|
|
|
|
if (value == max) {
|
|
|
|
scroll = down;
|
|
|
|
} else {
|
|
|
|
scroll = nothing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-04-13 20:38:20 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-04-14 20:37:02 +00:00
|
|
|
manualSliderChange = false;
|
2019-04-13 20:38:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-14 20:37:02 +00:00
|
|
|
void Conversation::onSliderValueChanged(int value)
|
|
|
|
{
|
|
|
|
if (!manualSliderChange) {
|
|
|
|
if (value == m_ui->scrollArea->verticalScrollBar()->maximum()) {
|
|
|
|
scroll = down;
|
|
|
|
} else {
|
2019-05-15 17:36:37 +00:00
|
|
|
if (!requestingHistory && value == 0) {
|
2019-07-01 13:53:01 +00:00
|
|
|
m_ui->historyStatus->setPixmap(Shared::icon("view-refresh", true).pixmap(25));
|
2019-05-15 17:36:37 +00:00
|
|
|
requestingHistory = true;
|
|
|
|
emit requestArchive(line->firstMessageId());
|
|
|
|
}
|
2019-04-14 20:37:02 +00:00
|
|
|
scroll = nothing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 17:36:37 +00:00
|
|
|
|
|
|
|
void Conversation::responseArchive(const std::list<Shared::Message> list)
|
|
|
|
{
|
|
|
|
requestingHistory = false;
|
|
|
|
scroll = keep;
|
|
|
|
|
|
|
|
m_ui->historyStatus->clear();
|
|
|
|
for (std::list<Shared::Message>::const_iterator itr = list.begin(), end = list.end(); itr != end; ++itr) {
|
|
|
|
addMessage(*itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::showEvent(QShowEvent* event)
|
|
|
|
{
|
|
|
|
if (!everShown) {
|
|
|
|
everShown = true;
|
2019-07-01 13:53:01 +00:00
|
|
|
m_ui->historyStatus->setPixmap(Shared::icon("view-refresh", true).pixmap(25));
|
2019-05-15 17:36:37 +00:00
|
|
|
requestingHistory = true;
|
|
|
|
emit requestArchive(line->firstMessageId());
|
|
|
|
}
|
2019-06-18 15:08:03 +00:00
|
|
|
emit shown();
|
2019-05-15 17:36:37 +00:00
|
|
|
|
|
|
|
QWidget::showEvent(event);
|
|
|
|
}
|
2019-06-21 19:33:38 +00:00
|
|
|
|
|
|
|
void Conversation::onAttach()
|
|
|
|
{
|
|
|
|
QFileDialog* d = new QFileDialog(this, "Chose a file to send");
|
|
|
|
d->setFileMode(QFileDialog::ExistingFile);
|
|
|
|
|
|
|
|
connect(d, SIGNAL(accepted()), this, SLOT(onFileSelected()));
|
|
|
|
connect(d, SIGNAL(rejected()), d, SLOT(deleteLater()));
|
|
|
|
|
|
|
|
d->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Conversation::onFileSelected()
|
|
|
|
{
|
|
|
|
QFileDialog* d = static_cast<QFileDialog*>(sender());
|
|
|
|
|
|
|
|
qDebug() << d->selectedFiles();
|
|
|
|
|
|
|
|
d->deleteLater();
|
|
|
|
}
|