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 char charbuf[256];
58 sprintf(charbuf, "%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(QLatin1Char('/'), 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
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
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
178{
179 if (url.isLocalFile() || !url.scheme().startsWith(QLatin1String("http"))) {
180 return QString();
181 }
182
183 return FavIconsCache::instance()->iconForUrl(url);
184}
185
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(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(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("Empty", 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(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
237{
238 if (!url.isValid() || url.isRelative()) {
239 return QUrl();
240 }
241
242 QUrl u(url);
243 if (url.hasQuery()) {
244 u.setQuery(QString());
245 return u;
246 }
247 if (url.hasFragment()) {
248 u.setFragment(QString());
249 }
250 u = u.adjusted(QUrl::StripTrailingSlash); /// don't combine with the line below
252}
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:186
KIOCORE_EXPORT QString convertSeconds(unsigned int seconds)
Convert seconds to a string representing number of days, hours, minutes and seconds.
Definition global.cpp:71
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:156
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:62
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:236
KIOCORE_EXPORT QString favIconForUrl(const QUrl &url)
Return the "favicon" (see http://www.favicon.com) for the given url, if available.
Definition global.cpp:177
CacheControl
Specifies how to use the cache.
Definition global.h:223
@ CC_Cache
Use cached entry if available.
Definition global.h:225
@ CC_Verify
Validate cached entry with remote site if expired.
Definition global.h:226
@ CC_Reload
Always fetch from remote site.
Definition global.h:228
@ CC_CacheOnly
Fail request if not in cache.
Definition global.h:224
@ CC_Refresh
Always validate cached entry with remote site.
Definition global.h:227
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:120
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:87
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:113
KIOCORE_EXPORT KIO::CacheControl parseCacheControl(const QString &cacheControl)
Parses the string representation of the cache control option.
Definition global.cpp:132
QMimeType mimeTypeForUrl(const QUrl &url) const const
bool inherits(const QString &mimeTypeName) const const
bool isEmpty() const const
qsizetype length() const const
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 May 17 2024 11:53:29 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.