]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Disable unmount option for / or /home
[dolphin.git] / src / panels / places / placespanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesView from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
22 ***************************************************************************/
23
24 #include "placespanel.h"
25
26 #include "dolphin_generalsettings.h"
27 #include "global.h"
28 #include "kitemviews/kitemlistcontainer.h"
29 #include "kitemviews/kitemlistcontroller.h"
30 #include "kitemviews/kitemlistselectionmanager.h"
31 #include "kitemviews/kstandarditem.h"
32 #include "placesitem.h"
33 #include "placesitemeditdialog.h"
34 #include "placesitemlistgroupheader.h"
35 #include "placesitemlistwidget.h"
36 #include "placesitemmodel.h"
37 #include "placesview.h"
38 #include "trash/dolphintrash.h"
39 #include "views/draganddrophelper.h"
40
41 #include <KFilePlacesModel>
42 #include <KIO/DropJob>
43 #include <KIO/EmptyTrashJob>
44 #include <KIO/Job>
45 #include <KIconLoader>
46 #include <KLocalizedString>
47 #include <KMountPoint>
48
49 #include <QGraphicsSceneDragDropEvent>
50 #include <QIcon>
51 #include <QMenu>
52 #include <QMimeData>
53 #include <QVBoxLayout>
54
55 PlacesPanel::PlacesPanel(QWidget* parent) :
56 Panel(parent),
57 m_controller(nullptr),
58 m_model(nullptr),
59 m_view(nullptr),
60 m_storageSetupFailedUrl(),
61 m_triggerStorageSetupButton(),
62 m_itemDropEventIndex(-1),
63 m_itemDropEventMimeData(nullptr),
64 m_itemDropEvent(nullptr)
65 {
66 }
67
68 PlacesPanel::~PlacesPanel()
69 {
70 }
71
72 void PlacesPanel::proceedWithTearDown()
73 {
74 m_model->proceedWithTearDown();
75 }
76
77 bool PlacesPanel::urlChanged()
78 {
79 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
80 // Skip results shown by a search, as possible identical
81 // directory names are useless without parent-path information.
82 return false;
83 }
84
85 if (m_controller) {
86 selectClosestItem();
87 }
88
89 return true;
90 }
91
92 void PlacesPanel::readSettings()
93 {
94 if (m_controller) {
95 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
96 m_controller->setAutoActivationDelay(delay);
97 }
98 }
99
100 void PlacesPanel::showEvent(QShowEvent* event)
101 {
102 if (event->spontaneous()) {
103 Panel::showEvent(event);
104 return;
105 }
106
107 if (!m_controller) {
108 // Postpone the creating of the controller to the first show event.
109 // This assures that no performance and memory overhead is given when the folders panel is not
110 // used at all and stays invisible.
111 m_model = new PlacesItemModel(this);
112 m_model->setGroupedSorting(true);
113 connect(m_model, &PlacesItemModel::errorMessage,
114 this, &PlacesPanel::errorMessage);
115 connect(m_model, &PlacesItemModel::storageTearDownRequested,
116 this, &PlacesPanel::storageTearDownRequested);
117 connect(m_model, &PlacesItemModel::storageTearDownExternallyRequested,
118 this, &PlacesPanel::storageTearDownExternallyRequested);
119
120 m_view = new PlacesView();
121 m_view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
122 m_view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
123
124 m_controller = new KItemListController(m_model, m_view, this);
125 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
126 m_controller->setSingleClickActivationEnforced(true);
127
128 readSettings();
129
130 connect(m_controller, &KItemListController::itemActivated, this, &PlacesPanel::slotItemActivated);
131 connect(m_controller, &KItemListController::itemMiddleClicked, this, &PlacesPanel::slotItemMiddleClicked);
132 connect(m_controller, &KItemListController::itemContextMenuRequested, this, &PlacesPanel::slotItemContextMenuRequested);
133 connect(m_controller, &KItemListController::viewContextMenuRequested, this, &PlacesPanel::slotViewContextMenuRequested);
134 connect(m_controller, &KItemListController::itemDropEvent, this, &PlacesPanel::slotItemDropEvent);
135 connect(m_controller, &KItemListController::aboveItemDropEvent, this, &PlacesPanel::slotAboveItemDropEvent);
136
137 KItemListContainer* container = new KItemListContainer(m_controller, this);
138 container->setEnabledFrame(false);
139
140 QVBoxLayout* layout = new QVBoxLayout(this);
141 layout->setMargin(0);
142 layout->addWidget(container);
143
144 selectClosestItem();
145 }
146
147 Panel::showEvent(event);
148 }
149
150 void PlacesPanel::slotItemActivated(int index)
151 {
152 triggerItem(index, Qt::LeftButton);
153 }
154
155 void PlacesPanel::slotItemMiddleClicked(int index)
156 {
157 triggerItem(index, Qt::MiddleButton);
158 }
159
160 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
161 {
162 PlacesItem* item = m_model->placesItem(index);
163 if (!item) {
164 return;
165 }
166
167 QMenu menu(this);
168
169 QAction* emptyTrashAction = nullptr;
170 QAction* editAction = nullptr;
171 QAction* teardownAction = nullptr;
172 QAction* ejectAction = nullptr;
173 QAction* mountAction = nullptr;
174
175 const bool isDevice = !item->udi().isEmpty();
176 const bool isTrash = (item->url().scheme() == QLatin1String("trash"));
177 if (isDevice) {
178 ejectAction = m_model->ejectAction(index);
179 if (ejectAction) {
180 ejectAction->setParent(&menu);
181 menu.addAction(ejectAction);
182 }
183
184 teardownAction = m_model->teardownAction(index);
185 if (teardownAction) {
186 // Disable teardown option for root and home partitions
187 bool teardownEnabled = item->url() != QUrl::fromLocalFile(QDir::rootPath());
188 if (teardownEnabled) {
189 KMountPoint::Ptr mountPoint = KMountPoint::currentMountPoints().findByPath(QDir::homePath());
190 if (mountPoint && item->url() == QUrl::fromLocalFile(mountPoint->mountPoint())) {
191 teardownEnabled = false;
192 }
193 }
194 teardownAction->setEnabled(teardownEnabled);
195
196 teardownAction->setParent(&menu);
197 menu.addAction(teardownAction);
198 }
199
200 if (item->storageSetupNeeded()) {
201 mountAction = menu.addAction(QIcon::fromTheme(QStringLiteral("media-mount")), i18nc("@action:inmenu", "Mount"));
202 }
203
204 if (teardownAction || ejectAction || mountAction) {
205 menu.addSeparator();
206 }
207 } else {
208 if (isTrash) {
209 emptyTrashAction = menu.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
210 emptyTrashAction->setEnabled(item->icon() == QLatin1String("user-trash-full"));
211 menu.addSeparator();
212 }
213 }
214
215 QAction* openInNewWindowAction = menu.addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@item:inmenu", "Open in New Window"));
216 QAction* openInNewTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@item:inmenu", "Open in New Tab"));
217 if (!isDevice && !isTrash) {
218 menu.addSeparator();
219 }
220
221 if (!isDevice) {
222 editAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-entry")), i18nc("@item:inmenu", "Edit..."));
223 }
224
225 QAction* removeAction = nullptr;
226 if (!isDevice && !item->isSystemItem()) {
227 removeAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
228 }
229
230 QAction* hideAction = menu.addAction(QIcon::fromTheme(QStringLiteral("hint")), i18nc("@item:inmenu", "Hide"));
231 hideAction->setCheckable(true);
232 hideAction->setChecked(item->isHidden());
233
234 buildGroupContextMenu(&menu, index);
235
236 QAction* action = menu.exec(pos.toPoint());
237 if (action) {
238 if (action == emptyTrashAction) {
239 Trash::empty(this);
240 } else {
241 // The index might have changed if devices were added/removed while
242 // the context menu was open.
243 index = m_model->index(item);
244 if (index < 0) {
245 // The item is not in the model any more, probably because it was an
246 // external device that has been removed while the context menu was open.
247 return;
248 }
249
250 if (action == editAction) {
251 editEntry(index);
252 } else if (action == removeAction) {
253 m_model->deleteItem(index);
254 } else if (action == hideAction) {
255 item->setHidden(hideAction->isChecked());
256 } else if (action == openInNewWindowAction) {
257 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(m_model->data(index).value("url").toUrl())}, this);
258 } else if (action == openInNewTabAction) {
259 // TriggerItem does set up the storage first and then it will
260 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
261 triggerItem(index, Qt::MiddleButton);
262 } else if (action == mountAction) {
263 m_model->requestStorageSetup(index);
264 } else if (action == teardownAction) {
265 m_model->requestTearDown(index);
266 } else if (action == ejectAction) {
267 m_model->requestEject(index);
268 }
269 }
270 }
271
272 selectClosestItem();
273 }
274
275 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
276 {
277 QMenu menu(this);
278
279 QAction* addAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
280
281 QAction* showAllAction = nullptr;
282 if (m_model->hiddenCount() > 0) {
283 showAllAction = menu.addAction(QIcon::fromTheme(QStringLiteral("visibility")), i18nc("@item:inmenu", "Show All Entries"));
284 showAllAction->setCheckable(true);
285 showAllAction->setChecked(m_model->hiddenItemsShown());
286 }
287
288 buildGroupContextMenu(&menu, m_controller->indexCloseToMousePressedPosition());
289
290 QMenu* iconSizeSubMenu = new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu);
291
292 struct IconSizeInfo
293 {
294 int size;
295 const char* context;
296 const char* text;
297 };
298
299 const int iconSizeCount = 4;
300 static const IconSizeInfo iconSizes[iconSizeCount] = {
301 {KIconLoader::SizeSmall, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
302 {KIconLoader::SizeSmallMedium, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
303 {KIconLoader::SizeMedium, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
304 {KIconLoader::SizeLarge, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
305 };
306
307 QHash<QAction*, int> iconSizeActionMap;
308 QActionGroup* iconSizeGroup = new QActionGroup(iconSizeSubMenu);
309
310 for (int i = 0; i < iconSizeCount; ++i) {
311 const int size = iconSizes[i].size;
312 const QString text = i18nc(iconSizes[i].context, iconSizes[i].text,
313 size, size);
314
315 QAction* action = iconSizeSubMenu->addAction(text);
316 iconSizeActionMap.insert(action, size);
317 action->setActionGroup(iconSizeGroup);
318 action->setCheckable(true);
319 action->setChecked(m_view->iconSize() == size);
320 }
321
322 menu.addMenu(iconSizeSubMenu);
323
324 menu.addSeparator();
325 foreach (QAction* action, customContextMenuActions()) {
326 menu.addAction(action);
327 }
328
329 QAction* action = menu.exec(pos.toPoint());
330 if (action) {
331 if (action == addAction) {
332 addEntry();
333 } else if (action == showAllAction) {
334 m_model->setHiddenItemsShown(showAllAction->isChecked());
335 } else if (iconSizeActionMap.contains(action)) {
336 m_view->setIconSize(iconSizeActionMap.value(action));
337 }
338 }
339
340 selectClosestItem();
341 }
342
343 QAction *PlacesPanel::buildGroupContextMenu(QMenu *menu, int index)
344 {
345 if (index == -1) {
346 return nullptr;
347 }
348
349 KFilePlacesModel::GroupType groupType = m_model->groupType(index);
350 QAction *hideGroupAction = menu->addAction(QIcon::fromTheme(QStringLiteral("hint")), i18nc("@item:inmenu", "Hide Section '%1'", m_model->item(index)->group()));
351 hideGroupAction->setCheckable(true);
352 hideGroupAction->setChecked(m_model->isGroupHidden(groupType));
353
354 connect(hideGroupAction, &QAction::triggered, this, [this, groupType, hideGroupAction]{
355 m_model->setGroupHidden(groupType, hideGroupAction->isChecked());
356 });
357
358 return hideGroupAction;
359 }
360
361 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
362 {
363 if (index < 0) {
364 return;
365 }
366
367 const PlacesItem* destItem = m_model->placesItem(index);
368
369 if (destItem->isSearchOrTimelineUrl()) {
370 return;
371 }
372
373 if (m_model->storageSetupNeeded(index)) {
374 connect(m_model, &PlacesItemModel::storageSetupDone,
375 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
376
377 m_itemDropEventIndex = index;
378
379 // Make a full copy of the Mime-Data
380 m_itemDropEventMimeData = new QMimeData;
381 m_itemDropEventMimeData->setText(event->mimeData()->text());
382 m_itemDropEventMimeData->setHtml(event->mimeData()->html());
383 m_itemDropEventMimeData->setUrls(event->mimeData()->urls());
384 m_itemDropEventMimeData->setImageData(event->mimeData()->imageData());
385 m_itemDropEventMimeData->setColorData(event->mimeData()->colorData());
386
387 m_itemDropEvent = new QDropEvent(event->pos().toPoint(),
388 event->possibleActions(),
389 m_itemDropEventMimeData,
390 event->buttons(),
391 event->modifiers());
392
393 m_model->requestStorageSetup(index);
394 return;
395 }
396
397 QUrl destUrl = destItem->url();
398 QDropEvent dropEvent(event->pos().toPoint(),
399 event->possibleActions(),
400 event->mimeData(),
401 event->buttons(),
402 event->modifiers());
403
404 slotUrlsDropped(destUrl, &dropEvent, this);
405 }
406
407 void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success)
408 {
409 disconnect(m_model, &PlacesItemModel::storageSetupDone,
410 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
411
412 if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) {
413 if (success) {
414 QUrl destUrl = m_model->placesItem(index)->url();
415 slotUrlsDropped(destUrl, m_itemDropEvent, this);
416 }
417
418 delete m_itemDropEventMimeData;
419 delete m_itemDropEvent;
420
421 m_itemDropEventIndex = -1;
422 m_itemDropEventMimeData = nullptr;
423 m_itemDropEvent = nullptr;
424 }
425 }
426
427 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
428 {
429 m_model->dropMimeDataBefore(index, event->mimeData());
430 }
431
432 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
433 {
434 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
435 if (job) {
436 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
437 }
438 }
439
440 void PlacesPanel::slotStorageSetupDone(int index, bool success)
441 {
442 disconnect(m_model, &PlacesItemModel::storageSetupDone,
443 this, &PlacesPanel::slotStorageSetupDone);
444
445 if (m_triggerStorageSetupButton == Qt::NoButton) {
446 return;
447 }
448
449 if (success) {
450 Q_ASSERT(!m_model->storageSetupNeeded(index));
451 triggerItem(index, m_triggerStorageSetupButton);
452 m_triggerStorageSetupButton = Qt::NoButton;
453 } else {
454 setUrl(m_storageSetupFailedUrl);
455 m_storageSetupFailedUrl = QUrl();
456 }
457 }
458
459 void PlacesPanel::addEntry()
460 {
461 const int index = m_controller->selectionManager()->currentItem();
462 const QUrl url = m_model->data(index).value("url").toUrl();
463
464 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
465 dialog->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
466 dialog->setAllowGlobal(true);
467 dialog->setUrl(url);
468 if (dialog->exec() == QDialog::Accepted) {
469 m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
470 }
471
472 delete dialog;
473 }
474
475 void PlacesPanel::editEntry(int index)
476 {
477 QHash<QByteArray, QVariant> data = m_model->data(index);
478
479 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
480 dialog->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
481 dialog->setIcon(data.value("iconName").toString());
482 dialog->setText(data.value("text").toString());
483 dialog->setUrl(data.value("url").toUrl());
484 dialog->setAllowGlobal(true);
485 if (dialog->exec() == QDialog::Accepted) {
486 PlacesItem* oldItem = m_model->placesItem(index);
487 if (oldItem) {
488 oldItem->setText(dialog->text());
489 oldItem->setUrl(dialog->url());
490 oldItem->setIcon(dialog->icon());
491 m_model->refresh();
492 }
493 }
494
495 delete dialog;
496 }
497
498 void PlacesPanel::selectClosestItem()
499 {
500 const int index = m_model->closestItem(url());
501 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
502 selectionManager->setCurrentItem(index);
503 selectionManager->clearSelection();
504 selectionManager->setSelected(index);
505 }
506
507 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
508 {
509 const PlacesItem* item = m_model->placesItem(index);
510 if (!item) {
511 return;
512 }
513
514 if (m_model->storageSetupNeeded(index)) {
515 m_triggerStorageSetupButton = button;
516 m_storageSetupFailedUrl = url();
517
518 connect(m_model, &PlacesItemModel::storageSetupDone,
519 this, &PlacesPanel::slotStorageSetupDone);
520
521 m_model->requestStorageSetup(index);
522 } else {
523 m_triggerStorageSetupButton = Qt::NoButton;
524
525 const QUrl url = m_model->data(index).value("url").toUrl();
526 if (!url.isEmpty()) {
527 if (button == Qt::MiddleButton) {
528 emit placeMiddleClicked(KFilePlacesModel::convertedUrl(url));
529 } else {
530 emit placeActivated(KFilePlacesModel::convertedUrl(url));
531 }
532 }
533 }
534 }