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 <KConfigGroup>
31 #include <KIO/JobUiDelegate>
33 #include <kitemviews/kitemlistcontainer.h>
34 #include <kitemviews/kitemlistcontroller.h>
35 #include <kitemviews/kitemlistselectionmanager.h>
36 #include <kitemviews/kstandarditem.h>
37 #include <kitemviews/kstandarditemlistview.h>
39 #include <KMessageBox>
40 #include <KNotification>
41 #include "placesitemeditdialog.h"
42 #include "placesitemlistgroupheader.h"
43 #include "placesitemmodel.h"
44 #include <views/draganddrophelper.h>
45 #include <QVBoxLayout>
48 PlacesPanel::PlacesPanel(QWidget
* parent
) :
55 PlacesPanel::~PlacesPanel()
59 bool PlacesPanel::urlChanged()
64 void PlacesPanel::showEvent(QShowEvent
* event
)
66 if (event
->spontaneous()) {
67 Panel::showEvent(event
);
72 // Postpone the creating of the controller to the first show event.
73 // This assures that no performance and memory overhead is given when the folders panel is not
74 // used at all and stays invisible.
75 m_model
= new PlacesItemModel(this);
76 m_model
->setGroupedSorting(true);
77 m_model
->setSortRole("group");
79 KStandardItemListView
* view
= new KStandardItemListView();
80 view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
82 m_controller
= new KItemListController(m_model
, view
, this);
83 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
84 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
85 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
86 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
87 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
89 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
90 container
->setEnabledFrame(false);
92 QVBoxLayout
* layout
= new QVBoxLayout(this);
94 layout
->addWidget(container
);
99 Panel::showEvent(event
);
102 void PlacesPanel::slotItemActivated(int index
)
104 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
105 if (!url
.isEmpty()) {
106 emit
placeActivated(url
);
110 void PlacesPanel::slotItemMiddleClicked(int index
)
112 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
113 if (!url
.isEmpty()) {
114 emit
placeMiddleClicked(url
);
118 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
120 const QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
121 const QString label
= data
.value("text").toString();
125 QAction
* emptyTrashAction
= 0;
126 QAction
* addAction
= 0;
127 QAction
* mainSeparator
= 0;
128 QAction
* editAction
= 0;
129 QAction
* tearDownAction
= 0;
130 QAction
* ejectAction
= 0;
132 const bool isSystemItem
= m_model
->isSystemItem(index
);
133 const bool isDevice
= !data
.value("udi").toString().isEmpty();
135 ejectAction
= m_model
->ejectAction(index
);
137 ejectAction
->setParent(&menu
);
138 menu
.addAction(ejectAction
);
141 tearDownAction
= m_model
->tearDownAction(index
);
142 if (tearDownAction
) {
143 tearDownAction
->setParent(&menu
);
144 menu
.addAction(tearDownAction
);
147 if (tearDownAction
|| ejectAction
) {
148 mainSeparator
= menu
.addSeparator();
151 if (data
.value("url").value
<KUrl
>() == KUrl("trash:/")) {
152 emptyTrashAction
= menu
.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
153 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
154 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
157 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
159 mainSeparator
= menu
.addSeparator();
160 editAction
= menu
.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit Entry '%1'...", label
));
165 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
168 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide Entry '%1'", label
));
169 hideAction
->setCheckable(true);
170 //hideEntry->setChecked(data.value("hidden").toBool());
172 QAction
* showAllAction
= 0;
173 if (m_model
->hiddenCount() > 0) {
174 if (!mainSeparator
) {
175 mainSeparator
= menu
.addSeparator();
177 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
178 showAllAction
->setCheckable(true);
179 //showAllEntries->setChecked(showAll)
182 QAction
* removeAction
= 0;
183 if (!isDevice
&& !isSystemItem
) {
184 removeAction
= menu
.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove Entry '%1'", label
));
188 foreach (QAction
* action
, customContextMenuActions()) {
189 menu
.addAction(action
);
192 QAction
* action
= menu
.exec(pos
.toPoint());
194 if (action
== emptyTrashAction
) {
196 } else if (action
== addAction
) {
198 } else if (action
== editAction
) {
200 } else if (action
== removeAction
) {
201 m_model
->removeItem(index
);
202 } else if (action
== hideAction
) {
203 } else if (action
== showAllAction
) {
204 } else if (action
== tearDownAction
) {
205 } else if (action
== ejectAction
) {
212 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
216 QAction
* addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
218 foreach (QAction
* action
, customContextMenuActions()) {
219 menu
.addAction(action
);
222 QAction
* action
= menu
.exec(pos
.toPoint());
223 if (action
== addAction
) {
230 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
233 DragAndDropHelper::dropUrls(KFileItem(), dest
, event
);
236 void PlacesPanel::slotTrashUpdated(KJob
* job
)
239 // TODO: Show error-string inside Dolphin, don't use job->ui->showErrorMessage().
241 org::kde::KDirNotify::emitFilesAdded("trash:/");
244 void PlacesPanel::emptyTrash()
246 const QString text
= i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
247 const bool del
= KMessageBox::warningContinueCancel(window(),
250 KGuiItem(i18nc("@action:button", "Empty Trash"),
252 ) == KMessageBox::Continue
;
254 QByteArray packedArgs
;
255 QDataStream
stream(&packedArgs
, QIODevice::WriteOnly
);
257 KIO::Job
*job
= KIO::special(KUrl("trash:/"), packedArgs
);
258 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent
);
259 job
->ui()->setWindow(parentWidget());
260 connect(job
, SIGNAL(result(KJob
*)), SLOT(slotTrashUpdated(KJob
*)));
264 void PlacesPanel::addEntry()
266 const int index
= m_controller
->selectionManager()->currentItem();
267 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
269 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
270 dialog
->setCaption(i18nc("@title:window", "Add Places Entry"));
271 dialog
->setAllowGlobal(true);
273 if (dialog
->exec() == QDialog::Accepted
) {
274 KStandardItem
* item
= createStandardItemFromDialog(dialog
);
276 // Insert the item as last item of the "Places" group
277 bool inserted
= false;
279 while (!inserted
&& i
< m_model
->count()) {
280 if (m_model
->item(i
)->group() != m_model
->placesGroupName()) {
281 m_model
->insertItem(i
, item
);
288 m_model
->appendItem(item
);
295 void PlacesPanel::editEntry(int index
)
297 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
299 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
300 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
301 dialog
->setIcon(data
.value("iconName").toString());
302 dialog
->setText(data
.value("text").toString());
303 dialog
->setUrl(data
.value("url").value
<KUrl
>());
304 dialog
->setAllowGlobal(true);
305 if (dialog
->exec() == QDialog::Accepted
) {
306 KStandardItem
* oldItem
= m_model
->item(index
);
308 KStandardItem
* item
= createStandardItemFromDialog(dialog
);
309 item
->setGroup(oldItem
->group());
310 m_model
->replaceItem(index
, item
);
317 void PlacesPanel::selectClosestItem()
319 const int index
= m_model
->closestItem(url());
320 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
321 selectionManager
->setCurrentItem(index
);
322 selectionManager
->clearSelection();
323 selectionManager
->setSelected(index
);
326 KStandardItem
* PlacesPanel::createStandardItemFromDialog(PlacesItemEditDialog
* dialog
) const
330 KStandardItem
* item
= new KStandardItem();
331 item
->setIcon(KIcon(dialog
->icon()));
332 item
->setText(dialog
->text());
333 item
->setDataValue("url", dialog
->url());
334 item
->setGroup(m_model
->placesGroupName());
339 #include "placespanel.moc"