2014-11-16 19:58:43 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
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 COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
2014-11-12 21:11:25 +08:00
|
|
|
#include "chatmessage.h"
|
2015-01-03 22:03:33 +08:00
|
|
|
#include "chatlinecontentproxy.h"
|
2014-11-12 23:45:24 +08:00
|
|
|
#include "content/text.h"
|
|
|
|
#include "content/spinner.h"
|
2015-01-03 22:03:33 +08:00
|
|
|
#include "content/filetransferwidget.h"
|
|
|
|
#include "content/image.h"
|
2014-11-12 21:11:25 +08:00
|
|
|
|
2015-01-03 03:07:45 +08:00
|
|
|
#include "src/misc/settings.h"
|
2015-01-03 22:03:33 +08:00
|
|
|
#include "src/misc/smileypack.h"
|
|
|
|
#include "src/misc/style.h"
|
2015-01-03 03:07:45 +08:00
|
|
|
|
2015-01-03 22:03:33 +08:00
|
|
|
#define NAME_COL_WIDTH 75.0
|
|
|
|
#define TIME_COL_WIDTH 85.0
|
2014-11-12 23:45:24 +08:00
|
|
|
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatMessage::ChatMessage()
|
2014-11-12 23:45:24 +08:00
|
|
|
{
|
2014-11-16 19:58:43 +08:00
|
|
|
|
2014-11-12 23:45:24 +08:00
|
|
|
}
|
|
|
|
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatMessage::Ptr ChatMessage::createChatMessage(const QString &sender, const QString &rawMessage, bool isAction, bool alert, bool isMe, const QDateTime &date)
|
2015-01-03 22:03:33 +08:00
|
|
|
{
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);
|
2015-01-03 22:03:33 +08:00
|
|
|
|
2015-01-04 02:05:38 +08:00
|
|
|
QString text = toHtmlChars(rawMessage);
|
|
|
|
|
|
|
|
if(Settings::getInstance().getUseEmoticons())
|
|
|
|
text = SmileyPack::getInstance().smileyfied(text);
|
|
|
|
|
|
|
|
text = detectQuotes(detectAnchors(text));
|
2015-01-03 22:03:33 +08:00
|
|
|
|
|
|
|
if(isAction)
|
|
|
|
{
|
|
|
|
text = QString("<div class=action>%1 %2</div>").arg(sender, text);
|
|
|
|
msg->setAsAction();
|
|
|
|
}
|
|
|
|
else if(alert)
|
2015-01-04 02:06:10 +08:00
|
|
|
{
|
2015-01-03 22:03:33 +08:00
|
|
|
text = "<div class=alert>" + text + "</div>";
|
2015-01-04 02:06:10 +08:00
|
|
|
}
|
2015-01-03 22:03:33 +08:00
|
|
|
|
|
|
|
msg->addColumn(new Text(isAction ? "*" : sender, isMe ? Style::getFont(Style::BigBold) : Style::getFont(Style::Big), true), ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
|
2015-01-05 01:21:35 +08:00
|
|
|
msg->addColumn(new Text(text, Style::getFont(Style::Big), false, rawMessage), ColumnFormat(1.0, ColumnFormat::VariableSize));
|
2015-01-03 22:03:33 +08:00
|
|
|
msg->addColumn(new Spinner(QSizeF(16, 16)), ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
|
|
|
|
|
|
|
|
if(!date.isNull())
|
|
|
|
msg->markAsSent(date);
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatMessage::Ptr ChatMessage::createChatInfoMessage(const QString &rawMessage, SystemMessageType type, const QDateTime &date)
|
2015-01-03 22:03:33 +08:00
|
|
|
{
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);
|
2015-01-03 22:03:33 +08:00
|
|
|
|
|
|
|
msg->addColumn(new Image(QSizeF(16, 16), ":/ui/chatArea/info.png"), ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
|
2015-01-05 01:21:35 +08:00
|
|
|
msg->addColumn(new Text(rawMessage, Style::getFont(Style::Big), false, rawMessage), ColumnFormat(1.0, ColumnFormat::VariableSize));
|
2015-01-03 22:03:33 +08:00
|
|
|
msg->addColumn(new Text(date.toString(Settings::getInstance().getTimestampFormat()), Style::getFont(Style::Big)), ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
|
|
|
|
|
2015-01-04 20:29:14 +08:00
|
|
|
Q_UNUSED(type)
|
|
|
|
|
2015-01-03 22:03:33 +08:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatMessage::Ptr ChatMessage::createFileTransferMessage(const QString& sender, ToxFile file, bool isMe, const QDateTime& date)
|
2015-01-03 22:03:33 +08:00
|
|
|
{
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatMessage::Ptr msg = ChatMessage::Ptr(new ChatMessage);
|
2015-01-03 22:03:33 +08:00
|
|
|
|
|
|
|
msg->addColumn(new Text(sender, isMe ? Style::getFont(Style::BigBold) : Style::getFont(Style::Big), true), ColumnFormat(NAME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
|
|
|
|
msg->addColumn(new ChatLineContentProxy(new FileTransferWidget(0, file), 380, 0.6f), ColumnFormat(1.0, ColumnFormat::VariableSize));
|
|
|
|
msg->addColumn(new Text(date.toString(Settings::getInstance().getTimestampFormat()), Style::getFont(Style::Big)), ColumnFormat(TIME_COL_WIDTH, ColumnFormat::FixedSize, ColumnFormat::Right));
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2014-11-12 23:45:24 +08:00
|
|
|
void ChatMessage::markAsSent(const QDateTime &time)
|
2014-11-12 21:11:25 +08:00
|
|
|
{
|
2014-11-12 23:45:24 +08:00
|
|
|
// remove the spinner and replace it by $time
|
2015-01-03 03:07:45 +08:00
|
|
|
replaceContent(2, new Text(time.toString(Settings::getInstance().getTimestampFormat())));
|
2014-11-12 21:11:25 +08:00
|
|
|
}
|
2014-11-14 01:27:32 +08:00
|
|
|
|
|
|
|
QString ChatMessage::toString() const
|
|
|
|
{
|
2015-01-05 01:21:35 +08:00
|
|
|
ChatLineContent* c = getContent(1);
|
|
|
|
if(c)
|
|
|
|
return c->getText();
|
|
|
|
|
|
|
|
return QString();
|
2014-11-14 01:27:32 +08:00
|
|
|
}
|
2014-12-14 04:11:03 +08:00
|
|
|
|
|
|
|
bool ChatMessage::isAction() const
|
|
|
|
{
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessage::setAsAction()
|
|
|
|
{
|
|
|
|
action = true;
|
|
|
|
}
|
2015-01-03 22:03:33 +08:00
|
|
|
|
2015-01-05 01:21:35 +08:00
|
|
|
void ChatMessage::hideSender()
|
|
|
|
{
|
|
|
|
ChatLineContent* c = getContent(0);
|
|
|
|
if(c)
|
|
|
|
c->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatMessage::hideDate()
|
|
|
|
{
|
|
|
|
ChatLineContent* c = getContent(2);
|
|
|
|
if(c)
|
|
|
|
c->hide();
|
|
|
|
}
|
|
|
|
|
2015-01-03 22:03:33 +08:00
|
|
|
QString ChatMessage::detectAnchors(const QString &str)
|
|
|
|
{
|
|
|
|
QString out = str;
|
|
|
|
|
|
|
|
// detect urls
|
|
|
|
QRegExp exp("(?:\\b)(www\\.|http[s]?:\\/\\/|ftp:\\/\\/|tox:\\/\\/|tox:)\\S+");
|
|
|
|
int offset = 0;
|
|
|
|
while ((offset = exp.indexIn(out, offset)) != -1)
|
|
|
|
{
|
|
|
|
QString url = exp.cap();
|
|
|
|
|
|
|
|
// If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:"
|
|
|
|
if (url == "tox:\"")
|
|
|
|
{
|
|
|
|
offset += url.length();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add scheme if not specified
|
|
|
|
if (exp.cap(1) == "www.")
|
|
|
|
url.prepend("http://");
|
|
|
|
|
|
|
|
QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
|
|
|
|
out.replace(offset, exp.cap().length(), htmledUrl);
|
|
|
|
|
|
|
|
offset += htmledUrl.length();
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChatMessage::detectQuotes(const QString& str)
|
|
|
|
{
|
|
|
|
// detect text quotes
|
|
|
|
QStringList messageLines = str.split("\n");
|
|
|
|
QString quotedText;
|
|
|
|
for (int i=0;i<messageLines.size();++i)
|
|
|
|
{
|
|
|
|
if (QRegExp("^[ ]*>.*").exactMatch(messageLines[i]))
|
|
|
|
quotedText += "<span class=quote>" + messageLines[i] + "</span>";
|
|
|
|
else
|
|
|
|
quotedText += messageLines[i];
|
|
|
|
|
|
|
|
if (i < messageLines.size() - 1)
|
|
|
|
quotedText += "<br/>";
|
|
|
|
}
|
|
|
|
|
|
|
|
return quotedText;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ChatMessage::toHtmlChars(const QString &str)
|
|
|
|
{
|
|
|
|
static QList<QPair<QString, QString>> replaceList = {{"&","&"}, {">",">"}, {"<","<"}};
|
|
|
|
QString res = str;
|
|
|
|
|
|
|
|
for (auto &it : replaceList)
|
|
|
|
res = res.replace(it.first,it.second);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|