┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/kitemviews/private/kbaloorolesprovider.cpp
blob: d45b06777ba300e684fbb312f9107303e139df82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * SPDX-FileCopyrightText: 2012 Peter Penz <[email protected]>
 * SPDX-FileCopyrightText: 2013 Vishesh Handa <[email protected]>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "kbaloorolesprovider.h"

#include <Baloo/File>
#include <KFileMetaData/PropertyInfo>
#include <KFileMetaData/UserMetaData>
#include <KFormat>
#include <KLocalizedString>

#include <QCollator>
#include <QDebug>
#include <QTime>

struct KBalooRolesProviderSingleton
{
    KBalooRolesProvider instance;
};
Q_GLOBAL_STATIC(KBalooRolesProviderSingleton, s_balooRolesProvider)


KBalooRolesProvider& KBalooRolesProvider::instance()
{
    return s_balooRolesProvider->instance;
}

KBalooRolesProvider::~KBalooRolesProvider()
{
}

QSet<QByteArray> KBalooRolesProvider::roles() const
{
    return m_roles;
}

QHash<QByteArray, QVariant> KBalooRolesProvider::roleValues(const Baloo::File& file,
                                                            const QSet<QByteArray>& roles) const
{
    QHash<QByteArray, QVariant> values;

    using entry = std::pair<const KFileMetaData::Property::Property&, const QVariant&>;

    const auto& propMap = file.properties();
    auto rangeBegin = propMap.constKeyValueBegin();

    while (rangeBegin != propMap.constKeyValueEnd()) {
        auto key = (*rangeBegin).first;
        const KFileMetaData::PropertyInfo propertyInfo(key);
        const QByteArray role = roleForProperty(propertyInfo.name());

        auto rangeEnd = std::find_if(rangeBegin, propMap.constKeyValueEnd(),
            [key](const entry& e) { return e.first != key; });

        if (role.isEmpty() || !roles.contains(role)) {
            rangeBegin = rangeEnd;
            continue;
        }

        auto distance = std::distance(rangeBegin, rangeEnd);
        if (distance > 1) {
            QVariantList list;
            list.reserve(static_cast<int>(distance));
            std::for_each(rangeBegin, rangeEnd, [&list](const entry& s) { list.append(s.second); });
            values.insert(role, propertyInfo.formatAsDisplayString(list));
        } else {
            if (propertyInfo.valueType() == QVariant::DateTime) {
                // Let dolphin format later Dates
                values.insert(role, (*rangeBegin).second);
            } else {
                values.insert(role, propertyInfo.formatAsDisplayString((*rangeBegin).second));
            }
        }
        rangeBegin = rangeEnd;
    }

    KFileMetaData::UserMetaData md(file.path());
    if (roles.contains("tags")) {
        values.insert("tags", tagsFromValues(md.tags()));
    }
    if (roles.contains("rating")) {
        values.insert("rating", QString::number(md.rating()));
    }
    if (roles.contains("comment")) {
        values.insert("comment", md.userComment());
    }
    if (roles.contains("originUrl")) {
        values.insert("originUrl", md.originUrl());
    }

    return values;
}

QByteArray KBalooRolesProvider::roleForProperty(const QString& property) const
{
    return m_roleForProperty.value(property);
}

KBalooRolesProvider::KBalooRolesProvider() :
    m_roles(),
    m_roleForProperty()
{
    struct PropertyInfo
    {
        const char* const property;
        const char* const role;
    };

    // Mapping from the URIs to the KFileItemModel roles. Note that this must not be
    // a 1:1 mapping: One role may contain several URI-values
    static const PropertyInfo propertyInfoList[] = {
        { "rating", "rating" },
        { "tag",        "tags" },
        { "comment",   "comment" },
        { "title",         "title" },
        { "wordCount",     "wordCount" },
        { "lineCount",     "lineCount" },
        { "width",         "width" },
        { "height",        "height" },
        { "imageDateTime",   "imageDateTime"},
        { "imageOrientation", "orientation", },
        { "artist",     "artist" },
        { "genre",	"genre"  },
        { "album",    "album" },
        { "duration",      "duration" },
        { "bitRate", "bitrate" },
        { "aspectRatio", "aspectRatio" },
        { "frameRate", "frameRate" },
        { "releaseYear",    "releaseYear" },
        { "trackNumber",   "track" },
        { "originUrl", "originUrl" }
    };

    for (unsigned int i = 0; i < sizeof(propertyInfoList) / sizeof(PropertyInfo); ++i) {
        m_roleForProperty.insert(propertyInfoList[i].property, propertyInfoList[i].role);
        m_roles.insert(propertyInfoList[i].role);
    }
}

QString KBalooRolesProvider::tagsFromValues(const QStringList& values) const
{
    QStringList alphabeticalOrderTags = values;
    QCollator coll;
    coll.setNumericMode(true);
    std::sort(alphabeticalOrderTags.begin(), alphabeticalOrderTags.end(), [&](const QString& s1, const QString& s2){ return coll.compare(s1, s2) < 0; });
    return alphabeticalOrderTags.join(QLatin1String(", "));
}