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 void PlacesPanel::proceedWithTearDown()
77 m_model
->proceedWithTearDown();
80 bool PlacesPanel::urlChanged()
82 if (!url().isValid() || url().scheme().contains(QStringLiteral("search"))) {
83 // Skip results shown by a search, as possible identical
84 // directory names are useless without parent-path information.
95 void PlacesPanel::readSettings()
98 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
99 m_controller
->setAutoActivationDelay(delay
);
103 void PlacesPanel::showEvent(QShowEvent
* event
)
105 if (event
->spontaneous()) {
106 Panel::showEvent(event
);
111 // Postpone the creating of the controller to the first show event.
112 // This assures that no performance and memory overhead is given when the folders panel is not
113 // used at all and stays invisible.
114 m_model
= new PlacesItemModel(this);
115 m_model
->setGroupedSorting(true);
116 connect(m_model
, &PlacesItemModel::errorMessage
,
117 this, &PlacesPanel::errorMessage
);
118 connect(m_model
, &PlacesItemModel::storageTearDownRequested
,
119 this, &PlacesPanel::storageTearDownRequested
);
120 connect(m_model
, &PlacesItemModel::storageTearDownExternallyRequested
,
121 this, &PlacesPanel::storageTearDownExternallyRequested
);
123 m_view
= new PlacesView();
124 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
125 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
127 m_controller
= new KItemListController(m_model
, m_view
, this);
128 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
129 m_controller
->setSingleClickActivationEnforced(true);
133 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
134 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
135 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
136 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
137 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
138 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
140 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
141 container
->setEnabledFrame(false);
143 QVBoxLayout
* layout
= new QVBoxLayout(this);
144 layout
->setMargin(0);
145 layout
->addWidget(container
);
150 Panel::showEvent(event
);
153 void PlacesPanel::slotItemActivated(int index
)
155 triggerItem(index
, Qt::LeftButton
);
158 void PlacesPanel::slotItemMiddleClicked(int index
)
160 triggerItem(index
, Qt::MiddleButton
);
163 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
165 PlacesItem
* item
= m_model
->placesItem(index
);
172 QAction
* emptyTrashAction
= 0;
173 QAction
* editAction
= 0;
174 QAction
* teardownAction
= 0;
175 QAction
* ejectAction
= 0;
177 const QString label
= item
->text();
179 const bool isDevice
= !item
->udi().isEmpty();
180 const bool isTrash
= (item
->url().scheme() == QLatin1String("trash"));
182 ejectAction
= m_model
->ejectAction(index
);
184 ejectAction
->setParent(&menu
);
185 menu
.addAction(ejectAction
);
188 teardownAction
= m_model
->teardownAction(index
);
189 if (teardownAction
) {
190 teardownAction
->setParent(&menu
);
191 menu
.addAction(teardownAction
);
194 if (teardownAction
|| ejectAction
) {
199 emptyTrashAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"));
200 emptyTrashAction
->setEnabled(item
->icon() == QLatin1String("user-trash-full"));
205 QAction
* openInNewWindowAction
= menu
.addAction(QIcon::fromTheme("window-new"), i18nc("@item:inmenu", "Open in New Window"));
206 QAction
* openInNewTabAction
= menu
.addAction(QIcon::fromTheme("tab-new"), i18nc("@item:inmenu", "Open in New Tab"));
207 if (!isDevice
&& !isTrash
) {
212 editAction
= menu
.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit..."));
215 QAction
* removeAction
= 0;
216 if (!isDevice
&& !item
->isSystemItem()) {
217 removeAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@item:inmenu", "Remove"));
220 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide"));
221 hideAction
->setCheckable(true);
222 hideAction
->setChecked(item
->isHidden());
224 QAction
* action
= menu
.exec(pos
.toPoint());
226 if (action
== emptyTrashAction
) {
229 // The index might have changed if devices were added/removed while
230 // the context menu was open.
231 index
= m_model
->index(item
);
233 // The item is not in the model any more, probably because it was an
234 // external device that has been removed while the context menu was open.
238 if (action
== editAction
) {
240 } else if (action
== removeAction
) {
241 m_model
->removeItem(index
);
242 m_model
->saveBookmarks();
243 } else if (action
== hideAction
) {
244 item
->setHidden(hideAction
->isChecked());
245 m_model
->saveBookmarks();
246 } else if (action
== openInNewWindowAction
) {
247 Dolphin::openNewWindow({PlacesItemModel::convertedUrl(m_model
->data(index
).value("url").toUrl())}, this);
248 } else if (action
== openInNewTabAction
) {
249 // TriggerItem does set up the storage first and then it will
250 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
251 triggerItem(index
, Qt::MiddleButton
);
252 } else if (action
== teardownAction
) {
253 m_model
->requestTearDown(index
);
254 } else if (action
== ejectAction
) {
255 m_model
->requestEject(index
);
263 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
267 QAction
* addAction
= menu
.addAction(QIcon::fromTheme(QStringLiteral("document-new")), i18nc("@item:inmenu", "Add Entry..."));
269 QAction
* showAllAction
= 0;
270 if (m_model
->hiddenCount() > 0) {
271 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
272 showAllAction
->setCheckable(true);
273 showAllAction
->setChecked(m_model
->hiddenItemsShown());
276 QMenu
* iconSizeSubMenu
= new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
285 const int iconSizeCount
= 4;
286 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
287 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
288 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
289 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
290 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
293 QMap
<QAction
*, int> iconSizeActionMap
;
294 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
296 for (int i
= 0; i
< iconSizeCount
; ++i
) {
297 const int size
= iconSizes
[i
].size
;
298 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
301 QAction
* action
= iconSizeSubMenu
->addAction(text
);
302 iconSizeActionMap
.insert(action
, size
);
303 action
->setActionGroup(iconSizeGroup
);
304 action
->setCheckable(true);
305 action
->setChecked(m_view
->iconSize() == size
);
308 menu
.addMenu(iconSizeSubMenu
);
311 foreach (QAction
* action
, customContextMenuActions()) {
312 menu
.addAction(action
);
315 QAction
* action
= menu
.exec(pos
.toPoint());
317 if (action
== addAction
) {
319 } else if (action
== showAllAction
) {
320 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
321 } else if (iconSizeActionMap
.contains(action
)) {
322 m_view
->setIconSize(iconSizeActionMap
.value(action
));
329 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
335 const PlacesItem
* destItem
= m_model
->placesItem(index
);
336 const PlacesItem::GroupType group
= destItem
->groupType();
337 if (group
== PlacesItem::SearchForType
|| group
== PlacesItem::RecentlySavedType
) {
341 if (m_model
->storageSetupNeeded(index
)) {
342 connect(m_model
, &PlacesItemModel::storageSetupDone
,
343 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
345 m_itemDropEventIndex
= index
;
347 // Make a full copy of the Mime-Data
348 m_itemDropEventMimeData
= new QMimeData
;
349 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
350 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
351 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
352 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
353 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
355 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
356 event
->possibleActions(),
357 m_itemDropEventMimeData
,
361 m_model
->requestStorageSetup(index
);
365 QUrl destUrl
= destItem
->url();
366 QDropEvent
dropEvent(event
->pos().toPoint(),
367 event
->possibleActions(),
372 slotUrlsDropped(destUrl
, &dropEvent
, this);
375 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
377 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
378 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
380 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
382 QUrl destUrl
= m_model
->placesItem(index
)->url();
383 slotUrlsDropped(destUrl
, m_itemDropEvent
, this);
386 delete m_itemDropEventMimeData
;
387 delete m_itemDropEvent
;
389 m_itemDropEventIndex
= -1;
390 m_itemDropEventMimeData
= 0;
395 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
397 m_model
->dropMimeDataBefore(index
, event
->mimeData());
398 m_model
->saveBookmarks();
401 void PlacesPanel::slotUrlsDropped(const QUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
403 KIO::DropJob
*job
= DragAndDropHelper::dropUrls(dest
, event
, parent
);
405 connect(job
, &KIO::DropJob::result
, this, [this](KJob
*job
) { if (job
->error()) emit
errorMessage(job
->errorString()); });
409 void PlacesPanel::slotTrashUpdated(KJob
* job
)
412 emit
errorMessage(job
->errorString());
414 // as long as KIO doesn't do this, do it ourselves
415 KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), 0, KNotification::DefaultEvent
);
418 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
420 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
421 this, &PlacesPanel::slotStorageSetupDone
);
423 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
428 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
429 triggerItem(index
, m_triggerStorageSetupButton
);
430 m_triggerStorageSetupButton
= Qt::NoButton
;
432 setUrl(m_storageSetupFailedUrl
);
433 m_storageSetupFailedUrl
= QUrl();
437 void PlacesPanel::emptyTrash()
439 KIO::JobUiDelegate uiDelegate
;
440 uiDelegate
.setWindow(window());
441 if (uiDelegate
.askDeleteConfirmation(QList
<QUrl
>(), KIO::JobUiDelegate::EmptyTrash
, KIO::JobUiDelegate::DefaultConfirmation
)) {
442 KIO::Job
* job
= KIO::emptyTrash();
443 KJobWidgets::setWindow(job
, window());
444 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
448 void PlacesPanel::addEntry()
450 const int index
= m_controller
->selectionManager()->currentItem();
451 const QUrl url
= m_model
->data(index
).value("url").toUrl();
453 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
454 dialog
->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
455 dialog
->setAllowGlobal(true);
457 if (dialog
->exec() == QDialog::Accepted
) {
458 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
459 m_model
->appendItemToGroup(item
);
460 m_model
->saveBookmarks();
466 void PlacesPanel::editEntry(int index
)
468 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
470 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
471 dialog
->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
472 dialog
->setIcon(data
.value("iconName").toString());
473 dialog
->setText(data
.value("text").toString());
474 dialog
->setUrl(data
.value("url").toUrl());
475 dialog
->setAllowGlobal(true);
476 if (dialog
->exec() == QDialog::Accepted
) {
477 PlacesItem
* oldItem
= m_model
->placesItem(index
);
479 oldItem
->setText(dialog
->text());
480 oldItem
->setUrl(dialog
->url());
481 oldItem
->setIcon(dialog
->icon());
482 m_model
->saveBookmarks();
489 void PlacesPanel::selectClosestItem()
491 const int index
= m_model
->closestItem(url());
492 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
493 selectionManager
->setCurrentItem(index
);
494 selectionManager
->clearSelection();
495 selectionManager
->setSelected(index
);
498 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
500 const PlacesItem
* item
= m_model
->placesItem(index
);
505 if (m_model
->storageSetupNeeded(index
)) {
506 m_triggerStorageSetupButton
= button
;
507 m_storageSetupFailedUrl
= url();
509 connect(m_model
, &PlacesItemModel::storageSetupDone
,
510 this, &PlacesPanel::slotStorageSetupDone
);
512 m_model
->requestStorageSetup(index
);
514 m_triggerStorageSetupButton
= Qt::NoButton
;
516 const QUrl url
= m_model
->data(index
).value("url").toUrl();
517 if (!url
.isEmpty()) {
518 if (button
== Qt::MiddleButton
) {
519 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
521 emit
placeActivated(PlacesItemModel::convertedUrl(url
));