File indexing completed on 2024-04-28 05:45:26

0001 /*
0002  * SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz19@gmail.com>
0003  * SPDX-FileCopyrightText: 2006 Aaron J. Seigo <aseigo@kde.org>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #ifndef VIEWPROPERTIES_H
0009 #define VIEWPROPERTIES_H
0010 
0011 #include "dolphin_export.h"
0012 #include "views/dolphinview.h"
0013 
0014 #include <QUrl>
0015 
0016 class ViewPropertySettings;
0017 /**
0018  * @brief Maintains the view properties like 'view mode' or
0019  *        'show hidden files' for a directory.
0020  *
0021  * The view properties are automatically stored as part of the file
0022  * .directory inside the corresponding path. To read out the view properties
0023  * just construct an instance by passing the path of the directory:
0024  *
0025  * \code
0026  * ViewProperties props(QUrl::fromLocalFile("/home/peter/Documents"));
0027  * const DolphinView::Mode mode = props.viewMode();
0028  * const bool hiddenFilesShown = props.hiddenFilesShown();
0029  * \endcode
0030  *
0031  * When modifying a view property, the '.directory' file is automatically updated
0032  * inside the destructor.
0033  *
0034  * If no .directory file is available or the global view mode is turned on
0035  * (see GeneralSettings::globalViewMode()), the values from the global .directory file
0036  * are used for initialization.
0037  */
0038 class DOLPHIN_EXPORT ViewProperties
0039 {
0040 public:
0041     explicit ViewProperties(const QUrl &url);
0042     virtual ~ViewProperties();
0043 
0044     void setViewMode(DolphinView::Mode mode);
0045     DolphinView::Mode viewMode() const;
0046 
0047     void setPreviewsShown(bool show);
0048     bool previewsShown() const;
0049 
0050     void setHiddenFilesShown(bool show);
0051     bool hiddenFilesShown() const;
0052 
0053     void setGroupedSorting(bool grouped);
0054     bool groupedSorting() const;
0055 
0056     void setSortRole(const QByteArray &role);
0057     QByteArray sortRole() const;
0058 
0059     void setSortOrder(Qt::SortOrder sortOrder);
0060     Qt::SortOrder sortOrder() const;
0061 
0062     void setSortFoldersFirst(bool foldersFirst);
0063     bool sortFoldersFirst() const;
0064 
0065     void setSortHiddenLast(bool hiddenLast);
0066     bool sortHiddenLast() const;
0067 
0068     /**
0069      * Sets the additional information for the current set view-mode.
0070      * Note that the additional-info property is the only property where
0071      * the value is dependent from another property (in this case the view-mode).
0072      */
0073     void setVisibleRoles(const QList<QByteArray> &info);
0074 
0075     /**
0076      * Returns the additional information for the current set view-mode.
0077      * Note that the additional-info property is the only property where
0078      * the value is dependent from another property (in this case the view-mode).
0079      */
0080     QList<QByteArray> visibleRoles() const;
0081 
0082     void setHeaderColumnWidths(const QList<int> &widths);
0083     QList<int> headerColumnWidths() const;
0084 
0085     /**
0086      * Sets the directory properties view mode, show preview,
0087      * show hidden files, sorting and sort order like
0088      * set in \a props.
0089      */
0090     void setDirProperties(const ViewProperties &props);
0091 
0092     /**
0093      * If \a autoSave is true, the properties are automatically
0094      * saved when the destructor is called. Per default autosaving
0095      * is enabled.
0096      */
0097     void setAutoSaveEnabled(bool autoSave);
0098     bool isAutoSaveEnabled() const;
0099 
0100     void update();
0101 
0102     /**
0103      * Saves the view properties for the directory specified
0104      * in the constructor. The method is automatically
0105      * invoked in the destructor, if
0106      * ViewProperties::isAutoSaveEnabled() returns true and
0107      * at least one property has been changed.
0108      */
0109     void save();
0110 
0111     /**
0112      * @return True if properties for the given URL exist:
0113      *         As soon as the properties for an URL have been saved with
0114      *         ViewProperties::save(), true will be returned. If false is
0115      *         returned, the default view-properties are used.
0116      */
0117     bool exist() const;
0118 
0119 private:
0120     /**
0121      * Returns the destination directory path where the view
0122      * properties are stored. \a subDir specifies the used sub
0123      * directory.
0124      */
0125     QString destinationDir(const QString &subDir) const;
0126 
0127     /**
0128      * Returns the view-mode prefix when storing additional properties for
0129      * a view-mode.
0130      */
0131     QString viewModePrefix() const;
0132 
0133     /**
0134      * Provides backward compatibility with .directory files created with
0135      * Dolphin < 2.0: Converts the old additionalInfo-property into
0136      * the visibleRoles-property and clears the additionalInfo-property.
0137      */
0138     void convertAdditionalInfo();
0139 
0140     /**
0141      * Provides backward compatibility with .directory files created with
0142      * Dolphin < 2.1: Converts the old name-role "name" to the generic
0143      * role "text".
0144      */
0145     void convertNameRoleToTextRole();
0146 
0147     /**
0148      * Provides backward compatibility with .directory files created with
0149      * Dolphin < 16.11.70: Converts the old name-role "date" to "modificationtime"
0150      */
0151 
0152     void convertDateRoleToModificationTimeRole();
0153     /**
0154      * Returns true, if \a filePath is part of the home-path (see QDir::homePath()).
0155      */
0156     static bool isPartOfHome(const QString &filePath);
0157 
0158     /**
0159      * @return A hash-value for an URL that can be used as directory name.
0160      *         Is used to be able to remember view-properties for long baloo-URLs.
0161      */
0162     static QString directoryHashForUrl(const QUrl &url);
0163 
0164     Q_DISABLE_COPY(ViewProperties)
0165 
0166 private:
0167     bool m_changedProps;
0168     bool m_autoSave;
0169     QString m_filePath;
0170     ViewPropertySettings *m_node;
0171 };
0172 
0173 #endif