| 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 | |
| 11 | using namespace KFileMetaData; |
| 12 | |
| 13 | class KFileMetaData::UserMetaDataPrivate |
| 14 | { |
| 15 | public: |
| 16 | QString filePath; |
| 17 | }; |
| 18 | |
| 19 | UserMetaData::UserMetaData(const QString& filePath) |
| 20 | : d(new UserMetaDataPrivate) |
| 21 | { |
| 22 | d->filePath = filePath; |
| 23 | } |
| 24 | |
| 25 | UserMetaData::UserMetaData(const UserMetaData& rhs) |
| 26 | : d(new UserMetaDataPrivate(*rhs.d)) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | UserMetaData::~UserMetaData() = default; |
| 31 | |
| 32 | const UserMetaData& UserMetaData::operator=(const UserMetaData& rhs) |
| 33 | { |
| 34 | d->filePath = rhs.d->filePath; |
| 35 | return *this; |
| 36 | } |
| 37 | |
| 38 | QString UserMetaData::filePath() const |
| 39 | { |
| 40 | return d->filePath; |
| 41 | } |
| 42 | |
| 43 | UserMetaData::Error UserMetaData::setTags(const QStringList& tags) |
| 44 | { |
| 45 | return setAttribute(QStringLiteral("xdg.tags" ), value: !tags.empty() ? tags.join(sep: QLatin1Char(',')) : QString()); |
| 46 | } |
| 47 | |
| 48 | QStringList UserMetaData::tags() const |
| 49 | { |
| 50 | QString value; |
| 51 | |
| 52 | k_getxattr(path: d->filePath, QStringLiteral("xdg.tags" ), value: &value); |
| 53 | return value.split(sep: QLatin1Char(','), behavior: Qt::SkipEmptyParts); |
| 54 | } |
| 55 | |
| 56 | int UserMetaData::rating() const |
| 57 | { |
| 58 | return attribute(QStringLiteral("baloo.rating" )).toInt(); |
| 59 | } |
| 60 | |
| 61 | UserMetaData::Error UserMetaData::setRating(int rating) |
| 62 | { |
| 63 | return setAttribute(QStringLiteral("baloo.rating" ), value: rating ? QString::number(rating) : QString()); |
| 64 | } |
| 65 | |
| 66 | QString UserMetaData::() const |
| 67 | { |
| 68 | return attribute(QStringLiteral("xdg.comment" )); |
| 69 | } |
| 70 | |
| 71 | UserMetaData::Error UserMetaData::(const QString& ) |
| 72 | { |
| 73 | return setAttribute(QStringLiteral("xdg.comment" ), value: userComment); |
| 74 | } |
| 75 | |
| 76 | QUrl UserMetaData::originUrl() const |
| 77 | { |
| 78 | return QUrl(attribute(QStringLiteral("xdg.origin.url" ))); |
| 79 | } |
| 80 | |
| 81 | UserMetaData::Error UserMetaData::setOriginUrl(const QUrl &originUrl) |
| 82 | { |
| 83 | return setAttribute(QStringLiteral("xdg.origin.url" ), value: !originUrl.isEmpty() ? originUrl.toString(): QString()); |
| 84 | } |
| 85 | |
| 86 | QString UserMetaData::originEmailSubject() const |
| 87 | { |
| 88 | return attribute(QStringLiteral("xdg.origin.email.subject" )); |
| 89 | } |
| 90 | |
| 91 | UserMetaData::Error UserMetaData::setOriginEmailSubject(const QString &originEmailSubject) |
| 92 | { |
| 93 | return setAttribute(QStringLiteral("xdg.origin.email.subject" ), value: originEmailSubject); |
| 94 | } |
| 95 | |
| 96 | QString UserMetaData::originEmailSender() const |
| 97 | { |
| 98 | return attribute(QStringLiteral("xdg.origin.email.sender" )); |
| 99 | } |
| 100 | |
| 101 | UserMetaData::Error UserMetaData::setOriginEmailSender(const QString &originEmailSender) |
| 102 | { |
| 103 | return setAttribute(QStringLiteral("xdg.origin.email.sender" ), value: originEmailSender); |
| 104 | } |
| 105 | |
| 106 | QString UserMetaData::originEmailMessageId() const |
| 107 | { |
| 108 | return attribute(QStringLiteral("xdg.origin.email.message-id" )); |
| 109 | } |
| 110 | |
| 111 | UserMetaData::Error UserMetaData::setOriginEmailMessageId(const QString &originEmailMessageId) |
| 112 | { |
| 113 | return setAttribute(QStringLiteral("xdg.origin.email.message-id" ), value: originEmailMessageId); |
| 114 | } |
| 115 | |
| 116 | UserMetaData::Error UserMetaData::setAttribute(const QString& key, const QString& value) |
| 117 | { |
| 118 | int result; |
| 119 | if (!value.isEmpty()) { |
| 120 | result = k_setxattr(path: d->filePath, name: key, value); |
| 121 | } else { |
| 122 | result = k_removexattr(path: d->filePath, name: 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) |
| 153 | bool UserMetaData::hasAttribute(const QString& key) |
| 154 | { |
| 155 | return std::as_const(t&: *this).hasAttribute(name: key); |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | bool UserMetaData::hasAttribute(const QString &key) const |
| 160 | { |
| 161 | return k_hasAttribute(path: d->filePath, name: key); |
| 162 | } |
| 163 | |
| 164 | #if KFILEMETADATA_BUILD_DEPRECATED_SINCE(6, 2) |
| 165 | QString UserMetaData::attribute(const QString &key) |
| 166 | { |
| 167 | return std::as_const(t&: *this).attribute(name: key); |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | QString UserMetaData::attribute(const QString& key) const |
| 172 | { |
| 173 | QString value; |
| 174 | k_getxattr(path: d->filePath, name: key, value: &value); |
| 175 | |
| 176 | return value; |
| 177 | } |
| 178 | |
| 179 | bool UserMetaData::isSupported() const |
| 180 | { |
| 181 | return k_isSupported(path: d->filePath); |
| 182 | } |
| 183 | |
| 184 | UserMetaData::Attributes UserMetaData::queryAttributes(UserMetaData::Attributes attributes) const |
| 185 | { |
| 186 | return k_queryAttributes(path: d->filePath, attributes); |
| 187 | } |
| 188 | |