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>
34 #include <kitemviews/kitemlistcontainer.h>
35 #include <kitemviews/kitemlistcontroller.h>
36 #include <kitemviews/kitemlistselectionmanager.h>
37 #include <kitemviews/kstandarditem.h>
39 #include <KMessageBox>
40 #include <KNotification>
41 #include "placesitem.h"
42 #include "placesitemeditdialog.h"
43 #include "placesitemlistgroupheader.h"
44 #include "placesitemlistwidget.h"
45 #include "placesitemmodel.h"
46 #include "placesview.h"
47 #include <views/draganddrophelper.h>
48 #include <QGraphicsSceneDragDropEvent>
49 #include <QVBoxLayout>
52 PlacesPanel::PlacesPanel(QWidget
* parent
) :
56 m_storageSetupFailedUrl(),
57 m_triggerStorageSetupButton(),
58 m_itemDropEventIndex(-1),
59 m_itemDropEventMimeData(0),
64 PlacesPanel::~PlacesPanel()
68 bool PlacesPanel::urlChanged()
70 if (!url().isValid() || url().protocol().contains("search")) {
71 // Skip results shown by a search, as possible identical
72 // directory names are useless without parent-path information.
83 void PlacesPanel::readSettings()
86 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
87 m_controller
->setAutoActivationDelay(delay
);
91 void PlacesPanel::showEvent(QShowEvent
* event
)
93 if (event
->spontaneous()) {
94 Panel::showEvent(event
);
99 // Postpone the creating of the controller to the first show event.
100 // This assures that no performance and memory overhead is given when the folders panel is not
101 // used at all and stays invisible.
102 m_model
= new PlacesItemModel(this);
103 m_model
->setGroupedSorting(true);
104 connect(m_model
, SIGNAL(errorMessage(QString
)),
105 this, SIGNAL(errorMessage(QString
)));
107 m_view
= new PlacesView();
108 m_view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
109 m_view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
111 m_controller
= new KItemListController(m_model
, m_view
, this);
112 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
113 m_controller
->setSingleClickActivationEnforced(true);
117 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
118 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
119 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
120 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
121 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
122 connect(m_controller
, SIGNAL(aboveItemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotAboveItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
124 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
125 container
->setEnabledFrame(false);
127 QVBoxLayout
* layout
= new QVBoxLayout(this);
128 layout
->setMargin(0);
129 layout
->addWidget(container
);
134 Panel::showEvent(event
);
137 void PlacesPanel::slotItemActivated(int index
)
139 triggerItem(index
, Qt::LeftButton
);
142 void PlacesPanel::slotItemMiddleClicked(int index
)
144 triggerItem(index
, Qt::MiddleButton
);
147 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
149 PlacesItem
* item
= m_model
->placesItem(index
);
156 QAction
* emptyTrashAction
= 0;
157 QAction
* addAction
= 0;
158 QAction
* mainSeparator
= 0;
159 QAction
* editAction
= 0;
160 QAction
* teardownAction
= 0;
161 QAction
* ejectAction
= 0;
163 const QString label
= item
->text();
165 const bool isDevice
= !item
->udi().isEmpty();
167 ejectAction
= m_model
->ejectAction(index
);
169 ejectAction
->setParent(&menu
);
170 menu
.addAction(ejectAction
);
173 teardownAction
= m_model
->teardownAction(index
);
174 if (teardownAction
) {
175 teardownAction
->setParent(&menu
);
176 menu
.addAction(teardownAction
);
179 if (teardownAction
|| ejectAction
) {
180 mainSeparator
= menu
.addSeparator();
183 if (item
->url() == KUrl("trash:/")) {
184 emptyTrashAction
= menu
.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
185 emptyTrashAction
->setEnabled(item
->icon() == "user-trash-full");
188 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
189 mainSeparator
= menu
.addSeparator();
190 editAction
= menu
.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label
));
194 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
197 QAction
* openInNewTabAction
= menu
.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label
));
198 openInNewTabAction
->setIcon(KIcon("tab-new"));
200 QAction
* removeAction
= 0;
201 if (!isDevice
&& !item
->isSystemItem()) {
202 removeAction
= menu
.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label
));
205 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide '%1'", label
));
206 hideAction
->setCheckable(true);
207 hideAction
->setChecked(item
->isHidden());
209 QAction
* showAllAction
= 0;
210 if (m_model
->hiddenCount() > 0) {
211 if (!mainSeparator
) {
212 mainSeparator
= menu
.addSeparator();
214 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
215 showAllAction
->setCheckable(true);
216 showAllAction
->setChecked(m_model
->hiddenItemsShown());
220 KMenu
* iconSizeSubMenu
= new KMenu(i18nc("@item:inmenu", "Icon Size"), &menu
);
229 const int iconSizeCount
= 4;
230 static const IconSizeInfo iconSizes
[iconSizeCount
] = {
231 {KIconLoader::SizeSmall
, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
232 {KIconLoader::SizeSmallMedium
, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
233 {KIconLoader::SizeMedium
, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
234 {KIconLoader::SizeLarge
, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
237 QMap
<QAction
*, int> iconSizeActionMap
;
238 QActionGroup
* iconSizeGroup
= new QActionGroup(iconSizeSubMenu
);
240 for (int i
= 0; i
< iconSizeCount
; ++i
) {
241 const int size
= iconSizes
[i
].size
;
242 const QString text
= i18nc(iconSizes
[i
].context
, iconSizes
[i
].text
,
245 QAction
* action
= iconSizeSubMenu
->addAction(text
);
246 iconSizeActionMap
.insert(action
, size
);
247 action
->setActionGroup(iconSizeGroup
);
248 action
->setCheckable(true);
249 action
->setChecked(m_view
->iconSize() == size
);
252 menu
.addMenu(iconSizeSubMenu
);
255 foreach (QAction
* action
, customContextMenuActions()) {
256 menu
.addAction(action
);
259 QAction
* action
= menu
.exec(pos
.toPoint());
261 if (action
== emptyTrashAction
) {
263 } else if (action
== addAction
) {
265 } else if (action
== showAllAction
) {
266 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
267 } else if (iconSizeActionMap
.contains(action
)) {
268 m_view
->setIconSize(iconSizeActionMap
.value(action
));
270 // The index might have changed if devices were added/removed while
271 // the context menu was open.
272 index
= m_model
->index(item
);
274 // The item is not in the model any more, probably because it was an
275 // external device that has been removed while the context menu was open.
279 if (action
== editAction
) {
281 } else if (action
== removeAction
) {
282 m_model
->removeItem(index
);
283 m_model
->saveBookmarks();
284 } else if (action
== hideAction
) {
285 item
->setHidden(hideAction
->isChecked());
286 m_model
->saveBookmarks();
287 } else if (action
== openInNewTabAction
) {
288 // TriggerItem does set up the storage first and then it will
289 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
290 triggerItem(index
, Qt::MiddleButton
);
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::RecentlySavedType
) {
344 if (m_model
->storageSetupNeeded(index
)) {
345 connect(m_model
, SIGNAL(storageSetupDone(int,bool)),
346 this, SLOT(slotItemDropEventStorageSetupDone(int,bool)));
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
, SIGNAL(storageSetupDone(int,bool)),
385 this, SLOT(slotItemDropEventStorageSetupDone(int,bool)));
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());
410 m_model
->saveBookmarks();
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("trash:/");
432 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
434 disconnect(m_model
, SIGNAL(storageSetupDone(int,bool)),
435 this, SLOT(slotStorageSetupDone(int,bool)));
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 job
->ui()->setWindow(parentWidget());
467 connect(job
, SIGNAL(result(KJob
*)), SLOT(slotTrashUpdated(KJob
*)));
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
);
483 m_model
->saveBookmarks();
489 void PlacesPanel::editEntry(int index
)
491 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
493 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
494 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
495 dialog
->setIcon(data
.value("iconName").toString());
496 dialog
->setText(data
.value("text").toString());
497 dialog
->setUrl(data
.value("url").value
<KUrl
>());
498 dialog
->setAllowGlobal(true);
499 if (dialog
->exec() == QDialog::Accepted
) {
500 PlacesItem
* oldItem
= m_model
->placesItem(index
);
502 oldItem
->setText(dialog
->text());
503 oldItem
->setUrl(dialog
->url());
504 oldItem
->setIcon(dialog
->icon());
505 m_model
->saveBookmarks();
512 void PlacesPanel::selectClosestItem()
514 const int index
= m_model
->closestItem(url());
515 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
516 selectionManager
->setCurrentItem(index
);
517 selectionManager
->clearSelection();
518 selectionManager
->setSelected(index
);
521 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
523 const PlacesItem
* item
= m_model
->placesItem(index
);
528 if (m_model
->storageSetupNeeded(index
)) {
529 m_triggerStorageSetupButton
= button
;
530 m_storageSetupFailedUrl
= url();
532 connect(m_model
, SIGNAL(storageSetupDone(int,bool)),
533 this, SLOT(slotStorageSetupDone(int,bool)));
535 m_model
->requestStorageSetup(index
);
537 m_triggerStorageSetupButton
= Qt::NoButton
;
539 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
540 if (!url
.isEmpty()) {
541 if (button
== Qt::MiddleButton
) {
542 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
544 emit
placeActivated(PlacesItemModel::convertedUrl(url
));
551 #include "placespanel.moc"