|
| 1 | +#include "unlockableicon.h" |
| 2 | +#include <QFile> |
| 3 | +#include <QBuffer> |
| 4 | + |
| 5 | +// TODO: QT_VERSION checks |
| 6 | +// TODO: Obfuscate the activation method? |
| 7 | + |
| 8 | +constexpr int Version = QDataStream::Qt_6_8; |
| 9 | + |
| 10 | +UnlockableIcon::UnlockableIcon(QObject* parent) : QObject(parent) {}; |
| 11 | +UnlockableIcon::UnlockableIcon(const QString& dataFilepath, QObject* parent) : UnlockableIcon(parent) { |
| 12 | + load(dataFilepath); |
| 13 | +}; |
| 14 | + |
| 15 | +bool UnlockableIcon::createDataFile(const QString& filepath, const QString& key, const QPixmap& icon) { |
| 16 | + QFile file(filepath); |
| 17 | + if (!file.open(QIODevice::WriteOnly)) return false; |
| 18 | + |
| 19 | + QDataStream out(&file); |
| 20 | + out.setVersion(Version); |
| 21 | + |
| 22 | + QByteArray key64 = key.toUtf8().toBase64(); |
| 23 | + out << (quint8)key.length(); |
| 24 | + out << (quint8)key64.length(); |
| 25 | + for (const auto& byte : key64) out << byte; |
| 26 | + |
| 27 | + QByteArray iconData; |
| 28 | + QBuffer buffer(&iconData); |
| 29 | + buffer.open(QIODevice::WriteOnly); |
| 30 | + icon.save(&buffer, "PNG"); |
| 31 | + buffer.close(); |
| 32 | + |
| 33 | + QByteArray iconData64 = iconData.toBase64(); |
| 34 | + out << (quint16)iconData.length(); |
| 35 | + out << (quint16)iconData64.length(); |
| 36 | + for (const auto& byte : iconData64) out << byte; |
| 37 | + |
| 38 | + file.close(); |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +bool UnlockableIcon::load(const QString& dataFilepath) { |
| 43 | + clear(); |
| 44 | + |
| 45 | + QFile file(dataFilepath); |
| 46 | + if (!file.open(QIODevice::ReadOnly)) return false; |
| 47 | + |
| 48 | + QDataStream in(&file); |
| 49 | + in.setVersion(Version); |
| 50 | + |
| 51 | + quint8 keyLength = 0; |
| 52 | + quint8 key64Length = 0; |
| 53 | + in >> keyLength; |
| 54 | + in >> key64Length; |
| 55 | + if (keyLength == 0 || key64Length == 0) return false; |
| 56 | + |
| 57 | + QByteArray key64(key64Length,0); |
| 58 | + for (quint8 i = 0; i < key64Length; i++) in >> key64[i]; |
| 59 | + QString key = QString(QByteArray::fromBase64(key64)); |
| 60 | + if (key.length() != keyLength) return false; |
| 61 | + |
| 62 | + quint16 iconDataLength = 0; |
| 63 | + quint16 iconData64Length = 0; |
| 64 | + in >> iconDataLength; |
| 65 | + in >> iconData64Length; |
| 66 | + if (iconDataLength == 0 || iconData64Length == 0) return false; |
| 67 | + |
| 68 | + QByteArray iconData64(iconData64Length,0); |
| 69 | + for (quint16 i = 0; i < iconData64Length; i++) in >> iconData64[i]; |
| 70 | + QByteArray iconData = QByteArray::fromBase64(iconData64); |
| 71 | + if (iconData.length() != iconDataLength) return false; |
| 72 | + |
| 73 | + QPixmap iconPixmap; |
| 74 | + iconPixmap.loadFromData(iconData); |
| 75 | + if (iconPixmap.isNull()) return false; |
| 76 | + |
| 77 | + m_icon = QIcon(iconPixmap); |
| 78 | + m_key = key; |
| 79 | + m_loaded = true; |
| 80 | + return true; |
| 81 | +} |
| 82 | + |
| 83 | +void UnlockableIcon::clear() { |
| 84 | + m_icon = QIcon(); |
| 85 | + m_key = QString(); |
| 86 | + m_keyIndex = 0; |
| 87 | + m_loaded = false; |
| 88 | +} |
| 89 | + |
| 90 | +bool UnlockableIcon::tryKeyMatch(const QSet<QChar>& cSet) { |
| 91 | + if (m_keyIndex >= m_key.length()) return false; |
| 92 | + if (!cSet.contains(m_key.at(m_keyIndex))) { |
| 93 | + m_keyIndex = 0; |
| 94 | + return false; |
| 95 | + } |
| 96 | + if (++m_keyIndex == m_key.length()) { |
| 97 | + emit unlocked(m_icon); |
| 98 | + } |
| 99 | + return true; |
| 100 | +} |
| 101 | + |
| 102 | +void UnlockableIcon::tryUnlock(const QSet<QChar>& cSet) { |
| 103 | + if (canUnlock()) tryKeyMatch(cSet); |
| 104 | +} |
| 105 | + |
| 106 | +void UnlockableIcon::tryUnlock(const QChar& c) { |
| 107 | + tryUnlock(QSet<QChar>{c}); |
| 108 | +} |
| 109 | + |
| 110 | +void UnlockableIcon::tryUnlock(const QString& key) { |
| 111 | + if (!canUnlock()) return; |
| 112 | + for (const QChar& c : key) { |
| 113 | + if (!tryKeyMatch(QSet<QChar>{c})) return; |
| 114 | + } |
| 115 | +} |
0 commit comments