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#ifndef DOLPHINVIEW_H
22#define DOLPHINVIEW_H
23
24#include <config-baloo.h>
25
26#include "libdolphin_export.h"
27
28#include <kparts/part.h>
29#include <KFileItem>
30#include <KFileItemDelegate>
31#include <kio/fileundomanager.h>
32#include <KIO/Job>
33
34#include <QBoxLayout>
35#include <QKeyEvent>
36#include <QLinkedList>
37#include <QWidget>
38
39typedef KIO::FileUndoManager::CommandType CommandType;
40
41class DolphinItemListView;
42class KAction;
43class KActionCollection;
44class KFileItemModel;
45class KItemListContainer;
46class KItemModelBase;
47class KItemSet;
48class KUrl;
49class ToolTipManager;
50class VersionControlObserver;
51class ViewProperties;
52class QGraphicsSceneDragDropEvent;
53class QRegExp;
54
55/**
56 * @short Represents a view for the directory content.
57 *
58 * View modes for icons, compact and details are supported. It's
59 * possible to adjust:
60 * - sort order
61 * - sort type
62 * - show hidden files
63 * - show previews
64 * - enable grouping
65 */
66class LIBDOLPHINPRIVATE_EXPORT DolphinView : public QWidget
67{
68 Q_OBJECT
69
70public:
71 /**
72 * Defines the view mode for a directory. The
73 * view mode is automatically updated if the directory itself
74 * defines a view mode (see class ViewProperties for details).
75 */
76 enum Mode
77 {
78 /**
79 * The items are shown as icons with a name-label below.
80 */
81 IconsView = 0,
82
83 /**
84 * The icon, the name and the size of the items are
85 * shown per default as a table.
86 */
87 DetailsView,
88
89 /**
90 * The items are shown as icons with the name-label aligned
91 * to the right side.
92 */
93 CompactView
94 };
95
96 /**
97 * @param url Specifies the content which should be shown.
98 * @param parent Parent widget of the view.
99 */
100 DolphinView(const KUrl& url, QWidget* parent);
101
102 virtual ~DolphinView();
103
104 /**
105 * Returns the current active URL, where all actions are applied.
106 * The URL navigator is synchronized with this URL.
107 */
108 KUrl url() const;
109
110 /**
111 * If \a active is true, the view will marked as active. The active
112 * view is defined as view where all actions are applied to.
113 */
114 void setActive(bool active);
115 bool isActive() const;
116
117 /**
118 * Changes the view mode for the current directory to \a mode.
119 * If the view properties should be remembered for each directory
120 * (GeneralSettings::globalViewProps() returns false), then the
121 * changed view mode will be stored automatically.
122 */
123 void setMode(Mode mode);
124 Mode mode() const;
125
126 /**
127 * Turns on the file preview for the all files of the current directory,
128 * if \a show is true.
129 * If the view properties should be remembered for each directory
130 * (GeneralSettings::globalViewProps() returns false), then the
131 * preview setting will be stored automatically.
132 */
133 void setPreviewsShown(bool show);
134 bool previewsShown() const;
135
136 /**
137 * Shows all hidden files of the current directory,
138 * if \a show is true.
139 * If the view properties should be remembered for each directory
140 * (GeneralSettings::globalViewProps() returns false), then the
141 * show hidden file setting will be stored automatically.
142 */
143 void setHiddenFilesShown(bool show);
144 bool hiddenFilesShown() const;
145
146 /**
147 * Turns on sorting by groups if \a enable is true.
148 */
149 void setGroupedSorting(bool grouped);
150 bool groupedSorting() const;
151
152 /**
153 * Returns the items of the view.
154 */
155 KFileItemList items() const;
156
157 /**
158 * @return The number of items. itemsCount() is faster in comparison
159 * to items().count().
160 */
161 int itemsCount() const;
162
163 /**
164 * Returns the selected items. The list is empty if no item has been
165 * selected.
166 */
167 KFileItemList selectedItems() const;
168
169 /**
170 * Returns the number of selected items (this is faster than
171 * invoking selectedItems().count()).
172 */
173 int selectedItemsCount() const;
174
175 /**
176 * Marks the items indicated by \p urls to get selected after the
177 * directory DolphinView::url() has been loaded. Note that nothing
178 * gets selected if no loading of a directory has been triggered
179 * by DolphinView::setUrl() or DolphinView::reload().
180 */
181 void markUrlsAsSelected(const QList<KUrl>& urls);
182
183 /**
184 * Marks the item indicated by \p url to be scrolled to and as the
185 * current item after directory DolphinView::url() has been loaded.
186 */
187 void markUrlAsCurrent(const KUrl& url);
188
189 /**
190 * All items that match to the pattern \a pattern will get selected
191 * if \a enabled is true and deselected if \a enabled is false.
192 */
193 void selectItems(const QRegExp& pattern, bool enabled);
194
195 /**
196 * Sets the zoom level to \a level. It is assured that the used
197 * level is adjusted to be inside the range ZoomLevelInfo::minimumLevel() and
198 * ZoomLevelInfo::maximumLevel().
199 */
200 void setZoomLevel(int level);
201 int zoomLevel() const;
202
203 void setSortRole(const QByteArray& role);
204 QByteArray sortRole() const;
205
206 void setSortOrder(Qt::SortOrder order);
207 Qt::SortOrder sortOrder() const;
208
209 /** Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false). */
210 void setSortFoldersFirst(bool foldersFirst);
211 bool sortFoldersFirst() const;
212
213 /** Sets the additional information which should be shown for the items. */
214 void setVisibleRoles(const QList<QByteArray>& roles);
215
216 /** Returns the additional information which should be shown for the items. */
217 QList<QByteArray> visibleRoles() const;
218
219 void reload();
220
221 /**
222 * Refreshes the view to get synchronized with the settings (e.g. icons size,
223 * font, ...).
224 */
225 void readSettings();
226
227 /**
228 * Saves the current settings (e.g. icons size, font, ..).
229 */
230 void writeSettings();
231
232 /**
233 * Filters the currently shown items by \a nameFilter. All items
234 * which contain the given filter string will be shown.
235 */
236 void setNameFilter(const QString& nameFilter);
237 QString nameFilter() const;
238
239 /**
240 * Filters the currently shown items by \a filters. All items
241 * whose content-type matches those given by the list of filters
242 * will be shown.
243 */
244 void setMimeTypeFilters(const QStringList& filters);
245 QStringList mimeTypeFilters() const;
246
247 /**
248 * Returns a textual representation of the state of the current
249 * folder or selected items, suitable for use in the status bar.
250 */
251 QString statusBarText() const;
252
253 /**
254 * Returns the version control actions that are provided for the items \p items.
255 * Usually the actions are presented in the context menu.
256 */
257 QList<QAction*> versionControlActions(const KFileItemList& items) const;
258
259 /**
260 * Returns the state of the paste action:
261 * first is whether the action should be enabled
262 * second is the text for the action
263 */
264 QPair<bool, QString> pasteInfo() const;
265
266 /**
267 * If \a tabsForFiles is true, the signal tabRequested() will also
268 * emitted also for files. Per default tabs for files is disabled
269 * and hence the signal tabRequested() will only be emitted for
270 * directories.
271 */
272 void setTabsForFilesEnabled(bool tabsForFiles);
273 bool isTabsForFilesEnabled() const;
274
275 /**
276 * Returns true if the current view allows folders to be expanded,
277 * i.e. presents a hierarchical view to the user.
278 */
279 bool itemsExpandable() const;
280
281 /**
282 * Restores the view state (current item, contents position, details view expansion state)
283 */
284 void restoreState(QDataStream& stream);
285
286 /**
287 * Saves the view state (current item, contents position, details view expansion state)
288 */
289 void saveState(QDataStream& stream);
290
291 /**
292 * Returns the root item which represents the current URL.
293 */
294 KFileItem rootItem() const;
295
296 /**
297 * Sets a context that is used for remembering the view-properties.
298 * Per default the context is empty and the path of the currently set URL
299 * is used for remembering the view-properties. Setting a custom context
300 * makes sense if specific types of URLs (e.g. search-URLs) should
301 * share common view-properties.
302 */
303 void setViewPropertiesContext(const QString& context);
304 QString viewPropertiesContext() const;
305
306 /**
307 * Checks if the given \a item can be opened as folder (e.g. archives).
308 * This function will also adjust the \a url (e.g. change the protocol).
309 * @return a valid and adjusted url if the item can be opened as folder,
310 * otherwise return an empty url.
311 */
312 static KUrl openItemAsFolderUrl(const KFileItem& item, const bool browseThroughArchives = true);
313
314public slots:
315 /**
316 * Changes the directory to \a url. If the current directory is equal to
317 * \a url, nothing will be done (use DolphinView::reload() instead).
318 */
319 void setUrl(const KUrl& url);
320
321 /**
322 * Selects all items.
323 * @see DolphinView::selectedItems()
324 */
325 void selectAll();
326
327 /**
328 * Inverts the current selection: selected items get unselected,
329 * unselected items get selected.
330 * @see DolphinView::selectedItems()
331 */
332 void invertSelection();
333
334 void clearSelection();
335
336 /**
337 * Triggers the renaming of the currently selected items, where
338 * the user must input a new name for the items.
339 */
340 void renameSelectedItems();
341
342 /**
343 * Moves all selected items to the trash.
344 */
345 void trashSelectedItems();
346
347 /**
348 * Deletes all selected items.
349 */
350 void deleteSelectedItems();
351
352 /**
353 * Copies all selected items to the clipboard and marks
354 * the items as cut.
355 */
356 void cutSelectedItems();
357
358 /** Copies all selected items to the clipboard. */
359 void copySelectedItems();
360
361 /** Pastes the clipboard data to this view. */
362 void paste();
363
364 /**
365 * Pastes the clipboard data into the currently selected
366 * folder. If the current selection is not exactly one folder, no
367 * paste operation is done.
368 */
369 void pasteIntoFolder();
370
371 void stopLoading();
372
373 /** Activates the view if the item list container gets focus. */
374 virtual bool eventFilter(QObject* watched, QEvent* event);
375
376signals:
377 /**
378 * Is emitted if the view has been activated by e. g. a mouse click.
379 */
380 void activated();
381
382 /**
383 * Is emitted if the URL of the view will be changed to \a url.
384 * After the URL has been changed the signal urlChanged() will
385 * be emitted.
386 */
387 void urlAboutToBeChanged(const KUrl& url);
388
389 /** Is emitted if the URL of the view has been changed to \a url. */
390 void urlChanged(const KUrl& url);
391
392 /**
393 * Is emitted when clicking on an item with the left mouse button.
394 */
395 void itemActivated(const KFileItem& item);
396
397 /**
398 * Is emitted when multiple items have been activated by e. g.
399 * context menu open with.
400 */
401 void itemsActivated(const KFileItemList& items);
402
403 /**
404 * Is emitted if items have been added or deleted.
405 */
406 void itemCountChanged();
407
408 /**
409 * Is emitted if a new tab should be opened for the URL \a url.
410 */
411 void tabRequested(const KUrl& url);
412
413 /**
414 * Is emitted if the view mode (IconsView, DetailsView,
415 * PreviewsView) has been changed.
416 */
417 void modeChanged(DolphinView::Mode current, DolphinView::Mode previous);
418
419 /** Is emitted if the 'show preview' property has been changed. */
420 void previewsShownChanged(bool shown);
421
422 /** Is emitted if the 'show hidden files' property has been changed. */
423 void hiddenFilesShownChanged(bool shown);
424
425 /** Is emitted if the 'grouped sorting' property has been changed. */
426 void groupedSortingChanged(bool groupedSorting);
427
428 /** Is emitted if the sorting by name, size or date has been changed. */
429 void sortRoleChanged(const QByteArray& role);
430
431 /** Is emitted if the sort order (ascending or descending) has been changed. */
432 void sortOrderChanged(Qt::SortOrder order);
433
434 /**
435 * Is emitted if the sorting of files and folders (separate with folders
436 * first or mixed) has been changed.
437 */
438 void sortFoldersFirstChanged(bool foldersFirst);
439
440 /** Is emitted if the additional information shown for this view has been changed. */
441 void visibleRolesChanged(const QList<QByteArray>& current,
442 const QList<QByteArray>& previous);
443
444 /** Is emitted if the zoom level has been changed by zooming in or out. */
445 void zoomLevelChanged(int current, int previous);
446
447 /**
448 * Is emitted if information of an item is requested to be shown e. g. in the panel.
449 * If item is null, no item information request is pending.
450 */
451 void requestItemInfo(const KFileItem& item);
452
453 /**
454 * Is emitted whenever the selection has been changed.
455 */
456 void selectionChanged(const KFileItemList& selection);
457
458 /**
459 * Is emitted if a context menu is requested for the item \a item,
460 * which is part of \a url. If the item is null, the context menu
461 * for the URL should be shown and the custom actions \a customActions
462 * will be added.
463 */
464 void requestContextMenu(const QPoint& pos,
465 const KFileItem& item,
466 const KUrl& url,
467 const QList<QAction*>& customActions);
468
469 /**
470 * Is emitted if an information message with the content \a msg
471 * should be shown.
472 */
473 void infoMessage(const QString& msg);
474
475 /**
476 * Is emitted if an error message with the content \a msg
477 * should be shown.
478 */
479 void errorMessage(const QString& msg);
480
481 /**
482 * Is emitted if an "operation completed" message with the content \a msg
483 * should be shown.
484 */
485 void operationCompletedMessage(const QString& msg);
486
487 /**
488 * Is emitted after DolphinView::setUrl() has been invoked and
489 * the current directory is loaded. If this signal is emitted,
490 * it is assured that the view contains already the correct root
491 * URL and property settings.
492 */
493 void directoryLoadingStarted();
494
495 /**
496 * Is emitted after the directory triggered by DolphinView::setUrl()
497 * has been loaded.
498 */
499 void directoryLoadingCompleted();
500
501 /**
502 * Is emitted after the directory loading triggered by DolphinView::setUrl()
503 * has been canceled.
504 */
505 void directoryLoadingCanceled();
506
507 /**
508 * Is emitted after DolphinView::setUrl() has been invoked and provides
509 * the information how much percent of the current directory have been loaded.
510 */
511 void directoryLoadingProgress(int percent);
512
513 /**
514 * Is emitted if the sorting is done asynchronously and provides the
515 * progress information of the sorting.
516 */
517 void directorySortingProgress(int percent);
518
519 /**
520 * Emitted when the file-item-model emits redirection.
521 * Testcase: fish://localhost
522 */
523 void redirection(const KUrl& oldUrl, const KUrl& newUrl);
524
525 /**
526 * Is emitted when the URL set by DolphinView::setUrl() represents a file.
527 * In this case no signal errorMessage() will be emitted.
528 */
529 void urlIsFileError(const KUrl& url);
530
531 /**
532 * Is emitted when the write state of the folder has been changed. The application
533 * should disable all actions like "Create New..." that depend on the write
534 * state.
535 */
536 void writeStateChanged(bool isFolderWritable);
537
538 /**
539 * Is emitted if the URL should be changed to the previous URL of the
540 * history (e.g. because the "back"-mousebutton has been pressed).
541 */
542 void goBackRequested();
543
544 /**
545 * Is emitted if the URL should be changed to the next URL of the
546 * history (e.g. because the "next"-mousebutton has been pressed).
547 */
548 void goForwardRequested();
549
550protected:
551 /** Changes the zoom level if Control is pressed during a wheel event. */
552 virtual void wheelEvent(QWheelEvent* event);
553
554 /** @reimp */
555 virtual void hideEvent(QHideEvent* event);
556 virtual bool event(QEvent* event);
557
558private slots:
559 /**
560 * Marks the view as active (DolphinView:isActive() will return true)
561 * and emits the 'activated' signal if it is not already active.
562 */
563 void activate();
564
565 void slotItemActivated(int index);
566 void slotItemsActivated(const KItemSet& indexes);
567 void slotItemMiddleClicked(int index);
568 void slotItemContextMenuRequested(int index, const QPointF& pos);
569 void slotViewContextMenuRequested(const QPointF& pos);
570 void slotHeaderContextMenuRequested(const QPointF& pos);
571 void slotHeaderColumnWidthChanged(const QByteArray& role, qreal current, qreal previous);
572 void slotItemHovered(int index);
573 void slotItemUnhovered(int index);
574 void slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* event);
575 void slotModelChanged(KItemModelBase* current, KItemModelBase* previous);
576 void slotMouseButtonPressed(int itemIndex, Qt::MouseButtons buttons);
577
578 /*
579 * Is called when new items get pasted or dropped.
580 */
581 void slotAboutToCreate(const KUrl::List& urls);
582
583 /**
584 * Emits the signal \a selectionChanged() with a small delay. This is
585 * because getting all file items for the selection can be an expensive
586 * operation. Fast selection changes are collected in this case and
587 * the signal is emitted only after no selection change has been done
588 * within a small delay.
589 */
590 void slotSelectionChanged(const KItemSet& current, const KItemSet& previous);
591
592 /**
593 * Is called by emitDelayedSelectionChangedSignal() and emits the
594 * signal \a selectionChanged() with all selected file items as parameter.
595 */
596 void emitSelectionChangedSignal();
597
598 /**
599 * Updates the view properties of the current URL to the
600 * sorting given by \a role.
601 */
602 void updateSortRole(const QByteArray& role);
603
604 /**
605 * Updates the view properties of the current URL to the
606 * sort order given by \a order.
607 */
608 void updateSortOrder(Qt::SortOrder order);
609
610 /**
611 * Updates the view properties of the current URL to the
612 * sorting of files and folders (separate with folders first or mixed) given by \a foldersFirst.
613 */
614 void updateSortFoldersFirst(bool foldersFirst);
615
616 /**
617 * Indicates in the status bar that the delete operation
618 * of the job \a job has been finished.
619 */
620 void slotDeleteFileFinished(KJob* job);
621
622 void slotRenamingFailed(const KUrl& oldUrl, const KUrl& newUrl);
623
624 /**
625 * Invoked when the file item model has started the loading
626 * of the directory specified by DolphinView::url().
627 */
628 void slotDirectoryLoadingStarted();
629
630 /**
631 * Invoked when the file item model indicates that the loading of a directory has
632 * been completed. Assures that pasted items and renamed items get seleced.
633 */
634 void slotDirectoryLoadingCompleted();
635
636 /**
637 * Is invoked when items of KFileItemModel have been changed.
638 */
639 void slotItemsChanged();
640
641 /**
642 * Is invoked when the sort order has been changed by the user by clicking
643 * on a header item. The view properties of the directory will get updated.
644 */
645 void slotSortOrderChangedByHeader(Qt::SortOrder current, Qt::SortOrder previous);
646
647 /**
648 * Is invoked when the sort role has been changed by the user by clicking
649 * on a header item. The view properties of the directory will get updated.
650 */
651 void slotSortRoleChangedByHeader(const QByteArray& current, const QByteArray& previous);
652
653 /**
654 * Is invoked when the visible roles have been changed by the user by dragging
655 * a header item. The view properties of the directory will get updated.
656 */
657 void slotVisibleRolesChangedByHeader(const QList<QByteArray>& current,
658 const QList<QByteArray>& previous);
659
660 void slotRoleEditingCanceled();
661 void slotRoleEditingFinished(int index, const QByteArray& role, const QVariant& value);
662
663 /**
664 * Observes the item with the URL \a url. As soon as the directory
665 * model indicates that the item is available, the item will
666 * get selected and it is assured that the item stays visible.
667 */
668 void observeCreatedItem(const KUrl& url);
669
670 /**
671 * Called when a redirection happens.
672 * Testcase: fish://localhost
673 */
674 void slotDirectoryRedirection(const KUrl& oldUrl, const KUrl& newUrl);
675
676 /**
677 * Applies the state that has been restored by restoreViewState()
678 * to the view.
679 */
680 void updateViewState();
681
682 void hideToolTip();
683
684 /**
685 * Calculates the number of currently shown files into
686 * \a fileCount and the number of folders into \a folderCount.
687 * The size of all files is written into \a totalFileSize.
688 * It is recommend using this method instead of asking the
689 * directory lister or the model directly, as it takes
690 * filtering and hierarchical previews into account.
691 */
692 void calculateItemCount(int& fileCount, int& folderCount, KIO::filesize_t& totalFileSize) const;
693
694private:
695 void loadDirectory(const KUrl& url, bool reload = false);
696
697 /**
698 * Applies the view properties which are defined by the current URL
699 * to the DolphinView properties. The view properties are read from a
700 * .directory file either in the current directory, or in the
701 * share/apps/dolphin/view_properties/ subfolder of the user's .kde folder.
702 */
703 void applyViewProperties();
704
705 /**
706 * Applies the given view properties to the DolphinView.
707 */
708 void applyViewProperties(const ViewProperties& props);
709
710 /**
711 * Applies the m_mode property to the corresponding
712 * itemlayout-property of the KItemListView.
713 */
714 void applyModeToView();
715
716 /**
717 * Helper method for DolphinView::paste() and DolphinView::pasteIntoFolder().
718 * Pastes the clipboard data into the URL \a url.
719 */
720 void pasteToUrl(const KUrl& url);
721
722 /**
723 * Returns a list of URLs for all selected items. The list is
724 * simplified, so that when the URLs are part of different tree
725 * levels, only the parent is returned.
726 */
727 KUrl::List simplifiedSelectedUrls() const;
728
729 /**
730 * Returns the MIME data for all selected items.
731 */
732 QMimeData* selectionMimeData() const;
733
734 /**
735 * Updates m_isFolderWritable dependent on whether the folder represented by
736 * the current URL is writable. If the state has changed, the signal
737 * writeableStateChanged() will be emitted.
738 */
739 void updateWritableState();
740
741 /**
742 * @return The current URL if no viewproperties-context is given (see
743 * DolphinView::viewPropertiesContext(), otherwise the context
744 * is returned.
745 */
746 KUrl viewPropertiesUrl() const;
747
748private:
749 bool m_active;
750 bool m_tabsForFiles;
751 bool m_assureVisibleCurrentIndex;
752 bool m_isFolderWritable;
753 bool m_dragging; // True if a dragging is done. Required to be able to decide whether a
754 // tooltip may be shown when hovering an item.
755
756 KUrl m_url;
757 QString m_viewPropertiesContext;
758 Mode m_mode;
759 QList<QByteArray> m_visibleRoles;
760
761 QVBoxLayout* m_topLayout;
762
763 KFileItemModel* m_model;
764 DolphinItemListView* m_view;
765 KItemListContainer* m_container;
766
767 ToolTipManager* m_toolTipManager;
768
769 QTimer* m_selectionChangedTimer;
770
771 KUrl m_currentItemUrl; // Used for making the view to remember the current URL after F5
772 bool m_scrollToCurrentItem; // Used for marking we need to scroll to current item or not
773 QPoint m_restoredContentsPosition;
774
775 QList<KUrl> m_selectedUrls; // Used for making the view to remember selections after F5
776 bool m_clearSelectionBeforeSelectingNewItems;
777 bool m_markFirstNewlySelectedItemAsCurrent;
778
779 VersionControlObserver* m_versionControlObserver;
780
781 // For unit tests
782 friend class TestBase;
783 friend class DolphinDetailsViewTest;
784 friend class DolphinPart; // Accesses m_model
785};
786
787/// Allow using DolphinView::Mode in QVariant
788Q_DECLARE_METATYPE(DolphinView::Mode)
789
790#endif // DOLPHINVIEW_H
791