From 59f539c4605e674a53b87c42e3f8d84d165b12ff Mon Sep 17 00:00:00 2001 From: blue Date: Thu, 12 Sep 2019 23:54:44 +0300 Subject: [PATCH] yet not working button to download offered files --- main.cpp | 3 ++ ui/squawk.cpp | 50 +++++++++++++++++++++++++ ui/squawk.h | 6 +++ ui/widgets/chat.cpp | 7 ---- ui/widgets/conversation.cpp | 12 ++++++ ui/widgets/conversation.h | 4 ++ ui/widgets/message.cpp | 74 ++++++++++++++++++++++++++++++++++++- ui/widgets/message.h | 18 +++++++++ ui/widgets/messageline.cpp | 29 +++++++++++++++ ui/widgets/messageline.h | 4 ++ 10 files changed, 199 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index ae50a8d..c69ea5c 100644 --- a/main.cpp +++ b/main.cpp @@ -87,6 +87,7 @@ int main(int argc, char *argv[]) squawk, SLOT(removeRoomRequest(const QString&, const QString&))); QObject::connect(&w, SIGNAL(addRoomRequest(const QString&, const QString&, const QString&, const QString&, bool)), squawk, SLOT(addRoomRequest(const QString&, const QString&, const QString&, const QString&, bool))); + QObject::connect(&w, SIGNAL(fileLocalPathRequest(const QString&, const QString&)), squawk, SLOT(fileLocalPathRequest(const QString&, const QString&))); QObject::connect(squawk, SIGNAL(newAccount(const QMap&)), &w, SLOT(newAccount(const QMap&))); QObject::connect(squawk, SIGNAL(addContact(const QString&, const QString&, const QString&, const QMap&)), @@ -119,6 +120,8 @@ int main(int argc, char *argv[]) &w, SLOT(changeRoomParticipant(const QString&, const QString&, const QString&, const QMap&))); QObject::connect(squawk, SIGNAL(removeRoomParticipant(const QString&, const QString&, const QString&)), &w, SLOT(removeRoomParticipant(const QString&, const QString&, const QString&))); + QObject::connect(squawk, SIGNAL(fileLocalPathResponse(const QString&, const QString&)), &w, SLOT(fileLocalPathResponse(const QString&, const QString&))); + //qDebug() << QStandardPaths::writableLocation(QStandardPaths::CacheLocation); diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 63820e9..596250f 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -262,14 +262,17 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item) Conversations::const_iterator itr = conversations.find(*id); Conversation* conv = 0; bool created = false; + Models::Contact::Messages deque; if (itr != conversations.end()) { conv = itr->second; } else if (contact != 0) { created = true; conv = new Chat(contact); + contact->getMessages(deque); } else if (room != 0) { created = true; conv = new Room(room); + room->getMessages(deque); if (!room->getJoined()) { emit setRoomJoined(id->account, id->name, true); @@ -283,9 +286,17 @@ void Squawk::onRosterItemDoubleClicked(const QModelIndex& item) connect(conv, SIGNAL(destroyed(QObject*)), this, SLOT(onConversationClosed(QObject*))); connect(conv, SIGNAL(sendMessage(const Shared::Message&)), this, SLOT(onConversationMessage(const Shared::Message&))); connect(conv, SIGNAL(requestArchive(const QString&)), this, SLOT(onConversationRequestArchive(const QString&))); + connect(conv, SIGNAL(requestLocalFile(const QString&, const QString&)), this, SLOT(onConversationRequestLocalFile(const QString&, const QString&))); + connect(conv, SIGNAL(downloadFile(const QString&, const QString&)), this, SLOT(onConversationDownloadFile(const QString&, const QString&))); connect(conv, SIGNAL(shown()), this, SLOT(onConversationShown())); conversations.insert(std::make_pair(*id, conv)); + + if (created) { + for (Models::Contact::Messages::const_iterator itr = deque.begin(), end = deque.end(); itr != end; ++itr) { + conv->addMessage(*itr); + } + } } conv->show(); @@ -325,6 +336,45 @@ void Squawk::onConversationClosed(QObject* parent) conversations.erase(itr); } +void Squawk::onConversationDownloadFile(const QString& messageId, const QString& url) +{ + +} + +void Squawk::fileLocalPathResponse(const QString& messageId, const QString& path) +{ + std::map>::const_iterator itr = requestedFiles.find(messageId); + if (itr == requestedFiles.end()) { + qDebug() << "fileLocalPathResponse in UI Squawk but there is nobody waiting for that path, skipping"; + return; + } else { + const std::set& convs = itr->second; + for (std::set::const_iterator cItr = convs.begin(), cEnd = convs.end(); cItr != cEnd; ++cItr) { + const Models::Roster::ElId& id = *cItr; + Conversations::const_iterator c = conversations.find(id); + if (c != conversations.end()) { + c->second->responseLocalFile(messageId, path); + } + } + requestedFiles.erase(itr); + } +} + +void Squawk::onConversationRequestLocalFile(const QString& messageId, const QString& url) +{ + Conversation* conv = static_cast(sender()); + std::map>::iterator itr = requestedFiles.find(messageId); + bool created = false; + if (itr == requestedFiles.end()) { + itr = requestedFiles.insert(std::make_pair(messageId, std::set())).first; + created = true; + } + itr->second.insert(Models::Roster::ElId(conv->getAccount(), conv->getJid())); + if (created) { + emit fileLocalPathRequest(messageId, url); + } +} + void Squawk::accountMessage(const QString& account, const Shared::Message& data) { const QString& from = data.getPenPalJid(); diff --git a/ui/squawk.h b/ui/squawk.h index a36caf5..c7adddb 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "widgets/accounts.h" @@ -65,6 +66,7 @@ signals: void setRoomAutoJoin(const QString& account, const QString& jid, bool joined); void addRoomRequest(const QString& account, const QString& jid, const QString& nick, const QString& password, bool autoJoin); void removeRoomRequest(const QString& account, const QString& jid); + void fileLocalPathRequest(const QString& messageId, const QString& url); public slots: void newAccount(const QMap& account); @@ -87,6 +89,7 @@ public slots: void addRoomParticipant(const QString& account, const QString& jid, const QString& name, const QMap& data); void changeRoomParticipant(const QString& account, const QString& jid, const QString& name, const QMap& data); void removeRoomParticipant(const QString& account, const QString& jid, const QString& name); + void fileLocalPathResponse(const QString& messageId, const QString& path); private: typedef std::map Conversations; @@ -97,6 +100,7 @@ private: Conversations conversations; QMenu* contextMenu; QDBusInterface dbus; + std::map> requestedFiles; protected: void closeEvent(QCloseEvent * event) override; @@ -117,6 +121,8 @@ private slots: void onConversationRequestArchive(const QString& before); void onRosterContextMenu(const QPoint& point); void onConversationShown(); + void onConversationRequestLocalFile(const QString& messageId, const QString& url); + void onConversationDownloadFile(const QString& messageId, const QString& url); }; diff --git a/ui/widgets/chat.cpp b/ui/widgets/chat.cpp index 720584a..aeaec65 100644 --- a/ui/widgets/chat.cpp +++ b/ui/widgets/chat.cpp @@ -29,13 +29,6 @@ Chat::Chat(Models::Contact* p_contact, QWidget* parent): connect(contact, SIGNAL(childChanged(Models::Item*, int, int)), this, SLOT(onContactChanged(Models::Item*, int, int))); line->setMyName(p_contact->getAccountName()); - - Models::Contact::Messages deque; - contact->getMessages(deque); - - for (Models::Contact::Messages::const_iterator itr = deque.begin(), end = deque.end(); itr != end; ++itr) { - addMessage(*itr); - } } Chat::~Chat() diff --git a/ui/widgets/conversation.cpp b/ui/widgets/conversation.cpp index 26ac8ad..47a8f25 100644 --- a/ui/widgets/conversation.cpp +++ b/ui/widgets/conversation.cpp @@ -58,6 +58,8 @@ Conversation::Conversation(bool muc, const QString& mJid, const QString mRes, co connect(&vis, SIGNAL(hidden()), this, SLOT(onScrollResize())); connect(m_ui->sendButton, SIGNAL(clicked(bool)), this, SLOT(onEnterPressed())); 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())); m_ui->messageEditor->installEventFilter(&ker); @@ -283,6 +285,16 @@ void Conversation::onScrollResize() } } +void Conversation::responseDownloadProgress(const QString& messageId, qreal progress) +{ + line->responseDownloadProgress(messageId, progress); +} + +void Conversation::responseLocalFile(const QString& messageId, const QString& path) +{ + line->responseLocalFile(messageId, path); +} + Resizer::Resizer(QWidget* parent): QObject(parent) { diff --git a/ui/widgets/conversation.h b/ui/widgets/conversation.h index 4d84423..2c0ce9d 100644 --- a/ui/widgets/conversation.h +++ b/ui/widgets/conversation.h @@ -82,11 +82,15 @@ public: void setPalResource(const QString& res); void responseArchive(const std::list list); void showEvent(QShowEvent * event) override; + void responseLocalFile(const QString& messageId, const QString& path); + void responseDownloadProgress(const QString& messageId, qreal progress); signals: void sendMessage(const Shared::Message& message); void requestArchive(const QString& before); void shown(); + void requestLocalFile(const QString& messageId, const QString& url); + void downloadFile(const QString& messageId, const QString& url); protected: virtual void setName(const QString& name); diff --git a/ui/widgets/message.cpp b/ui/widgets/message.cpp index 965e548..5efc94a 100644 --- a/ui/widgets/message.cpp +++ b/ui/widgets/message.cpp @@ -30,7 +30,13 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_ date(new QLabel(msg.getTime().toLocalTime().toString())), sender(new QLabel(p_sender)), text(new QLabel()), - shadow(new QGraphicsDropShadowEffect()) + shadow(new QGraphicsDropShadowEffect()), + downloadButton(0), + file(0), + progress(0), + hasDownloadButton(false), + hasProgress(false), + hasFile(false) { body->setBackgroundRole(QPalette::AlternateBase); body->setAutoFillBackground(true); @@ -42,6 +48,9 @@ Message::Message(const Shared::Message& source, bool outgoing, const QString& p_ 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); @@ -87,3 +96,66 @@ void Message::setSender(const QString& p_sender) { sender->setText(p_sender); } + +void Message::addDownloadDialog() +{ + if (hasFile) { + file->deleteLater(); + file = 0; + hasFile = false; + } + if (hasProgress) { + progress->deleteLater(); + progress = 0; + hasProgress = false;; + } + if (!hasDownloadButton) { + downloadButton = new QPushButton(QIcon::fromTheme("download"), "Download"); + connect(downloadButton, SIGNAL(clicked()), this, SLOT(onDownload())); + bodyLayout->insertWidget(2, downloadButton); + hasDownloadButton = true; + } +} + +void Message::onDownload() +{ + emit downloadFile(msg.getId(), msg.getOutOfBandUrl()); +} + +void Message::setProgress(qreal value) +{ + if (hasFile) { + file->deleteLater(); + file = 0; + hasFile = false; + } + if (hasDownloadButton) { + downloadButton->deleteLater(); + downloadButton = 0; + hasDownloadButton = false; + } + if (!hasProgress) { + progress = new QLabel(std::to_string(value).c_str()); + bodyLayout->insertWidget(2, progress); + hasProgress = true; + } +} + +void Message::showFile(const QString& path) +{ + if (hasDownloadButton) { + downloadButton->deleteLater(); + downloadButton = 0; + hasDownloadButton = false; + } + if (hasProgress) { + progress->deleteLater(); + progress = 0; + hasProgress = false;; + } + if (!hasFile) { + file = new QLabel(path); + bodyLayout->insertWidget(2, file); + hasFile = true; + } +} diff --git a/ui/widgets/message.h b/ui/widgets/message.h index 80f979b..2adb983 100644 --- a/ui/widgets/message.h +++ b/ui/widgets/message.h @@ -24,6 +24,7 @@ #include #include #include +#include #include "../../global.h" @@ -40,6 +41,13 @@ public: void setSender(const QString& sender); QString getId() const; + void addDownloadDialog(); + void showFile(const QString& path); + void setProgress(qreal value); + +signals: + void downloadFile(const QString& messageId, const QString& url); + private: Shared::Message msg; QWidget* body; @@ -48,6 +56,16 @@ private: QLabel* sender; QLabel* text; QGraphicsDropShadowEffect* shadow; + QPushButton* downloadButton; + QLabel* file; + QLabel* progress; + bool hasDownloadButton; + bool hasProgress; + bool hasFile; + +private slots: + void onDownload(); + }; #endif // MESSAGE_H diff --git a/ui/widgets/messageline.cpp b/ui/widgets/messageline.cpp index 64627ca..7ff8a38 100644 --- a/ui/widgets/messageline.cpp +++ b/ui/widgets/messageline.cpp @@ -149,6 +149,11 @@ MessageLine::Position MessageLine::message(const Shared::Message& msg) 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; } @@ -217,3 +222,27 @@ 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(); + } + } +} diff --git a/ui/widgets/messageline.h b/ui/widgets/messageline.h index 2715696..ce435c7 100644 --- a/ui/widgets/messageline.h +++ b/ui/widgets/messageline.h @@ -52,9 +52,13 @@ public: QString firstMessageId() const; void showBusyIndicator(); void hideBusyIndicator(); + void responseLocalFile(const QString& messageId, const QString& path); + 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;