]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Merge branch 'Applications/14.12' into frameworks
[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 "dolphin_generalsettings.h"
27
28 #include <KFileItem>
29 #include <KDebug>
30 #include <KDirNotify>
31 #include <QIcon>
32 #include <KIO/Job>
33 #include <KIO/DropJob>
34 #include <KIO/EmptyTrashJob>
35 #include <KIO/JobUiDelegate>
36 #include <KJobWidgets>
37 #include <KLocalizedString>
38 #include <KIconLoader>
39 #include <kitemviews/kitemlistcontainer.h>
40 #include <kitemviews/kitemlistcontroller.h>
41 #include <kitemviews/kitemlistselectionmanager.h>
42 #include <kitemviews/kstandarditem.h>
43 #include <QMenu>
44 #include <KMessageBox>
45 #include <KNotification>
46 #include "placesitem.h"
47 #include "placesitemeditdialog.h"
48 #include "placesitemlistgroupheader.h"
49 #include "placesitemlistwidget.h"
50 #include "placesitemmodel.h"
51 #include "placesview.h"
52 #include <views/draganddrophelper.h>
53 #include <QGraphicsSceneDragDropEvent>
54 #include <QVBoxLayout>
55 #include <QShowEvent>
56 #include <QMimeData>
57
58 PlacesPanel::PlacesPanel(QWidget* parent) :
59 Panel(parent),
60 m_controller(0),
61 m_model(0),
62 m_storageSetupFailedUrl(),
63 m_triggerStorageSetupButton(),
64 m_itemDropEventIndex(-1),
65 m_itemDropEventMimeData(0),
66 m_itemDropEvent(0)
67 {
68 }
69
70 PlacesPanel::~PlacesPanel()
71 {
72 }
73
74 bool PlacesPanel::urlChanged()
75 {
76 if (!url().isValid() || url().scheme().contains("search")) {
77 // Skip results shown by a search, as possible identical
78 // directory names are useless without parent-path information.
79 return false;
80 }
81
82 if (m_controller) {
83 selectClosestItem();
84 }
85
86 return true;
87 }
88
89 void PlacesPanel::readSettings()
90 {
91 if (m_controller) {
92 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
93 m_controller->setAutoActivationDelay(delay);
94 }
95 }
96
97 void PlacesPanel::showEvent(QShowEvent* event)
98 {
99 if (event->spontaneous()) {
100 Panel::showEvent(event);
101 return;
102 }
103
104 if (!m_controller) {
105 // Postpone the creating of the controller to the first show event.
106 // This assures that no performance and memory overhead is given when the folders panel is not
107 // used at all and stays invisible.
108 m_model = new PlacesItemModel(this);
109 m_model->setGroupedSorting(true);
110 connect(m_model, &PlacesItemModel::errorMessage,
111 this, &PlacesPanel::errorMessage);
112
113 m_view = new PlacesView();
114 m_view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
115 m_view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
116
117 m_controller = new KItemListController(m_model, m_view, this);
118 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
119 m_controller->setSingleClickActivationEnforced(true);
120
121 readSettings();
122
123 connect(m_controller, &KItemListController::itemActivated, this, &PlacesPanel::slotItemActivated);
124 connect(m_controller, &KItemListController::itemMiddleClicked, this, &PlacesPanel::slotItemMiddleClicked);
125 connect(m_controller, &KItemListController::itemContextMenuRequested, this, &PlacesPanel::slotItemContextMenuRequested);
126 connect(m_controller, &KItemListController::viewContextMenuRequested, this, &PlacesPanel::slotViewContextMenuRequested);
127 connect(m_controller, &KItemListController::itemDropEvent, this, &PlacesPanel::slotItemDropEvent);
128 connect(m_controller, &KItemListController::aboveItemDropEvent, this, &PlacesPanel::slotAboveItemDropEvent);
129
130 KItemListContainer* container = new KItemListContainer(m_controller, this);
131 container->setEnabledFrame(false);
132
133 QVBoxLayout* layout = new QVBoxLayout(this);
134 layout->setMargin(0);
135 layout->addWidget(container);
136
137 selectClosestItem();
138 }
139
140 Panel::showEvent(event);
141 }
142
143 void PlacesPanel::slotItemActivated(int index)
144 {
145 triggerItem(index, Qt::LeftButton);
146 }
147
148 void PlacesPanel::slotItemMiddleClicked(int index)
149 {
150 triggerItem(index, Qt::MiddleButton);
151 }
152
153 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
154 {
155 PlacesItem* item = m_model->placesItem(index);
156 if (!item) {
157 return;
158 }
159
160 QMenu menu(this);
161
162 QAction* emptyTrashAction = 0;
163 QAction* addAction = 0;
164 QAction* mainSeparator = 0;
165 QAction* editAction = 0;
166 QAction* teardownAction = 0;
167 QAction* ejectAction = 0;
168
169 const QString label = item->text();
170
171 const bool isDevice = !item->udi().isEmpty();
172 if (isDevice) {
173 ejectAction = m_model->ejectAction(index);
174 if (ejectAction) {
175 ejectAction->setParent(&menu);
176 menu.addAction(ejectAction);
177 }
178
179 teardownAction = m_model->teardownAction(index);
180 if (teardownAction) {
181 teardownAction->setParent(&menu);
182 menu.addAction(teardownAction);
183 }
184
185 if (teardownAction || ejectAction) {
186 mainSeparator = menu.addSeparator();
187 }
188 } else {
189 if (item->url() == QUrl("trash:/")) {
190 emptyTrashAction = menu.addAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
191 emptyTrashAction->setEnabled(item->icon() == "user-trash-full");
192 menu.addSeparator();
193 }
194 addAction = menu.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
195 mainSeparator = menu.addSeparator();
196 editAction = menu.addAction(QIcon::fromTheme("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label));
197 }
198
199 if (!addAction) {
200 addAction = menu.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
201 }
202
203 QAction* openInNewTabAction = menu.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label));
204 openInNewTabAction->setIcon(QIcon::fromTheme("tab-new"));
205
206 QAction* removeAction = 0;
207 if (!isDevice && !item->isSystemItem()) {
208 removeAction = menu.addAction(QIcon::fromTheme("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label));
209 }
210
211 QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide '%1'", label));
212 hideAction->setCheckable(true);
213 hideAction->setChecked(item->isHidden());
214
215 QAction* showAllAction = 0;
216 if (m_model->hiddenCount() > 0) {
217 if (!mainSeparator) {
218 mainSeparator = menu.addSeparator();
219 }
220 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
221 showAllAction->setCheckable(true);
222 showAllAction->setChecked(m_model->hiddenItemsShown());
223 }
224
225 menu.addSeparator();
226 QMenu* iconSizeSubMenu = new QMenu(i18nc("@item:inmenu", "Icon Size"), &menu);
227
228 struct IconSizeInfo
229 {
230 int size;
231 const char* context;
232 const char* text;
233 };
234
235 const int iconSizeCount = 4;
236 static const IconSizeInfo iconSizes[iconSizeCount] = {
237 {KIconLoader::SizeSmall, I18N_NOOP2_NOSTRIP("Small icon size", "Small (%1x%2)")},
238 {KIconLoader::SizeSmallMedium, I18N_NOOP2_NOSTRIP("Medium icon size", "Medium (%1x%2)")},
239 {KIconLoader::SizeMedium, I18N_NOOP2_NOSTRIP("Large icon size", "Large (%1x%2)")},
240 {KIconLoader::SizeLarge, I18N_NOOP2_NOSTRIP("Huge icon size", "Huge (%1x%2)")}
241 };
242
243 QMap<QAction*, int> iconSizeActionMap;
244 QActionGroup* iconSizeGroup = new QActionGroup(iconSizeSubMenu);
245
246 for (int i = 0; i < iconSizeCount; ++i) {
247 const int size = iconSizes[i].size;
248 const QString text = i18nc(iconSizes[i].context, iconSizes[i].text,
249 size, size);
250
251 QAction* action = iconSizeSubMenu->addAction(text);
252 iconSizeActionMap.insert(action, size);
253 action->setActionGroup(iconSizeGroup);
254 action->setCheckable(true);
255 action->setChecked(m_view->iconSize() == size);
256 }
257
258 menu.addMenu(iconSizeSubMenu);
259
260 menu.addSeparator();
261 foreach (QAction* action, customContextMenuActions()) {
262 menu.addAction(action);
263 }
264
265 QAction* action = menu.exec(pos.toPoint());
266 if (action) {
267 if (action == emptyTrashAction) {
268 emptyTrash();
269 } else if (action == addAction) {
270 addEntry();
271 } else if (action == showAllAction) {
272 m_model->setHiddenItemsShown(showAllAction->isChecked());
273 } else if (iconSizeActionMap.contains(action)) {
274 m_view->setIconSize(iconSizeActionMap.value(action));
275 } else {
276 // The index might have changed if devices were added/removed while
277 // the context menu was open.
278 index = m_model->index(item);
279 if (index < 0) {
280 // The item is not in the model any more, probably because it was an
281 // external device that has been removed while the context menu was open.
282 return;
283 }
284
285 if (action == editAction) {
286 editEntry(index);
287 } else if (action == removeAction) {
288 m_model->removeItem(index);
289 } else if (action == hideAction) {
290 item->setHidden(hideAction->isChecked());
291 } else if (action == openInNewTabAction) {
292 // TriggerItem does set up the storage first and then it will
293 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
294 triggerItem(index, Qt::MiddleButton);
295 } else if (action == teardownAction) {
296 m_model->requestTeardown(index);
297 } else if (action == ejectAction) {
298 m_model->requestEject(index);
299 }
300 }
301 }
302
303 selectClosestItem();
304 }
305
306 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
307 {
308 QMenu menu(this);
309
310 QAction* addAction = menu.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
311
312 QAction* showAllAction = 0;
313 if (m_model->hiddenCount() > 0) {
314 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
315 showAllAction->setCheckable(true);
316 showAllAction->setChecked(m_model->hiddenItemsShown());
317 }
318
319 menu.addSeparator();
320 foreach (QAction* action, customContextMenuActions()) {
321 menu.addAction(action);
322 }
323
324 QAction* action = menu.exec(pos.toPoint());
325 if (action) {
326 if (action == addAction) {
327 addEntry();
328 } else if (action == showAllAction) {
329 m_model->setHiddenItemsShown(showAllAction->isChecked());
330 }
331 }
332
333 selectClosestItem();
334 }
335
336 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
337 {
338 if (index < 0) {
339 return;
340 }
341
342 const PlacesItem* destItem = m_model->placesItem(index);
343 const PlacesItem::GroupType group = destItem->groupType();
344 if (group == PlacesItem::SearchForType || group == PlacesItem::RecentlySavedType) {
345 return;
346 }
347
348 if (m_model->storageSetupNeeded(index)) {
349 connect(m_model, &PlacesItemModel::storageSetupDone,
350 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
351
352 m_itemDropEventIndex = index;
353
354 // Make a full copy of the Mime-Data
355 m_itemDropEventMimeData = new QMimeData;
356 m_itemDropEventMimeData->setText(event->mimeData()->text());
357 m_itemDropEventMimeData->setHtml(event->mimeData()->html());
358 m_itemDropEventMimeData->setUrls(event->mimeData()->urls());
359 m_itemDropEventMimeData->setImageData(event->mimeData()->imageData());
360 m_itemDropEventMimeData->setColorData(event->mimeData()->colorData());
361
362 m_itemDropEvent = new QDropEvent(event->pos().toPoint(),
363 event->possibleActions(),
364 m_itemDropEventMimeData,
365 event->buttons(),
366 event->modifiers());
367
368 m_model->requestStorageSetup(index);
369 return;
370 }
371
372 QUrl destUrl = destItem->url();
373 QDropEvent dropEvent(event->pos().toPoint(),
374 event->possibleActions(),
375 event->mimeData(),
376 event->buttons(),
377 event->modifiers());
378
379 slotUrlsDropped(destUrl, &dropEvent, this);
380 }
381
382 void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success)
383 {
384 disconnect(m_model, &PlacesItemModel::storageSetupDone,
385 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
386
387 if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) {
388 if (success) {
389 QUrl destUrl = m_model->placesItem(index)->url();
390 slotUrlsDropped(destUrl, m_itemDropEvent, this);
391 }
392
393 delete m_itemDropEventMimeData;
394 delete m_itemDropEvent;
395
396 m_itemDropEventIndex = -1;
397 m_itemDropEventMimeData = 0;
398 m_itemDropEvent = 0;
399 }
400 }
401
402 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
403 {
404 m_model->dropMimeDataBefore(index, event->mimeData());
405 }
406
407 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
408 {
409 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
410 if (job) {
411 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
412 }
413 }
414
415 void PlacesPanel::slotTrashUpdated(KJob* job)
416 {
417 if (job->error()) {
418 emit errorMessage(job->errorString());
419 }
420 // as long as KIO doesn't do this, do it ourselves
421 KNotification::event("Trash: emptied", QString(), QPixmap(), 0, KNotification::DefaultEvent);
422 }
423
424 void PlacesPanel::slotStorageSetupDone(int index, bool success)
425 {
426 disconnect(m_model, &PlacesItemModel::storageSetupDone,
427 this, &PlacesPanel::slotStorageSetupDone);
428
429 if (m_triggerStorageSetupButton == Qt::NoButton) {
430 return;
431 }
432
433 if (success) {
434 Q_ASSERT(!m_model->storageSetupNeeded(index));
435 triggerItem(index, m_triggerStorageSetupButton);
436 m_triggerStorageSetupButton = Qt::NoButton;
437 } else {
438 setUrl(m_storageSetupFailedUrl);
439 m_storageSetupFailedUrl = QUrl();
440 }
441 }
442
443 void PlacesPanel::emptyTrash()
444 {
445 KIO::JobUiDelegate uiDelegate;
446 uiDelegate.setWindow(window());
447 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
448 KIO::Job* job = KIO::emptyTrash();
449 KJobWidgets::setWindow(job, window());
450 connect(job, &KIO::Job::result, this, &PlacesPanel::slotTrashUpdated);
451 }
452 }
453
454 void PlacesPanel::addEntry()
455 {
456 const int index = m_controller->selectionManager()->currentItem();
457 const QUrl url = m_model->data(index).value("url").value<QUrl>();
458
459 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
460 dialog->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
461 dialog->setAllowGlobal(true);
462 dialog->setUrl(url);
463 if (dialog->exec() == QDialog::Accepted) {
464 PlacesItem* item = m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
465 m_model->appendItemToGroup(item);
466 }
467
468 delete dialog;
469 }
470
471 void PlacesPanel::editEntry(int index)
472 {
473 QHash<QByteArray, QVariant> data = m_model->data(index);
474
475 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
476 dialog->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
477 dialog->setIcon(data.value("iconName").toString());
478 dialog->setText(data.value("text").toString());
479 dialog->setUrl(data.value("url").value<QUrl>());
480 dialog->setAllowGlobal(true);
481 if (dialog->exec() == QDialog::Accepted) {
482 PlacesItem* oldItem = m_model->placesItem(index);
483 if (oldItem) {
484 oldItem->setText(dialog->text());
485 oldItem->setUrl(dialog->url());
486 oldItem->setIcon(dialog->icon());
487 }
488 }
489
490 delete dialog;
491 }
492
493 void PlacesPanel::selectClosestItem()
494 {
495 const int index = m_model->closestItem(url());
496 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
497 selectionManager->setCurrentItem(index);
498 selectionManager->clearSelection();
499 selectionManager->setSelected(index);
500 }
501
502 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
503 {
504 const PlacesItem* item = m_model->placesItem(index);
505 if (!item) {
506 return;
507 }
508
509 if (m_model->storageSetupNeeded(index)) {
510 m_triggerStorageSetupButton = button;
511 m_storageSetupFailedUrl = url();
512
513 connect(m_model, &PlacesItemModel::storageSetupDone,
514 this, &PlacesPanel::slotStorageSetupDone);
515
516 m_model->requestStorageSetup(index);
517 } else {
518 m_triggerStorageSetupButton = Qt::NoButton;
519
520 const QUrl url = m_model->data(index).value("url").value<QUrl>();
521 if (!url.isEmpty()) {
522 if (button == Qt::MiddleButton) {
523 emit placeMiddleClicked(PlacesItemModel::convertedUrl(url));
524 } else {
525 emit placeActivated(PlacesItemModel::convertedUrl(url));
526 }
527 }
528 }
529 }