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