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/EmptyTrashJob>
34 #include <KIO/JobUiDelegate>
35 #include <KJobWidgets>
36 #include <KLocalizedString>
37 #include <KIconLoader>
38 #include <kitemviews/kitemlistcontainer.h>
39 #include <kitemviews/kitemlistcontroller.h>
40 #include <kitemviews/kitemlistselectionmanager.h>
41 #include <kitemviews/kstandarditem.h>
43 #include <KMessageBox>
44 #include <KNotification>
45 #include "placesitem.h"
46 #include "placesitemeditdialog.h"
47 #include "placesitemlistgroupheader.h"
48 #include "placesitemlistwidget.h"
49 #include "placesitemmodel.h"
50 #include "placesview.h"
51 #include <views/draganddrophelper.h>
52 #include <QGraphicsSceneDragDropEvent>
53 #include <QVBoxLayout>
57 PlacesPanel::PlacesPanel(QWidget
* parent
) :
61 m_storageSetupFailedUrl(),
62 m_triggerStorageSetupButton(),
63 m_itemDropEventIndex(-1),
64 m_itemDropEventMimeData(0),
69 PlacesPanel::~PlacesPanel()
73 bool PlacesPanel::urlChanged()
75 if (!url().isValid() || url().protocol().contains("search")) {
76 // Skip results shown by a search, as possible identical
77 // directory names are useless without parent-path information.
88 void PlacesPanel::readSettings()
91 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
92 m_controller
->setAutoActivationDelay(delay
);
96 void PlacesPanel::showEvent(QShowEvent
* event
)
98 if (event
->spontaneous()) {
99 Panel::showEvent(event
);
104 // Postpone the creating of the controller to the first show event.
105 // This assures that no performance and memory overhead is given when the folders panel is not
106 // used at all and stays invisible.
107 m_model
= new PlacesItemModel(this);
108 m_model
->setGroupedSorting(true);
109 connect(m_model
, &PlacesItemModel::errorMessage
,
110 this, &PlacesPanel::errorMessage
);
112 m_view
= new PlacesView();
113 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
114 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
116 m_controller
= new KItemListController(m_model
, m_view
, this);
117 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
118 m_controller
->setSingleClickActivationEnforced(true);
122 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
123 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
124 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
125 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
126 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
127 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
129 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
130 container
->setEnabledFrame(false);
132 QVBoxLayout
* layout
= new QVBoxLayout(this);
133 layout
->setMargin(0);
134 layout
->addWidget(container
);
139 Panel::showEvent(event
);
142 void PlacesPanel::slotItemActivated(int index
)
144 triggerItem(index
, Qt::LeftButton
);
147 void PlacesPanel::slotItemMiddleClicked(int index
)
149 triggerItem(index
, Qt::MiddleButton
);
152 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
154 PlacesItem
* item
= m_model
->placesItem(index
);
161 QAction
* emptyTrashAction
= 0;
162 QAction
* addAction
= 0;
163 QAction
* mainSeparator
= 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();
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
) {
185 mainSeparator
= menu
.addSeparator();
188 if (item
->url() == KUrl("trash:/")) {
189 emptyTrashAction
= menu
.addAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
190 emptyTrashAction
->setEnabled(item
->icon() == "user-trash-full");
193 addAction
= menu
.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
194 mainSeparator
= menu
.addSeparator();
195 editAction
= menu
.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label
));
199 addAction
= menu
.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
202 QAction
* openInNewTabAction
= menu
.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label
));
203 openInNewTabAction
->setIcon(QIcon::fromTheme("tab-new"));
205 QAction
* removeAction
= 0;
206 if (!isDevice
&& !item
->isSystemItem()) {
207 removeAction
= menu
.addAction(QIcon::fromTheme("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label
));
210 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide '%1'", label
));
211 hideAction
->setCheckable(true);
212 hideAction
->setChecked(item
->isHidden());
214 QAction
* showAllAction
= 0;
215 if (m_model
->hiddenCount() > 0) {
216 if (!mainSeparator
) {
217 mainSeparator
= menu
.addSeparator();
219 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
220 showAllAction
->setCheckable(true);
221 showAllAction
->setChecked(m_model
->hiddenItemsShown());
225 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
234 const int iconSizeCount
= 4;
235 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
236 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
237 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
238 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
239 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
242 QMap
<QAction
*, int> iconSizeActionMap
;
243 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
245 for (int i
= 0; i
< iconSizeCount
; ++i
) {
246 const int size
= iconSizes
[i
].size
;
247 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
250 QAction
* action
= iconSizeSubMenu
->addAction(text
);
251 iconSizeActionMap
.insert(action
, size
);
252 action
->setActionGroup(iconSizeGroup
);
253 action
->setCheckable(true);
254 action
->setChecked(m_view
->iconSize() == size
);
257 menu
.addMenu(iconSizeSubMenu
);
260 foreach (QAction
* action
, customContextMenuActions()) {
261 menu
.addAction(action
);
264 QAction
* action
= menu
.exec(pos
.toPoint());
266 if (action
== emptyTrashAction
) {
268 } else if (action
== addAction
) {
270 } else if (action
== showAllAction
) {
271 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
272 } else if (iconSizeActionMap
.contains(action
)) {
273 m_view
->setIconSize(iconSizeActionMap
.value(action
));
275 // The index might have changed if devices were added/removed while
276 // the context menu was open.
277 index
= m_model
->index(item
);
279 // The item is not in the model any more, probably because it was an
280 // external device that has been removed while the context menu was open.
284 if (action
== editAction
) {
286 } else if (action
== removeAction
) {
287 m_model
->removeItem(index
);
288 } else if (action
== hideAction
) {
289 item
->setHidden(hideAction
->isChecked());
290 } else if (action
== openInNewTabAction
) {
291 // TriggerItem does set up the storage first and then it will
292 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
293 triggerItem(index
, Qt::MiddleButton
);
294 } else if (action
== teardownAction
) {
295 m_model
->requestTeardown(index
);
296 } else if (action
== ejectAction
) {
297 m_model
->requestEject(index
);
305 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
309 QAction
* addAction
= menu
.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
311 QAction
* showAllAction
= 0;
312 if (m_model
->hiddenCount() > 0) {
313 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
314 showAllAction
->setCheckable(true);
315 showAllAction
->setChecked(m_model
->hiddenItemsShown());
319 foreach (QAction
* action
, customContextMenuActions()) {
320 menu
.addAction(action
);
323 QAction
* action
= menu
.exec(pos
.toPoint());
325 if (action
== addAction
) {
327 } else if (action
== showAllAction
) {
328 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
335 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
341 const PlacesItem
* destItem
= m_model
->placesItem(index
);
342 const PlacesItem::GroupType group
= destItem
->groupType();
343 if (group
== PlacesItem::SearchForType
|| group
== PlacesItem::RecentlySavedType
) {
347 if (m_model
->storageSetupNeeded(index
)) {
348 connect(m_model
, &PlacesItemModel::storageSetupDone
,
349 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
351 m_itemDropEventIndex
= index
;
353 // Make a full copy of the Mime-Data
354 m_itemDropEventMimeData
= new QMimeData
;
355 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
356 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
357 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
358 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
359 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
361 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
362 event
->possibleActions(),
363 m_itemDropEventMimeData
,
367 m_model
->requestStorageSetup(index
);
371 KUrl destUrl
= destItem
->url();
372 QDropEvent
dropEvent(event
->pos().toPoint(),
373 event
->possibleActions(),
379 DragAndDropHelper::dropUrls(KFileItem(), destUrl
, &dropEvent
, error
);
380 if (!error
.isEmpty()) {
381 emit
errorMessage(error
);
385 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
387 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
388 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
390 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
392 KUrl destUrl
= m_model
->placesItem(index
)->url();
395 DragAndDropHelper::dropUrls(KFileItem(), destUrl
, m_itemDropEvent
, error
);
396 if (!error
.isEmpty()) {
397 emit
errorMessage(error
);
401 delete m_itemDropEventMimeData
;
402 delete m_itemDropEvent
;
404 m_itemDropEventIndex
= -1;
405 m_itemDropEventMimeData
= 0;
410 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
412 m_model
->dropMimeDataBefore(index
, event
->mimeData());
415 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
419 DragAndDropHelper::dropUrls(KFileItem(), dest
, event
, error
);
420 if (!error
.isEmpty()) {
421 emit
errorMessage(error
);
426 void PlacesPanel::slotTrashUpdated(KJob
* job
)
429 emit
errorMessage(job
->errorString());
431 // as long as KIO doesn't do this, do it ourselves
432 KNotification::event("Trash: emptied", QString(), QPixmap(), 0, KNotification::DefaultEvent
);
435 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
437 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
438 this, &PlacesPanel::slotStorageSetupDone
);
440 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
445 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
446 triggerItem(index
, m_triggerStorageSetupButton
);
447 m_triggerStorageSetupButton
= Qt::NoButton
;
449 setUrl(m_storageSetupFailedUrl
);
450 m_storageSetupFailedUrl
= KUrl();
454 void PlacesPanel::emptyTrash()
456 KIO::JobUiDelegate uiDelegate
;
457 uiDelegate
.setWindow(window());
458 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
459 KIO::Job
* job
= KIO::emptyTrash();
460 KJobWidgets::setWindow(job
, window());
461 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
465 void PlacesPanel::addEntry()
467 const int index
= m_controller
->selectionManager()->currentItem();
468 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
470 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
471 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
472 dialog
->setAllowGlobal(true);
474 if (dialog
->exec() == QDialog::Accepted
) {
475 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
476 m_model
->appendItemToGroup(item
);
482 void PlacesPanel::editEntry(int index
)
484 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
486 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
487 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
488 dialog
->setIcon(data
.value("iconName").toString());
489 dialog
->setText(data
.value("text").toString());
490 dialog
->setUrl(data
.value("url").value
<KUrl
>());
491 dialog
->setAllowGlobal(true);
492 if (dialog
->exec() == QDialog::Accepted
) {
493 PlacesItem
* oldItem
= m_model
->placesItem(index
);
495 oldItem
->setText(dialog
->text());
496 oldItem
->setUrl(dialog
->url());
497 oldItem
->setIcon(dialog
->icon());
504 void PlacesPanel::selectClosestItem()
506 const int index
= m_model
->closestItem(url());
507 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
508 selectionManager
->setCurrentItem(index
);
509 selectionManager
->clearSelection();
510 selectionManager
->setSelected(index
);
513 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
515 const PlacesItem
* item
= m_model
->placesItem(index
);
520 if (m_model
->storageSetupNeeded(index
)) {
521 m_triggerStorageSetupButton
= button
;
522 m_storageSetupFailedUrl
= url();
524 connect(m_model
, &PlacesItemModel::storageSetupDone
,
525 this, &PlacesPanel::slotStorageSetupDone
);
527 m_model
->requestStorageSetup(index
);
529 m_triggerStorageSetupButton
= Qt::NoButton
;
531 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
532 if (!url
.isEmpty()) {
533 if (button
== Qt::MiddleButton
) {
534 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
536 emit
placeActivated(PlacesItemModel::convertedUrl(url
));