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");
80 KStandardItemListView
* view
= new KStandardItemListView();
81 view
->setWidgetCreator(new KItemListWidgetCreator
<PlacesItemListWidget
>());
82 view
->setGroupHeaderCreator(new KItemListGroupHeaderCreator
<PlacesItemListGroupHeader
>());
84 m_controller
= new KItemListController(m_model
, view
, this);
85 m_controller
->setSelectionBehavior(KItemListController::SingleSelection
);
86 connect(m_controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
87 connect(m_controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
88 connect(m_controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
89 connect(m_controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
91 KItemListContainer
* container
= new KItemListContainer(m_controller
, this);
92 container
->setEnabledFrame(false);
94 QVBoxLayout
* layout
= new QVBoxLayout(this);
96 layout
->addWidget(container
);
101 Panel::showEvent(event
);
104 void PlacesPanel::slotItemActivated(int index
)
106 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
107 if (!url
.isEmpty()) {
108 emit
placeActivated(url
);
112 void PlacesPanel::slotItemMiddleClicked(int index
)
114 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
115 if (!url
.isEmpty()) {
116 emit
placeMiddleClicked(url
);
120 void PlacesPanel::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
122 const QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
123 const QString label
= data
.value("text").toString();
127 QAction
* emptyTrashAction
= 0;
128 QAction
* addAction
= 0;
129 QAction
* mainSeparator
= 0;
130 QAction
* editAction
= 0;
131 QAction
* tearDownAction
= 0;
132 QAction
* ejectAction
= 0;
134 const bool isSystemItem
= m_model
->isSystemItem(index
);
135 const bool isDevice
= !data
.value("udi").toString().isEmpty();
137 ejectAction
= m_model
->ejectAction(index
);
139 ejectAction
->setParent(&menu
);
140 menu
.addAction(ejectAction
);
143 tearDownAction
= m_model
->tearDownAction(index
);
144 if (tearDownAction
) {
145 tearDownAction
->setParent(&menu
);
146 menu
.addAction(tearDownAction
);
149 if (tearDownAction
|| ejectAction
) {
150 mainSeparator
= menu
.addSeparator();
153 if (data
.value("url").value
<KUrl
>() == KUrl("trash:/")) {
154 emptyTrashAction
= menu
.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
155 KConfig
trashConfig("trashrc", KConfig::SimpleConfig
);
156 emptyTrashAction
->setEnabled(!trashConfig
.group("Status").readEntry("Empty", true));
159 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
161 mainSeparator
= menu
.addSeparator();
162 editAction
= menu
.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit Entry '%1'...", label
));
167 addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
170 QAction
* hideAction
= menu
.addAction(i18nc("@item:inmenu", "Hide Entry '%1'", label
));
171 hideAction
->setCheckable(true);
172 hideAction
->setChecked(data
.value("isHidden").toBool());
174 QAction
* showAllAction
= 0;
175 if (m_model
->hiddenCount() > 0) {
176 if (!mainSeparator
) {
177 mainSeparator
= menu
.addSeparator();
179 showAllAction
= menu
.addAction(i18nc("@item:inmenu", "Show All Entries"));
180 showAllAction
->setCheckable(true);
181 showAllAction
->setChecked(m_model
->hiddenItemsShown());
184 QAction
* removeAction
= 0;
185 if (!isDevice
&& !isSystemItem
) {
186 removeAction
= menu
.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove Entry '%1'", label
));
190 foreach (QAction
* action
, customContextMenuActions()) {
191 menu
.addAction(action
);
194 QAction
* action
= menu
.exec(pos
.toPoint());
196 if (action
== emptyTrashAction
) {
198 } else if (action
== addAction
) {
200 } else if (action
== editAction
) {
202 } else if (action
== removeAction
) {
203 m_model
->removeItem(index
);
204 } else if (action
== hideAction
) {
205 m_model
->setItemHidden(index
, hideAction
->isChecked());
206 } else if (action
== showAllAction
) {
207 m_model
->setHiddenItemsShown(showAllAction
->isChecked());
208 } else if (action
== tearDownAction
) {
209 } else if (action
== ejectAction
) {
216 void PlacesPanel::slotViewContextMenuRequested(const QPointF
& pos
)
220 QAction
* addAction
= menu
.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
222 foreach (QAction
* action
, customContextMenuActions()) {
223 menu
.addAction(action
);
226 QAction
* action
= menu
.exec(pos
.toPoint());
227 if (action
== addAction
) {
234 void PlacesPanel::slotUrlsDropped(const KUrl
& dest
, QDropEvent
* event
, QWidget
* parent
)
237 const QString error
= DragAndDropHelper::dropUrls(KFileItem(), dest
, event
);
238 if (!error
.isEmpty()) {
239 emit
errorMessage(error
);
244 void PlacesPanel::slotTrashUpdated(KJob
* job
)
247 // TODO: Show error-string inside Dolphin, don't use job->ui->showErrorMessage().
249 org::kde::KDirNotify::emitFilesAdded("trash:/");
252 void PlacesPanel::emptyTrash()
254 const QString text
= i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
255 const bool del
= KMessageBox::warningContinueCancel(window(),
258 KGuiItem(i18nc("@action:button", "Empty Trash"),
260 ) == KMessageBox::Continue
;
262 QByteArray packedArgs
;
263 QDataStream
stream(&packedArgs
, QIODevice::WriteOnly
);
265 KIO::Job
*job
= KIO::special(KUrl("trash:/"), packedArgs
);
266 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent
);
267 job
->ui()->setWindow(parentWidget());
268 connect(job
, SIGNAL(result(KJob
*)), SLOT(slotTrashUpdated(KJob
*)));
272 void PlacesPanel::addEntry()
274 const int index
= m_controller
->selectionManager()->currentItem();
275 const KUrl url
= m_model
->data(index
).value("url").value
<KUrl
>();
277 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
278 dialog
->setCaption(i18nc("@title:window", "Add Places Entry"));
279 dialog
->setAllowGlobal(true);
281 if (dialog
->exec() == QDialog::Accepted
) {
282 KStandardItem
* item
= createStandardItemFromDialog(dialog
);
284 // Insert the item as last item of the corresponding group.
286 while (i
< m_model
->count() && m_model
->item(i
)->group() != item
->group()) {
290 bool inserted
= false;
291 while (!inserted
&& i
< m_model
->count()) {
292 if (m_model
->item(i
)->group() != item
->group()) {
293 m_model
->insertItem(i
, item
);
300 m_model
->appendItem(item
);
307 void PlacesPanel::editEntry(int index
)
309 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
311 QPointer
<PlacesItemEditDialog
> dialog
= new PlacesItemEditDialog(this);
312 dialog
->setCaption(i18nc("@title:window", "Edit Places Entry"));
313 dialog
->setIcon(data
.value("iconName").toString());
314 dialog
->setText(data
.value("text").toString());
315 dialog
->setUrl(data
.value("url").value
<KUrl
>());
316 dialog
->setAllowGlobal(true);
317 if (dialog
->exec() == QDialog::Accepted
) {
318 KStandardItem
* oldItem
= m_model
->item(index
);
320 KStandardItem
* item
= createStandardItemFromDialog(dialog
);
321 // Although the user might have changed the URL of the item in a way
322 // that another group should be assigned, we still apply the old
323 // group to keep the same position for the item.
324 item
->setGroup(oldItem
->group());
325 m_model
->replaceItem(index
, item
);
332 void PlacesPanel::selectClosestItem()
334 const int index
= m_model
->closestItem(url());
335 KItemListSelectionManager
* selectionManager
= m_controller
->selectionManager();
336 selectionManager
->setCurrentItem(index
);
337 selectionManager
->clearSelection();
338 selectionManager
->setSelected(index
);
341 KStandardItem
* PlacesPanel::createStandardItemFromDialog(PlacesItemEditDialog
* dialog
) const
345 const KUrl newUrl
= dialog
->url();
346 KStandardItem
* item
= new KStandardItem();
347 item
->setIcon(KIcon(dialog
->icon()));
348 item
->setText(dialog
->text());
349 item
->setDataValue("url", newUrl
);
350 item
->setGroup(m_model
->groupName(newUrl
));
355 #include "placespanel.moc"