]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Replace kDebug/kWarning by categorized logging (org.kde.dolphin)
[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 "dolphindebug.h"
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 m_model->saveBookmarks();
290 } else if (action == hideAction) {
291 item->setHidden(hideAction->isChecked());
292 m_model->saveBookmarks();
293 } else if (action == openInNewTabAction) {
294 // TriggerItem does set up the storage first and then it will
295 // emit the slotItemMiddleClicked signal, because of Qt::MiddleButton.
296 triggerItem(index, Qt::MiddleButton);
297 } else if (action == teardownAction) {
298 m_model->requestTeardown(index);
299 } else if (action == ejectAction) {
300 m_model->requestEject(index);
301 }
302 }
303 }
304
305 selectClosestItem();
306 }
307
308 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
309 {
310 QMenu menu(this);
311
312 QAction* addAction = menu.addAction(QIcon::fromTheme("document-new"), i18nc("@item:inmenu", "Add Entry..."));
313
314 QAction* showAllAction = 0;
315 if (m_model->hiddenCount() > 0) {
316 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
317 showAllAction->setCheckable(true);
318 showAllAction->setChecked(m_model->hiddenItemsShown());
319 }
320
321 menu.addSeparator();
322 foreach (QAction* action, customContextMenuActions()) {
323 menu.addAction(action);
324 }
325
326 QAction* action = menu.exec(pos.toPoint());
327 if (action) {
328 if (action == addAction) {
329 addEntry();
330 } else if (action == showAllAction) {
331 m_model->setHiddenItemsShown(showAllAction->isChecked());
332 }
333 }
334
335 selectClosestItem();
336 }
337
338 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
339 {
340 if (index < 0) {
341 return;
342 }
343
344 const PlacesItem* destItem = m_model->placesItem(index);
345 const PlacesItem::GroupType group = destItem->groupType();
346 if (group == PlacesItem::SearchForType || group == PlacesItem::RecentlySavedType) {
347 return;
348 }
349
350 if (m_model->storageSetupNeeded(index)) {
351 connect(m_model, &PlacesItemModel::storageSetupDone,
352 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
353
354 m_itemDropEventIndex = index;
355
356 // Make a full copy of the Mime-Data
357 m_itemDropEventMimeData = new QMimeData;
358 m_itemDropEventMimeData->setText(event->mimeData()->text());
359 m_itemDropEventMimeData->setHtml(event->mimeData()->html());
360 m_itemDropEventMimeData->setUrls(event->mimeData()->urls());
361 m_itemDropEventMimeData->setImageData(event->mimeData()->imageData());
362 m_itemDropEventMimeData->setColorData(event->mimeData()->colorData());
363
364 m_itemDropEvent = new QDropEvent(event->pos().toPoint(),
365 event->possibleActions(),
366 m_itemDropEventMimeData,
367 event->buttons(),
368 event->modifiers());
369
370 m_model->requestStorageSetup(index);
371 return;
372 }
373
374 QUrl destUrl = destItem->url();
375 QDropEvent dropEvent(event->pos().toPoint(),
376 event->possibleActions(),
377 event->mimeData(),
378 event->buttons(),
379 event->modifiers());
380
381 slotUrlsDropped(destUrl, &dropEvent, this);
382 }
383
384 void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success)
385 {
386 disconnect(m_model, &PlacesItemModel::storageSetupDone,
387 this, &PlacesPanel::slotItemDropEventStorageSetupDone);
388
389 if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) {
390 if (success) {
391 QUrl destUrl = m_model->placesItem(index)->url();
392 slotUrlsDropped(destUrl, m_itemDropEvent, this);
393 }
394
395 delete m_itemDropEventMimeData;
396 delete m_itemDropEvent;
397
398 m_itemDropEventIndex = -1;
399 m_itemDropEventMimeData = 0;
400 m_itemDropEvent = 0;
401 }
402 }
403
404 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
405 {
406 m_model->dropMimeDataBefore(index, event->mimeData());
407 m_model->saveBookmarks();
408 }
409
410 void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
411 {
412 KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
413 if (job) {
414 connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
415 }
416 }
417
418 void PlacesPanel::slotTrashUpdated(KJob* job)
419 {
420 if (job->error()) {
421 emit errorMessage(job->errorString());
422 }
423 // as long as KIO doesn't do this, do it ourselves
424 KNotification::event("Trash: emptied", QString(), QPixmap(), 0, KNotification::DefaultEvent);
425 }
426
427 void PlacesPanel::slotStorageSetupDone(int index, bool success)
428 {
429 disconnect(m_model, &PlacesItemModel::storageSetupDone,
430 this, &PlacesPanel::slotStorageSetupDone);
431
432 if (m_triggerStorageSetupButton == Qt::NoButton) {
433 return;
434 }
435
436 if (success) {
437 Q_ASSERT(!m_model->storageSetupNeeded(index));
438 triggerItem(index, m_triggerStorageSetupButton);
439 m_triggerStorageSetupButton = Qt::NoButton;
440 } else {
441 setUrl(m_storageSetupFailedUrl);
442 m_storageSetupFailedUrl = QUrl();
443 }
444 }
445
446 void PlacesPanel::emptyTrash()
447 {
448 KIO::JobUiDelegate uiDelegate;
449 uiDelegate.setWindow(window());
450 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
451 KIO::Job* job = KIO::emptyTrash();
452 KJobWidgets::setWindow(job, window());
453 connect(job, &KIO::Job::result, this, &PlacesPanel::slotTrashUpdated);
454 }
455 }
456
457 void PlacesPanel::addEntry()
458 {
459 const int index = m_controller->selectionManager()->currentItem();
460 const QUrl url = m_model->data(index).value("url").value<QUrl>();
461
462 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
463 dialog->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
464 dialog->setAllowGlobal(true);
465 dialog->setUrl(url);
466 if (dialog->exec() == QDialog::Accepted) {
467 PlacesItem* item = m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
468 m_model->appendItemToGroup(item);
469 m_model->saveBookmarks();
470 }
471
472 delete dialog;
473 }
474
475 void PlacesPanel::editEntry(int index)
476 {
477 QHash<QByteArray, QVariant> data = m_model->data(index);
478
479 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
480 dialog->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
481 dialog->setIcon(data.value("iconName").toString());
482 dialog->setText(data.value("text").toString());
483 dialog->setUrl(data.value("url").value<QUrl>());
484 dialog->setAllowGlobal(true);
485 if (dialog->exec() == QDialog::Accepted) {
486 PlacesItem* oldItem = m_model->placesItem(index);
487 if (oldItem) {
488 oldItem->setText(dialog->text());
489 oldItem->setUrl(dialog->url());
490 oldItem->setIcon(dialog->icon());
491 m_model->saveBookmarks();
492 }
493 }
494
495 delete dialog;
496 }
497
498 void PlacesPanel::selectClosestItem()
499 {
500 const int index = m_model->closestItem(url());
501 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
502 selectionManager->setCurrentItem(index);
503 selectionManager->clearSelection();
504 selectionManager->setSelected(index);
505 }
506
507 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
508 {
509 const PlacesItem* item = m_model->placesItem(index);
510 if (!item) {
511 return;
512 }
513
514 if (m_model->storageSetupNeeded(index)) {
515 m_triggerStorageSetupButton = button;
516 m_storageSetupFailedUrl = url();
517
518 connect(m_model, &PlacesItemModel::storageSetupDone,
519 this, &PlacesPanel::slotStorageSetupDone);
520
521 m_model->requestStorageSetup(index);
522 } else {
523 m_triggerStorageSetupButton = Qt::NoButton;
524
525 const QUrl url = m_model->data(index).value("url").value<QUrl>();
526 if (!url.isEmpty()) {
527 if (button == Qt::MiddleButton) {
528 emit placeMiddleClicked(PlacesItemModel::convertedUrl(url));
529 } else {
530 emit placeActivated(PlacesItemModel::convertedUrl(url));
531 }
532 }
533 }
534 }