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 const KUrl url
= m_model
->item(index
)->dataValue("url").value
<KUrl
>();
290 emit
placeMiddleClicked(url
);
291 } else if (action
== teardownAction
) {
292 m_model
->requestTeardown(index
);
293 } else if (action
== ejectAction
) {
294 m_model
->requestEject(index
);
302 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
306 QAction
* addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
308 QAction
* showAllAction
= 0;
309 if (m_model
->hiddenCount() > 0) {
310 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
311 showAllAction
->setCheckable(true);
312 showAllAction
->setChecked(m_model
->hiddenItemsShown());
316 foreach (QAction
* action
, customContextMenuActions()) {
317 menu
.addAction(action
);
320 QAction
* action
= menu
.exec(pos
.toPoint());
322 if (action
== addAction
) {
324 } else if (action
== showAllAction
) {
325 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
332 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
338 const PlacesItem
* destItem
= m_model
->placesItem(index
);
339 const PlacesItem::GroupType group
= destItem
->groupType();
340 if (group
== PlacesItem::SearchForType
|| group
== PlacesItem::RecentlyAccessedType
) {
344 if (m_model
->storageSetupNeeded(index
)) {
345 connect(m_model
, &PlacesItemModel::storageSetupDone
,
346 this, &PlacesPanel::slotItemDropEventStorageSetupDone
);
348 m_itemDropEventIndex
= index
;
350 // Make a full copy of the Mime-Data
351 m_itemDropEventMimeData
= new QMimeData
;
352 m_itemDropEventMimeData
->setText(event
->mimeData()->text());
353 m_itemDropEventMimeData
->setHtml(event
->mimeData()->html());
354 m_itemDropEventMimeData
->setUrls(event
->mimeData()->urls());
355 m_itemDropEventMimeData
->setImageData(event
->mimeData()->imageData());
356 m_itemDropEventMimeData
->setColorData(event
->mimeData()->colorData());
358 m_itemDropEvent
= new QDropEvent(event
->pos().toPoint(),
359 event
->possibleActions(),
360 m_itemDropEventMimeData
,
364 m_model
->requestStorageSetup(index
);
368 KUrl destUrl
= destItem
->url();
369 QDropEvent
dropEvent(event
->pos().toPoint(),
370 event
->possibleActions(),
376 DragAndDropHelper::dropUrls(KFileItem(), destUrl
, &dropEvent
, error
);
377 if (!error
.isEmpty()) {
378 emit
errorMessage(error
);
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 KUrl destUrl
= m_model
->placesItem(index
)->url();
392 DragAndDropHelper::dropUrls(KFileItem(), destUrl
, m_itemDropEvent
, error
);
393 if (!error
.isEmpty()) {
394 emit
errorMessage(error
);
398 delete m_itemDropEventMimeData
;
399 delete m_itemDropEvent
;
401 m_itemDropEventIndex
= -1;
402 m_itemDropEventMimeData
= 0;
407 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
409 m_model
->dropMimeDataBefore(index
, event
->mimeData());
412 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
416 DragAndDropHelper::dropUrls(KFileItem(), dest
, event
, error
);
417 if (!error
.isEmpty()) {
418 emit
errorMessage(error
);
423 void PlacesPanel::slotTrashUpdated(KJob
* job
)
426 emit
errorMessage(job
->errorString());
428 org::kde::KDirNotify::emitFilesAdded(QUrl("trash:/"));
431 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
433 disconnect(m_model
, &PlacesItemModel::storageSetupDone
,
434 this, &PlacesPanel::slotStorageSetupDone
);
436 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
441 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
442 triggerItem(index
, m_triggerStorageSetupButton
);
443 m_triggerStorageSetupButton
= Qt::NoButton
;
445 setUrl(m_storageSetupFailedUrl
);
446 m_storageSetupFailedUrl
= KUrl();
450 void PlacesPanel::emptyTrash()
452 const QString text
= i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
453 const bool del
= KMessageBox::warningContinueCancel(window(),
456 KGuiItem(i18nc("@action:button", "Empty Trash"),
458 ) == KMessageBox::Continue
;
460 QByteArray packedArgs
;
461 QDataStream
stream(&packedArgs
, QIODevice::WriteOnly
);
463 KIO::Job
*job
= KIO::special(KUrl("trash:/"), packedArgs
);
464 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent
);
465 KJobWidgets::setWindow(job
, parentWidget());
466 connect(job
, &KIO::Job::result
, this, &PlacesPanel::slotTrashUpdated
);
470 void PlacesPanel::addEntry()
472 const int index
= m_controller
->selectionManager()->currentItem();
473 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
475 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
476 dialog
->setCaption(i18nc("@title:window", "Add Places Entry"));
477 dialog
->setAllowGlobal(true);
479 if (dialog
->exec() == QDialog::Accepted
) {
480 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
481 m_model
->appendItemToGroup(item
);
487 void PlacesPanel::editEntry(int index
)
489 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
491 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
492 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
493 dialog
->setIcon(data
.value("iconName").toString());
494 dialog
->setText(data
.value("text").toString());
495 dialog
->setUrl(data
.value("url").value
<KUrl
>());
496 dialog
->setAllowGlobal(true);
497 if (dialog
->exec() == QDialog::Accepted
) {
498 PlacesItem
* oldItem
= m_model
->placesItem(index
);
500 oldItem
->setText(dialog
->text());
501 oldItem
->setUrl(dialog
->url());
502 oldItem
->setIcon(dialog
->icon());
509 void PlacesPanel::selectClosestItem()
511 const int index
= m_model
->closestItem(url());
512 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
513 selectionManager
->setCurrentItem(index
);
514 selectionManager
->clearSelection();
515 selectionManager
->setSelected(index
);
518 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
520 const PlacesItem
* item
= m_model
->placesItem(index
);
525 if (m_model
->storageSetupNeeded(index
)) {
526 m_triggerStorageSetupButton
= button
;
527 m_storageSetupFailedUrl
= url();
529 connect(m_model
, &PlacesItemModel::storageSetupDone
,
530 this, &PlacesPanel::slotStorageSetupDone
);
532 m_model
->requestStorageSetup(index
);
534 m_triggerStorageSetupButton
= Qt::NoButton
;
536 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
537 if (!url
.isEmpty()) {
538 if (button
== Qt::MiddleButton
) {
539 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
541 emit
placeActivated(PlacesItemModel::convertedUrl(url
));
548 #include "placespanel.moc"