ctkCmdLineModuleManager.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "ctkCmdLineModuleReference.h"
  18. #include "ctkCmdLineModuleReferencePrivate.h"
  19. #include "ctkCmdLineModuleInstanceFactory.h"
  20. #include <ctkException.h>
  21. #include <QStringList>
  22. #include <QBuffer>
  23. #include <QProcess>
  24. #include <QFuture>
  25. struct ctkCmdLineModuleManagerPrivate
  26. {
  27. ctkCmdLineModuleInstanceFactory* InstanceFactory;
  28. QHash<QString, ctkCmdLineModuleReference> Cache;
  29. };
  30. ctkCmdLineModuleManager::ctkCmdLineModuleManager(ctkCmdLineModuleInstanceFactory *instanceFactory,
  31. ValidationMode validationMode)
  32. : d(new ctkCmdLineModuleManagerPrivate)
  33. {
  34. d->InstanceFactory = instanceFactory;
  35. }
  36. ctkCmdLineModuleManager::~ctkCmdLineModuleManager()
  37. {
  38. delete d->InstanceFactory;
  39. }
  40. ctkCmdLineModuleReference
  41. ctkCmdLineModuleManager::registerModule(const QString& location)
  42. {
  43. QProcess process;
  44. process.setReadChannel(QProcess::StandardOutput);
  45. process.start(location, QStringList("--xml"));
  46. ctkCmdLineModuleReference ref;
  47. ref.d->Location = location;
  48. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  49. process.error() != QProcess::UnknownError)
  50. {
  51. qWarning() << "The executable at" << location << "could not be started:" << process.errorString();
  52. return ref;
  53. }
  54. process.waitForReadyRead();
  55. QByteArray xml = process.readAllStandardOutput();
  56. qDebug() << xml;
  57. // validate the outputted xml description
  58. QBuffer input(&xml);
  59. input.open(QIODevice::ReadOnly);
  60. ctkCmdLineModuleXmlValidator validator(&input);
  61. if (!validator.validateInput())
  62. {
  63. qCritical() << validator.errorString();
  64. return ref;
  65. }
  66. ref.d->RawXmlDescription = xml;
  67. // ref.d->objectRepresentation = descriptionFactory->createObjectRepresentationFromXML(ref.d->xml);
  68. // ref.d->setGUI(descriptionFactory->createGUIFromXML(ref.d->xml));
  69. d->Cache[location] = ref;
  70. return ref;
  71. }
  72. void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference&)
  73. {
  74. throw ctkException("not implemented yet");
  75. }
  76. ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QString& location) const
  77. {
  78. throw ctkException("not implemented yet");
  79. }
  80. QList<ctkCmdLineModuleReference> ctkCmdLineModuleManager::moduleReferences() const
  81. {
  82. throw ctkException("not implemented yet");
  83. }
  84. ctkCmdLineModuleInstance*
  85. ctkCmdLineModuleManager::createModuleInstance(const ctkCmdLineModuleReference& moduleRef)
  86. {
  87. return d->InstanceFactory->create(moduleRef);
  88. }
  89. QList<ctkCmdLineModuleInstance*>
  90. ctkCmdLineModuleManager::moduleInstances(const ctkCmdLineModuleReference& moduleRef) const
  91. {
  92. throw ctkException("not implemented yet");
  93. }