Multiple fixes to custom icon downloading
* Fixes #904, icons are saved at or below 128x128 * Fixes #403, crash occurs due to dialog on non-gui thread * Fixes #232, icon hashes calculated and compared against
This commit is contained in:
committed by
Jonathan White
parent
2e4f1a21b4
commit
cb0b948603
@@ -15,6 +15,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <QtCore/QCryptographicHash>
|
||||
#include "Metadata.h"
|
||||
|
||||
#include "core/Entry.h"
|
||||
@@ -390,6 +391,9 @@ void Metadata::addCustomIcon(const Uuid& uuid, const QImage& icon)
|
||||
m_customIconCacheKeys[uuid] = QPixmapCache::Key();
|
||||
m_customIconScaledCacheKeys[uuid] = QPixmapCache::Key();
|
||||
m_customIconsOrder.append(uuid);
|
||||
// Associate image hash to uuid
|
||||
QByteArray hash = hashImage(icon);
|
||||
m_customIconsHashes[hash] = uuid;
|
||||
Q_ASSERT(m_customIcons.count() == m_customIconsOrder.count());
|
||||
emit modified();
|
||||
}
|
||||
@@ -415,6 +419,12 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
|
||||
Q_ASSERT(!uuid.isNull());
|
||||
Q_ASSERT(m_customIcons.contains(uuid));
|
||||
|
||||
// Remove hash record only if this is the same uuid
|
||||
QByteArray hash = hashImage(m_customIcons[uuid]);
|
||||
if (m_customIconsHashes.contains(hash) && m_customIconsHashes[hash] == uuid) {
|
||||
m_customIconsHashes.remove(hash);
|
||||
}
|
||||
|
||||
m_customIcons.remove(uuid);
|
||||
QPixmapCache::remove(m_customIconCacheKeys.value(uuid));
|
||||
m_customIconCacheKeys.remove(uuid);
|
||||
@@ -425,6 +435,12 @@ void Metadata::removeCustomIcon(const Uuid& uuid)
|
||||
emit modified();
|
||||
}
|
||||
|
||||
Uuid Metadata::findCustomIcon(const QImage &candidate)
|
||||
{
|
||||
QByteArray hash = hashImage(candidate);
|
||||
return m_customIconsHashes.value(hash, Uuid());
|
||||
}
|
||||
|
||||
void Metadata::copyCustomIcons(const QSet<Uuid>& iconList, const Metadata* otherMetadata)
|
||||
{
|
||||
for (const Uuid& uuid : iconList) {
|
||||
@@ -436,6 +452,12 @@ void Metadata::copyCustomIcons(const QSet<Uuid>& iconList, const Metadata* other
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray Metadata::hashImage(const QImage& image)
|
||||
{
|
||||
auto data = QByteArray((char*)image.bits(), image.byteCount());
|
||||
return QCryptographicHash::hash(data, QCryptographicHash::Md5);
|
||||
}
|
||||
|
||||
void Metadata::setRecycleBinEnabled(bool value)
|
||||
{
|
||||
set(m_data.recycleBinEnabled, value);
|
||||
|
||||
@@ -122,6 +122,7 @@ public:
|
||||
void addCustomIconScaled(const Uuid& uuid, const QImage& icon);
|
||||
void removeCustomIcon(const Uuid& uuid);
|
||||
void copyCustomIcons(const QSet<Uuid>& iconList, const Metadata* otherMetadata);
|
||||
Uuid findCustomIcon(const QImage& candidate);
|
||||
void setRecycleBinEnabled(bool value);
|
||||
void setRecycleBin(Group* group);
|
||||
void setRecycleBinChanged(const QDateTime& value);
|
||||
@@ -154,12 +155,15 @@ private:
|
||||
template <class P, class V> bool set(P& property, const V& value);
|
||||
template <class P, class V> bool set(P& property, const V& value, QDateTime& dateTime);
|
||||
|
||||
QByteArray hashImage(const QImage& image);
|
||||
|
||||
MetadataData m_data;
|
||||
|
||||
QHash<Uuid, QImage> m_customIcons;
|
||||
mutable QHash<Uuid, QPixmapCache::Key> m_customIconCacheKeys;
|
||||
mutable QHash<Uuid, QPixmapCache::Key> m_customIconScaledCacheKeys;
|
||||
QList<Uuid> m_customIconsOrder;
|
||||
QHash<QByteArray, Uuid> m_customIconsHashes;
|
||||
|
||||
QPointer<Group> m_recycleBin;
|
||||
QDateTime m_recycleBinChanged;
|
||||
|
||||
Reference in New Issue
Block a user