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