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