KIO

global.cpp
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("BinaryUnitDialect", int(KFormat::DefaultBinaryDialect))));
33 dialect = static_cast<KFormat::BinaryUnitDialect>(mainGroup.readEntry("BinaryUnitDialect", int(dialect)));
34
35 // Error checking
36 if (dialect <= KFormat::DefaultBinaryDialect || dialect > KFormat::LastBinaryDialect) {
38 }
39
40 return dialect;
41}
42
44{
45 const KFormat::BinaryUnitDialect dialect = *_k_defaultBinaryDialect();
46
47 return KFormat().formatByteSize(fileSize, 1, dialect);
48}
49
51{
52 return convertSize(kibSize * 1024);
53}
54
56{
57 return QString::number(size);
58}
59
60KIOCORE_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
69KIOCORE_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
85KIOCORE_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
111KIOCORE_EXPORT QString KIO::encodeFileName(const QString &_str)
112{
113 QString str(_str);
114 str.replace(QLatin1Char('/'), QChar(0x2044)); // "Fraction slash"
115 return str;
116}
117
118KIOCORE_EXPORT QString KIO::decodeFileName(const QString &_str)
119{
120 // Nothing to decode. "Fraction slash" is fine in filenames.
121 return _str;
122}
123
124/***************************************************************
125 *
126 * Utility functions
127 *
128 ***************************************************************/
129
130#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 6)
132{
133 QString tmp = cacheControl.toLower();
134
135 if (tmp == QLatin1String("cacheonly")) {
136 return KIO::CC_CacheOnly;
137 }
138 if (tmp == QLatin1String("cache")) {
139 return KIO::CC_Cache;
140 }
141 if (tmp == QLatin1String("verify")) {
142 return KIO::CC_Verify;
143 }
144 if (tmp == QLatin1String("refresh")) {
145 return KIO::CC_Refresh;
146 }
147 if (tmp == QLatin1String("reload")) {
148 return KIO::CC_Reload;
149 }
150
151 qCDebug(KIO_CORE) << "unrecognized Cache control option:" << cacheControl;
152 return KIO::CC_Verify;
153}
154#endif
155
156#if KIOCORE_BUILD_DEPRECATED_SINCE(6, 6)
158{
159 if (cacheControl == KIO::CC_CacheOnly) {
160 return QStringLiteral("CacheOnly");
161 }
162 if (cacheControl == KIO::CC_Cache) {
163 return QStringLiteral("Cache");
164 }
165 if (cacheControl == KIO::CC_Verify) {
166 return QStringLiteral("Verify");
167 }
168 if (cacheControl == KIO::CC_Refresh) {
169 return QStringLiteral("Refresh");
170 }
171 if (cacheControl == KIO::CC_Reload) {
172 return QStringLiteral("Reload");
173 }
174 qCDebug(KIO_CORE) << "unrecognized Cache control enum value:" << cacheControl;
175 return QString();
176}
177#endif
178
180{
181 if (url.isLocalFile() || !url.scheme().startsWith(QLatin1String("http"))) {
182 return QString();
183 }
184
185 return FavIconsCache::instance()->iconForUrl(url);
186}
187
189{
190 if (url.scheme().isEmpty()) { // empty URL or relative URL (e.g. '~')
191 return QStringLiteral("unknown");
192 }
193 QMimeDatabase db;
194 const QMimeType mt = db.mimeTypeForUrl(url);
195 QString iconName;
196
197 if (url.isLocalFile()) {
198 // Check to see whether it's an xdg location (e.g. Pictures folder)
199 if (mt.inherits(QStringLiteral("inode/directory"))) {
200 iconName = KIOPrivate::iconForStandardPath(url.toLocalFile());
201 }
202
203 // Let KFileItem::iconName handle things for us
204 if (iconName.isEmpty()) {
205 const KFileItem item(url, mt.name());
206 iconName = item.iconName();
207 }
208
209 } else {
210 // It's non-local and maybe on a slow filesystem
211
212 // Look for a favicon
213 if (url.scheme().startsWith(QLatin1String("http"))) {
214 iconName = favIconForUrl(url);
215 }
216
217 // Then handle the trash
218 else if (url.scheme() == QLatin1String("trash")) {
219 if (url.path().length() <= 1) { // trash:/ itself
220 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
221 iconName =
222 trashConfig.group(QStringLiteral("Status")).readEntry("Empty", true) ? QStringLiteral("user-trash") : QStringLiteral("user-trash-full");
223 } else { // url.path().length() > 1, it's a file/folder under trash:/
224 iconName = mt.iconName();
225 }
226 }
227
228 // and other protocols
229 if (iconName.isEmpty() && (mt.isDefault() || url.path().size() <= 1)) {
230 iconName = KProtocolInfo::icon(url.scheme());
231 }
232 }
233 // if we found nothing, return QMimeType.iconName()
234 // (which fallbacks to "application-octet-stream" when no MIME type could be determined)
235 return !iconName.isEmpty() ? iconName : mt.iconName();
236}
237
239{
240 if (!url.isValid() || url.isRelative()) {
241 return QUrl();
242 }
243
244 QUrl u(url);
245 if (url.hasQuery()) {
246 u.setQuery(QString());
247 return u;
248 }
249 if (url.hasFragment()) {
250 u.setFragment(QString());
251 }
252 u = u.adjusted(QUrl::StripTrailingSlash); /// don't combine with the line below
254}
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
A KFileItem is a generic class to handle a file, local or remote.
Definition kfileitem.h:36
QString formatByteSize(double size, int precision=1, KFormat::BinaryUnitDialect dialect=KFormat::DefaultBinaryDialect, KFormat::BinarySizeUnits units=KFormat::DefaultBinaryUnits) const
DefaultBinaryDialect
static QString icon(const QString &protocol)
Returns the name of the icon, associated with the specified protocol.
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KIOCORE_EXPORT QString convertSizeFromKiB(KIO::filesize_t kibSize)
Converts size from kibi-bytes (2^10) to the string representation.
Definition global.cpp:50
KIOCORE_EXPORT QString iconNameForUrl(const QUrl &url)
Return the icon name for a URL.
Definition global.cpp:188
KIOCORE_EXPORT QString convertSeconds(unsigned int seconds)
Convert seconds to a string representing number of days, hours, minutes and seconds.
Definition global.cpp:69
KIOCORE_EXPORT QString number(KIO::filesize_t size)
Converts a size to a string representation Not unlike QString::number(...)
Definition global.cpp:55
KIOCORE_EXPORT QString getCacheControlString(KIO::CacheControl cacheControl)
Returns a string representation of the given cache control method.
Definition global.cpp:157
KIOCORE_EXPORT QString convertSize(KIO::filesize_t size)
Converts size from bytes to the string representation.
Definition global.cpp:43
KIOCORE_EXPORT unsigned int calculateRemainingSeconds(KIO::filesize_t totalSize, KIO::filesize_t processedSize, KIO::filesize_t speed)
Calculates remaining time in seconds from total size, processed size and speed.
Definition global.cpp:60
KIOCORE_EXPORT QUrl upUrl(const QUrl &url)
This function is useful to implement the "Up" button in a file manager for example.
Definition global.cpp:238
KIOCORE_EXPORT QString favIconForUrl(const QUrl &url)
Return the "favicon" (see http://www.favicon.com) for the given url, if available.
Definition global.cpp:179
CacheControl
Specifies how to use the cache.
Definition global.h:226
@ CC_Cache
Use cached entry if available.
Definition global.h:228
@ CC_Verify
Validate cached entry with remote site if expired.
Definition global.h:229
@ CC_Reload
Always fetch from remote site.
Definition global.h:231
@ CC_CacheOnly
Fail request if not in cache.
Definition global.h:227
@ CC_Refresh
Always validate cached entry with remote site.
Definition global.h:230
qulonglong filesize_t
64-bit file size
Definition global.h:35
KIOCORE_EXPORT QString decodeFileName(const QString &str)
Decodes (from the filename to the text displayed) This doesn't do anything anymore,...
Definition global.cpp:118
KIOCORE_EXPORT QString itemsSummaryString(uint items, uint files, uint dirs, KIO::filesize_t size, bool showSize)
Helper for showing information about a set of files and directories.
Definition global.cpp:85
KIOCORE_EXPORT QString encodeFileName(const QString &str)
Encodes (from the text displayed to the real filename) This translates '/' into a "unicode fraction s...
Definition global.cpp:111
KIOCORE_EXPORT KIO::CacheControl parseCacheControl(const QString &cacheControl)
Parses the string representation of the cache control option.
Definition global.cpp:131
QMimeType mimeTypeForUrl(const QUrl &url) const const
bool inherits(const QString &mimeTypeName) const const
bool isEmpty() const const
qsizetype length() const const
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString toLower() const const
QString toString(QStringView format) const const
StripTrailingSlash
QUrl adjusted(FormattingOptions options) const const
bool hasFragment() const const
bool hasQuery() const const
bool isLocalFile() const const
bool isRelative() const const
bool isValid() const const
QString path(ComponentFormattingOptions options) const const
QString scheme() const const
void setFragment(const QString &fragment, ParsingMode mode)
void setQuery(const QString &query, ParsingMode mode)
QString toLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:04:58 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.