]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/folders/folderspanel.cpp
FolderPanel: prevents scanning directory tree recursively
[dolphin.git] / src / panels / folders / folderspanel.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "folderspanel.h"
8
9 #include "dolphin_folderspanelsettings.h"
10 #include "dolphin_generalsettings.h"
11 #include "foldersitemlistwidget.h"
12 #include "global.h"
13 #include "kitemviews/kfileitemlistview.h"
14 #include "kitemviews/kfileitemmodel.h"
15 #include "kitemviews/kitemlistcontainer.h"
16 #include "kitemviews/kitemlistcontroller.h"
17 #include "kitemviews/kitemlistselectionmanager.h"
18 #include "treeviewcontextmenu.h"
19 #include "views/draganddrophelper.h"
20
21 #include <KJobWidgets>
22 #include <KJobUiDelegate>
23 #include <KIO/CopyJob>
24 #include <KIO/DropJob>
25 #include <KIO/FileUndoManager>
26 #include <KIO/RenameFileDialog>
27
28 #include <QApplication>
29 #include <QBoxLayout>
30 #include <QGraphicsSceneDragDropEvent>
31 #include <QGraphicsView>
32 #include <QPropertyAnimation>
33 #include <QTimer>
34
35 FoldersPanel::FoldersPanel(QWidget* parent) :
36 Panel(parent),
37 m_updateCurrentItem(false),
38 m_controller(nullptr),
39 m_model(nullptr)
40 {
41 setLayoutDirection(Qt::LeftToRight);
42 }
43
44 FoldersPanel::~FoldersPanel()
45 {
46 FoldersPanelSettings::self()->save();
47
48 if (m_controller) {
49 KItemListView* view = m_controller->view();
50 m_controller->setView(nullptr);
51 delete view;
52 }
53 }
54
55 void FoldersPanel::setShowHiddenFiles(bool show)
56 {
57 FoldersPanelSettings::setHiddenFilesShown(show);
58 m_model->setShowHiddenFiles(show);
59 }
60
61 bool FoldersPanel::showHiddenFiles() const
62 {
63 return FoldersPanelSettings::hiddenFilesShown();
64 }
65
66 void FoldersPanel::setLimitFoldersPanelToHome(bool enable)
67 {
68 FoldersPanelSettings::setLimitFoldersPanelToHome(enable);
69 reloadTree();
70 }
71
72 bool FoldersPanel::limitFoldersPanelToHome() const
73 {
74 return FoldersPanelSettings::limitFoldersPanelToHome();
75 }
76
77 void FoldersPanel::setAutoScrolling(bool enable)
78 {
79 // TODO: Not supported yet in Dolphin 2.0
80 FoldersPanelSettings::setAutoScrolling(enable);
81 }
82
83 bool FoldersPanel::autoScrolling() const
84 {
85 return FoldersPanelSettings::autoScrolling();
86 }
87
88 void FoldersPanel::rename(const KFileItem& item)
89 {
90 if (GeneralSettings::renameInline()) {
91 const int index = m_model->index(item);
92 m_controller->view()->editRole(index, "text");
93 } else {
94 KIO::RenameFileDialog* dialog = new KIO::RenameFileDialog(KFileItemList({item}), this);
95 dialog->open();
96 }
97 }
98
99 bool FoldersPanel::urlChanged()
100 {
101 if (!url().isValid() || url().scheme().contains(QLatin1String("search"))) {
102 // Skip results shown by a search, as possible identical
103 // directory names are useless without parent-path information.
104 return false;
105 }
106
107 if (m_controller) {
108 loadTree(url());
109 }
110
111 return true;
112 }
113
114 void FoldersPanel::reloadTree()
115 {
116 if (m_controller) {
117 loadTree(url(), AllowJumpHome);
118 }
119 }
120
121
122 void FoldersPanel::showEvent(QShowEvent* event)
123 {
124 if (event->spontaneous()) {
125 Panel::showEvent(event);
126 return;
127 }
128
129 if (!m_controller) {
130 // Postpone the creating of the controller to the first show event.
131 // This assures that no performance and memory overhead is given when the folders panel is not
132 // used at all and stays invisible.
133 KFileItemListView* view = new KFileItemListView();
134 view->setScanDirectories(false);
135 view->setWidgetCreator(new KItemListWidgetCreator<FoldersItemListWidget>());
136 view->setSupportsItemExpanding(true);
137 // Set the opacity to 0 initially. The opacity will be increased after the loading of the initial tree
138 // has been finished in slotLoadingCompleted(). This prevents an unnecessary animation-mess when
139 // opening the folders panel.
140 view->setOpacity(0);
141
142 connect(view, &KFileItemListView::roleEditingFinished,
143 this, &FoldersPanel::slotRoleEditingFinished);
144
145 m_model = new KFileItemModel(this);
146 m_model->setShowDirectoriesOnly(true);
147 m_model->setShowHiddenFiles(FoldersPanelSettings::hiddenFilesShown());
148 // Use a QueuedConnection to give the view the possibility to react first on the
149 // finished loading.
150 connect(m_model, &KFileItemModel::directoryLoadingCompleted, this, &FoldersPanel::slotLoadingCompleted, Qt::QueuedConnection);
151
152 m_controller = new KItemListController(m_model, view, this);
153 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
154 m_controller->setAutoActivationBehavior(KItemListController::ExpansionOnly);
155 m_controller->setMouseDoubleClickAction(KItemListController::ActivateAndExpandItem);
156 m_controller->setAutoActivationDelay(750);
157 m_controller->setSingleClickActivationEnforced(true);
158
159 connect(m_controller, &KItemListController::itemActivated, this, &FoldersPanel::slotItemActivated);
160 connect(m_controller, &KItemListController::itemMiddleClicked, this, &FoldersPanel::slotItemMiddleClicked);
161 connect(m_controller, &KItemListController::itemContextMenuRequested, this, &FoldersPanel::slotItemContextMenuRequested);
162 connect(m_controller, &KItemListController::viewContextMenuRequested, this, &FoldersPanel::slotViewContextMenuRequested);
163 connect(m_controller, &KItemListController::itemDropEvent, this, &FoldersPanel::slotItemDropEvent);
164
165 KItemListContainer* container = new KItemListContainer(m_controller, this);
166 container->setEnabledFrame(false);
167
168 QVBoxLayout* layout = new QVBoxLayout(this);
169 layout->setContentsMargins(0, 0, 0, 0);
170 layout->addWidget(container);
171 }
172
173 loadTree(url());
174 Panel::showEvent(event);
175 }
176
177 void FoldersPanel::keyPressEvent(QKeyEvent* event)
178 {
179 const int key = event->key();
180 if ((key == Qt::Key_Enter) || (key == Qt::Key_Return)) {
181 event->accept();
182 } else {
183 Panel::keyPressEvent(event);
184 }
185 }
186
187 void FoldersPanel::slotItemActivated(int index)
188 {
189 const KFileItem item = m_model->fileItem(index);
190 if (!item.isNull()) {
191 Q_EMIT folderActivated(item.url());
192 }
193 }
194
195 void FoldersPanel::slotItemMiddleClicked(int index)
196 {
197 const KFileItem item = m_model->fileItem(index);
198 if (!item.isNull()) {
199 Q_EMIT folderMiddleClicked(item.url());
200 }
201 }
202
203 void FoldersPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
204 {
205 const KFileItem fileItem = m_model->fileItem(index);
206
207 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, fileItem);
208 contextMenu.data()->open(pos.toPoint());
209 if (contextMenu.data()) {
210 delete contextMenu.data();
211 }
212 }
213
214 void FoldersPanel::slotViewContextMenuRequested(const QPointF& pos)
215 {
216 QPointer<TreeViewContextMenu> contextMenu = new TreeViewContextMenu(this, KFileItem());
217 contextMenu.data()->open(pos.toPoint());
218 if (contextMenu.data()) {
219 delete contextMenu.data();
220 }
221 }
222
223 void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
224 {
225 if (index >= 0) {
226 KFileItem destItem = m_model->fileItem(index);
227 if (destItem.isNull()) {
228 return;
229 }
230
231 QDropEvent dropEvent(event->pos().toPoint(),
232 event->possibleActions(),
233 event->mimeData(),
234 event->buttons(),
235 event->modifiers());
236
237 KIO::DropJob *job = DragAndDropHelper::dropUrls(destItem.mostLocalUrl(), &dropEvent, this);
238 if (job) {
239 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) Q_EMIT errorMessage(job->errorString()); });
240 }
241 }
242 }
243
244 void FoldersPanel::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
245 {
246 if (role == "text") {
247 const KFileItem item = m_model->fileItem(index);
248 const QString newName = value.toString();
249 if (!newName.isEmpty() && newName != item.text() && newName != QLatin1Char('.') && newName != QLatin1String("..")) {
250 const QUrl oldUrl = item.url();
251 QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename);
252 newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName));
253
254 KIO::Job* job = KIO::moveAs(oldUrl, newUrl);
255 KJobWidgets::setWindow(job, this);
256 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job);
257 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
258 }
259 }
260 }
261
262 void FoldersPanel::slotLoadingCompleted()
263 {
264 if (m_controller->view()->opacity() == 0) {
265 // The loading of the initial tree after opening the Folders panel
266 // has been finished. Trigger the increasing of the opacity after
267 // a short delay to give the view the chance to finish its internal
268 // animations.
269 // TODO: Check whether it makes sense to allow accessing the
270 // view-internal delay for usecases like this.
271 QTimer::singleShot(250, this, &FoldersPanel::startFadeInAnimation);
272 }
273
274 if (!m_updateCurrentItem) {
275 return;
276 }
277
278 const int index = m_model->index(url());
279 updateCurrentItem(index);
280 m_updateCurrentItem = false;
281 }
282
283 void FoldersPanel::startFadeInAnimation()
284 {
285 QPropertyAnimation* anim = new QPropertyAnimation(m_controller->view(), "opacity", this);
286 anim->setStartValue(0);
287 anim->setEndValue(1);
288 anim->setEasingCurve(QEasingCurve::InOutQuad);
289 anim->start(QAbstractAnimation::DeleteWhenStopped);
290 anim->setDuration(200);
291 }
292
293 void FoldersPanel::loadTree(const QUrl& url, FoldersPanel::NavigationBehaviour navigationBehaviour)
294 {
295 Q_ASSERT(m_controller);
296
297 m_updateCurrentItem = false;
298 bool jumpHome = false;
299
300 QUrl baseUrl;
301 if (!url.isLocalFile()) {
302 // Clear the path for non-local URLs and use it as base
303 baseUrl = url;
304 baseUrl.setPath(QStringLiteral("/"));
305 } else if (Dolphin::homeUrl().isParentOf(url) || (Dolphin::homeUrl() == url)) {
306 if (FoldersPanelSettings::limitFoldersPanelToHome() ) {
307 baseUrl = Dolphin::homeUrl();
308 } else {
309 // Use the root directory as base for local URLs (#150941)
310 baseUrl = QUrl::fromLocalFile(QDir::rootPath());
311 }
312 } else if (FoldersPanelSettings::limitFoldersPanelToHome() && navigationBehaviour == AllowJumpHome) {
313 baseUrl = Dolphin::homeUrl();
314 jumpHome = true;
315 } else {
316 // Use the root directory as base for local URLs (#150941)
317 baseUrl = QUrl::fromLocalFile(QDir::rootPath());
318 }
319
320 if (m_model->directory() != baseUrl && !jumpHome) {
321 m_updateCurrentItem = true;
322 m_model->refreshDirectory(baseUrl);
323 }
324
325 const int index = m_model->index(url);
326 if (jumpHome) {
327 Q_EMIT folderActivated(baseUrl);
328 } else if (index >= 0) {
329 updateCurrentItem(index);
330 } else if (url == baseUrl) {
331 // clear the selection when visiting the base url
332 updateCurrentItem(-1);
333 } else {
334 m_updateCurrentItem = true;
335 m_model->expandParentDirectories(url);
336
337 // slotLoadingCompleted() will be invoked after the model has
338 // expanded the url
339 }
340 }
341
342 void FoldersPanel::updateCurrentItem(int index)
343 {
344 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
345 selectionManager->setCurrentItem(index);
346 selectionManager->clearSelection();
347 selectionManager->setSelected(index);
348
349 m_controller->view()->scrollToItem(index);
350 }
351