General cleanups/reformats

- Clean up imports
- Comment EmojiData
- Reorder TwitchAccount constructor
- Fix typo in TwitchChannel
- Add emoji parsing test code at the bottom of EmoteManager
This commit is contained in:
Rasmus Karlsson 2018-03-31 13:44:15 +02:00
parent 3cdaeb071a
commit be66338fe2
17 changed files with 118 additions and 64 deletions

View file

@ -4,13 +4,14 @@
namespace chatterino {
static const QString ANONYMOUS_USERNAME_LABEL(" - anonymous - ");
static const char *ANONYMOUS_USERNAME_LABEL = " - anonymous - ";
namespace providers {
namespace twitch {
static const QString ANONYMOUS_USERNAME("justinfan64537");
}
static const char *ANONYMOUS_USERNAME = "justinfan64537";
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -1,16 +1,11 @@
#pragma once
#include "messages/image.hpp"
#include "util/concurrentmap.hpp"
#include <QObject>
#include <QString>
#include <unordered_map>
namespace chatterino {
struct EmojiData {
// actual byte-representation of the emoji (i.e. \154075\156150 which is :male:)
QString value;
// what's used in the emoji-one url

View file

@ -1,15 +1,15 @@
#include "providers/twitch/twitchaccount.hpp"
#include "const.hpp"
#include "util/urlfetch.hpp"
namespace chatterino {
namespace providers {
namespace twitch {
TwitchAccount::TwitchAccount(const QString &_username, const QString &_oauthToken,
const QString &_oauthClient)
: userName(_username)
, oauthClient(_oauthClient)
: oauthClient(_oauthClient)
, oauthToken(_oauthToken)
, userName(_username)
, _isAnon(_username == ANONYMOUS_USERNAME)
{
}
@ -65,6 +65,7 @@ bool TwitchAccount::isAnon() const
{
return this->_isAnon;
}
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -6,6 +6,7 @@
namespace chatterino {
namespace providers {
namespace twitch {
class TwitchAccount
{
public:
@ -37,6 +38,7 @@ private:
QString userName;
const bool _isAnon;
};
} // namespace twitch
} // namespace providers
} // namespace chatterino

View file

@ -25,7 +25,7 @@ TwitchChannel::TwitchChannel(const QString &channelName, Communi::IrcConnection
, channelURL("https://twitch.tv/" + name)
, popoutPlayerURL("https://player.twitch.tv/?channel=" + name)
, mod(false)
, readConnecetion(_readConnection)
, readConnection(_readConnection)
{
debug::Log("[TwitchChannel:{}] Opened", this->name);
@ -285,7 +285,7 @@ void TwitchChannel::fetchRecentMessages()
auto channel = dynamic_cast<TwitchChannel *>(shared.get());
assert(channel != nullptr);
static auto readConnection = channel->readConnecetion;
static auto readConnection = channel->readConnection;
QJsonArray msgArray = obj.value("messages").toArray();
if (msgArray.empty()) {

View file

@ -95,7 +95,7 @@ private:
QByteArray messageSuffix;
QString lastSentMessage;
Communi::IrcConnection *readConnecetion;
Communi::IrcConnection *readConnection;
friend class TwitchServer;

View file

@ -26,7 +26,7 @@ namespace singletons {
namespace {
static QString GetTwitchEmoteLink(long id, const QString &emoteScale)
QString GetTwitchEmoteLink(long id, const QString &emoteScale)
{
QString value = TWITCH_EMOTE_TEMPLATE;
@ -35,14 +35,14 @@ static QString GetTwitchEmoteLink(long id, const QString &emoteScale)
return value.replace("{id}", QString::number(id)).replace("{scale}", emoteScale);
}
static QString GetBTTVEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale)
QString GetBTTVEmoteLink(QString urlTemplate, const QString &id, const QString &emoteScale)
{
urlTemplate.detach();
return urlTemplate.replace("{{id}}", id).replace("{{image}}", emoteScale);
}
static QString GetFFZEmoteLink(const QJsonObject &urls, const QString &emoteScale)
QString GetFFZEmoteLink(const QJsonObject &urls, const QString &emoteScale)
{
auto emote = urls.value(emoteScale);
if (emote.isUndefined()) {
@ -54,8 +54,7 @@ static QString GetFFZEmoteLink(const QJsonObject &urls, const QString &emoteScal
return "http:" + emote.toString();
}
static void FillInFFZEmoteData(const QJsonObject &urls, const QString &code,
util::EmoteData &emoteData)
void FillInFFZEmoteData(const QJsonObject &urls, const QString &code, util::EmoteData &emoteData)
{
QString url1x = GetFFZEmoteLink(urls, "1");
QString url2x = GetFFZEmoteLink(urls, "2");
@ -366,9 +365,8 @@ void EmoteManager::parseEmojis(std::vector<std::tuple<util::EmoteData, QString>>
if (charactersFromLastParsedEmoji > 0) {
// Add characters inbetween emojis
parsedWords.push_back(std::tuple<util::EmoteData, QString>(
util::EmoteData(),
text.mid(lastParsedEmojiEndIndex, charactersFromLastParsedEmoji)));
parsedWords.emplace_back(util::EmoteData(), text.mid(lastParsedEmojiEndIndex,
charactersFromLastParsedEmoji));
}
QString url = "https://cdnjs.cloudflare.com/ajax/libs/"
@ -376,7 +374,7 @@ void EmoteManager::parseEmojis(std::vector<std::tuple<util::EmoteData, QString>>
matchedEmoji.code + ".png";
// Create or fetch cached emoji image
auto emojiImage = this->emojis.getOrAdd(matchedEmoji.code, [this, &url] {
auto emojiImage = this->emojis.getOrAdd(matchedEmoji.code, [&url] {
return util::EmoteData(new Image(url, 0.35, "?????????", "???????????????")); //
});
@ -390,8 +388,7 @@ void EmoteManager::parseEmojis(std::vector<std::tuple<util::EmoteData, QString>>
if (lastParsedEmojiEndIndex < text.length()) {
// Add remaining characters
parsedWords.push_back(std::tuple<util::EmoteData, QString>(
util::EmoteData(), text.mid(lastParsedEmojiEndIndex)));
parsedWords.emplace_back(util::EmoteData(), text.mid(lastParsedEmojiEndIndex));
}
}
@ -546,7 +543,7 @@ util::EmoteData EmoteManager::getTwitchEmoteById(long id, const QString &emoteNa
QString _emoteName = emoteName;
_emoteName.replace("<", "&lt;");
return _twitchEmoteFromCache.getOrAdd(id, [this, &emoteName, &_emoteName, &id] {
return _twitchEmoteFromCache.getOrAdd(id, [&emoteName, &_emoteName, &id] {
util::EmoteData newEmoteData;
newEmoteData.image1x = new Image(GetTwitchEmoteLink(id, "1.0"), 1, emoteName,
_emoteName + "<br/>Twitch Emote 1x");
@ -593,3 +590,61 @@ boost::signals2::signal<void()> &EmoteManager::getGifUpdateSignal()
} // namespace singletons
} // namespace chatterino
#if 0
namespace chatterino {
void EmojiTest()
{
auto &emoteManager = singletons::EmoteManager::getInstance();
emoteManager.loadEmojis();
{
std::vector<std::tuple<util::EmoteData, QString>> dummy;
// couple_mm 1f468-2764-1f468
// "\154075\156150❤\154075\156150"
// [0] 55357 0xd83d QChar
// [1] 56424 0xdc68 QChar
// [2] '❤' 10084 0x2764 QChar
// [3] 55357 0xd83d QChar
// [4] 56424 0xdc68 QChar
QString text = "👨❤👨";
emoteManager.parseEmojis(dummy, text);
assert(dummy.size() == 1);
}
{
std::vector<std::tuple<util::EmoteData, QString>> dummy;
// "✍\154074\157777"
// [0] '✍' 9997 0x270d QChar
// [1] 55356 0xd83c QChar
// [2] 57343 0xdfff QChar
QString text = "✍🏿";
emoteManager.parseEmojis(dummy, text);
assert(dummy.size() == 1);
assert(std::get<0>(dummy[0]).isValid());
}
{
std::vector<std::tuple<util::EmoteData, QString>> dummy;
QString text = "";
emoteManager.parseEmojis(dummy, text);
assert(dummy.size() == 1);
assert(std::get<0>(dummy[0]).isValid());
}
}
} // namespace chatterino
#endif

View file

@ -15,9 +15,7 @@ template <typename TKey, typename TValue>
class ConcurrentMap
{
public:
ConcurrentMap()
{
}
ConcurrentMap() = default;
bool tryGet(const TKey &name, TValue &value) const
{
@ -84,5 +82,6 @@ private:
mutable QMutex mutex;
QMap<TKey, TValue> data;
};
} // namespace util
} // namespace chatterino
}

View file

@ -1,6 +1,8 @@
#pragma once
#include <math.h>
#include <QPointF>
#include <cmath>
namespace chatterino {
namespace util {

View file

@ -9,9 +9,7 @@ namespace chatterino {
namespace util {
struct EmoteData {
EmoteData()
{
}
EmoteData() = default;
EmoteData(messages::Image *_image)
: image1x(_image)
@ -29,7 +27,7 @@ struct EmoteData {
messages::Image *image3x = nullptr;
};
typedef ConcurrentMap<QString, EmoteData> EmoteMap;
using EmoteMap = ConcurrentMap<QString, EmoteData>;
} // namespace util
} // namespace chatterino

View file

@ -4,7 +4,8 @@
namespace chatterino {
namespace util {
QString ParseTagString(const QString &input)
inline QString ParseTagString(const QString &input)
{
QString output = input;
output.detach();
@ -49,9 +50,10 @@ QString ParseTagString(const QString &input)
if (changed) {
return output.replace("\0", "");
} else {
return output;
}
return output;
}
}
} // namespace util
} // namespace chatterino

View file

@ -51,11 +51,11 @@ public:
worker->moveToThread(&NetworkManager::workerThread);
QObject::connect(
&requester, &NetworkRequester::requestUrl, worker,
[ worker, onFinished = std::move(onFinished), request = std::move(request) ]() {
[worker, onFinished = std::move(onFinished), request = std::move(request)]() {
QNetworkReply *reply = NetworkManager::NaM.get(request);
QObject::connect(reply, &QNetworkReply::finished,
[ worker, reply, onFinished = std::move(onFinished) ]() {
[worker, reply, onFinished = std::move(onFinished)]() {
onFinished(reply);
delete worker;
});
@ -111,11 +111,11 @@ public:
worker->moveToThread(&NetworkManager::workerThread);
QObject::connect(
&requester, &NetworkRequester::requestUrl, worker,
[ worker, data, onFinished = std::move(onFinished), request = std::move(request) ]() {
[worker, data, onFinished = std::move(onFinished), request = std::move(request)]() {
QNetworkReply *reply = NetworkManager::NaM.put(request, *data);
QObject::connect(reply, &QNetworkReply::finished,
[ worker, reply, onFinished = std::move(onFinished) ]() {
[worker, reply, onFinished = std::move(onFinished)]() {
onFinished(reply);
delete worker;
});
@ -133,11 +133,11 @@ public:
worker->moveToThread(&NetworkManager::workerThread);
QObject::connect(
&requester, &NetworkRequester::requestUrl, worker,
[ onFinished = std::move(onFinished), request = std::move(request), worker ]() {
[onFinished = std::move(onFinished), request = std::move(request), worker]() {
QNetworkReply *reply = NetworkManager::NaM.put(request, "");
QObject::connect(reply, &QNetworkReply::finished,
[ onFinished = std::move(onFinished), reply, worker ]() {
[onFinished = std::move(onFinished), reply, worker]() {
onFinished(reply);
delete worker;
});
@ -155,11 +155,11 @@ public:
worker->moveToThread(&NetworkManager::workerThread);
QObject::connect(
&requester, &NetworkRequester::requestUrl, worker,
[ onFinished = std::move(onFinished), request = std::move(request), worker ]() {
[onFinished = std::move(onFinished), request = std::move(request), worker]() {
QNetworkReply *reply = NetworkManager::NaM.deleteResource(request);
QObject::connect(reply, &QNetworkReply::finished,
[ onFinished = std::move(onFinished), reply, worker ]() {
[onFinished = std::move(onFinished), reply, worker]() {
onFinished(reply);
delete worker;
});

View file

@ -33,9 +33,9 @@ float BaseWidget::getScale() const
if (baseWidget == nullptr) {
return 1.f;
} else {
return baseWidget->scale;
}
return baseWidget->scale;
}
QSize BaseWidget::getScaleIndependantSize() const
@ -98,7 +98,7 @@ void BaseWidget::childEvent(QChildEvent *event)
if (event->added()) {
BaseWidget *widget = dynamic_cast<BaseWidget *>(event->child());
if (widget) {
if (widget != nullptr) {
this->widgets.push_back(widget);
}
} else if (event->removed()) {

View file

@ -2,6 +2,7 @@
namespace chatterino {
namespace widgets {
TitleBarButton::TitleBarButton()
: RippleEffectButton(nullptr)
{
@ -114,5 +115,6 @@ void TitleBarButton::paintEvent(QPaintEvent *)
this->fancyPaint(painter);
}
} // namespace widgets
} // namespace chatterino

View file

@ -4,6 +4,7 @@
namespace chatterino {
namespace widgets {
class TitleBarButton : public RippleEffectButton
{
public:
@ -15,10 +16,11 @@ public:
void setButtonStyle(Style style);
protected:
virtual void paintEvent(QPaintEvent *) override;
void paintEvent(QPaintEvent *) override;
private:
Style style;
};
} // namespace widgets
} // namespace chatterino

View file

@ -18,17 +18,12 @@
#include <QApplication>
#include <QClipboard>
#include <QDebug>
#include <QDesktopServices>
#include <QDockWidget>
#include <QDrag>
#include <QFont>
#include <QFontDatabase>
#include <QListWidget>
#include <QMimeData>
#include <QPainter>
#include <QShortcut>
#include <QTimer>
#include <QVBoxLayout>
#include <boost/signals2.hpp>
@ -540,5 +535,6 @@ void Split::drag()
SplitContainer::isDraggingSplit = false;
}
}
} // namespace widgets
} // namespace chatterino

View file

@ -19,7 +19,6 @@
#include <boost/signals2/connection.hpp>
namespace chatterino {
namespace widgets {
class SplitContainer;
@ -45,7 +44,7 @@ class Split : public BaseWidget
public:
Split(SplitContainer *parent, const std::string &_uuid);
virtual ~Split();
~Split() override;
pajlada::Settings::Setting<QString> channelName;
boost::signals2::signal<void()> channelChanged;
@ -75,11 +74,11 @@ public:
void drag();
protected:
virtual void paintEvent(QPaintEvent *) override;
virtual void mouseMoveEvent(QMouseEvent *) override;
virtual void mousePressEvent(QMouseEvent *event) override;
virtual void keyPressEvent(QKeyEvent *) override;
virtual void keyReleaseEvent(QKeyEvent *) override;
void paintEvent(QPaintEvent *) override;
void mouseMoveEvent(QMouseEvent *) override;
void mousePressEvent(QMouseEvent *event) override;
void keyPressEvent(QKeyEvent *) override;
void keyReleaseEvent(QKeyEvent *) override;
private:
SplitContainer &parentPage;