ctkCmdLineModuleManager.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "ctkCmdLineModuleReference_p.h"
  19. #include "ctkCmdLineModuleFactory.h"
  20. #include <ctkException.h>
  21. #include <QStringList>
  22. #include <QBuffer>
  23. #include <QProcess>
  24. #include <QFuture>
  25. struct ctkCmdLineModuleManagerPrivate
  26. {
  27. ctkCmdLineModuleManagerPrivate()
  28. : Verbose(false)
  29. {}
  30. ctkCmdLineModuleFactory* InstanceFactory;
  31. QHash<QString, ctkCmdLineModuleReference> Cache;
  32. bool Verbose;
  33. };
  34. ctkCmdLineModuleManager::ctkCmdLineModuleManager(ctkCmdLineModuleFactory *instanceFactory,
  35. ValidationMode validationMode)
  36. : d(new ctkCmdLineModuleManagerPrivate)
  37. {
  38. d->InstanceFactory = instanceFactory;
  39. }
  40. ctkCmdLineModuleManager::~ctkCmdLineModuleManager()
  41. {
  42. }
  43. void ctkCmdLineModuleManager::setVerboseOutput(bool verbose)
  44. {
  45. d->Verbose = verbose;
  46. }
  47. bool ctkCmdLineModuleManager::verboseOutput() const
  48. {
  49. return d->Verbose;
  50. }
  51. ctkCmdLineModuleReference
  52. ctkCmdLineModuleManager::registerModule(const QString& location)
  53. {
  54. QProcess process;
  55. process.setReadChannel(QProcess::StandardOutput);
  56. process.start(location, QStringList("--xml"));
  57. ctkCmdLineModuleReference ref;
  58. ref.d->Location = location;
  59. if (!process.waitForFinished() || process.exitStatus() == QProcess::CrashExit ||
  60. process.error() != QProcess::UnknownError)
  61. {
  62. if(d->Verbose)
  63. {
  64. qWarning() << "The executable at" << location << "could not be started:" << process.errorString();
  65. }
  66. return ref;
  67. }
  68. process.waitForReadyRead();
  69. QByteArray xml = process.readAllStandardOutput();
  70. // validate the outputted xml description
  71. QBuffer input(&xml);
  72. input.open(QIODevice::ReadOnly);
  73. ctkCmdLineModuleXmlValidator validator(&input);
  74. if (!validator.validateInput())
  75. {
  76. if(d->Verbose)
  77. {
  78. qWarning() << validator.errorString();
  79. }
  80. return ref;
  81. }
  82. ref.d->RawXmlDescription = xml;
  83. d->Cache[location] = ref;
  84. emit moduleAdded(ref);
  85. return ref;
  86. }
  87. void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference& ref)
  88. {
  89. d->Cache.remove(ref.location());
  90. emit moduleRemoved(ref);
  91. }
  92. ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QString& location) const
  93. {
  94. return d->Cache[location];
  95. }
  96. QList<ctkCmdLineModuleReference> ctkCmdLineModuleManager::moduleReferences() const
  97. {
  98. return d->Cache.values();
  99. }
  100. ctkCmdLineModule*
  101. ctkCmdLineModuleManager::createModule(const ctkCmdLineModuleReference& moduleRef)
  102. {
  103. return d->InstanceFactory->create(moduleRef);
  104. }
  105. QList<ctkCmdLineModule*>
  106. ctkCmdLineModuleManager::modules(const ctkCmdLineModuleReference& moduleRef) const
  107. {
  108. throw ctkException("not implemented yet");
  109. }