1
0
Fork 0
forked from blue/squawk

some creepy file attaching gui, not sending yet

This commit is contained in:
Blue 2019-09-19 17:31:27 +03:00
parent 2089d6af86
commit 100b2e8943
12 changed files with 424 additions and 6 deletions

View file

@ -23,6 +23,7 @@
#include <QTimer>
#include <QGraphicsDropShadowEffect>
#include <QFileDialog>
#include <QMimeDatabase>
Conversation::Conversation(bool muc, const QString& mJid, const QString mRes, const QString pJid, const QString pRes, const QString& acc, QWidget* parent):
QWidget(parent),
@ -40,12 +41,18 @@ Conversation::Conversation(bool muc, const QString& mJid, const QString mRes, co
thread(),
statusIcon(0),
statusLabel(0),
filesLayout(0),
filesToAttach(),
scroll(down),
manualSliderChange(false),
requestingHistory(false),
everShown(false)
{
m_ui->setupUi(this);
filesLayout = new FlowLayout(m_ui->filesPanel, 0);
m_ui->filesPanel->setLayout(filesLayout);
m_ui->splitter->setSizes({300, 0});
m_ui->splitter->setStretchFactor(1, 0);
@ -60,7 +67,7 @@ Conversation::Conversation(bool muc, const QString& mJid, const QString mRes, co
connect(line, SIGNAL(resize(int)), this, SLOT(onMessagesResize(int)));
connect(line, SIGNAL(downloadFile(const QString&, const QString&)), this, SIGNAL(downloadFile(const QString&, const QString&)));
connect(line, SIGNAL(requestLocalFile(const QString&, const QString&)), this, SIGNAL(requestLocalFile(const QString&, const QString&)));
//connect(m_ui->attachButton, SIGNAL(clicked(bool)), this, SLOT(onAttach()));
connect(m_ui->attachButton, SIGNAL(clicked(bool)), this, SLOT(onAttach()));
m_ui->messageEditor->installEventFilter(&ker);
@ -263,7 +270,9 @@ void Conversation::onFileSelected()
{
QFileDialog* d = static_cast<QFileDialog*>(sender());
qDebug() << d->selectedFiles();
for (const QString& path : d->selectedFiles()) {
addAttachedFile(path);
}
d->deleteLater();
}
@ -300,6 +309,49 @@ void Conversation::responseLocalFile(const QString& messageId, const QString& pa
line->responseLocalFile(messageId, path);
}
void Conversation::addAttachedFile(const QString& path)
{
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(path);
QFileInfo info(path);
Badge* badge = new Badge(path, info.fileName(), QIcon::fromTheme(type.iconName()));
connect(badge, SIGNAL(close()), this, SLOT(onBadgeClose()));
filesToAttach.push_back(badge); //TODO neet to check if there are any duplicated ids
filesLayout->addWidget(badge);
if (filesLayout->count() == 1) {
filesLayout->setContentsMargins(3, 3, 3, 3);
}
}
void Conversation::removeAttachedFile(Badge* badge)
{
W::Order<Badge*, Badge::Comparator>::const_iterator itr = filesToAttach.find(badge);
if (itr != filesToAttach.end()) {
filesToAttach.erase(badge);
if (filesLayout->count() == 1) {
filesLayout->setContentsMargins(0, 0, 0, 0);
}
badge->deleteLater();
}
}
void Conversation::onBadgeClose()
{
Badge* badge = static_cast<Badge*>(sender());
removeAttachedFile(badge);
}
void Conversation::clearAttachedFiles()
{
for (Badge* badge : filesToAttach) {
badge->deleteLater();
}
filesToAttach.clear();
filesLayout->setContentsMargins(0, 0, 0, 0);
}
bool VisibilityCatcher::eventFilter(QObject* obj, QEvent* event)
{
if (event->type() == QEvent::Show) {

View file

@ -22,8 +22,11 @@
#include <QWidget>
#include <QScopedPointer>
#include "../../global.h"
#include "messageline.h"
#include "../../order.h"
#include "../utils/messageline.h"
#include "../utils/resizer.h"
#include "../utils/flowlayout.h"
#include "../utils/badge.h"
namespace Ui
{
@ -87,6 +90,9 @@ protected:
void applyVisualEffects();
virtual void handleSendMessage(const QString& text) = 0;
void setStatus(const QString& status);
void addAttachedFile(const QString& path);
void removeAttachedFile(Badge* badge);
void clearAttachedFiles();
protected slots:
void onEnterPressed();
@ -95,6 +101,7 @@ protected slots:
void onAttach();
void onFileSelected();
void onScrollResize();
void onBadgeClose();
public:
const bool isMuc;
@ -118,6 +125,8 @@ protected:
QString thread;
QLabel* statusIcon;
QLabel* statusLabel;
FlowLayout* filesLayout;
W::Order<Badge*, Badge::Comparator> filesToAttach;
Scroll scroll;
bool manualSliderChange;
bool requestingHistory;

View file

@ -202,7 +202,7 @@
<x>0</x>
<y>0</y>
<width>572</width>
<height>123</height>
<height>118</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
@ -384,6 +384,13 @@
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="filesPanel" native="true">
<property name="autoFillBackground">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="ut" native="true">
<property name="sizePolicy">
@ -443,6 +450,7 @@
</layout>
<zorder>messageEditor</zorder>
<zorder>ut</zorder>
<zorder>filesPanel</zorder>
<zorder>panel</zorder>
</widget>
</widget>

View file

@ -1,245 +0,0 @@
/*
* Squawk messenger.
* 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 <QDebug>
#include <QMimeDatabase>
#include <QPixmap>
#include <QFileInfo>
#include "message.h"
const QRegExp urlReg("^(?!<img\\ssrc=\")((?:https?|ftp)://\\S+)");
const QRegExp imgReg("((?:https?|ftp)://\\S+\\.(?:jpg|jpeg|png|svg|gif))");
Message::Message(const Shared::Message& source, bool outgoing, const QString& p_sender, QWidget* parent):
QHBoxLayout(parent),
msg(source),
body(new QWidget()),
bodyLayout(new QVBoxLayout(body)),
date(new QLabel(msg.getTime().toLocalTime().toString())),
sender(new QLabel(p_sender)),
text(new QLabel()),
shadow(new QGraphicsDropShadowEffect()),
downloadButton(0),
file(0),
progress(0),
fileComment(new QLabel()),
errorText(""),
hasDownloadButton(false),
hasProgress(false),
hasFile(false),
commentAdded(false),
errorDownloadingFile(false)
{
body->setBackgroundRole(QPalette::AlternateBase);
body->setAutoFillBackground(true);
QString bd = msg.getBody();
//bd.replace(imgReg, "<img src=\"\\1\"/>");
bd.replace(urlReg, "<a href=\"\\1\">\\1</a>");
text->setText(bd);;
text->setTextInteractionFlags(text->textInteractionFlags() | Qt::TextSelectableByMouse | Qt::LinksAccessibleByMouse);
text->setWordWrap(true);
text->setOpenExternalLinks(true);
if (bd.size() == 0) {
text->hide();
}
QFont dFont = date->font();
dFont.setItalic(true);
dFont.setPointSize(dFont.pointSize() - 2);
date->setFont(dFont);
date->setForegroundRole(QPalette::ToolTipText);
QFont f;
f.setBold(true);
sender->setFont(f);
bodyLayout->addWidget(sender);
bodyLayout->addWidget(text);
bodyLayout->addWidget(date);
shadow->setBlurRadius(10);
shadow->setXOffset(1);
shadow->setYOffset(1);
shadow->setColor(Qt::black);
body->setGraphicsEffect(shadow);
if (outgoing) {
addWidget(body);
addStretch();
} else {
sender->setAlignment(Qt::AlignRight);
date->setAlignment(Qt::AlignRight);
addStretch();
addWidget(body);
}
}
Message::~Message()
{
if (!commentAdded) {
delete fileComment;
}
}
QString Message::getId() const
{
return msg.getId();
}
void Message::setSender(const QString& p_sender)
{
sender->setText(p_sender);
}
void Message::addDownloadDialog()
{
hideFile();
hideProgress();
if (!hasDownloadButton) {
hideComment();
if (msg.getBody() == msg.getOutOfBandUrl()) {
text->setText("");
text->hide();
}
downloadButton = new QPushButton(QIcon::fromTheme("download"), "Download");
downloadButton->setToolTip("<a href=\"" + msg.getOutOfBandUrl() + "\">" + msg.getOutOfBandUrl() + "</a>");
if (errorDownloadingFile) {
fileComment->setWordWrap(true);
fileComment->setText("Error downloading file: " + errorText + "\nYou can try again");
} else {
fileComment->setText(sender->text() + " is offering you to download a file");
}
fileComment->show();
connect(downloadButton, SIGNAL(clicked()), this, SLOT(onDownload()));
bodyLayout->insertWidget(2, fileComment);
bodyLayout->insertWidget(3, downloadButton);
hasDownloadButton = true;
commentAdded = true;
}
}
void Message::onDownload()
{
emit downloadFile(msg.getId(), msg.getOutOfBandUrl());
}
void Message::setProgress(qreal value)
{
hideFile();
hideDownload();
if (!hasProgress) {
hideComment();
if (msg.getBody() == msg.getOutOfBandUrl()) {
text->setText("");
text->hide();
}
progress = new QProgressBar();
progress->setRange(0, 100);
fileComment->setText("Downloading...");
fileComment->show();
bodyLayout->insertWidget(2, progress);
bodyLayout->insertWidget(3, fileComment);
hasProgress = true;
commentAdded = true;
}
progress->setValue(value * 100);
}
void Message::showFile(const QString& path)
{
hideDownload();
hideProgress();
if (!hasFile) {
hideComment();
if (msg.getBody() == msg.getOutOfBandUrl()) {
text->setText("");
text->hide();
}
QMimeDatabase db;
QMimeType type = db.mimeTypeForFile(path);
QStringList parts = type.name().split("/");
QString big = parts.front();
QFileInfo info(path);
fileComment = new QLabel();
if (big == "image") {
file = new Image(path);
} else {
file = new QLabel();
file->setPixmap(QIcon::fromTheme(type.iconName()).pixmap(50));
file->setAlignment(Qt::AlignCenter);
fileComment->setText(info.fileName());
fileComment->setWordWrap(true);
fileComment->show();
}
file->setContextMenuPolicy(Qt::ActionsContextMenu);
QAction* openAction = new QAction(QIcon::fromTheme("document-new-from-template"), "Open", file);
connect(openAction, &QAction::triggered, [path]() { //TODO need to get rid of this shame
QDesktopServices::openUrl(QUrl::fromLocalFile(path));
});
file->addAction(openAction);
bodyLayout->insertWidget(2, file);
bodyLayout->insertWidget(3, fileComment);
hasFile = true;
commentAdded = true;
}
}
void Message::hideComment()
{
if (commentAdded) {
bodyLayout->removeWidget(fileComment);
fileComment->hide();
fileComment->setWordWrap(false);
}
}
void Message::hideDownload()
{
if (hasDownloadButton) {
downloadButton->deleteLater();
downloadButton = 0;
hasDownloadButton = false;
errorDownloadingFile = false;
}
}
void Message::hideFile()
{
if (hasFile) {
file->deleteLater();
file = 0;
hasFile = false;
}
}
void Message::hideProgress()
{
if (hasProgress) {
progress->deleteLater();
progress = 0;
hasProgress = false;;
}
}
void Message::showError(const QString& error)
{
errorDownloadingFile = true;
errorText = error;
addDownloadDialog();
}

View file

@ -1,87 +0,0 @@
/*
* Squawk messenger.
* 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/>.
*/
#ifndef MESSAGE_H
#define MESSAGE_H
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QGraphicsDropShadowEffect>
#include <QPushButton>
#include <QProgressBar>
#include <QAction>
#include <QDesktopServices>
#include <QUrl>
#include "../../global.h"
#include "../utils/resizer.h"
#include "../utils/image.h"
/**
* @todo write docs
*/
class Message : public QHBoxLayout
{
Q_OBJECT
public:
Message(const Shared::Message& source, bool outgoing, const QString& sender, QWidget* parent = nullptr);
~Message();
void setSender(const QString& sender);
QString getId() const;
void addDownloadDialog();
void showFile(const QString& path);
void showError(const QString& error);
void setProgress(qreal value);
signals:
void downloadFile(const QString& messageId, const QString& url);
private:
Shared::Message msg;
QWidget* body;
QVBoxLayout* bodyLayout;
QLabel* date;
QLabel* sender;
QLabel* text;
QGraphicsDropShadowEffect* shadow;
QPushButton* downloadButton;
QLabel* file;
QProgressBar* progress;
QLabel* fileComment;
QString errorText;
bool hasDownloadButton;
bool hasProgress;
bool hasFile;
bool commentAdded;
bool errorDownloadingFile;
private slots:
void onDownload();
private:
void hideDownload();
void hideProgress();
void hideFile();
void hideComment();
};
#endif // MESSAGE_H

View file

@ -1,259 +0,0 @@
/*
* Squawk messenger.
* 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 "messageline.h"
#include <QDebug>
#include <cmath>
MessageLine::MessageLine(bool p_room, QWidget* parent):
QWidget(parent),
messageIndex(),
messageOrder(),
myMessages(),
palMessages(),
layout(new QVBoxLayout(this)),
myName(),
palNames(),
views(),
room(p_room),
busyPixmap(new QGraphicsPixmapItem(Shared::icon("view-refresh", true).pixmap(70))),
busyScene(),
busyLabel(&busyScene),
busyLayout(),
busyShown(false),
rotation()
{
setBackgroundRole(QPalette::Base);
layout->addStretch();
busyScene.addItem(busyPixmap);
busyLayout.addStretch();
busyLayout.addWidget(&busyLabel);
busyLayout.addStretch();
busyLabel.setMaximumSize(70, 70);
busyLabel.setMinimumSize(70, 70);
busyLabel.setSceneRect(0, 0, 70, 70);
busyLabel.setFrameStyle(0);
busyLabel.setContentsMargins(0, 0, 0, 0);
busyLabel.setInteractive(false);
busyPixmap->setTransformOriginPoint(35, 35);
busyPixmap->setTransformationMode(Qt::SmoothTransformation);
busyPixmap->setOffset(0, 0);;
rotation.setDuration(500);
rotation.setStartValue(0.0f);
rotation.setEndValue(180.0f);
rotation.setLoopCount(-1);
connect(&rotation, SIGNAL(valueChanged(const QVariant&)), this, SLOT(onAnimationValueChanged(const QVariant&)));
}
MessageLine::~MessageLine()
{
for (Index::const_iterator itr = messageIndex.begin(), end = messageIndex.end(); itr != end; ++itr) {
delete itr->second;
}
}
MessageLine::Position MessageLine::message(const Shared::Message& msg)
{
QString id = msg.getId();
Index::iterator itr = messageIndex.find(id);
if (itr != messageIndex.end()) {
qDebug() << "received more then one message with the same id, skipping yet the new one";
return invalid;
}
QString sender;
bool outgoing;
if (room) {
if (msg.getFromResource() == myName) {
sender = myName;
outgoing = false;
} else {
sender = msg.getFromResource();
outgoing = true;
}
} else {
if (msg.getOutgoing()) {
sender = myName;
outgoing = false;
} else {
QString jid = msg.getFromJid();
std::map<QString, QString>::iterator itr = palNames.find(jid);
if (itr != palNames.end()) {
sender = itr->second;
} else {
sender = jid;
}
outgoing = true;
}
}
Message* message = new Message(msg, outgoing, sender);
std::pair<Order::const_iterator, bool> result = messageOrder.insert(std::make_pair(msg.getTime(), message));
if (!result.second) {
qDebug() << "Error appending a message into a message list - seems like the time of that message exactly matches the time of some other message, can't put them in order, skipping yet";
delete message;
return invalid;
}
if (outgoing) {
if (room) {
} else {
QString jid = msg.getFromJid();
std::map<QString, Index>::iterator pItr = palMessages.find(jid);
if (pItr == palMessages.end()) {
pItr = palMessages.insert(std::make_pair(jid, Index())).first;
}
pItr->second.insert(std::make_pair(id, message));
}
} else {
myMessages.insert(std::make_pair(id, message));
}
messageIndex.insert(std::make_pair(id, message));
int index = std::distance<Order::const_iterator>(messageOrder.begin(), result.first); //need to make with binary indexed tree
Position res = invalid;
if (index == 0) {
res = beggining;
} else if (index == messageIndex.size() - 1) {
res = end;
} else {
res = middle;
}
if (busyShown) {
index += 1;
}
if (res == end) {
layout->addLayout(message);
} else {
layout->insertLayout(index, message);
}
if (msg.hasOutOfBandUrl()) {\
emit requestLocalFile(msg.getId(), msg.getOutOfBandUrl());
connect(message, SIGNAL(downloadFile(const QString&, const QString&)), this, SIGNAL(downloadFile(const QString&, const QString&)));
}
return res;
}
void MessageLine::setMyName(const QString& name)
{
myName = name;
for (Index::const_iterator itr = myMessages.begin(), end = myMessages.end(); itr != end; ++itr) {
itr->second->setSender(name);
}
}
void MessageLine::setPalName(const QString& jid, const QString& name)
{
std::map<QString, QString>::iterator itr = palNames.find(jid);
if (itr == palNames.end()) {
palNames.insert(std::make_pair(jid, name));
} else {
itr->second = name;
}
std::map<QString, Index>::iterator pItr = palMessages.find(jid);
if (pItr != palMessages.end()) {
for (Index::const_iterator itr = pItr->second.begin(), end = pItr->second.end(); itr != end; ++itr) {
itr->second->setSender(name);
}
}
}
void MessageLine::resizeEvent(QResizeEvent* event)
{
QWidget::resizeEvent(event);
emit resize(event->size().height() - event->oldSize().height());
}
QString MessageLine::firstMessageId() const
{
if (messageOrder.size() == 0) {
return "";
} else {
return messageOrder.begin()->second->getId();
}
}
void MessageLine::showBusyIndicator()
{
if (!busyShown) {
layout->insertLayout(0, &busyLayout);
busyShown = true;
rotation.start();
busyLabel.show();
}
}
void MessageLine::hideBusyIndicator()
{
if (busyShown) {
busyLabel.hide();
rotation.stop();
layout->removeItem(&busyLayout);
busyShown = false;
}
}
void MessageLine::onAnimationValueChanged(const QVariant& value)
{
busyPixmap->setRotation(value.toReal());
}
void MessageLine::responseDownloadProgress(const QString& messageId, qreal progress)
{
Index::const_iterator itr = messageIndex.find(messageId);
if (itr == messageIndex.end()) {
} else {
itr->second->setProgress(progress);
}
}
void MessageLine::responseLocalFile(const QString& messageId, const QString& path)
{
Index::const_iterator itr = messageIndex.find(messageId);
if (itr == messageIndex.end()) {
} else {
if (path.size() > 0) {
itr->second->showFile(path);
} else {
itr->second->addDownloadDialog();
}
}
}
void MessageLine::downloadError(const QString& messageId, const QString& error)
{
Index::const_iterator itr = messageIndex.find(messageId);
if (itr == messageIndex.end()) {
} else {
itr->second->showError(error);
}
}

View file

@ -1,99 +0,0 @@
/*
* Squawk messenger.
* 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/>.
*/
#ifndef MESSAGELINE_H
#define MESSAGELINE_H
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QResizeEvent>
#include <QIcon>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QVariantAnimation>
#include "../global.h"
#include "message.h"
class MessageLine : public QWidget
{
Q_OBJECT
public:
enum Position {
beggining,
middle,
end,
invalid
};
MessageLine(bool p_room, QWidget* parent = 0);
~MessageLine();
Position message(const Shared::Message& msg);
void setMyName(const QString& name);
void setPalName(const QString& jid, const QString& name);
QString firstMessageId() const;
void showBusyIndicator();
void hideBusyIndicator();
void responseLocalFile(const QString& messageId, const QString& path);
void downloadError(const QString& messageId, const QString& error);
void responseDownloadProgress(const QString& messageId, qreal progress);
signals:
void resize(int amount);
void downloadFile(const QString& messageId, const QString& url);
void requestLocalFile(const QString& messageId, const QString& url);
protected:
void resizeEvent(QResizeEvent * event) override;
private:
struct Comparator {
bool operator()(const Shared::Message& a, const Shared::Message& b) const {
return a.getTime() < b.getTime();
}
bool operator()(const Shared::Message* a, const Shared::Message* b) const {
return a->getTime() < b->getTime();
}
};
typedef std::map<QDateTime, Message*> Order;
typedef std::map<QString, Message*> Index;
Index messageIndex;
Order messageOrder;
Index myMessages;
std::map<QString, Index> palMessages;
QVBoxLayout* layout;
QString myName;
std::map<QString, QString> palNames;
std::deque<QHBoxLayout*> views;
bool room;
QGraphicsPixmapItem* busyPixmap;
QGraphicsScene busyScene;
QGraphicsView busyLabel;
QHBoxLayout busyLayout;
bool busyShown;
QVariantAnimation rotation;
private slots:
void onAnimationValueChanged(const QVariant& value);
};
#endif // MESSAGELINE_H