Baloo

file.cpp
1/*
2 This file is part of the KDE Baloo Project
3 SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
4
5 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "file.h"
9#include "global.h"
10#include "database.h"
11#include "transaction.h"
12#include "idutils.h"
13#include "propertydata.h"
14
15#include <QJsonDocument>
16#include <QFileInfo>
17#include <QJsonObject>
18
19using namespace Baloo;
20
21class BALOO_CORE_NO_EXPORT File::Private {
22public:
23 QString url;
24 KFileMetaData::PropertyMultiMap propertyMap;
25};
26
27File::File()
28 : d(new Private)
29{
30}
31
32File::File(const File& f)
33 : d(new Private(*f.d))
34{
35}
36
37File::File(const QString& url)
38 : d(new Private)
39{
40 d->url = QFileInfo(url).canonicalFilePath();
41}
42
43File::~File() = default;
44
45const File& File::operator=(const File& f)
46{
47 if (&f != this) {
48 *d = *f.d;
49 }
50 return *this;
51}
52
54{
55 return d->url;
56}
57
58KFileMetaData::PropertyMultiMap File::properties() const
59{
60 return d->propertyMap;
61}
62
63QVariant File::property(KFileMetaData::Property::Property property) const
64{
65 return d->propertyMap.value(property);
66}
67
68bool File::load(const QString& url)
69{
70 d->url = QFileInfo(url).canonicalFilePath();
71 d->propertyMap.clear();
72 return load();
73}
74
75bool File::load()
76{
77 const QString& url = d->url;
78 if (url.isEmpty() || !QFile::exists(url)) {
79 return false;
80 }
81
82 Database *db = globalDatabaseInstance();
83 if (!db->open(Database::ReadOnlyDatabase)) {
84 return false;
85 }
86
87 quint64 id = filePathToId(QFile::encodeName(d->url));
88 if (!id) {
89 return false;
90 }
91
92 QByteArray arr;
93 {
94 Transaction tr(db, Transaction::ReadOnly);
95 arr = tr.documentData(id);
96 }
97 // Ignore empty JSON documents, i.e. "" or "{}"
98 if (arr.isEmpty() || arr.size() <= 2) {
99 return false;
100 }
101
102 const QJsonDocument jdoc = QJsonDocument::fromJson(arr);
103 d->propertyMap = Baloo::jsonToPropertyMap(jdoc.object());
104
105 return true;
106}
Provides access to all File Metadata.
Definition file.h:26
QString path() const
The local url of the file.
Definition file.cpp:53
KFileMetaData::PropertyMultiMap properties() const
Gives a variant map of the properties that have been extracted from the file by the indexer.
Definition file.cpp:58
Implements storage for docIds without any associated data Instantiated for:
Definition coding.cpp:11
bool isEmpty() const const
qsizetype size() const const
QByteArray encodeName(const QString &fileName)
bool exists() const const
QString canonicalFilePath() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.