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"
30 #include <KIO/JobUiDelegate>
32 #include <kitemviews/kitemlistcontainer.h>
33 #include <kitemviews/kitemlistcontroller.h>
34 #include <kitemviews/kitemlistselectionmanager.h>
35 #include <kitemviews/kstandarditem.h>
36 #include <kitemviews/kstandarditemlistview.h>
38 #include <KMessageBox>
39 #include <KNotification>
40 #include "placesitem.h"
41 #include "placesitemeditdialog.h"
42 #include "placesitemlistgroupheader.h"
43 #include "placesitemlistwidget.h"
44 #include "placesitemmodel.h"
45 #include <views/draganddrophelper.h>
46 #include <QGraphicsSceneDragDropEvent>
47 #include <QVBoxLayout>
50 PlacesPanel::PlacesPanel(QWidget
* parent
) :
54 m_storageSetupFailedUrl(),
55 m_triggerStorageSetupButton()
59 PlacesPanel::~PlacesPanel()
63 bool PlacesPanel::urlChanged()
68 void PlacesPanel::showEvent(QShowEvent
* event
)
70 if (event
->spontaneous()) {
71 Panel::showEvent(event
);
76 // Postpone the creating of the controller to the first show event.
77 // This assures that no performance and memory overhead is given when the folders panel is not
78 // used at all and stays invisible.
79 m_model
= new PlacesItemModel(this);
80 m_model
->setGroupedSorting(true);
81 connect(m_model
, SIGNAL(errorMessage(QString
)),
82 this, SIGNAL(errorMessage(QString
)));
84 KStandardItemListView
* view
= new KStandardItemListView();
85 view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
86 view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
88 m_controller
= new KItemListController(m_model
, view
, this);
89 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
90 m_controller
->setSingleClickActivation(true);
91 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
92 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
93 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
94 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
95 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
96 connect(m_controller
, SIGNAL(aboveItemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotAboveItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
98 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
99 container
->setEnabledFrame(false);
101 QVBoxLayout
* layout
= new QVBoxLayout(this);
102 layout
->setMargin(0);
103 layout
->addWidget(container
);
108 Panel::showEvent(event
);
111 void PlacesPanel::slotItemActivated(int index
)
113 triggerItem(index
, Qt::LeftButton
);
116 void PlacesPanel::slotItemMiddleClicked(int index
)
118 triggerItem(index
, Qt::MiddleButton
);
121 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
123 PlacesItem
* item
= m_model
->placesItem(index
);
130 QAction
* emptyTrashAction
= 0;
131 QAction
* addAction
= 0;
132 QAction
* mainSeparator
= 0;
133 QAction
* editAction
= 0;
134 QAction
* teardownAction
= 0;
135 QAction
* ejectAction
= 0;
137 const QString label
= item
->text();
139 const bool isDevice
= !item
->udi().isEmpty();
141 ejectAction
= m_model
->ejectAction(index
);
143 ejectAction
->setParent(&menu
);
144 menu
.addAction(ejectAction
);
147 teardownAction
= m_model
->teardownAction(index
);
148 if (teardownAction
) {
149 teardownAction
->setParent(&menu
);
150 menu
.addAction(teardownAction
);
153 if (teardownAction
|| ejectAction
) {
154 mainSeparator
= menu
.addSeparator();
157 if (item
->url() == KUrl("trash:/")) {
158 emptyTrashAction
= menu
.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
159 emptyTrashAction
->setEnabled(item
->icon() == "user-trash-full");
162 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
163 mainSeparator
= menu
.addSeparator();
164 editAction
= menu
.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label
));
168 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
171 QAction
* openInNewTabAction
= menu
.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label
));
172 openInNewTabAction
->setIcon(KIcon("tab-new"));
174 QAction
* removeAction
= 0;
175 if (!isDevice
&& !item
->isSystemItem()) {
176 removeAction
= menu
.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label
));
179 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide '%1'", label
));
180 hideAction
->setCheckable(true);
181 hideAction
->setChecked(item
->isHidden());
183 QAction
* showAllAction
= 0;
184 if (m_model
->hiddenCount() > 0) {
185 if (!mainSeparator
) {
186 mainSeparator
= menu
.addSeparator();
188 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
189 showAllAction
->setCheckable(true);
190 showAllAction
->setChecked(m_model
->hiddenItemsShown());
194 foreach (QAction
* action
, customContextMenuActions()) {
195 menu
.addAction(action
);
198 QAction
* action
= menu
.exec(pos
.toPoint());
200 if (action
== emptyTrashAction
) {
202 } else if (action
== addAction
) {
204 } else if (action
== editAction
) {
206 } else if (action
== removeAction
) {
207 m_model
->removeItem(index
);
208 } else if (action
== hideAction
) {
209 item
->setHidden(hideAction
->isChecked());
210 } else if (action
== openInNewTabAction
) {
211 const KUrl url
= m_model
->item(index
)->dataValue("url").value
<KUrl
>();
212 emit
placeMiddleClicked(url
);
213 } else if (action
== showAllAction
) {
214 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
215 } else if (action
== teardownAction
) {
216 m_model
->requestTeardown(index
);
217 } else if (action
== ejectAction
) {
218 m_model
->requestEject(index
);
225 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
229 QAction
* addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
231 QAction
* showAllAction
= 0;
232 if (m_model
->hiddenCount() > 0) {
233 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
234 showAllAction
->setCheckable(true);
235 showAllAction
->setChecked(m_model
->hiddenItemsShown());
239 foreach (QAction
* action
, customContextMenuActions()) {
240 menu
.addAction(action
);
243 QAction
* action
= menu
.exec(pos
.toPoint());
245 if (action
== addAction
) {
247 } else if (action
== showAllAction
) {
248 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
255 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
261 KUrl destUrl
= m_model
->placesItem(index
)->url();
262 QDropEvent
dropEvent(event
->pos().toPoint(),
263 event
->possibleActions(),
268 DragAndDropHelper::dropUrls(KFileItem(), destUrl
, &dropEvent
);
271 void PlacesPanel::slotAboveItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
273 m_model
->dropMimeDataBefore(index
, event
->mimeData());
276 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
279 const QString error
= DragAndDropHelper::dropUrls(KFileItem(), dest
, event
);
280 if (!error
.isEmpty()) {
281 emit
errorMessage(error
);
286 void PlacesPanel::slotTrashUpdated(KJob
* job
)
289 emit
errorMessage(job
->errorString());
291 org::kde::KDirNotify::emitFilesAdded("trash:/");
294 void PlacesPanel::slotStorageSetupDone(int index
, bool success
)
296 disconnect(m_model
, SIGNAL(storageSetupDone(int,bool)),
297 this, SLOT(slotStorageSetupDone(int,bool)));
299 if (m_triggerStorageSetupButton
== Qt::NoButton
) {
304 Q_ASSERT(!m_model
->storageSetupNeeded(index
));
305 triggerItem(index
, m_triggerStorageSetupButton
);
306 m_triggerStorageSetupButton
= Qt::NoButton
;
308 setUrl(m_storageSetupFailedUrl
);
309 m_storageSetupFailedUrl
= KUrl();
313 void PlacesPanel::emptyTrash()
315 const QString text
= i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
316 const bool del
= KMessageBox::warningContinueCancel(window(),
319 KGuiItem(i18nc("@action:button", "Empty Trash"),
321 ) == KMessageBox::Continue
;
323 QByteArray packedArgs
;
324 QDataStream
stream(&packedArgs
, QIODevice::WriteOnly
);
326 KIO::Job
*job
= KIO::special(KUrl("trash:/"), packedArgs
);
327 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent
);
328 job
->ui()->setWindow(parentWidget());
329 connect(job
, SIGNAL(result(KJob
*)), SLOT(slotTrashUpdated(KJob
*)));
333 void PlacesPanel::addEntry()
335 const int index
= m_controller
->selectionManager()->currentItem();
336 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
338 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
339 dialog
->setCaption(i18nc("@title:window", "Add Places Entry"));
340 dialog
->setAllowGlobal(true);
342 if (dialog
->exec() == QDialog::Accepted
) {
343 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
344 m_model
->appendItemToGroup(item
);
350 void PlacesPanel::editEntry(int index
)
352 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
354 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
355 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
356 dialog
->setIcon(data
.value("iconName").toString());
357 dialog
->setText(data
.value("text").toString());
358 dialog
->setUrl(data
.value("url").value
<KUrl
>());
359 dialog
->setAllowGlobal(true);
360 if (dialog
->exec() == QDialog::Accepted
) {
361 PlacesItem
* oldItem
= m_model
->placesItem(index
);
363 oldItem
->setText(dialog
->text());
364 oldItem
->setUrl(dialog
->url());
365 oldItem
->setIcon(dialog
->icon());
372 void PlacesPanel::selectClosestItem()
374 const int index
= m_model
->closestItem(url());
375 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
376 selectionManager
->setCurrentItem(index
);
377 selectionManager
->clearSelection();
378 selectionManager
->setSelected(index
);
381 void PlacesPanel::triggerItem(int index
, Qt::MouseButton button
)
383 const PlacesItem
* item
= m_model
->placesItem(index
);
388 if (m_model
->storageSetupNeeded(index
)) {
389 m_triggerStorageSetupButton
= button
;
390 m_storageSetupFailedUrl
= url();
392 connect(m_model
, SIGNAL(storageSetupDone(int,bool)),
393 this, SLOT(slotStorageSetupDone(int,bool)));
395 m_model
->requestStorageSetup(index
);
397 m_triggerStorageSetupButton
= Qt::NoButton
;
399 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
400 if (!url
.isEmpty()) {
401 if (button
== Qt::MiddleButton
) {
402 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
404 emit
placeActivated(PlacesItemModel::convertedUrl(url
));
411 #include "placespanel.moc"