| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-only |
| 6 | */ |
| 7 | |
| 8 | #include "global.h" |
| 9 | #include "faviconscache_p.h" |
| 10 | #include "kioglobal_p.h" |
| 11 | |
| 12 | #include <KConfig> |
| 13 | #include <KConfigGroup> |
| 14 | #include <KFileUtils> |
| 15 | #include <KFormat> |
| 16 | #include <KLocalizedString> |
| 17 | #include <KSharedConfig> |
| 18 | #include <QMimeDatabase> |
| 19 | #include <QUrl> |
| 20 | #include <kfileitem.h> |
| 21 | #include <kprotocolinfo.h> |
| 22 | |
| 23 | #include "kiocoredebug.h" |
| 24 | |
| 25 | KFormat::BinaryUnitDialect _k_loadBinaryDialect(); |
| 26 | Q_GLOBAL_STATIC_WITH_ARGS(KFormat::BinaryUnitDialect, _k_defaultBinaryDialect, (_k_loadBinaryDialect())) |
| 27 | |
| 28 | KFormat::BinaryUnitDialect _k_loadBinaryDialect() |
| 29 | { |
| 30 | KConfigGroup mainGroup(KSharedConfig::openConfig(), QStringLiteral("Locale" )); |
| 31 | |
| 32 | KFormat::BinaryUnitDialect dialect(KFormat::BinaryUnitDialect(mainGroup.readEntry(key: "BinaryUnitDialect" , defaultValue: int(KFormat::DefaultBinaryDialect)))); |
| 33 | dialect = static_cast<KFormat::BinaryUnitDialect>(mainGroup.readEntry(key: "BinaryUnitDialect" , defaultValue: int(dialect))); |
| 34 | |
| 35 | // Error checking |
| 36 | if (dialect <= KFormat::DefaultBinaryDialect || dialect > KFormat::LastBinaryDialect) { |
| 37 | dialect = KFormat::IECBinaryDialect; |
| 38 | } |
| 39 | |
| 40 | return dialect; |
| 41 | } |
| 42 | |
| 43 | KIOCORE_EXPORT QString KIO::convertSize(KIO::filesize_t fileSize) |
| 44 | { |
| 45 | const KFormat::BinaryUnitDialect dialect = *_k_defaultBinaryDialect(); |
| 46 | |
| 47 | return KFormat().formatByteSize(size: fileSize, precision: 1, dialect); |
| 48 | } |
| 49 | |
| 50 | KIOCORE_EXPORT QString KIO::convertSizeFromKiB(KIO::filesize_t kibSize) |
| 51 | { |
| 52 | return convertSize(fileSize: kibSize * 1024); |
| 53 | } |
| 54 | |
| 55 | KIOCORE_EXPORT QString KIO::number(KIO::filesize_t size) |
| 56 | { |
| 57 | return QString::number(size); |
| 58 | } |
| 59 | |
| 60 | KIOCORE_EXPORT unsigned int KIO::calculateRemainingSeconds(KIO::filesize_t totalSize, KIO::filesize_t processedSize, KIO::filesize_t speed) |
| 61 | { |
| 62 | if ((speed != 0) && (totalSize != 0)) { |
| 63 | return (totalSize - processedSize) / speed; |
| 64 | } else { |
| 65 | return 0; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | KIOCORE_EXPORT QString KIO::convertSeconds(unsigned int seconds) |
| 70 | { |
| 71 | unsigned int days = seconds / 86400; |
| 72 | unsigned int hours = (seconds - (days * 86400)) / 3600; |
| 73 | unsigned int mins = (seconds - (days * 86400) - (hours * 3600)) / 60; |
| 74 | seconds = (seconds - (days * 86400) - (hours * 3600) - (mins * 60)); |
| 75 | |
| 76 | const QTime time(hours, mins, seconds); |
| 77 | const QString timeStr(time.toString(QStringLiteral("hh:mm:ss" ))); |
| 78 | if (days > 0) { |
| 79 | return i18np("1 day %2" , "%1 days %2" , days, timeStr); |
| 80 | } else { |
| 81 | return timeStr; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | KIOCORE_EXPORT QString KIO::itemsSummaryString(uint items, uint files, uint dirs, KIO::filesize_t size, bool showSize) |
| 86 | { |
| 87 | if (files == 0 && dirs == 0 && items == 0) { |
| 88 | return i18np("%1 Item" , "%1 Items" , 0); |
| 89 | } |
| 90 | |
| 91 | QString summary; |
| 92 | const QString foldersText = i18np("1 Folder" , "%1 Folders" , dirs); |
| 93 | const QString filesText = i18np("1 File" , "%1 Files" , files); |
| 94 | if (files > 0 && dirs > 0) { |
| 95 | summary = showSize ? i18nc("folders, files (size)" , "%1, %2 (%3)" , foldersText, filesText, KIO::convertSize(size)) |
| 96 | : i18nc("folders, files" , "%1, %2" , foldersText, filesText); |
| 97 | } else if (files > 0) { |
| 98 | summary = showSize ? i18nc("files (size)" , "%1 (%2)" , filesText, KIO::convertSize(size)) : filesText; |
| 99 | } else if (dirs > 0) { |
| 100 | summary = foldersText; |
| 101 | } |
| 102 | |
| 103 | if (items > dirs + files) { |
| 104 | const QString itemsText = i18np("%1 Item" , "%1 Items" , items); |
| 105 | summary = summary.isEmpty() ? itemsText : i18nc("items: folders, files (size)" , "%1: %2" , itemsText, summary); |
| 106 | } |
| 107 | |
| 108 | return summary; |
| 109 | } |
| 110 | |
| 111 | KIOCORE_EXPORT QString KIO::encodeFileName(const QString &_str) |
| 112 | { |
| 113 | QString str(_str); |
| 114 | str.replace(before: QLatin1Char('/'), after: QChar(0x2044)); // "Fraction slash" |
| 115 | return str; |
| 116 | } |
| 117 | |
| 118 | KIOCORE_EXPORT QString KIO::decodeFileName(const QString &_str) |
| 119 | { |
| 120 | // Nothing to decode. "Fraction slash" is fine in filenames. |
| 121 | return _str; |
| 122 | } |
| 123 | |
| 124 | // Utility functions |
| 125 | |
| 126 | #if KIOCORE_BUILD_DEPRECATED_SINCE(6, 6) |
| 127 | KIO::CacheControl KIO::parseCacheControl(const QString &cacheControl) |
| 128 | { |
| 129 | QString tmp = cacheControl.toLower(); |
| 130 | |
| 131 | if (tmp == QLatin1String("cacheonly" )) { |
| 132 | return KIO::CC_CacheOnly; |
| 133 | } |
| 134 | if (tmp == QLatin1String("cache" )) { |
| 135 | return KIO::CC_Cache; |
| 136 | } |
| 137 | if (tmp == QLatin1String("verify" )) { |
| 138 | return KIO::CC_Verify; |
| 139 | } |
| 140 | if (tmp == QLatin1String("refresh" )) { |
| 141 | return KIO::CC_Refresh; |
| 142 | } |
| 143 | if (tmp == QLatin1String("reload" )) { |
| 144 | return KIO::CC_Reload; |
| 145 | } |
| 146 | |
| 147 | qCDebug(KIO_CORE) << "unrecognized Cache control option:" << cacheControl; |
| 148 | return KIO::CC_Verify; |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | #if KIOCORE_BUILD_DEPRECATED_SINCE(6, 6) |
| 153 | QString KIO::getCacheControlString(KIO::CacheControl cacheControl) |
| 154 | { |
| 155 | if (cacheControl == KIO::CC_CacheOnly) { |
| 156 | return QStringLiteral("CacheOnly" ); |
| 157 | } |
| 158 | if (cacheControl == KIO::CC_Cache) { |
| 159 | return QStringLiteral("Cache" ); |
| 160 | } |
| 161 | if (cacheControl == KIO::CC_Verify) { |
| 162 | return QStringLiteral("Verify" ); |
| 163 | } |
| 164 | if (cacheControl == KIO::CC_Refresh) { |
| 165 | return QStringLiteral("Refresh" ); |
| 166 | } |
| 167 | if (cacheControl == KIO::CC_Reload) { |
| 168 | return QStringLiteral("Reload" ); |
| 169 | } |
| 170 | qCDebug(KIO_CORE) << "unrecognized Cache control enum value:" << cacheControl; |
| 171 | return QString(); |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | QString KIO::favIconForUrl(const QUrl &url) |
| 176 | { |
| 177 | if (url.isLocalFile() || !url.scheme().startsWith(s: QLatin1String("http" ))) { |
| 178 | return QString(); |
| 179 | } |
| 180 | |
| 181 | return FavIconsCache::instance()->iconForUrl(url); |
| 182 | } |
| 183 | |
| 184 | QString KIO::iconNameForUrl(const QUrl &url) |
| 185 | { |
| 186 | if (url.scheme().isEmpty()) { // empty URL or relative URL (e.g. '~') |
| 187 | return QStringLiteral("unknown" ); |
| 188 | } |
| 189 | QMimeDatabase db; |
| 190 | const QMimeType mt = db.mimeTypeForUrl(url); |
| 191 | QString iconName; |
| 192 | |
| 193 | if (url.isLocalFile()) { |
| 194 | // Check to see whether it's an xdg location (e.g. Pictures folder) |
| 195 | if (mt.inherits(QStringLiteral("inode/directory" ))) { |
| 196 | iconName = KIOPrivate::iconForStandardPath(localDirectory: url.toLocalFile()); |
| 197 | } |
| 198 | |
| 199 | // Let KFileItem::iconName handle things for us |
| 200 | if (iconName.isEmpty()) { |
| 201 | const KFileItem item(url, mt.name()); |
| 202 | iconName = item.iconName(); |
| 203 | } |
| 204 | |
| 205 | } else { |
| 206 | // It's non-local and maybe on a slow filesystem |
| 207 | |
| 208 | // Look for a favicon |
| 209 | if (url.scheme().startsWith(s: QLatin1String("http" ))) { |
| 210 | iconName = favIconForUrl(url); |
| 211 | } |
| 212 | |
| 213 | // Then handle the trash |
| 214 | else if (url.scheme() == QLatin1String("trash" )) { |
| 215 | if (url.path().length() <= 1) { // trash:/ itself |
| 216 | KConfig trashConfig(QStringLiteral("trashrc" ), KConfig::SimpleConfig); |
| 217 | iconName = |
| 218 | trashConfig.group(QStringLiteral("Status" )).readEntry(key: "Empty" , defaultValue: true) ? QStringLiteral("user-trash" ) : QStringLiteral("user-trash-full" ); |
| 219 | } else { // url.path().length() > 1, it's a file/folder under trash:/ |
| 220 | iconName = mt.iconName(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // and other protocols |
| 225 | if (iconName.isEmpty() && (mt.isDefault() || url.path().size() <= 1)) { |
| 226 | iconName = KProtocolInfo::icon(protocol: url.scheme()); |
| 227 | } |
| 228 | } |
| 229 | // if we found nothing, return QMimeType.iconName() |
| 230 | // (which fallbacks to "application-octet-stream" when no MIME type could be determined) |
| 231 | return !iconName.isEmpty() ? iconName : mt.iconName(); |
| 232 | } |
| 233 | |
| 234 | QUrl KIO::upUrl(const QUrl &url) |
| 235 | { |
| 236 | if (!url.isValid() || url.isRelative()) { |
| 237 | return QUrl(); |
| 238 | } |
| 239 | |
| 240 | QUrl u(url); |
| 241 | if (url.hasQuery()) { |
| 242 | u.setQuery(query: QString()); |
| 243 | return u; |
| 244 | } |
| 245 | if (url.hasFragment()) { |
| 246 | u.setFragment(fragment: QString()); |
| 247 | } |
| 248 | u = u.adjusted(options: QUrl::StripTrailingSlash); /// don't combine with the line below |
| 249 | return u.adjusted(options: QUrl::RemoveFilename); |
| 250 | } |
| 251 | |