ctkCmdLineModuleBackendXMLChecker.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) University College London
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =============================================================================*/
  14. #include "ctkCmdLineModuleBackendXMLChecker.h"
  15. #include "ctkCmdLineModuleXMLCheckerTask_p.h"
  16. #include "ctkCmdLineModuleFrontend.h"
  17. #include "ctkCmdLineModuleFuture.h"
  18. #include <QUrl>
  19. #include <QFile>
  20. #include <QString>
  21. #include <QDateTime>
  22. #include <ctkUtils.h>
  23. //----------------------------------------------------------------------------
  24. struct ctkCmdLineModuleBackendXMLCheckerPrivate
  25. {
  26. QString m_HardCodedXML;
  27. QDateTime m_LastModified;
  28. };
  29. //----------------------------------------------------------------------------
  30. ctkCmdLineModuleBackendXMLChecker::ctkCmdLineModuleBackendXMLChecker()
  31. : d(new ctkCmdLineModuleBackendXMLCheckerPrivate)
  32. {
  33. d->m_HardCodedXML = QString("");
  34. d->m_LastModified = QDateTime::currentDateTime();
  35. }
  36. //----------------------------------------------------------------------------
  37. ctkCmdLineModuleBackendXMLChecker::ctkCmdLineModuleBackendXMLChecker(const QString &xmlToValidate)
  38. : d(new ctkCmdLineModuleBackendXMLCheckerPrivate)
  39. {
  40. d->m_HardCodedXML = xmlToValidate;
  41. d->m_LastModified = QDateTime::currentDateTime();
  42. }
  43. //----------------------------------------------------------------------------
  44. ctkCmdLineModuleBackendXMLChecker::~ctkCmdLineModuleBackendXMLChecker()
  45. {
  46. }
  47. //----------------------------------------------------------------------------
  48. void ctkCmdLineModuleBackendXMLChecker::setXML(const QString& xml)
  49. {
  50. d->m_HardCodedXML = xml;
  51. d->m_LastModified = QDateTime::currentDateTime();
  52. }
  53. //----------------------------------------------------------------------------
  54. QString ctkCmdLineModuleBackendXMLChecker::xml() const
  55. {
  56. return d->m_HardCodedXML;
  57. }
  58. //----------------------------------------------------------------------------
  59. QString ctkCmdLineModuleBackendXMLChecker::name() const
  60. {
  61. return "XML Checker";
  62. }
  63. //----------------------------------------------------------------------------
  64. QString ctkCmdLineModuleBackendXMLChecker::description() const
  65. {
  66. return "Fakes a backend process, returning a static piece of XML.";
  67. }
  68. //----------------------------------------------------------------------------
  69. QList<QString> ctkCmdLineModuleBackendXMLChecker::schemes() const
  70. {
  71. static QList<QString> supportedSchemes = QList<QString>() << "xmlchecker";
  72. return supportedSchemes;
  73. }
  74. //----------------------------------------------------------------------------
  75. qint64 ctkCmdLineModuleBackendXMLChecker::timeStamp(const QUrl & /*location*/) const
  76. {
  77. return ctk::msecsTo(QDateTime::fromTime_t(0), d->m_LastModified);
  78. }
  79. //----------------------------------------------------------------------------
  80. QByteArray ctkCmdLineModuleBackendXMLChecker::rawXmlDescription(const QUrl& location)
  81. {
  82. if (location.isValid())
  83. {
  84. QFile file(location.path());
  85. file.open(QFile::ReadOnly);
  86. return file.readAll();
  87. }
  88. return d->m_HardCodedXML.toAscii();
  89. }
  90. //----------------------------------------------------------------------------
  91. ctkCmdLineModuleFuture ctkCmdLineModuleBackendXMLChecker::run(ctkCmdLineModuleFrontend* /*frontend*/)
  92. {
  93. // Instances of ctkCmdLineModuleProcessTask are auto-deleted by the thread pool.
  94. ctkCmdLineModuleXMLCheckerTask* moduleProcess = new ctkCmdLineModuleXMLCheckerTask();
  95. return moduleProcess->start();
  96. }