gui: previewgenerator: Use QHash and guard using mutexes

This commit is contained in:
Albert S. 2022-10-22 15:07:46 +02:00
parent 48ca25abe3
commit 49a1a14009

View File

@ -1,20 +1,24 @@
#include "../shared/common.h" #include "../shared/common.h"
#include "previewgenerator.h" #include "previewgenerator.h"
#include <QMutexLocker>
#include "previewgeneratorpdf.h" #include "previewgeneratorpdf.h"
#include "previewgeneratorplaintext.h" #include "previewgeneratorplaintext.h"
#include "previewgeneratorodt.h" #include "previewgeneratorodt.h"
static PreviewGenerator *plainTextGenerator = new PreviewGeneratorPlainText(); static PreviewGenerator *plainTextGenerator = new PreviewGeneratorPlainText();
static QMap<QString, PreviewGenerator *> generators{ static QHash<QString, PreviewGenerator *> generators{
{"pdf", new PreviewGeneratorPdf()}, {"txt", plainTextGenerator}, {"md", plainTextGenerator}, {"pdf", new PreviewGeneratorPdf()}, {"txt", plainTextGenerator}, {"md", plainTextGenerator},
{"py", plainTextGenerator}, {"java", plainTextGenerator}, {"js", plainTextGenerator}, {"py", plainTextGenerator}, {"java", plainTextGenerator}, {"js", plainTextGenerator},
{"cpp", plainTextGenerator}, {"c", plainTextGenerator}, {"sql", plainTextGenerator}, {"cpp", plainTextGenerator}, {"c", plainTextGenerator}, {"sql", plainTextGenerator},
{"odt", new PreviewGeneratorOdt()}}; {"odt", new PreviewGeneratorOdt()}};
static QMutex generatorsMutex;
PreviewGenerator *PreviewGenerator::get(QFileInfo &info) PreviewGenerator *PreviewGenerator::get(QFileInfo &info)
{ {
QMutexLocker locker(&generatorsMutex);
PreviewGenerator *result = generators.value(info.suffix(), nullptr); PreviewGenerator *result = generators.value(info.suffix(), nullptr);
locker.unlock();
if(result == nullptr) if(result == nullptr)
{ {
if(Common::isTextFile(info)) if(Common::isTextFile(info))