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

source code of kio/src/core/global.cpp