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 <KJobWidgets>
38 #include <KLocalizedString>
39 #include <KIconLoader>
40 #include <kitemviews/kitemlistcontainer.h>
41 #include <kitemviews/kitemlistcontroller.h>
42 #include <kitemviews/kitemlistselectionmanager.h>
43 #include <kitemviews/kstandarditem.h>
45 #include <KMessageBox>
46 #include <KNotification>
47 #include "placesitem.h"
48 #include "placesitemeditdialog.h"
49 #include "placesitemlistgroupheader.h"
50 #include "placesitemlistwidget.h"
51 #include "placesitemmodel.h"
52 #include "placesview.h"
53 #include <views/draganddrophelper.h>
54 #include <QGraphicsSceneDragDropEvent>
55 #include <QVBoxLayout>
59 PlacesPanel::PlacesPanel(QWidget
* parent
) :
63 m_storageSetupFailedUrl(),
64 m_triggerStorageSetupButton(),
65 m_itemDropEventIndex(-1),
66 m_itemDropEventMimeData(0),
71 PlacesPanel::~PlacesPanel()
75 bool PlacesPanel::urlChanged()
77 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
78 // Skip results shown by a search, as possible identical
79 // directory names are useless without parent-path information.
90 void PlacesPanel::readSettings()
93 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
94 m_controller
->setAutoActivationDelay(delay
);
98 void PlacesPanel::showEvent(QShowEvent
* event
)
100 if (event
->spontaneous()) {
101 Panel::showEvent(event
);
106 // Postpone the creating of the controller to the first show event.
107 // This assures that no performance and memory overhead is given when the folders panel is not
108 // used at all and stays invisible.
109 m_model
= new PlacesItemModel(this);
110 m_model
->setGroupedSorting(true);
111 connect(m_model
, &PlacesItemModel::errorMessage
,
112 this, &PlacesPanel::errorMessage
);
114 m_view
= new PlacesView();
115 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
116 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
118 m_controller
= new KItemListController(m_model
, m_view
, this);
119 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
120 m_controller
->setSingleClickActivationEnforced(true);
124 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
125 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
126 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
127 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
128 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
129 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
131 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
132 container
->setEnabledFrame(false);
134 QVBoxLayout
* layout
= new QVBoxLayout(this);
135 layout
->setMargin(0);
136 layout
->addWidget(container
);
141 Panel::showEvent(event
);
144 void PlacesPanel::slotItemActivated(int index
)
146 triggerItem(index
, Qt::LeftButton
);
149 void PlacesPanel::slotItemMiddleClicked(int index
)
151 triggerItem(index
, Qt::MiddleButton
);
154 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
156 PlacesItem
* item
= m_model
->placesItem(index
);
163 QAction
* emptyTrashAction
= 0;
164 QAction
* editAction
= 0;
165 QAction
* teardownAction
= 0;
166 QAction
* ejectAction
= 0;
168 const QString label
= item
->text();
170 const bool isDevice
= !item
->udi().isEmpty();
171 const bool isTrash
= (item
->url().scheme() == QLatin1String("trash"));
173 ejectAction
= m_model
->ejectAction(index
);
175 ejectAction
->setParent(&menu
);
176 menu
.addAction(ejectAction
);
179 teardownAction
= m_model
->teardownAction(index
);
180 if (teardownAction
) {
181 teardownAction
->setParent(&menu
);
182 menu
.addAction(teardownAction
);
185 if (teardownAction
|| ejectAction
) {
190 emptyTrashAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
191 emptyTrashAction
->setEnabled(item
->icon() == QLatin1String("user-trash-full"));
196 QAction
* openInNewWindowAction
= menu
.addAction(QIcon::fromTheme("window-new"), i18nc("@item:inmenu", "Open in New Window"));
197 QAction
* openInNewTabAction
= menu
.addAction(QIcon::fromTheme("tab-new"), i18nc("@item:inmenu", "Open in New Tab"));
198 if (!isDevice
&& !isTrash
) {
203 editAction
= menu
.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit..."));
206 QAction
* removeAction
= 0;
207 if (!isDevice
&& !item
->isSystemItem()) {
208 removeAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
211 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide"));
212 hideAction
->setCheckable(true);
213 hideAction
->setChecked(item
->isHidden());
215 QAction
* action
= menu
.exec(pos
.toPoint());
217 if (action
== emptyTrashAction
) {
220 // The index might have changed if devices were added/removed while
221 // the context menu was open.
222 index
= m_model
->index(item
);
224 // The item is not in the model any more, probably because it was an
225 // external device that has been removed while the context menu was open.
229 if (action
== editAction
) {
231 } else if (action
== removeAction
) {
232 m_model
->removeItem(index
);
233 m_model
->saveBookmarks();
234 } else if (action
== hideAction
) {
235 item
->setHidden(hideAction
->isChecked());
236 m_model
->saveBookmarks();
237 } else if (action
== openInNewWindowAction
) {
238 Dolphin::openNewWindow({PlacesItemModel::convertedUrl(m_model
->data(index
).value("url").toUrl())}, this);
239 } else if (action
== openInNewTabAction
) {
240 // TriggerItem does set up the storage first and then it will
241 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
242 triggerItem(index
, Qt::MiddleButton
);
243 } else if (action
== teardownAction
) {
244 m_model
->requestTeardown(index
);
245 } else if (action
== ejectAction
) {
246 m_model
->requestEject(index
);
254 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
258 QAction
* addAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
260 QAction
* showAllAction
= 0;
261 if (m_model
->hiddenCount() > 0) {
262 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
263 showAllAction
->setCheckable(true);
264 showAllAction
->setChecked(m_model
->hiddenItemsShown());
267 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
276 const int iconSizeCount
= 4;
277 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
278 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
279 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
280 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
281 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
284 QMap
<QAction
*, int> iconSizeActionMap
;
285 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
287 for (int i
= 0; i
< iconSizeCount
; ++i
) {
288 const int size
= iconSizes
[i
].size
;
289 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
292 QAction
* action
= iconSizeSubMenu
->addAction(text
);
293 iconSizeActionMap
.insert(action
, size
);
294 action
->setActionGroup(iconSizeGroup
);
295 action
->setCheckable(true);
296 action
->setChecked(m_view
->iconSize() == size
);
299 menu
.addMenu(iconSizeSubMenu
);
302 foreach (QAction
* action
, customContextMenuActions()) {
303 menu
.addAction(action
);
306 QAction
* action
= menu
.exec(pos
.toPoint());
308 if (action
== addAction
) {
310 } else if (action
== showAllAction
) {
311 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
312 } else if (iconSizeActionMap
.contains(action
)) {
313 m_view
->setIconSize(iconSizeActionMap
.value(action
));
320 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
326 const PlacesItem
* destItem
= m_model
->placesItem(index
);
327 const PlacesItem::GroupType group
= destItem
->groupType();
328 if (group
== PlacesItem::SearchForType
|| group
== PlacesItem::RecentlySavedType
) {
332 if (m_model
->storageSetupNeeded(index
)) {
333 connect(m_model
, &PlacesItemModel::storageSetupDone
,
334 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
336 m_itemDropEventIndex
= index
;
338 // Make a full copy of the Mime-Data
339 m_itemDropEventMimeData
= new QMimeData
;
340 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
341 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
342 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
343 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
344 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
346 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
347 event
->possibleActions(),
348 m_itemDropEventMimeData
,
352 m_model
->requestStorageSetup(index
);
356 QUrl destUrl
= destItem
->url();
357 QDropEvent
dropEvent(event
->pos().toPoint(),
358 event
->possibleActions(),
363 slotUrlsDropped(destUrl
, &dropEvent
, this);
366 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
368 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
369 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
371 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
373 QUrl destUrl
= m_model
->placesItem(index
)->url();
374 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
377 delete m_itemDropEventMimeData
;
378 delete m_itemDropEvent
;
380 m_itemDropEventIndex
= -1;
381 m_itemDropEventMimeData
= 0;
386 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
388 m_model
->dropMimeDataBefore(index
, event
->mimeData());
389 m_model
->saveBookmarks();
392 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
394 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
396 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
400 void PlacesPanel::slotTrashUpdated(KJob
* job
)
403 emit
errorMessage(job
->errorString());
405 // as long as KIO doesn't do this, do it ourselves
406 KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), 0, KNotification::DefaultEvent
);
409 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
411 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
412 this, &PlacesPanel::slotStorageSetupDone
);
414 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
419 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
420 triggerItem(index
, m_triggerStorageSetupButton
);
421 m_triggerStorageSetupButton
= Qt::NoButton
;
423 setUrl(m_storageSetupFailedUrl
);
424 m_storageSetupFailedUrl
= QUrl();
428 void PlacesPanel::emptyTrash()
430 KIO::JobUiDelegate uiDelegate
;
431 uiDelegate
.setWindow(window());
432 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
433 KIO::Job
* job
= KIO::emptyTrash();
434 KJobWidgets::setWindow(job
, window());
435 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
439 void PlacesPanel::addEntry()
441 const int index
= m_controller
->selectionManager()->currentItem();
442 const QUrl url
= m_model
->data(index
).value("url").toUrl();
444 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
445 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
446 dialog
->setAllowGlobal(true);
448 if (dialog
->exec() == QDialog::Accepted
) {
449 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
450 m_model
->appendItemToGroup(item
);
451 m_model
->saveBookmarks();
457 void PlacesPanel::editEntry(int index
)
459 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
461 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
462 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
463 dialog
->setIcon(data
.value("iconName").toString());
464 dialog
->setText(data
.value("text").toString());
465 dialog
->setUrl(data
.value("url").toUrl());
466 dialog
->setAllowGlobal(true);
467 if (dialog
->exec() == QDialog::Accepted
) {
468 PlacesItem
* oldItem
= m_model
->placesItem(index
);
470 oldItem
->setText(dialog
->text());
471 oldItem
->setUrl(dialog
->url());
472 oldItem
->setIcon(dialog
->icon());
473 m_model
->saveBookmarks();
480 void PlacesPanel::selectClosestItem()
482 const int index
= m_model
->closestItem(url());
483 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
484 selectionManager
->setCurrentItem(index
);
485 selectionManager
->clearSelection();
486 selectionManager
->setSelected(index
);
489 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
491 const PlacesItem
* item
= m_model
->placesItem(index
);
496 if (m_model
->storageSetupNeeded(index
)) {
497 m_triggerStorageSetupButton
= button
;
498 m_storageSetupFailedUrl
= url();
500 connect(m_model
, &PlacesItemModel::storageSetupDone
,
501 this, &PlacesPanel::slotStorageSetupDone
);
503 m_model
->requestStorageSetup(index
);
505 m_triggerStorageSetupButton
= Qt::NoButton
;
507 const QUrl url
= m_model
->data(index
).value("url").toUrl();
508 if (!url
.isEmpty()) {
509 if (button
== Qt::MiddleButton
) {
510 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
512 emit
placeActivated(PlacesItemModel::convertedUrl(url
));