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"
32 #include <KIO/JobUiDelegate>
33 #include <KJobWidgets>
35 #include <KIconLoader>
36 #include <kitemviews/kitemlistcontainer.h>
37 #include <kitemviews/kitemlistcontroller.h>
38 #include <kitemviews/kitemlistselectionmanager.h>
39 #include <kitemviews/kstandarditem.h>
41 #include <KMessageBox>
42 #include <KNotification>
43 #include "placesitem.h"
44 #include "placesitemeditdialog.h"
45 #include "placesitemlistgroupheader.h"
46 #include "placesitemlistwidget.h"
47 #include "placesitemmodel.h"
48 #include "placesview.h"
49 #include <views/draganddrophelper.h>
50 #include <QGraphicsSceneDragDropEvent>
51 #include <QVBoxLayout>
55 PlacesPanel::PlacesPanel(QWidget
* parent
) :
59 m_storageSetupFailedUrl(),
60 m_triggerStorageSetupButton(),
61 m_itemDropEventIndex(-1),
62 m_itemDropEventMimeData(0),
67 PlacesPanel::~PlacesPanel()
71 bool PlacesPanel::urlChanged()
73 if (!url().isValid() || url().protocol().contains("search")) {
74 // Skip results shown by a search, as possible identical
75 // directory names are useless without parent-path information.
86 void PlacesPanel::readSettings()
89 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
90 m_controller
->setAutoActivationDelay(delay
);
94 void PlacesPanel::showEvent(QShowEvent
* event
)
96 if (event
->spontaneous()) {
97 Panel::showEvent(event
);
102 // Postpone the creating of the controller to the first show event.
103 // This assures that no performance and memory overhead is given when the folders panel is not
104 // used at all and stays invisible.
105 m_model
= new PlacesItemModel(this);
106 m_model
->setGroupedSorting(true);
107 connect(m_model
, &PlacesItemModel::errorMessage
,
108 this, &PlacesPanel::errorMessage
);
110 m_view
= new PlacesView();
111 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
112 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
114 m_controller
= new KItemListController(m_model
, m_view
, this);
115 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
116 m_controller
->setSingleClickActivationEnforced(true);
120 connect(m_controller
, &KItemListController::itemActivated
, this, &PlacesPanel::slotItemActivated
);
121 connect(m_controller
, &KItemListController::itemMiddleClicked
, this, &PlacesPanel::slotItemMiddleClicked
);
122 connect(m_controller
, &KItemListController::itemContextMenuRequested
, this, &PlacesPanel::slotItemContextMenuRequested
);
123 connect(m_controller
, &KItemListController::viewContextMenuRequested
, this, &PlacesPanel::slotViewContextMenuRequested
);
124 connect(m_controller
, &KItemListController::itemDropEvent
, this, &PlacesPanel::slotItemDropEvent
);
125 connect(m_controller
, &KItemListController::aboveItemDropEvent
, this, &PlacesPanel::slotAboveItemDropEvent
);
127 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
128 container
->setEnabledFrame(false);
130 QVBoxLayout
* layout
= new QVBoxLayout(this);
131 layout
->setMargin(0);
132 layout
->addWidget(container
);
137 Panel::showEvent(event
);
140 void PlacesPanel::slotItemActivated(int index
)
142 triggerItem(index
, Qt::LeftButton
);
145 void PlacesPanel::slotItemMiddleClicked(int index
)
147 triggerItem(index
, Qt::MiddleButton
);
150 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
152 PlacesItem
* item
= m_model
->placesItem(index
);
159 QAction
* emptyTrashAction
= 0;
160 QAction
* addAction
= 0;
161 QAction
* mainSeparator
= 0;
162 QAction
* editAction
= 0;
163 QAction
* teardownAction
= 0;
164 QAction
* ejectAction
= 0;
166 const QString label
= item
->text();
168 const bool isDevice
= !item
->udi().isEmpty();
170 ejectAction
= m_model
->ejectAction(index
);
172 ejectAction
->setParent(&menu
);
173 menu
.addAction(ejectAction
);
176 teardownAction
= m_model
->teardownAction(index
);
177 if (teardownAction
) {
178 teardownAction
->setParent(&menu
);
179 menu
.addAction(teardownAction
);
182 if (teardownAction
|| ejectAction
) {
183 mainSeparator
= menu
.addSeparator();
186 if (item
->url() == KUrl("trash:/")) {
187 emptyTrashAction
= menu
.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
188 emptyTrashAction
->setEnabled(item
->icon() == "user-trash-full");
191 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
192 mainSeparator
= menu
.addSeparator();
193 editAction
= menu
.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label
));
197 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
200 QAction
* openInNewTabAction
= menu
.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label
));
201 openInNewTabAction
->setIcon(KIcon("tab-new"));
203 QAction
* removeAction
= 0;
204 if (!isDevice
&& !item
->isSystemItem()) {
205 removeAction
= menu
.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label
));
208 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide '%1'", label
));
209 hideAction
->setCheckable(true);
210 hideAction
->setChecked(item
->isHidden());
212 QAction
* showAllAction
= 0;
213 if (m_model
->hiddenCount() > 0) {
214 if (!mainSeparator
) {
215 mainSeparator
= menu
.addSeparator();
217 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
218 showAllAction
->setCheckable(true);
219 showAllAction
->setChecked(m_model
->hiddenItemsShown());
223 KMenu
* iconSizeSubMenu
= new KMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
232 const int iconSizeCount
= 4;
233 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
234 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
235 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
236 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
237 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
240 QMap
<QAction
*, int> iconSizeActionMap
;
241 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
243 for (int i
= 0; i
< iconSizeCount
; ++i
) {
244 const int size
= iconSizes
[i
].size
;
245 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
248 QAction
* action
= iconSizeSubMenu
->addAction(text
);
249 iconSizeActionMap
.insert(action
, size
);
250 action
->setActionGroup(iconSizeGroup
);
251 action
->setCheckable(true);
252 action
->setChecked(m_view
->iconSize() == size
);
255 menu
.addMenu(iconSizeSubMenu
);
258 foreach (QAction
* action
, customContextMenuActions()) {
259 menu
.addAction(action
);
262 QAction
* action
= menu
.exec(pos
.toPoint());
264 if (action
== emptyTrashAction
) {
266 } else if (action
== addAction
) {
268 } else if (action
== showAllAction
) {
269 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
270 } else if (iconSizeActionMap
.contains(action
)) {
271 m_view
->setIconSize(iconSizeActionMap
.value(action
));
273 // The index might have changed if devices were added/removed while
274 // the context menu was open.
275 index
= m_model
->index(item
);
277 // The item is not in the model any more, probably because it was an
278 // external device that has been removed while the context menu was open.
282 if (action
== editAction
) {
284 } else if (action
== removeAction
) {
285 m_model
->removeItem(index
);
286 } else if (action
== hideAction
) {
287 item
->setHidden(hideAction
->isChecked());
288 } else if (action
== openInNewTabAction
) {
289 // TriggerItem does set up the storage first and then it will
290 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
291 triggerItem(index
, Qt::MiddleButton
);
292 } else if (action
== teardownAction
) {
293 m_model
->requestTeardown(index
);
294 } else if (action
== ejectAction
) {
295 m_model
->requestEject(index
);
303 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
307 QAction
* addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
309 QAction
* showAllAction
= 0;
310 if (m_model
->hiddenCount() > 0) {
311 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
312 showAllAction
->setCheckable(true);
313 showAllAction
->setChecked(m_model
->hiddenItemsShown());
317 foreach (QAction
* action
, customContextMenuActions()) {
318 menu
.addAction(action
);
321 QAction
* action
= menu
.exec(pos
.toPoint());
323 if (action
== addAction
) {
325 } else if (action
== showAllAction
) {
326 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
333 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
339 const PlacesItem
* destItem
= m_model
->placesItem(index
);
340 const PlacesItem::GroupType group
= destItem
->groupType();
341 if (group
== PlacesItem::SearchForType
|| group
== PlacesItem::RecentlyAccessedType
) {
345 if (m_model
->storageSetupNeeded(index
)) {
346 connect(m_model
, &PlacesItemModel::storageSetupDone
,
347 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
349 m_itemDropEventIndex
= index
;
351 // Make a full copy of the Mime-Data
352 m_itemDropEventMimeData
= new QMimeData
;
353 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
354 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
355 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
356 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
357 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
359 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
360 event
->possibleActions(),
361 m_itemDropEventMimeData
,
365 m_model
->requestStorageSetup(index
);
369 KUrl destUrl
= destItem
->url();
370 QDropEvent
dropEvent(event
->pos().toPoint(),
371 event
->possibleActions(),
377 DragAndDropHelper::dropUrls(KFileItem(), destUrl
, &dropEvent
, error
);
378 if (!error
.isEmpty()) {
379 emit
errorMessage(error
);
383 void PlacesPanel::slotItemDropEventStorageSetupDone(int index
, bool success
)
385 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
386 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
388 if ((index
== m_itemDropEventIndex
) && m_itemDropEvent
&& m_itemDropEventMimeData
) {
390 KUrl destUrl
= m_model
->placesItem(index
)->url();
393 DragAndDropHelper::dropUrls(KFileItem(), destUrl
, m_itemDropEvent
, error
);
394 if (!error
.isEmpty()) {
395 emit
errorMessage(error
);
399 delete m_itemDropEventMimeData
;
400 delete m_itemDropEvent
;
402 m_itemDropEventIndex
= -1;
403 m_itemDropEventMimeData
= 0;
408 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
410 m_model
->dropMimeDataBefore(index
, event
->mimeData());
413 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
417 DragAndDropHelper::dropUrls(KFileItem(), dest
, event
, error
);
418 if (!error
.isEmpty()) {
419 emit
errorMessage(error
);
424 void PlacesPanel::slotTrashUpdated(KJob
* job
)
427 emit
errorMessage(job
->errorString());
429 org::kde::KDirNotify::emitFilesAdded(QUrl("trash:/"));
432 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
434 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
435 this, &PlacesPanel::slotStorageSetupDone
);
437 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
442 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
443 triggerItem(index
, m_triggerStorageSetupButton
);
444 m_triggerStorageSetupButton
= Qt::NoButton
;
446 setUrl(m_storageSetupFailedUrl
);
447 m_storageSetupFailedUrl
= KUrl();
451 void PlacesPanel::emptyTrash()
453 const QString text
= i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
454 const bool del
= KMessageBox::warningContinueCancel(window(),
457 KGuiItem(i18nc("@action:button", "Empty Trash"),
459 ) == KMessageBox::Continue
;
461 QByteArray packedArgs
;
462 QDataStream
stream(&packedArgs
, QIODevice::WriteOnly
);
464 KIO::Job
*job
= KIO::special(KUrl("trash:/"), packedArgs
);
465 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent
);
466 KJobWidgets::setWindow(job
, parentWidget());
467 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
471 void PlacesPanel::addEntry()
473 const int index
= m_controller
->selectionManager()->currentItem();
474 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
476 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
477 dialog
->setCaption(i18nc("@title:window", "Add Places Entry"));
478 dialog
->setAllowGlobal(true);
480 if (dialog
->exec() == QDialog::Accepted
) {
481 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
482 m_model
->appendItemToGroup(item
);
488 void PlacesPanel::editEntry(int index
)
490 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
492 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
493 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
494 dialog
->setIcon(data
.value("iconName").toString());
495 dialog
->setText(data
.value("text").toString());
496 dialog
->setUrl(data
.value("url").value
<KUrl
>());
497 dialog
->setAllowGlobal(true);
498 if (dialog
->exec() == QDialog::Accepted
) {
499 PlacesItem
* oldItem
= m_model
->placesItem(index
);
501 oldItem
->setText(dialog
->text());
502 oldItem
->setUrl(dialog
->url());
503 oldItem
->setIcon(dialog
->icon());
510 void PlacesPanel::selectClosestItem()
512 const int index
= m_model
->closestItem(url());
513 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
514 selectionManager
->setCurrentItem(index
);
515 selectionManager
->clearSelection();
516 selectionManager
->setSelected(index
);
519 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
521 const PlacesItem
* item
= m_model
->placesItem(index
);
526 if (m_model
->storageSetupNeeded(index
)) {
527 m_triggerStorageSetupButton
= button
;
528 m_storageSetupFailedUrl
= url();
530 connect(m_model
, &PlacesItemModel::storageSetupDone
,
531 this, &PlacesPanel::slotStorageSetupDone
);
533 m_model
->requestStorageSetup(index
);
535 m_triggerStorageSetupButton
= Qt::NoButton
;
537 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
538 if (!url
.isEmpty()) {
539 if (button
== Qt::MiddleButton
) {
540 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
542 emit
placeActivated(PlacesItemModel::convertedUrl(url
));