diff --git a/plugins/sms/smsplugin.cpp b/plugins/sms/smsplugin.cpp index d5b5956d0..21b7cca77 100644 --- a/plugins/sms/smsplugin.cpp +++ b/plugins/sms/smsplugin.cpp @@ -37,15 +37,30 @@ K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_sms.json", regi Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SMS, "kdeconnect.plugin.sms") +QMap SmsPlugin::conversationInterfaces; + SmsPlugin::SmsPlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) , m_telepathyInterface(QStringLiteral("org.freedesktop.Telepathy.ConnectionManager.kdeconnect"), QStringLiteral("/kdeconnect")) , m_conversationInterface(new ConversationsDbusInterface(this)) { + // Once the new ConversationsDbusInterface has been fully set up, the old one can safely be + // deleted + const auto& oldInterfaceItr = SmsPlugin::conversationInterfaces.find(device()->id()); + if (oldInterfaceItr != SmsPlugin::conversationInterfaces.end()) { + ConversationsDbusInterface* oldInterface = oldInterfaceItr.value(); + oldInterface->deleteLater(); + SmsPlugin::conversationInterfaces.erase(oldInterfaceItr); + } } SmsPlugin::~SmsPlugin() { + // Because of how Qt's Dbus is designed, we are unable to immediately delete m_conversationInterface, + // See the comment in ~NotificationsPlugin() for more information + // We save the old pointer for now; the internal data can be used as a cache. + // The next time we construct an SmsPlugin, we access the old pointer and delete it + SmsPlugin::conversationInterfaces[device()->id()] = m_conversationInterface; } bool SmsPlugin::receivePacket(const NetworkPacket& np) diff --git a/plugins/sms/smsplugin.h b/plugins/sms/smsplugin.h index 3a46065a5..fb059638a 100644 --- a/plugins/sms/smsplugin.h +++ b/plugins/sms/smsplugin.h @@ -131,6 +131,13 @@ private: QDBusInterface m_telepathyInterface; ConversationsDbusInterface* m_conversationInterface; + + /* + * Keep a map of all interfaces ever constructed + * See comments in ~SmsPlugin() for the reason + */ + static QMap conversationInterfaces; + }; #endif