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 "placesitemlistwidget.h"
44 #include "placesitemmodel.h"
45 #include <views/draganddrophelper.h>
46 #include <QVBoxLayout>
49 PlacesPanel::PlacesPanel(QWidget
* parent
) :
56 PlacesPanel::~PlacesPanel()
60 bool PlacesPanel::urlChanged()
65 void PlacesPanel::showEvent(QShowEvent
* event
)
67 if (event
->spontaneous()) {
68 Panel::showEvent(event
);
73 // Postpone the creating of the controller to the first show event.
74 // This assures that no performance and memory overhead is given when the folders panel is not
75 // used at all and stays invisible.
76 m_model
= new PlacesItemModel(this);
77 m_model
->setGroupedSorting(true);
78 m_model
->setSortRole("group");
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
)));
93 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
94 container
->setEnabledFrame(false);
96 QVBoxLayout
* layout
= new QVBoxLayout(this);
98 layout
->addWidget(container
);
103 Panel::showEvent(event
);
106 void PlacesPanel::slotItemActivated(int index
)
108 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
109 if (!url
.isEmpty()) {
110 emit
placeActivated(url
);
114 void PlacesPanel::slotItemMiddleClicked(int index
)
116 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
117 if (!url
.isEmpty()) {
118 emit
placeMiddleClicked(url
);
122 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
124 const QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
125 const QString label
= data
.value("text").toString();
129 QAction
* emptyTrashAction
= 0;
130 QAction
* addAction
= 0;
131 QAction
* mainSeparator
= 0;
132 QAction
* editAction
= 0;
133 QAction
* teardownAction
= 0;
134 QAction
* ejectAction
= 0;
136 const bool isSystemItem
= m_model
->isSystemItem(index
);
137 const bool isDevice
= !data
.value("udi").toString().isEmpty();
139 ejectAction
= m_model
->ejectAction(index
);
141 ejectAction
->setParent(&menu
);
142 menu
.addAction(ejectAction
);
145 teardownAction
= m_model
->teardownAction(index
);
146 if (teardownAction
) {
147 teardownAction
->setParent(&menu
);
148 menu
.addAction(teardownAction
);
151 if (teardownAction
|| ejectAction
) {
152 mainSeparator
= menu
.addSeparator();
155 if (data
.value("url").value
<KUrl
>() == KUrl("trash:/")) {
156 emptyTrashAction
= menu
.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
157 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
158 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
161 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
162 mainSeparator
= menu
.addSeparator();
163 editAction
= menu
.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label
));
167 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
170 QAction
* openInNewTabAction
= menu
.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label
));
171 openInNewTabAction
->setIcon(KIcon("tab-new"));
173 QAction
* removeAction
= 0;
174 if (!isDevice
&& !isSystemItem
) {
175 removeAction
= menu
.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label
));
178 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide '%1'", label
));
179 hideAction
->setCheckable(true);
180 hideAction
->setChecked(data
.value("isHidden").toBool());
182 QAction
* showAllAction
= 0;
183 if (m_model
->hiddenCount() > 0) {
184 if (!mainSeparator
) {
185 mainSeparator
= menu
.addSeparator();
187 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
188 showAllAction
->setCheckable(true);
189 showAllAction
->setChecked(m_model
->hiddenItemsShown());
193 foreach (QAction
* action
, customContextMenuActions()) {
194 menu
.addAction(action
);
197 QAction
* action
= menu
.exec(pos
.toPoint());
199 if (action
== emptyTrashAction
) {
201 } else if (action
== addAction
) {
203 } else if (action
== editAction
) {
205 } else if (action
== removeAction
) {
206 m_model
->removeItem(index
);
207 } else if (action
== hideAction
) {
208 m_model
->setItemHidden(index
, hideAction
->isChecked());
209 } else if (action
== openInNewTabAction
) {
210 const KUrl url
= m_model
->item(index
)->dataValue("url").value
<KUrl
>();
211 emit
placeMiddleClicked(url
);
212 } else if (action
== showAllAction
) {
213 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
214 } else if (action
== teardownAction
) {
215 m_model
->requestTeardown(index
);
216 } else if (action
== ejectAction
) {
217 m_model
->requestEject(index
);
224 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
228 QAction
* addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
230 QAction
* showAllAction
= 0;
231 if (m_model
->hiddenCount() > 0) {
232 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
233 showAllAction
->setCheckable(true);
234 showAllAction
->setChecked(m_model
->hiddenItemsShown());
238 foreach (QAction
* action
, customContextMenuActions()) {
239 menu
.addAction(action
);
242 QAction
* action
= menu
.exec(pos
.toPoint());
244 if (action
== addAction
) {
246 } else if (action
== showAllAction
) {
247 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
254 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
257 const QString error
= DragAndDropHelper::dropUrls(KFileItem(), dest
, event
);
258 if (!error
.isEmpty()) {
259 emit
errorMessage(error
);
264 void PlacesPanel::slotTrashUpdated(KJob
* job
)
267 // TODO: Show error-string inside Dolphin, don't use job->ui->showErrorMessage().
269 org::kde::KDirNotify::emitFilesAdded("trash:/");
272 void PlacesPanel::emptyTrash()
274 const QString text
= i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
275 const bool del
= KMessageBox::warningContinueCancel(window(),
278 KGuiItem(i18nc("@action:button", "Empty Trash"),
280 ) == KMessageBox::Continue
;
282 QByteArray packedArgs
;
283 QDataStream
stream(&packedArgs
, QIODevice::WriteOnly
);
285 KIO::Job
*job
= KIO::special(KUrl("trash:/"), packedArgs
);
286 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent
);
287 job
->ui()->setWindow(parentWidget());
288 connect(job
, SIGNAL(result(KJob
*)), SLOT(slotTrashUpdated(KJob
*)));
292 void PlacesPanel::addEntry()
294 const int index
= m_controller
->selectionManager()->currentItem();
295 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
297 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
298 dialog
->setCaption(i18nc("@title:window", "Add Places Entry"));
299 dialog
->setAllowGlobal(true);
301 if (dialog
->exec() == QDialog::Accepted
) {
302 KStandardItem
* item
= createStandardItemFromDialog(dialog
);
304 // Insert the item as last item of the corresponding group.
306 while (i
< m_model
->count() && m_model
->item(i
)->group() != item
->group()) {
310 bool inserted
= false;
311 while (!inserted
&& i
< m_model
->count()) {
312 if (m_model
->item(i
)->group() != item
->group()) {
313 m_model
->insertItem(i
, item
);
320 m_model
->appendItem(item
);
327 void PlacesPanel::editEntry(int index
)
329 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
331 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
332 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
333 dialog
->setIcon(data
.value("iconName").toString());
334 dialog
->setText(data
.value("text").toString());
335 dialog
->setUrl(data
.value("url").value
<KUrl
>());
336 dialog
->setAllowGlobal(true);
337 if (dialog
->exec() == QDialog::Accepted
) {
338 KStandardItem
* oldItem
= m_model
->item(index
);
340 KStandardItem
* item
= createStandardItemFromDialog(dialog
);
341 // Although the user might have changed the URL of the item in a way
342 // that another group should be assigned, we still apply the old
343 // group to keep the same position for the item.
344 item
->setGroup(oldItem
->group());
345 m_model
->replaceItem(index
, item
);
352 void PlacesPanel::selectClosestItem()
354 const int index
= m_model
->closestItem(url());
355 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
356 selectionManager
->setCurrentItem(index
);
357 selectionManager
->clearSelection();
358 selectionManager
->setSelected(index
);
361 KStandardItem
* PlacesPanel::createStandardItemFromDialog(PlacesItemEditDialog
* dialog
) const
365 const KUrl newUrl
= dialog
->url();
366 KStandardItem
* item
= new KStandardItem();
367 item
->setIcon(dialog
->icon());
368 item
->setText(dialog
->text());
369 item
->setDataValue("url", newUrl
);
370 item
->setGroup(m_model
->groupName(newUrl
));
375 #include "placespanel.moc"