Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: check if WEBP is supported #284

Draft
wants to merge 1 commit into
base: chatterino7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ set(SOURCE_FILES
util/RenameThread.hpp
util/SampleData.cpp
util/SampleData.hpp
util/SanityCheckImages.cpp
util/SanityCheckImages.hpp
util/SharedPtrElementLess.hpp
util/SignalListener.hpp
util/StreamLink.cpp
Expand Down
3 changes: 3 additions & 0 deletions src/RunGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "singletons/Settings.hpp"
#include "singletons/Updates.hpp"
#include "util/CombinePath.hpp"
#include "util/SanityCheckImages.hpp"
#include "widgets/dialogs/LastRunCrashDialog.hpp"

#include <QApplication>
Expand Down Expand Up @@ -241,6 +242,8 @@ void runGui(QApplication &a, const Paths &paths, Settings &settings,
}
#endif

sanityCheckImages();

updates.deleteOldFiles();

// Clear the cache 1 minute after start.
Expand Down
69 changes: 69 additions & 0 deletions src/util/SanityCheckImages.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "util/SanityCheckImages.hpp"

#include "common/Literals.hpp"

#include <QBuffer>
#include <QImageReader>
#include <QMessageBox>
#include <QMimeDatabase>
#include <QStringList>

namespace {

using namespace chatterino::literals;

// lossy image from https://developers.google.com/speed/webp/faq#in_your_own_javascript
const QByteArray WEBP_IMAGE =
"RIFF\x22\0\0\0WEBPVP8\x20\x16\0\0\0\x30\x01\0\x9d\x01\x2a\x01\0\x01\0\x0e\xc0\xfe\x25\xa4\0\x03p\0\0\0\0"_ba;

} // namespace

namespace chatterino {

void sanityCheckImages()
{
QStringList messages;
QMimeDatabase mimeDb;

auto mime = mimeDb.mimeTypeForData(WEBP_IMAGE);
if (!mime.inherits("image/webp"))
{
messages.emplace_back(u"Failed to determine MIME type for WEBP image - "
u"detected MIME type: \"" %
mime.name() % '"');
}

QBuffer buffer;
buffer.setData(WEBP_IMAGE);
QImageReader reader(&buffer);

if (reader.canRead())
{
if (reader.imageCount() != 1)
{
messages.emplace_back(u"Minimal WEBP image doesn't have one (1) "
u"expected image - got: " %
QString::number(reader.imageCount()) %
u" - error: " % reader.errorString());
}
reader.read();
}
else
{
messages.emplace_back(
u"Minimal WEBP image can't be read - QImageReader::canRead "
u"returned false - error: \"" %
reader.errorString() % '"');
}

if (!messages.empty())
{
QMessageBox::warning(
nullptr, u"Chatterino - Sanity Check"_s,
u"Your Chatterino instance is not able to load WEBP files.\nMake "
u"sure you have qtimageformats installed.\n\n" %
messages.join(u"\n\n"));
}
}

} // namespace chatterino
8 changes: 8 additions & 0 deletions src/util/SanityCheckImages.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

namespace chatterino {

/// Checks if WEBPs can be loaded
void sanityCheckImages();

} // namespace chatterino
Loading