X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/894232ebda5b2cf155a4f4e5bf1287eb700faa18..f65b0899c3666561cafac14f67ab0bb8a5bfa00a:/src/panels/places/placesitemmodel.h diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h index 18264f06a..cd4079a73 100644 --- a/src/panels/places/placesitemmodel.h +++ b/src/panels/places/placesitemmodel.h @@ -1,78 +1,55 @@ -/*************************************************************************** - * Copyright (C) 2012 by Peter Penz * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ +/* + * SPDX-FileCopyrightText: 2012 Peter Penz + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #ifndef PLACESITEMMODEL_H #define PLACESITEMMODEL_H -#include +#include "kitemviews/kstandarditemmodel.h" -#include +#include +#include +#include -#include #include #include #include -#include -#include +#include class KBookmark; -class KBookmarkManager; class PlacesItem; class QAction; -class QTimer; - -#ifdef HAVE_NEPOMUK - namespace Nepomuk - { - namespace Query - { - class Term; - } - } -#endif - -// #define PLACESITEMMODEL_DEBUG /** * @brief Model for maintaining the bookmarks of the places panel. * - * It is compatible to the KFilePlacesModel from kdelibs but adds - * the ability to have groups for places. + * It is based on KFilePlacesModel from KIO. */ class PlacesItemModel: public KStandardItemModel { Q_OBJECT public: - explicit PlacesItemModel(QObject* parent = 0); - virtual ~PlacesItemModel(); + explicit PlacesItemModel(QObject* parent = nullptr); + ~PlacesItemModel() override; /** - * @return A new instance of a places item with the given - * attributes. + * @brief Create a new place entry in the bookmark file + * and add it to the model */ - PlacesItem* createPlacesItem(const QString& text, - const KUrl& url, - const QString& iconName); + void createPlacesItem(const QString& text, const QUrl& url, const QString& iconName = {}, const QString& appName = {}); + void createPlacesItem(const QString& text, const QUrl& url, const QString& iconName, const QString& appName, int after); PlacesItem* placesItem(int index) const; + /** + * @brief Mark an item as hidden + * @param index of the item to be hidden + */ + void hideItem(int index); + /** * If set to true, all items that are marked as hidden * will be shown in the view. The items will @@ -97,103 +74,101 @@ public: * range of the URL. -1 is returned if no closest item * could be found. */ - int closestItem(const KUrl& url) const; + int closestItem(const QUrl& url) const; QAction* ejectAction(int index) const; QAction* teardownAction(int index) const; void requestEject(int index); - void requestTeardown(int index); + void requestTearDown(int index); + + bool storageSetupNeeded(int index) const; + void requestStorageSetup(int index); - /** @reimp */ - virtual QMimeData* createMimeData(const QSet& indexes) const; + QMimeData* createMimeData(const KItemSet& indexes) const override; + + bool supportsDropping(int index) const override; + + void dropMimeDataBefore(int index, const QMimeData* mimeData); /** * @return Converts the URL, which contains "virtual" URLs for system-items like - * "search:/documents" into a Nepomuk-Query-URL that will be handled by + * "search:/documents" into a Query-URL that will be handled by * the corresponding IO-slave. Virtual URLs for bookmarks are used to * be independent from internal format changes. */ - static KUrl convertedUrl(const KUrl& url); + static QUrl convertedUrl(const QUrl& url); -signals: - void errorMessage(const QString& message); + void clear() override; -protected: - virtual void onItemInserted(int index); - virtual void onItemRemoved(int index, KStandardItem* removedItem); - virtual void onItemChanged(int index, const QSet& changedRoles); - -private slots: - void slotDeviceAdded(const QString& udi); - void slotDeviceRemoved(const QString& udi); - void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData); - void hideItem(); + void proceedWithTearDown(); /** - * Updates the bookmarks from the model corresponding to the changed - * bookmarks stored by the bookmark-manager. Is called whenever the bookmarks - * have been changed by another application. + * @brief Remove item from bookmark + * + * This function remove the index from bookmark file permanently + * + * @param index - the item to be removed */ - void updateBookmarks(); + void deleteItem(int index); /** - * Saves the bookmarks and indicates to other applications that the - * state of the bookmarks has been changed. Is only called by the - * timeout of m_saveBookmarksTimer to prevent unnecessary savings. - */ - void saveBookmarks(); + * Force a sync on the bookmarks and indicates to other applications that the + * state of the bookmarks has been changed. + */ + void refresh(); -private: - struct SystemBookmarkData; - - /** - * Loads the bookmarks from the bookmark-manager and creates items for - * the model or moves hidden items to m_bookmarkedItems. - */ - void loadBookmarks(); + bool isDir(int index) const override; - /** - * @return True, if the bookmark can be accepted in the context of the - * current application (e.g. bookmarks from other applications - * will be ignored). - */ - bool acceptBookmark(const KBookmark& bookmark) const; - /** - * Creates a PlacesItem for a system-bookmark: - * - PlacesItem::isSystemItem() will return true - * - Default view-properties will be created for "Search For" items - * The item is not inserted to the model yet. - */ - PlacesItem* createSystemPlacesItem(const SystemBookmarkData& data); + KFilePlacesModel::GroupType groupType(int row) const; + bool isGroupHidden(KFilePlacesModel::GroupType type) const; + void setGroupHidden(KFilePlacesModel::GroupType type, bool hidden); - /** - * Creates system bookmarks that are shown per default and can - * only be hidden but not removed. The result will be stored - * in m_systemBookmarks. - */ - void createSystemBookmarks(); +Q_SIGNALS: + void errorMessage(const QString& message); + void storageSetupDone(int index, bool success); + void storageTearDownRequested(const QString& mountPath); + void storageTearDownExternallyRequested(const QString& mountPath); + void storageTearDownSuccessful(); - void initializeAvailableDevices(); +protected: + void onItemInserted(int index) override; + void onItemRemoved(int index, KStandardItem* removedItem) override; + void onItemChanged(int index, const QSet& changedRoles) override; + +private Q_SLOTS: + void slotStorageTearDownDone(Solid::ErrorType error, const QVariant& errorData); + void slotStorageSetupDone(Solid::ErrorType error, const QVariant& errorData, const QString& udi); + + // source model control + void onSourceModelRowsInserted(const QModelIndex &parent, int first, int last); + void onSourceModelRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last); + void onSourceModelRowsAboutToBeMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row); + void onSourceModelRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row); + void onSourceModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles); + void onSourceModelGroupHiddenChanged(KFilePlacesModel::GroupType group, bool hidden); +private: /** - * @param index Item index related to the model. - * @return Corresponding index related to m_bookmarkedItems. + * Remove bookmarks created by the previous version of dolphin that are + * not valid anymore */ - int bookmarkIndex(int index) const; + void cleanupBookmarks(); /** - * Marks the item with the index \a index as hidden and - * removes it from the model so that it gets invisible. + * Loads the bookmarks from the bookmark-manager and creates items for + * the model or moves hidden items to m_bookmarkedItems. */ - void hideItem(int index); + void loadBookmarks(); + + QString internalMimeType() const; /** - * Triggers a delayed saving of bookmarks by starting - * m_saveBookmarksTimer. + * @return Adjusted drop index which assures that the item is aligned + * into the same group as specified by PlacesItem::groupType(). */ - void triggerBookmarksSaving(); + int groupedDropIndex(int index, const PlacesItem* item) const; /** * @return True if the bookmarks have the same identifiers. The identifier @@ -203,74 +178,37 @@ private: static bool equalBookmarkIdentifiers(const KBookmark& b1, const KBookmark& b2); /** - * @return URL using the timeline-protocol for searching (see convertedUrl()). + * Appends the item \a item as last element of the group + * the item belongs to. If no item with the same group is + * present, the item gets appended as last element of the + * model. PlacesItemModel takes the ownership + * of the item. */ - static KUrl createTimelineUrl(const KUrl& url); + void insertSortedItem(PlacesItem* item); - /** - * Helper method for createTimelineUrl(). - * @return String that represents a date-path in the format that - * the timeline-protocol expects. - */ - static QString timelineDateString(int year, int month, int day = 0); + PlacesItem *itemFromBookmark(const KBookmark &bookmark) const; - /** - * @return URL that can be listed by KIO and results in searching - * for a given term. The URL \a url represents a places-internal - * URL like e.g. "search:/documents" (see convertedUrl()). - */ - static KUrl createSearchUrl(const KUrl& url); + void addItemFromSourceModel(const QModelIndex &index); + void removeItemByIndex(const QModelIndex &mapToSource); -#ifdef HAVE_NEPOMUK - /** - * Helper method for createSearchUrl(). - * @return URL that can be listed by KIO and results in searching - * for the given term. - */ - static KUrl searchUrlForTerm(const Nepomuk::Query::Term& term); -#endif + QString bookmarkId(const KBookmark &bookmark) const; + void initializeDefaultViewProperties() const; -#ifdef PLACESITEMMODEL_DEBUG - void showModelState(); -#endif + int mapFromSource(const QModelIndex &index) const; + QModelIndex mapToSource(int row) const; + + static void updateItem(PlacesItem *item, const QModelIndex &index); private: - bool m_nepomukRunning; bool m_hiddenItemsShown; - QSet m_availableDevices; - Solid::Predicate m_predicate; - KBookmarkManager* m_bookmarkManager; - - struct SystemBookmarkData - { - SystemBookmarkData(const KUrl& url, - const QString& icon, - const QString& text) : - url(url), icon(icon), text(text) {} - KUrl url; - QString icon; - QString text; - }; - - QList m_systemBookmarks; - QHash m_systemBookmarksIndexes; - - // Contains hidden and unhidden items that are stored as - // bookmark (the model itself only contains items that - // are shown in the view). If an entry is 0, then the - // places-item is part of the model. If an entry is not - // 0, the item is hidden and not part of the model. - QList m_bookmarkedItems; - - // Index of the hidden item that should be removed in - // removeHiddenItem(). The removing must be done - // asynchronously as in the scope of onItemChanged() - // removing an item is not allowed. - int m_hiddenItemToRemove; - - QTimer* m_saveBookmarksTimer; - QTimer* m_updateBookmarksTimer; + Solid::StorageAccess *m_deviceToTearDown; + + QHash m_storageSetupInProgress; + + KFilePlacesModel *m_sourceModel; + + QVector m_indexMap; }; #endif