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