File: dirhistoryqueue.cpp

package info (click to toggle)
krusader 2%3A2.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,448 kB
  • sloc: cpp: 56,112; ansic: 1,187; xml: 811; sh: 23; makefile: 3
file content (150 lines) | stat: -rw-r--r-- 3,689 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
    SPDX-FileCopyrightText: 2004 Shie Erlich <erlich@users.sourceforge.net>
    SPDX-FileCopyrightText: 2004 Rafi Yanai <yanai@users.sourceforge.net>
    SPDX-FileCopyrightText: 2010 Jan Lepper <dehtris@yahoo.de>
    SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "dirhistoryqueue.h"

#include "../defaults.h"
#include "../krservices.h"
#include "PanelView/krview.h"
#include "krpanel.h"

// QtCore
#include <QDir>

DirHistoryQueue::DirHistoryQueue(KrPanel *panel)
    : _panel(panel)
    , _currentPos(0)
{
}

DirHistoryQueue::~DirHistoryQueue() = default;

void DirHistoryQueue::clear()
{
    _urlQueue.clear();
    _currentItems.clear();
    _currentPos = 0;
}

QUrl DirHistoryQueue::currentUrl()
{
    if (_urlQueue.count())
        return _urlQueue[_currentPos];
    else
        return QUrl();
}

void DirHistoryQueue::setCurrentUrl(const QUrl &url)
{
    if (_urlQueue.count())
        _urlQueue[_currentPos] = url;
}

QString DirHistoryQueue::currentItem()
{
    if (count())
        return _currentItems[_currentPos];
    else
        return QString();
}

void DirHistoryQueue::saveCurrentItem()
{
    // if the filesystem-url hasn't been refreshed yet,
    // avoid saving current item for the wrong url
    if (count() && _panel->virtualPath().matches(_urlQueue[_currentPos], QUrl::StripTrailingSlash))
        _currentItems[_currentPos] = _panel->view->getCurrentItem();
}

void DirHistoryQueue::add(QUrl url, const QString &currentItem)
{
    url.setPath(QDir::cleanPath(url.path()));

    if (_urlQueue.isEmpty()) {
        _urlQueue.push_front(url);
        _currentItems.push_front(currentItem);
        return;
    }

    if (_urlQueue[_currentPos].matches(url, QUrl::StripTrailingSlash)) {
        _currentItems[_currentPos] = currentItem;
        return;
    }

    for (int i = 0; i < _currentPos; i++) {
        _urlQueue.pop_front();
        _currentItems.pop_front();
    }

    _currentPos = 0;

    // do we have room for another ?
    if (_urlQueue.count() > 12) { // FIXME: use user-defined size
        // no room - remove the oldest entry
        _urlQueue.pop_back();
        _currentItems.pop_back();
    }

    saveCurrentItem();
    _urlQueue.push_front(url);
    _currentItems.push_front(currentItem);
}

QUrl DirHistoryQueue::gotoPos(int pos)
{
    if (pos >= 0 && pos < _urlQueue.count()) {
        saveCurrentItem();
        _currentPos = pos;
        return currentUrl();
    }
    return QUrl();
}

QUrl DirHistoryQueue::goBack()
{
    return gotoPos(_currentPos + 1);
}

QUrl DirHistoryQueue::goForward()
{
    return gotoPos(_currentPos - 1);
}

void DirHistoryQueue::save(KConfigGroup cfg)
{
    saveCurrentItem();

    QList<QUrl> urls;
    for (const QUrl &url : std::as_const(_urlQueue)) {
        // make sure no passwords are permanently stored
        QUrl safeUrl(url);
        safeUrl.setPassword(QString());
        urls << safeUrl;
    }

    cfg.writeEntry("Entrys", KrServices::toStringList(urls)); // krazy:exclude=spelling
    cfg.writeEntry("CurrentItems", _currentItems);
    cfg.writeEntry("CurrentIndex", _currentPos);
}

bool DirHistoryQueue::restore(const KConfigGroup &cfg)
{
    clear();
    _urlQueue = KrServices::toUrlList(cfg.readEntry("Entrys", QStringList())); // krazy:exclude=spelling
    _currentItems = cfg.readEntry("CurrentItems", QStringList());
    if (!_urlQueue.count() || _urlQueue.count() != _currentItems.count()) {
        clear();
        return false;
    }
    _currentPos = cfg.readEntry("CurrentIndex", 0);
    if (_currentPos >= _urlQueue.count() || _currentPos < 0)
        _currentPos = 0;

    return true;
}