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"
30 #include "dolphindebug.h"
34 #include <KIO/DropJob>
35 #include <KIO/EmptyTrashJob>
36 #include <KIO/JobUiDelegate>
37 #include <KFilePlacesModel>
38 #include <KJobWidgets>
39 #include <KLocalizedString>
40 #include <KIconLoader>
41 #include <kitemviews/kitemlistcontainer.h>
42 #include <kitemviews/kitemlistcontroller.h>
43 #include <kitemviews/kitemlistselectionmanager.h>
44 #include <kitemviews/kstandarditem.h>
46 #include <KMessageBox>
47 #include <KNotification>
48 #include "placesitem.h"
49 #include "placesitemeditdialog.h"
50 #include "placesitemlistgroupheader.h"
51 #include "placesitemlistwidget.h"
52 #include "placesitemmodel.h"
53 #include "placesview.h"
54 #include <views/draganddrophelper.h>
55 #include <QGraphicsSceneDragDropEvent>
56 #include <QVBoxLayout>
60 PlacesPanel::PlacesPanel(QWidget
* parent
) :
62 m_controller(nullptr),
65 m_storageSetupFailedUrl(),
66 m_triggerStorageSetupButton(),
67 m_itemDropEventIndex(-1),
68 m_itemDropEventMimeData(nullptr),
69 m_itemDropEvent(nullptr)
73 PlacesPanel::~PlacesPanel()
77 void PlacesPanel::proceedWithTearDown()
79 m_model
->proceedWithTearDown();
82 bool PlacesPanel::urlChanged()
84 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
85 // Skip results shown by a search, as possible identical
86 // directory names are useless without parent-path information.
97 void PlacesPanel::readSettings()
100 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
101 m_controller
->setAutoActivationDelay(delay
);
105 void PlacesPanel::showEvent(QShowEvent
* event
)
107 if (event
->spontaneous()) {
108 Panel::showEvent(event
);
113 // Postpone the creating of the controller to the first show event.
114 // This assures that no performance and memory overhead is given when the folders panel is not
115 // used at all and stays invisible.
116 m_model
= new PlacesItemModel(this);
117 m_model
->setGroupedSorting(true);
118 connect(m_model
, &PlacesItemModel::errorMessage
,
119 this, &PlacesPanel::errorMessage
);
120 connect(m_model
, &PlacesItemModel::storageTearDownRequested
,
121 this, &PlacesPanel::storageTearDownRequested
);
122 connect(m_model
, &PlacesItemModel::storageTearDownExternallyRequested
,
123 this, &PlacesPanel::storageTearDownExternallyRequested
);
125 m_view
= new PlacesView();
126 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
127 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
129 m_controller
= new KItemListController(m_model
, m_view
, this);
130 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
131 m_controller
->setSingleClickActivationEnforced(true);
135 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
136 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
137 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
138 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
139 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
140 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
142 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
143 container
->setEnabledFrame(false);
145 QVBoxLayout
* layout
= new QVBoxLayout(this);
146 layout
->setMargin(0);
147 layout
->addWidget(container
);
152 Panel::showEvent(event
);
155 void PlacesPanel::slotItemActivated(int index
)
157 triggerItem(index
, Qt::LeftButton
);
160 void PlacesPanel::slotItemMiddleClicked(int index
)
162 triggerItem(index
, Qt::MiddleButton
);
165 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
167 PlacesItem
* item
= m_model
->placesItem(index
);
174 QAction
* emptyTrashAction
= nullptr;
175 QAction
* editAction
= nullptr;
176 QAction
* teardownAction
= nullptr;
177 QAction
* ejectAction
= nullptr;
179 const QString label
= item
->text();
181 const bool isDevice
= !item
->udi().isEmpty();
182 const bool isTrash
= (item
->url().scheme() == QLatin1String("trash"));
184 ejectAction
= m_model
->ejectAction(index
);
186 ejectAction
->setParent(&menu
);
187 menu
.addAction(ejectAction
);
190 teardownAction
= m_model
->teardownAction(index
);
191 if (teardownAction
) {
192 teardownAction
->setParent(&menu
);
193 menu
.addAction(teardownAction
);
196 if (teardownAction
|| ejectAction
) {
201 emptyTrashAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
202 emptyTrashAction
->setEnabled(item
->icon() == QLatin1String("user-trash-full"));
207 QAction
* openInNewWindowAction
= menu
.addAction(QIcon::fromTheme("window-new"), i18nc("@item:inmenu", "Open in New Window"));
208 QAction
* openInNewTabAction
= menu
.addAction(QIcon::fromTheme("tab-new"), i18nc("@item:inmenu", "Open in New Tab"));
209 if (!isDevice
&& !isTrash
) {
214 editAction
= menu
.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit..."));
217 QAction
* removeAction
= nullptr;
218 if (!isDevice
&& !item
->isSystemItem()) {
219 removeAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
222 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide"));
223 hideAction
->setCheckable(true);
224 hideAction
->setChecked(item
->isHidden());
226 buildGroupContextMenu(&menu
, index
);
228 QAction
* action
= menu
.exec(pos
.toPoint());
230 if (action
== emptyTrashAction
) {
233 // The index might have changed if devices were added/removed while
234 // the context menu was open.
235 index
= m_model
->index(item
);
237 // The item is not in the model any more, probably because it was an
238 // external device that has been removed while the context menu was open.
242 if (action
== editAction
) {
244 } else if (action
== removeAction
) {
245 m_model
->deleteItem(index
);
246 } else if (action
== hideAction
) {
247 item
->setHidden(hideAction
->isChecked());
248 } else if (action
== openInNewWindowAction
) {
249 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(m_model
->data(index
).value("url").toUrl())}, this);
250 } else if (action
== openInNewTabAction
) {
251 // TriggerItem does set up the storage first and then it will
252 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
253 triggerItem(index
, Qt::MiddleButton
);
254 } else if (action
== teardownAction
) {
255 m_model
->requestTearDown(index
);
256 } else if (action
== ejectAction
) {
257 m_model
->requestEject(index
);
265 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
269 QAction
* addAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
271 QAction
* showAllAction
= nullptr;
272 if (m_model
->hiddenCount() > 0) {
273 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
274 showAllAction
->setCheckable(true);
275 showAllAction
->setChecked(m_model
->hiddenItemsShown());
278 buildGroupContextMenu(&menu
, m_controller
->indexCloseToMousePressedPosition());
280 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
289 const int iconSizeCount
= 4;
290 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
291 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
292 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
293 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
294 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
297 QMap
<QAction
*, int> iconSizeActionMap
;
298 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
300 for (int i
= 0; i
< iconSizeCount
; ++i
) {
301 const int size
= iconSizes
[i
].size
;
302 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
305 QAction
* action
= iconSizeSubMenu
->addAction(text
);
306 iconSizeActionMap
.insert(action
, size
);
307 action
->setActionGroup(iconSizeGroup
);
308 action
->setCheckable(true);
309 action
->setChecked(m_view
->iconSize() == size
);
312 menu
.addMenu(iconSizeSubMenu
);
315 foreach (QAction
* action
, customContextMenuActions()) {
316 menu
.addAction(action
);
319 QAction
* action
= menu
.exec(pos
.toPoint());
321 if (action
== addAction
) {
323 } else if (action
== showAllAction
) {
324 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
325 } else if (iconSizeActionMap
.contains(action
)) {
326 m_view
->setIconSize(iconSizeActionMap
.value(action
));
333 QAction
*PlacesPanel::buildGroupContextMenu(QMenu
*menu
, int index
)
339 KFilePlacesModel::GroupType groupType
= m_model
->groupType(index
);
340 QAction
*hideGroupAction
= menu
->addAction(i18nc("@item:inmenu", "Hide Section '%1'", m_model
->item(index
)->group()));
341 hideGroupAction
->setCheckable(true);
342 hideGroupAction
->setChecked(m_model
->isGroupHidden(groupType
));
344 connect(hideGroupAction
, &QAction::triggered
, this, [this, groupType
, hideGroupAction
]{
345 m_model
->setGroupHidden(groupType
, hideGroupAction
->isChecked());
348 return hideGroupAction
;
351 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
357 const PlacesItem
* destItem
= m_model
->placesItem(index
);
359 if (destItem
->isSearchOrTimelineUrl()) {
363 if (m_model
->storageSetupNeeded(index
)) {
364 connect(m_model
, &PlacesItemModel::storageSetupDone
,
365 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
367 m_itemDropEventIndex
= index
;
369 // Make a full copy of the Mime-Data
370 m_itemDropEventMimeData
= new QMimeData
;
371 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
372 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
373 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
374 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
375 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
377 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
378 event
->possibleActions(),
379 m_itemDropEventMimeData
,
383 m_model
->requestStorageSetup(index
);
387 QUrl destUrl
= destItem
->url();
388 QDropEvent
dropEvent(event
->pos().toPoint(),
389 event
->possibleActions(),
394 slotUrlsDropped(destUrl
, &dropEvent
, this);
397 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
399 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
400 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
402 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
404 QUrl destUrl
= m_model
->placesItem(index
)->url();
405 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
408 delete m_itemDropEventMimeData
;
409 delete m_itemDropEvent
;
411 m_itemDropEventIndex
= -1;
412 m_itemDropEventMimeData
= nullptr;
413 m_itemDropEvent
= nullptr;
417 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
419 m_model
->dropMimeDataBefore(index
, event
->mimeData());
422 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
424 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
426 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
430 void PlacesPanel::slotTrashUpdated(KJob
* job
)
433 emit
errorMessage(job
->errorString());
435 // as long as KIO doesn't do this, do it ourselves
436 KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), nullptr, KNotification::DefaultEvent
);
439 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
441 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
442 this, &PlacesPanel::slotStorageSetupDone
);
444 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
449 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
450 triggerItem(index
, m_triggerStorageSetupButton
);
451 m_triggerStorageSetupButton
= Qt::NoButton
;
453 setUrl(m_storageSetupFailedUrl
);
454 m_storageSetupFailedUrl
= QUrl();
458 void PlacesPanel::emptyTrash()
460 KIO::JobUiDelegate uiDelegate
;
461 uiDelegate
.setWindow(window());
462 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
463 KIO::Job
* job
= KIO::emptyTrash();
464 KJobWidgets::setWindow(job
, window());
465 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
469 void PlacesPanel::addEntry()
471 const int index
= m_controller
->selectionManager()->currentItem();
472 const QUrl url
= m_model
->data(index
).value("url").toUrl();
474 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
475 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
476 dialog
->setAllowGlobal(true);
478 if (dialog
->exec() == QDialog::Accepted
) {
479 m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
485 void PlacesPanel::editEntry(int index
)
487 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
489 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
490 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
491 dialog
->setIcon(data
.value("iconName").toString());
492 dialog
->setText(data
.value("text").toString());
493 dialog
->setUrl(data
.value("url").toUrl());
494 dialog
->setAllowGlobal(true);
495 if (dialog
->exec() == QDialog::Accepted
) {
496 PlacesItem
* oldItem
= m_model
->placesItem(index
);
498 oldItem
->setText(dialog
->text());
499 oldItem
->setUrl(dialog
->url());
500 oldItem
->setIcon(dialog
->icon());
508 void PlacesPanel::selectClosestItem()
510 const int index
= m_model
->closestItem(url());
511 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
512 selectionManager
->setCurrentItem(index
);
513 selectionManager
->clearSelection();
514 selectionManager
->setSelected(index
);
517 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
519 const PlacesItem
* item
= m_model
->placesItem(index
);
524 if (m_model
->storageSetupNeeded(index
)) {
525 m_triggerStorageSetupButton
= button
;
526 m_storageSetupFailedUrl
= url();
528 connect(m_model
, &PlacesItemModel::storageSetupDone
,
529 this, &PlacesPanel::slotStorageSetupDone
);
531 m_model
->requestStorageSetup(index
);
533 m_triggerStorageSetupButton
= Qt::NoButton
;
535 const QUrl url
= m_model
->data(index
).value("url").toUrl();
536 if (!url
.isEmpty()) {
537 if (button
== Qt::MiddleButton
) {
538 emit
placeMiddleClicked(KFilePlacesModel::convertedUrl(url
));
540 emit
placeActivated(KFilePlacesModel::convertedUrl(url
));