From a1d396ad202dbfd51ff7449c4c9659db78c21db2 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Sun, 18 Aug 2013 18:22:54 +0200 Subject: [PATCH] Error checking in KCM, in case KDED is not running Added a dbus interface isValid check to prevent a crash. BUG: 323653 --- kcm/devicesmodel.cpp | 14 +++++++++++--- kcm/kcm.cpp | 15 +++++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/kcm/devicesmodel.cpp b/kcm/devicesmodel.cpp index dc5b3673d..dc01bea5e 100644 --- a/kcm/devicesmodel.cpp +++ b/kcm/devicesmodel.cpp @@ -95,7 +95,6 @@ void DevicesModel::refreshDeviceList() QVariant DevicesModel::data(const QModelIndex &index, int role) const { - if (!m_dbusInterface->isValid()) { switch (role) { case IconModelRole: @@ -117,7 +116,7 @@ QVariant DevicesModel::data(const QModelIndex &index, int role) const DeviceDbusInterface* device = m_deviceList[index.row()]; - //FIXME: This function gets called lots of times per second, producing lots of dbus calls + //FIXME: This function gets called lots of times, producing lots of dbus calls. Add a cache. switch (role) { case IconModelRole: { bool paired = device->paired(); @@ -144,7 +143,16 @@ QVariant DevicesModel::data(const QModelIndex &index, int role) const DeviceDbusInterface* DevicesModel::getDevice(const QModelIndex& index) { - return m_deviceList[index.row()]; + if (!index.isValid()) { + return NULL; + } + + int row = index.row(); + if (row < 0 || row >= m_deviceList.size()) { + return NULL; + } + + return m_deviceList[row]; } int DevicesModel::rowCount(const QModelIndex &parent) const diff --git a/kcm/kcm.cpp b/kcm/kcm.cpp index 1bdd657b6..983cbdfc9 100644 --- a/kcm/kcm.cpp +++ b/kcm/kcm.cpp @@ -91,18 +91,25 @@ void KdeConnectKcm::deviceSelected(const QModelIndex& current) //Store previous device config pluginsConfigChanged(); - currentIndex = sortProxyModel->mapToSource(current); + if (!current.isValid()) { + kcmUi->deviceInfo->setVisible(false); + return; + } - bool valid = currentIndex.isValid(); + currentIndex = sortProxyModel->mapToSource(current); + currentDevice = devicesModel->getDevice(currentIndex); + + bool valid = (currentDevice != NULL && currentDevice->isValid()); kcmUi->deviceInfo->setVisible(valid); - if (!valid) return; + if (!valid) { + return; + } //FIXME: KPluginSelector has no way to remove a list of plugins and load another, so we need to destroy and recreate it each time delete kcmUi->pluginSelector; kcmUi->pluginSelector = new KPluginSelector(this); kcmUi->verticalLayout_2->addWidget(kcmUi->pluginSelector); - currentDevice = devicesModel->getDevice(currentIndex); kcmUi->deviceName->setText(currentDevice->name()); kcmUi->trust_checkbox->setChecked(currentDevice->paired());