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"
29 #include "dolphindebug.h"
33 #include <KIO/DropJob>
34 #include <KIO/EmptyTrashJob>
35 #include <KIO/JobUiDelegate>
36 #include <KJobWidgets>
37 #include <KLocalizedString>
38 #include <KIconLoader>
39 #include <kitemviews/kitemlistcontainer.h>
40 #include <kitemviews/kitemlistcontroller.h>
41 #include <kitemviews/kitemlistselectionmanager.h>
42 #include <kitemviews/kstandarditem.h>
44 #include <KMessageBox>
45 #include <KNotification>
46 #include "placesitem.h"
47 #include "placesitemeditdialog.h"
48 #include "placesitemlistgroupheader.h"
49 #include "placesitemlistwidget.h"
50 #include "placesitemmodel.h"
51 #include "placesview.h"
52 #include <views/draganddrophelper.h>
53 #include <QGraphicsSceneDragDropEvent>
54 #include <QVBoxLayout>
58 PlacesPanel::PlacesPanel(QWidget
* parent
) :
62 m_storageSetupFailedUrl(),
63 m_triggerStorageSetupButton(),
64 m_itemDropEventIndex(-1),
65 m_itemDropEventMimeData(0),
70 PlacesPanel::~PlacesPanel()
74 bool PlacesPanel::urlChanged()
76 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
77 // Skip results shown by a search, as possible identical
78 // directory names are useless without parent-path information.
89 void PlacesPanel::readSettings()
92 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
93 m_controller
->setAutoActivationDelay(delay
);
97 void PlacesPanel::showEvent(QShowEvent
* event
)
99 if (event
->spontaneous()) {
100 Panel::showEvent(event
);
105 // Postpone the creating of the controller to the first show event.
106 // This assures that no performance and memory overhead is given when the folders panel is not
107 // used at all and stays invisible.
108 m_model
= new PlacesItemModel(this);
109 m_model
->setGroupedSorting(true);
110 connect(m_model
, &PlacesItemModel::errorMessage
,
111 this, &PlacesPanel::errorMessage
);
113 m_view
= new PlacesView();
114 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
115 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
117 m_controller
= new KItemListController(m_model
, m_view
, this);
118 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
119 m_controller
->setSingleClickActivationEnforced(true);
123 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
124 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
125 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
126 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
127 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
128 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
130 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
131 container
->setEnabledFrame(false);
133 QVBoxLayout
* layout
= new QVBoxLayout(this);
134 layout
->setMargin(0);
135 layout
->addWidget(container
);
140 Panel::showEvent(event
);
143 void PlacesPanel::slotItemActivated(int index
)
145 triggerItem(index
, Qt::LeftButton
);
148 void PlacesPanel::slotItemMiddleClicked(int index
)
150 triggerItem(index
, Qt::MiddleButton
);
153 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
155 PlacesItem
* item
= m_model
->placesItem(index
);
162 QAction
* emptyTrashAction
= 0;
163 QAction
* editAction
= 0;
164 QAction
* teardownAction
= 0;
165 QAction
* ejectAction
= 0;
167 const QString label
= item
->text();
169 const bool isDevice
= !item
->udi().isEmpty();
170 const bool isTrash
= (item
->url().scheme() == QLatin1String("trash"));
172 ejectAction
= m_model
->ejectAction(index
);
174 ejectAction
->setParent(&menu
);
175 menu
.addAction(ejectAction
);
178 teardownAction
= m_model
->teardownAction(index
);
179 if (teardownAction
) {
180 teardownAction
->setParent(&menu
);
181 menu
.addAction(teardownAction
);
184 if (teardownAction
|| ejectAction
) {
189 emptyTrashAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
190 emptyTrashAction
->setEnabled(item
->icon() == QLatin1String("user-trash-full"));
195 QAction
* openInNewTabAction
= menu
.addAction(QIcon::fromTheme("tab-new"), i18nc("@item:inmenu", "Open in New Tab"));
196 if (!isDevice
&& !isTrash
) {
200 editAction
= menu
.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit..."));
202 QAction
* removeAction
= 0;
203 if (!isDevice
&& !item
->isSystemItem()) {
204 removeAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
207 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide"));
208 hideAction
->setCheckable(true);
209 hideAction
->setChecked(item
->isHidden());
211 QAction
* action
= menu
.exec(pos
.toPoint());
213 if (action
== emptyTrashAction
) {
216 // The index might have changed if devices were added/removed while
217 // the context menu was open.
218 index
= m_model
->index(item
);
220 // The item is not in the model any more, probably because it was an
221 // external device that has been removed while the context menu was open.
225 if (action
== editAction
) {
227 } else if (action
== removeAction
) {
228 m_model
->removeItem(index
);
229 m_model
->saveBookmarks();
230 } else if (action
== hideAction
) {
231 item
->setHidden(hideAction
->isChecked());
232 m_model
->saveBookmarks();
233 } else if (action
== openInNewTabAction
) {
234 // TriggerItem does set up the storage first and then it will
235 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
236 triggerItem(index
, Qt::MiddleButton
);
237 } else if (action
== teardownAction
) {
238 m_model
->requestTeardown(index
);
239 } else if (action
== ejectAction
) {
240 m_model
->requestEject(index
);
248 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
252 QAction
* addAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
254 QAction
* showAllAction
= 0;
255 if (m_model
->hiddenCount() > 0) {
256 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
257 showAllAction
->setCheckable(true);
258 showAllAction
->setChecked(m_model
->hiddenItemsShown());
261 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
270 const int iconSizeCount
= 4;
271 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
272 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
273 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
274 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
275 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
278 QMap
<QAction
*, int> iconSizeActionMap
;
279 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
281 for (int i
= 0; i
< iconSizeCount
; ++i
) {
282 const int size
= iconSizes
[i
].size
;
283 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
286 QAction
* action
= iconSizeSubMenu
->addAction(text
);
287 iconSizeActionMap
.insert(action
, size
);
288 action
->setActionGroup(iconSizeGroup
);
289 action
->setCheckable(true);
290 action
->setChecked(m_view
->iconSize() == size
);
293 menu
.addMenu(iconSizeSubMenu
);
296 foreach (QAction
* action
, customContextMenuActions()) {
297 menu
.addAction(action
);
300 QAction
* action
= menu
.exec(pos
.toPoint());
302 if (action
== addAction
) {
304 } else if (action
== showAllAction
) {
305 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
306 } else if (iconSizeActionMap
.contains(action
)) {
307 m_view
->setIconSize(iconSizeActionMap
.value(action
));
314 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
320 const PlacesItem
* destItem
= m_model
->placesItem(index
);
321 const PlacesItem::GroupType group
= destItem
->groupType();
322 if (group
== PlacesItem::SearchForType
|| group
== PlacesItem::RecentlySavedType
) {
326 if (m_model
->storageSetupNeeded(index
)) {
327 connect(m_model
, &PlacesItemModel::storageSetupDone
,
328 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
330 m_itemDropEventIndex
= index
;
332 // Make a full copy of the Mime-Data
333 m_itemDropEventMimeData
= new QMimeData
;
334 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
335 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
336 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
337 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
338 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
340 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
341 event
->possibleActions(),
342 m_itemDropEventMimeData
,
346 m_model
->requestStorageSetup(index
);
350 QUrl destUrl
= destItem
->url();
351 QDropEvent
dropEvent(event
->pos().toPoint(),
352 event
->possibleActions(),
357 slotUrlsDropped(destUrl
, &dropEvent
, this);
360 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
362 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
363 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
365 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
367 QUrl destUrl
= m_model
->placesItem(index
)->url();
368 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
371 delete m_itemDropEventMimeData
;
372 delete m_itemDropEvent
;
374 m_itemDropEventIndex
= -1;
375 m_itemDropEventMimeData
= 0;
380 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
382 m_model
->dropMimeDataBefore(index
, event
->mimeData());
383 m_model
->saveBookmarks();
386 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
388 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
390 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
394 void PlacesPanel::slotTrashUpdated(KJob
* job
)
397 emit
errorMessage(job
->errorString());
399 // as long as KIO doesn't do this, do it ourselves
400 KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), 0, KNotification::DefaultEvent
);
403 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
405 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
406 this, &PlacesPanel::slotStorageSetupDone
);
408 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
413 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
414 triggerItem(index
, m_triggerStorageSetupButton
);
415 m_triggerStorageSetupButton
= Qt::NoButton
;
417 setUrl(m_storageSetupFailedUrl
);
418 m_storageSetupFailedUrl
= QUrl();
422 void PlacesPanel::emptyTrash()
424 KIO::JobUiDelegate uiDelegate
;
425 uiDelegate
.setWindow(window());
426 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
427 KIO::Job
* job
= KIO::emptyTrash();
428 KJobWidgets::setWindow(job
, window());
429 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
433 void PlacesPanel::addEntry()
435 const int index
= m_controller
->selectionManager()->currentItem();
436 const QUrl url
= m_model
->data(index
).value("url").toUrl();
438 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
439 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
440 dialog
->setAllowGlobal(true);
442 if (dialog
->exec() == QDialog::Accepted
) {
443 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
444 m_model
->appendItemToGroup(item
);
445 m_model
->saveBookmarks();
451 void PlacesPanel::editEntry(int index
)
453 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
455 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
456 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
457 dialog
->setIcon(data
.value("iconName").toString());
458 dialog
->setText(data
.value("text").toString());
459 dialog
->setUrl(data
.value("url").toUrl());
460 dialog
->setAllowGlobal(true);
461 if (dialog
->exec() == QDialog::Accepted
) {
462 PlacesItem
* oldItem
= m_model
->placesItem(index
);
464 oldItem
->setText(dialog
->text());
465 oldItem
->setUrl(dialog
->url());
466 oldItem
->setIcon(dialog
->icon());
467 m_model
->saveBookmarks();
474 void PlacesPanel::selectClosestItem()
476 const int index
= m_model
->closestItem(url());
477 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
478 selectionManager
->setCurrentItem(index
);
479 selectionManager
->clearSelection();
480 selectionManager
->setSelected(index
);
483 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
485 const PlacesItem
* item
= m_model
->placesItem(index
);
490 if (m_model
->storageSetupNeeded(index
)) {
491 m_triggerStorageSetupButton
= button
;
492 m_storageSetupFailedUrl
= url();
494 connect(m_model
, &PlacesItemModel::storageSetupDone
,
495 this, &PlacesPanel::slotStorageSetupDone
);
497 m_model
->requestStorageSetup(index
);
499 m_triggerStorageSetupButton
= Qt::NoButton
;
501 const QUrl url
= m_model
->data(index
).value("url").toUrl();
502 if (!url
.isEmpty()) {
503 if (button
== Qt::MiddleButton
) {
504 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
506 emit
placeActivated(PlacesItemModel::convertedUrl(url
));