]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Re-implement dropping of files on folders in the Places Panel.
[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 <KDebug>
27 #include <KDirNotify>
28 #include <KIcon>
29 #include <KIO/Job>
30 #include <KIO/JobUiDelegate>
31 #include <KLocale>
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>
37 #include <KMenu>
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>
48 #include <QShowEvent>
49
50 PlacesPanel::PlacesPanel(QWidget* parent) :
51 Panel(parent),
52 m_controller(0),
53 m_model(0),
54 m_storageSetupFailedUrl(),
55 m_triggerStorageSetupButton()
56 {
57 }
58
59 PlacesPanel::~PlacesPanel()
60 {
61 }
62
63 bool PlacesPanel::urlChanged()
64 {
65 return true;
66 }
67
68 void PlacesPanel::showEvent(QShowEvent* event)
69 {
70 if (event->spontaneous()) {
71 Panel::showEvent(event);
72 return;
73 }
74
75 if (!m_controller) {
76 // Postpone the creating of the controller to the first show event.
77 // This assures that no performance and memory overhead is given when the folders panel is not
78 // used at all and stays invisible.
79 m_model = new PlacesItemModel(this);
80 m_model->setGroupedSorting(true);
81 connect(m_model, SIGNAL(errorMessage(QString)),
82 this, SIGNAL(errorMessage(QString)));
83
84 KStandardItemListView* view = new KStandardItemListView();
85 view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
86 view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
87
88 m_controller = new KItemListController(m_model, view, this);
89 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
90 m_controller->setSingleClickActivation(true);
91 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
92 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
93 connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
94 connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
95 connect(m_controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
96 connect(m_controller, SIGNAL(aboveItemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotAboveItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
97
98 KItemListContainer* container = new KItemListContainer(m_controller, this);
99 container->setEnabledFrame(false);
100
101 QVBoxLayout* layout = new QVBoxLayout(this);
102 layout->setMargin(0);
103 layout->addWidget(container);
104
105 selectClosestItem();
106 }
107
108 Panel::showEvent(event);
109 }
110
111 void PlacesPanel::slotItemActivated(int index)
112 {
113 triggerItem(index, Qt::LeftButton);
114 }
115
116 void PlacesPanel::slotItemMiddleClicked(int index)
117 {
118 triggerItem(index, Qt::MiddleButton);
119 }
120
121 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
122 {
123 PlacesItem* item = m_model->placesItem(index);
124 if (!item) {
125 return;
126 }
127
128 KMenu menu(this);
129
130 QAction* emptyTrashAction = 0;
131 QAction* addAction = 0;
132 QAction* mainSeparator = 0;
133 QAction* editAction = 0;
134 QAction* teardownAction = 0;
135 QAction* ejectAction = 0;
136
137 const QString label = item->text();
138
139 const bool isDevice = !item->udi().isEmpty();
140 if (isDevice) {
141 ejectAction = m_model->ejectAction(index);
142 if (ejectAction) {
143 ejectAction->setParent(&menu);
144 menu.addAction(ejectAction);
145 }
146
147 teardownAction = m_model->teardownAction(index);
148 if (teardownAction) {
149 teardownAction->setParent(&menu);
150 menu.addAction(teardownAction);
151 }
152
153 if (teardownAction || ejectAction) {
154 mainSeparator = menu.addSeparator();
155 }
156 } else {
157 if (item->url() == KUrl("trash:/")) {
158 emptyTrashAction = menu.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
159 emptyTrashAction->setEnabled(item->icon() == "user-trash-full");
160 menu.addSeparator();
161 }
162 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
163 mainSeparator = menu.addSeparator();
164 editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label));
165 }
166
167 if (!addAction) {
168 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
169 }
170
171 QAction* openInNewTabAction = menu.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label));
172 openInNewTabAction->setIcon(KIcon("tab-new"));
173
174 QAction* removeAction = 0;
175 if (!isDevice && !item->isSystemItem()) {
176 removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label));
177 }
178
179 QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide '%1'", label));
180 hideAction->setCheckable(true);
181 hideAction->setChecked(item->isHidden());
182
183 QAction* showAllAction = 0;
184 if (m_model->hiddenCount() > 0) {
185 if (!mainSeparator) {
186 mainSeparator = menu.addSeparator();
187 }
188 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
189 showAllAction->setCheckable(true);
190 showAllAction->setChecked(m_model->hiddenItemsShown());
191 }
192
193 menu.addSeparator();
194 foreach (QAction* action, customContextMenuActions()) {
195 menu.addAction(action);
196 }
197
198 QAction* action = menu.exec(pos.toPoint());
199 if (action) {
200 if (action == emptyTrashAction) {
201 emptyTrash();
202 } else if (action == addAction) {
203 addEntry();
204 } else if (action == editAction) {
205 editEntry(index);
206 } else if (action == removeAction) {
207 m_model->removeItem(index);
208 } else if (action == hideAction) {
209 item->setHidden(hideAction->isChecked());
210 } else if (action == openInNewTabAction) {
211 const KUrl url = m_model->item(index)->dataValue("url").value<KUrl>();
212 emit placeMiddleClicked(url);
213 } else if (action == showAllAction) {
214 m_model->setHiddenItemsShown(showAllAction->isChecked());
215 } else if (action == teardownAction) {
216 m_model->requestTeardown(index);
217 } else if (action == ejectAction) {
218 m_model->requestEject(index);
219 }
220 }
221
222 selectClosestItem();
223 }
224
225 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
226 {
227 KMenu menu(this);
228
229 QAction* addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
230
231 QAction* showAllAction = 0;
232 if (m_model->hiddenCount() > 0) {
233 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
234 showAllAction->setCheckable(true);
235 showAllAction->setChecked(m_model->hiddenItemsShown());
236 }
237
238 menu.addSeparator();
239 foreach (QAction* action, customContextMenuActions()) {
240 menu.addAction(action);
241 }
242
243 QAction* action = menu.exec(pos.toPoint());
244 if (action) {
245 if (action == addAction) {
246 addEntry();
247 } else if (action == showAllAction) {
248 m_model->setHiddenItemsShown(showAllAction->isChecked());
249 }
250 }
251
252 selectClosestItem();
253 }
254
255 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
256 {
257 if (index < 0) {
258 return;
259 }
260
261 KUrl destUrl = m_model->placesItem(index)->url();
262 QDropEvent dropEvent(event->pos().toPoint(),
263 event->possibleActions(),
264 event->mimeData(),
265 event->buttons(),
266 event->modifiers());
267
268 DragAndDropHelper::dropUrls(KFileItem(), destUrl, &dropEvent);
269 }
270
271 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
272 {
273 m_model->dropMimeDataBefore(index, event->mimeData());
274 }
275
276 void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent)
277 {
278 Q_UNUSED(parent);
279 const QString error = DragAndDropHelper::dropUrls(KFileItem(), dest, event);
280 if (!error.isEmpty()) {
281 emit errorMessage(error);
282 }
283
284 }
285
286 void PlacesPanel::slotTrashUpdated(KJob* job)
287 {
288 if (job->error()) {
289 emit errorMessage(job->errorString());
290 }
291 org::kde::KDirNotify::emitFilesAdded("trash:/");
292 }
293
294 void PlacesPanel::slotStorageSetupDone(int index, bool success)
295 {
296 disconnect(m_model, SIGNAL(storageSetupDone(int,bool)),
297 this, SLOT(slotStorageSetupDone(int,bool)));
298
299 if (m_triggerStorageSetupButton == Qt::NoButton) {
300 return;
301 }
302
303 if (success) {
304 Q_ASSERT(!m_model->storageSetupNeeded(index));
305 triggerItem(index, m_triggerStorageSetupButton);
306 m_triggerStorageSetupButton = Qt::NoButton;
307 } else {
308 setUrl(m_storageSetupFailedUrl);
309 m_storageSetupFailedUrl = KUrl();
310 }
311 }
312
313 void PlacesPanel::emptyTrash()
314 {
315 const QString text = i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
316 const bool del = KMessageBox::warningContinueCancel(window(),
317 text,
318 QString(),
319 KGuiItem(i18nc("@action:button", "Empty Trash"),
320 KIcon("user-trash"))
321 ) == KMessageBox::Continue;
322 if (del) {
323 QByteArray packedArgs;
324 QDataStream stream(&packedArgs, QIODevice::WriteOnly);
325 stream << int(1);
326 KIO::Job *job = KIO::special(KUrl("trash:/"), packedArgs);
327 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent);
328 job->ui()->setWindow(parentWidget());
329 connect(job, SIGNAL(result(KJob*)), SLOT(slotTrashUpdated(KJob*)));
330 }
331 }
332
333 void PlacesPanel::addEntry()
334 {
335 const int index = m_controller->selectionManager()->currentItem();
336 const KUrl url = m_model->data(index).value("url").value<KUrl>();
337
338 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
339 dialog->setCaption(i18nc("@title:window", "Add Places Entry"));
340 dialog->setAllowGlobal(true);
341 dialog->setUrl(url);
342 if (dialog->exec() == QDialog::Accepted) {
343 PlacesItem* item = m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
344 m_model->appendItemToGroup(item);
345 }
346
347 delete dialog;
348 }
349
350 void PlacesPanel::editEntry(int index)
351 {
352 QHash<QByteArray, QVariant> data = m_model->data(index);
353
354 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
355 dialog->setCaption(i18nc("@title:window", "Edit Places Entry"));
356 dialog->setIcon(data.value("iconName").toString());
357 dialog->setText(data.value("text").toString());
358 dialog->setUrl(data.value("url").value<KUrl>());
359 dialog->setAllowGlobal(true);
360 if (dialog->exec() == QDialog::Accepted) {
361 PlacesItem* oldItem = m_model->placesItem(index);
362 if (oldItem) {
363 oldItem->setText(dialog->text());
364 oldItem->setUrl(dialog->url());
365 oldItem->setIcon(dialog->icon());
366 }
367 }
368
369 delete dialog;
370 }
371
372 void PlacesPanel::selectClosestItem()
373 {
374 const int index = m_model->closestItem(url());
375 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
376 selectionManager->setCurrentItem(index);
377 selectionManager->clearSelection();
378 selectionManager->setSelected(index);
379 }
380
381 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
382 {
383 const PlacesItem* item = m_model->placesItem(index);
384 if (!item) {
385 return;
386 }
387
388 if (m_model->storageSetupNeeded(index)) {
389 m_triggerStorageSetupButton = button;
390 m_storageSetupFailedUrl = url();
391
392 connect(m_model, SIGNAL(storageSetupDone(int,bool)),
393 this, SLOT(slotStorageSetupDone(int,bool)));
394
395 m_model->requestStorageSetup(index);
396 } else {
397 m_triggerStorageSetupButton = Qt::NoButton;
398
399 const KUrl url = m_model->data(index).value("url").value<KUrl>();
400 if (!url.isEmpty()) {
401 if (button == Qt::MiddleButton) {
402 emit placeMiddleClicked(PlacesItemModel::convertedUrl(url));
403 } else {
404 emit placeActivated(PlacesItemModel::convertedUrl(url));
405 }
406 }
407 }
408 }
409
410
411 #include "placespanel.moc"