Commit 688e0492 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

[Device Monitor] Create KFilePlacesModel on demand

Doing so can be quite expensive as it queries devices and what not.
This is particularly wasteful if the user doesn't have any Bluetooth devices or no browsable.

Differential Revision: https://phabricator.kde.org/D13751
parent ebba20a3
......@@ -35,7 +35,6 @@
DeviceMonitor::DeviceMonitor(BlueDevilDaemon *daemon)
: QObject(daemon)
, m_manager(daemon->manager())
, m_places(new KFilePlacesModel(this))
, m_config(KSharedConfig::openConfig(QStringLiteral("bluedevilglobalrc")))
{
Q_FOREACH (BluezQt::AdapterPtr adapter, m_manager->adapters()) {
......@@ -62,6 +61,15 @@ DeviceMonitor::DeviceMonitor(BlueDevilDaemon *daemon)
restoreState();
}
KFilePlacesModel *DeviceMonitor::places()
{
if (!m_places) {
m_places = new KFilePlacesModel(this);
}
return m_places;
}
void DeviceMonitor::bluetoothOperationalChanged(bool operational)
{
if (!operational) {
......@@ -158,10 +166,10 @@ void DeviceMonitor::restoreAdapter(BluezQt::AdapterPtr adapter)
void DeviceMonitor::clearPlaces()
{
for (int i = 0; i < m_places->rowCount(); ++i) {
const QModelIndex &index = m_places->index(i, 0);
if (m_places->url(index).scheme() == QLatin1String("obexftp")) {
m_places->removePlace(index);
for (int i = 0; i < places()->rowCount(); ++i) {
const QModelIndex &index = places()->index(i, 0);
if (places()->url(index).scheme() == QLatin1String("obexftp")) {
places()->removePlace(index);
i--;
}
}
......@@ -177,21 +185,21 @@ void DeviceMonitor::updateDevicePlace(BluezQt::DevicePtr device)
url.setScheme(QStringLiteral("obexftp"));
url.setHost(device->address().replace(QLatin1Char(':'), QLatin1Char('-')));
const QModelIndex &index = m_places->closestItem(url);
const QModelIndex &index = places()->closestItem(url);
if (device->isConnected()) {
if (m_places->url(index) != url) {
if (places()->url(index) != url) {
qCDebug(BLUEDAEMON) << "Adding place" << url;
QString icon = device->icon();
if (icon == QLatin1String("phone")) {
icon.prepend(QLatin1String("smart")); // Better breeze icon
}
m_places->addPlace(device->name(), url, icon);
places()->addPlace(device->name(), url, icon);
}
} else {
if (m_places->url(index) == url) {
if (places()->url(index) == url) {
qCDebug(BLUEDAEMON) << "Removing place" << url;
m_places->removePlace(index);
places()->removePlace(index);
}
}
}
......@@ -54,8 +54,10 @@ private:
void clearPlaces();
void updateDevicePlace(BluezQt::DevicePtr device);
KFilePlacesModel *places();
BluezQt::Manager *m_manager;
KFilePlacesModel *m_places;
KFilePlacesModel *m_places = nullptr;
KSharedConfig::Ptr m_config;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment