]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
reducing a little bit the number of deprecation warnings
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QItemSelectionModel>
24
25 #include <kdirmodel.h>
26
27 #include <qlayout.h>
28 //Added by qt3to4:
29 #include <Q3ValueList>
30 #include <QDropEvent>
31 #include <QMouseEvent>
32 #include <QVBoxLayout>
33 #include <kurl.h>
34 #include <klocale.h>
35 #include <kio/netaccess.h>
36 #include <kio/renamedlg.h>
37 #include <kmimetyperesolver.h>
38 #include <assert.h>
39
40 #include "urlnavigator.h"
41 #include "dolphinstatusbar.h"
42 #include "dolphinmainwindow.h"
43 #include "dolphindirlister.h"
44 #include "viewproperties.h"
45 #include "dolphindetailsview.h"
46 #include "dolphiniconsview.h"
47 #include "dolphincontextmenu.h"
48 #include "undomanager.h"
49 #include "renamedialog.h"
50 #include "progressindicator.h"
51
52 #include "filterbar.h"
53
54 DolphinView::DolphinView(DolphinMainWindow *mainWindow,
55 QWidget *parent,
56 const KUrl& url,
57 Mode mode,
58 bool showHiddenFiles) :
59 QWidget(parent),
60 m_mainWindow(mainWindow),
61 m_refreshing(false),
62 m_showProgress(false),
63 m_mode(mode),
64 m_statusBar(0),
65 m_iconSize(0),
66 m_folderCount(0),
67 m_fileCount(0),
68 m_filterBar(0)
69 {
70 setFocusPolicy(Qt::StrongFocus);
71 m_topLayout = new QVBoxLayout(this);
72
73 connect(this, SIGNAL(signalModeChanged()),
74 mainWindow, SLOT(slotViewModeChanged()));
75 connect(this, SIGNAL(signalShowHiddenFilesChanged()),
76 mainWindow, SLOT(slotShowHiddenFilesChanged()));
77 connect(this, SIGNAL(signalSortingChanged(DolphinView::Sorting)),
78 mainWindow, SLOT(slotSortingChanged(DolphinView::Sorting)));
79 connect(this, SIGNAL(signalSortOrderChanged(Qt::SortOrder)),
80 mainWindow, SLOT(slotSortOrderChanged(Qt::SortOrder)));
81
82 m_urlNavigator = new UrlNavigator(url, this);
83 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
84 this, SLOT(slotUrlChanged(const KUrl&)));
85 connect(m_urlNavigator, SIGNAL(urlChanged(const KUrl&)),
86 mainWindow, SLOT(slotUrlChanged(const KUrl&)));
87 connect(m_urlNavigator, SIGNAL(historyChanged()),
88 mainWindow, SLOT(slotHistoryChanged()));
89
90 m_statusBar = new DolphinStatusBar(this);
91
92 m_dirLister = new DolphinDirLister();
93 m_dirLister->setAutoUpdate(true);
94 m_dirLister->setMainWindow(this);
95 m_dirLister->setShowingDotFiles(showHiddenFiles);
96 connect(m_dirLister, SIGNAL(clear()),
97 this, SLOT(slotClear()));
98 connect(m_dirLister, SIGNAL(percent(int)),
99 this, SLOT(slotPercent(int)));
100 connect(m_dirLister, SIGNAL(deleteItem(KFileItem*)),
101 this, SLOT(slotDeleteItem(KFileItem*)));
102 connect(m_dirLister, SIGNAL(completed()),
103 this, SLOT(slotCompleted()));
104 connect(m_dirLister, SIGNAL(infoMessage(const QString&)),
105 this, SLOT(slotInfoMessage(const QString&)));
106 connect(m_dirLister, SIGNAL(errorMessage(const QString&)),
107 this, SLOT(slotErrorMessage(const QString&)));
108
109 m_iconsView = new DolphinIconsView(this);
110 connect(m_iconsView, SIGNAL(clicked(const QModelIndex&)),
111 this, SLOT(triggerItem(const QModelIndex&)));
112 applyModeToView();
113
114 KDirModel* model = new KDirModel();
115 model->setDirLister(m_dirLister);
116 m_iconsView->setModel(model);
117
118 m_dirLister->setDelayedMimeTypes(true);
119 new KMimeTypeResolver( m_iconsView, model );
120
121 m_iconSize = K3Icon::SizeMedium;
122
123 m_filterBar = new FilterBar(mainWindow, this);
124 m_filterBar->hide();
125 connect(m_filterBar, SIGNAL(signalFilterChanged(const QString&)),
126 this, SLOT(slotChangeNameFilter(const QString&)));
127
128 m_topLayout->addWidget(m_urlNavigator);
129 m_topLayout->addWidget(m_iconsView);
130 m_topLayout->addWidget(m_filterBar);
131 m_topLayout->addWidget(m_statusBar);
132
133 startDirLister(m_urlNavigator->url());
134 }
135
136 DolphinView::~DolphinView()
137 {
138 delete m_dirLister;
139 m_dirLister = 0;
140 }
141
142 void DolphinView::setUrl(const KUrl& url)
143 {
144 m_urlNavigator->setUrl(url);
145 }
146
147 const KUrl& DolphinView::url() const
148 {
149 return m_urlNavigator->url();
150 }
151
152 void DolphinView::requestActivation()
153 {
154 mainWindow()->setActiveView(this);
155 }
156
157 bool DolphinView::isActive() const
158 {
159 return (mainWindow()->activeView() == this);
160 }
161
162 void DolphinView::setMode(Mode mode)
163 {
164 if (mode == m_mode) {
165 return; // the wished mode is already set
166 }
167
168 m_mode = mode;
169
170 ViewProperties props(m_urlNavigator->url());
171 props.setViewMode(m_mode);
172
173 applyModeToView();
174 startDirLister(m_urlNavigator->url());
175
176 emit signalModeChanged();
177 }
178
179 DolphinView::Mode DolphinView::mode() const
180 {
181 return m_mode;
182 }
183
184 void DolphinView::setShowHiddenFilesEnabled(bool show)
185 {
186 if (m_dirLister->showingDotFiles() == show) {
187 return;
188 }
189
190 ViewProperties props(m_urlNavigator->url());
191 props.setShowHiddenFilesEnabled(show);
192 props.save();
193
194 m_dirLister->setShowingDotFiles(show);
195
196 emit signalShowHiddenFilesChanged();
197
198 reload();
199 }
200
201 bool DolphinView::isShowHiddenFilesEnabled() const
202 {
203 return m_dirLister->showingDotFiles();
204 }
205
206 void DolphinView::setViewProperties(const ViewProperties& props)
207 {
208 setMode(props.viewMode());
209 setSorting(props.sorting());
210 setSortOrder(props.sortOrder());
211 setShowHiddenFilesEnabled(props.isShowHiddenFilesEnabled());
212 }
213
214 void DolphinView::renameSelectedItems()
215 {
216 const KUrl::List urls = selectedUrls();
217 if (urls.count() > 1) {
218 // More than one item has been selected for renaming. Open
219 // a rename dialog and rename all items afterwards.
220 RenameDialog dialog(urls);
221 if (dialog.exec() == QDialog::Rejected) {
222 return;
223 }
224
225 DolphinView* view = mainWindow()->activeView();
226 const QString& newName = dialog.newName();
227 if (newName.isEmpty()) {
228 view->statusBar()->setMessage(i18n("The new item name is invalid."),
229 DolphinStatusBar::Error);
230 }
231 else {
232 UndoManager& undoMan = UndoManager::instance();
233 undoMan.beginMacro();
234
235 assert(newName.contains('#'));
236
237 const int urlsCount = urls.count();
238 ProgressIndicator* progressIndicator =
239 new ProgressIndicator(mainWindow(),
240 i18n("Renaming items..."),
241 i18n("Renaming finished."),
242 urlsCount);
243
244 // iterate through all selected items and rename them...
245 const int replaceIndex = newName.indexOf('#');
246 assert(replaceIndex >= 0);
247 for (int i = 0; i < urlsCount; ++i) {
248 const KUrl& source = urls[i];
249 QString name(newName);
250 name.replace(replaceIndex, 1, renameIndexPresentation(i + 1, urlsCount));
251
252 if (source.fileName() != name) {
253 KUrl dest(source.upUrl());
254 dest.addPath(name);
255
256 const bool destExists = KIO::NetAccess::exists(dest, false, view);
257 if (destExists) {
258 delete progressIndicator;
259 progressIndicator = 0;
260 view->statusBar()->setMessage(i18n("Renaming failed (item '%1' already exists).",name),
261 DolphinStatusBar::Error);
262 break;
263 }
264 else if (KIO::NetAccess::file_move(source, dest)) {
265 // TODO: From the users point of view he executed one 'rename n files' operation,
266 // but internally we store it as n 'rename 1 file' operations for the undo mechanism.
267 DolphinCommand command(DolphinCommand::Rename, source, dest);
268 undoMan.addCommand(command);
269 }
270 }
271
272 progressIndicator->execOperation();
273 }
274 delete progressIndicator;
275 progressIndicator = 0;
276
277 undoMan.endMacro();
278 }
279 }
280 else {
281 // Only one item has been selected for renaming. Use the custom
282 // renaming mechanism from the views.
283 assert(urls.count() == 1);
284 // TODO:
285 /*if (m_mode == DetailsView) {
286 Q3ListViewItem* item = m_iconsView->firstChild();
287 while (item != 0) {
288 if (item->isSelected()) {
289 m_iconsView->rename(item, DolphinDetailsView::NameColumn);
290 break;
291 }
292 item = item->nextSibling();
293 }
294 }
295 else {
296 KFileIconViewItem* item = static_cast<KFileIconViewItem*>(m_iconsView->firstItem());
297 while (item != 0) {
298 if (item->isSelected()) {
299 item->rename();
300 break;
301 }
302 item = static_cast<KFileIconViewItem*>(item->nextItem());
303 }
304 }*/
305 }
306 }
307
308 void DolphinView::selectAll()
309 {
310 //fileView()->selectAll();
311 }
312
313 void DolphinView::invertSelection()
314 {
315 //fileView()->invertSelection();
316 }
317
318 DolphinStatusBar* DolphinView::statusBar() const
319 {
320 return m_statusBar;
321 }
322
323 int DolphinView::contentsX() const
324 {
325
326 return 0; //scrollView()->contentsX();
327 }
328
329 int DolphinView::contentsY() const
330 {
331 return 0; //scrollView()->contentsY();
332 }
333
334 void DolphinView::refreshSettings()
335 {
336 startDirLister(m_urlNavigator->url());
337 }
338
339 void DolphinView::updateStatusBar()
340 {
341 // As the item count information is less important
342 // in comparison with other messages, it should only
343 // be shown if:
344 // - the status bar is empty or
345 // - shows already the item count information or
346 // - shows only a not very important information
347 // - if any progress is given don't show the item count info at all
348 const QString msg(m_statusBar->message());
349 const bool updateStatusBarMsg = (msg.isEmpty() ||
350 (msg == m_statusBar->defaultText()) ||
351 (m_statusBar->type() == DolphinStatusBar::Information)) &&
352 (m_statusBar->progress() == 100);
353
354 const QString text(hasSelection() ? selectionStatusBarText() : defaultStatusBarText());
355 m_statusBar->setDefaultText(text);
356
357 if (updateStatusBarMsg) {
358 m_statusBar->setMessage(text, DolphinStatusBar::Default);
359 }
360 }
361
362 void DolphinView::requestItemInfo(const KUrl& url)
363 {
364 emit signalRequestItemInfo(url);
365 }
366
367 bool DolphinView::isUrlEditable() const
368 {
369 return m_urlNavigator->isUrlEditable();
370 }
371
372 void DolphinView::zoomIn()
373 {
374 //itemEffectsManager()->zoomIn();
375 }
376
377 void DolphinView::zoomOut()
378 {
379 //itemEffectsManager()->zoomOut();
380 }
381
382 bool DolphinView::isZoomInPossible() const
383 {
384 return false; //itemEffectsManager()->isZoomInPossible();
385 }
386
387 bool DolphinView::isZoomOutPossible() const
388 {
389 return false; //itemEffectsManager()->isZoomOutPossible();
390 }
391
392 void DolphinView::setSorting(Sorting sorting)
393 {
394 if (sorting != this->sorting()) {
395 /*KFileView* view = fileView();
396 int spec = view->sorting() & ~QDir::Name & ~QDir::Size & ~QDir::Time & ~QDir::Unsorted;
397
398 switch (sorting) {
399 case SortByName: spec = spec | QDir::Name; break;
400 case SortBySize: spec = spec | QDir::Size; break;
401 case SortByDate: spec = spec | QDir::Time; break;
402 default: break;
403 }
404
405 ViewProperties props(url());
406 props.setSorting(sorting);
407
408 view->setSorting(static_cast<QDir::SortFlags>(spec));
409
410 emit signalSortingChanged(sorting);*/
411 }
412 }
413
414 DolphinView::Sorting DolphinView::sorting() const
415 {
416 /*const QDir::SortFlags spec = fileView()->sorting();
417
418 if (spec & QDir::Time) {
419 return SortByDate;
420 }
421
422 if (spec & QDir::Size) {
423 return SortBySize;
424 }*/
425
426 return SortByName;
427 }
428
429 void DolphinView::setSortOrder(Qt::SortOrder order)
430 {
431 if (sortOrder() != order) {
432 /*KFileView* view = fileView();
433 int sorting = view->sorting();
434 sorting = (order == Qt::Ascending) ? (sorting & ~QDir::Reversed) :
435 (sorting | QDir::Reversed);
436
437 ViewProperties props(url());
438 props.setSortOrder(order);
439
440 view->setSorting(static_cast<QDir::SortFlags>(sorting));
441
442 emit signalSortOrderChanged(order);*/
443 }
444 }
445
446 Qt::SortOrder DolphinView::sortOrder() const
447 {
448 //return fileView()->isReversed() ? Qt::Descending : Qt::Ascending;
449 return Qt::Descending;
450 }
451
452 void DolphinView::goBack()
453 {
454 m_urlNavigator->goBack();
455 }
456
457 void DolphinView::goForward()
458 {
459 m_urlNavigator->goForward();
460 }
461
462 void DolphinView::goUp()
463 {
464 m_urlNavigator->goUp();
465 }
466
467 void DolphinView::goHome()
468 {
469 m_urlNavigator->goHome();
470 }
471
472 void DolphinView::setUrlEditable(bool editable)
473 {
474 m_urlNavigator->editUrl(editable);
475 }
476
477 const Q3ValueList<UrlNavigator::HistoryElem> DolphinView::urlHistory(int& index) const
478 {
479 return m_urlNavigator->history(index);
480 }
481
482 bool DolphinView::hasSelection() const
483 {
484 return m_iconsView->selectionModel()->hasSelection();
485 }
486
487 KFileItemList DolphinView::selectedItems() const
488 {
489 QItemSelectionModel* selModel = m_iconsView->selectionModel();
490 assert(selModel != 0);
491
492 KFileItemList itemList;
493 if (selModel->hasSelection()) {
494 KDirModel* dirModel = static_cast<KDirModel*>(m_iconsView->model());
495 const QModelIndexList indexList = selModel->selectedIndexes();
496
497 QModelIndexList::const_iterator end = indexList.end();
498 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
499 KFileItem* item = dirModel->itemForIndex(*it);
500 if (item != 0) {
501 itemList.append(item);
502 }
503 }
504 }
505 return itemList;
506 }
507
508 KUrl::List DolphinView::selectedUrls() const
509 {
510 KUrl::List urls;
511
512 const KFileItemList list = selectedItems();
513 KFileItemList::const_iterator it = list.begin();
514 const KFileItemList::const_iterator end = list.end();
515 while (it != end) {
516 KFileItem* item = *it;
517 urls.append(item->url());
518 ++it;
519 }
520
521 return urls;
522 }
523
524 const KFileItem* DolphinView::currentFileItem() const
525 {
526 return 0; // fileView()->currentFileItem();
527 }
528
529 void DolphinView::openContextMenu(KFileItem* fileInfo, const QPoint& pos)
530 {
531 DolphinContextMenu contextMenu(this, fileInfo, pos);
532 contextMenu.open();
533 }
534
535 void DolphinView::rename(const KUrl& source, const QString& newName)
536 {
537 bool ok = false;
538
539 if (newName.isEmpty() || (source.fileName() == newName)) {
540 return;
541 }
542
543 KUrl dest(source.upUrl());
544 dest.addPath(newName);
545
546 const bool destExists = KIO::NetAccess::exists(dest,
547 false,
548 mainWindow()->activeView());
549 if (destExists) {
550 // the destination already exists, hence ask the user
551 // how to proceed...
552 KIO::RenameDlg renameDialog(this,
553 i18n("File Already Exists"),
554 source.path(),
555 dest.path(),
556 KIO::M_OVERWRITE);
557 switch (renameDialog.exec()) {
558 case KIO::R_OVERWRITE:
559 // the destination should be overwritten
560 ok = KIO::NetAccess::file_move(source, dest, -1, true);
561 break;
562
563 case KIO::R_RENAME: {
564 // a new name for the destination has been used
565 KUrl newDest(renameDialog.newDestUrl());
566 ok = KIO::NetAccess::file_move(source, newDest);
567 break;
568 }
569
570 default:
571 // the renaming operation has been canceled
572 reload();
573 return;
574 }
575 }
576 else {
577 // no destination exists, hence just move the file to
578 // do the renaming
579 ok = KIO::NetAccess::file_move(source, dest);
580 }
581
582 if (ok) {
583 m_statusBar->setMessage(i18n("Renamed file '%1' to '%2'.",source.fileName(), dest.fileName()),
584 DolphinStatusBar::OperationCompleted);
585
586 DolphinCommand command(DolphinCommand::Rename, source, dest);
587 UndoManager::instance().addCommand(command);
588 }
589 else {
590 m_statusBar->setMessage(i18n("Renaming of file '%1' to '%2' failed.",source.fileName(), dest.fileName()),
591 DolphinStatusBar::Error);
592 reload();
593 }
594 }
595
596 void DolphinView::reload()
597 {
598 startDirLister(m_urlNavigator->url(), true);
599 }
600
601 void DolphinView::slotUrlListDropped(QDropEvent* /* event */,
602 const KUrl::List& urls,
603 const KUrl& url)
604 {
605 KUrl destination(url);
606 if (destination.isEmpty()) {
607 destination = m_urlNavigator->url();
608 }
609 else {
610 // Check whether the destination Url is a directory. If this is not the
611 // case, use the navigator Url as destination (otherwise the destination,
612 // which represents a file, would be replaced by a copy- or move-operation).
613 KFileItem fileItem(KFileItem::Unknown, KFileItem::Unknown, destination);
614 if (!fileItem.isDir()) {
615 destination = m_urlNavigator->url();
616 }
617 }
618
619 mainWindow()->dropUrls(urls, destination);
620 }
621
622 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
623 {
624 QWidget::mouseReleaseEvent(event);
625 mainWindow()->setActiveView(this);
626 }
627
628 DolphinMainWindow* DolphinView::mainWindow() const
629 {
630 return m_mainWindow;
631 }
632
633 void DolphinView::slotUrlChanged(const KUrl& url)
634 {
635 const ViewProperties props(url);
636 setMode(props.viewMode());
637
638 const bool showHiddenFiles = props.isShowHiddenFilesEnabled();
639 setShowHiddenFilesEnabled(showHiddenFiles);
640 m_dirLister->setShowingDotFiles(showHiddenFiles);
641
642 setSorting(props.sorting());
643 setSortOrder(props.sortOrder());
644
645 startDirLister(url);
646
647 // The selectionChanged signal is not emitted when a new view object is
648 // created. The application does not care whether a view is represented by a
649 // different instance, hence inform the application that the selection might have
650 // changed so that it can update it's actions.
651 mainWindow()->slotSelectionChanged();
652
653 emit signalUrlChanged(url);
654 }
655
656 void DolphinView::triggerIconsViewItem(Q3IconViewItem* item)
657 {
658 /* KDE4-TODO:
659 const Qt::ButtonState keyboardState = KApplication::keyboardMouseState();
660 const bool isSelectionActive = ((keyboardState & Qt::ShiftModifier) > 0) ||
661 ((keyboardState & Qt::ControlModifier) > 0);*/
662 const bool isSelectionActive = false;
663 if ((item != 0) && !isSelectionActive) {
664 // Updating the Url must be done outside the scope of this slot,
665 // as iconview items will get deleted.
666 QTimer::singleShot(0, this, SLOT(updateUrl()));
667 mainWindow()->setActiveView(this);
668 }
669 }
670
671 void DolphinView::triggerItem(const QModelIndex& index)
672 {
673 KDirModel* dirModel = static_cast<KDirModel*>(m_iconsView->model());
674 KFileItem* item = dirModel->itemForIndex(index);
675 if (item == 0) {
676 return;
677 }
678
679 if (item->isDir()) {
680 // Prefer the local path over the Url. This assures that the
681 // volume space information is correct. Assuming that the Url is media:/sda1,
682 // and the local path is /windows/C: For the Url the space info is related
683 // to the root partition (and hence wrong) and for the local path the space
684 // info is related to the windows partition (-> correct).
685 //m_dirLister->stop();
686 //m_dirLister->openUrl(item->url());
687 //return;
688
689 const QString localPath(item->localPath());
690 if (localPath.isEmpty()) {
691 setUrl(item->url());
692 }
693 else {
694 setUrl(KUrl(localPath));
695 }
696 }
697 else {
698 item->run();
699 }
700 }
701
702 void DolphinView::updateUrl()
703 {
704 //KFileView* fileView = (m_iconsView != 0) ? static_cast<KFileView*>(m_iconsView) :
705 // static_cast<KFileView*>(m_iconsView);
706
707 KFileItem* fileItem = 0; // TODO: fileView->currentFileItem();
708 if (fileItem == 0) {
709 return;
710 }
711
712 if (fileItem->isDir()) {
713 // Prefer the local path over the Url. This assures that the
714 // volume space information is correct. Assuming that the Url is media:/sda1,
715 // and the local path is /windows/C: For the Url the space info is related
716 // to the root partition (and hence wrong) and for the local path the space
717 // info is related to the windows partition (-> correct).
718 const QString localPath(fileItem->localPath());
719 if (localPath.isEmpty()) {
720 setUrl(fileItem->url());
721 }
722 else {
723 setUrl(KUrl(localPath));
724 }
725 }
726 else {
727 fileItem->run();
728 }
729 }
730
731 void DolphinView::slotPercent(int percent)
732 {
733 if (m_showProgress) {
734 m_statusBar->setProgress(percent);
735 }
736 }
737
738 void DolphinView::slotClear()
739 {
740 //fileView()->clearView();
741 updateStatusBar();
742 }
743
744 void DolphinView::slotDeleteItem(KFileItem* item)
745 {
746 //fileView()->removeItem(item);
747 updateStatusBar();
748 }
749
750 void DolphinView::slotCompleted()
751 {
752 m_refreshing = true;
753
754 //KFileView* view = fileView();
755 //view->clearView();
756
757 // TODO: in Qt4 the code should get a lot
758 // simpler and nicer due to Interview...
759 /*if (m_iconsView != 0) {
760 m_iconsView->beginItemUpdates();
761 }
762 if (m_iconsView != 0) {
763 m_iconsView->beginItemUpdates();
764 }*/
765
766 if (m_showProgress) {
767 m_statusBar->setProgressText(QString::null);
768 m_statusBar->setProgress(100);
769 m_showProgress = false;
770 }
771
772 KFileItemList items(m_dirLister->items());
773 KFileItemList::const_iterator it = items.begin();
774 const KFileItemList::const_iterator end = items.end();
775
776 m_fileCount = 0;
777 m_folderCount = 0;
778
779 while (it != end) {
780 KFileItem* item = *it;
781 //view->insertItem(item);
782 if (item->isDir()) {
783 ++m_folderCount;
784 }
785 else {
786 ++m_fileCount;
787 }
788 ++it;
789 }
790
791 updateStatusBar();
792
793 /*if (m_iconsView != 0) {
794 // Prevent a flickering of the icon view widget by giving a small
795 // timeslot to swallow asynchronous update events.
796 m_iconsView->setUpdatesEnabled(false);
797 QTimer::singleShot(10, this, SLOT(slotDelayedUpdate()));
798 }
799
800 if (m_iconsView != 0) {
801 m_iconsView->endItemUpdates();
802 m_refreshing = false;
803 }*/
804 }
805
806 void DolphinView::slotInfoMessage(const QString& msg)
807 {
808 m_statusBar->setMessage(msg, DolphinStatusBar::Information);
809 }
810
811 void DolphinView::slotErrorMessage(const QString& msg)
812 {
813 m_statusBar->setMessage(msg, DolphinStatusBar::Error);
814 }
815
816 void DolphinView::slotGrabActivation()
817 {
818 mainWindow()->setActiveView(this);
819 }
820
821 void DolphinView::slotContentsMoving(int x, int y)
822 {
823 if (!m_refreshing) {
824 // Only emit a 'contents moved' signal if the user
825 // moved the content by adjusting the sliders. Adjustments
826 // resulted by refreshing a directory should not be respected.
827 emit contentsMoved(x, y);
828 }
829 }
830
831 /*KFileView* DolphinView::fileView() const
832 {
833 return (m_mode == DetailsView) ? static_cast<KFileView*>(m_iconsView) :
834 static_cast<KFileView*>(m_iconsView);
835 }*/
836
837 Q3ScrollView* DolphinView::scrollView() const
838 {
839 return 0; //(m_mode == DetailsView) ? static_cast<Q3ScrollView*>(m_iconsView) :
840 // static_cast<Q3ScrollView*>(m_iconsView);
841 }
842
843 ItemEffectsManager* DolphinView::itemEffectsManager() const
844 {
845 return 0;
846 }
847
848 void DolphinView::startDirLister(const KUrl& url, bool reload)
849 {
850 if (!url.isValid()) {
851 const QString location(url.pathOrUrl());
852 if (location.isEmpty()) {
853 m_statusBar->setMessage(i18n("The location is empty."), DolphinStatusBar::Error);
854 }
855 else {
856 m_statusBar->setMessage(i18n("The location '%1' is invalid.",location),
857 DolphinStatusBar::Error);
858 }
859 return;
860 }
861
862 // Only show the directory loading progress if the status bar does
863 // not contain another progress information. This means that
864 // the directory loading progress information has the lowest priority.
865 const QString progressText(m_statusBar->progressText());
866 m_showProgress = progressText.isEmpty() ||
867 (progressText == i18n("Loading directory..."));
868 if (m_showProgress) {
869 m_statusBar->setProgressText(i18n("Loading directory..."));
870 m_statusBar->setProgress(0);
871 }
872
873 m_refreshing = true;
874 m_dirLister->stop();
875 m_dirLister->openUrl(url, false, reload);
876 }
877
878 QString DolphinView::defaultStatusBarText() const
879 {
880 // TODO: the following code is not suitable for languages where multiple forms
881 // of plurals are given (e. g. in Poland three forms of plurals exist).
882 const int itemCount = m_folderCount + m_fileCount;
883
884 QString text;
885 if (itemCount == 1) {
886 text = i18n("1 Item");
887 }
888 else {
889 text = i18n("%1 Items",itemCount);
890 }
891
892 text += " (";
893
894 if (m_folderCount == 1) {
895 text += i18n("1 Folder");
896 }
897 else {
898 text += i18n("%1 Folders",m_folderCount);
899 }
900
901 text += ", ";
902
903 if (m_fileCount == 1) {
904 text += i18n("1 File");
905 }
906 else {
907 text += i18n("%1 Files",m_fileCount);
908 }
909
910 text += ")";
911
912 return text;
913 }
914
915 QString DolphinView::selectionStatusBarText() const
916 {
917 // TODO: the following code is not suitable for languages where multiple forms
918 // of plurals are given (e. g. in Poland three forms of plurals exist).
919 QString text;
920 const KFileItemList list = selectedItems();
921 if (list.isEmpty()) {
922 // TODO: assert(!list.isEmpty()) should be used, as this method is only invoked if
923 // DolphinView::hasSelection() is true. Inconsistent behavior?
924 return QString();
925 }
926
927 int fileCount = 0;
928 int folderCount = 0;
929 KIO::filesize_t byteSize = 0;
930 KFileItemList::const_iterator it = list.begin();
931 const KFileItemList::const_iterator end = list.end();
932 while (it != end){
933 KFileItem* item = *it;
934 if (item->isDir()) {
935 ++folderCount;
936 }
937 else {
938 ++fileCount;
939 byteSize += item->size();
940 }
941 ++it;
942 }
943
944 if (folderCount == 1) {
945 text = i18n("1 Folder selected");
946 }
947 else if (folderCount > 1) {
948 text = i18n("%1 Folders selected",folderCount);
949 }
950
951 if ((fileCount > 0) && (folderCount > 0)) {
952 text += ", ";
953 }
954
955 const QString sizeText(KIO::convertSize(byteSize));
956 if (fileCount == 1) {
957 text += i18n("1 File selected (%1)",sizeText);
958 }
959 else if (fileCount > 1) {
960 text += i18n("%1 Files selected (%1)",fileCount,sizeText);
961 }
962
963 return text;
964 }
965
966 QString DolphinView::renameIndexPresentation(int index, int itemCount) const
967 {
968 // assure that the string reprentation for all indicess have the same
969 // number of characters based on the given number of items
970 QString str(QString::number(index));
971 int chrCount = 1;
972 while (itemCount >= 10) {
973 ++chrCount;
974 itemCount /= 10;
975 }
976 str.reserve(chrCount);
977
978 const int insertCount = chrCount - str.length();
979 for (int i = 0; i < insertCount; ++i) {
980 str.insert(0, '0');
981 }
982 return str;
983 }
984
985 void DolphinView::slotShowFilterBar(bool show)
986 {
987 assert(m_filterBar != 0);
988 if (show) {
989 m_filterBar->show();
990 }
991 else {
992 m_filterBar->hide();
993 }
994 }
995
996 void DolphinView::declareViewActive()
997 {
998 mainWindow()->setActiveView( this );
999 }
1000
1001 void DolphinView::slotChangeNameFilter(const QString& nameFilter)
1002 {
1003 // The name filter of KDirLister does a 'hard' filtering, which
1004 // means that only the items are shown where the names match
1005 // exactly the filter. This is non-transparent for the user, which
1006 // just wants to have a 'soft' filtering: does the name contain
1007 // the filter string?
1008 QString adjustedFilter(nameFilter);
1009 adjustedFilter.insert(0, '*');
1010 adjustedFilter.append('*');
1011
1012 m_dirLister->setNameFilter(adjustedFilter);
1013 m_dirLister->emitChanges();
1014
1015 // TODO: this is a workaround for QIconView: the item position
1016 // stay as they are by filtering, only an inserting of an item
1017 // results to an automatic adjusting of the item position. In Qt4/KDE4
1018 // this workaround should get obsolete due to Interview.
1019 /*KFileView* view = fileView();
1020 if (view == m_iconsView) {
1021 KFileItem* first = view->firstFileItem();
1022 if (first != 0) {
1023 view->removeItem(first);
1024 view->insertItem(first);
1025 }
1026 }*/
1027 }
1028
1029 bool DolphinView::isFilterBarVisible()
1030 {
1031 return m_filterBar->isVisible();
1032 }
1033
1034 void DolphinView::applyModeToView()
1035 {
1036 // TODO: the following code just tries to test some QListView capabilities
1037 switch (m_mode) {
1038 case IconsView:
1039 m_iconsView->setViewMode(QListView::IconMode);
1040 m_iconsView->setGridSize(QSize(128, 64));
1041 break;
1042
1043 case DetailsView:
1044 m_iconsView->setViewMode(QListView::ListMode);
1045 m_iconsView->setGridSize(QSize(256, 24));
1046 break;
1047
1048 case PreviewsView:
1049 m_iconsView->setViewMode(QListView::IconMode);
1050 m_iconsView->setGridSize(QSize(128, 128));
1051 break;
1052 }
1053 }
1054
1055 #include "dolphinview.moc"