File indexing completed on 2024-04-28 17:06:19
0001 /* 0002 SPDX-FileCopyrightText: 2004 Shie Erlich <erlich@users.sourceforge.net> 0003 SPDX-FileCopyrightText: 2004 Rafi Yanai <yanai@users.sourceforge.net> 0004 SPDX-FileCopyrightText: 2010 Jan Lepper <dehtris@yahoo.de> 0005 SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org> 0006 0007 SPDX-License-Identifier: GPL-2.0-or-later 0008 */ 0009 0010 #include "dirhistoryqueue.h" 0011 0012 #include "../defaults.h" 0013 #include "../krservices.h" 0014 #include "PanelView/krview.h" 0015 #include "krpanel.h" 0016 0017 // QtCore 0018 #include <QDir> 0019 0020 DirHistoryQueue::DirHistoryQueue(KrPanel *panel) 0021 : _panel(panel) 0022 , _currentPos(0) 0023 { 0024 } 0025 0026 DirHistoryQueue::~DirHistoryQueue() = default; 0027 0028 void DirHistoryQueue::clear() 0029 { 0030 _urlQueue.clear(); 0031 _currentItems.clear(); 0032 _currentPos = 0; 0033 } 0034 0035 QUrl DirHistoryQueue::currentUrl() 0036 { 0037 if (_urlQueue.count()) 0038 return _urlQueue[_currentPos]; 0039 else 0040 return QUrl(); 0041 } 0042 0043 void DirHistoryQueue::setCurrentUrl(const QUrl &url) 0044 { 0045 if (_urlQueue.count()) 0046 _urlQueue[_currentPos] = url; 0047 } 0048 0049 QString DirHistoryQueue::currentItem() 0050 { 0051 if (count()) 0052 return _currentItems[_currentPos]; 0053 else 0054 return QString(); 0055 } 0056 0057 void DirHistoryQueue::saveCurrentItem() 0058 { 0059 // if the filesystem-url hasn't been refreshed yet, 0060 // avoid saving current item for the wrong url 0061 if (count() && _panel->virtualPath().matches(_urlQueue[_currentPos], QUrl::StripTrailingSlash)) 0062 _currentItems[_currentPos] = _panel->view->getCurrentItem(); 0063 } 0064 0065 void DirHistoryQueue::add(QUrl url, const QString ¤tItem) 0066 { 0067 url.setPath(QDir::cleanPath(url.path())); 0068 0069 if (_urlQueue.isEmpty()) { 0070 _urlQueue.push_front(url); 0071 _currentItems.push_front(currentItem); 0072 return; 0073 } 0074 0075 if (_urlQueue[_currentPos].matches(url, QUrl::StripTrailingSlash)) { 0076 _currentItems[_currentPos] = currentItem; 0077 return; 0078 } 0079 0080 for (int i = 0; i < _currentPos; i++) { 0081 _urlQueue.pop_front(); 0082 _currentItems.pop_front(); 0083 } 0084 0085 _currentPos = 0; 0086 0087 // do we have room for another ? 0088 if (_urlQueue.count() > 12) { // FIXME: use user-defined size 0089 // no room - remove the oldest entry 0090 _urlQueue.pop_back(); 0091 _currentItems.pop_back(); 0092 } 0093 0094 saveCurrentItem(); 0095 _urlQueue.push_front(url); 0096 _currentItems.push_front(currentItem); 0097 } 0098 0099 bool DirHistoryQueue::gotoPos(int pos) 0100 { 0101 if (pos >= 0 && pos < _urlQueue.count()) { 0102 saveCurrentItem(); 0103 _currentPos = pos; 0104 return true; 0105 } 0106 return false; 0107 } 0108 0109 bool DirHistoryQueue::goBack() 0110 { 0111 return gotoPos(_currentPos + 1); 0112 } 0113 0114 bool DirHistoryQueue::goForward() 0115 { 0116 return gotoPos(_currentPos - 1); 0117 } 0118 0119 void DirHistoryQueue::save(KConfigGroup cfg) 0120 { 0121 saveCurrentItem(); 0122 0123 QList<QUrl> urls; 0124 foreach (const QUrl &url, _urlQueue) { 0125 // make sure no passwords are permanently stored 0126 QUrl safeUrl(url); 0127 safeUrl.setPassword(QString()); 0128 urls << safeUrl; 0129 } 0130 0131 cfg.writeEntry("Entrys", KrServices::toStringList(urls)); // krazy:exclude=spelling 0132 cfg.writeEntry("CurrentItems", _currentItems); 0133 cfg.writeEntry("CurrentIndex", _currentPos); 0134 } 0135 0136 bool DirHistoryQueue::restore(const KConfigGroup &cfg) 0137 { 0138 clear(); 0139 _urlQueue = KrServices::toUrlList(cfg.readEntry("Entrys", QStringList())); // krazy:exclude=spelling 0140 _currentItems = cfg.readEntry("CurrentItems", QStringList()); 0141 if (!_urlQueue.count() || _urlQueue.count() != _currentItems.count()) { 0142 clear(); 0143 return false; 0144 } 0145 _currentPos = cfg.readEntry("CurrentIndex", 0); 0146 if (_currentPos >= _urlQueue.count() || _currentPos < 0) 0147 _currentPos = 0; 0148 0149 return true; 0150 }