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