10#include "kcoreurlnavigator.h"
15#include <KLocalizedString>
17#include <kprotocolinfo.h>
21#include <QMimeDatabase>
31class KCoreUrlNavigatorPrivate
36 ~KCoreUrlNavigatorPrivate()
44 bool isCompressedPath(
const QUrl &path,
const QStringList &archiveMimetypes)
const;
53 int adjustedHistoryIndex(
int historyIndex)
const;
58 int m_historyIndex = 0;
66bool KCoreUrlNavigatorPrivate::isCompressedPath(
const QUrl &url,
const QStringList &archiveMimetypes)
const
70 return std::any_of(archiveMimetypes.
begin(), archiveMimetypes.
end(), [mime](
const QString &archiveType) {
71 return mime.inherits(archiveType);
75int KCoreUrlNavigatorPrivate::adjustedHistoryIndex(
int historyIndex)
const
77 const int historySize = m_history.
size();
78 if (historyIndex < 0) {
79 historyIndex = m_historyIndex;
80 }
else if (historyIndex >= historySize) {
81 historyIndex = historySize - 1;
82 Q_ASSERT(historyIndex >= 0);
89KCoreUrlNavigator::KCoreUrlNavigator(
const QUrl &url,
QObject *parent)
91 , d(new KCoreUrlNavigatorPrivate(this))
96KCoreUrlNavigator::~KCoreUrlNavigator()
108 d->m_history[d->m_historyIndex].state = state;
119 const int count = d->m_history.size();
120 if (d->m_historyIndex < count - 1) {
126 Q_EMIT historyIndexChanged();
128 Q_EMIT currentLocationUrlChanged();
137 if (d->m_historyIndex > 0) {
143 Q_EMIT historyIndexChanged();
145 Q_EMIT currentLocationUrlChanged();
157 setCurrentLocationUrl(upUrl);
164QUrl KCoreUrlNavigator::currentLocationUrl()
const
169void KCoreUrlNavigator::setCurrentLocationUrl(
const QUrl &newUrl)
186 if (!archiveMimetypes.
isEmpty()) {
189 bool insideCompressedPath = d->isCompressedPath(url, archiveMimetypes);
190 if (!insideCompressedPath) {
193 while (parentUrl != prevUrl) {
194 if (d->isCompressedPath(parentUrl, archiveMimetypes)) {
195 insideCompressedPath =
true;
202 if (!insideCompressedPath) {
206 firstChildUrl.
setScheme(QStringLiteral(
"file"));
213 const LocationData &data = d->m_history.at(d->m_historyIndex);
221 if (d->m_historyIndex > 0) {
225 auto begin = d->m_history.begin();
226 auto end =
begin + d->m_historyIndex;
227 d->m_history.erase(begin, end);
228 d->m_historyIndex = 0;
231 Q_ASSERT(d->m_historyIndex == 0);
232 d->m_history.insert(0, LocationData{url, {}});
236 const int historyMax = 100;
237 if (d->m_history.size() > historyMax) {
238 auto begin = d->m_history.begin() + historyMax;
239 auto end = d->m_history.end();
240 d->m_history.erase(begin, end);
243 Q_EMIT historyIndexChanged();
244 Q_EMIT historySizeChanged();
246 Q_EMIT currentLocationUrlChanged();
254 return d->m_history.count();
259 return d->m_historyIndex;
262#include "moc_kcoreurlnavigator.cpp"
Object that helps with keeping track of URLs in file-manager like interfaces.
Q_SIGNAL void urlSelectionRequested(const QUrl &url)
When the URL is changed and the new URL (e.g. /home/user1/) is a parent of the previous URL (e....
Q_SIGNAL void currentUrlAboutToChange(const QUrl &newUrl)
Is emitted, before the location URL is going to be changed to newUrl.
int historyIndex
The history index of the current location, where 0 <= history index < KCoreUrlNavigator::historySize(...
Q_INVOKABLE bool goBack()
Goes back one step in the URL history.
Q_INVOKABLE bool goForward()
Goes forward one step in the URL history.
Q_SIGNAL void historyChanged()
Is emitted, if the history has been changed.
Q_INVOKABLE bool goUp()
Goes up one step of the URL path and remembers the old path in the history.
Q_INVOKABLE QVariant locationState(int historyIndex=-1) const
Q_INVOKABLE QUrl locationUrl(int historyIndex=-1) const
int historySize
The amount of locations in the history.
Q_INVOKABLE void saveLocationState(const QVariant &state)
Saves the location state described by state for the current location.
static QStringList archiveMimetypes(const QString &protocol)
Returns the list of archive MIME types handled by the KIO worker implementing this protocol.
KIOCORE_EXPORT QUrl upUrl(const QUrl &url)
This function is useful to implement the "Up" button in a file manager for example.
const QList< QKeySequence > & begin()
const QList< QKeySequence > & end()
bool isEmpty() const const
qsizetype size() const const
QMimeType mimeTypeForUrl(const QUrl &url) const const
bool isEmpty() const const
QUrl adjusted(FormattingOptions options) const const
bool isValid() const const
bool matches(const QUrl &url, FormattingOptions options) const const
QString scheme() const const
void setScheme(const QString &scheme)
QString toString(FormattingOptions options) const const