]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Ask before opening more than 5 items.
[dolphin.git] / src / views / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <config-nepomuk.h>
24
25 #include <QAbstractItemView>
26 #include <QApplication>
27 #include <QBoxLayout>
28 #include <QClipboard>
29 #include <QDropEvent>
30 #include <QGraphicsSceneDragDropEvent>
31 #include <QKeyEvent>
32 #include <QItemSelection>
33 #include <QTimer>
34 #include <QScrollBar>
35
36 #include <KActionCollection>
37 #include <KColorScheme>
38 #include <KDirModel>
39 #include <KIconEffect>
40 #include <KFileItem>
41 #include <KFileItemListProperties>
42 #include <KLocale>
43 #include <kitemviews/kfileitemmodel.h>
44 #include <kitemviews/kfileitemlistview.h>
45 #include <kitemviews/kitemlistcontainer.h>
46 #include <kitemviews/kitemlistheader.h>
47 #include <kitemviews/kitemlistselectionmanager.h>
48 #include <kitemviews/kitemlistview.h>
49 #include <kitemviews/kitemlistcontroller.h>
50 #include <KIO/DeleteJob>
51 #include <KIO/JobUiDelegate>
52 #include <KIO/NetAccess>
53 #include <KIO/PreviewJob>
54 #include <KJob>
55 #include <KMenu>
56 #include <KMessageBox>
57 #include <konq_fileitemcapabilities.h>
58 #include <konq_operations.h>
59 #include <konqmimedata.h>
60 #include <KToggleAction>
61 #include <KUrl>
62
63 #include "dolphinnewfilemenuobserver.h"
64 #include "dolphin_detailsmodesettings.h"
65 #include "dolphin_generalsettings.h"
66 #include "dolphinitemlistview.h"
67 #include "draganddrophelper.h"
68 #include "renamedialog.h"
69 #include "versioncontrol/versioncontrolobserver.h"
70 #include "viewmodecontroller.h"
71 #include "viewproperties.h"
72 #include "views/tooltips/tooltipmanager.h"
73 #include "zoomlevelinfo.h"
74
75 #ifdef HAVE_NEPOMUK
76 #include <Nepomuk/ResourceManager>
77 #endif
78
79 namespace {
80 const int MaxModeEnum = DolphinView::CompactView;
81 };
82
83 DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
84 QWidget(parent),
85 m_active(true),
86 m_tabsForFiles(false),
87 m_assureVisibleCurrentIndex(false),
88 m_isFolderWritable(true),
89 m_dragging(false),
90 m_url(url),
91 m_viewPropertiesContext(),
92 m_mode(DolphinView::IconsView),
93 m_visibleRoles(),
94 m_topLayout(0),
95 m_model(0),
96 m_view(0),
97 m_container(0),
98 m_toolTipManager(0),
99 m_selectionChangedTimer(0),
100 m_currentItemUrl(),
101 m_restoredContentsPosition(),
102 m_createdItemUrl(),
103 m_selectedUrls(),
104 m_versionControlObserver(0)
105 {
106 m_topLayout = new QVBoxLayout(this);
107 m_topLayout->setSpacing(0);
108 m_topLayout->setMargin(0);
109
110 // When a new item has been created by the "Create New..." menu, the item should
111 // get selected and it must be assured that the item will get visible. As the
112 // creation is done asynchronously, several signals must be checked:
113 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl)),
114 this, SLOT(observeCreatedItem(KUrl)));
115
116 m_selectionChangedTimer = new QTimer(this);
117 m_selectionChangedTimer->setSingleShot(true);
118 m_selectionChangedTimer->setInterval(300);
119 connect(m_selectionChangedTimer, SIGNAL(timeout()),
120 this, SLOT(emitSelectionChangedSignal()));
121
122 m_model = new KFileItemModel(this);
123 m_view = new DolphinItemListView();
124 m_view->setEnabledSelectionToggles(GeneralSettings::showSelectionToggle());
125 m_view->setEnlargeSmallPreviews(GeneralSettings::enlargeSmallPreviews());
126 m_view->setVisibleRoles(QList<QByteArray>() << "text");
127 applyModeToView();
128
129 KItemListController* controller = new KItemListController(m_model, m_view, this);
130 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
131 controller->setAutoActivationDelay(delay);
132
133 m_container = new KItemListContainer(controller, this);
134 m_container->installEventFilter(this);
135 setFocusProxy(m_container);
136 connect(m_container->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
137 connect(m_container->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
138
139 controller->setSelectionBehavior(KItemListController::MultiSelection);
140 connect(controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
141 connect(controller, SIGNAL(itemsActivated(QSet<int>)), this, SLOT(slotItemsActivated(QSet<int>)));
142 connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
143 connect(controller, SIGNAL(itemContextMenuRequested(int,QPointF)), this, SLOT(slotItemContextMenuRequested(int,QPointF)));
144 connect(controller, SIGNAL(viewContextMenuRequested(QPointF)), this, SLOT(slotViewContextMenuRequested(QPointF)));
145 connect(controller, SIGNAL(headerContextMenuRequested(QPointF)), this, SLOT(slotHeaderContextMenuRequested(QPointF)));
146 connect(controller, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons)));
147 connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
148 connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
149 connect(controller, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent*)));
150 connect(controller, SIGNAL(modelChanged(KItemModelBase*,KItemModelBase*)), this, SLOT(slotModelChanged(KItemModelBase*,KItemModelBase*)));
151
152 connect(m_model, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
153 connect(m_model, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
154 connect(m_model, SIGNAL(directoryLoadingProgress(int)), this, SIGNAL(directoryLoadingProgress(int)));
155 connect(m_model, SIGNAL(directorySortingProgress(int)), this, SIGNAL(directorySortingProgress(int)));
156 connect(m_model, SIGNAL(itemsChanged(KItemRangeList,QSet<QByteArray>)),
157 this, SLOT(slotItemsChanged()));
158 connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)), this, SIGNAL(itemCountChanged()));
159 connect(m_model, SIGNAL(itemsInserted(KItemRangeList)), this, SIGNAL(itemCountChanged()));
160 connect(m_model, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
161 connect(m_model, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
162 connect(m_model, SIGNAL(directoryRedirection(KUrl,KUrl)), this, SLOT(slotDirectoryRedirection(KUrl,KUrl)));
163 connect(m_model, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl)));
164
165 m_view->installEventFilter(this);
166 connect(m_view, SIGNAL(sortOrderChanged(Qt::SortOrder,Qt::SortOrder)),
167 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder,Qt::SortOrder)));
168 connect(m_view, SIGNAL(sortRoleChanged(QByteArray,QByteArray)),
169 this, SLOT(slotSortRoleChangedByHeader(QByteArray,QByteArray)));
170 connect(m_view, SIGNAL(visibleRolesChanged(QList<QByteArray>,QList<QByteArray>)),
171 this, SLOT(slotVisibleRolesChangedByHeader(QList<QByteArray>,QList<QByteArray>)));
172 connect(m_view, SIGNAL(roleEditingFinished(int,QByteArray,QVariant)),
173 this, SLOT(slotRoleEditingFinished(int,QByteArray,QVariant)));
174 connect(m_view->header(), SIGNAL(columnWidthChanged(QByteArray,qreal,qreal)),
175 this, SLOT(slotHeaderColumnWidthChanged(QByteArray,qreal,qreal)));
176
177 KItemListSelectionManager* selectionManager = controller->selectionManager();
178 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
179 this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
180
181 m_toolTipManager = new ToolTipManager(this);
182
183 m_versionControlObserver = new VersionControlObserver(this);
184 m_versionControlObserver->setModel(m_model);
185 connect(m_versionControlObserver, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
186 connect(m_versionControlObserver, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
187 connect(m_versionControlObserver, SIGNAL(operationCompletedMessage(QString)), this, SIGNAL(operationCompletedMessage(QString)));
188
189 applyViewProperties();
190 m_topLayout->addWidget(m_container);
191
192 loadDirectory(url);
193 }
194
195 DolphinView::~DolphinView()
196 {
197 }
198
199 KUrl DolphinView::url() const
200 {
201 return m_url;
202 }
203
204 void DolphinView::setActive(bool active)
205 {
206 if (active == m_active) {
207 return;
208 }
209
210 m_active = active;
211
212 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
213 if (!active) {
214 color.setAlpha(150);
215 }
216
217 QWidget* viewport = m_container->viewport();
218 if (viewport) {
219 QPalette palette;
220 palette.setColor(viewport->backgroundRole(), color);
221 viewport->setPalette(palette);
222 }
223
224 update();
225
226 if (active) {
227 m_container->setFocus();
228 emit activated();
229 emit writeStateChanged(m_isFolderWritable);
230 }
231 }
232
233 bool DolphinView::isActive() const
234 {
235 return m_active;
236 }
237
238 void DolphinView::setMode(Mode mode)
239 {
240 if (mode != m_mode) {
241 ViewProperties props(viewPropertiesUrl());
242 props.setViewMode(mode);
243 props.save();
244
245 applyViewProperties();
246 }
247 }
248
249 DolphinView::Mode DolphinView::mode() const
250 {
251 return m_mode;
252 }
253
254 void DolphinView::setPreviewsShown(bool show)
255 {
256 if (previewsShown() == show) {
257 return;
258 }
259
260 ViewProperties props(viewPropertiesUrl());
261 props.setPreviewsShown(show);
262
263 const int oldZoomLevel = m_view->zoomLevel();
264 m_view->setPreviewsShown(show);
265 emit previewsShownChanged(show);
266
267 const int newZoomLevel = m_view->zoomLevel();
268 if (newZoomLevel != oldZoomLevel) {
269 emit zoomLevelChanged(newZoomLevel, oldZoomLevel);
270 }
271 }
272
273 bool DolphinView::previewsShown() const
274 {
275 return m_view->previewsShown();
276 }
277
278 void DolphinView::setHiddenFilesShown(bool show)
279 {
280 if (m_model->showHiddenFiles() == show) {
281 return;
282 }
283
284 const KFileItemList itemList = selectedItems();
285 m_selectedUrls.clear();
286 m_selectedUrls = itemList.urlList();
287
288 ViewProperties props(viewPropertiesUrl());
289 props.setHiddenFilesShown(show);
290
291 m_model->setShowHiddenFiles(show);
292 emit hiddenFilesShownChanged(show);
293 }
294
295 bool DolphinView::hiddenFilesShown() const
296 {
297 return m_model->showHiddenFiles();
298 }
299
300 void DolphinView::setGroupedSorting(bool grouped)
301 {
302 if (grouped == groupedSorting()) {
303 return;
304 }
305
306 ViewProperties props(viewPropertiesUrl());
307 props.setGroupedSorting(grouped);
308 props.save();
309
310 m_container->controller()->model()->setGroupedSorting(grouped);
311
312 emit groupedSortingChanged(grouped);
313 }
314
315 bool DolphinView::groupedSorting() const
316 {
317 return m_model->groupedSorting();
318 }
319
320 KFileItemList DolphinView::items() const
321 {
322 KFileItemList list;
323 const int itemCount = m_model->count();
324 list.reserve(itemCount);
325
326 for (int i = 0; i < itemCount; ++i) {
327 list.append(m_model->fileItem(i));
328 }
329
330 return list;
331 }
332
333 int DolphinView::itemsCount() const
334 {
335 return m_model->count();
336 }
337
338 KFileItemList DolphinView::selectedItems() const
339 {
340 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
341 const QSet<int> selectedIndexes = selectionManager->selectedItems();
342
343 KFileItemList selectedItems;
344 QSetIterator<int> it(selectedIndexes);
345 while (it.hasNext()) {
346 const int index = it.next();
347 selectedItems.append(m_model->fileItem(index));
348 }
349 return selectedItems;
350 }
351
352 int DolphinView::selectedItemsCount() const
353 {
354 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
355 return selectionManager->selectedItems().count();
356 }
357
358 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
359 {
360 m_selectedUrls = urls;
361 }
362
363 void DolphinView::markUrlAsCurrent(const KUrl& url)
364 {
365 m_currentItemUrl = url;
366 }
367
368 void DolphinView::selectItems(const QRegExp& pattern, bool enabled)
369 {
370 const KItemListSelectionManager::SelectionMode mode = enabled
371 ? KItemListSelectionManager::Select
372 : KItemListSelectionManager::Deselect;
373 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
374
375 for (int index = 0; index < m_model->count(); index++) {
376 const KFileItem item = m_model->fileItem(index);
377 if (pattern.exactMatch(item.text())) {
378 // An alternative approach would be to store the matching items in a QSet<int> and
379 // select them in one go after the loop, but we'd need a new function
380 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
381 // for that.
382 selectionManager->setSelected(index, 1, mode);
383 }
384 }
385 }
386
387 void DolphinView::setZoomLevel(int level)
388 {
389 const int oldZoomLevel = zoomLevel();
390 m_view->setZoomLevel(level);
391 if (zoomLevel() != oldZoomLevel) {
392 hideToolTip();
393 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
394 }
395 }
396
397 int DolphinView::zoomLevel() const
398 {
399 return m_view->zoomLevel();
400 }
401
402 void DolphinView::setSortRole(const QByteArray& role)
403 {
404 if (role != sortRole()) {
405 updateSortRole(role);
406 }
407 }
408
409 QByteArray DolphinView::sortRole() const
410 {
411 const KItemModelBase* model = m_container->controller()->model();
412 return model->sortRole();
413 }
414
415 void DolphinView::setSortOrder(Qt::SortOrder order)
416 {
417 if (sortOrder() != order) {
418 updateSortOrder(order);
419 }
420 }
421
422 Qt::SortOrder DolphinView::sortOrder() const
423 {
424 return m_model->sortOrder();
425 }
426
427 void DolphinView::setSortFoldersFirst(bool foldersFirst)
428 {
429 if (sortFoldersFirst() != foldersFirst) {
430 updateSortFoldersFirst(foldersFirst);
431 }
432 }
433
434 bool DolphinView::sortFoldersFirst() const
435 {
436 return m_model->sortDirectoriesFirst();
437 }
438
439 void DolphinView::setVisibleRoles(const QList<QByteArray>& roles)
440 {
441 const QList<QByteArray> previousRoles = roles;
442
443 ViewProperties props(viewPropertiesUrl());
444 props.setVisibleRoles(roles);
445
446 m_visibleRoles = roles;
447 m_view->setVisibleRoles(roles);
448
449 emit visibleRolesChanged(m_visibleRoles, previousRoles);
450 }
451
452 QList<QByteArray> DolphinView::visibleRoles() const
453 {
454 return m_visibleRoles;
455 }
456
457 void DolphinView::reload()
458 {
459 QByteArray viewState;
460 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
461 saveState(saveStream);
462
463 const KFileItemList itemList = selectedItems();
464 m_selectedUrls.clear();
465 m_selectedUrls = itemList.urlList();
466
467 setUrl(url());
468 loadDirectory(url(), true);
469
470 QDataStream restoreStream(viewState);
471 restoreState(restoreStream);
472 }
473
474 void DolphinView::stopLoading()
475 {
476 m_model->cancelDirectoryLoading();
477 }
478
479 void DolphinView::readSettings()
480 {
481 const int oldZoomLevel = m_view->zoomLevel();
482
483 GeneralSettings::self()->readConfig();
484 m_view->readSettings();
485 applyViewProperties();
486
487 const int delay = GeneralSettings::autoExpandFolders() ? 750 : -1;
488 m_container->controller()->setAutoActivationDelay(delay);
489
490 const int newZoomLevel = m_view->zoomLevel();
491 if (newZoomLevel != oldZoomLevel) {
492 emit zoomLevelChanged(newZoomLevel, oldZoomLevel);
493 }
494 }
495
496 void DolphinView::writeSettings()
497 {
498 GeneralSettings::self()->writeConfig();
499 m_view->writeSettings();
500 }
501
502 void DolphinView::setNameFilter(const QString& nameFilter)
503 {
504 m_model->setNameFilter(nameFilter);
505 }
506
507 QString DolphinView::nameFilter() const
508 {
509 return m_model->nameFilter();
510 }
511
512 QString DolphinView::statusBarText() const
513 {
514 QString summary;
515 QString foldersText;
516 QString filesText;
517
518 int folderCount = 0;
519 int fileCount = 0;
520 KIO::filesize_t totalFileSize = 0;
521
522 if (m_container->controller()->selectionManager()->hasSelection()) {
523 // Give a summary of the status of the selected files
524 const KFileItemList list = selectedItems();
525 foreach (const KFileItem& item, list) {
526 if (item.isDir()) {
527 ++folderCount;
528 } else {
529 ++fileCount;
530 totalFileSize += item.size();
531 }
532 }
533
534 if (folderCount + fileCount == 1) {
535 // If only one item is selected, show the filename
536 filesText = i18nc("@info:status", "<filename>%1</filename> selected", list.first().text());
537 } else {
538 // At least 2 items are selected
539 foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
540 filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
541 }
542 } else {
543 calculateItemCount(fileCount, folderCount, totalFileSize);
544 foldersText = i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount);
545 filesText = i18ncp("@info:status", "1 File", "%1 Files", fileCount);
546 }
547
548 if (fileCount > 0 && folderCount > 0) {
549 summary = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
550 foldersText, filesText,
551 KGlobal::locale()->formatByteSize(totalFileSize));
552 } else if (fileCount > 0) {
553 summary = i18nc("@info:status files (size)", "%1 (%2)",
554 filesText,
555 KGlobal::locale()->formatByteSize(totalFileSize));
556 } else if (folderCount > 0) {
557 summary = foldersText;
558 } else {
559 summary = i18nc("@info:status", "0 Folders, 0 Files");
560 }
561
562 return summary;
563 }
564
565 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
566 {
567 QList<QAction*> actions;
568
569 if (items.isEmpty()) {
570 const KFileItem item = m_model->rootItem();
571 if (!item.isNull()) {
572 actions = m_versionControlObserver->actions(KFileItemList() << item);
573 }
574 } else {
575 actions = m_versionControlObserver->actions(items);
576 }
577
578 return actions;
579 }
580
581 void DolphinView::setUrl(const KUrl& url)
582 {
583 if (url == m_url) {
584 return;
585 }
586
587 emit urlAboutToBeChanged(url);
588 m_url = url;
589
590 hideToolTip();
591
592 // It is important to clear the items from the model before
593 // applying the view properties, otherwise expensive operations
594 // might be done on the existing items although they get cleared
595 // anyhow afterwards by loadDirectory().
596 m_model->clear();
597 applyViewProperties();
598 loadDirectory(url);
599
600 emit urlChanged(url);
601 }
602
603 void DolphinView::selectAll()
604 {
605 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
606 selectionManager->setSelected(0, m_model->count());
607 }
608
609 void DolphinView::invertSelection()
610 {
611 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
612 selectionManager->setSelected(0, m_model->count(), KItemListSelectionManager::Toggle);
613 }
614
615 void DolphinView::clearSelection()
616 {
617 m_container->controller()->selectionManager()->clearSelection();
618 }
619
620 void DolphinView::renameSelectedItems()
621 {
622 const KFileItemList items = selectedItems();
623 if (items.isEmpty()) {
624 return;
625 }
626
627 if (items.count() == 1) {
628 const int index = m_model->index(items.first());
629 m_view->editRole(index, "text");
630 } else {
631 RenameDialog* dialog = new RenameDialog(this, items);
632 dialog->setAttribute(Qt::WA_DeleteOnClose);
633 dialog->show();
634 dialog->raise();
635 dialog->activateWindow();
636 }
637
638 // Assure that the current index remains visible when KFileItemModel
639 // will notify the view about changed items (which might result in
640 // a changed sorting).
641 m_assureVisibleCurrentIndex = true;
642 }
643
644 void DolphinView::trashSelectedItems()
645 {
646 const KUrl::List list = simplifiedSelectedUrls();
647 KonqOperations::del(this, KonqOperations::TRASH, list);
648 }
649
650 void DolphinView::deleteSelectedItems()
651 {
652 const KUrl::List list = simplifiedSelectedUrls();
653 const bool del = KonqOperations::askDeleteConfirmation(list,
654 KonqOperations::DEL,
655 KonqOperations::DEFAULT_CONFIRMATION,
656 this);
657
658 if (del) {
659 KIO::Job* job = KIO::del(list);
660 if (job->ui()) {
661 job->ui()->setWindow(this);
662 }
663 connect(job, SIGNAL(result(KJob*)),
664 this, SLOT(slotDeleteFileFinished(KJob*)));
665 }
666 }
667
668 void DolphinView::cutSelectedItems()
669 {
670 QMimeData* mimeData = selectionMimeData();
671 KonqMimeData::addIsCutSelection(mimeData, true);
672 QApplication::clipboard()->setMimeData(mimeData);
673 }
674
675 void DolphinView::copySelectedItems()
676 {
677 QMimeData* mimeData = selectionMimeData();
678 QApplication::clipboard()->setMimeData(mimeData);
679 }
680
681 void DolphinView::paste()
682 {
683 pasteToUrl(url());
684 }
685
686 void DolphinView::pasteIntoFolder()
687 {
688 const KFileItemList items = selectedItems();
689 if ((items.count() == 1) && items.first().isDir()) {
690 pasteToUrl(items.first().url());
691 }
692 }
693
694 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
695 {
696 switch (event->type()) {
697 case QEvent::FocusIn:
698 if (watched == m_container) {
699 setActive(true);
700 }
701 break;
702
703 case QEvent::GraphicsSceneDragEnter:
704 if (watched == m_view) {
705 m_dragging = true;
706 }
707 break;
708
709 case QEvent::GraphicsSceneDragLeave:
710 if (watched == m_view) {
711 m_dragging = false;
712 }
713 break;
714
715 case QEvent::GraphicsSceneDrop:
716 if (watched == m_view) {
717 m_dragging = false;
718 }
719 default:
720 break;
721 }
722
723 return QWidget::eventFilter(watched, event);
724 }
725
726 void DolphinView::wheelEvent(QWheelEvent* event)
727 {
728 if (event->modifiers().testFlag(Qt::ControlModifier)) {
729 const int numDegrees = event->delta() / 8;
730 const int numSteps = numDegrees / 15;
731
732 setZoomLevel(zoomLevel() + numSteps);
733 event->accept();
734 } else {
735 event->ignore();
736 }
737 }
738
739 void DolphinView::hideEvent(QHideEvent* event)
740 {
741 hideToolTip();
742 QWidget::hideEvent(event);
743 }
744
745 void DolphinView::activate()
746 {
747 setActive(true);
748 }
749
750 void DolphinView::slotItemActivated(int index)
751 {
752 const KFileItem item = m_model->fileItem(index);
753 if (!item.isNull()) {
754 emit itemActivated(item);
755 }
756 }
757
758 void DolphinView::slotItemsActivated(const QSet<int>& indexes)
759 {
760 Q_ASSERT(indexes.count() >= 2);
761
762 KFileItemList items;
763
764 QSetIterator<int> it(indexes);
765 while (it.hasNext()) {
766 const int index = it.next();
767 items.append(m_model->fileItem(index));
768 }
769
770 if (items.count() > 5) {
771 QString question = QString("Are you sure you want to open %1 items?").arg(items.count());
772 const int answer = KMessageBox::warningYesNo(this, question);
773 if (answer != KMessageBox::Yes) {
774 return;
775 }
776 }
777
778 foreach (const KFileItem& item, items) {
779 if (item.isDir()) {
780 emit tabRequested(item.url());
781 } else {
782 emit itemActivated(item);
783 }
784 }
785 }
786
787 void DolphinView::slotItemMiddleClicked(int index)
788 {
789 const KFileItem item = m_model->fileItem(index);
790 if (item.isDir() || isTabsForFilesEnabled()) {
791 emit tabRequested(item.url());
792 }
793 }
794
795 void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos)
796 {
797 const KFileItem item = m_model->fileItem(index);
798 emit requestContextMenu(pos.toPoint(), item, url(), QList<QAction*>());
799 }
800
801 void DolphinView::slotViewContextMenuRequested(const QPointF& pos)
802 {
803 emit requestContextMenu(pos.toPoint(), KFileItem(), url(), QList<QAction*>());
804 }
805
806 void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
807 {
808 ViewProperties props(viewPropertiesUrl());
809
810 QPointer<KMenu> menu = new KMenu(QApplication::activeWindow());
811
812 KItemListView* view = m_container->controller()->view();
813 const QSet<QByteArray> visibleRolesSet = view->visibleRoles().toSet();
814
815 bool nepomukRunning = false;
816 bool indexingEnabled = false;
817 #ifdef HAVE_NEPOMUK
818 nepomukRunning = (Nepomuk::ResourceManager::instance()->initialized());
819 if (nepomukRunning) {
820 KConfig config("nepomukserverrc");
821 indexingEnabled = config.group("Service-nepomukfileindexer").readEntry("autostart", false);
822 }
823 #endif
824
825 QString groupName;
826 QMenu* groupMenu = 0;
827
828 // Add all roles to the menu that can be shown or hidden by the user
829 const QList<KFileItemModel::RoleInfo> rolesInfo = KFileItemModel::rolesInformation();
830 foreach (const KFileItemModel::RoleInfo& info, rolesInfo) {
831 if (info.role == "text") {
832 // It should not be possible to hide the "text" role
833 continue;
834 }
835
836 const QString text = m_model->roleDescription(info.role);
837 QAction* action = 0;
838 if (info.group.isEmpty()) {
839 action = menu->addAction(text);
840 } else {
841 if (!groupMenu || info.group != groupName) {
842 groupName = info.group;
843 groupMenu = menu->addMenu(groupName);
844 }
845
846 action = groupMenu->addAction(text);
847 }
848
849 action->setCheckable(true);
850 action->setChecked(visibleRolesSet.contains(info.role));
851 action->setData(info.role);
852
853 const bool enable = (!info.requiresNepomuk && !info.requiresIndexer) ||
854 (info.requiresNepomuk && nepomukRunning) ||
855 (info.requiresIndexer && indexingEnabled);
856 action->setEnabled(enable);
857 }
858
859 menu->addSeparator();
860
861 QActionGroup* widthsGroup = new QActionGroup(menu);
862 const bool autoColumnWidths = props.headerColumnWidths().isEmpty();
863
864 QAction* autoAdjustWidthsAction = menu->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
865 autoAdjustWidthsAction->setCheckable(true);
866 autoAdjustWidthsAction->setChecked(autoColumnWidths);
867 autoAdjustWidthsAction->setActionGroup(widthsGroup);
868
869 QAction* customWidthsAction = menu->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
870 customWidthsAction->setCheckable(true);
871 customWidthsAction->setChecked(!autoColumnWidths);
872 customWidthsAction->setActionGroup(widthsGroup);
873
874 QAction* action = menu->exec(pos.toPoint());
875 if (menu && action) {
876 KItemListHeader* header = view->header();
877
878 if (action == autoAdjustWidthsAction) {
879 // Clear the column-widths from the viewproperties and turn on
880 // the automatic resizing of the columns
881 props.setHeaderColumnWidths(QList<int>());
882 header->setAutomaticColumnResizing(true);
883 } else if (action == customWidthsAction) {
884 // Apply the current column-widths as custom column-widths and turn
885 // off the automatic resizing of the columns
886 QList<int> columnWidths;
887 foreach (const QByteArray& role, view->visibleRoles()) {
888 columnWidths.append(header->columnWidth(role));
889 }
890 props.setHeaderColumnWidths(columnWidths);
891 header->setAutomaticColumnResizing(false);
892 } else {
893 // Show or hide the selected role
894 const QByteArray selectedRole = action->data().toByteArray();
895
896 QList<QByteArray> visibleRoles = view->visibleRoles();
897 if (action->isChecked()) {
898 visibleRoles.append(selectedRole);
899 } else {
900 visibleRoles.removeOne(selectedRole);
901 }
902
903 view->setVisibleRoles(visibleRoles);
904 props.setVisibleRoles(visibleRoles);
905
906 QList<int> columnWidths;
907 if (!header->automaticColumnResizing()) {
908 foreach (const QByteArray& role, view->visibleRoles()) {
909 columnWidths.append(header->columnWidth(role));
910 }
911 }
912 props.setHeaderColumnWidths(columnWidths);
913 }
914 }
915
916 delete menu;
917 }
918
919 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous)
920 {
921 Q_UNUSED(previous);
922
923 const QList<QByteArray> visibleRoles = m_view->visibleRoles();
924
925 ViewProperties props(viewPropertiesUrl());
926 QList<int> columnWidths = props.headerColumnWidths();
927 if (columnWidths.count() != visibleRoles.count()) {
928 columnWidths.clear();
929 columnWidths.reserve(visibleRoles.count());
930 const KItemListHeader* header = m_view->header();
931 foreach (const QByteArray& role, visibleRoles) {
932 const int width = header->columnWidth(role);
933 columnWidths.append(width);
934 }
935 }
936
937 const int roleIndex = visibleRoles.indexOf(role);
938 Q_ASSERT(roleIndex >= 0 && roleIndex < columnWidths.count());
939 columnWidths[roleIndex] = current;
940
941 props.setHeaderColumnWidths(columnWidths);
942 }
943
944 void DolphinView::slotItemHovered(int index)
945 {
946 const KFileItem item = m_model->fileItem(index);
947
948 if (GeneralSettings::showToolTips() && !m_dragging) {
949 QRectF itemRect = m_container->controller()->view()->itemContextRect(index);
950 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
951 itemRect.moveTo(pos);
952
953 m_toolTipManager->showToolTip(item, itemRect);
954 }
955
956 emit requestItemInfo(item);
957 }
958
959 void DolphinView::slotItemUnhovered(int index)
960 {
961 Q_UNUSED(index);
962 hideToolTip();
963 emit requestItemInfo(KFileItem());
964 }
965
966 void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event)
967 {
968 KUrl destUrl;
969 KFileItem destItem = m_model->fileItem(index);
970 if (destItem.isNull() || (!destItem.isDir() && !destItem.isDesktopFile())) {
971 // Use the URL of the view as drop target if the item is no directory
972 // or desktop-file
973 destItem = m_model->rootItem();
974 destUrl = url();
975 } else {
976 // The item represents a directory or desktop-file
977 destUrl = destItem.url();
978 }
979
980 QDropEvent dropEvent(event->pos().toPoint(),
981 event->possibleActions(),
982 event->mimeData(),
983 event->buttons(),
984 event->modifiers());
985
986 const QString error = DragAndDropHelper::dropUrls(destItem, destUrl, &dropEvent);
987 if (!error.isEmpty()) {
988 emit errorMessage(error);
989 }
990
991 if (destUrl == url()) {
992 // Mark the dropped urls as selected.
993 markPastedUrlsAsSelected(event->mimeData());
994 }
995 }
996
997 void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous)
998 {
999 if (previous != 0) {
1000 disconnect(previous, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
1001 m_versionControlObserver->setModel(0);
1002 }
1003
1004 if (current) {
1005 Q_ASSERT(qobject_cast<KFileItemModel*>(current));
1006 connect(current, SIGNAL(loadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
1007
1008 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(current);
1009 m_versionControlObserver->setModel(fileItemModel);
1010 }
1011 }
1012
1013 void DolphinView::slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons)
1014 {
1015 hideToolTip();
1016
1017 if (itemIndex < 0) {
1018 // Trigger the history navigation only when clicking on the viewport:
1019 // Above an item the XButtons provide a simple way to select items in
1020 // the singleClick mode.
1021 if (buttons & Qt::XButton1) {
1022 emit goBackRequested();
1023 } else if (buttons & Qt::XButton2) {
1024 emit goForwardRequested();
1025 }
1026 }
1027 }
1028
1029 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
1030 {
1031 const int currentCount = current.count();
1032 const int previousCount = previous.count();
1033 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
1034 (currentCount > 0 && previousCount == 0);
1035
1036 // If nothing has been selected before and something got selected (or if something
1037 // was selected before and now nothing is selected) the selectionChangedSignal must
1038 // be emitted asynchronously as fast as possible to update the edit-actions.
1039 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
1040 m_selectionChangedTimer->start();
1041 }
1042
1043 void DolphinView::emitSelectionChangedSignal()
1044 {
1045 m_selectionChangedTimer->stop();
1046 emit selectionChanged(selectedItems());
1047 }
1048
1049 void DolphinView::updateSortRole(const QByteArray& role)
1050 {
1051 ViewProperties props(viewPropertiesUrl());
1052 props.setSortRole(role);
1053
1054 KItemModelBase* model = m_container->controller()->model();
1055 model->setSortRole(role);
1056
1057 emit sortRoleChanged(role);
1058 }
1059
1060 void DolphinView::updateSortOrder(Qt::SortOrder order)
1061 {
1062 ViewProperties props(viewPropertiesUrl());
1063 props.setSortOrder(order);
1064
1065 m_model->setSortOrder(order);
1066
1067 emit sortOrderChanged(order);
1068 }
1069
1070 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
1071 {
1072 ViewProperties props(viewPropertiesUrl());
1073 props.setSortFoldersFirst(foldersFirst);
1074
1075 m_model->setSortDirectoriesFirst(foldersFirst);
1076
1077 emit sortFoldersFirstChanged(foldersFirst);
1078 }
1079
1080 QPair<bool, QString> DolphinView::pasteInfo() const
1081 {
1082 return KonqOperations::pasteInfo(url());
1083 }
1084
1085 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
1086 {
1087 m_tabsForFiles = tabsForFiles;
1088 }
1089
1090 bool DolphinView::isTabsForFilesEnabled() const
1091 {
1092 return m_tabsForFiles;
1093 }
1094
1095 bool DolphinView::itemsExpandable() const
1096 {
1097 return m_mode == DetailsView;
1098 }
1099
1100 void DolphinView::restoreState(QDataStream& stream)
1101 {
1102 // Restore the current item that had the keyboard focus
1103 stream >> m_currentItemUrl;
1104
1105 // Restore the view position
1106 stream >> m_restoredContentsPosition;
1107
1108 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1109 QSet<KUrl> urls;
1110 stream >> urls;
1111 m_model->restoreExpandedDirectories(urls);
1112 }
1113
1114 void DolphinView::saveState(QDataStream& stream)
1115 {
1116 // Save the current item that has the keyboard focus
1117 const int currentIndex = m_container->controller()->selectionManager()->currentItem();
1118 if (currentIndex != -1) {
1119 KFileItem item = m_model->fileItem(currentIndex);
1120 Q_ASSERT(!item.isNull()); // If the current index is valid a item must exist
1121 KUrl currentItemUrl = item.url();
1122 stream << currentItemUrl;
1123 } else {
1124 stream << KUrl();
1125 }
1126
1127 // Save view position
1128 const qreal x = m_container->horizontalScrollBar()->value();
1129 const qreal y = m_container->verticalScrollBar()->value();
1130 stream << QPoint(x, y);
1131
1132 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1133 stream << m_model->expandedDirectories();
1134 }
1135
1136 KFileItem DolphinView::rootItem() const
1137 {
1138 return m_model->rootItem();
1139 }
1140
1141 void DolphinView::setViewPropertiesContext(const QString& context)
1142 {
1143 m_viewPropertiesContext = context;
1144 }
1145
1146 QString DolphinView::viewPropertiesContext() const
1147 {
1148 return m_viewPropertiesContext;
1149 }
1150
1151 void DolphinView::observeCreatedItem(const KUrl& url)
1152 {
1153 m_createdItemUrl = url;
1154 connect(m_model, SIGNAL(directoryLoadingCompleted()),
1155 this, SLOT(selectAndScrollToCreatedItem()));
1156 }
1157
1158 void DolphinView::selectAndScrollToCreatedItem()
1159 {
1160 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1161 const int index = m_model->index(m_createdItemUrl);
1162 if (index != -1) {
1163 selectionManager->setCurrentItem(index);
1164 selectionManager->clearSelection();
1165 selectionManager->setSelected(index);
1166 m_view->scrollToItem(index);
1167 }
1168
1169 disconnect(m_model, SIGNAL(directoryLoadingCompleted()),
1170 this, SLOT(selectAndScrollToCreatedItem()));
1171 m_createdItemUrl = KUrl();
1172 }
1173
1174 void DolphinView::slotDirectoryRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1175 {
1176 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
1177 emit redirection(oldUrl, newUrl);
1178 m_url = newUrl; // #186947
1179 }
1180 }
1181
1182 void DolphinView::updateViewState()
1183 {
1184 if (m_currentItemUrl != KUrl()) {
1185 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1186 const int currentIndex = m_model->index(m_currentItemUrl);
1187 if (currentIndex != -1) {
1188 selectionManager->setCurrentItem(currentIndex);
1189 } else {
1190 selectionManager->setCurrentItem(0);
1191 }
1192 m_currentItemUrl = KUrl();
1193 }
1194
1195 if (!m_restoredContentsPosition.isNull()) {
1196 const int x = m_restoredContentsPosition.x();
1197 const int y = m_restoredContentsPosition.y();
1198 m_restoredContentsPosition = QPoint();
1199
1200 m_container->horizontalScrollBar()->setValue(x);
1201 m_container->verticalScrollBar()->setValue(y);
1202 }
1203
1204 if (!m_selectedUrls.isEmpty()) {
1205 clearSelection();
1206
1207 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1208 QSet<int> selectedItems = selectionManager->selectedItems();
1209
1210 foreach (const KUrl& url, m_selectedUrls) {
1211 const int index = m_model->index(url);
1212 if (index >= 0) {
1213 selectedItems.insert(index);
1214 }
1215 }
1216
1217 selectionManager->setSelectedItems(selectedItems);
1218 m_selectedUrls.clear();
1219 }
1220 }
1221
1222 void DolphinView::hideToolTip()
1223 {
1224 if (GeneralSettings::showToolTips()) {
1225 m_toolTipManager->hideToolTip();
1226 }
1227 }
1228
1229 void DolphinView::calculateItemCount(int& fileCount,
1230 int& folderCount,
1231 KIO::filesize_t& totalFileSize) const
1232 {
1233 const int itemCount = m_model->count();
1234 for (int i = 0; i < itemCount; ++i) {
1235 const KFileItem item = m_model->fileItem(i);
1236 if (item.isDir()) {
1237 ++folderCount;
1238 } else {
1239 ++fileCount;
1240 totalFileSize += item.size();
1241 }
1242 }
1243 }
1244
1245 void DolphinView::showHoverInformation(const KFileItem& item)
1246 {
1247 emit requestItemInfo(item);
1248 }
1249
1250 void DolphinView::clearHoverInformation()
1251 {
1252 emit requestItemInfo(KFileItem());
1253 }
1254
1255 void DolphinView::slotDeleteFileFinished(KJob* job)
1256 {
1257 if (job->error() == 0) {
1258 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1259 } else if (job->error() != KIO::ERR_USER_CANCELED) {
1260 emit errorMessage(job->errorString());
1261 }
1262 }
1263
1264 void DolphinView::slotDirectoryLoadingStarted()
1265 {
1266 // Disable the writestate temporary until it can be determined in a fast way
1267 // in DolphinView::slotLoadingCompleted()
1268 if (m_isFolderWritable) {
1269 m_isFolderWritable = false;
1270 emit writeStateChanged(m_isFolderWritable);
1271 }
1272
1273 emit directoryLoadingStarted();
1274 }
1275
1276 void DolphinView::slotDirectoryLoadingCompleted()
1277 {
1278 // Update the view-state. This has to be done asynchronously
1279 // because the view might not be in its final state yet.
1280 QTimer::singleShot(0, this, SLOT(updateViewState()));
1281
1282 emit directoryLoadingCompleted();
1283
1284 updateWritableState();
1285 }
1286
1287 void DolphinView::slotItemsChanged()
1288 {
1289 m_assureVisibleCurrentIndex = false;
1290 }
1291
1292 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous)
1293 {
1294 Q_UNUSED(previous);
1295 Q_ASSERT(m_model->sortOrder() == current);
1296
1297 ViewProperties props(viewPropertiesUrl());
1298 props.setSortOrder(current);
1299
1300 emit sortOrderChanged(current);
1301 }
1302
1303 void DolphinView::slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous)
1304 {
1305 Q_UNUSED(previous);
1306 Q_ASSERT(m_model->sortRole() == current);
1307
1308 ViewProperties props(viewPropertiesUrl());
1309 props.setSortRole(current);
1310
1311 emit sortRoleChanged(current);
1312 }
1313
1314 void DolphinView::slotVisibleRolesChangedByHeader(const QList<QByteArray>& current,
1315 const QList<QByteArray>& previous)
1316 {
1317 Q_UNUSED(previous);
1318 Q_ASSERT(m_container->controller()->view()->visibleRoles() == current);
1319
1320 const QList<QByteArray> previousVisibleRoles = m_visibleRoles;
1321
1322 m_visibleRoles = current;
1323
1324 ViewProperties props(viewPropertiesUrl());
1325 props.setVisibleRoles(m_visibleRoles);
1326
1327 emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
1328 }
1329
1330 void DolphinView::slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value)
1331 {
1332 if (role == "text") {
1333 const KFileItem oldItem = m_model->fileItem(index);
1334 const QString newName = value.toString();
1335 if (!newName.isEmpty() && newName != oldItem.text() && newName != QLatin1String(".") && newName != QLatin1String("..")) {
1336 const KUrl oldUrl = oldItem.url();
1337
1338 QHash<QByteArray, QVariant> data;
1339 data.insert(role, value);
1340 m_model->setData(index, data);
1341
1342 KonqOperations::rename(this, oldUrl, newName);
1343 }
1344 }
1345 }
1346
1347 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1348 {
1349 if (!url.isValid()) {
1350 const QString location(url.pathOrUrl());
1351 if (location.isEmpty()) {
1352 emit errorMessage(i18nc("@info:status", "The location is empty."));
1353 } else {
1354 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1355 }
1356 return;
1357 }
1358
1359 if (reload) {
1360 m_model->refreshDirectory(url);
1361 } else {
1362 m_model->loadDirectory(url);
1363 }
1364 }
1365
1366 void DolphinView::applyViewProperties()
1367 {
1368 m_view->beginTransaction();
1369
1370 const ViewProperties props(viewPropertiesUrl());
1371
1372 const Mode mode = props.viewMode();
1373 if (m_mode != mode) {
1374 const Mode previousMode = m_mode;
1375 m_mode = mode;
1376
1377 // Changing the mode might result in changing
1378 // the zoom level. Remember the old zoom level so
1379 // that zoomLevelChanged() can get emitted.
1380 const int oldZoomLevel = m_view->zoomLevel();
1381 applyModeToView();
1382
1383 emit modeChanged(m_mode, previousMode);
1384
1385 if (m_view->zoomLevel() != oldZoomLevel) {
1386 emit zoomLevelChanged(m_view->zoomLevel(), oldZoomLevel);
1387 }
1388 }
1389
1390 const bool hiddenFilesShown = props.hiddenFilesShown();
1391 if (hiddenFilesShown != m_model->showHiddenFiles()) {
1392 m_model->setShowHiddenFiles(hiddenFilesShown);
1393 emit hiddenFilesShownChanged(hiddenFilesShown);
1394 }
1395
1396 const bool groupedSorting = props.groupedSorting();
1397 if (groupedSorting != m_model->groupedSorting()) {
1398 m_model->setGroupedSorting(groupedSorting);
1399 emit groupedSortingChanged(groupedSorting);
1400 }
1401
1402 const QByteArray sortRole = props.sortRole();
1403 if (sortRole != m_model->sortRole()) {
1404 m_model->setSortRole(sortRole);
1405 emit sortRoleChanged(sortRole);
1406 }
1407
1408 const Qt::SortOrder sortOrder = props.sortOrder();
1409 if (sortOrder != m_model->sortOrder()) {
1410 m_model->setSortOrder(sortOrder);
1411 emit sortOrderChanged(sortOrder);
1412 }
1413
1414 const bool sortFoldersFirst = props.sortFoldersFirst();
1415 if (sortFoldersFirst != m_model->sortDirectoriesFirst()) {
1416 m_model->setSortDirectoriesFirst(sortFoldersFirst);
1417 emit sortFoldersFirstChanged(sortFoldersFirst);
1418 }
1419
1420 const QList<QByteArray> visibleRoles = props.visibleRoles();
1421 if (visibleRoles != m_visibleRoles) {
1422 const QList<QByteArray> previousVisibleRoles = m_visibleRoles;
1423 m_visibleRoles = visibleRoles;
1424 m_view->setVisibleRoles(visibleRoles);
1425 emit visibleRolesChanged(m_visibleRoles, previousVisibleRoles);
1426 }
1427
1428 const bool previewsShown = props.previewsShown();
1429 if (previewsShown != m_view->previewsShown()) {
1430 const int oldZoomLevel = zoomLevel();
1431
1432 m_view->setPreviewsShown(previewsShown);
1433 emit previewsShownChanged(previewsShown);
1434
1435 // Changing the preview-state might result in a changed zoom-level
1436 if (oldZoomLevel != zoomLevel()) {
1437 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1438 }
1439 }
1440
1441 KItemListView* itemListView = m_container->controller()->view();
1442 if (itemListView->isHeaderVisible()) {
1443 KItemListHeader* header = itemListView->header();
1444 const QList<int> headerColumnWidths = props.headerColumnWidths();
1445 const int rolesCount = m_visibleRoles.count();
1446 if (headerColumnWidths.count() == rolesCount) {
1447 header->setAutomaticColumnResizing(false);
1448
1449 QHash<QByteArray, qreal> columnWidths;
1450 for (int i = 0; i < rolesCount; ++i) {
1451 columnWidths.insert(m_visibleRoles[i], headerColumnWidths[i]);
1452 }
1453 header->setColumnWidths(columnWidths);
1454 } else {
1455 header->setAutomaticColumnResizing(true);
1456 }
1457 }
1458
1459 m_view->endTransaction();
1460 }
1461
1462 void DolphinView::applyModeToView()
1463 {
1464 switch (m_mode) {
1465 case IconsView: m_view->setItemLayout(KFileItemListView::IconsLayout); break;
1466 case CompactView: m_view->setItemLayout(KFileItemListView::CompactLayout); break;
1467 case DetailsView: m_view->setItemLayout(KFileItemListView::DetailsLayout); break;
1468 default: Q_ASSERT(false); break;
1469 }
1470 }
1471
1472 void DolphinView::pasteToUrl(const KUrl& url)
1473 {
1474 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1475 KonqOperations::doPaste(this, url);
1476 }
1477
1478 KUrl::List DolphinView::simplifiedSelectedUrls() const
1479 {
1480 KUrl::List urls;
1481
1482 const KFileItemList items = selectedItems();
1483 foreach (const KFileItem& item, items) {
1484 urls.append(item.url());
1485 }
1486
1487 if (itemsExpandable()) {
1488 // TODO: Check if we still need KDirModel for this in KDE 5.0
1489 urls = KDirModel::simplifiedUrlList(urls);
1490 }
1491
1492 return urls;
1493 }
1494
1495 QMimeData* DolphinView::selectionMimeData() const
1496 {
1497 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1498 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1499
1500 return m_model->createMimeData(selectedIndexes);
1501 }
1502
1503 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1504 {
1505 const KUrl::List sourceUrls = KUrl::List::fromMimeData(mimeData);
1506 KUrl::List destUrls;
1507 foreach (const KUrl& source, sourceUrls) {
1508 KUrl destination(url().url() + '/' + source.fileName());
1509 destUrls << destination;
1510 }
1511 markUrlsAsSelected(destUrls);
1512 }
1513
1514 void DolphinView::updateWritableState()
1515 {
1516 const bool wasFolderWritable = m_isFolderWritable;
1517 m_isFolderWritable = true;
1518
1519 const KFileItem item = m_model->rootItem();
1520 if (!item.isNull()) {
1521 KFileItemListProperties capabilities(KFileItemList() << item);
1522 m_isFolderWritable = capabilities.supportsWriting();
1523 }
1524 if (m_isFolderWritable != wasFolderWritable) {
1525 emit writeStateChanged(m_isFolderWritable);
1526 }
1527 }
1528
1529 KUrl DolphinView::viewPropertiesUrl() const
1530 {
1531 if (m_viewPropertiesContext.isEmpty()) {
1532 return m_url;
1533 }
1534
1535 KUrl url;
1536 url.setProtocol(m_url.protocol());
1537 url.setPath(m_viewPropertiesContext);
1538 return url;
1539 }
1540
1541 #include "dolphinview.moc"