From 7cee23157f099837fffc22380b85ac33a2006a49 Mon Sep 17 00:00:00 2001 From: Kai Uwe Broulik Date: Mon, 19 Mar 2018 09:57:24 +0100 Subject: [PATCH] Introduce singleton for KFilePlacesModel 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 | 1 + src/dolphinmainwindow.cpp | 8 ++--- src/dolphinplacesmodelsingleton.cpp | 45 +++++++++++++++++++++++ src/dolphinplacesmodelsingleton.h | 51 +++++++++++++++++++++++++++ src/dolphinviewcontainer.cpp | 4 +-- src/panels/places/placesitemmodel.cpp | 20 +++++------ src/panels/places/placesitemmodel.h | 2 +- 7 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 src/dolphinplacesmodelsingleton.cpp create mode 100644 src/dolphinplacesmodelsingleton.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 58af19ebd..7b7037003 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -202,6 +202,7 @@ set(dolphinstatic_SRCS dolphinviewcontainer.cpp dolphincontextmenu.cpp dolphintabbar.cpp + dolphinplacesmodelsingleton.cpp dolphinrecenttabsmenu.cpp dolphintabpage.cpp dolphintabwidget.cpp diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index b09d7deef..d112007bc 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -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 index 000000000..1020ba186 --- /dev/null +++ b/src/dolphinplacesmodelsingleton.cpp @@ -0,0 +1,45 @@ +/*************************************************************************** + * Copyright (C) 2018 Kai Uwe Broulik * + * * + * 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 +#include + +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 index 000000000..6bce25d16 --- /dev/null +++ b/src/dolphinplacesmodelsingleton.h @@ -0,0 +1,51 @@ +/*************************************************************************** + * Copyright (C) 2018 Kai Uwe Broulik * + * * + * 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 +#include + +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 m_placesModel; +}; + +#endif // DOLPHINPLACESMODELSINGLETON_H diff --git a/src/dolphinviewcontainer.cpp b/src/dolphinviewcontainer.cpp index 2b829f4e0..dc9b4538f 100644 --- a/src/dolphinviewcontainer.cpp +++ b/src/dolphinviewcontainer.cpp @@ -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 -#include #include #include #include @@ -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, diff --git a/src/panels/places/placesitemmodel.cpp b/src/panels/places/placesitemmodel.cpp index 207a98271..444ad29ea 100644 --- a/src/panels/places/placesitemmodel.cpp +++ b/src/panels/places/placesitemmodel.cpp @@ -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 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 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 { diff --git a/src/panels/places/placesitemmodel.h b/src/panels/places/placesitemmodel.h index 1aa31facd..a2086efc5 100644 --- a/src/panels/places/placesitemmodel.h +++ b/src/panels/places/placesitemmodel.h @@ -228,7 +228,7 @@ private: QHash m_storageSetupInProgress; - QScopedPointer m_sourceModel; + KFilePlacesModel *m_sourceModel; QVector m_indexMap; }; -- 2.47.3