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"
43 #include <KFilePlacesModel>
44 #include <KIO/DropJob>
45 #include <KIO/EmptyTrashJob>
47 #include <KIO/JobUiDelegate>
48 #include <KIconLoader>
49 #include <KJobWidgets>
50 #include <KLocalizedString>
51 #include <KMessageBox>
52 #include <KNotification>
54 #include <QGraphicsSceneDragDropEvent>
57 #include <QVBoxLayout>
59 PlacesPanel::PlacesPanel(QWidget
* parent
) :
61 m_controller(nullptr),
64 m_storageSetupFailedUrl(),
65 m_triggerStorageSetupButton(),
66 m_itemDropEventIndex(-1),
67 m_itemDropEventMimeData(nullptr),
68 m_itemDropEvent(nullptr)
72 PlacesPanel::~PlacesPanel()
76 void PlacesPanel::proceedWithTearDown()
78 m_model
->proceedWithTearDown();
81 bool PlacesPanel::urlChanged()
83 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
84 // Skip results shown by a search, as possible identical
85 // directory names are useless without parent-path information.
96 void PlacesPanel::readSettings()
99 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
100 m_controller
->setAutoActivationDelay(delay
);
104 void PlacesPanel::showEvent(QShowEvent
* event
)
106 if (event
->spontaneous()) {
107 Panel::showEvent(event
);
112 // Postpone the creating of the controller to the first show event.
113 // This assures that no performance and memory overhead is given when the folders panel is not
114 // used at all and stays invisible.
115 m_model
= new PlacesItemModel(this);
116 m_model
->setGroupedSorting(true);
117 connect(m_model
, &PlacesItemModel::errorMessage
,
118 this, &PlacesPanel::errorMessage
);
119 connect(m_model
, &PlacesItemModel::storageTearDownRequested
,
120 this, &PlacesPanel::storageTearDownRequested
);
121 connect(m_model
, &PlacesItemModel::storageTearDownExternallyRequested
,
122 this, &PlacesPanel::storageTearDownExternallyRequested
);
124 m_view
= new PlacesView();
125 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
126 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
128 m_controller
= new KItemListController(m_model
, m_view
, this);
129 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
130 m_controller
->setSingleClickActivationEnforced(true);
134 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
135 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
136 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
137 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
138 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
139 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
141 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
142 container
->setEnabledFrame(false);
144 QVBoxLayout
* layout
= new QVBoxLayout(this);
145 layout
->setMargin(0);
146 layout
->addWidget(container
);
151 Panel::showEvent(event
);
154 void PlacesPanel::slotItemActivated(int index
)
156 triggerItem(index
, Qt::LeftButton
);
159 void PlacesPanel::slotItemMiddleClicked(int index
)
161 triggerItem(index
, Qt::MiddleButton
);
164 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
166 PlacesItem
* item
= m_model
->placesItem(index
);
173 QAction
* emptyTrashAction
= nullptr;
174 QAction
* editAction
= nullptr;
175 QAction
* teardownAction
= nullptr;
176 QAction
* ejectAction
= nullptr;
178 const bool isDevice
= !item
->udi().isEmpty();
179 const bool isTrash
= (item
->url().scheme() == QLatin1String("trash"));
181 ejectAction
= m_model
->ejectAction(index
);
183 ejectAction
->setParent(&menu
);
184 menu
.addAction(ejectAction
);
187 teardownAction
= m_model
->teardownAction(index
);
188 if (teardownAction
) {
189 teardownAction
->setParent(&menu
);
190 menu
.addAction(teardownAction
);
193 if (teardownAction
|| ejectAction
) {
198 emptyTrashAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
199 emptyTrashAction
->setEnabled(item
->icon() == QLatin1String("user-trash-full"));
204 QAction
* openInNewWindowAction
= menu
.addAction(QIcon::fromTheme("window-new"), i18nc("@item:inmenu", "Open in New Window"));
205 QAction
* openInNewTabAction
= menu
.addAction(QIcon::fromTheme("tab-new"), i18nc("@item:inmenu", "Open in New Tab"));
206 if (!isDevice
&& !isTrash
) {
211 editAction
= menu
.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit..."));
214 QAction
* removeAction
= nullptr;
215 if (!isDevice
&& !item
->isSystemItem()) {
216 removeAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
219 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide"));
220 hideAction
->setCheckable(true);
221 hideAction
->setChecked(item
->isHidden());
223 buildGroupContextMenu(&menu
, index
);
225 QAction
* action
= menu
.exec(pos
.toPoint());
227 if (action
== emptyTrashAction
) {
230 // The index might have changed if devices were added/removed while
231 // the context menu was open.
232 index
= m_model
->index(item
);
234 // The item is not in the model any more, probably because it was an
235 // external device that has been removed while the context menu was open.
239 if (action
== editAction
) {
241 } else if (action
== removeAction
) {
242 m_model
->deleteItem(index
);
243 } else if (action
== hideAction
) {
244 item
->setHidden(hideAction
->isChecked());
245 } else if (action
== openInNewWindowAction
) {
246 Dolphin::openNewWindow({KFilePlacesModel::convertedUrl(m_model
->data(index
).value("url").toUrl())}, this);
247 } else if (action
== openInNewTabAction
) {
248 // TriggerItem does set up the storage first and then it will
249 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
250 triggerItem(index
, Qt::MiddleButton
);
251 } else if (action
== teardownAction
) {
252 m_model
->requestTearDown(index
);
253 } else if (action
== ejectAction
) {
254 m_model
->requestEject(index
);
262 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
266 QAction
* addAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
268 QAction
* showAllAction
= nullptr;
269 if (m_model
->hiddenCount() > 0) {
270 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
271 showAllAction
->setCheckable(true);
272 showAllAction
->setChecked(m_model
->hiddenItemsShown());
275 buildGroupContextMenu(&menu
, m_controller
->indexCloseToMousePressedPosition());
277 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
286 const int iconSizeCount
= 4;
287 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
288 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
289 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
290 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
291 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
294 QHash
<QAction
*, int> iconSizeActionMap
;
295 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
297 for (int i
= 0; i
< iconSizeCount
; ++i
) {
298 const int size
= iconSizes
[i
].size
;
299 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
302 QAction
* action
= iconSizeSubMenu
->addAction(text
);
303 iconSizeActionMap
.insert(action
, size
);
304 action
->setActionGroup(iconSizeGroup
);
305 action
->setCheckable(true);
306 action
->setChecked(m_view
->iconSize() == size
);
309 menu
.addMenu(iconSizeSubMenu
);
312 foreach (QAction
* action
, customContextMenuActions()) {
313 menu
.addAction(action
);
316 QAction
* action
= menu
.exec(pos
.toPoint());
318 if (action
== addAction
) {
320 } else if (action
== showAllAction
) {
321 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
322 } else if (iconSizeActionMap
.contains(action
)) {
323 m_view
->setIconSize(iconSizeActionMap
.value(action
));
330 QAction
*PlacesPanel::buildGroupContextMenu(QMenu
*menu
, int index
)
336 KFilePlacesModel::GroupType groupType
= m_model
->groupType(index
);
337 QAction
*hideGroupAction
= menu
->addAction(i18nc("@item:inmenu", "Hide Section '%1'", m_model
->item(index
)->group()));
338 hideGroupAction
->setCheckable(true);
339 hideGroupAction
->setChecked(m_model
->isGroupHidden(groupType
));
341 connect(hideGroupAction
, &QAction::triggered
, this, [this, groupType
, hideGroupAction
]{
342 m_model
->setGroupHidden(groupType
, hideGroupAction
->isChecked());
345 return hideGroupAction
;
348 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
354 const PlacesItem
* destItem
= m_model
->placesItem(index
);
356 if (destItem
->isSearchOrTimelineUrl()) {
360 if (m_model
->storageSetupNeeded(index
)) {
361 connect(m_model
, &PlacesItemModel::storageSetupDone
,
362 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
364 m_itemDropEventIndex
= index
;
366 // Make a full copy of the Mime-Data
367 m_itemDropEventMimeData
= new QMimeData
;
368 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
369 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
370 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
371 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
372 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
374 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
375 event
->possibleActions(),
376 m_itemDropEventMimeData
,
380 m_model
->requestStorageSetup(index
);
384 QUrl destUrl
= destItem
->url();
385 QDropEvent
dropEvent(event
->pos().toPoint(),
386 event
->possibleActions(),
391 slotUrlsDropped(destUrl
, &dropEvent
, this);
394 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
396 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
397 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
399 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
401 QUrl destUrl
= m_model
->placesItem(index
)->url();
402 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
405 delete m_itemDropEventMimeData
;
406 delete m_itemDropEvent
;
408 m_itemDropEventIndex
= -1;
409 m_itemDropEventMimeData
= nullptr;
410 m_itemDropEvent
= nullptr;
414 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
416 m_model
->dropMimeDataBefore(index
, event
->mimeData());
419 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
421 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
423 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
427 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
429 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
430 this, &PlacesPanel::slotStorageSetupDone
);
432 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
437 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
438 triggerItem(index
, m_triggerStorageSetupButton
);
439 m_triggerStorageSetupButton
= Qt::NoButton
;
441 setUrl(m_storageSetupFailedUrl
);
442 m_storageSetupFailedUrl
= QUrl();
446 void PlacesPanel::addEntry()
448 const int index
= m_controller
->selectionManager()->currentItem();
449 const QUrl url
= m_model
->data(index
).value("url").toUrl();
451 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
452 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
453 dialog
->setAllowGlobal(true);
455 if (dialog
->exec() == QDialog::Accepted
) {
456 m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
462 void PlacesPanel::editEntry(int index
)
464 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
466 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
467 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
468 dialog
->setIcon(data
.value("iconName").toString());
469 dialog
->setText(data
.value("text").toString());
470 dialog
->setUrl(data
.value("url").toUrl());
471 dialog
->setAllowGlobal(true);
472 if (dialog
->exec() == QDialog::Accepted
) {
473 PlacesItem
* oldItem
= m_model
->placesItem(index
);
475 oldItem
->setText(dialog
->text());
476 oldItem
->setUrl(dialog
->url());
477 oldItem
->setIcon(dialog
->icon());
485 void PlacesPanel::selectClosestItem()
487 const int index
= m_model
->closestItem(url());
488 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
489 selectionManager
->setCurrentItem(index
);
490 selectionManager
->clearSelection();
491 selectionManager
->setSelected(index
);
494 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
496 const PlacesItem
* item
= m_model
->placesItem(index
);
501 if (m_model
->storageSetupNeeded(index
)) {
502 m_triggerStorageSetupButton
= button
;
503 m_storageSetupFailedUrl
= url();
505 connect(m_model
, &PlacesItemModel::storageSetupDone
,
506 this, &PlacesPanel::slotStorageSetupDone
);
508 m_model
->requestStorageSetup(index
);
510 m_triggerStorageSetupButton
= Qt::NoButton
;
512 const QUrl url
= m_model
->data(index
).value("url").toUrl();
513 if (!url
.isEmpty()) {
514 if (button
== Qt::MiddleButton
) {
515 emit
placeMiddleClicked(KFilePlacesModel::convertedUrl(url
));
517 emit
placeActivated(KFilePlacesModel::convertedUrl(url
));