1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: Remove DocumentCache singleton

Leaf of our evil singleton tree.
This commit is contained in:
Anthony Bilinski 2022-03-11 06:45:22 -08:00
parent 0afc11fafc
commit 7d40bcf43d
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
18 changed files with 107 additions and 78 deletions

View File

@ -41,7 +41,8 @@
#define TIME_COL_WIDTH 90.0 #define TIME_COL_WIDTH 90.0
ChatMessage::ChatMessage() ChatMessage::ChatMessage(DocumentCache& documentCache_)
: documentCache{documentCache_}
{ {
} }
@ -49,9 +50,10 @@ ChatMessage::~ChatMessage() = default;
ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QString& rawMessage, ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QString& rawMessage,
MessageType type, bool isMe, MessageState state, MessageType type, bool isMe, MessageState state,
const QDateTime& date, bool colorizeName) const QDateTime& date, DocumentCache& documentCache,
bool colorizeName)
{ {
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage); ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache));
QString text = rawMessage.toHtmlEscaped(); QString text = rawMessage.toHtmlEscaped();
QString senderText = sender; QString senderText = sender;
@ -108,16 +110,17 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
} }
} }
msg->addColumn(new Text(senderText, authorFont, true, sender, textType, color), msg->addColumn(new Text(documentCache, senderText, authorFont, true, sender, textType, color),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text(text, baseFont, false, ((type == ACTION) && isMe) msg->addColumn(new Text(documentCache, text, baseFont, false, ((type == ACTION) && isMe)
? QString("%1 %2").arg(sender, rawMessage) ? QString("%1 %2").arg(sender, rawMessage)
: rawMessage), : rawMessage),
ColumnFormat(1.0, ColumnFormat::VariableSize)); ColumnFormat(1.0, ColumnFormat::VariableSize));
switch (state) { switch (state) {
case MessageState::complete: case MessageState::complete:
msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont), msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(),
baseFont, documentCache),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
break; break;
case MessageState::pending: case MessageState::pending:
@ -133,9 +136,10 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
} }
ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage, ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
SystemMessageType type, const QDateTime& date) SystemMessageType type, const QDateTime& date,
DocumentCache& documentCache)
{ {
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage); ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache));
QString text = rawMessage.toHtmlEscaped(); QString text = rawMessage.toHtmlEscaped();
QString img; QString img;
@ -155,18 +159,20 @@ ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString& rawMessage,
msg->addColumn(new Image(QSize(18, 18), img), msg->addColumn(new Image(QSize(18, 18), img),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text("<b>" + text + "</b>", baseFont, false, text), msg->addColumn(new Text(documentCache, "<b>" + text + "</b>", baseFont, false, text),
ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Left)); ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Left));
msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont), msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(),
baseFont, documentCache),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
return msg; return msg;
} }
ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, CoreFile& coreFile, ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, CoreFile& coreFile,
ToxFile file, bool isMe, const QDateTime& date) ToxFile file, bool isMe, const QDateTime& date,
DocumentCache& documentCache)
{ {
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage); ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache));
QFont baseFont = Settings::getInstance().getChatMessageFont(); QFont baseFont = Settings::getInstance().getChatMessageFont();
QFont authorFont = baseFont; QFont authorFont = baseFont;
@ -174,19 +180,19 @@ ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, C
authorFont.setBold(true); authorFont.setBold(true);
} }
msg->addColumn(new Text(sender, authorFont, true), msg->addColumn(new Text(documentCache, sender, authorFont, true),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(nullptr, coreFile, file), 320, 0.6f), msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(nullptr, coreFile, file), 320, 0.6f),
ColumnFormat(1.0, ColumnFormat::VariableSize)); ColumnFormat(1.0, ColumnFormat::VariableSize));
msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont), msg->addColumn(new Timestamp(date, Settings::getInstance().getTimestampFormat(), baseFont, documentCache),
ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
return msg; return msg;
} }
ChatMessage::Ptr ChatMessage::createTypingNotification() ChatMessage::Ptr ChatMessage::createTypingNotification(DocumentCache& documentCache)
{ {
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage); ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache));
QFont baseFont = Settings::getInstance().getChatMessageFont(); QFont baseFont = Settings::getInstance().getChatMessageFont();
@ -199,7 +205,7 @@ ChatMessage::Ptr ChatMessage::createTypingNotification()
// as user will keep typing. Issue #1280 // as user will keep typing. Issue #1280
msg->addColumn(new NotificationIcon(QSize(18, 18)), msg->addColumn(new NotificationIcon(QSize(18, 18)),
ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right)); ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
msg->addColumn(new Text("[user]...", baseFont, false, ""), msg->addColumn(new Text(documentCache, "[user]...", baseFont, false, ""),
ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Left)); ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Left));
return msg; return msg;
@ -213,14 +219,14 @@ ChatMessage::Ptr ChatMessage::createTypingNotification()
* *
* @return created message * @return created message
*/ */
ChatMessage::Ptr ChatMessage::createBusyNotification() ChatMessage::Ptr ChatMessage::createBusyNotification(DocumentCache& documentCache)
{ {
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage); ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage(documentCache));
QFont baseFont = Settings::getInstance().getChatMessageFont(); QFont baseFont = Settings::getInstance().getChatMessageFont();
baseFont.setPixelSize(baseFont.pixelSize() + 2); baseFont.setPixelSize(baseFont.pixelSize() + 2);
baseFont.setBold(true); baseFont.setBold(true);
msg->addColumn(new Text(QObject::tr("Reformatting text...", "Waiting for text to be reformatted"), baseFont, false, ""), msg->addColumn(new Text(documentCache, QObject::tr("Reformatting text...", "Waiting for text to be reformatted"), baseFont, false, ""),
ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Center)); ColumnFormat(1.0, ColumnFormat::VariableSize, ColumnFormat::Center));
return msg; return msg;
@ -231,7 +237,7 @@ void ChatMessage::markAsDelivered(const QDateTime& time)
QFont baseFont = Settings::getInstance().getChatMessageFont(); QFont baseFont = Settings::getInstance().getChatMessageFont();
// remove the spinner and replace it by $time // remove the spinner and replace it by $time
replaceContent(2, new Timestamp(time, Settings::getInstance().getTimestampFormat(), baseFont)); replaceContent(2, new Timestamp(time, Settings::getInstance().getTimestampFormat(), baseFont, documentCache));
} }
void ChatMessage::markAsBroken() void ChatMessage::markAsBroken()

View File

@ -27,6 +27,7 @@
class CoreFile; class CoreFile;
class QGraphicsScene; class QGraphicsScene;
class DocumentCache;
class ChatMessage : public ChatLine class ChatMessage : public ChatLine
{ {
@ -47,21 +48,22 @@ public:
ALERT, ALERT,
}; };
ChatMessage(); explicit ChatMessage(DocumentCache& documentCache);
~ChatMessage(); ~ChatMessage();
ChatMessage(const ChatMessage&) = default; ChatMessage(const ChatMessage&) = default;
ChatMessage& operator=(const ChatMessage&) = default;
ChatMessage(ChatMessage&&) = default; ChatMessage(ChatMessage&&) = default;
static ChatMessage::Ptr createChatMessage(const QString& sender, const QString& rawMessage, static ChatMessage::Ptr createChatMessage(const QString& sender, const QString& rawMessage,
MessageType type, bool isMe, MessageState state, MessageType type, bool isMe, MessageState state,
const QDateTime& date, bool colorizeName = false); const QDateTime& date, DocumentCache&,
bool colorizeName = false);
static ChatMessage::Ptr createChatInfoMessage(const QString& rawMessage, SystemMessageType type, static ChatMessage::Ptr createChatInfoMessage(const QString& rawMessage, SystemMessageType type,
const QDateTime& date); const QDateTime& date, DocumentCache&);
static ChatMessage::Ptr createFileTransferMessage(const QString& sender, CoreFile& coreFile, static ChatMessage::Ptr createFileTransferMessage(const QString& sender, CoreFile& coreFile,
ToxFile file, bool isMe, const QDateTime& date); ToxFile file, bool isMe, const QDateTime& date,
static ChatMessage::Ptr createTypingNotification(); DocumentCache&);
static ChatMessage::Ptr createBusyNotification(); static ChatMessage::Ptr createTypingNotification(DocumentCache&);
static ChatMessage::Ptr createBusyNotification(DocumentCache&);
void markAsDelivered(const QDateTime& time); void markAsDelivered(const QDateTime& time);
void markAsBroken(); void markAsBroken();
@ -77,4 +79,5 @@ protected:
private: private:
bool action = false; bool action = false;
DocumentCache& documentCache;
}; };

View File

@ -62,16 +62,16 @@ T clamp(T x, T min, T max)
return x; return x;
} }
ChatMessage::Ptr createDateMessage(QDateTime timestamp) ChatMessage::Ptr createDateMessage(QDateTime timestamp, DocumentCache& documentCache)
{ {
const auto& s = Settings::getInstance(); const auto& s = Settings::getInstance();
const auto date = timestamp.date(); const auto date = timestamp.date();
auto dateText = date.toString(s.getDateFormat()); auto dateText = date.toString(s.getDateFormat());
return ChatMessage::createChatInfoMessage(dateText, ChatMessage::INFO, QDateTime()); return ChatMessage::createChatInfoMessage(dateText, ChatMessage::INFO, QDateTime(), documentCache);
} }
ChatMessage::Ptr createMessage(const QString& displayName, bool isSelf, bool colorizeNames, ChatMessage::Ptr createMessage(const QString& displayName, bool isSelf, bool colorizeNames,
const ChatLogMessage& chatLogMessage) const ChatLogMessage& chatLogMessage, DocumentCache& documentCache)
{ {
auto messageType = chatLogMessage.message.isAction ? ChatMessage::MessageType::ACTION auto messageType = chatLogMessage.message.isAction ? ChatMessage::MessageType::ACTION
: ChatMessage::MessageType::NORMAL; : ChatMessage::MessageType::NORMAL;
@ -88,11 +88,13 @@ ChatMessage::Ptr createMessage(const QString& displayName, bool isSelf, bool col
const auto timestamp = chatLogMessage.message.timestamp; const auto timestamp = chatLogMessage.message.timestamp;
return ChatMessage::createChatMessage(displayName, chatLogMessage.message.content, messageType, return ChatMessage::createChatMessage(displayName, chatLogMessage.message.content, messageType,
isSelf, chatLogMessage.state, timestamp, colorizeNames); isSelf, chatLogMessage.state, timestamp, documentCache,
colorizeNames);
} }
void renderMessageRaw(const QString& displayName, bool isSelf, bool colorizeNames, void renderMessageRaw(const QString& displayName, bool isSelf, bool colorizeNames,
const ChatLogMessage& chatLogMessage, ChatLine::Ptr& chatLine) const ChatLogMessage& chatLogMessage, ChatLine::Ptr& chatLine,
DocumentCache& documentCache)
{ {
// HACK: This is kind of gross, but there's not an easy way to fit this into // HACK: This is kind of gross, but there's not an easy way to fit this into
// the existing architecture. This shouldn't ever fail since we should only // the existing architecture. This shouldn't ever fail since we should only
@ -108,7 +110,7 @@ void renderMessageRaw(const QString& displayName, bool isSelf, bool colorizeName
chatMessage->markAsBroken(); chatMessage->markAsBroken();
} }
} else { } else {
chatLine = createMessage(displayName, isSelf, colorizeNames, chatLogMessage); chatLine = createMessage(displayName, isSelf, colorizeNames, chatLogMessage, documentCache);
} }
} }
@ -204,11 +206,13 @@ ChatLogIdx clampedAdd(ChatLogIdx idx, int val, IChatLog& chatLog)
} // namespace } // namespace
ChatWidget::ChatWidget(IChatLog& chatLog_, const Core& core_, QWidget* parent) ChatWidget::ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache& documentCache_,
QWidget* parent)
: QGraphicsView(parent) : QGraphicsView(parent)
, chatLog(chatLog_) , chatLog(chatLog_)
, core(core_) , core(core_)
, chatLineStorage(new ChatLineStorage()) , chatLineStorage(new ChatLineStorage())
, documentCache(documentCache_)
{ {
// Create the scene // Create the scene
busyScene = new QGraphicsScene(this); busyScene = new QGraphicsScene(this);
@ -216,7 +220,7 @@ ChatWidget::ChatWidget(IChatLog& chatLog_, const Core& core_, QWidget* parent)
scene->setItemIndexMethod(QGraphicsScene::BspTreeIndex); scene->setItemIndexMethod(QGraphicsScene::BspTreeIndex);
setScene(scene); setScene(scene);
busyNotification = ChatMessage::createBusyNotification(); busyNotification = ChatMessage::createBusyNotification(documentCache);
busyNotification->addToScene(busyScene); busyNotification->addToScene(busyScene);
busyNotification->visibilityChanged(true); busyNotification->visibilityChanged(true);
@ -539,7 +543,7 @@ void ChatWidget::insertChatlines(std::map<ChatLogIdx, ChatLine::Ptr> chatLines)
if (!chatLineStorage->contains(date)) { if (!chatLineStorage->contains(date)) {
// If there is no dateline for the given date we need to insert it // If there is no dateline for the given date we need to insert it
// above the line we'd like to insert. // above the line we'd like to insert.
auto dateLine = createDateMessage(date); auto dateLine = createDateMessage(date, documentCache);
chatLineStorage->insertDateLine(date, dateLine); chatLineStorage->insertDateLine(date, dateLine);
dateLine->addToScene(scene); dateLine->addToScene(scene);
dateLine->visibilityChanged(false); dateLine->visibilityChanged(false);
@ -1391,7 +1395,7 @@ bool ChatWidget::isActiveFileTransfer(ChatLine::Ptr l)
void ChatWidget::setTypingNotification() void ChatWidget::setTypingNotification()
{ {
typingNotification = ChatMessage::createTypingNotification(); typingNotification = ChatMessage::createTypingNotification(documentCache);
typingNotification->visibilityChanged(true); typingNotification->visibilityChanged(true);
typingNotification->setVisible(false); typingNotification->setVisible(false);
typingNotification->addToScene(scene); typingNotification->addToScene(scene);
@ -1409,7 +1413,8 @@ void ChatWidget::renderItem(const ChatLogItem& item, bool hideName, bool coloriz
case ChatLogItem::ContentType::message: { case ChatLogItem::ContentType::message: {
const auto& chatLogMessage = item.getContentAsMessage(); const auto& chatLogMessage = item.getContentAsMessage();
renderMessageRaw(item.getDisplayName(), isSelf, colorizeNames_, chatLogMessage, chatMessage); renderMessageRaw(item.getDisplayName(), isSelf, colorizeNames_, chatLogMessage,
chatMessage, documentCache);
break; break;
} }
@ -1422,7 +1427,8 @@ void ChatWidget::renderItem(const ChatLogItem& item, bool hideName, bool coloriz
const auto& systemMessage = item.getContentAsSystemMessage(); const auto& systemMessage = item.getContentAsSystemMessage();
auto chatMessageType = getChatMessageType(systemMessage); auto chatMessageType = getChatMessageType(systemMessage);
chatMessage = ChatMessage::createChatInfoMessage(systemMessage.toString(), chatMessageType, QDateTime::currentDateTime()); chatMessage = ChatMessage::createChatInfoMessage(systemMessage.toString(),
chatMessageType, QDateTime::currentDateTime(), documentCache);
// Ignore caller's decision to hide the name. We show the icon in the // Ignore caller's decision to hide the name. We show the icon in the
// slot of the sender's name so we always want it visible // slot of the sender's name so we always want it visible
hideName = false; hideName = false;
@ -1441,7 +1447,8 @@ void ChatWidget::renderFile(QString displayName, ToxFile file, bool isSelf, QDat
if (!chatMessage) { if (!chatMessage) {
CoreFile* coreFile = core.getCoreFile(); CoreFile* coreFile = core.getCoreFile();
assert(coreFile); assert(coreFile);
chatMessage = ChatMessage::createFileTransferMessage(displayName, *coreFile, file, isSelf, timestamp); chatMessage = ChatMessage::createFileTransferMessage(displayName, *coreFile,
file, isSelf, timestamp, documentCache);
} else { } else {
auto proxy = static_cast<ChatLineContentProxy*>(chatMessage->getContent(1)); auto proxy = static_cast<ChatLineContentProxy*>(chatMessage->getContent(1));
assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType); assert(proxy->getWidgetType() == ChatLineContentProxy::FileTransferWidgetType);

View File

@ -42,7 +42,8 @@ class ChatWidget : public QGraphicsView
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ChatWidget(IChatLog& chatLog_, const Core& core_, QWidget* parent = nullptr); ChatWidget(IChatLog& chatLog_, const Core& core_, DocumentCache&,
QWidget* parent = nullptr);
virtual ~ChatWidget(); virtual ~ChatWidget();
void insertChatlines(std::map<ChatLogIdx, ChatLine::Ptr> chatLines); void insertChatlines(std::map<ChatLogIdx, ChatLine::Ptr> chatLines);
@ -214,4 +215,5 @@ private:
std::unique_ptr<ChatLineStorage> chatLineStorage; std::unique_ptr<ChatLineStorage> chatLineStorage;
std::vector<std::function<void(void)>> renderCompletionFns; std::vector<std::function<void(void)>> renderCompletionFns;
DocumentCache& documentCache;
}; };

View File

@ -31,14 +31,16 @@
#include <QTextBlock> #include <QTextBlock>
#include <QTextFragment> #include <QTextFragment>
Text::Text(const QString& txt, const QFont& font, bool enableElide, const QString& rwText, Text::Text(DocumentCache& documentCache_, const QString& txt, const QFont& font,
const TextType& type, const QColor& custom) bool enableElide, const QString& rwText, const TextType& type,
const QColor& custom)
: rawText(rwText) : rawText(rwText)
, elide(enableElide) , elide(enableElide)
, defFont(font) , defFont(font)
, defStyleSheet(Style::getStylesheet(QStringLiteral("chatArea/innerStyle.css"), font)) , defStyleSheet(Style::getStylesheet(QStringLiteral("chatArea/innerStyle.css"), font))
, textType(type) , textType(type)
, customColor(custom) , customColor(custom)
, documentCache(documentCache_)
{ {
color = textColor(); color = textColor();
setText(txt); setText(txt);
@ -49,7 +51,7 @@ Text::Text(const QString& txt, const QFont& font, bool enableElide, const QStrin
Text::~Text() Text::~Text()
{ {
if (doc) if (doc)
DocumentCache::getInstance().push(doc); documentCache.push(doc);
} }
void Text::setText(const QString& txt) void Text::setText(const QString& txt)
@ -315,7 +317,7 @@ QString Text::getLinkAt(QPointF scenePos) const
void Text::regenerate() void Text::regenerate()
{ {
if (!doc) { if (!doc) {
doc = DocumentCache::getInstance().pop(); doc = documentCache.pop();
dirty = true; dirty = true;
} }
@ -362,7 +364,7 @@ void Text::regenerate()
void Text::freeResources() void Text::freeResources()
{ {
DocumentCache::getInstance().push(doc); documentCache.push(doc);
doc = nullptr; doc = nullptr;
} }

View File

@ -25,6 +25,7 @@
#include <QFont> #include <QFont>
class QTextDocument; class QTextDocument;
class DocumentCache;
class Text : public ChatLineContent class Text : public ChatLineContent
{ {
@ -38,8 +39,10 @@ public:
CUSTOM CUSTOM
}; };
Text(const QString& txt = "", const QFont& font = QFont(), bool enableElide = false, Text(DocumentCache&, const QString& txt = "", const QFont& font = QFont(),
const QString& rawText = QString(), const TextType& type = NORMAL, const QColor& custom = Style::getColor(Style::MainText)); bool enableElide = false, const QString& rawText = QString(),
const TextType& type = NORMAL,
const QColor& custom = Style::getColor(Style::MainText));
virtual ~Text(); virtual ~Text();
void setText(const QString& txt); void setText(const QString& txt);
@ -109,4 +112,5 @@ private:
TextType textType; TextType textType;
QColor color; QColor color;
QColor customColor; QColor customColor;
DocumentCache& documentCache;
}; };

View File

@ -19,8 +19,8 @@
#include "timestamp.h" #include "timestamp.h"
Timestamp::Timestamp(const QDateTime& time_, const QString& format, const QFont& font) Timestamp::Timestamp(const QDateTime& time_, const QString& format, const QFont& font, DocumentCache& documentCache_)
: Text(time_.toString(format), font, false, time_.toString(format)) : Text(documentCache_, time_.toString(format), font, false, time_.toString(format))
{ {
time = time_; time = time_;
} }

View File

@ -24,12 +24,14 @@
#include <QTextDocument> #include <QTextDocument>
class QTextDocument; class QTextDocument;
class DocumentCache;
class Timestamp : public Text class Timestamp : public Text
{ {
Q_OBJECT Q_OBJECT
public: public:
Timestamp(const QDateTime& time_, const QString& format, const QFont& font); Timestamp(const QDateTime& time_, const QString& format, const QFont& font,
DocumentCache&);
QDateTime getTime(); QDateTime getTime();
protected: protected:

View File

@ -41,12 +41,3 @@ void DocumentCache::push(QTextDocument* doc)
documents.push(doc); documents.push(doc);
} }
} }
/**
* @brief Returns the singleton instance.
*/
DocumentCache& DocumentCache::getInstance()
{
static DocumentCache instance;
return instance;
}

View File

@ -26,17 +26,13 @@ class QTextDocument;
class DocumentCache class DocumentCache
{ {
public: public:
static DocumentCache& getInstance();
QTextDocument* pop();
void push(QTextDocument* doc);
private:
DocumentCache() = default; DocumentCache() = default;
~DocumentCache(); ~DocumentCache();
DocumentCache(DocumentCache&) = delete; DocumentCache(DocumentCache&) = delete;
DocumentCache& operator=(const DocumentCache&) = delete; DocumentCache& operator=(const DocumentCache&) = delete;
QTextDocument* pop();
void push(QTextDocument* doc);
private: private:
QStack<QTextDocument*> documents; QStack<QTextDocument*> documents;
}; };

View File

@ -106,8 +106,10 @@ QString secondsToDHMS(quint32 duration)
} }
} // namespace } // namespace
ChatForm::ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_) ChatForm::ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_,
: GenericChatForm(profile.getCore(), chatFriend, chatLog_, messageDispatcher_) IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache_)
: GenericChatForm(profile.getCore(), chatFriend, chatLog_, messageDispatcher_,
documentCache_)
, core{profile.getCore()} , core{profile.getCore()}
, f(chatFriend) , f(chatFriend)
, isTyping{false} , isTyping{false}

View File

@ -42,12 +42,14 @@ class QPixmap;
class QHideEvent; class QHideEvent;
class QMoveEvent; class QMoveEvent;
class ImagePreviewButton; class ImagePreviewButton;
class DocumentCache;
class ChatForm : public GenericChatForm class ChatForm : public GenericChatForm
{ {
Q_OBJECT Q_OBJECT
public: public:
ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_); ChatForm(Profile& profile, Friend* chatFriend, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, DocumentCache&);
~ChatForm() override; ~ChatForm() override;
void setStatusMessage(const QString& newMessage); void setStatusMessage(const QString& newMessage);

View File

@ -137,7 +137,8 @@ QPushButton* createButton(const QString& name, T* self, Fun onClickSlot)
} // namespace } // namespace
GenericChatForm::GenericChatForm(const Core& core_, const Contact* contact, IChatLog& chatLog_, GenericChatForm::GenericChatForm(const Core& core_, const Contact* contact, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, QWidget* parent_) IMessageDispatcher& messageDispatcher_, DocumentCache& documentCache,
QWidget* parent_)
: QWidget(parent_, Qt::Window) : QWidget(parent_, Qt::Window)
, core{core_} , core{core_}
, audioInputFlag(false) , audioInputFlag(false)
@ -149,7 +150,7 @@ GenericChatForm::GenericChatForm(const Core& core_, const Contact* contact, ICha
headWidget = new ChatFormHeader(); headWidget = new ChatFormHeader();
searchForm = new SearchForm(); searchForm = new SearchForm();
dateInfo = new QLabel(this); dateInfo = new QLabel(this);
chatWidget = new ChatWidget(chatLog_, core, this); chatWidget = new ChatWidget(chatLog_, core, documentCache, this);
searchForm->hide(); searchForm->hide();
dateInfo->setAlignment(Qt::AlignHCenter); dateInfo->setAlignment(Qt::AlignHCenter);
dateInfo->setVisible(false); dateInfo->setVisible(false);

View File

@ -53,6 +53,7 @@ class QVBoxLayout;
class IMessageDispatcher; class IMessageDispatcher;
struct Message; struct Message;
class DocumentCache;
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
@ -69,7 +70,8 @@ class GenericChatForm : public QWidget
Q_OBJECT Q_OBJECT
public: public:
GenericChatForm(const Core& core_, const Contact* contact, IChatLog& chatLog_, GenericChatForm(const Core& core_, const Contact* contact, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, QWidget* parent_ = nullptr); IMessageDispatcher& messageDispatcher_, DocumentCache&,
QWidget* parent_ = nullptr);
~GenericChatForm() override; ~GenericChatForm() override;
void setName(const QString& newName); void setName(const QString& newName);

View File

@ -82,8 +82,9 @@ QString editName(const QString& name)
* @brief Timeout = peer stopped sending audio. * @brief Timeout = peer stopped sending audio.
*/ */
GroupChatForm::GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, IGroupSettings& settings_) GroupChatForm::GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_,
: GenericChatForm(core_, chatGroup, chatLog_, messageDispatcher_) IMessageDispatcher& messageDispatcher_, IGroupSettings& settings_, DocumentCache& documentCache_)
: GenericChatForm(core_, chatGroup, chatLog_, messageDispatcher_, documentCache_)
, core{core_} , core{core_}
, group(chatGroup) , group(chatGroup)
, inCall(false) , inCall(false)

View File

@ -35,12 +35,15 @@ class IMessageDispatcher;
struct Message; struct Message;
class Settings; class Settings;
class IGroupSettings; class IGroupSettings;
class DocumentCache;
class GroupChatForm : public GenericChatForm class GroupChatForm : public GenericChatForm
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_, IMessageDispatcher& messageDispatcher_, IGroupSettings& settings_); explicit GroupChatForm(Core& core_, Group* chatGroup, IChatLog& chatLog_,
IMessageDispatcher& messageDispatcher_, IGroupSettings& settings_,
DocumentCache&);
~GroupChatForm(); ~GroupChatForm();
void peerAudioPlaying(ToxPk peerPk); void peerAudioPlaying(ToxPk peerPk);

View File

@ -49,6 +49,7 @@
#include "splitterrestorer.h" #include "splitterrestorer.h"
#include "form/groupchatform.h" #include "form/groupchatform.h"
#include "src/chatlog/content/filetransferwidget.h" #include "src/chatlog/content/filetransferwidget.h"
#include "src/chatlog/documentcache.h"
#include "src/core/core.h" #include "src/core/core.h"
#include "src/core/coreav.h" #include "src/core/coreav.h"
#include "src/core/corefile.h" #include "src/core/corefile.h"
@ -147,6 +148,7 @@ Widget::Widget(Profile &profile_, IAudioControl& audio_, QWidget* parent)
, eventIcon(false) , eventIcon(false)
, audio(audio_) , audio(audio_)
, settings(Settings::getInstance()) , settings(Settings::getInstance())
, documentCache(new DocumentCache())
{ {
installEventFilter(this); installEventFilter(this);
QString locale = settings.getTranslation(); QString locale = settings.getTranslation();
@ -1185,7 +1187,8 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
auto chatHistory = auto chatHistory =
std::make_shared<ChatHistory>(*newfriend, history, *core, settings, std::make_shared<ChatHistory>(*newfriend, history, *core, settings,
*friendMessageDispatcher); *friendMessageDispatcher);
auto friendForm = new ChatForm(profile, newfriend, *chatHistory, *friendMessageDispatcher); auto friendForm = new ChatForm(profile, newfriend, *chatHistory,
*friendMessageDispatcher, *documentCache);
connect(friendForm, &ChatForm::updateFriendActivity, this, &Widget::updateFriendActivity); connect(friendForm, &ChatForm::updateFriendActivity, this, &Widget::updateFriendActivity);
friendMessageDispatchers[friendPk] = friendMessageDispatcher; friendMessageDispatchers[friendPk] = friendMessageDispatcher;
@ -2168,7 +2171,7 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId)
connect(messageDispatcher.get(), &IMessageDispatcher::messageReceived, notifyReceivedCallback); connect(messageDispatcher.get(), &IMessageDispatcher::messageReceived, notifyReceivedCallback);
groupAlertConnections.insert(groupId, notifyReceivedConnection); groupAlertConnections.insert(groupId, notifyReceivedConnection);
auto form = new GroupChatForm(*core, newgroup, *groupChatLog, *messageDispatcher, settings); auto form = new GroupChatForm(*core, newgroup, *groupChatLog, *messageDispatcher, settings, *documentCache);
connect(&settings, &Settings::nameColorsChanged, form, &GenericChatForm::setColorizedNames); connect(&settings, &Settings::nameColorsChanged, form, &GenericChatForm::setColorizedNames);
form->setColorizedNames(settings.getEnableGroupChatsColor()); form->setColorizedNames(settings.getEnableGroupChatsColor());
groupMessageDispatchers[groupId] = messageDispatcher; groupMessageDispatchers[groupId] = messageDispatcher;

View File

@ -83,6 +83,7 @@ class UpdateCheck;
class Settings; class Settings;
class IChatLog; class IChatLog;
class ChatHistory; class ChatHistory;
class DocumentCache;
class Widget final : public QMainWindow class Widget final : public QMainWindow
{ {
@ -381,6 +382,7 @@ private:
QAction* nextConversationAction; QAction* nextConversationAction;
QAction* previousConversationAction; QAction* previousConversationAction;
#endif #endif
std::unique_ptr<DocumentCache> documentCache;
}; };
bool toxActivateEventHandler(const QByteArray& data); bool toxActivateEventHandler(const QByteArray& data);