]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Fixed a trivial bug that was breaking build.
[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 <QAbstractItemView>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QKeyEvent>
27 #include <QItemSelection>
28 #include <QBoxLayout>
29 #include <QTimer>
30 #include <QScrollBar>
31
32 #include <KActionCollection>
33 #include <KColorScheme>
34 #include <KDirLister>
35 #include <KIconEffect>
36 #include <KFileItem>
37 #include <KFileItemListProperties>
38 #include <KLocale>
39 #include <kitemviews/kfileitemmodel.h>
40 #include <kitemviews/kfileitemlistview.h>
41 #include <kitemviews/kitemlistselectionmanager.h>
42 #include <kitemviews/kitemlistview.h>
43 #include <kitemviews/kitemlistcontroller.h>
44 #include <KIO/DeleteJob>
45 #include <KIO/NetAccess>
46 #include <KIO/PreviewJob>
47 #include <KJob>
48 #include <KMenu>
49 #include <KMessageBox>
50 #include <konq_fileitemcapabilities.h>
51 #include <konq_operations.h>
52 #include <konqmimedata.h>
53 #include <KToggleAction>
54 #include <KUrl>
55
56 #include "additionalinfoaccessor.h"
57 #include "dolphindirlister.h"
58 #include "dolphinnewfilemenuobserver.h"
59 #include "dolphin_detailsmodesettings.h"
60 #include "dolphin_generalsettings.h"
61 #include "dolphinitemlistcontainer.h"
62 #include "renamedialog.h"
63 #include "settings/dolphinsettings.h"
64 #include "viewmodecontroller.h"
65 #include "viewproperties.h"
66 #include "views/tooltips/tooltipmanager.h"
67 #include "zoomlevelinfo.h"
68
69 namespace {
70 const int MaxModeEnum = DolphinView::CompactView;
71 const int MaxSortingEnum = DolphinView::SortByPath;
72 };
73
74 DolphinView::DolphinView(const KUrl& url, QWidget* parent) :
75 QWidget(parent),
76 m_active(true),
77 m_tabsForFiles(false),
78 m_assureVisibleCurrentIndex(false),
79 m_isFolderWritable(true),
80 m_url(url),
81 m_mode(DolphinView::IconsView),
82 m_additionalInfoList(),
83 m_topLayout(0),
84 m_dirLister(0),
85 m_container(0),
86 m_toolTipManager(0),
87 m_selectionChangedTimer(0),
88 m_currentItemIndex(-1),
89 m_restoredContentsPosition(),
90 m_createdItemUrl(),
91 m_selectedItems()
92 {
93 m_topLayout = new QVBoxLayout(this);
94 m_topLayout->setSpacing(0);
95 m_topLayout->setMargin(0);
96
97 //m_dolphinViewController = new DolphinViewController(this);
98
99 //m_viewModeController = new ViewModeController(this);
100 //m_viewModeController->setUrl(url);
101
102 /*connect(m_viewModeController, SIGNAL(urlChanged(KUrl)),
103 this, SIGNAL(urlChanged(KUrl)));
104
105 connect(m_dolphinViewController, SIGNAL(requestContextMenu(QPoint,QList<QAction*>)),
106 this, SLOT(openContextMenu(QPoint,QList<QAction*>)));
107 connect(m_dolphinViewController, SIGNAL(urlsDropped(KFileItem,KUrl,QDropEvent*)),
108 this, SLOT(dropUrls(KFileItem,KUrl,QDropEvent*)));
109 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
110 this, SLOT(updateSorting(DolphinView::Sorting)));
111 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
112 this, SLOT(updateSortOrder(Qt::SortOrder)));
113 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
114 this, SLOT(updateSortFoldersFirst(bool)));
115 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(QList<DolphinView::AdditionalInfo>)),
116 this, SLOT(updateAdditionalInfo(QList<DolphinView::AdditionalInfo>)));*/
117 //connect(m_dolphinViewController, SIGNAL(itemTriggered(KFileItem)),
118 // this, SLOT(triggerItem(KFileItem)));
119 //connect(m_dolphinViewController, SIGNAL(tabRequested(KUrl)),
120 // this, SIGNAL(tabRequested(KUrl)));
121 /*connect(m_dolphinViewController, SIGNAL(activated()),
122 this, SLOT(activate()));
123 connect(m_dolphinViewController, SIGNAL(itemEntered(KFileItem)),
124 this, SLOT(showHoverInformation(KFileItem)));
125 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
126 this, SLOT(clearHoverInformation()));
127 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
128 this, SLOT(slotUrlChangeRequested(KUrl)));*/
129
130 // When a new item has been created by the "Create New..." menu, the item should
131 // get selected and it must be assured that the item will get visible. As the
132 // creation is done asynchronously, several signals must be checked:
133 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl)),
134 this, SLOT(observeCreatedItem(KUrl)));
135
136 m_selectionChangedTimer = new QTimer(this);
137 m_selectionChangedTimer->setSingleShot(true);
138 m_selectionChangedTimer->setInterval(300);
139 connect(m_selectionChangedTimer, SIGNAL(timeout()),
140 this, SLOT(emitSelectionChangedSignal()));
141
142 m_dirLister = new DolphinDirLister(this);
143 m_dirLister->setAutoUpdate(true);
144 m_dirLister->setDelayedMimeTypes(true);
145
146 connect(m_dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(slotRedirection(KUrl,KUrl)));
147 connect(m_dirLister, SIGNAL(started(KUrl)), this, SLOT(slotDirListerStarted(KUrl)));
148 connect(m_dirLister, SIGNAL(completed()), this, SLOT(slotDirListerCompleted()));
149 connect(m_dirLister, SIGNAL(refreshItems(QList<QPair<KFileItem,KFileItem> >)),
150 this, SLOT(slotRefreshItems()));
151
152 connect(m_dirLister, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
153 connect(m_dirLister, SIGNAL(newItems(KFileItemList)), this, SIGNAL(itemCountChanged()));
154 connect(m_dirLister, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
155 connect(m_dirLister, SIGNAL(errorMessage(QString)), this, SIGNAL(infoMessage(QString)));
156 connect(m_dirLister, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
157 connect(m_dirLister, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl)));
158 connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SIGNAL(itemCountChanged()));
159
160 m_container = new DolphinItemListContainer(m_dirLister, this);
161 QHash<QByteArray, int> visibleRoles;
162 visibleRoles.insert("name", 0);
163 m_container->setVisibleRoles(visibleRoles);
164
165 KItemListController* controller = m_container->controller();
166 controller->setSelectionBehavior(KItemListController::MultiSelection);
167 connect(controller, SIGNAL(itemActivated(int)),
168 this, SLOT(slotItemActivated(int)));
169 connect(controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
170 connect(controller, SIGNAL(contextMenuRequested(int,QPointF)), this, SLOT(slotContextMenuRequested(int,QPointF)));
171 connect(controller, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
172 connect(controller, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
173 connect(controller, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
174
175 KItemListSelectionManager* selectionManager = controller->selectionManager();
176 connect(selectionManager, SIGNAL(selectionChanged(QSet<int>,QSet<int>)),
177 this, SLOT(slotSelectionChanged(QSet<int>,QSet<int>)));
178
179 m_toolTipManager = new ToolTipManager(this);
180
181 applyViewProperties();
182 m_topLayout->addWidget(m_container);
183
184 loadDirectory(url);
185 }
186
187 DolphinView::~DolphinView()
188 {
189 }
190
191 KUrl DolphinView::url() const
192 {
193 return m_url;
194 }
195
196 void DolphinView::setActive(bool active)
197 {
198 if (active == m_active) {
199 return;
200 }
201
202 m_active = active;
203
204 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
205 if (!active) {
206 color.setAlpha(150);
207 }
208
209 /*QAbstractItemView* view = m_viewAccessor.itemView();
210 QWidget* viewport = view ? view->viewport() : 0;
211 if (viewport) {
212 QPalette palette;
213 palette.setColor(viewport->backgroundRole(), color);
214 viewport->setPalette(palette);
215 }*/
216
217 update();
218
219 if (active) {
220 //if (view) {
221 // view->setFocus();
222 //}
223 emit activated();
224 emit writeStateChanged(m_isFolderWritable);
225 }
226
227 //m_viewModeController->indicateActivationChange(active);
228 }
229
230 bool DolphinView::isActive() const
231 {
232 return m_active;
233 }
234
235 void DolphinView::setMode(Mode mode)
236 {
237 if (mode != m_mode) {
238 ViewProperties props(url());
239 props.setViewMode(mode);
240 props.save();
241
242 applyViewProperties();
243 }
244 }
245
246 DolphinView::Mode DolphinView::mode() const
247 {
248 return m_mode;
249 }
250
251 bool DolphinView::previewsShown() const
252 {
253 return m_container->previewsShown();
254 }
255
256 bool DolphinView::hiddenFilesShown() const
257 {
258 return m_dirLister->showingDotFiles();
259 }
260
261 bool DolphinView::categorizedSorting() const
262 {
263 return false; //m_storedCategorizedSorting;
264 }
265
266 KFileItemList DolphinView::items() const
267 {
268 return m_dirLister->items();
269 }
270
271 KFileItemList DolphinView::selectedItems() const
272 {
273 const KFileItemModel* model = fileItemModel();
274 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
275 const QSet<int> selectedIndexes = selectionManager->selectedItems();
276
277 KFileItemList selectedItems;
278 QSetIterator<int> it(selectedIndexes);
279 while (it.hasNext()) {
280 const int index = it.next();
281 selectedItems.append(model->fileItem(index));
282 }
283 return selectedItems;
284 }
285
286 int DolphinView::selectedItemsCount() const
287 {
288 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
289 return selectionManager->selectedItems().count();
290 }
291
292 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
293 {
294 foreach (const KUrl& url, urls) {
295 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
296 m_selectedItems.append(item);
297 }
298 }
299
300 void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled)
301 {
302 const KItemListSelectionManager::SelectionMode mode = enabled
303 ? KItemListSelectionManager::Select
304 : KItemListSelectionManager::Deselect;
305 const KFileItemModel* model = fileItemModel();
306 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
307
308 for (int index = 0; index < model->count(); index++) {
309 const KFileItem item = model->fileItem(index);
310 if (pattern.exactMatch(item.name())) {
311 // An alternative approach would be to store the matching items in a QSet<int> and
312 // select them in one go after the loop, but we'd need a new function
313 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
314 // for that.
315 selectionManager->setSelected(index, 1, mode);
316 }
317 }
318 }
319
320 void DolphinView::setZoomLevel(int level)
321 {
322 const int oldZoomLevel = zoomLevel();
323 m_container->setZoomLevel(level);
324 if (zoomLevel() != oldZoomLevel) {
325 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
326 }
327 }
328
329 int DolphinView::zoomLevel() const
330 {
331 return m_container->zoomLevel();
332 }
333
334 void DolphinView::setSorting(Sorting sorting)
335 {
336 if (sorting != this->sorting()) {
337 updateSorting(sorting);
338 }
339 }
340
341 DolphinView::Sorting DolphinView::sorting() const
342 {
343 return DolphinView::SortByName;
344 //return m_viewAccessor.proxyModel()->sorting();
345 }
346
347 void DolphinView::setSortOrder(Qt::SortOrder order)
348 {
349 if (sortOrder() != order) {
350 updateSortOrder(order);
351 }
352 }
353
354 Qt::SortOrder DolphinView::sortOrder() const
355 {
356 return Qt::AscendingOrder; // m_viewAccessor.proxyModel()->sortOrder();
357 }
358
359 void DolphinView::setSortFoldersFirst(bool foldersFirst)
360 {
361 if (sortFoldersFirst() != foldersFirst) {
362 updateSortFoldersFirst(foldersFirst);
363 }
364 }
365
366 bool DolphinView::sortFoldersFirst() const
367 {
368 return true; // m_viewAccessor.proxyModel()->sortFoldersFirst();
369 }
370
371 void DolphinView::setAdditionalInfoList(const QList<AdditionalInfo>& info)
372 {
373 const QList<AdditionalInfo> previousList = info;
374
375 ViewProperties props(url());
376 props.setAdditionalInfoList(info);
377
378 m_additionalInfoList = info;
379 applyAdditionalInfoListToView();
380
381 emit additionalInfoListChanged(m_additionalInfoList, previousList);
382 }
383
384 QList<DolphinView::AdditionalInfo> DolphinView::additionalInfoList() const
385 {
386 return m_additionalInfoList;
387 }
388
389 void DolphinView::reload()
390 {
391 QByteArray viewState;
392 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
393 saveState(saveStream);
394 m_selectedItems= selectedItems();
395
396 setUrl(url());
397 loadDirectory(url(), true);
398
399 QDataStream restoreStream(viewState);
400 restoreState(restoreStream);
401 }
402
403 void DolphinView::stopLoading()
404 {
405 m_dirLister->stop();
406 }
407
408 void DolphinView::refresh()
409 {
410 const bool oldActivationState = m_active;
411 const int oldZoomLevel = zoomLevel();
412 m_active = true;
413
414 applyViewProperties();
415 reload();
416
417 setActive(oldActivationState);
418 updateZoomLevel(oldZoomLevel);
419 }
420
421 void DolphinView::setNameFilter(const QString& nameFilter)
422 {
423 Q_UNUSED(nameFilter);
424 //m_viewModeController->setNameFilter(nameFilter);
425 }
426
427 QString DolphinView::nameFilter() const
428 {
429 return QString(); //m_viewModeController->nameFilter();
430 }
431
432 void DolphinView::calculateItemCount(int& fileCount,
433 int& folderCount,
434 KIO::filesize_t& totalFileSize) const
435 {
436 foreach (const KFileItem& item, m_dirLister->items()) {
437 if (item.isDir()) {
438 ++folderCount;
439 } else {
440 ++fileCount;
441 totalFileSize += item.size();
442 }
443 }
444 }
445
446 QString DolphinView::statusBarText() const
447 {
448 QString text;
449 int folderCount = 0;
450 int fileCount = 0;
451 KIO::filesize_t totalFileSize = 0;
452
453 if (hasSelection()) {
454 // give a summary of the status of the selected files
455 const KFileItemList list = selectedItems();
456 if (list.isEmpty()) {
457 // when an item is triggered, it is temporary selected but selectedItems()
458 // will return an empty list
459 return text;
460 }
461
462 KFileItemList::const_iterator it = list.begin();
463 const KFileItemList::const_iterator end = list.end();
464 while (it != end) {
465 const KFileItem& item = *it;
466 if (item.isDir()) {
467 ++folderCount;
468 } else {
469 ++fileCount;
470 totalFileSize += item.size();
471 }
472 ++it;
473 }
474
475 if (folderCount + fileCount == 1) {
476 // if only one item is selected, show the filename
477 const QString name = list.first().text();
478 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
479 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
480 name, KIO::convertSize(totalFileSize));
481 } else {
482 // at least 2 items are selected
483 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
484 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
485 if ((folderCount > 0) && (fileCount > 0)) {
486 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
487 foldersText, filesText, KIO::convertSize(totalFileSize));
488 } else if (fileCount > 0) {
489 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
490 } else {
491 Q_ASSERT(folderCount > 0);
492 text = foldersText;
493 }
494 }
495 } else {
496 calculateItemCount(fileCount, folderCount, totalFileSize);
497 text = KIO::itemsSummaryString(fileCount + folderCount,
498 fileCount, folderCount,
499 totalFileSize, true);
500 }
501
502 return text;
503 }
504
505 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
506 {
507 Q_UNUSED(items);
508 return QList<QAction*>(); //m_dolphinViewController->versionControlActions(items);
509 }
510
511 void DolphinView::setUrl(const KUrl& url)
512 {
513 if (url == m_url) {
514 return;
515 }
516
517 emit urlAboutToBeChanged(url);
518 m_url = url;
519
520 if (GeneralSettings::showToolTips()) {
521 m_toolTipManager->hideToolTip();
522 }
523
524 // It is important to clear the items from the model before
525 // applying the view properties, otherwise expensive operations
526 // might be done on the existing items although they get cleared
527 // anyhow afterwards by loadDirectory().
528 fileItemModel()->clear();
529 applyViewProperties();
530 loadDirectory(url);
531
532 emit urlChanged(url);
533 }
534
535 void DolphinView::selectAll()
536 {
537 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
538 selectionManager->setSelected(0, fileItemModel()->count());
539 }
540
541 void DolphinView::invertSelection()
542 {
543 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
544 selectionManager->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle);
545 }
546
547 void DolphinView::clearSelection()
548 {
549 m_container->controller()->selectionManager()->clearSelection();
550 }
551
552 void DolphinView::renameSelectedItems()
553 {
554 KFileItemList items = selectedItems();
555 const int itemCount = items.count();
556 if (itemCount < 1) {
557 return;
558 }
559
560 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
561 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
562 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
563 m_viewAccessor.itemView()->edit(proxyIndex);
564 } else {*/
565 RenameDialog* dialog = new RenameDialog(this, items);
566 dialog->setAttribute(Qt::WA_DeleteOnClose);
567 dialog->show();
568 dialog->raise();
569 dialog->activateWindow();
570 //}
571
572 // assure that the current index remains visible when KDirLister
573 // will notify the view about changed items
574 m_assureVisibleCurrentIndex = true;
575 }
576
577 void DolphinView::trashSelectedItems()
578 {
579 const KUrl::List list = simplifiedSelectedUrls();
580 KonqOperations::del(this, KonqOperations::TRASH, list);
581 }
582
583 void DolphinView::deleteSelectedItems()
584 {
585 const KUrl::List list = simplifiedSelectedUrls();
586 const bool del = KonqOperations::askDeleteConfirmation(list,
587 KonqOperations::DEL,
588 KonqOperations::DEFAULT_CONFIRMATION,
589 this);
590
591 if (del) {
592 KIO::Job* job = KIO::del(list);
593 connect(job, SIGNAL(result(KJob*)),
594 this, SLOT(slotDeleteFileFinished(KJob*)));
595 }
596 }
597
598 void DolphinView::cutSelectedItems()
599 {
600 QMimeData* mimeData = selectionMimeData();
601 KonqMimeData::addIsCutSelection(mimeData, true);
602 QApplication::clipboard()->setMimeData(mimeData);
603 }
604
605 void DolphinView::copySelectedItems()
606 {
607 QMimeData* mimeData = selectionMimeData();
608 QApplication::clipboard()->setMimeData(mimeData);
609 }
610
611 void DolphinView::paste()
612 {
613 pasteToUrl(url());
614 }
615
616 void DolphinView::pasteIntoFolder()
617 {
618 const KFileItemList items = selectedItems();
619 if ((items.count() == 1) && items.first().isDir()) {
620 pasteToUrl(items.first().url());
621 }
622 }
623
624 void DolphinView::setPreviewsShown(bool show)
625 {
626 if (previewsShown() == show) {
627 return;
628 }
629
630 ViewProperties props(url());
631 props.setPreviewsShown(show);
632
633 m_container->setPreviewsShown(show);
634 emit previewsShownChanged(show);
635 }
636
637 void DolphinView::setHiddenFilesShown(bool show)
638 {
639 if (m_dirLister->showingDotFiles() == show) {
640 return;
641 }
642
643 m_selectedItems = selectedItems();
644
645 ViewProperties props(url());
646 props.setHiddenFilesShown(show);
647
648 m_dirLister->setShowingDotFiles(show);
649 m_dirLister->emitChanges();
650 emit hiddenFilesShownChanged(show);
651 }
652
653 void DolphinView::setCategorizedSorting(bool categorized)
654 {
655 if (categorized == categorizedSorting()) {
656 return;
657 }
658
659 ViewProperties props(url());
660 props.setCategorizedSorting(categorized);
661 props.save();
662
663 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
664
665 emit categorizedSortingChanged(categorized);
666 }
667
668 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
669 {
670 QWidget::mouseReleaseEvent(event);
671 setActive(true);
672 }
673
674 void DolphinView::contextMenuEvent(QContextMenuEvent* event)
675 {
676 Q_UNUSED(event);
677
678 const QPoint pos = m_container->mapFromGlobal(QCursor::pos());
679 const KItemListView* view = m_container->controller()->view();
680 if (view->itemAt(pos) < 0) {
681 // Only open the context-menu if the cursor is above the viewport
682 // (the context-menu for items is handled in slotContextMenuRequested())
683 requestContextMenu(KFileItem(), url(), QList<QAction*>());
684 }
685 }
686
687 void DolphinView::activate()
688 {
689 setActive(true);
690 }
691
692 void DolphinView::slotItemActivated(int index)
693 {
694 const QSet<int> selectedItems = m_container->controller()->selectionManager()->selectedItems();
695 if (selectedItems.isEmpty())
696 return;
697 if (selectedItems.count() == 1) {
698 emit itemTriggered(fileItemModel()->fileItem(index)); // caught by DolphinViewContainer or DolphinPart
699 }
700 else {
701 foreach (int i, selectedItems) {
702 const KFileItem fileItem = fileItemModel()->fileItem(i);
703 if (fileItem.isDir()) {
704 emit tabRequested(fileItem.url());
705 } else {
706 emit itemTriggered(fileItem);
707 }
708 }
709 }
710 }
711
712 void DolphinView::slotItemMiddleClicked(int index)
713 {
714 const KFileItem item = fileItemModel()->fileItem(index);
715 if (item.isDir() || isTabsForFilesEnabled()) {
716 emit tabRequested(item.url());
717 }
718 }
719
720 void DolphinView::slotContextMenuRequested(int index, const QPointF& pos)
721 {
722 if (GeneralSettings::showToolTips()) {
723 m_toolTipManager->hideToolTip();
724 }
725 const KFileItem item = fileItemModel()->fileItem(index);
726 emit requestContextMenu(item, url(), QList<QAction*>());
727 }
728
729 void DolphinView::slotItemExpansionToggleClicked(int index)
730 {
731 // TODO: When doing a model->setExpanded(false) it should
732 // be checked here whether the current index is part of the
733 // closed sub-tree. If this is the case, the current index
734 // should be adjusted to the parent index.
735 KFileItemModel* model = fileItemModel();
736 const bool expanded = model->isExpanded(index);
737 model->setExpanded(index, !expanded);
738 }
739
740 void DolphinView::slotItemHovered(int index)
741 {
742 const KFileItem item = fileItemModel()->fileItem(index);
743
744 if (GeneralSettings::showToolTips()) {
745 QRectF itemRect = m_container->controller()->view()->itemBoundingRect(index);
746 const QPoint pos = m_container->mapToGlobal(itemRect.topLeft().toPoint());
747 itemRect.moveTo(pos);
748
749 m_toolTipManager->showToolTip(item, itemRect);
750 }
751
752 emit requestItemInfo(item);
753 }
754
755 void DolphinView::slotItemUnhovered(int index)
756 {
757 Q_UNUSED(index);
758 if (GeneralSettings::showToolTips()) {
759 m_toolTipManager->hideToolTip();
760 }
761 emit requestItemInfo(KFileItem());
762 }
763
764 void DolphinView::slotSelectionChanged(const QSet<int>& current, const QSet<int>& previous)
765 {
766 const int currentCount = current.count();
767 const int previousCount = previous.count();
768 const bool selectionStateChanged = (currentCount == 0 && previousCount > 0) ||
769 (currentCount > 0 && previousCount == 0);
770
771 // If nothing has been selected before and something got selected (or if something
772 // was selected before and now nothing is selected) the selectionChangedSignal must
773 // be emitted asynchronously as fast as possible to update the edit-actions.
774 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
775 m_selectionChangedTimer->start();
776 }
777
778 void DolphinView::emitSelectionChangedSignal()
779 {
780 m_selectionChangedTimer->stop();
781 emit selectionChanged(selectedItems());
782 }
783
784 void DolphinView::openContextMenu(const QPoint& pos,
785 const QList<QAction*>& customActions)
786 {
787 KFileItem item;
788 const int index = m_container->controller()->view()->itemAt(pos);
789 if (index >= 0) {
790 item = fileItemModel()->fileItem(index);
791 }
792
793 emit requestContextMenu(item, url(), customActions);
794 }
795
796 void DolphinView::dropUrls(const KFileItem& destItem,
797 const KUrl& destPath,
798 QDropEvent* event)
799 {
800 Q_UNUSED(destItem);
801 Q_UNUSED(destPath);
802 markPastedUrlsAsSelected(event->mimeData());
803 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
804 }
805
806 void DolphinView::updateSorting(DolphinView::Sorting sorting)
807 {
808 ViewProperties props(url());
809 props.setSorting(sorting);
810
811 KItemModelBase* model = m_container->controller()->model();
812 model->setSortRole(sortRoleForSorting(sorting));
813
814 emit sortingChanged(sorting);
815 }
816
817 void DolphinView::updateSortOrder(Qt::SortOrder order)
818 {
819 ViewProperties props(url());
820 props.setSortOrder(order);
821
822 //m_viewAccessor.proxyModel()->setSortOrder(order);
823
824 emit sortOrderChanged(order);
825 }
826
827 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
828 {
829 ViewProperties props(url());
830 props.setSortFoldersFirst(foldersFirst);
831
832 //m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
833
834 emit sortFoldersFirstChanged(foldersFirst);
835 }
836
837 QPair<bool, QString> DolphinView::pasteInfo() const
838 {
839 return KonqOperations::pasteInfo(url());
840 }
841
842 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
843 {
844 m_tabsForFiles = tabsForFiles;
845 }
846
847 bool DolphinView::isTabsForFilesEnabled() const
848 {
849 return m_tabsForFiles;
850 }
851
852 bool DolphinView::itemsExpandable() const
853 {
854 return false; //m_viewAccessor.itemsExpandable();
855 }
856
857 void DolphinView::restoreState(QDataStream& stream)
858 {
859 // Restore the current item that had the keyboard focus
860 stream >> m_currentItemIndex;
861
862 // Restore the view position
863 stream >> m_restoredContentsPosition;
864
865 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
866 QSet<KUrl> urlsToExpand;
867 stream >> urlsToExpand;
868 /*const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
869 if (expander) {
870 m_expanderActive = true;
871 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
872 }
873 else {
874 m_expanderActive = false;
875 }*/
876 }
877
878 void DolphinView::saveState(QDataStream& stream)
879 {
880 // Save the current item that has the keyboard focus
881 stream << m_container->controller()->selectionManager()->currentItem();
882
883 // Save view position
884 const qreal x = m_container->horizontalScrollBar()->value();
885 const qreal y = m_container->verticalScrollBar()->value();
886 stream << QPoint(x, y);
887
888 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
889 //stream << m_viewAccessor.expandedUrls();
890 }
891
892 bool DolphinView::hasSelection() const
893 {
894 return m_container->controller()->selectionManager()->hasSelection();
895 }
896
897 KFileItem DolphinView::rootItem() const
898 {
899 return m_dirLister->rootItem();
900 }
901
902 void DolphinView::observeCreatedItem(const KUrl& url)
903 {
904 m_createdItemUrl = url;
905 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
906 // this, SLOT(selectAndScrollToCreatedItem()));
907 }
908
909 void DolphinView::selectAndScrollToCreatedItem()
910 {
911 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
912 if (dirIndex.isValid()) {
913 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
914 QAbstractItemView* view = m_viewAccessor.itemView();
915 if (view) {
916 view->setCurrentIndex(proxyIndex);
917 }
918 }
919
920 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
921 this, SLOT(selectAndScrollToCreatedItem()));*/
922 m_createdItemUrl = KUrl();
923 }
924
925 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
926 {
927 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
928 emit redirection(oldUrl, newUrl);
929 m_url = newUrl; // #186947
930 }
931 }
932
933 void DolphinView::updateViewState()
934 {
935 if (m_currentItemIndex >= 0) {
936 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
937 selectionManager->setCurrentItem(m_currentItemIndex);
938 m_currentItemIndex =-1;
939 }
940
941 if (!m_restoredContentsPosition.isNull()) {
942 const int x = m_restoredContentsPosition.x();
943 const int y = m_restoredContentsPosition.y();
944 m_restoredContentsPosition = QPoint();
945
946 m_container->horizontalScrollBar()->setValue(x);
947 m_container->verticalScrollBar()->setValue(y);
948 }
949
950 if (!m_selectedItems.isEmpty()) {
951 KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
952 QSet<int> selectedItems = selectionManager->selectedItems();
953 const KFileItemModel* model = fileItemModel();
954
955 foreach (const KFileItem& selectedItem, m_selectedItems) {
956 const int index = model->index(selectedItem);
957 if (index >= 0) {
958 selectedItems.insert(index);
959 }
960 }
961
962 selectionManager->setSelectedItems(selectedItems);
963 m_selectedItems.clear();
964 }
965 }
966
967 void DolphinView::showHoverInformation(const KFileItem& item)
968 {
969 emit requestItemInfo(item);
970 }
971
972 void DolphinView::clearHoverInformation()
973 {
974 emit requestItemInfo(KFileItem());
975 }
976
977 void DolphinView::slotDeleteFileFinished(KJob* job)
978 {
979 if (job->error() == 0) {
980 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
981 } else if (job->error() != KIO::ERR_USER_CANCELED) {
982 emit errorMessage(job->errorString());
983 }
984 }
985
986 void DolphinView::slotDirListerStarted(const KUrl& url)
987 {
988 // Disable the writestate temporary until it can be determined in a fast way
989 // in DolphinView::slotDirListerCompleted()
990 if (m_isFolderWritable) {
991 m_isFolderWritable = false;
992 emit writeStateChanged(m_isFolderWritable);
993 }
994
995 emit startedPathLoading(url);
996 }
997
998 void DolphinView::slotDirListerCompleted()
999 {
1000 // Update the view-state. This has to be done using a Qt::QueuedConnection
1001 // because the view might not be in its final state yet (the view also
1002 // listens to the completed()-signal from KDirLister and the order of
1003 // of slots is undefined).
1004 QTimer::singleShot(0, this, SLOT(updateViewState()));
1005
1006 emit finishedPathLoading(url());
1007
1008 updateWritableState();
1009 }
1010
1011 void DolphinView::slotRefreshItems()
1012 {
1013 if (m_assureVisibleCurrentIndex) {
1014 m_assureVisibleCurrentIndex = false;
1015 //QAbstractItemView* view = m_viewAccessor.itemView();
1016 //if (view) {
1017 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1018 //}
1019 }
1020 }
1021
1022 KFileItemModel* DolphinView::fileItemModel() const
1023 {
1024 return static_cast<KFileItemModel*>(m_container->controller()->model());
1025 }
1026
1027 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1028 {
1029 if (!url.isValid()) {
1030 const QString location(url.pathOrUrl());
1031 if (location.isEmpty()) {
1032 emit errorMessage(i18nc("@info:status", "The location is empty."));
1033 } else {
1034 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1035 }
1036 return;
1037 }
1038
1039 m_dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1040 }
1041
1042 void DolphinView::applyViewProperties()
1043 {
1044 m_container->beginTransaction();
1045
1046 const ViewProperties props(url());
1047
1048 const Mode mode = props.viewMode();
1049 if (m_mode != mode) {
1050 const Mode previousMode = m_mode;
1051 m_mode = mode;
1052
1053 // Changing the mode might result in changing
1054 // the zoom level. Remember the old zoom level so
1055 // that zoomLevelChanged() can get emitted.
1056 const int oldZoomLevel = m_container->zoomLevel();
1057
1058 switch (m_mode) {
1059 case IconsView: m_container->setItemLayout(KFileItemListView::IconsLayout); break;
1060 case CompactView: m_container->setItemLayout(KFileItemListView::CompactLayout); break;
1061 case DetailsView: m_container->setItemLayout(KFileItemListView::DetailsLayout); break;
1062 default: Q_ASSERT(false); break;
1063 }
1064
1065 emit modeChanged(m_mode, previousMode);
1066
1067 if (m_container->zoomLevel() != oldZoomLevel) {
1068 emit zoomLevelChanged(m_container->zoomLevel(), oldZoomLevel);
1069 }
1070 }
1071
1072 const bool hiddenFilesShown = props.hiddenFilesShown();
1073 if (hiddenFilesShown != m_dirLister->showingDotFiles()) {
1074 m_dirLister->setShowingDotFiles(hiddenFilesShown);
1075 m_dirLister->emitChanges();
1076 emit hiddenFilesShownChanged(hiddenFilesShown);
1077 }
1078
1079 /* m_storedCategorizedSorting = props.categorizedSorting();
1080 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1081 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1082 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1083 emit categorizedSortingChanged();
1084 }*/
1085
1086 const DolphinView::Sorting sorting = props.sorting();
1087 KItemModelBase* model = m_container->controller()->model();
1088 const QByteArray newSortRole = sortRoleForSorting(sorting);
1089 if (newSortRole != model->sortRole()) {
1090 model->setSortRole(newSortRole);
1091 emit sortingChanged(sorting);
1092 }
1093 /*
1094 const Qt::SortOrder sortOrder = props.sortOrder();
1095 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1096 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1097 emit sortOrderChanged(sortOrder);
1098 }
1099
1100 const bool sortFoldersFirst = props.sortFoldersFirst();
1101 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1102 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1103 emit sortFoldersFirstChanged(sortFoldersFirst);
1104 }
1105 */
1106 const QList<DolphinView::AdditionalInfo> infoList = props.additionalInfoList();
1107 if (infoList != m_additionalInfoList) {
1108 const QList<DolphinView::AdditionalInfo> previousList = m_additionalInfoList;
1109 m_additionalInfoList = infoList;
1110 applyAdditionalInfoListToView();
1111 emit additionalInfoListChanged(m_additionalInfoList, previousList);
1112 }
1113
1114 const bool previewsShown = props.previewsShown();
1115 if (previewsShown != m_container->previewsShown()) {
1116 const int oldZoomLevel = zoomLevel();
1117
1118 m_container->setPreviewsShown(previewsShown);
1119 emit previewsShownChanged(previewsShown);
1120
1121 // Changing the preview-state might result in a changed zoom-level
1122 if (oldZoomLevel != zoomLevel()) {
1123 emit zoomLevelChanged(zoomLevel(), oldZoomLevel);
1124 }
1125 }
1126
1127 m_container->endTransaction();
1128 }
1129
1130 void DolphinView::applyAdditionalInfoListToView()
1131 {
1132 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
1133
1134 QHash<QByteArray, int> visibleRoles;
1135 visibleRoles.insert("name", 0);
1136
1137 int index = 1;
1138 foreach (AdditionalInfo info, m_additionalInfoList) {
1139 visibleRoles.insert(infoAccessor.role(info), index);
1140 ++index;
1141 }
1142
1143 m_container->setVisibleRoles(visibleRoles);
1144 }
1145
1146 void DolphinView::pasteToUrl(const KUrl& url)
1147 {
1148 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1149 KonqOperations::doPaste(this, url);
1150 }
1151
1152 void DolphinView::updateZoomLevel(int oldZoomLevel)
1153 {
1154 Q_UNUSED(oldZoomLevel);
1155 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1156 if (oldZoomLevel != newZoomLevel) {
1157 m_viewModeController->setZoomLevel(newZoomLevel);
1158 emit zoomLevelChanged(newZoomLevel);
1159 }*/
1160 }
1161
1162 KUrl::List DolphinView::simplifiedSelectedUrls() const
1163 {
1164 Q_ASSERT(false); // TODO
1165 KUrl::List urls;
1166 /*
1167 const KFileItemList items = selectedItems();
1168 foreach (const KFileItem &item, items) {
1169 urls.append(item.url());
1170 }
1171
1172
1173 if (itemsExpandable()) {
1174 urls = KDirModel::simplifiedUrlList(urls);
1175 }*/
1176
1177 return urls;
1178 }
1179
1180 QMimeData* DolphinView::selectionMimeData() const
1181 {
1182 const KFileItemModel* model = fileItemModel();
1183 const KItemListSelectionManager* selectionManager = m_container->controller()->selectionManager();
1184 const QSet<int> selectedIndexes = selectionManager->selectedItems();
1185
1186 return model->createMimeData(selectedIndexes);
1187 }
1188
1189 void DolphinView::markPastedUrlsAsSelected(const QMimeData* mimeData)
1190 {
1191 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1192 markUrlsAsSelected(urls);
1193 }
1194
1195 void DolphinView::updateWritableState()
1196 {
1197 const bool wasFolderWritable = m_isFolderWritable;
1198 m_isFolderWritable = true;
1199
1200 const KFileItem item = m_dirLister->rootItem();
1201 if (!item.isNull()) {
1202 KFileItemListProperties capabilities(KFileItemList() << item);
1203 m_isFolderWritable = capabilities.supportsWriting();
1204 }
1205 if (m_isFolderWritable != wasFolderWritable) {
1206 emit writeStateChanged(m_isFolderWritable);
1207 }
1208 }
1209
1210 QByteArray DolphinView::sortRoleForSorting(Sorting sorting) const
1211 {
1212 switch (sorting) {
1213 case SortByName: return "name";
1214 case SortBySize: return "size";
1215 case SortByDate: return "date";
1216 case SortByPermissions: return "permissions";
1217 case SortByOwner: return "owner";
1218 case SortByGroup: return "group";
1219 case SortByType: return "type";
1220 case SortByDestination: return "destination";
1221 case SortByPath: return "path";
1222 default: break;
1223 }
1224
1225 return QByteArray();
1226 }
1227
1228 #include "dolphinview.moc"