ctkCmdLineModuleManager.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkCmdLineModuleManager.h"
  16. #include "ctkCmdLineModuleXmlValidator.h"
  17. #include "ctkCmdLineModuleObjectHierarchyReader.h"
  18. #include "ctkCmdLineModuleProcessRunner_p.h"
  19. #include "ctkCmdLineModuleReference.h"
  20. #include "ctkCmdLineModuleReferencePrivate.h"
  21. #include "ctkCmdLineModuleInstanceFactory.h"
  22. #include <ctkException.h>
  23. #include <QStringList>
  24. #include <QBuffer>
  25. #include <QProcess>
  26. #include <QFuture>
  27. struct ctkCmdLineModuleManagerPrivate
  28. {
  29. ctkCmdLineModuleInstanceFactory* InstanceFactory;
  30. QHash<QString, ctkCmdLineModuleReference> Cache;
  31. };
  32. ctkCmdLineModuleManager::ctkCmdLineModuleManager(ctkCmdLineModuleInstanceFactory *instanceFactory,
  33. ValidationMode validationMode)
  34. : d(new ctkCmdLineModuleManagerPrivate)
  35. {
  36. d->InstanceFactory = instanceFactory;
  37. }
  38. ctkCmdLineModuleManager::~ctkCmdLineModuleManager()
  39. {
  40. delete d->InstanceFactory;
  41. }
  42. ctkCmdLineModuleReference
  43. ctkCmdLineModuleManager::registerModule(const QString& location)
  44. {
  45. QProcess process;
  46. process.setReadChannel(QProcess::StandardOutput);
  47. process.start(location, QStringList("--xml"));
  48. ctkCmdLineModuleReference ref;
  49. ref.d->Location = location;
  50. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  51. process.error() != QProcess::UnknownError)
  52. {
  53. qWarning() << "The executable at" << location << "could not be started:" << process.errorString();
  54. return ref;
  55. }
  56. process.waitForReadyRead();
  57. QByteArray xml = process.readAllStandardOutput();
  58. qDebug() << xml;
  59. // validate the outputted xml description
  60. QBuffer input(&xml);
  61. input.open(QIODevice::ReadOnly);
  62. ctkCmdLineModuleXmlValidator validator(&input);
  63. if (!validator.validateInput())
  64. {
  65. qCritical() << validator.errorString();
  66. return ref;
  67. }
  68. ref.d->RawXmlDescription = xml;
  69. // ref.d->objectRepresentation = descriptionFactory->createObjectRepresentationFromXML(ref.d->xml);
  70. // ref.d->setGUI(descriptionFactory->createGUIFromXML(ref.d->xml));
  71. d->Cache[location] = ref;
  72. return ref;
  73. }
  74. void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference&)
  75. {
  76. throw ctkException("not implemented yet");
  77. }
  78. ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QString& location) const
  79. {
  80. throw ctkException("not implemented yet");
  81. }
  82. QList<ctkCmdLineModuleReference> ctkCmdLineModuleManager::moduleReferences() const
  83. {
  84. throw ctkException("not implemented yet");
  85. }
  86. ctkCmdLineModuleInstance*
  87. ctkCmdLineModuleManager::createModuleInstance(const ctkCmdLineModuleReference& moduleRef)
  88. {
  89. return d->InstanceFactory->create(moduleRef);
  90. }
  91. QList<ctkCmdLineModuleInstance*>
  92. ctkCmdLineModuleManager::moduleInstances(const ctkCmdLineModuleReference& moduleRef) const
  93. {
  94. throw ctkException("not implemented yet");
  95. }