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