]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodel.h
KFileItemModel: interface cleanups
[dolphin.git] / src / kitemviews / kfileitemmodel.h
index ff816c85ca0e5ce5835b427df9424cc8d6a4fc6b..b29c76c9201be76a711bf5f49f88923d12d72e5f 100644 (file)
 #include <libdolphin_export.h>
 #include <KFileItemList>
 #include <KUrl>
-#include <kitemviews/kfileitemmodelfilter_p.h>
 #include <kitemviews/kitemmodelbase.h>
+#include <kitemviews/private/kfileitemmodelfilter.h>
 
 #include <QHash>
 
-class KDirLister;
+class KFileItemModelDirLister;
 class QTimer;
 
 /**
  * @brief KItemModelBase implementation for KFileItems.
  *
- * KFileItemModel is connected with one KDirLister. Each time the KDirLister
- * emits new items, removes items or changes items the model gets synchronized.
+ * Allows to load items of a directory. Sorting and grouping of
+ * items are supported. Roles that are not part of KFileItem can
+ * be added with KFileItemModel::setData().
  *
- * KFileItemModel supports sorting and grouping of items. Additional roles that
- * are not part of KFileItem can be added with KFileItemModel::setData().
- *
- * Also the recursive expansion of sub-directories is supported by
+ * Recursive expansion of sub-directories is supported by
  * KFileItemModel::setExpanded().
- *
- * TODO: In the longterm instead of passing a KDirLister just an URL should
- * be passed and a KDirLister used internally. This solves the following issues:
- * - The user of the API does not need to decide whether he listens to KDirLister
- *   or KFileItemModel.
- * - It resolves minor conceptual differences between KDirLister and KFileItemModel.
- *   E.g. there is no way for KFileItemModel to check whether a completed() signal
- *   will be emitted after newItems() will be send by KDirLister or not (in the case
- *   of setShowingDotFiles() no completed() signal will get emitted).
  */
 class LIBDOLPHINPRIVATE_EXPORT KFileItemModel : public KItemModelBase
 {
     Q_OBJECT
 
 public:
-    explicit KFileItemModel(KDirLister* dirLister, QObject* parent = 0);
+    explicit KFileItemModel(QObject* parent = 0);
     virtual ~KFileItemModel();
 
+    /**
+     * Loads the directory specified by \a url. The signals
+     * directoryLoadingStarted(), directoryLoadingProgress() and directoryLoadingCompleted()
+     * indicate the current state of the loading process. The items
+     * of the directory are added after the loading has been completed.
+     */
+    void loadDirectory(const KUrl& url);
+
+    /**
+     * Throws away all currently loaded items and refreshes the directory
+     * by reloading all items again.
+     */
+    void refreshDirectory(const KUrl& url);
+
+    /**
+     * @return Parent directory of the items that are shown. In case
+     *         if a directory tree is shown, KFileItemModel::dir() returns
+     *         the root-parent of all items.
+     * @see rootItem()
+     */
+    KUrl directory() const;
+
+    /**
+     * Cancels the loading of a directory which has been started by either
+     * loadDirectory() or refreshDirectory().
+     */
+    void cancelDirectoryLoading();
+
     virtual int count() const;
     virtual QHash<QByteArray, QVariant> data(int index) const;
     virtual bool setData(int index, const QHash<QByteArray, QVariant>& values);
 
     /**
-     * Sets a separate sorting with folders first (true) or a mixed sorting of files and folders (false).
+     * Sets a separate sorting with directories first (true) or a mixed
+     * sorting of files and directories (false).
      */
-    void setSortFoldersFirst(bool foldersFirst);
-    bool sortFoldersFirst() const;
+    void setSortDirectoriesFirst(bool dirsFirst);
+    bool sortDirectoriesFirst() const;
 
     void setShowHiddenFiles(bool show);
     bool showHiddenFiles() const;
 
     /**
-     * If set to true, only folders are shown as items of the model. Files
+     * If set to true, only directories are shown as items of the model. Files
      * are ignored.
      */
-    void setShowFoldersOnly(bool enabled);
-    bool showFoldersOnly() const;
+    void setShowDirectoriesOnly(bool enabled);
+    bool showDirectoriesOnly() const;
 
     /** @reimp */
     virtual QMimeData* createMimeData(const QSet<int>& indexes) const;
@@ -90,7 +108,7 @@ public:
     virtual bool supportsDropping(int index) const;
 
     /** @reimp */
-    virtual QString roleDescription(const QByteArray& role) const;
+    virtual QString roleDescription(const QByteArray& typeForRole) const;
 
     /** @reimp */
     virtual QList<QPair<int, QVariant> > groups() const;
@@ -123,7 +141,8 @@ public:
     int index(const KUrl& url) const;
 
     /**
-     * @return Root item of all items.
+     * @return Root item of all items representing the item
+     *         for KFileItemModel::dir().
      */
     KFileItem rootItem() const;
 
@@ -132,32 +151,59 @@ public:
      */
     void clear();
 
-    // TODO: "name" + "isDir" is default in ctor
+    /**
+     * Sets the roles that should be shown for each item.
+     */
     void setRoles(const QSet<QByteArray>& roles);
     QSet<QByteArray> roles() const;
 
     virtual bool setExpanded(int index, bool expanded);
     virtual bool isExpanded(int index) const;
     virtual bool isExpandable(int index) const;
+    virtual int expandedParentsCount(int index) const;
 
-    QSet<KUrl> expandedUrls() const;
+    QSet<KUrl> expandedDirectories() const;
 
     /**
-     * Marks the URLs in \a urls as subfolders which were expanded previously.
-     * They are re-expanded one by one each time the KDirLister's completed() signal is received.
-     * Note that a manual triggering of the KDirLister is required.
+     * Marks the URLs in \a urls as sub-directories which were expanded previously.
+     * After calling loadDirectory() or refreshDirectory() the marked sub-directories
+     * will be expanded step-by-step.
      */
-    void restoreExpandedUrls(const QSet<KUrl>& urls);
+    void restoreExpandedDirectories(const QSet<KUrl>& urls);
 
     /**
-     * Expands all parent-items of each URL given by \a urls.
+     * Expands all parent-directories of the item \a url.
      */
-    void setExpanded(const QSet<KUrl>& urls);
+    void expandParentDirectories(const KUrl& url);
 
     void setNameFilter(const QString& nameFilter);
     QString nameFilter() const;
 
+    struct RoleInfo
+    {   QByteArray role;
+        QString translation;
+        QString group;
+        bool requiresNepomuk;
+        bool requiresIndexer;
+    };
+
+    /**
+     * @return Provides static information for all available roles that
+     *         are supported by KFileItemModel. Some roles can only be
+     *         determined if Nepomuk is enabled and/or the Nepomuk
+     *         indexing is enabled.
+     */
+    static QList<RoleInfo> rolesInformation();
+
 signals:
+    /**
+     * Is emitted if the loading of a directory has been started. It is
+     * assured that a signal directoryLoadingCompleted() will be send after
+     * the loading has been finished. For tracking the loading progress
+     * the signal directoryLoadingProgress() gets emitted in between.
+     */
+    void directoryLoadingStarted();
+
     /**
      * Is emitted after the loading of a directory has been completed or new
      * items have been inserted to an already loaded directory. Usually
@@ -165,7 +211,38 @@ signals:
      * (the only exception is loading an empty directory, where only a
      * loadingCompleted() signal gets emitted).
      */
-    void loadingCompleted();
+    void directoryLoadingCompleted();
+
+    /**
+     * Informs about the progress in percent when loading a directory. It is assured
+     * that the signal directoryLoadingStarted() has been emitted before.
+     */
+    void directoryLoadingProgress(int percent);
+
+    /**
+     * Is emitted if the sort-role gets resolved asynchronously and provides
+     * the progress-information of the sorting in percent. It is assured
+     * that the last sortProgress-signal contains 100 as value.
+     */
+    void directorySortingProgress(int percent);
+
+    /**
+     * Is emitted if an information message (e.g. "Connecting to host...")
+     * should be shown.
+     */
+    void infoMessage(const QString& message);
+
+    /**
+     * Is emitted if an error message (e.g. "Unknown location")
+     * should be shown.
+     */
+    void errorMessage(const QString& message);
+
+    /**
+     * Is emitted if a redirection from the current URL \a oldUrl
+     * to the new URL \a newUrl has been done.
+     */
+    void directoryRedirection(const KUrl& oldUrl, const KUrl& newUrl);
 
 protected:
     virtual void onGroupedSortingChanged(bool current);
@@ -178,7 +255,7 @@ private slots:
      * and foldersFirst() settings.
      */
     void resortAllItems();
-    
+
     void slotCompleted();
     void slotCanceled();
     void slotNewItems(const KFileItemList& items);
@@ -191,25 +268,18 @@ private slots:
     void dispatchPendingItemsToInsert();
 
 private:
-    enum Role {
-        NoRole,
-        NameRole,
-        SizeRole,
-        DateRole,
-        PermissionsRole,
-        OwnerRole,
-        GroupRole,
-        TypeRole,
-        DestinationRole,
-        PathRole,
-        CommentRole,
-        TagsRole,
-        RatingRole,
-        IsDirRole,
-        IsExpandedRole,
-        IsExpandableRole,
-        ExpansionLevelRole,
-        RolesCount // Mandatory last entry
+    enum RoleType {
+        // User visible roles:
+        NoRole, NameRole, SizeRole, DateRole, PermissionsRole, OwnerRole,
+        GroupRole, TypeRole, DestinationRole, PathRole,
+        // User visible roles available with Nepomuk:
+        CommentRole, TagsRole, RatingRole, ImageSizeRole, OrientationRole,
+        WordCountRole, LineCountRole, ArtistRole, AlbumRole, DurationRole, TrackRole,
+        CopiedFromRole,
+        // Non-visible roles:
+        IsDirRole, IsExpandedRole, IsExpandableRole, ExpandedParentsCountRole,
+        // Mandatory last entry:
+        RolesCount
     };
 
     struct ItemData
@@ -218,10 +288,10 @@ private:
         QHash<QByteArray, QVariant> values;
         ItemData* parent;
     };
-    
+
     void insertItems(const KFileItemList& items);
     void removeItems(const KFileItemList& items);
-    
+
     /**
      * Helper method for insertItems() and removeItems(): Creates
      * a list of ItemData elements based on the given items.
@@ -238,19 +308,19 @@ private:
     void resetRoles();
 
     /**
-     * @return Role-index for the given role byte-array.
+     * @return Role-type for the given role.
      *         Runtime complexity is O(1).
      */
-    Role roleIndex(const QByteArray& role) const;
-    
+    RoleType typeForRole(const QByteArray& role) const;
+
     /**
-     * @return Role-byte-array for the given role-index.
+     * @return Role-byte-array for the given role-type.
      *         Runtime complexity is O(1).
      */
-    QByteArray roleByteArray(Role role) const;
+    QByteArray roleForType(RoleType roleType) const;
 
     QHash<QByteArray, QVariant> retrieveData(const KFileItem& item) const;
-    
+
     /**
      * @return True if the item-data \a a should be ordered before the item-data
      *         \b. The item-data may have different parent-items.
@@ -258,36 +328,12 @@ private:
     bool lessThan(const ItemData* a, const ItemData* b) const;
 
     /**
-     * Helper method for lessThan() and expansionLevelsCompare(): Compares
+     * Helper method for lessThan() and expandedParentsCountCompare(): Compares
      * the passed item-data using m_sortRole as criteria. Both items must
      * have the same parent item, otherwise the comparison will be wrong.
      */
     int sortRoleCompare(const ItemData* a, const ItemData* b) const;
-    
-    /**
-     * Sorts the items by using lessThan() as comparison criteria.
-     * The merge sort algorithm is used to assure a worst-case
-     * of O(n * log(n)) and to keep the number of comparisons low.
-     */
-    void sort(QList<ItemData*>::iterator begin, QList<ItemData*>::iterator end);
-    
-    /** Helper method for sort(). */
-    void merge(QList<ItemData*>::iterator begin,
-               QList<ItemData*>::iterator pivot,
-               QList<ItemData*>::iterator end);
-
-    /** Helper method for sort(). */
-    QList<ItemData*>::iterator lowerBound(QList<ItemData*>::iterator begin,
-                                          QList<ItemData*>::iterator end,
-                                          const ItemData* value);
-    
-    /** Helper method for sort(). */
-    QList<ItemData*>::iterator upperBound(QList<ItemData*>::iterator begin,
-                                          QList<ItemData*>::iterator end,
-                                          const ItemData* value);
-    /** Helper method for sort(). */
-    void reverse(QList<ItemData*>::iterator begin, QList<ItemData*>::iterator end);
-    
+
     int stringCompare(const QString& a, const QString& b) const;
 
     /**
@@ -296,10 +342,10 @@ private:
      * is not sufficient, it is also important to check the hierarchy for having
      * a correct order like shown in a tree.
      */
-    int expansionLevelsCompare(const ItemData* a, const ItemData* b) const;
+    int expandedParentsCountCompare(const ItemData* a, const ItemData* b) const;
 
     /**
-     * Helper method for expansionLevelCompare().
+     * Helper method for expandedParentsCountCompare().
      */
     QString subPath(const KFileItem& item,
                     const QString& itemPath,
@@ -313,7 +359,7 @@ private:
     QList<QPair<int, QVariant> > dateRoleGroups() const;
     QList<QPair<int, QVariant> > permissionRoleGroups() const;
     QList<QPair<int, QVariant> > ratingRoleGroups() const;
-    QList<QPair<int, QVariant> > genericStringRoleGroups(const QByteArray& role) const;
+    QList<QPair<int, QVariant> > genericStringRoleGroups(const QByteArray& typeForRole) const;
 
     /**
      * Helper method for all xxxRoleGroups() methods to check whether the
@@ -330,16 +376,50 @@ private:
      */
     KFileItemList childItems(const KFileItem& item) const;
 
+    /**
+     * Is invoked by KFileItemModelRolesUpdater and results in emitting the
+     * sortProgress signal with a percent-value of the progress.
+     */
+    void emitSortProgress(int resolvedCount);
+
+    /**
+     * Maps the QByteArray-roles to RoleTypes and provides translation- and
+     * group-contexts.
+     */
+    struct RoleInfoMap
+    {
+        const char* const role;
+        const RoleType roleType;
+        const char* const roleTranslationContext;
+        const char* const roleTranslation;
+        const char* const groupTranslationContext;
+        const char* const groupTranslation;
+        const bool requiresNepomuk;
+        const bool requiresIndexer;
+    };
+
+    /**
+     * @return Map of user visible roles that are accessible by KFileItemModel::rolesInformation().
+     */
+    static const RoleInfoMap* rolesInfoMap(int& count);
+
+    /**
+     * Determines the MIME-types of all items that can be done within
+     * the given timeout.
+     */
+    static void determineMimeTypes(const KFileItemList& items, int timeout);
+
 private:
-    QWeakPointer<KDirLister> m_dirLister;
+    KFileItemModelDirLister* m_dirLister;
 
     bool m_naturalSorting;
-    bool m_sortFoldersFirst;
+    bool m_sortDirsFirst;
 
-    Role m_sortRole;
+    RoleType m_sortRole;
+    int m_sortingProgressPercent; // Value of directorySortingProgress() signal
     QSet<QByteArray> m_roles;
     Qt::CaseSensitivity m_caseSensitivity;
-        
+
     QList<ItemData*> m_itemData;
     QHash<KUrl, int> m_items; // Allows O(1) access for KFileItemModel::index(const KFileItem& item)
 
@@ -348,43 +428,43 @@ private:
 
     bool m_requestRole[RolesCount];
 
-    QTimer* m_minimumUpdateIntervalTimer;
     QTimer* m_maximumUpdateIntervalTimer;
     QTimer* m_resortAllItemsTimer;
     KFileItemList m_pendingItemsToInsert;
-    bool m_pendingEmitLoadingCompleted;
 
     // Cache for KFileItemModel::groups()
     mutable QList<QPair<int, QVariant> > m_groups;
 
     // Stores the smallest expansion level of the root-URL. Is required to calculate
-    // the "expansionLevel" role in an efficient way. A value < 0 indicates a
+    // the "expandedParentsCount" role in an efficient way. A value < 0 indicates a
     // special meaning:
-    enum RootExpansionLevelTypes
+    enum ExpandedParentsCountRootTypes
     {
-        // m_rootExpansionLevel is uninitialized and must be determined by checking
+        // m_expandedParentsCountRoot is uninitialized and must be determined by checking
         // the root URL from the KDirLister.
-        UninitializedRootExpansionLevel = -1,
-        // All items should be forced to get an expansion level of 0 even if they
+        UninitializedExpandedParentsCountRoot = -1,
+        // All items should be forced to get an expanded parents count of 0 even if they
         // represent child items. This is useful for slaves that provide no parent items
         // for child items like e.g. the search IO slaves.
-        ForceRootExpansionLevel = -2
+        ForceExpandedParentsCountRoot = -2
     };
-    mutable int m_rootExpansionLevel;
+    mutable int m_expandedParentsCountRoot;
 
-    // Stores the URLs of the expanded folders.
-    QSet<KUrl> m_expandedUrls;
+    // Stores the URLs of the expanded directories.
+    QSet<KUrl> m_expandedDirs;
 
     // URLs that must be expanded. The expanding is initially triggered in setExpanded()
     // and done step after step in slotCompleted().
     QSet<KUrl> m_urlsToExpand;
 
-    friend class KFileItemModelTest; // For unit testing
+    friend class KFileItemModelSortAlgorithm;  // Accesses lessThan() method
+    friend class KFileItemModelRolesUpdater;   // Accesses emitSortProgress() method
+    friend class KFileItemModelTest;           // For unit testing
 };
 
 inline bool KFileItemModel::isChildItem(int index) const
 {
-    return m_requestRole[ExpansionLevelRole] && m_itemData.at(index)->values.value("expansionLevel").toInt() > 0;
+    return m_requestRole[ExpandedParentsCountRole] && m_itemData.at(index)->values.value("expandedParentsCount").toInt() > 0;
 }
 
 #endif