+QAction *PlacesPanel::buildGroupContextMenu(QMenu *menu, int index)
+{
+ if (index == -1) {
+ return nullptr;
+ }
+
+ KFilePlacesModel::GroupType groupType = m_model->groupType(index);
+ QAction *hideGroupAction = menu->addAction(i18nc("@item:inmenu", "Hide Section '%1'", m_model->item(index)->group()));
+ hideGroupAction->setCheckable(true);
+ hideGroupAction->setChecked(m_model->isGroupHidden(groupType));
+
+ connect(hideGroupAction, &QAction::triggered, this, [this, groupType, hideGroupAction]{
+ m_model->setGroupHidden(groupType, hideGroupAction->isChecked());
+ });
+
+ return hideGroupAction;
+}
+
+void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
+{
+ if (index < 0) {
+ return;
+ }
+
+ const PlacesItem* destItem = m_model->placesItem(index);
+
+ if (destItem->isSearchOrTimelineUrl()) {
+ return;
+ }
+
+ if (m_model->storageSetupNeeded(index)) {
+ connect(m_model, &PlacesItemModel::storageSetupDone,
+ this, &PlacesPanel::slotItemDropEventStorageSetupDone);
+
+ m_itemDropEventIndex = index;
+
+ // Make a full copy of the Mime-Data
+ m_itemDropEventMimeData = new QMimeData;
+ m_itemDropEventMimeData->setText(event->mimeData()->text());
+ m_itemDropEventMimeData->setHtml(event->mimeData()->html());
+ m_itemDropEventMimeData->setUrls(event->mimeData()->urls());
+ m_itemDropEventMimeData->setImageData(event->mimeData()->imageData());
+ m_itemDropEventMimeData->setColorData(event->mimeData()->colorData());
+
+ m_itemDropEvent = new QDropEvent(event->pos().toPoint(),
+ event->possibleActions(),
+ m_itemDropEventMimeData,
+ event->buttons(),
+ event->modifiers());
+
+ m_model->requestStorageSetup(index);
+ return;
+ }
+
+ QUrl destUrl = destItem->url();
+ QDropEvent dropEvent(event->pos().toPoint(),
+ event->possibleActions(),
+ event->mimeData(),
+ event->buttons(),
+ event->modifiers());
+
+ slotUrlsDropped(destUrl, &dropEvent, this);
+}
+
+void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success)
+{
+ disconnect(m_model, &PlacesItemModel::storageSetupDone,
+ this, &PlacesPanel::slotItemDropEventStorageSetupDone);
+
+ if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) {
+ if (success) {
+ QUrl destUrl = m_model->placesItem(index)->url();
+ slotUrlsDropped(destUrl, m_itemDropEvent, this);
+ }
+
+ delete m_itemDropEventMimeData;
+ delete m_itemDropEvent;
+
+ m_itemDropEventIndex = -1;
+ m_itemDropEventMimeData = nullptr;
+ m_itemDropEvent = nullptr;
+ }
+}
+
+void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
+{
+ m_model->dropMimeDataBefore(index, event->mimeData());
+}
+
+void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent)
+{
+ KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent);
+ if (job) {
+ connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); });
+ }
+}
+
+void PlacesPanel::slotTrashUpdated(KJob* job)
+{
+ if (job->error()) {
+ emit errorMessage(job->errorString());
+ }
+ // as long as KIO doesn't do this, do it ourselves
+ KNotification::event(QStringLiteral("Trash: emptied"), QString(), QPixmap(), nullptr, KNotification::DefaultEvent);
+}
+
+void PlacesPanel::slotStorageSetupDone(int index, bool success)
+{
+ disconnect(m_model, &PlacesItemModel::storageSetupDone,
+ this, &PlacesPanel::slotStorageSetupDone);
+
+ if (m_triggerStorageSetupButton == Qt::NoButton) {
+ return;
+ }
+
+ if (success) {
+ Q_ASSERT(!m_model->storageSetupNeeded(index));
+ triggerItem(index, m_triggerStorageSetupButton);
+ m_triggerStorageSetupButton = Qt::NoButton;
+ } else {
+ setUrl(m_storageSetupFailedUrl);
+ m_storageSetupFailedUrl = QUrl();
+ }
+}
+
+void PlacesPanel::emptyTrash()
+{
+ KIO::JobUiDelegate uiDelegate;
+ uiDelegate.setWindow(window());
+ if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
+ KIO::Job* job = KIO::emptyTrash();
+ KJobWidgets::setWindow(job, window());
+ connect(job, &KIO::Job::result, this, &PlacesPanel::slotTrashUpdated);
+ }
+}
+
+void PlacesPanel::addEntry()
+{
+ const int index = m_controller->selectionManager()->currentItem();
+ const QUrl url = m_model->data(index).value("url").toUrl();
+
+ QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
+ dialog->setWindowTitle(i18nc("@title:window", "Add Places Entry"));
+ dialog->setAllowGlobal(true);
+ dialog->setUrl(url);
+ if (dialog->exec() == QDialog::Accepted) {
+ m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
+ }
+
+ delete dialog;
+}
+
+void PlacesPanel::editEntry(int index)
+{
+ QHash<QByteArray, QVariant> data = m_model->data(index);
+
+ QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
+ dialog->setWindowTitle(i18nc("@title:window", "Edit Places Entry"));
+ dialog->setIcon(data.value("iconName").toString());
+ dialog->setText(data.value("text").toString());
+ dialog->setUrl(data.value("url").toUrl());
+ dialog->setAllowGlobal(true);
+ if (dialog->exec() == QDialog::Accepted) {
+ PlacesItem* oldItem = m_model->placesItem(index);
+ if (oldItem) {
+ oldItem->setText(dialog->text());
+ oldItem->setUrl(dialog->url());
+ oldItem->setIcon(dialog->icon());
+ m_model->refresh();
+ }
+ }
+
+ delete dialog;
+}
+
+void PlacesPanel::selectClosestItem()
+{
+ const int index = m_model->closestItem(url());
+ KItemListSelectionManager* selectionManager = m_controller->selectionManager();
+ selectionManager->setCurrentItem(index);
+ selectionManager->clearSelection();
+ selectionManager->setSelected(index);
+}
+
+void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
+{
+ const PlacesItem* item = m_model->placesItem(index);
+ if (!item) {
+ return;
+ }
+
+ if (m_model->storageSetupNeeded(index)) {
+ m_triggerStorageSetupButton = button;
+ m_storageSetupFailedUrl = url();
+
+ connect(m_model, &PlacesItemModel::storageSetupDone,
+ this, &PlacesPanel::slotStorageSetupDone);
+
+ m_model->requestStorageSetup(index);
+ } else {
+ m_triggerStorageSetupButton = Qt::NoButton;
+
+ const QUrl url = m_model->data(index).value("url").toUrl();
+ if (!url.isEmpty()) {
+ if (button == Qt::MiddleButton) {
+ emit placeMiddleClicked(KFilePlacesModel::convertedUrl(url));
+ } else {
+ emit placeActivated(KFilePlacesModel::convertedUrl(url));
+ }
+ }
+ }
+}