Changeset View
Changeset View
Standalone View
Standalone View
wallpapers/image/backgroundlistmodel.cpp
Show All 27 Lines | |||||
28 | #include <QStandardPaths> | 28 | #include <QStandardPaths> | ||
29 | #include <QThreadPool> | 29 | #include <QThreadPool> | ||
30 | #include <QUuid> | 30 | #include <QUuid> | ||
31 | #include <QGuiApplication> | 31 | #include <QGuiApplication> | ||
32 | #include <QFontMetrics> | 32 | #include <QFontMetrics> | ||
33 | #include <QImageReader> | 33 | #include <QImageReader> | ||
34 | #include <QMimeDatabase> | 34 | #include <QMimeDatabase> | ||
35 | #include <QMimeType> | 35 | #include <QMimeType> | ||
36 | #include <QMutex> | ||||
37 | #include <QMutexLocker> | ||||
36 | 38 | | |||
37 | #include <QDebug> | 39 | #include <QDebug> | ||
38 | #include <KIO/PreviewJob> | 40 | #include <KIO/PreviewJob> | ||
39 | #include <KLocalizedString> | 41 | #include <KLocalizedString> | ||
40 | #include <kimagecache.h> | 42 | #include <kimagecache.h> | ||
41 | #include <kaboutdata.h> | 43 | #include <kaboutdata.h> | ||
42 | 44 | | |||
43 | #include <KPackage/Package> | 45 | #include <KPackage/Package> | ||
44 | #include <KPackage/PackageStructure> | 46 | #include <KPackage/PackageStructure> | ||
45 | #include <KPackage/PackageLoader> | 47 | #include <KPackage/PackageLoader> | ||
46 | 48 | | |||
47 | #include <KIO/OpenFileManagerWindowJob> | 49 | #include <KIO/OpenFileManagerWindowJob> | ||
48 | 50 | | |||
49 | #include "image.h" | 51 | #include "image.h" | ||
50 | 52 | | |||
51 | QStringList BackgroundFinder::m_suffixes; | 53 | QStringList BackgroundFinder::s_suffixes; | ||
54 | QMutex BackgroundFinder::s_suffixMutex; | ||||
52 | 55 | | |||
53 | ImageSizeFinder::ImageSizeFinder(const QString &path, QObject *parent) | 56 | ImageSizeFinder::ImageSizeFinder(const QString &path, QObject *parent) | ||
54 | : QObject(parent), | 57 | : QObject(parent), | ||
55 | m_path(path) | 58 | m_path(path) | ||
56 | { | 59 | { | ||
57 | } | 60 | } | ||
58 | 61 | | |||
59 | void ImageSizeFinder::run() | 62 | void ImageSizeFinder::run() | ||
▲ Show 20 Lines • Show All 447 Lines • ▼ Show 20 Line(s) | 509 | { | |||
507 | wait(); | 510 | wait(); | ||
508 | } | 511 | } | ||
509 | 512 | | |||
510 | QString BackgroundFinder::token() const | 513 | QString BackgroundFinder::token() const | ||
511 | { | 514 | { | ||
512 | return m_token; | 515 | return m_token; | ||
513 | } | 516 | } | ||
514 | 517 | | |||
515 | const QStringList &BackgroundFinder::suffixes() | 518 | QStringList BackgroundFinder::suffixes() | ||
516 | { | 519 | { | ||
517 | if (m_suffixes.isEmpty()) { | 520 | QMutexLocker lock(&s_suffixMutex); | ||
521 | if (s_suffixes.isEmpty()) { | ||||
518 | QSet<QString> suffixes; | 522 | QSet<QString> suffixes; | ||
519 | 523 | | |||
520 | QMimeDatabase db; | 524 | QMimeDatabase db; | ||
521 | Q_FOREACH (const QByteArray &mimeType, QImageReader::supportedMimeTypes()) { | 525 | Q_FOREACH (const QByteArray &mimeType, QImageReader::supportedMimeTypes()) { | ||
522 | QMimeType mime(db.mimeTypeForName(mimeType)); | 526 | QMimeType mime(db.mimeTypeForName(mimeType)); | ||
523 | Q_FOREACH (const QString &pattern, mime.globPatterns()) { | 527 | Q_FOREACH (const QString &pattern, mime.globPatterns()) { | ||
524 | suffixes.insert(pattern); | 528 | suffixes.insert(pattern); | ||
525 | } | 529 | } | ||
526 | } | 530 | } | ||
527 | 531 | | |||
528 | m_suffixes = suffixes.toList(); | 532 | s_suffixes = suffixes.toList(); | ||
anthonyfieroni: Potentially it still can produce a crash, the function returns a reference to list… | |||||
529 | } | 533 | } | ||
530 | 534 | | |||
531 | return m_suffixes; | 535 | return s_suffixes; | ||
532 | } | 536 | } | ||
533 | 537 | | |||
534 | bool BackgroundFinder::isAcceptableSuffix(const QString &suffix) | 538 | bool BackgroundFinder::isAcceptableSuffix(const QString &suffix) | ||
535 | { | 539 | { | ||
536 | // Despite its name, suffixes() returns a list of glob patterns. | 540 | // Despite its name, suffixes() returns a list of glob patterns. | ||
537 | // Therefore the file suffix check needs to include the "*." prefix. | 541 | // Therefore the file suffix check needs to include the "*." prefix. | ||
538 | const QStringList &globPatterns = suffixes(); | 542 | const QStringList &globPatterns = suffixes(); | ||
539 | return globPatterns.contains(QStringLiteral("*.") + suffix.toLower()); | 543 | return globPatterns.contains(QStringLiteral("*.") + suffix.toLower()); | ||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |
Potentially it still can produce a crash, the function returns a reference to list, simultaneously read/write on list is race condition. So function can return a copy to static list for this function, then global variables or mutexes doesn't needed.