Changeset View
Changeset View
Standalone View
Standalone View
core/device.cpp
Show First 20 Lines • Show All 96 Lines • ▼ Show 20 Line(s) | |||||
97 | { | 97 | { | ||
98 | QHash<QString, KdeConnectPlugin*> newPluginMap, oldPluginMap = m_plugins; | 98 | QHash<QString, KdeConnectPlugin*> newPluginMap, oldPluginMap = m_plugins; | ||
99 | QMultiMap<QString, KdeConnectPlugin*> newPluginsByIncomingCapability; | 99 | QMultiMap<QString, KdeConnectPlugin*> newPluginsByIncomingCapability; | ||
100 | 100 | | |||
101 | if (isTrusted() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices | 101 | if (isTrusted() && isReachable()) { //Do not load any plugin for unpaired devices, nor useless loading them for unreachable devices | ||
102 | 102 | | |||
103 | PluginLoader* loader = PluginLoader::instance(); | 103 | PluginLoader* loader = PluginLoader::instance(); | ||
104 | 104 | | |||
105 | Q_FOREACH (const QString& pluginName, m_supportedPlugins) { | 105 | for (const QString& pluginName : qAsConst(m_supportedPlugins)) { | ||
106 | const KPluginMetaData service = loader->getPluginInfo(pluginName); | 106 | const KPluginMetaData service = loader->getPluginInfo(pluginName); | ||
107 | 107 | | |||
108 | const bool pluginEnabled = isPluginEnabled(pluginName); | 108 | const bool pluginEnabled = isPluginEnabled(pluginName); | ||
109 | const QSet<QString> incomingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPackageType")).toSet(); | 109 | const QSet<QString> incomingCapabilities = KPluginMetaData::readStringList(service.rawData(), QStringLiteral("X-KdeConnect-SupportedPackageType")).toSet(); | ||
110 | 110 | | |||
111 | if (pluginEnabled) { | 111 | if (pluginEnabled) { | ||
112 | KdeConnectPlugin* plugin = m_plugins.take(pluginName); | 112 | KdeConnectPlugin* plugin = m_plugins.take(pluginName); | ||
113 | 113 | | |||
114 | if (!plugin) { | 114 | if (!plugin) { | ||
115 | plugin = loader->instantiatePluginForDevice(pluginName, this); | 115 | plugin = loader->instantiatePluginForDevice(pluginName, this); | ||
116 | } | 116 | } | ||
117 | Q_ASSERT(plugin); | 117 | Q_ASSERT(plugin); | ||
118 | 118 | | |||
119 | Q_FOREACH (const QString& interface, incomingCapabilities) { | 119 | for (const QString& interface : incomingCapabilities) { | ||
120 | newPluginsByIncomingCapability.insert(interface, plugin); | 120 | newPluginsByIncomingCapability.insert(interface, plugin); | ||
121 | } | 121 | } | ||
122 | 122 | | |||
123 | newPluginMap[pluginName] = plugin; | 123 | newPluginMap[pluginName] = plugin; | ||
124 | } | 124 | } | ||
125 | } | 125 | } | ||
126 | } | 126 | } | ||
127 | 127 | | |||
128 | const bool differentPlugins = oldPluginMap != newPluginMap; | 128 | const bool differentPlugins = oldPluginMap != newPluginMap; | ||
129 | 129 | | |||
130 | //Erase all left plugins in the original map (meaning that we don't want | 130 | //Erase all left plugins in the original map (meaning that we don't want | ||
131 | //them anymore, otherwise they would have been moved to the newPluginMap) | 131 | //them anymore, otherwise they would have been moved to the newPluginMap) | ||
132 | qDeleteAll(m_plugins); | 132 | qDeleteAll(m_plugins); | ||
133 | m_plugins = newPluginMap; | 133 | m_plugins = newPluginMap; | ||
134 | m_pluginsByIncomingCapability = newPluginsByIncomingCapability; | 134 | m_pluginsByIncomingCapability = newPluginsByIncomingCapability; | ||
135 | 135 | | |||
136 | QDBusConnection bus = QDBusConnection::sessionBus(); | 136 | QDBusConnection bus = QDBusConnection::sessionBus(); | ||
137 | Q_FOREACH(KdeConnectPlugin* plugin, m_plugins) { | 137 | for (KdeConnectPlugin* plugin : qAsConst(m_plugins)) { | ||
138 | //TODO: see how it works in Android (only done once, when created) | 138 | //TODO: see how it works in Android (only done once, when created) | ||
139 | plugin->connected(); | 139 | plugin->connected(); | ||
140 | 140 | | |||
141 | const QString dbusPath = plugin->dbusPath(); | 141 | const QString dbusPath = plugin->dbusPath(); | ||
142 | if (!dbusPath.isEmpty()) { | 142 | if (!dbusPath.isEmpty()) { | ||
143 | bus.registerObject(dbusPath, plugin, QDBusConnection::ExportAllProperties | QDBusConnection::ExportScriptableInvokables | QDBusConnection::ExportScriptableSignals | QDBusConnection::ExportScriptableSlots); | 143 | bus.registerObject(dbusPath, plugin, QDBusConnection::ExportAllProperties | QDBusConnection::ExportScriptableInvokables | QDBusConnection::ExportScriptableSignals | QDBusConnection::ExportScriptableSlots); | ||
144 | } | 144 | } | ||
145 | } | 145 | } | ||
Show All 14 Lines | 158 | if (isTrusted()) { | |||
160 | return; | 160 | return; | ||
161 | } | 161 | } | ||
162 | 162 | | |||
163 | if (!isReachable()) { | 163 | if (!isReachable()) { | ||
164 | Q_EMIT pairingError(i18n("Device not reachable")); | 164 | Q_EMIT pairingError(i18n("Device not reachable")); | ||
165 | return; | 165 | return; | ||
166 | } | 166 | } | ||
167 | 167 | | |||
168 | Q_FOREACH(DeviceLink* dl, m_deviceLinks) { | 168 | for (DeviceLink* dl : qAsConst(m_deviceLinks)) { | ||
169 | dl->userRequestsPair(); | 169 | dl->userRequestsPair(); | ||
170 | } | 170 | } | ||
171 | } | 171 | } | ||
172 | 172 | | |||
173 | void Device::unpair() | 173 | void Device::unpair() | ||
174 | { | 174 | { | ||
175 | Q_FOREACH(DeviceLink* dl, m_deviceLinks) { | 175 | for (DeviceLink* dl : qAsConst(m_deviceLinks)) { | ||
176 | dl->userRequestsUnpair(); | 176 | dl->userRequestsUnpair(); | ||
177 | } | 177 | } | ||
178 | KdeConnectConfig::instance()->removeTrustedDevice(id()); | 178 | KdeConnectConfig::instance()->removeTrustedDevice(id()); | ||
179 | Q_EMIT trustedChanged(false); | 179 | Q_EMIT trustedChanged(false); | ||
180 | } | 180 | } | ||
181 | 181 | | |||
182 | void Device::pairStatusChanged(DeviceLink::PairStatus status) | 182 | void Device::pairStatusChanged(DeviceLink::PairStatus status) | ||
183 | { | 183 | { | ||
184 | if (status == DeviceLink::NotPaired) { | 184 | if (status == DeviceLink::NotPaired) { | ||
185 | KdeConnectConfig::instance()->removeTrustedDevice(id()); | 185 | KdeConnectConfig::instance()->removeTrustedDevice(id()); | ||
186 | 186 | | |||
187 | Q_FOREACH(DeviceLink* dl, m_deviceLinks) { | 187 | for (DeviceLink* dl : qAsConst(m_deviceLinks)) { | ||
188 | if (dl != sender()) { | 188 | if (dl != sender()) { | ||
189 | dl->setPairStatus(DeviceLink::NotPaired); | 189 | dl->setPairStatus(DeviceLink::NotPaired); | ||
190 | } | 190 | } | ||
191 | } | 191 | } | ||
192 | } else { | 192 | } else { | ||
193 | KdeConnectConfig::instance()->addTrustedDevice(id(), name(), type()); | 193 | KdeConnectConfig::instance()->addTrustedDevice(id(), name(), type()); | ||
194 | } | 194 | } | ||
195 | 195 | | |||
▲ Show 20 Lines • Show All 124 Lines • ▼ Show 20 Line(s) | |||||
320 | } | 320 | } | ||
321 | 321 | | |||
322 | bool Device::sendPackage(NetworkPackage& np) | 322 | bool Device::sendPackage(NetworkPackage& np) | ||
323 | { | 323 | { | ||
324 | Q_ASSERT(np.type() != PACKAGE_TYPE_PAIR); | 324 | Q_ASSERT(np.type() != PACKAGE_TYPE_PAIR); | ||
325 | Q_ASSERT(isTrusted()); | 325 | Q_ASSERT(isTrusted()); | ||
326 | 326 | | |||
327 | //Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data | 327 | //Maybe we could block here any package that is not an identity or a pairing package to prevent sending non encrypted data | ||
328 | Q_FOREACH(DeviceLink* dl, m_deviceLinks) { | 328 | for (DeviceLink* dl : qAsConst(m_deviceLinks)) { | ||
329 | if (dl->sendPackage(np)) return true; | 329 | if (dl->sendPackage(np)) return true; | ||
330 | } | 330 | } | ||
331 | 331 | | |||
332 | return false; | 332 | return false; | ||
333 | } | 333 | } | ||
334 | 334 | | |||
335 | void Device::privateReceivedPackage(const NetworkPackage& np) | 335 | void Device::privateReceivedPackage(const NetworkPackage& np) | ||
336 | { | 336 | { | ||
337 | Q_ASSERT(np.type() != PACKAGE_TYPE_PAIR); | 337 | Q_ASSERT(np.type() != PACKAGE_TYPE_PAIR); | ||
338 | if (isTrusted()) { | 338 | if (isTrusted()) { | ||
339 | const QList<KdeConnectPlugin*> plugins = m_pluginsByIncomingCapability.values(np.type()); | 339 | const QList<KdeConnectPlugin*> plugins = m_pluginsByIncomingCapability.values(np.type()); | ||
340 | if (plugins.isEmpty()) { | 340 | if (plugins.isEmpty()) { | ||
341 | qWarning() << "discarding unsupported package" << np.type() << "for" << name(); | 341 | qWarning() << "discarding unsupported package" << np.type() << "for" << name(); | ||
342 | } | 342 | } | ||
343 | Q_FOREACH (KdeConnectPlugin* plugin, plugins) { | 343 | for (KdeConnectPlugin* plugin : plugins) { | ||
344 | plugin->receivePackage(np); | 344 | plugin->receivePackage(np); | ||
345 | } | 345 | } | ||
346 | } else { | 346 | } else { | ||
347 | qCDebug(KDECONNECT_CORE) << "device" << name() << "not paired, ignoring package" << np.type(); | 347 | qCDebug(KDECONNECT_CORE) << "device" << name() << "not paired, ignoring package" << np.type(); | ||
348 | unpair(); | 348 | unpair(); | ||
349 | } | 349 | } | ||
350 | 350 | | |||
351 | } | 351 | } | ||
352 | 352 | | |||
353 | bool Device::isTrusted() const | 353 | bool Device::isTrusted() const | ||
354 | { | 354 | { | ||
355 | return KdeConnectConfig::instance()->trustedDevices().contains(id()); | 355 | return KdeConnectConfig::instance()->trustedDevices().contains(id()); | ||
356 | } | 356 | } | ||
357 | 357 | | |||
358 | QStringList Device::availableLinks() const | 358 | QStringList Device::availableLinks() const | ||
359 | { | 359 | { | ||
360 | QStringList sl; | 360 | QStringList sl; | ||
361 | sl.reserve(m_deviceLinks.size()); | 361 | sl.reserve(m_deviceLinks.size()); | ||
362 | Q_FOREACH(DeviceLink* dl, m_deviceLinks) { | 362 | for (DeviceLink* dl : qAsConst(m_deviceLinks)) { | ||
363 | sl.append(dl->provider()->name()); | 363 | sl.append(dl->provider()->name()); | ||
364 | } | 364 | } | ||
365 | return sl; | 365 | return sl; | ||
366 | } | 366 | } | ||
367 | 367 | | |||
368 | void Device::cleanUnneededLinks() { | 368 | void Device::cleanUnneededLinks() { | ||
369 | if (isTrusted()) { | 369 | if (isTrusted()) { | ||
370 | return; | 370 | return; | ||
▲ Show 20 Lines • Show All 107 Lines • Show Last 20 Lines |