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
) :
57 PlacesPanel::~PlacesPanel()
61 bool PlacesPanel::urlChanged()
66 void PlacesPanel::showEvent(QShowEvent
* event
)
68 if (event
->spontaneous()) {
69 Panel::showEvent(event
);
74 // Postpone the creating of the controller to the first show event.
75 // This assures that no performance and memory overhead is given when the folders panel is not
76 // used at all and stays invisible.
77 m_model
= new PlacesItemModel(this);
78 m_model
->setGroupedSorting(true);
79 connect(m_model
, SIGNAL(errorMessage(QString
)),
80 this, SIGNAL(errorMessage(QString
)));
82 KStandardItemListView
* view
= new KStandardItemListView();
83 view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
84 view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
86 m_controller
= new KItemListController(m_model
, view
, this);
87 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
88 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
89 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
90 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
91 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
92 connect(m_controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
94 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
95 container
->setEnabledFrame(false);
97 QVBoxLayout
* layout
= new QVBoxLayout(this);
99 layout
->addWidget(container
);
104 Panel::showEvent(event
);
107 void PlacesPanel::slotItemActivated(int index
)
109 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
110 if (!url
.isEmpty()) {
111 emit
placeActivated(PlacesItemModel::convertedUrl(url
));
115 void PlacesPanel::slotItemMiddleClicked(int index
)
117 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
118 if (!url
.isEmpty()) {
119 emit
placeMiddleClicked(PlacesItemModel::convertedUrl(url
));
123 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
125 PlacesItem
* item
= m_model
->placesItem(index
);
132 QAction
* emptyTrashAction
= 0;
133 QAction
* addAction
= 0;
134 QAction
* mainSeparator
= 0;
135 QAction
* editAction
= 0;
136 QAction
* teardownAction
= 0;
137 QAction
* ejectAction
= 0;
139 const QString label
= item
->text();
141 const bool isDevice
= !item
->udi().isEmpty();
143 ejectAction
= m_model
->ejectAction(index
);
145 ejectAction
->setParent(&menu
);
146 menu
.addAction(ejectAction
);
149 teardownAction
= m_model
->teardownAction(index
);
150 if (teardownAction
) {
151 teardownAction
->setParent(&menu
);
152 menu
.addAction(teardownAction
);
155 if (teardownAction
|| ejectAction
) {
156 mainSeparator
= menu
.addSeparator();
159 if (item
->url() == KUrl("trash:/")) {
160 emptyTrashAction
= menu
.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
161 emptyTrashAction
->setEnabled(item
->icon() == "user-trash-full");
164 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
165 mainSeparator
= menu
.addSeparator();
166 editAction
= menu
.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label
));
170 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
173 QAction
* openInNewTabAction
= menu
.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label
));
174 openInNewTabAction
->setIcon(KIcon("tab-new"));
176 QAction
* removeAction
= 0;
177 if (!isDevice
&& !item
->isSystemItem()) {
178 removeAction
= menu
.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label
));
181 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide '%1'", label
));
182 hideAction
->setCheckable(true);
183 hideAction
->setChecked(item
->isHidden());
185 QAction
* showAllAction
= 0;
186 if (m_model
->hiddenCount() > 0) {
187 if (!mainSeparator
) {
188 mainSeparator
= menu
.addSeparator();
190 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
191 showAllAction
->setCheckable(true);
192 showAllAction
->setChecked(m_model
->hiddenItemsShown());
196 foreach (QAction
* action
, customContextMenuActions()) {
197 menu
.addAction(action
);
200 QAction
* action
= menu
.exec(pos
.toPoint());
202 if (action
== emptyTrashAction
) {
204 } else if (action
== addAction
) {
206 } else if (action
== editAction
) {
208 } else if (action
== removeAction
) {
209 m_model
->removeItem(index
);
210 } else if (action
== hideAction
) {
211 item
->setHidden(hideAction
->isChecked());
212 } else if (action
== openInNewTabAction
) {
213 const KUrl url
= m_model
->item(index
)->dataValue("url").value
<KUrl
>();
214 emit
placeMiddleClicked(url
);
215 } else if (action
== showAllAction
) {
216 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
217 } else if (action
== teardownAction
) {
218 m_model
->requestTeardown(index
);
219 } else if (action
== ejectAction
) {
220 m_model
->requestEject(index
);
227 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
231 QAction
* addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
233 QAction
* showAllAction
= 0;
234 if (m_model
->hiddenCount() > 0) {
235 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
236 showAllAction
->setCheckable(true);
237 showAllAction
->setChecked(m_model
->hiddenItemsShown());
241 foreach (QAction
* action
, customContextMenuActions()) {
242 menu
.addAction(action
);
245 QAction
* action
= menu
.exec(pos
.toPoint());
247 if (action
== addAction
) {
249 } else if (action
== showAllAction
) {
250 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
257 void PlacesPanel::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
259 m_model
->dropMimeData(index
, event
->mimeData());
262 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
265 const QString error
= DragAndDropHelper::dropUrls(KFileItem(), dest
, event
);
266 if (!error
.isEmpty()) {
267 emit
errorMessage(error
);
272 void PlacesPanel::slotTrashUpdated(KJob
* job
)
275 emit
errorMessage(job
->errorString());
277 org::kde::KDirNotify::emitFilesAdded("trash:/");
280 void PlacesPanel::emptyTrash()
282 const QString text
= i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
283 const bool del
= KMessageBox::warningContinueCancel(window(),
286 KGuiItem(i18nc("@action:button", "Empty Trash"),
288 ) == KMessageBox::Continue
;
290 QByteArray packedArgs
;
291 QDataStream
stream(&packedArgs
, QIODevice::WriteOnly
);
293 KIO::Job
*job
= KIO::special(KUrl("trash:/"), packedArgs
);
294 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent
);
295 job
->ui()->setWindow(parentWidget());
296 connect(job
, SIGNAL(result(KJob
*)), SLOT(slotTrashUpdated(KJob
*)));
300 void PlacesPanel::addEntry()
302 const int index
= m_controller
->selectionManager()->currentItem();
303 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
305 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
306 dialog
->setCaption(i18nc("@title:window", "Add Places Entry"));
307 dialog
->setAllowGlobal(true);
309 if (dialog
->exec() == QDialog::Accepted
) {
310 PlacesItem
* item
= m_model
->createPlacesItem(dialog
->text(), dialog
->url(), dialog
->icon());
312 // Insert the item as last item of the corresponding group.
314 while (i
< m_model
->count() && m_model
->placesItem(i
)->group() != item
->group()) {
318 bool inserted
= false;
319 while (!inserted
&& i
< m_model
->count()) {
320 if (m_model
->placesItem(i
)->group() != item
->group()) {
321 m_model
->insertItem(i
, item
);
328 m_model
->appendItem(item
);
335 void PlacesPanel::editEntry(int index
)
337 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
339 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
340 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
341 dialog
->setIcon(data
.value("iconName").toString());
342 dialog
->setText(data
.value("text").toString());
343 dialog
->setUrl(data
.value("url").value
<KUrl
>());
344 dialog
->setAllowGlobal(true);
345 if (dialog
->exec() == QDialog::Accepted
) {
346 PlacesItem
* oldItem
= m_model
->placesItem(index
);
348 oldItem
->setText(dialog
->text());
349 oldItem
->setUrl(dialog
->url());
350 oldItem
->setIcon(dialog
->icon());
357 void PlacesPanel::selectClosestItem()
359 const int index
= m_model
->closestItem(url());
360 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
361 selectionManager
->setCurrentItem(index
);
362 selectionManager
->clearSelection();
363 selectionManager
->setSelected(index
);
366 #include "placespanel.moc"