1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on KFilePlacesView from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
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. *
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. *
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 ***************************************************************************/
24 #include "placespanel.h"
26 #include "dolphin_generalsettings.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"
41 #include <KFilePlacesModel>
42 #include <KIO/DropJob>
43 #include <KIO/EmptyTrashJob>
45 #include <KIconLoader>
46 #include <KLocalizedString>
47 #include <KMountPoint>
48 #include <KPropertiesDialog>
50 #include <QGraphicsSceneDragDropEvent>
54 #include <QVBoxLayout>
56 PlacesPanel::PlacesPanel(QWidget
* parent
) :
58 m_controller(nullptr),
61 m_storageSetupFailedUrl(),
62 m_triggerStorageSetupButton(),
63 m_itemDropEventIndex(-1),
64 m_itemDropEventMimeData(nullptr),
65 m_itemDropEvent(nullptr)
69 PlacesPanel::~PlacesPanel()
73 void PlacesPanel::proceedWithTearDown()
75 m_model
->proceedWithTearDown();
78 bool PlacesPanel::urlChanged()
80 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
81 // Skip results shown by a search, as possible identical
82 // directory names are useless without parent-path information.
93 void PlacesPanel::readSettings()
96 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
97 m_controller
->setAutoActivationDelay(delay
);
101 void PlacesPanel::showEvent(QShowEvent
* event
)
103 if (event
->spontaneous()) {
104 Panel::showEvent(event
);
109 // Postpone the creating of the controller to the first show event.
110 // This assures that no performance and memory overhead is given when the folders panel is not
111 // used at all and stays invisible.
112 m_model
= new PlacesItemModel(this);
113 m_model
->setGroupedSorting(true);
114 connect(m_model
, &PlacesItemModel::errorMessage
,
115 this, &PlacesPanel::errorMessage
);
116 connect(m_model
, &PlacesItemModel::storageTearDownRequested
,
117 this, &PlacesPanel::storageTearDownRequested
);
118 connect(m_model
, &PlacesItemModel::storageTearDownExternallyRequested
,
119 this, &PlacesPanel::storageTearDownExternallyRequested
);
121 m_view
= new PlacesView();
122 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
123 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
125 m_controller
= new KItemListController(m_model
, m_view
, this);
126 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
127 m_controller
->setSingleClickActivationEnforced(true);
131 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
132 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
133 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
134 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
135 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
136 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
138 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
139 container
->setEnabledFrame(false);
141 QVBoxLayout
* layout
= new QVBoxLayout(this);
142 layout
->setMargin(0);
143 layout
->addWidget(container
);
148 Panel::showEvent(event
);
151 void PlacesPanel::slotItemActivated(int index
)
153 triggerItem(index
, Qt::LeftButton
);
156 void PlacesPanel::slotItemMiddleClicked(int index
)
158 triggerItem(index
, Qt::MiddleButton
);
161 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
163 PlacesItem
* item
= m_model
->placesItem(index
);
170 QAction
* emptyTrashAction
= nullptr;
171 QAction
* editAction
= nullptr;
172 QAction
* teardownAction
= nullptr;
173 QAction
* ejectAction
= nullptr;
174 QAction
* mountAction
= nullptr;
176 const bool isDevice
= !item
->udi().isEmpty();
177 const bool isTrash
= (item
->url().scheme() == QLatin1String("trash"));
179 ejectAction
= m_model
->ejectAction(index
);
181 ejectAction
->setParent(&menu
);
182 menu
.addAction(ejectAction
);
185 teardownAction
= m_model
->teardownAction(index
);
186 if (teardownAction
) {
187 // Disable teardown option for root and home partitions
188 bool teardownEnabled
= item
->url() != QUrl::fromLocalFile(QDir::rootPath());
189 if (teardownEnabled
) {
190 KMountPoint::Ptr mountPoint
= KMountPoint::currentMountPoints().findByPath(QDir::homePath());
191 if (mountPoint
&& item
->url() == QUrl::fromLocalFile(mountPoint
->mountPoint())) {
192 teardownEnabled
= false;
195 teardownAction
->setEnabled(teardownEnabled
);
197 teardownAction
->setParent(&menu
);
198 menu
.addAction(teardownAction
);
201 if (item
->storageSetupNeeded()) {
202 mountAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("media-mount")), i18nc("@action:inmenu", "Mount"));
205 if (teardownAction
|| ejectAction
|| mountAction
) {
210 emptyTrashAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
211 emptyTrashAction
->setEnabled(item
->icon() == QLatin1String("user-trash-full"));
216 QAction
* openInNewWindowAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@item:inmenu", "Open in New Window"));
217 QAction
* openInNewTabAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@item:inmenu", "Open in New Tab"));
218 QAction
* propertiesAction
= nullptr;
219 if (item
->url().isLocalFile()) {
220 propertiesAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-properties")), i18nc("@action:inmenu", "Properties"));
222 if (!isDevice
&& !isTrash
) {
227 editAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-entry")), i18nc("@item:inmenu", "Edit..."));
230 QAction
* removeAction
= nullptr;
231 if (!isDevice
&& !item
->isSystemItem()) {
232 removeAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
235 QAction
* hideAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("hint")), i18nc("@item:inmenu", "Hide"));
236 hideAction
->setCheckable(true);
237 hideAction
->setChecked(item
->isHidden());
239 buildGroupContextMenu(&menu
, index
);
241 QAction
* action
= menu
.exec(pos
.toPoint());
243 if (action
== emptyTrashAction
) {
246 // The index might have changed if devices were added/removed while
247 // the context menu was open.
248 index
= m_model
->index(item
);
250 // The item is not in the model any more, probably because it was an
251 // external device that has been removed while the context menu was open.
255 if (action
== editAction
) {
257 } else if (action
== removeAction
) {
258 m_model
->deleteItem(index
);
259 } else if (action
== hideAction
) {
260 item
->setHidden(hideAction
->isChecked());
261 } else if (action
== openInNewWindowAction
) {
262 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(m_model
->data(index
).value("url").toUrl())}, this);
263 } else if (action
== openInNewTabAction
) {
264 // TriggerItem does set up the storage first and then it will
265 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
266 triggerItem(index
, Qt::MiddleButton
);
267 } else if (action
== mountAction
) {
268 m_model
->requestStorageSetup(index
);
269 } else if (action
== teardownAction
) {
270 m_model
->requestTearDown(index
);
271 } else if (action
== ejectAction
) {
272 m_model
->requestEject(index
);
273 } else if (action
== propertiesAction
) {
274 KPropertiesDialog
* dialog
= new KPropertiesDialog(item
->url(), this);
275 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
284 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
288 QAction
* addAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
290 QAction
* showAllAction
= nullptr;
291 if (m_model
->hiddenCount() > 0) {
292 showAllAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("visibility")), i18nc("@item:inmenu", "Show All Entries"));
293 showAllAction
->setCheckable(true);
294 showAllAction
->setChecked(m_model
->hiddenItemsShown());
297 buildGroupContextMenu(&menu
, m_controller
->indexCloseToMousePressedPosition());
299 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
308 const int iconSizeCount
= 4;
309 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
310 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
311 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
312 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
313 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
316 QHash
<QAction
*, int> iconSizeActionMap
;
317 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
319 for (int i
= 0; i
< iconSizeCount
; ++i
) {
320 const int size
= iconSizes
[i
].size
;
321 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
324 QAction
* action
= iconSizeSubMenu
->addAction(text
);
325 iconSizeActionMap
.insert(action
, size
);
326 action
->setActionGroup(iconSizeGroup
);
327 action
->setCheckable(true);
328 action
->setChecked(m_view
->iconSize() == size
);
331 menu
.addMenu(iconSizeSubMenu
);
334 foreach (QAction
* action
, customContextMenuActions()) {
335 menu
.addAction(action
);
338 QAction
* action
= menu
.exec(pos
.toPoint());
340 if (action
== addAction
) {
342 } else if (action
== showAllAction
) {
343 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
344 } else if (iconSizeActionMap
.contains(action
)) {
345 m_view
->setIconSize(iconSizeActionMap
.value(action
));
352 QAction
*PlacesPanel::buildGroupContextMenu(QMenu
*menu
, int index
)
358 KFilePlacesModel::GroupType groupType
= m_model
->groupType(index
);
359 QAction
*hideGroupAction
= menu
->addAction(QIcon::fromTheme(QStringLiteral("hint")), i18nc("@item:inmenu", "Hide Section '%1'", m_model
->item(index
)->group()));
360 hideGroupAction
->setCheckable(true);
361 hideGroupAction
->setChecked(m_model
->isGroupHidden(groupType
));
363 connect(hideGroupAction
, &QAction::triggered
, this, [this, groupType
, hideGroupAction
]{
364 m_model
->setGroupHidden(groupType
, hideGroupAction
->isChecked());
367 return hideGroupAction
;
370 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
376 const PlacesItem
* destItem
= m_model
->placesItem(index
);
378 if (destItem
->isSearchOrTimelineUrl()) {
382 if (m_model
->storageSetupNeeded(index
)) {
383 connect(m_model
, &PlacesItemModel::storageSetupDone
,
384 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
386 m_itemDropEventIndex
= index
;
388 // Make a full copy of the Mime-Data
389 m_itemDropEventMimeData
= new QMimeData
;
390 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
391 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
392 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
393 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
394 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
396 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
397 event
->possibleActions(),
398 m_itemDropEventMimeData
,
402 m_model
->requestStorageSetup(index
);
406 QUrl destUrl
= destItem
->url();
407 QDropEvent
dropEvent(event
->pos().toPoint(),
408 event
->possibleActions(),
413 slotUrlsDropped(destUrl
, &dropEvent
, this);
416 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
418 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
419 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
421 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
423 QUrl destUrl
= m_model
->placesItem(index
)->url();
424 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
427 delete m_itemDropEventMimeData
;
428 delete m_itemDropEvent
;
430 m_itemDropEventIndex
= -1;
431 m_itemDropEventMimeData
= nullptr;
432 m_itemDropEvent
= nullptr;
436 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
438 m_model
->dropMimeDataBefore(index
, event
->mimeData());
441 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
443 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
445 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
449 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
451 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
452 this, &PlacesPanel::slotStorageSetupDone
);
454 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
459 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
460 triggerItem(index
, m_triggerStorageSetupButton
);
461 m_triggerStorageSetupButton
= Qt::NoButton
;
463 setUrl(m_storageSetupFailedUrl
);
464 m_storageSetupFailedUrl
= QUrl();
468 void PlacesPanel::addEntry()
470 const int index
= m_controller
->selectionManager()->currentItem();
471 const QUrl url
= m_model
->data(index
).value("url").toUrl();
473 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
474 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
475 dialog
->setAllowGlobal(true);
477 if (dialog
->exec() == QDialog::Accepted
) {
478 m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
484 void PlacesPanel::editEntry(int index
)
486 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
488 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
489 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
490 dialog
->setIcon(data
.value("iconName").toString());
491 dialog
->setText(data
.value("text").toString());
492 dialog
->setUrl(data
.value("url").toUrl());
493 dialog
->setAllowGlobal(true);
494 if (dialog
->exec() == QDialog::Accepted
) {
495 PlacesItem
* oldItem
= m_model
->placesItem(index
);
497 oldItem
->setText(dialog
->text());
498 oldItem
->setUrl(dialog
->url());
499 oldItem
->setIcon(dialog
->icon());
507 void PlacesPanel::selectClosestItem()
509 const int index
= m_model
->closestItem(url());
510 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
511 selectionManager
->setCurrentItem(index
);
512 selectionManager
->clearSelection();
513 selectionManager
->setSelected(index
);
516 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
518 const PlacesItem
* item
= m_model
->placesItem(index
);
523 if (m_model
->storageSetupNeeded(index
)) {
524 m_triggerStorageSetupButton
= button
;
525 m_storageSetupFailedUrl
= url();
527 connect(m_model
, &PlacesItemModel::storageSetupDone
,
528 this, &PlacesPanel::slotStorageSetupDone
);
530 m_model
->requestStorageSetup(index
);
532 m_triggerStorageSetupButton
= Qt::NoButton
;
534 const QUrl url
= m_model
->data(index
).value("url").toUrl();
535 if (!url
.isEmpty()) {
536 if (button
== Qt::MiddleButton
) {
537 emit
placeMiddleClicked(KFilePlacesModel::convertedUrl(url
));
539 emit
placeActivated(KFilePlacesModel::convertedUrl(url
));