Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
Merge branch 'v1.17-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonybilinski committed Oct 22, 2019
2 parents 1ad561c + 1a726b5 commit f7d82a4
Show file tree
Hide file tree
Showing 18 changed files with 534 additions and 161 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ set(${PROJECT_NAME}_SOURCES
src/chatlog/content/text.h
src/chatlog/content/timestamp.cpp
src/chatlog/content/timestamp.h
src/chatlog/content/broken.cpp
src/chatlog/content/broken.h
src/chatlog/customtextdocument.cpp
src/chatlog/customtextdocument.h
src/chatlog/documentcache.cpp
Expand Down
26 changes: 19 additions & 7 deletions src/chatlog/chatmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
#include "content/spinner.h"
#include "content/text.h"
#include "content/timestamp.h"
#include "content/broken.h"
#include "src/widget/style.h"

#include <QDebug>
#include <QCryptographicHash>

#include "src/persistence/settings.h"
#include "src/persistence/smileypack.h"
#include "src/persistence/history.h"

#define NAME_COL_WIDTH 90.0
#define TIME_COL_WIDTH 90.0
Expand All @@ -43,7 +45,8 @@ ChatMessage::ChatMessage()
}

ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QString& rawMessage,
MessageType type, bool isMe, const QDateTime& date, bool colorizeName)
MessageType type, bool isMe, MessageState state,
const QDateTime& date, bool colorizeName)
{
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);

Expand Down Expand Up @@ -105,12 +108,21 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
? QString("%1 %2").arg(sender, rawMessage)
: rawMessage),
ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Spinner(Style::getImagePath("chatArea/spinner.svg"), QSize(16, 16), 360.0 / 1.6),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));

if (!date.isNull())
msg->markAsSent(date);

switch (state) {
case MessageState::complete:
msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
break;
case MessageState::pending:
msg->addColumn(new Spinner(Style::getImagePath("chatArea/spinner.svg"), QSize(16, 16), 360.0 / 1.6),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
break;
case MessageState::broken:
msg->addColumn(new Broken(Style::getImagePath("chatArea/error.svg"), QSize(16, 16)),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
break;
}
return msg;
}

Expand Down Expand Up @@ -207,7 +219,7 @@ ChatMessage::Ptr ChatMessage::createBusyNotification()
return msg;
}

void ChatMessage::markAsSent(const QDateTime& time)
void ChatMessage::markAsDelivered(const QDateTime& time)
{
QFont baseFont = Settings::getInstance().getChatMessageFont();

Expand Down
8 changes: 5 additions & 3 deletions src/chatlog/chatmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "chatline.h"
#include "src/core/toxfile.h"
#include "src/persistence/history.h"

#include <QDateTime>

class QGraphicsScene;
Expand All @@ -48,16 +50,16 @@ class ChatMessage : public ChatLine
ChatMessage();

static ChatMessage::Ptr createChatMessage(const QString& sender, const QString& rawMessage,
MessageType type, bool isMe,
const QDateTime& date = QDateTime(), bool colorizeName = false);
MessageType type, bool isMe, MessageState state,
const QDateTime& date, bool colorizeName = false);
static ChatMessage::Ptr createChatInfoMessage(const QString& rawMessage, SystemMessageType type,
const QDateTime& date);
static ChatMessage::Ptr createFileTransferMessage(const QString& sender, ToxFile file,
bool isMe, const QDateTime& date);
static ChatMessage::Ptr createTypingNotification();
static ChatMessage::Ptr createBusyNotification();

void markAsSent(const QDateTime& time);
void markAsDelivered(const QDateTime& time);
QString toString() const;
bool isAction() const;
void setAsAction();
Expand Down
61 changes: 61 additions & 0 deletions src/chatlog/content/broken.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright © 2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre 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.
qTox 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 qTox. If not, see <http://www.gnu.org/licenses/>.
*/

#include "broken.h"
#include "src/chatlog/pixmapcache.h"
#include <QPainter>

class QStyleOptionGraphicsItem;

Broken::Broken(const QString& img, QSize size)
: pmap{PixmapCache::getInstance().get(img, size)}
, size{size}
{
}

QRectF Broken::boundingRect() const
{
return QRectF(QPointF(-size.width() / 2.0, -size.height() / 2.0), size);
}

void Broken::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
QWidget* widget)
{
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->drawPixmap(0, 0, pmap);

Q_UNUSED(option)
Q_UNUSED(widget)

}

void Broken::setWidth(qreal width)
{
Q_UNUSED(width);
}

void Broken::visibilityChanged(bool visible)
{
Q_UNUSED(visible);
}

qreal Broken::getAscent() const
{
return 0.0;
}
45 changes: 45 additions & 0 deletions src/chatlog/content/broken.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright © 2019 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre 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.
qTox 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 qTox. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef BROKEN_H
#define BROKEN_H

#include "../chatlinecontent.h"

#include <QObject>
#include <QPixmap>

class Broken : public ChatLineContent
{
Q_OBJECT
public:
Broken(const QString& img, QSize size);
QRectF boundingRect() const override;
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
QWidget* widget) override;
void setWidth(qreal width) override;
void visibilityChanged(bool visible) override;
qreal getAscent() const override;

private:
QSize size;
QPixmap pmap;
};

#endif // BROKEN_H
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ int main(int argc, char* argv[])
} else {
nexus.setParser(&parser);
int returnval = nexus.showLogin(profileName);
if (returnval != 0) {
return returnval;
if (returnval == QDialog::Rejected) {
return -1;
}
}

Expand Down
38 changes: 23 additions & 15 deletions src/model/chathistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& co
, settings(settings_)
, coreIdHandler(coreIdHandler)
{
connect(&messageDispatcher, &IMessageDispatcher::messageSent, this, &ChatHistory::onMessageSent);
connect(&messageDispatcher, &IMessageDispatcher::messageComplete, this,
&ChatHistory::onMessageComplete);
connect(&messageDispatcher, &IMessageDispatcher::messageReceived, this,
&ChatHistory::onMessageReceived);

if (canUseHistory()) {
// Defer messageSent callback until we finish firing off all our unsent messages.
Expand All @@ -105,8 +106,7 @@ ChatHistory::ChatHistory(Friend& f_, History* history_, const ICoreIdHandler& co
}

// Now that we've fired off our unsent messages we can connect the message
connect(&messageDispatcher, &IMessageDispatcher::messageReceived, this,
&ChatHistory::onMessageReceived);
connect(&messageDispatcher, &IMessageDispatcher::messageSent, this, &ChatHistory::onMessageSent);

// NOTE: this has to be done _after_ sending all sent messages since initial
// state of the message has to be marked according to our dispatch state
Expand Down Expand Up @@ -385,15 +385,23 @@ void ChatHistory::loadHistoryIntoSessionChatLog(ChatLogIdx start) const
std::find_if(dispatchedMessageRowIdMap.begin(), dispatchedMessageRowIdMap.end(),
[&](RowId dispatchedId) { return dispatchedId == message.id; });

bool isComplete = dispatchedMessageIt == dispatchedMessageRowIdMap.end();

if (isComplete) {
auto chatLogMessage = ChatLogMessage{true, processedMessage};
sessionChatLog.insertMessageAtIdx(currentIdx, sender, message.dispName, chatLogMessage);
} else {
// If the message is incomplete we have to pretend we sent it to ensure
// sessionChatLog state is correct
sessionChatLog.onMessageSent(dispatchedMessageIt.key(), processedMessage);
assert((message.state != MessageState::pending && dispatchedMessageIt == dispatchedMessageRowIdMap.end()) ||
(message.state == MessageState::pending && dispatchedMessageIt != dispatchedMessageRowIdMap.end()));

auto chatLogMessage = ChatLogMessage{message.state, processedMessage};
switch (message.state) {
case MessageState::complete:
sessionChatLog.insertCompleteMessageAtIdx(currentIdx, sender, message.dispName,
chatLogMessage);
break;
case MessageState::pending:
sessionChatLog.insertIncompleteMessageAtIdx(currentIdx, sender, message.dispName,
chatLogMessage, dispatchedMessageIt.key());
break;
case MessageState::broken:
sessionChatLog.insertBrokenMessageAtIdx(currentIdx, sender, message.dispName,
chatLogMessage);
break;
}
break;
}
Expand All @@ -409,7 +417,7 @@ void ChatHistory::loadHistoryIntoSessionChatLog(ChatLogIdx start) const
*/
void ChatHistory::dispatchUnsentMessages(IMessageDispatcher& messageDispatcher)
{
auto unsentMessages = history->getUnsentMessagesForFriend(f.getPublicKey());
auto unsentMessages = history->getUndeliveredMessagesForFriend(f.getPublicKey());
for (auto& message : unsentMessages) {
// We should only store messages as unsent, if this changes in the
// future we need to extend this logic
Expand Down Expand Up @@ -443,7 +451,7 @@ void ChatHistory::handleDispatchedMessage(DispatchedMessageId dispatchId, RowId
if (completedMessageIt == completedMessages.end()) {
dispatchedMessageRowIdMap.insert(dispatchId, historyId);
} else {
history->markAsSent(historyId);
history->markAsDelivered(historyId);
completedMessages.erase(completedMessageIt);
}
}
Expand All @@ -455,7 +463,7 @@ void ChatHistory::completeMessage(DispatchedMessageId id)
if (dispatchedMessageIt == dispatchedMessageRowIdMap.end()) {
completedMessages.insert(id);
} else {
history->markAsSent(*dispatchedMessageIt);
history->markAsDelivered(*dispatchedMessageIt);
dispatchedMessageRowIdMap.erase(dispatchedMessageIt);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/model/chatlogitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include "src/core/toxfile.h"
#include "src/core/toxpk.h"
#include "src/model/message.h"
#include "src/persistence/history.h"

#include <memory>

struct ChatLogMessage
{
bool isComplete;
MessageState state;
Message message;
};

Expand Down
44 changes: 38 additions & 6 deletions src/model/sessionchatlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,19 +289,51 @@ std::vector<IChatLog::DateChatLogIdxPair> SessionChatLog::getDateIdxs(const QDat
return ret;
}

void SessionChatLog::insertMessageAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName,
ChatLogMessage message)
void SessionChatLog::insertCompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName,
const ChatLogMessage& message)
{
auto item = ChatLogItem(sender, message);

if (!senderName.isEmpty()) {
item.setDisplayName(senderName);
}

assert(message.state == MessageState::complete);

items.emplace(idx, std::move(item));
}

void SessionChatLog::insertIncompleteMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName,
const ChatLogMessage& message,
DispatchedMessageId dispatchId)
{
auto item = ChatLogItem(sender, message);

if (!senderName.isEmpty()) {
item.setDisplayName(senderName);
}

assert(message.state == MessageState::pending);

items.emplace(idx, std::move(item));
outgoingMessages.insert(dispatchId, idx);
}

void SessionChatLog::insertBrokenMessageAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName,
const ChatLogMessage& message)
{
auto item = ChatLogItem(sender, message);

if (!senderName.isEmpty()) {
item.setDisplayName(senderName);
}

assert(message.state == MessageState::broken);

items.emplace(idx, std::move(item));
}

void SessionChatLog::insertFileAtIdx(ChatLogIdx idx, ToxPk sender, QString senderName, ChatLogFile file)
void SessionChatLog::insertFileAtIdx(ChatLogIdx idx, const ToxPk& sender, const QString& senderName, const ChatLogFile& file)
{
auto item = ChatLogItem(sender, file);

Expand All @@ -321,7 +353,7 @@ void SessionChatLog::onMessageReceived(const ToxPk& sender, const Message& messa
auto messageIdx = nextIdx++;

ChatLogMessage chatLogMessage;
chatLogMessage.isComplete = true;
chatLogMessage.state = MessageState::complete;
chatLogMessage.message = message;
items.emplace(messageIdx, ChatLogItem(sender, chatLogMessage));

Expand All @@ -337,7 +369,7 @@ void SessionChatLog::onMessageSent(DispatchedMessageId id, const Message& messag
auto messageIdx = nextIdx++;

ChatLogMessage chatLogMessage;
chatLogMessage.isComplete = false;
chatLogMessage.state = MessageState::pending;
chatLogMessage.message = message;
items.emplace(messageIdx, ChatLogItem(coreIdHandler.getSelfPublicKey(), chatLogMessage));

Expand Down Expand Up @@ -367,7 +399,7 @@ void SessionChatLog::onMessageComplete(DispatchedMessageId id)
return;
}

messageIt->second.getContentAsMessage().isComplete = true;
messageIt->second.getContentAsMessage().state = MessageState::complete;

emit this->itemUpdated(messageIt->first);
}
Expand Down

0 comments on commit f7d82a4

Please sign in to comment.