252{
254
256 false);
257 if (!cap) {
258 BOOST_LOG_TRIVIAL(warning) << "Printer-agent capability '" << capability_name << "' not found for plugin '" << plugin_key << "'";
259 return;
260 }
261
262 PluginDescriptor descriptor;
263 if (!plugin_manager.try_get_plugin_descriptor(plugin_key, descriptor)) {
264 BOOST_LOG_TRIVIAL(warning) << "Could not find descriptor for plugin key: '" << plugin_key;
265 return;
266 }
267 if (!cap->is_enabled()) {
268 BOOST_LOG_TRIVIAL(warning) << "Printer-agent capability '" << capability_name << "' is disabled for plugin '" << plugin_key << "'";
269 return;
270 }
271
272 auto plugin = std::dynamic_pointer_cast<PrinterAgentPluginCapability>(cap);
273
274 if (!plugin) {
275 BOOST_LOG_TRIVIAL(warning) << "Loaded plugin capability '" << capability_name << "' is not a printer-agent plugin";
276 return;
277 }
278
279 auto info = plugin->get_agent_info();
280
281 if (info.id.empty()) {
282 BOOST_LOG_TRIVIAL(warning) << "Printer-agent plugin '" << capability_name << "' returned an empty agent ID";
283 return;
284 }
285
286 const auto capability_key = std::make_pair(plugin_key, capability_name);
287 std::shared_ptr<PythonPrinterAgentRegistrationToken> registration_token;
288 {
289 std::lock_guard<std::mutex>
lock(s_registry_mutex);
291 [&plugin_key, &capability_name](const auto& entry) {
292 return entry.first.first == plugin_key && entry.first.second == capability_name;
293 });
295 registration_token = std::make_shared<PythonPrinterAgentRegistrationToken>();
297 } else {
298 registration_token = token_it->second;
299 }
300 }
301
303
304 std::weak_ptr<PrinterAgentPluginCapability> weak_plugin = plugin;
305 PrinterAgentFactory factory = [weak_plugin](std::shared_ptr<ICloudServiceAgent> cloud_agent,
306 const std::string& ) -> std::shared_ptr<IPrinterAgent> {
307
308
309 auto plugin = weak_plugin.lock();
310 if (!plugin)
311 return nullptr;
312
313 if (cloud_agent)
314 plugin->set_cloud_agent(cloud_agent);
315 return plugin;
316 };
317
318
319
320
321
323 if (current_cap != cap) {
324 BOOST_LOG_TRIVIAL(debug) << "Printer-agent capability '" << capability_name << "' from plugin '" << plugin_key
325 << "' changed or was disabled during registration";
326 return;
327 }
328
329 std::shared_ptr<IPrinterAgent> cached_agent;
330
331 auto reject_conflicting_capability = [plugin_key, capability_name](const std::string& error_message) {
332 if (!wxTheApp || GUI::wxGetApp().is_closing())
333 return;
334 GUI::wxGetApp().CallAfter([plugin_key, capability_name, error_message]() {
335 if (GUI::wxGetApp().is_closing())
336 return;
338 manager.set_plugin_error(plugin_key, error_message);
339
340
342 wxMessageBox(wxString::FromUTF8(error_message.c_str()),
_L(
"Plugins"), wxOK | wxICON_WARNING, GUI::wxGetApp().GetTopWindow());
343 });
344 };
345
346 {
347 std::lock_guard<std::mutex>
lock(s_registry_mutex);
348
350 [&plugin_key, &capability_name](const auto& entry) {
351 return entry.first.first == plugin_key && entry.first.second == capability_name;
352 });
353 const bool cap_still_enabled = cap->is_enabled();
355 !cap_still_enabled) {
356 BOOST_LOG_TRIVIAL(debug) << "Printer-agent capability '" << capability_name << "' from plugin '" << plugin_key
357 << "' was disabled before registry commit";
358 return;
359 }
360
362 for (const auto& pair : python_agent_ids) {
363 if (pair.first != capability_key && pair.second == info.id) {
365
366 _u8L(
"Printer-agent '%1%' could not be enabled: agent ID '%2%' is already registered by capability '%3%' from plugin '%4%'."),
367 info.name, info.id, pair.first.second, pair.first.first);
368 BOOST_LOG_TRIVIAL(warning) << error_message;
369 reject_conflicting_capability(error_message);
370 return;
371 }
372 }
373
374 auto plugin_it = python_agent_ids.find(capability_key);
375 if (plugin_it != python_agent_ids.end() && plugin_it->second != info.id) {
376 const std::string old_agent_id = plugin_it->second;
378
380 auto cache_it = cache.find(old_agent_id);
381 if (cache_it != cache.end()) {
382 cached_agent = std::move(cache_it->second);
383 cache.erase(cache_it);
384 }
385 }
386
388 auto agent_it = agents.find(info.id);
389
390
391
392
393 if (agent_it != agents.end() && agent_it->second.plugin_identifier != plugin_full_ref) {
395
396 _u8L(
"Printer-agent '%1%' could not be enabled: agent ID '%2%' is already registered by '%3%'."),
397 info.name, info.id, agent_it->second.display_name);
398 BOOST_LOG_TRIVIAL(warning) << error_message;
399 reject_conflicting_capability(error_message);
400 return;
401 }
402
403
404
405
406
407 agents.insert_or_assign(info.id, PrinterAgentInfo(info.id, info.name, plugin_full_ref, std::move(factory)));
408 python_agent_ids[capability_key] = info.id;
409
411 auto cache_it = cache.find(info.id);
412 if (cache_it != cache.end()) {
413 cached_agent = std::move(cache_it->second);
414 cache.erase(cache_it);
415 }
416 }
417
418 if (cached_agent)
419 cached_agent->disconnect_printer();
420
421 BOOST_LOG_TRIVIAL(info) << "Registered printer-agent plugin '" << capability_name << "' with agent ID '" << info.id
422 << "' and plugin ref '" << plugin_full_ref << "'";
423}
#define _L(s)
Definition OBJ.cpp:34
std::string format(const char *fmt, TArgs &&... args)
Definition format.hpp:36
std::string plugin_printer_agent_full_ref(const PluginDescriptor &descriptor, const std::string &capability_name)
Definition NetworkAgentFactory.cpp:60
std::function< std::shared_ptr< IPrinterAgent >(std::shared_ptr< ICloudServiceAgent > cloud_agent, const std::string &log_dir)> PrinterAgentFactory
Definition NetworkAgentFactory.hpp:24
#define _u8L(s)
Definition I18N.hpp:11