~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/metadata/metadata_engine.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009 Anne-Marie Mahfouf <annma@kde.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Library General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public License
 
15
 * along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc.,
 
17
 * 51 Franklin Street, Fifth Floor,
 
18
 * Boston, MA 02110-1301, USA.
 
19
 */
 
20
 
 
21
#include "metadata_engine.h"
 
22
 
 
23
#include <QDateTime>
 
24
#include <QUrl>
 
25
 
 
26
#include <kfilemetainfo.h>
 
27
 
 
28
// Nepomuk
 
29
#include <Nepomuk/Resource>
 
30
#include <Nepomuk/ResourceManager>
 
31
#include <Nepomuk/Tag>
 
32
 
 
33
MetaDataEngine::MetaDataEngine(QObject* parent, const QVariantList& args) :
 
34
        Plasma::DataEngine(parent, args)
 
35
{
 
36
}
 
37
 
 
38
MetaDataEngine::~MetaDataEngine()
 
39
{
 
40
}
 
41
 
 
42
void MetaDataEngine::init()
 
43
{
 
44
    Nepomuk::ResourceManager::instance()->init();
 
45
}
 
46
 
 
47
bool MetaDataEngine::sourceRequestEvent(const QString &name)
 
48
{
 
49
    updateSourceEvent(name);
 
50
    return true;
 
51
}
 
52
 
 
53
bool MetaDataEngine::updateSourceEvent(const QString &name)
 
54
{
 
55
    // Get picture general properties through KFileMetaInfo
 
56
    const KFileMetaInfo::WhatFlags flags = KFileMetaInfo::Fastest |
 
57
                                           KFileMetaInfo::TechnicalInfo |
 
58
                                           KFileMetaInfo::ContentInfo;
 
59
    const KFileMetaInfo fileMetaInfo(name, QString(), flags);
 
60
    if (fileMetaInfo.isValid()) {
 
61
        const QHash<QString, KFileMetaInfoItem>& items = fileMetaInfo.items();
 
62
        QHash<QString, KFileMetaInfoItem>::const_iterator it = items.constBegin();
 
63
        const QHash<QString, KFileMetaInfoItem>::const_iterator end = items.constEnd();
 
64
        QString labelText;
 
65
        while (it != end) {
 
66
            const KFileMetaInfoItem& metaInfoItem = it.value();
 
67
            const QVariant& value = metaInfoItem.value();
 
68
 
 
69
            if (value.isValid() && convertMetaInfo(metaInfoItem.name(), labelText)) {
 
70
                if (labelText == i18nc("@label", "Size")) {
 
71
                    const QString s = QString::number(value.toDouble()/1024/1024, 'f', 2);
 
72
                    setData(name, labelText, QString::fromLatin1("%1 Mb").arg(s));
 
73
                } else if (labelText == i18nc("@label", "Source Modified")) {
 
74
                    QVariant newValue = QVariant(value.toDateTime());
 
75
                    setData(name, labelText, newValue.toString());
 
76
                } else {
 
77
                    setData(name, labelText, value.toString());
 
78
                }
 
79
            }
 
80
 
 
81
            ++it;
 
82
        }
 
83
    }
 
84
 
 
85
    // Get picture tags through Nepomuk
 
86
    QUrl uri = QUrl(name);
 
87
    QStringList tags;
 
88
    Nepomuk::Resource res( uri);
 
89
    QList<Nepomuk::Tag> picTags = res.tags();
 
90
    Q_FOREACH(const Nepomuk::Tag& tag, picTags) {
 
91
        tags.append(tag.label() + ", ");
 
92
    }
 
93
 
 
94
    setData(name, "tags", tags);
 
95
    // Get picture comment through Nepomuk
 
96
    setData(name, "comment", res.description());
 
97
    // Get picture rating through Nepomuk
 
98
    setData(name, "rating", QString::number(res.rating()));
 
99
 
 
100
    return true;
 
101
}
 
102
 
 
103
bool MetaDataEngine::convertMetaInfo(const QString& key, QString& text) const
 
104
{
 
105
    struct MetaKey {
 
106
        const char* key;
 
107
        QString text;
 
108
    };
 
109
 
 
110
    // keys list, more can be added
 
111
    static const MetaKey keys[] = {
 
112
    { "http://freedesktop.org/standards/xesam/1.0/core#cameraModel", i18nc("@label", "Model") },
 
113
    { "http://freedesktop.org/standards/xesam/1.0/core#focalLength", i18nc("@label", "Focal Length") },
 
114
    { "http://freedesktop.org/standards/xesam/1.0/core#mimeType", i18nc("@label", "Mime Type") },
 
115
    { "http://freedesktop.org/standards/xesam/1.0/core#cameraManufacturer", i18nc("@label", "Manufacturer") },
 
116
    { "http://freedesktop.org/standards/xesam/1.0/core#sourceModified", i18nc("@label", "Source Modified") },
 
117
    { "http://freedesktop.org/standards/xesam/1.0/core#orientation", i18nc("@label", "Orientation") },
 
118
    { "http://freedesktop.org/standards/xesam/1.0/core#flashUsed", i18nc("@label", "Flash Used") },
 
119
    { "http://freedesktop.org/standards/xesam/1.0/core#height", i18nc("@label", "Height") },
 
120
    { "http://freedesktop.org/standards/xesam/1.0/core#width",  i18nc("@label", "Width") },
 
121
    { "http://freedesktop.org/standards/xesam/1.0/core#url", i18nc("@label", "Url") },    
 
122
    { "http://freedesktop.org/standards/xesam/1.0/core#size", i18nc("@label", "Size") },
 
123
    { "http://freedesktop.org/standards/xesam/1.0/core#aperture", i18nc("@label", "Aperture") },
 
124
    { "http://freedesktop.org/standards/xesam/1.0/core#meteringMode", i18nc("@label", "Metering Mode") },
 
125
    { "http://freedesktop.org/standards/xesam/1.0/core#35mmEquivalent", i18nc("@label", "35mm Equivalent") },
 
126
    { "http://freedesktop.org/standards/xesam/1.0/core#fileExtension", i18nc("@label", "File Extension") },
 
127
    { "http://freedesktop.org/standards/xesam/1.0/core#name", i18nc("@label", "Name") },
 
128
    { "http://freedesktop.org/standards/xesam/1.0/core#exposureTime", i18nc("@label", "Exposure Time") }
 
129
    };
 
130
 
 
131
    // search if the key exists and get its value
 
132
    int top = 0;
 
133
    int bottom = sizeof(keys) / sizeof(MetaKey) - 1;
 
134
    while (top <= bottom) {
 
135
        const int result = key.compare(keys[top].key);
 
136
        if (result == 0) {
 
137
            text = keys[top].text;
 
138
            return true;
 
139
        }
 
140
        top++;
 
141
    }
 
142
 
 
143
    return false;
 
144
}
 
145
 
 
146
K_EXPORT_PLASMA_DATAENGINE(metadata, MetaDataEngine)
 
147
 
 
148
#include "metadata_engine.moc"