|
22 | 22 | #include <QFileSystemWatcher> |
23 | 23 | #include <QSet> |
24 | 24 | #include <QDebug> |
| 25 | +#include <QCryptographicHash> |
25 | 26 |
|
26 | 27 | #define MAX_SIZE 1024 |
27 | 28 |
|
@@ -255,28 +256,96 @@ void IconList::installIcons(const QStringList &iconFiles) |
255 | 256 | for (QString file : iconFiles) |
256 | 257 | { |
257 | 258 | QFileInfo fileinfo(file); |
258 | | - if (!fileinfo.isReadable() || !fileinfo.isFile()) |
| 259 | + if(!fileinfo.isReadable() || !fileinfo.isFile()) |
259 | 260 | continue; |
260 | | - QString target = FS::PathCombine(m_dir.dirName(), fileinfo.fileName()); |
261 | 261 |
|
| 262 | + QFile sourceFile(file); |
| 263 | + if(!sourceFile.open(QIODevice::ReadOnly)) |
| 264 | + continue; |
| 265 | + |
262 | 266 | QString suffix = fileinfo.suffix(); |
263 | 267 | if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif") |
264 | 268 | continue; |
| 269 | + |
| 270 | + const QString sourceHash = getIconHash(sourceFile); |
| 271 | + QString sourceFileName = fileinfo.baseName(); |
| 272 | + sourceFileName = QString("%1_%2.%3").arg(sourceFileName, sourceHash, suffix); |
| 273 | + QString target = FS::PathCombine(m_dir.dirName(), sourceFileName); |
| 274 | + |
| 275 | + if(QFile::exists(target)) |
| 276 | + { |
| 277 | + QFile targetFile(target); |
| 278 | + if(!targetFile.open(QIODevice::ReadOnly)) |
| 279 | + continue; |
265 | 280 |
|
| 281 | + const QString targetHash = getIconHash(targetFile); |
| 282 | + |
| 283 | + if(sourceHash == targetHash) |
| 284 | + continue; |
| 285 | + |
| 286 | + QString targetFileName = fileinfo.baseName(); |
| 287 | + targetFileName = QString("%1_%2.%3").arg(targetFileName, targetHash, suffix); |
| 288 | + targetFileName = FS::PathCombine(m_dir.dirName(), targetFileName); |
| 289 | + if(!targetFile.rename(targetFileName)) |
| 290 | + continue; |
| 291 | + } |
266 | 292 | if (!QFile::copy(file, target)) |
267 | 293 | continue; |
268 | 294 | } |
269 | 295 | } |
270 | 296 |
|
271 | | -void IconList::installIcon(const QString &file, const QString &name) |
| 297 | +void IconList::installIcon(const QString &file, QString &name) |
272 | 298 | { |
273 | | - QFileInfo fileinfo(file); |
274 | | - if(!fileinfo.isReadable() || !fileinfo.isFile()) |
275 | | - return; |
| 299 | + QFileInfo fileinfo(file); |
| 300 | + if(!fileinfo.isReadable() || !fileinfo.isFile()) |
| 301 | + return; |
| 302 | + |
| 303 | + QFile sourceFile(file); |
| 304 | + if(!sourceFile.open(QIODevice::ReadOnly)) |
| 305 | + return; |
| 306 | + |
| 307 | + QString suffix = fileinfo.suffix(); |
| 308 | + if (suffix != "jpeg" && suffix != "png" && suffix != "jpg" && suffix != "ico" && suffix != "svg" && suffix != "gif") |
| 309 | + return; |
| 310 | + |
| 311 | + const QString sourceHash = getIconHash(sourceFile); |
| 312 | + name = QString("%1_%2.%3").arg(name, sourceHash, suffix); |
| 313 | + QString target = FS::PathCombine(m_dir.dirName(), name); |
| 314 | + |
| 315 | + if(QFile::exists(target)) |
| 316 | + { |
| 317 | + QFile targetFile(target); |
| 318 | + if(!targetFile.open(QIODevice::ReadOnly)) |
| 319 | + return; |
| 320 | + |
| 321 | + const QString targetHash = getIconHash(targetFile); |
| 322 | + |
| 323 | + if(sourceHash == targetHash) |
| 324 | + return; |
| 325 | + |
| 326 | + QString targetFileName = QString(name); |
| 327 | + targetFileName = QString("%1_%2.%3").arg(targetFileName, targetHash, suffix); |
| 328 | + targetFileName = FS::PathCombine(m_dir.dirName(), targetFileName); |
| 329 | + if(!targetFile.rename(targetFileName)) |
| 330 | + return; |
| 331 | + } |
| 332 | + if (!QFile::copy(file, target)) |
| 333 | + return; |
| 334 | +} |
276 | 335 |
|
277 | | - QString target = FS::PathCombine(m_dir.dirName(), name); |
| 336 | +const QString IconList::getIconHash(QFile& file) |
| 337 | +{ |
| 338 | + QByteArray iconByteArray = file.readAll(); |
| 339 | + const QString iconHash = QString(QCryptographicHash::hash((iconByteArray), QCryptographicHash::Md5).toHex()); |
| 340 | + return iconHash; |
| 341 | +} |
278 | 342 |
|
279 | | - QFile::copy(file, target); |
| 343 | +const QString IconList::getIconHash(const QString& fileName) |
| 344 | +{ |
| 345 | + QFile file(fileName); |
| 346 | + if(!file.open(QIODevice::ReadOnly)) |
| 347 | + return ""; |
| 348 | + return getIconHash(file); |
280 | 349 | } |
281 | 350 |
|
282 | 351 | bool IconList::iconFileExists(const QString &key) const |
|
0 commit comments