KFileMetaData

typeinfo.cpp
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;
31};
32
33const TypeInfoPrivate TypeInfoPrivate::s_Empty{ Type::Empty, QStringLiteral("empty"), kli18nc("@label", "Empty") };
34
35const std::array<TypeInfoPrivate, 9> TypeInfoPrivate::s_allTypes
36{
37 TypeInfoPrivate{ Type::Archive, QStringLiteral("Archive"), kli18nc("@label", "Archive") },
38 TypeInfoPrivate{ Type::Audio, QStringLiteral("Audio"), kli18nc("@label", "Audio") },
39 TypeInfoPrivate{ Type::Document, QStringLiteral("Document"), kli18nc("@label", "Document") },
40 TypeInfoPrivate{ Type::Image, QStringLiteral("Image"), kli18nc("@label", "Image") },
41 TypeInfoPrivate{ Type::Presentation, QStringLiteral("Presentation"), kli18nc("@label", "Presentation") },
42 TypeInfoPrivate{ Type::Spreadsheet, QStringLiteral("Spreadsheet"), kli18nc("@label", "Spreadsheet") },
43 TypeInfoPrivate{ Type::Text, QStringLiteral("Text"), kli18nc("@label", "Text") },
44 TypeInfoPrivate{ Type::Video, QStringLiteral("Video"), kli18nc("@label", "Video") },
45 TypeInfoPrivate{ Type::Folder, QStringLiteral("Folder"), kli18nc("@label", "Folder") },
46};
47
48const QHash<LcIdentifierName, const TypeInfoPrivate*> TypeInfoPrivate::s_typeHash = []()
49{
51 infoHash.reserve(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(LcIdentifierName(name), &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
100{
101 return d->displayName.toString();
102}
103
105{
106 return d->name;
107}
108
110{
111 return d->type;
112}
113
115{
116 TypeInfo ti;
117 ti.d = TypeInfoPrivate::fromName(name);
118 return ti;
119}
120
122{
123 static QStringList sNames = []() {
124 QStringList names;
125 names.reserve(TypeInfoPrivate::s_allTypes.size());
126 for (auto info: TypeInfoPrivate::s_allTypes) {
127 names.append(info.name);
128 }
129 return names;
130 }();
131 return sNames;
132}
The TypeInfo class can be used to obtain a rough type classification for a file.
Definition typeinfo.h:31
QString displayName() const
A user visible translated name for this type.
Definition typeinfo.cpp:99
Type::Type type() const
The type identifier.
Definition typeinfo.cpp:109
static TypeInfo fromName(const QString &name)
Construct a TypeInfo from the internal type name.
Definition typeinfo.cpp:114
static QStringList allNames()
Get all supported property names.
Definition typeinfo.cpp:121
QString name() const
An internal unique name for the type.
Definition typeinfo.cpp:104
QString toString() const
Type
A Type represents a way to represent a way to group files based on a higher level view,...
Definition types.h:29
The KFileMetaData namespace.
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
void reserve(qsizetype size)
void append(QList< T > &&value)
void reserve(qsizetype size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.