]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Places Panel: Show drop indicator
[dolphin.git] / src / panels / places / placespanel.cpp
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on KFilePlacesView from kdelibs: *
5 * Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
6 * Copyright (C) 2007 David Faure <faure@kde.org> *
7 * *
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. *
12 * *
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. *
17 * *
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 ***************************************************************************/
23
24 #include "placespanel.h"
25
26 #include <KConfigGroup>
27 #include <KDebug>
28 #include <KDirNotify>
29 #include <KIcon>
30 #include <KIO/Job>
31 #include <KIO/JobUiDelegate>
32 #include <KLocale>
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>
38 #include <KMenu>
39 #include <KMessageBox>
40 #include <KNotification>
41 #include "placesitem.h"
42 #include "placesitemeditdialog.h"
43 #include "placesitemlistgroupheader.h"
44 #include "placesitemlistwidget.h"
45 #include "placesitemmodel.h"
46 #include <views/draganddrophelper.h>
47 #include <QVBoxLayout>
48 #include <QShowEvent>
49
50 PlacesPanel::PlacesPanel(QWidget* parent) :
51 Panel(parent),
52 m_controller(0),
53 m_model(0)
54 {
55 }
56
57 PlacesPanel::~PlacesPanel()
58 {
59 }
60
61 bool PlacesPanel::urlChanged()
62 {
63 return true;
64 }
65
66 void PlacesPanel::showEvent(QShowEvent* event)
67 {
68 if (event->spontaneous()) {
69 Panel::showEvent(event);
70 return;
71 }
72
73 if (!m_controller) {
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)));
81
82 KStandardItemListView* view = new KStandardItemListView();
83 view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
84 view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
85
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
93 KItemListContainer* container = new KItemListContainer(m_controller, this);
94 container->setEnabledFrame(false);
95
96 QVBoxLayout* layout = new QVBoxLayout(this);
97 layout->setMargin(0);
98 layout->addWidget(container);
99
100 selectClosestItem();
101 }
102
103 Panel::showEvent(event);
104 }
105
106 void PlacesPanel::slotItemActivated(int index)
107 {
108 const KUrl url = m_model->data(index).value("url").value<KUrl>();
109 if (!url.isEmpty()) {
110 emit placeActivated(PlacesItemModel::convertedUrl(url));
111 }
112 }
113
114 void PlacesPanel::slotItemMiddleClicked(int index)
115 {
116 const KUrl url = m_model->data(index).value("url").value<KUrl>();
117 if (!url.isEmpty()) {
118 emit placeMiddleClicked(PlacesItemModel::convertedUrl(url));
119 }
120 }
121
122 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
123 {
124 PlacesItem* item = m_model->placesItem(index);
125 if (!item) {
126 return;
127 }
128
129 KMenu menu(this);
130
131 QAction* emptyTrashAction = 0;
132 QAction* addAction = 0;
133 QAction* mainSeparator = 0;
134 QAction* editAction = 0;
135 QAction* teardownAction = 0;
136 QAction* ejectAction = 0;
137
138 const QString label = item->text();
139
140 const bool isDevice = !item->udi().isEmpty();
141 if (isDevice) {
142 ejectAction = m_model->ejectAction(index);
143 if (ejectAction) {
144 ejectAction->setParent(&menu);
145 menu.addAction(ejectAction);
146 }
147
148 teardownAction = m_model->teardownAction(index);
149 if (teardownAction) {
150 teardownAction->setParent(&menu);
151 menu.addAction(teardownAction);
152 }
153
154 if (teardownAction || ejectAction) {
155 mainSeparator = menu.addSeparator();
156 }
157 } else {
158 if (item->url() == KUrl("trash:/")) {
159 emptyTrashAction = menu.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
160 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
161 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
162 menu.addSeparator();
163 }
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));
167 }
168
169 if (!addAction) {
170 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
171 }
172
173 QAction* openInNewTabAction = menu.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label));
174 openInNewTabAction->setIcon(KIcon("tab-new"));
175
176 QAction* removeAction = 0;
177 if (!isDevice && !item->isSystemItem()) {
178 removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label));
179 }
180
181 QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide '%1'", label));
182 hideAction->setCheckable(true);
183 hideAction->setChecked(item->isHidden());
184
185 QAction* showAllAction = 0;
186 if (m_model->hiddenCount() > 0) {
187 if (!mainSeparator) {
188 mainSeparator = menu.addSeparator();
189 }
190 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
191 showAllAction->setCheckable(true);
192 showAllAction->setChecked(m_model->hiddenItemsShown());
193 }
194
195 menu.addSeparator();
196 foreach (QAction* action, customContextMenuActions()) {
197 menu.addAction(action);
198 }
199
200 QAction* action = menu.exec(pos.toPoint());
201 if (action) {
202 if (action == emptyTrashAction) {
203 emptyTrash();
204 } else if (action == addAction) {
205 addEntry();
206 } else if (action == editAction) {
207 editEntry(index);
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);
221 }
222 }
223
224 selectClosestItem();
225 }
226
227 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
228 {
229 KMenu menu(this);
230
231 QAction* addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
232
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());
238 }
239
240 menu.addSeparator();
241 foreach (QAction* action, customContextMenuActions()) {
242 menu.addAction(action);
243 }
244
245 QAction* action = menu.exec(pos.toPoint());
246 if (action) {
247 if (action == addAction) {
248 addEntry();
249 } else if (action == showAllAction) {
250 m_model->setHiddenItemsShown(showAllAction->isChecked());
251 }
252 }
253
254 selectClosestItem();
255 }
256
257 void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent)
258 {
259 Q_UNUSED(parent);
260 const QString error = DragAndDropHelper::dropUrls(KFileItem(), dest, event);
261 if (!error.isEmpty()) {
262 emit errorMessage(error);
263 }
264
265 }
266
267 void PlacesPanel::slotTrashUpdated(KJob* job)
268 {
269 if (job->error()) {
270 emit errorMessage(job->errorString());
271 }
272 org::kde::KDirNotify::emitFilesAdded("trash:/");
273 }
274
275 void PlacesPanel::emptyTrash()
276 {
277 const QString text = i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
278 const bool del = KMessageBox::warningContinueCancel(window(),
279 text,
280 QString(),
281 KGuiItem(i18nc("@action:button", "Empty Trash"),
282 KIcon("user-trash"))
283 ) == KMessageBox::Continue;
284 if (del) {
285 QByteArray packedArgs;
286 QDataStream stream(&packedArgs, QIODevice::WriteOnly);
287 stream << int(1);
288 KIO::Job *job = KIO::special(KUrl("trash:/"), packedArgs);
289 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent);
290 job->ui()->setWindow(parentWidget());
291 connect(job, SIGNAL(result(KJob*)), SLOT(slotTrashUpdated(KJob*)));
292 }
293 }
294
295 void PlacesPanel::addEntry()
296 {
297 const int index = m_controller->selectionManager()->currentItem();
298 const KUrl url = m_model->data(index).value("url").value<KUrl>();
299
300 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
301 dialog->setCaption(i18nc("@title:window", "Add Places Entry"));
302 dialog->setAllowGlobal(true);
303 dialog->setUrl(url);
304 if (dialog->exec() == QDialog::Accepted) {
305 PlacesItem* item = m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
306
307 // Insert the item as last item of the corresponding group.
308 int i = 0;
309 while (i < m_model->count() && m_model->placesItem(i)->group() != item->group()) {
310 ++i;
311 }
312
313 bool inserted = false;
314 while (!inserted && i < m_model->count()) {
315 if (m_model->placesItem(i)->group() != item->group()) {
316 m_model->insertItem(i, item);
317 inserted = true;
318 }
319 ++i;
320 }
321
322 if (!inserted) {
323 m_model->appendItem(item);
324 }
325 }
326
327 delete dialog;
328 }
329
330 void PlacesPanel::editEntry(int index)
331 {
332 QHash<QByteArray, QVariant> data = m_model->data(index);
333
334 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
335 dialog->setCaption(i18nc("@title:window", "Edit Places Entry"));
336 dialog->setIcon(data.value("iconName").toString());
337 dialog->setText(data.value("text").toString());
338 dialog->setUrl(data.value("url").value<KUrl>());
339 dialog->setAllowGlobal(true);
340 if (dialog->exec() == QDialog::Accepted) {
341 PlacesItem* oldItem = m_model->placesItem(index);
342 if (oldItem) {
343 oldItem->setText(dialog->text());
344 oldItem->setUrl(dialog->url());
345 oldItem->setIcon(dialog->icon());
346 }
347 }
348
349 delete dialog;
350 }
351
352 void PlacesPanel::selectClosestItem()
353 {
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);
359 }
360
361 #include "placespanel.moc"