1/*
2 SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "typeinfo.h"
8
9#include <KLazyLocalizedString>
10#include <KLocalizedString>
11
12#include "icnamematch_p.h"
13
14#include <array>
15
16using namespace KFileMetaData;
17
18class KFileMetaData::TypeInfoPrivate
19{
20public:
21 const Type::Type type;
22 const QString name;
23 const KLazyLocalizedString displayName;
24
25 static constexpr auto fromId(Type::Type type) -> const TypeInfoPrivate*;
26 static auto fromName(const QString& name) -> const TypeInfoPrivate*;
27
28 static const TypeInfoPrivate s_Empty;
29 static const std::array<TypeInfoPrivate, 9> s_allTypes;
30 static const QHash<LcIdentifierName, const TypeInfoPrivate*> s_typeHash;
31};
32
33const TypeInfoPrivate TypeInfoPrivate::s_Empty{ .type: Type::Empty, QStringLiteral("empty"), .displayName: kli18nc(context: "@label", text: "Empty") };
34
35const std::array<TypeInfoPrivate, 9> TypeInfoPrivate::s_allTypes
36{
37 TypeInfoPrivate{ .type: Type::Archive, QStringLiteral("Archive"), .displayName: kli18nc(context: "@label", text: "Archive") },
38 TypeInfoPrivate{ .type: Type::Audio, QStringLiteral("Audio"), .displayName: kli18nc(context: "@label", text: "Audio") },
39 TypeInfoPrivate{ .type: Type::Document, QStringLiteral("Document"), .displayName: kli18nc(context: "@label", text: "Document") },
40 TypeInfoPrivate{ .type: Type::Image, QStringLiteral("Image"), .displayName: kli18nc(context: "@label", text: "Image") },
41 TypeInfoPrivate{ .type: Type::Presentation, QStringLiteral("Presentation"), .displayName: kli18nc(context: "@label", text: "Presentation") },
42 TypeInfoPrivate{ .type: Type::Spreadsheet, QStringLiteral("Spreadsheet"), .displayName: kli18nc(context: "@label", text: "Spreadsheet") },
43 TypeInfoPrivate{ .type: Type::Text, QStringLiteral("Text"), .displayName: kli18nc(context: "@label", text: "Text") },
44 TypeInfoPrivate{ .type: Type::Video, QStringLiteral("Video"), .displayName: kli18nc(context: "@label", text: "Video") },
45 TypeInfoPrivate{ .type: Type::Folder, QStringLiteral("Folder"), .displayName: kli18nc(context: "@label", text: "Folder") },
46};
47
48const QHash<LcIdentifierName, const TypeInfoPrivate*> TypeInfoPrivate::s_typeHash = []()
49{
50 QHash<LcIdentifierName, const TypeInfoPrivate*> infoHash;
51 infoHash.reserve(size: s_allTypes.size());
52
53 for (const auto& info: s_allTypes) {
54 infoHash[QStringView(info.name)] = &info;
55 }
56 return infoHash;
57}();
58
59constexpr auto TypeInfoPrivate::fromId(Type::Type type) -> const TypeInfoPrivate*
60{
61 for (const auto& t : s_allTypes) {
62 if (t.type == type)
63 return &t;
64 }
65 return &s_Empty;
66}
67
68auto TypeInfoPrivate::fromName(const QString& name) -> const TypeInfoPrivate*
69{
70 return s_typeHash.value(key: LcIdentifierName(name), defaultValue: &s_Empty);
71}
72
73TypeInfo::TypeInfo()
74 : d(&TypeInfoPrivate::s_Empty) {};
75
76TypeInfo::TypeInfo(Type::Type type)
77 : d(TypeInfoPrivate::fromId(type))
78{
79}
80
81TypeInfo::TypeInfo(const TypeInfo& ti)
82 : d(ti.d)
83{
84}
85
86TypeInfo::~TypeInfo() = default;
87
88TypeInfo& TypeInfo::operator=(const TypeInfo& rhs)
89{
90 d = rhs.d;
91 return *this;
92}
93
94bool TypeInfo::operator==(const TypeInfo& rhs) const
95{
96 return d == rhs.d;
97}
98
99QString TypeInfo::displayName() const
100{
101 return d->displayName.toString();
102}
103
104QString TypeInfo::name() const
105{
106 return d->name;
107}
108
109Type::Type TypeInfo::type() const
110{
111 return d->type;
112}
113
114TypeInfo TypeInfo::fromName(const QString& name)
115{
116 TypeInfo ti;
117 ti.d = TypeInfoPrivate::fromName(name);
118 return ti;
119}
120
121QStringList TypeInfo::allNames()
122{
123 static QStringList sNames = []() {
124 QStringList names;
125 names.reserve(asize: TypeInfoPrivate::s_allTypes.size());
126 for (auto info: TypeInfoPrivate::s_allTypes) {
127 names.append(t: info.name);
128 }
129 return names;
130 }();
131 return sNames;
132}
133

source code of kfilemetadata/src/typeinfo.cpp