File indexing completed on 2024-04-14 03:49:48
0001 /* 0002 This file is part of the KDE Baloo Project 0003 SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in> 0004 0005 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "file.h" 0009 #include "global.h" 0010 #include "database.h" 0011 #include "transaction.h" 0012 #include "idutils.h" 0013 #include "propertydata.h" 0014 0015 #include <QJsonDocument> 0016 #include <QFileInfo> 0017 #include <QJsonObject> 0018 0019 using namespace Baloo; 0020 0021 class BALOO_CORE_NO_EXPORT File::Private { 0022 public: 0023 QString url; 0024 KFileMetaData::PropertyMultiMap propertyMap; 0025 }; 0026 0027 File::File() 0028 : d(new Private) 0029 { 0030 } 0031 0032 File::File(const File& f) 0033 : d(new Private(*f.d)) 0034 { 0035 } 0036 0037 File::File(const QString& url) 0038 : d(new Private) 0039 { 0040 d->url = QFileInfo(url).canonicalFilePath(); 0041 } 0042 0043 File::~File() = default; 0044 0045 const File& File::operator=(const File& f) 0046 { 0047 if (&f != this) { 0048 *d = *f.d; 0049 } 0050 return *this; 0051 } 0052 0053 QString File::path() const 0054 { 0055 return d->url; 0056 } 0057 0058 KFileMetaData::PropertyMultiMap File::properties() const 0059 { 0060 return d->propertyMap; 0061 } 0062 0063 QVariant File::property(KFileMetaData::Property::Property property) const 0064 { 0065 return d->propertyMap.value(property); 0066 } 0067 0068 bool File::load(const QString& url) 0069 { 0070 d->url = QFileInfo(url).canonicalFilePath(); 0071 d->propertyMap.clear(); 0072 return load(); 0073 } 0074 0075 bool File::load() 0076 { 0077 const QString& url = d->url; 0078 if (url.isEmpty() || !QFile::exists(url)) { 0079 return false; 0080 } 0081 0082 Database *db = globalDatabaseInstance(); 0083 if (!db->open(Database::ReadOnlyDatabase)) { 0084 return false; 0085 } 0086 0087 quint64 id = filePathToId(QFile::encodeName(d->url)); 0088 if (!id) { 0089 return false; 0090 } 0091 0092 QByteArray arr; 0093 { 0094 Transaction tr(db, Transaction::ReadOnly); 0095 arr = tr.documentData(id); 0096 } 0097 // Ignore empty JSON documents, i.e. "" or "{}" 0098 if (arr.isEmpty() || arr.size() <= 2) { 0099 return false; 0100 } 0101 0102 const QJsonDocument jdoc = QJsonDocument::fromJson(arr); 0103 d->propertyMap = Baloo::jsonToPropertyMap(jdoc.object()); 0104 0105 return true; 0106 }