]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placespanel.cpp
Fixes Bug 242007 - "Open Folder during Drag operation" cannot go into different parti...
[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 <KLocale>
34 #include <kitemviews/kitemlistcontainer.h>
35 #include <kitemviews/kitemlistcontroller.h>
36 #include <kitemviews/kitemlistselectionmanager.h>
37 #include <kitemviews/kstandarditem.h>
38 #include <kitemviews/kstandarditemlistview.h>
39 #include <KMenu>
40 #include <KMessageBox>
41 #include <KNotification>
42 #include "placesitem.h"
43 #include "placesitemeditdialog.h"
44 #include "placesitemlistgroupheader.h"
45 #include "placesitemlistwidget.h"
46 #include "placesitemmodel.h"
47 #include <views/draganddrophelper.h>
48 #include <QGraphicsSceneDragDropEvent>
49 #include <QVBoxLayout>
50 #include <QShowEvent>
51
52 PlacesPanel::PlacesPanel(QWidget* parent) :
53 Panel(parent),
54 m_controller(0),
55 m_model(0),
56 m_storageSetupFailedUrl(),
57 m_triggerStorageSetupButton()
58 {
59 }
60
61 PlacesPanel::~PlacesPanel()
62 {
63 }
64
65 bool PlacesPanel::urlChanged()
66 {
67 if (!url().isValid() || url().protocol().contains("search")) {
68 // Skip results shown by a search, as possible identical
69 // directory names are useless without parent-path information.
70 return false;
71 }
72
73 if (m_controller) {
74 selectClosestItem();
75 }
76
77 return true;
78 }
79
80 void PlacesPanel::readSettings()
81 {
82 if (m_controller) {
83 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
84 m_controller->setAutoActivationDelay(delay);
85 }
86 }
87
88 void PlacesPanel::showEvent(QShowEvent* event)
89 {
90 if (event->spontaneous()) {
91 Panel::showEvent(event);
92 return;
93 }
94
95 if (!m_controller) {
96 // Postpone the creating of the controller to the first show event.
97 // This assures that no performance and memory overhead is given when the folders panel is not
98 // used at all and stays invisible.
99 m_model = new PlacesItemModel(this);
100 m_model->setGroupedSorting(true);
101 connect(m_model, SIGNAL(errorMessage(QString)),
102 this, SIGNAL(errorMessage(QString)));
103
104 KStandardItemListView* view = new KStandardItemListView();
105 view->setWidgetCreator(new KItemListWidgetCreator<PlacesItemListWidget>());
106 view->setGroupHeaderCreator(new KItemListGroupHeaderCreator<PlacesItemListGroupHeader>());
107
108 m_controller = new KItemListController(m_model, view, this);
109 m_controller->setSelectionBehavior(KItemListController::SingleSelection);
110 m_controller->setSingleClickActivation(true);
111
112 readSettings();
113
114 connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
115 connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
116 connect(m_controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
117 connect(m_controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
118 connect(m_controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
119 connect(m_controller, SIGNAL(aboveItemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotAboveItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
120
121 KItemListContainer* container = new KItemListContainer(m_controller, this);
122 container->setEnabledFrame(false);
123
124 QVBoxLayout* layout = new QVBoxLayout(this);
125 layout->setMargin(0);
126 layout->addWidget(container);
127
128 selectClosestItem();
129 }
130
131 Panel::showEvent(event);
132 }
133
134 void PlacesPanel::slotItemActivated(int index)
135 {
136 triggerItem(index, Qt::LeftButton);
137 }
138
139 void PlacesPanel::slotItemMiddleClicked(int index)
140 {
141 triggerItem(index, Qt::MiddleButton);
142 }
143
144 void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos)
145 {
146 PlacesItem* item = m_model->placesItem(index);
147 if (!item) {
148 return;
149 }
150
151 KMenu menu(this);
152
153 QAction* emptyTrashAction = 0;
154 QAction* addAction = 0;
155 QAction* mainSeparator = 0;
156 QAction* editAction = 0;
157 QAction* teardownAction = 0;
158 QAction* ejectAction = 0;
159
160 const QString label = item->text();
161
162 const bool isDevice = !item->udi().isEmpty();
163 if (isDevice) {
164 ejectAction = m_model->ejectAction(index);
165 if (ejectAction) {
166 ejectAction->setParent(&menu);
167 menu.addAction(ejectAction);
168 }
169
170 teardownAction = m_model->teardownAction(index);
171 if (teardownAction) {
172 teardownAction->setParent(&menu);
173 menu.addAction(teardownAction);
174 }
175
176 if (teardownAction || ejectAction) {
177 mainSeparator = menu.addSeparator();
178 }
179 } else {
180 if (item->url() == KUrl("trash:/")) {
181 emptyTrashAction = menu.addAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"));
182 emptyTrashAction->setEnabled(item->icon() == "user-trash-full");
183 menu.addSeparator();
184 }
185 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
186 mainSeparator = menu.addSeparator();
187 editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit '%1'...", label));
188 }
189
190 if (!addAction) {
191 addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
192 }
193
194 QAction* openInNewTabAction = menu.addAction(i18nc("@item:inmenu", "Open '%1' in New Tab", label));
195 openInNewTabAction->setIcon(KIcon("tab-new"));
196
197 QAction* removeAction = 0;
198 if (!isDevice && !item->isSystemItem()) {
199 removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove '%1'", label));
200 }
201
202 QAction* hideAction = menu.addAction(i18nc("@item:inmenu", "Hide '%1'", label));
203 hideAction->setCheckable(true);
204 hideAction->setChecked(item->isHidden());
205
206 QAction* showAllAction = 0;
207 if (m_model->hiddenCount() > 0) {
208 if (!mainSeparator) {
209 mainSeparator = menu.addSeparator();
210 }
211 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
212 showAllAction->setCheckable(true);
213 showAllAction->setChecked(m_model->hiddenItemsShown());
214 }
215
216 menu.addSeparator();
217 foreach (QAction* action, customContextMenuActions()) {
218 menu.addAction(action);
219 }
220
221 QAction* action = menu.exec(pos.toPoint());
222 if (action) {
223 if (action == emptyTrashAction) {
224 emptyTrash();
225 } else if (action == addAction) {
226 addEntry();
227 } else if (action == editAction) {
228 editEntry(index);
229 } else if (action == removeAction) {
230 m_model->removeItem(index);
231 } else if (action == hideAction) {
232 item->setHidden(hideAction->isChecked());
233 } else if (action == openInNewTabAction) {
234 const KUrl url = m_model->item(index)->dataValue("url").value<KUrl>();
235 emit placeMiddleClicked(url);
236 } else if (action == showAllAction) {
237 m_model->setHiddenItemsShown(showAllAction->isChecked());
238 } else if (action == teardownAction) {
239 m_model->requestTeardown(index);
240 } else if (action == ejectAction) {
241 m_model->requestEject(index);
242 }
243 }
244
245 selectClosestItem();
246 }
247
248 void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos)
249 {
250 KMenu menu(this);
251
252 QAction* addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry..."));
253
254 QAction* showAllAction = 0;
255 if (m_model->hiddenCount() > 0) {
256 showAllAction = menu.addAction(i18nc("@item:inmenu", "Show All Entries"));
257 showAllAction->setCheckable(true);
258 showAllAction->setChecked(m_model->hiddenItemsShown());
259 }
260
261 menu.addSeparator();
262 foreach (QAction* action, customContextMenuActions()) {
263 menu.addAction(action);
264 }
265
266 QAction* action = menu.exec(pos.toPoint());
267 if (action) {
268 if (action == addAction) {
269 addEntry();
270 } else if (action == showAllAction) {
271 m_model->setHiddenItemsShown(showAllAction->isChecked());
272 }
273 }
274
275 selectClosestItem();
276 }
277
278 void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
279 {
280 if (index < 0) {
281 return;
282 }
283
284 KUrl destUrl = m_model->placesItem(index)->url();
285 QDropEvent dropEvent(event->pos().toPoint(),
286 event->possibleActions(),
287 event->mimeData(),
288 event->buttons(),
289 event->modifiers());
290
291 DragAndDropHelper::dropUrls(KFileItem(), destUrl, &dropEvent);
292 }
293
294 void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
295 {
296 m_model->dropMimeDataBefore(index, event->mimeData());
297 }
298
299 void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent)
300 {
301 Q_UNUSED(parent);
302 const QString error = DragAndDropHelper::dropUrls(KFileItem(), dest, event);
303 if (!error.isEmpty()) {
304 emit errorMessage(error);
305 }
306
307 }
308
309 void PlacesPanel::slotTrashUpdated(KJob* job)
310 {
311 if (job->error()) {
312 emit errorMessage(job->errorString());
313 }
314 org::kde::KDirNotify::emitFilesAdded("trash:/");
315 }
316
317 void PlacesPanel::slotStorageSetupDone(int index, bool success)
318 {
319 disconnect(m_model, SIGNAL(storageSetupDone(int,bool)),
320 this, SLOT(slotStorageSetupDone(int,bool)));
321
322 if (m_triggerStorageSetupButton == Qt::NoButton) {
323 return;
324 }
325
326 if (success) {
327 Q_ASSERT(!m_model->storageSetupNeeded(index));
328 triggerItem(index, m_triggerStorageSetupButton);
329 m_triggerStorageSetupButton = Qt::NoButton;
330 } else {
331 setUrl(m_storageSetupFailedUrl);
332 m_storageSetupFailedUrl = KUrl();
333 }
334 }
335
336 void PlacesPanel::emptyTrash()
337 {
338 const QString text = i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.");
339 const bool del = KMessageBox::warningContinueCancel(window(),
340 text,
341 QString(),
342 KGuiItem(i18nc("@action:button", "Empty Trash"),
343 KIcon("user-trash"))
344 ) == KMessageBox::Continue;
345 if (del) {
346 QByteArray packedArgs;
347 QDataStream stream(&packedArgs, QIODevice::WriteOnly);
348 stream << int(1);
349 KIO::Job *job = KIO::special(KUrl("trash:/"), packedArgs);
350 KNotification::event("Trash: emptied", QString() , QPixmap() , 0, KNotification::DefaultEvent);
351 job->ui()->setWindow(parentWidget());
352 connect(job, SIGNAL(result(KJob*)), SLOT(slotTrashUpdated(KJob*)));
353 }
354 }
355
356 void PlacesPanel::addEntry()
357 {
358 const int index = m_controller->selectionManager()->currentItem();
359 const KUrl url = m_model->data(index).value("url").value<KUrl>();
360
361 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
362 dialog->setCaption(i18nc("@title:window", "Add Places Entry"));
363 dialog->setAllowGlobal(true);
364 dialog->setUrl(url);
365 if (dialog->exec() == QDialog::Accepted) {
366 PlacesItem* item = m_model->createPlacesItem(dialog->text(), dialog->url(), dialog->icon());
367 m_model->appendItemToGroup(item);
368 }
369
370 delete dialog;
371 }
372
373 void PlacesPanel::editEntry(int index)
374 {
375 QHash<QByteArray, QVariant> data = m_model->data(index);
376
377 QPointer<PlacesItemEditDialog> dialog = new PlacesItemEditDialog(this);
378 dialog->setCaption(i18nc("@title:window", "Edit Places Entry"));
379 dialog->setIcon(data.value("iconName").toString());
380 dialog->setText(data.value("text").toString());
381 dialog->setUrl(data.value("url").value<KUrl>());
382 dialog->setAllowGlobal(true);
383 if (dialog->exec() == QDialog::Accepted) {
384 PlacesItem* oldItem = m_model->placesItem(index);
385 if (oldItem) {
386 oldItem->setText(dialog->text());
387 oldItem->setUrl(dialog->url());
388 oldItem->setIcon(dialog->icon());
389 }
390 }
391
392 delete dialog;
393 }
394
395 void PlacesPanel::selectClosestItem()
396 {
397 const int index = m_model->closestItem(url());
398 KItemListSelectionManager* selectionManager = m_controller->selectionManager();
399 selectionManager->setCurrentItem(index);
400 selectionManager->clearSelection();
401 selectionManager->setSelected(index);
402 }
403
404 void PlacesPanel::triggerItem(int index, Qt::MouseButton button)
405 {
406 const PlacesItem* item = m_model->placesItem(index);
407 if (!item) {
408 return;
409 }
410
411 if (m_model->storageSetupNeeded(index)) {
412 m_triggerStorageSetupButton = button;
413 m_storageSetupFailedUrl = url();
414
415 connect(m_model, SIGNAL(storageSetupDone(int,bool)),
416 this, SLOT(slotStorageSetupDone(int,bool)));
417
418 m_model->requestStorageSetup(index);
419 } else {
420 m_triggerStorageSetupButton = Qt::NoButton;
421
422 const KUrl url = m_model->data(index).value("url").value<KUrl>();
423 if (!url.isEmpty()) {
424 if (button == Qt::MiddleButton) {
425 emit placeMiddleClicked(PlacesItemModel::convertedUrl(url));
426 } else {
427 emit placeActivated(PlacesItemModel::convertedUrl(url));
428 }
429 }
430 }
431 }
432
433
434 #include "placespanel.moc"