KFileMetaData

usermetadata.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "usermetadata.h"
8#include "xattr_p.h"
9
10
11using namespace KFileMetaData;
12
13class KFileMetaData::UserMetaDataPrivate
14{
15public:
16 QString filePath;
17};
18
19UserMetaData::UserMetaData(const QString& filePath)
20 : d(new UserMetaDataPrivate)
21{
22 d->filePath = filePath;
23}
24
25UserMetaData::UserMetaData(const UserMetaData& rhs)
26 : d(new UserMetaDataPrivate(*rhs.d))
27{
28}
29
30UserMetaData::~UserMetaData() = default;
31
32const UserMetaData& UserMetaData::operator=(const UserMetaData& rhs)
33{
34 d->filePath = rhs.d->filePath;
35 return *this;
36}
37
38QString UserMetaData::filePath() const
39{
40 return d->filePath;
41}
42
43UserMetaData::Error UserMetaData::setTags(const QStringList& tags)
44{
45 return setAttribute(QStringLiteral("xdg.tags"), !tags.empty() ? tags.join(QLatin1Char(',')) : QString());
46}
47
48QStringList UserMetaData::tags() const
49{
50 QString value;
51
52 k_getxattr(d->filePath, QStringLiteral("xdg.tags"), &value);
53 return value.split(QLatin1Char(','), Qt::SkipEmptyParts);
54}
55
56int UserMetaData::rating() const
57{
58 return attribute(QStringLiteral("baloo.rating")).toInt();
59}
60
61UserMetaData::Error UserMetaData::setRating(int rating)
62{
63 return setAttribute(QStringLiteral("baloo.rating"), rating ? QString::number(rating) : QString());
64}
65
66QString UserMetaData::userComment() const
67{
68 return attribute(QStringLiteral("xdg.comment"));
69}
70
71UserMetaData::Error UserMetaData::setUserComment(const QString& userComment)
72{
73 return setAttribute(QStringLiteral("xdg.comment"), userComment);
74}
75
76QUrl UserMetaData::originUrl() const
77{
78 return QUrl(attribute(QStringLiteral("xdg.origin.url")));
79}
80
81UserMetaData::Error UserMetaData::setOriginUrl(const QUrl &originUrl)
82{
83 return setAttribute(QStringLiteral("xdg.origin.url"), !originUrl.isEmpty() ? originUrl.toString(): QString());
84}
85
86QString UserMetaData::originEmailSubject() const
87{
88 return attribute(QStringLiteral("xdg.origin.email.subject"));
89}
90
91UserMetaData::Error UserMetaData::setOriginEmailSubject(const QString &originEmailSubject)
92{
93 return setAttribute(QStringLiteral("xdg.origin.email.subject"), originEmailSubject);
94}
95
96QString UserMetaData::originEmailSender() const
97{
98 return attribute(QStringLiteral("xdg.origin.email.sender"));
99}
100
101UserMetaData::Error UserMetaData::setOriginEmailSender(const QString &originEmailSender)
102{
103 return setAttribute(QStringLiteral("xdg.origin.email.sender"), originEmailSender);
104}
105
106QString UserMetaData::originEmailMessageId() const
107{
108 return attribute(QStringLiteral("xdg.origin.email.message-id"));
109}
110
111UserMetaData::Error UserMetaData::setOriginEmailMessageId(const QString &originEmailMessageId)
112{
113 return setAttribute(QStringLiteral("xdg.origin.email.message-id"), originEmailMessageId);
114}
115
116UserMetaData::Error UserMetaData::setAttribute(const QString& key, const QString& value)
117{
118 int result;
119 if (!value.isEmpty()) {
120 result = k_setxattr(d->filePath, key, value);
121 } else {
122 result = k_removexattr(d->filePath, key);
123 }
124
125 if (result != 0) {
126 switch (result) {
127#ifdef Q_OS_UNIX
128 case EDQUOT:
129#endif
130 case ENOSPC:
131 return NoSpace;
132 case ENOTSUP:
133 return NotSupported;
134 case EACCES:
135 case EPERM:
136 return MissingPermission;
137#ifdef Q_OS_WIN
138 case ERROR_FILENAME_EXCED_RANGE:
139#endif
140 case ENAMETOOLONG:
141 case ERANGE:
142 return NameToolong;
143 case E2BIG:
144 return ValueTooBig;
145 default:
146 return UnknownError;
147 }
148 }
149 return NoError;
150}
151
152#if KFILEMETADATA_BUILD_DEPRECATED_SINCE(6, 2)
154{
155 return std::as_const(*this).hasAttribute(key);
156}
157#endif
158
160{
161 return k_hasAttribute(d->filePath, key);
162}
163
164#if KFILEMETADATA_BUILD_DEPRECATED_SINCE(6, 2)
166{
167 return std::as_const(*this).attribute(key);
168}
169#endif
170
172{
173 QString value;
174 k_getxattr(d->filePath, key, &value);
175
176 return value;
177}
178
179bool UserMetaData::isSupported() const
180{
181 return k_isSupported(d->filePath);
182}
183
185{
186 return k_queryAttributes(d->filePath, attributes);
187}
The UserMetaData class can be used to read and set user meta data of files.
bool hasAttribute(const QString &name)
Attributes queryAttributes(Attributes attributes=Attribute::Any) const
Query list of available attributes.
@ NameToolong
The attribute name is too long (255 bytes for Linux VFS)
@ NoSpace
There is insufficient space remaining to store the extended attribute.
@ UnknownError
An error that's not currently handled specifically.
@ MissingPermission
Process doesn't have write permission to the file or the file is marked append-only.
@ ValueTooBig
The value size exceeds the maximum size allowed per-value (64 kB for Linux VFS)
@ NotSupported
Underlying filesystem does not provide extended attributes features.
QString attribute(const QString &name)
The KFileMetaData namespace.
bool empty() const const
bool isEmpty() const const
QString number(double n, char format, int precision)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
int toInt(bool *ok, int base) const const
QString join(QChar separator) const const
SkipEmptyParts
bool isEmpty() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.