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"
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("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
* addAction
= 0;
164 QAction
* mainSeparator
= 0;
165 QAction
* editAction
= 0;
166 QAction
* teardownAction
= 0;
167 QAction
* ejectAction
= 0;
169 const QString label
= item
->text();
171 const bool isDevice
= !item
->udi().isEmpty();
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
) {
186 mainSeparator
= menu
.addSeparator();
189 if (item
->url() == QUrl("trash:/")) {
190 emptyTrashAction
= menu
.addAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
191 emptyTrashAction
->setEnabled(item
->icon() == "user-trash-full");
194 addAction
= menu
.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
195 mainSeparator
= menu
.addSeparator();
196 editAction
= menu
.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label
));
200 addAction
= menu
.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
203 QAction
* openInNewTabAction
= menu
.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label
));
204 openInNewTabAction
->setIcon(QIcon::fromTheme("tab-new"));
206 QAction
* removeAction
= 0;
207 if (!isDevice
&& !item
->isSystemItem()) {
208 removeAction
= menu
.addAction(QIcon::fromTheme("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label
));
211 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide '%1'", label
));
212 hideAction
->setCheckable(true);
213 hideAction
->setChecked(item
->isHidden());
215 QAction
* showAllAction
= 0;
216 if (m_model
->hiddenCount() > 0) {
217 if (!mainSeparator
) {
218 mainSeparator
= menu
.addSeparator();
220 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
221 showAllAction
->setCheckable(true);
222 showAllAction
->setChecked(m_model
->hiddenItemsShown());
226 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
235 const int iconSizeCount
= 4;
236 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
237 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
238 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
239 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
240 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
243 QMap
<QAction
*, int> iconSizeActionMap
;
244 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
246 for (int i
= 0; i
< iconSizeCount
; ++i
) {
247 const int size
= iconSizes
[i
].size
;
248 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
251 QAction
* action
= iconSizeSubMenu
->addAction(text
);
252 iconSizeActionMap
.insert(action
, size
);
253 action
->setActionGroup(iconSizeGroup
);
254 action
->setCheckable(true);
255 action
->setChecked(m_view
->iconSize() == size
);
258 menu
.addMenu(iconSizeSubMenu
);
261 foreach (QAction
* action
, customContextMenuActions()) {
262 menu
.addAction(action
);
265 QAction
* action
= menu
.exec(pos
.toPoint());
267 if (action
== emptyTrashAction
) {
269 } else if (action
== addAction
) {
271 } else if (action
== showAllAction
) {
272 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
273 } else if (iconSizeActionMap
.contains(action
)) {
274 m_view
->setIconSize(iconSizeActionMap
.value(action
));
276 // The index might have changed if devices were added/removed while
277 // the context menu was open.
278 index
= m_model
->index(item
);
280 // The item is not in the model any more, probably because it was an
281 // external device that has been removed while the context menu was open.
285 if (action
== editAction
) {
287 } else if (action
== removeAction
) {
288 m_model
->removeItem(index
);
289 } else if (action
== hideAction
) {
290 item
->setHidden(hideAction
->isChecked());
291 } else if (action
== openInNewTabAction
) {
292 // TriggerItem does set up the storage first and then it will
293 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
294 triggerItem(index
, Qt::MiddleButton
);
295 } else if (action
== teardownAction
) {
296 m_model
->requestTeardown(index
);
297 } else if (action
== ejectAction
) {
298 m_model
->requestEject(index
);
306 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
310 QAction
* addAction
= menu
.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
312 QAction
* showAllAction
= 0;
313 if (m_model
->hiddenCount() > 0) {
314 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
315 showAllAction
->setCheckable(true);
316 showAllAction
->setChecked(m_model
->hiddenItemsShown());
320 foreach (QAction
* action
, customContextMenuActions()) {
321 menu
.addAction(action
);
324 QAction
* action
= menu
.exec(pos
.toPoint());
326 if (action
== addAction
) {
328 } else if (action
== showAllAction
) {
329 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
336 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
342 const PlacesItem
* destItem
= m_model
->placesItem(index
);
343 const PlacesItem::GroupType group
= destItem
->groupType();
344 if (group
== PlacesItem::SearchForType
|| group
== PlacesItem::RecentlySavedType
) {
348 if (m_model
->storageSetupNeeded(index
)) {
349 connect(m_model
, &PlacesItemModel::storageSetupDone
,
350 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
352 m_itemDropEventIndex
= index
;
354 // Make a full copy of the Mime-Data
355 m_itemDropEventMimeData
= new QMimeData
;
356 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
357 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
358 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
359 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
360 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
362 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
363 event
->possibleActions(),
364 m_itemDropEventMimeData
,
368 m_model
->requestStorageSetup(index
);
372 QUrl destUrl
= destItem
->url();
373 QDropEvent
dropEvent(event
->pos().toPoint(),
374 event
->possibleActions(),
379 slotUrlsDropped(destUrl
, &dropEvent
, this);
382 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
384 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
385 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
387 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
389 QUrl destUrl
= m_model
->placesItem(index
)->url();
390 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
393 delete m_itemDropEventMimeData
;
394 delete m_itemDropEvent
;
396 m_itemDropEventIndex
= -1;
397 m_itemDropEventMimeData
= 0;
402 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
404 m_model
->dropMimeDataBefore(index
, event
->mimeData());
407 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
409 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
411 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
415 void PlacesPanel::slotTrashUpdated(KJob
* job
)
418 emit
errorMessage(job
->errorString());
420 // as long as KIO doesn't do this, do it ourselves
421 KNotification::event("Trash: emptied", QString(), QPixmap(), 0, KNotification::DefaultEvent
);
424 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
426 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
427 this, &PlacesPanel::slotStorageSetupDone
);
429 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
434 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
435 triggerItem(index
, m_triggerStorageSetupButton
);
436 m_triggerStorageSetupButton
= Qt::NoButton
;
438 setUrl(m_storageSetupFailedUrl
);
439 m_storageSetupFailedUrl
= QUrl();
443 void PlacesPanel::emptyTrash()
445 KIO::JobUiDelegate uiDelegate
;
446 uiDelegate
.setWindow(window());
447 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
448 KIO::Job
* job
= KIO::emptyTrash();
449 KJobWidgets::setWindow(job
, window());
450 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
454 void PlacesPanel::addEntry()
456 const int index
= m_controller
->selectionManager()->currentItem();
457 const QUrl url
= m_model
->data(index
).value("url").value
<QUrl
>();
459 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
460 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
461 dialog
->setAllowGlobal(true);
463 if (dialog
->exec() == QDialog::Accepted
) {
464 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
465 m_model
->appendItemToGroup(item
);
471 void PlacesPanel::editEntry(int index
)
473 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
475 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
476 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
477 dialog
->setIcon(data
.value("iconName").toString());
478 dialog
->setText(data
.value("text").toString());
479 dialog
->setUrl(data
.value("url").value
<QUrl
>());
480 dialog
->setAllowGlobal(true);
481 if (dialog
->exec() == QDialog::Accepted
) {
482 PlacesItem
* oldItem
= m_model
->placesItem(index
);
484 oldItem
->setText(dialog
->text());
485 oldItem
->setUrl(dialog
->url());
486 oldItem
->setIcon(dialog
->icon());
493 void PlacesPanel::selectClosestItem()
495 const int index
= m_model
->closestItem(url());
496 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
497 selectionManager
->setCurrentItem(index
);
498 selectionManager
->clearSelection();
499 selectionManager
->setSelected(index
);
502 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
504 const PlacesItem
* item
= m_model
->placesItem(index
);
509 if (m_model
->storageSetupNeeded(index
)) {
510 m_triggerStorageSetupButton
= button
;
511 m_storageSetupFailedUrl
= url();
513 connect(m_model
, &PlacesItemModel::storageSetupDone
,
514 this, &PlacesPanel::slotStorageSetupDone
);
516 m_model
->requestStorageSetup(index
);
518 m_triggerStorageSetupButton
= Qt::NoButton
;
520 const QUrl url
= m_model
->data(index
).value("url").value
<QUrl
>();
521 if (!url
.isEmpty()) {
522 if (button
== Qt::MiddleButton
) {
523 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
525 emit
placeActivated(PlacesItemModel::convertedUrl(url
));