]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Introduce singleton for KFilePlacesModel
authorKai Uwe Broulik <kde@privat.broulik.de>
Mon, 19 Mar 2018 08:57:24 +0000 (09:57 +0100)
committerKai Uwe Broulik <kde@privat.broulik.de>
Mon, 19 Mar 2018 08:57:24 +0000 (09:57 +0100)
There are various places where Dolphin created a new KFilePlacesModel which would then query all storage devices and do other expensive work.

Differential Revision: https://phabricator.kde.org/D11283

src/CMakeLists.txt
src/dolphinmainwindow.cpp
src/dolphinplacesmodelsingleton.cpp [new file with mode: 0644]
src/dolphinplacesmodelsingleton.h [new file with mode: 0644]
src/dolphinviewcontainer.cpp
src/panels/places/placesitemmodel.cpp
src/panels/places/placesitemmodel.h

index 58af19ebd39750f0e5c6d959b265bcb021c00d3f..7b703700357e3e370b11cc1cf6f4cbf01a482612 100644 (file)
@@ -202,6 +202,7 @@ set(dolphinstatic_SRCS
     dolphinviewcontainer.cpp
     dolphincontextmenu.cpp
     dolphintabbar.cpp
+    dolphinplacesmodelsingleton.cpp
     dolphinrecenttabsmenu.cpp
     dolphintabpage.cpp
     dolphintabwidget.cpp
index b09d7deef2304331536298e6aefbadb8be0d187d..d112007bc542a66fc654f2cc973473c0c8cf7125 100644 (file)
@@ -25,6 +25,7 @@
 #include "dolphindockwidget.h"
 #include "dolphincontextmenu.h"
 #include "dolphinnewfilemenu.h"
+#include "dolphinplacesmodelsingleton.h"
 #include "dolphinrecenttabsmenu.h"
 #include "dolphintabwidget.h"
 #include "dolphinviewcontainer.h"
@@ -993,8 +994,6 @@ void DolphinMainWindow::tabCountChanged(int count)
 
 void DolphinMainWindow::setUrlAsCaption(const QUrl& url)
 {
-    static KFilePlacesModel s_placesModel;
-
     QString schemePrefix;
     if (!url.isLocalFile()) {
         schemePrefix.append(url.scheme() + " - ");
@@ -1009,10 +1008,11 @@ void DolphinMainWindow::setUrlAsCaption(const QUrl& url)
         return;
     }
 
-    const auto& matchedPlaces = s_placesModel.match(s_placesModel.index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly);
+    KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
+    const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly);
 
     if (!matchedPlaces.isEmpty()) {
-        setWindowTitle(s_placesModel.text(matchedPlaces.first()));
+        setWindowTitle(placesModel->text(matchedPlaces.first()));
         return;
     }
 
diff --git a/src/dolphinplacesmodelsingleton.cpp b/src/dolphinplacesmodelsingleton.cpp
new file mode 100644 (file)
index 0000000..1020ba1
--- /dev/null
@@ -0,0 +1,45 @@
+/***************************************************************************
+ *   Copyright (C) 2018 Kai Uwe Broulik <kde@privat.broulik.de>            *
+ *                                                                         *
+ *   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            *
+ ***************************************************************************/
+
+#include "dolphinplacesmodelsingleton.h"
+
+#include <KAboutData>
+#include <KFilePlacesModel>
+
+DolphinPlacesModelSingleton::DolphinPlacesModelSingleton()
+    : m_placesModel(new KFilePlacesModel(KAboutData::applicationData().componentName() + applicationNameSuffix()))
+{
+
+}
+
+DolphinPlacesModelSingleton &DolphinPlacesModelSingleton::instance()
+{
+    static DolphinPlacesModelSingleton s_self;
+    return s_self;
+}
+
+KFilePlacesModel *DolphinPlacesModelSingleton::placesModel() const
+{
+    return m_placesModel.data();
+}
+
+QString DolphinPlacesModelSingleton::applicationNameSuffix()
+{
+    return QStringLiteral("-places-panel");
+}
diff --git a/src/dolphinplacesmodelsingleton.h b/src/dolphinplacesmodelsingleton.h
new file mode 100644 (file)
index 0000000..6bce25d
--- /dev/null
@@ -0,0 +1,51 @@
+/***************************************************************************
+ *   Copyright (C) 2018 Kai Uwe Broulik <kde@privat.broulik.de>            *
+ *                                                                         *
+ *   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            *
+ ***************************************************************************/
+
+#ifndef DOLPHINPLACESMODELSINGLETON_H
+#define DOLPHINPLACESMODELSINGLETON_H
+
+#include <QString>
+#include <QScopedPointer>
+
+class KFilePlacesModel;
+
+/**
+ * @brief Provides a global KFilePlacesModel instance.
+ */
+class DolphinPlacesModelSingleton
+{
+
+public:
+    static DolphinPlacesModelSingleton& instance();
+
+    KFilePlacesModel *placesModel() const;
+    /** A suffix to the application-name of the stored bookmarks is
+     added, which is only read by PlacesItemModel. */
+    static QString applicationNameSuffix();
+
+    DolphinPlacesModelSingleton(const DolphinPlacesModelSingleton&) = delete;
+    DolphinPlacesModelSingleton& operator=(const DolphinPlacesModelSingleton&) = delete;
+
+private:
+    DolphinPlacesModelSingleton();
+
+    QScopedPointer<KFilePlacesModel> m_placesModel;
+};
+
+#endif // DOLPHINPLACESMODELSINGLETON_H
index 2b829f4e09c7d4e398e16932454703084916802f..dc9b4538fcd13d56d5d3c4d6818f7f69c3d64e69 100644 (file)
@@ -20,6 +20,7 @@
 #include "dolphinviewcontainer.h"
 
 #include "dolphin_generalsettings.h"
+#include "dolphinplacesmodelsingleton.h"
 #include "dolphindebug.h"
 #include "filterbar/filterbar.h"
 #include "global.h"
@@ -30,7 +31,6 @@
 #include "views/viewproperties.h"
 
 #include <KFileItemActions>
-#include <KFilePlacesModel>
 #include <KIO/PreviewJob>
 #include <KLocalizedString>
 #include <KMessageWidget>
@@ -77,7 +77,7 @@ DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) :
     navigatorLayout->setSpacing(0);
     navigatorLayout->setMargin(0);
 
-    m_urlNavigator = new KUrlNavigator(new KFilePlacesModel(this), url, this);
+    m_urlNavigator = new KUrlNavigator(DolphinPlacesModelSingleton::instance().placesModel(), url, this);
     connect(m_urlNavigator, &KUrlNavigator::activated,
             this, &DolphinViewContainer::activate);
     connect(m_urlNavigator->editor(), &KUrlComboBox::completionModeChanged,
index 207a982713be7a3bad71e0cdf49f89bdb022f2ff..444ad29eaba6ad51290c8e03e3aba1ab03fa3494 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "dolphin_generalsettings.h"
 #include "dolphindebug.h"
+#include "dolphinplacesmodelsingleton.h"
 #include "placesitem.h"
 #include "placesitemsignalhandler.h"
 #include "views/dolphinview.h"
@@ -42,9 +43,6 @@
 #include <QTimer>
 
 namespace {
-    // A suffix to the application-name of the stored bookmarks is
-    // added, which is only read by PlacesItemModel.
-    const QString AppNameSuffix = QStringLiteral("-places-panel");
     static QList<QUrl> balooURLs = {
         QUrl(QStringLiteral("timeline:/today")),
         QUrl(QStringLiteral("timeline:/yesterday")),
@@ -62,18 +60,18 @@ PlacesItemModel::PlacesItemModel(QObject* parent) :
     m_hiddenItemsShown(false),
     m_deviceToTearDown(nullptr),
     m_storageSetupInProgress(),
-    m_sourceModel(new KFilePlacesModel(KAboutData::applicationData().componentName() + AppNameSuffix, this))
+    m_sourceModel(DolphinPlacesModelSingleton::instance().placesModel())
 {
     cleanupBookmarks();
     loadBookmarks();
     initializeDefaultViewProperties();
 
-    connect(m_sourceModel.data(), &KFilePlacesModel::rowsInserted, this, &PlacesItemModel::onSourceModelRowsInserted);
-    connect(m_sourceModel.data(), &KFilePlacesModel::rowsAboutToBeRemoved, this, &PlacesItemModel::onSourceModelRowsAboutToBeRemoved);
-    connect(m_sourceModel.data(), &KFilePlacesModel::dataChanged, this, &PlacesItemModel::onSourceModelDataChanged);
-    connect(m_sourceModel.data(), &KFilePlacesModel::rowsAboutToBeMoved, this, &PlacesItemModel::onSourceModelRowsAboutToBeMoved);
-    connect(m_sourceModel.data(), &KFilePlacesModel::rowsMoved, this, &PlacesItemModel::onSourceModelRowsMoved);
-    connect(m_sourceModel.data(), &KFilePlacesModel::groupHiddenChanged, this, &PlacesItemModel::onSourceModelGroupHiddenChanged);
+    connect(m_sourceModel, &KFilePlacesModel::rowsInserted, this, &PlacesItemModel::onSourceModelRowsInserted);
+    connect(m_sourceModel, &KFilePlacesModel::rowsAboutToBeRemoved, this, &PlacesItemModel::onSourceModelRowsAboutToBeRemoved);
+    connect(m_sourceModel, &KFilePlacesModel::dataChanged, this, &PlacesItemModel::onSourceModelDataChanged);
+    connect(m_sourceModel, &KFilePlacesModel::rowsAboutToBeMoved, this, &PlacesItemModel::onSourceModelRowsAboutToBeMoved);
+    connect(m_sourceModel, &KFilePlacesModel::rowsMoved, this, &PlacesItemModel::onSourceModelRowsMoved);
+    connect(m_sourceModel, &KFilePlacesModel::groupHiddenChanged, this, &PlacesItemModel::onSourceModelGroupHiddenChanged);
 }
 
 PlacesItemModel::~PlacesItemModel()
@@ -622,7 +620,7 @@ void PlacesItemModel::cleanupBookmarks()
         const QString appName = bookmark.metaDataItem(QStringLiteral("OnlyInApp"));
 
         if ((appName == KAboutData::applicationData().componentName() ||
-             appName == KAboutData::applicationData().componentName() + AppNameSuffix) && balooURLs.contains(url)) {
+             appName == KAboutData::applicationData().componentName() + DolphinPlacesModelSingleton::applicationNameSuffix()) && balooURLs.contains(url)) {
             qCDebug(DolphinDebug) << "Removing old baloo url:" << url;
             m_sourceModel->removePlace(sourceIndex);
         } else {
index 1aa31facdc22bce6f7776350fb4e9c0a1c94239c..a2086efc57b6727a16fe672b6c1523aaf6022081 100644 (file)
@@ -228,7 +228,7 @@ private:
 
     QHash<QObject*, int> m_storageSetupInProgress;
 
-    QScopedPointer<KFilePlacesModel> m_sourceModel;
+    KFilePlacesModel *m_sourceModel;
 
     QVector<QPersistentModelIndex> m_indexMap;
 };