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 { namespace chatterino {
static const QString ANONYMOUS_USERNAME_LABEL(" - anonymous - "); static const char *ANONYMOUS_USERNAME_LABEL = " - anonymous - ";
namespace providers { namespace providers {
namespace twitch { namespace twitch {
static const QString ANONYMOUS_USERNAME("justinfan64537"); static const char *ANONYMOUS_USERNAME = "justinfan64537";
}
} // namespace twitch
} // namespace providers } // namespace providers
} // namespace chatterino } // namespace chatterino

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -26,7 +26,7 @@ namespace singletons {
namespace { namespace {
static QString GetTwitchEmoteLink(long id, const QString &emoteScale) QString GetTwitchEmoteLink(long id, const QString &emoteScale)
{ {
QString value = TWITCH_EMOTE_TEMPLATE; 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); 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(); urlTemplate.detach();
return urlTemplate.replace("{{id}}", id).replace("{{image}}", emoteScale); 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); auto emote = urls.value(emoteScale);
if (emote.isUndefined()) { if (emote.isUndefined()) {
@ -54,8 +54,7 @@ static QString GetFFZEmoteLink(const QJsonObject &urls, const QString &emoteScal
return "http:" + emote.toString(); return "http:" + emote.toString();
} }
static void FillInFFZEmoteData(const QJsonObject &urls, const QString &code, void FillInFFZEmoteData(const QJsonObject &urls, const QString &code, util::EmoteData &emoteData)
util::EmoteData &emoteData)
{ {
QString url1x = GetFFZEmoteLink(urls, "1"); QString url1x = GetFFZEmoteLink(urls, "1");
QString url2x = GetFFZEmoteLink(urls, "2"); QString url2x = GetFFZEmoteLink(urls, "2");
@ -366,9 +365,8 @@ void EmoteManager::parseEmojis(std::vector<std::tuple<util::EmoteData, QString>>
if (charactersFromLastParsedEmoji > 0) { if (charactersFromLastParsedEmoji > 0) {
// Add characters inbetween emojis // Add characters inbetween emojis
parsedWords.push_back(std::tuple<util::EmoteData, QString>( parsedWords.emplace_back(util::EmoteData(), text.mid(lastParsedEmojiEndIndex,
util::EmoteData(), charactersFromLastParsedEmoji));
text.mid(lastParsedEmojiEndIndex, charactersFromLastParsedEmoji)));
} }
QString url = "https://cdnjs.cloudflare.com/ajax/libs/" 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"; matchedEmoji.code + ".png";
// Create or fetch cached emoji image // 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, "?????????", "???????????????")); // 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()) { if (lastParsedEmojiEndIndex < text.length()) {
// Add remaining characters // Add remaining characters
parsedWords.push_back(std::tuple<util::EmoteData, QString>( parsedWords.emplace_back(util::EmoteData(), text.mid(lastParsedEmojiEndIndex));
util::EmoteData(), text.mid(lastParsedEmojiEndIndex)));
} }
} }
@ -546,7 +543,7 @@ util::EmoteData EmoteManager::getTwitchEmoteById(long id, const QString &emoteNa
QString _emoteName = emoteName; QString _emoteName = emoteName;
_emoteName.replace("<", "&lt;"); _emoteName.replace("<", "&lt;");
return _twitchEmoteFromCache.getOrAdd(id, [this, &emoteName, &_emoteName, &id] { return _twitchEmoteFromCache.getOrAdd(id, [&emoteName, &_emoteName, &id] {
util::EmoteData newEmoteData; util::EmoteData newEmoteData;
newEmoteData.image1x = new Image(GetTwitchEmoteLink(id, "1.0"), 1, emoteName, newEmoteData.image1x = new Image(GetTwitchEmoteLink(id, "1.0"), 1, emoteName,
_emoteName + "<br/>Twitch Emote 1x"); _emoteName + "<br/>Twitch Emote 1x");
@ -593,3 +590,61 @@ boost::signals2::signal<void()> &EmoteManager::getGifUpdateSignal()
} // namespace singletons } // namespace singletons
} // namespace chatterino } // 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 class ConcurrentMap
{ {
public: public:
ConcurrentMap() ConcurrentMap() = default;
{
}
bool tryGet(const TKey &name, TValue &value) const bool tryGet(const TKey &name, TValue &value) const
{ {
@ -84,5 +82,6 @@ private:
mutable QMutex mutex; mutable QMutex mutex;
QMap<TKey, TValue> data; QMap<TKey, TValue> data;
}; };
} // namespace util
} // namespace chatterino } // namespace chatterino
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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