Baloo

file.cpp
1 /*
2  This file is part of the KDE Baloo Project
3  SPDX-FileCopyrightText: 2013 Vishesh Handa <[email protected]>
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 
19 using namespace Baloo;
20 
21 class BALOO_CORE_NO_EXPORT File::Private {
22 public:
23  QString url;
24  KFileMetaData::PropertyMultiMap propertyMap;
25 };
26 
27 File::File()
28  : d(new Private)
29 {
30 }
31 
32 File::File(const File& f)
33  : d(new Private(*f.d))
34 {
35 }
36 
37 File::File(const QString& url)
38  : d(new Private)
39 {
40  d->url = QFileInfo(url).canonicalFilePath();
41 }
42 
43 File::~File()
44 {
45  delete d;
46 }
47 
48 const File& File::operator=(const File& f)
49 {
50  if (&f != this) {
51  *d = *f.d;
52  }
53  return *this;
54 }
55 
57 {
58  return d->url;
59 }
60 
61 #if BALOO_CORE_BUILD_DEPRECATED_SINCE(5, 91)
62 KFileMetaData::PropertyMap File::properties() const
63 #else
64 KFileMetaData::PropertyMultiMap File::properties() const
65 #endif
66 {
67  return d->propertyMap;
68 }
69 
70 QVariant File::property(KFileMetaData::Property::Property property) const
71 {
72  return d->propertyMap.value(property);
73 }
74 
75 bool File::load(const QString& url)
76 {
77  d->url = QFileInfo(url).canonicalFilePath();
78  d->propertyMap.clear();
79  return load();
80 }
81 
82 bool File::load()
83 {
84  const QString& url = d->url;
85  if (url.isEmpty() || !QFile::exists(url)) {
86  return false;
87  }
88 
89  Database *db = globalDatabaseInstance();
90  if (!db->open(Database::ReadOnlyDatabase)) {
91  return false;
92  }
93 
94  quint64 id = filePathToId(QFile::encodeName(d->url));
95  if (!id) {
96  return false;
97  }
98 
99  QByteArray arr;
100  {
101  Transaction tr(db, Transaction::ReadOnly);
102  arr = tr.documentData(id);
103  }
104  // Ignore empty JSON documents, i.e. "" or "{}"
105  if (arr.isEmpty() || arr.size() <= 2) {
106  return false;
107  }
108 
109  const QJsonDocument jdoc = QJsonDocument::fromJson(arr);
110  d->propertyMap = Baloo::jsonToPropertyMap(jdoc.object());
111 
112  return true;
113 }
Provides access to all File Metadata.
Definition: file.h:23
QJsonObject object() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QByteArray encodeName(const QString &fileName)
QString path() const
The local url of the file.
Definition: file.cpp:56
bool exists() const const
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
bool isEmpty() const const
bool isEmpty() const const
int size() const const
KFileMetaData::PropertyMultiMap properties() const
Gives a variant map of the properties that have been extracted from the file by the indexer.
Definition: file.cpp:64
QString canonicalFilePath() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.