ctkMetaTypeProviderImpl.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "ctkMetaTypeProviderImpl_p.h"
  16. #include "ctkObjectClassDefinitionImpl_p.h"
  17. #include "ctkAttributeDefinitionImpl_p.h"
  18. #include "ctkMTMsg_p.h"
  19. #include "ctkMTDataParser_p.h"
  20. #include <ctkPluginConstants.h>
  21. #include <service/log/ctkLogService.h>
  22. #include <service/metatype/ctkMetaTypeService.h>
  23. #include <QCoreApplication>
  24. #include <QStringList>
  25. #include <QBuffer>
  26. const QString ctkMetaTypeProviderImpl::METADATA_NOT_FOUND = "METADATA_NOT_FOUND";
  27. const QString ctkMetaTypeProviderImpl::OCD_ID_NOT_FOUND = "OCD_ID_NOT_FOUND";
  28. const QString ctkMetaTypeProviderImpl::ASK_INVALID_LOCALE = "ASK_INVALID_LOCALE";
  29. const QString ctkMetaTypeProviderImpl::META_FILE_EXT = ".XML";
  30. const QString ctkMetaTypeProviderImpl::RESOURCE_FILE_CONN = "_";
  31. const QString ctkMetaTypeProviderImpl::RESOURCE_FILE_EXT = ".qm";
  32. const QChar ctkMetaTypeProviderImpl::DIRECTORY_SEP = '/';
  33. ctkMetaTypeProviderImpl::ctkMetaTypeProviderImpl(
  34. const QSharedPointer<ctkPlugin>& plugin, ctkLogService* logger)
  35. : _plugin(plugin), logger(logger), _isThereMeta(false)
  36. {
  37. // read all plugin's metadata files and build internal data structures
  38. _isThereMeta = readMetaFiles(plugin);
  39. if (!_isThereMeta)
  40. {
  41. CTK_DEBUG(logger) << QCoreApplication::translate(ctkMTMsg::CONTEXT, ctkMTMsg::METADATA_NOT_FOUND)
  42. .arg(plugin->getPluginId()).arg(plugin->getSymbolicName());
  43. }
  44. }
  45. ctkObjectClassDefinitionPtr ctkMetaTypeProviderImpl::getObjectClassDefinition(
  46. const QString& pid, const QLocale& locale)
  47. {
  48. ctkObjectClassDefinitionImplPtr ocd;
  49. if (_allPidOCDs.contains(pid))
  50. {
  51. ocd = ctkObjectClassDefinitionImplPtr(new ctkObjectClassDefinitionImpl(*_allPidOCDs.value(pid).data()));
  52. ocd->setPluginLocalization(locale, _plugin);
  53. return ocd;
  54. }
  55. else if (_allFPidOCDs.contains(pid))
  56. {
  57. ocd = ctkObjectClassDefinitionImplPtr(new ctkObjectClassDefinitionImpl(*_allFPidOCDs.value(pid).data()));
  58. ocd->setPluginLocalization(locale, _plugin);
  59. return ocd;
  60. }
  61. else
  62. {
  63. QString msg = QCoreApplication::translate(ctkMTMsg::CONTEXT, ctkMTMsg::OCD_ID_NOT_FOUND).arg(pid);
  64. throw std::invalid_argument(msg.toStdString());
  65. }
  66. }
  67. QList<QLocale> ctkMetaTypeProviderImpl::getLocales() const
  68. {
  69. if (!_locales.isEmpty())
  70. return checkForDefault(_locales);
  71. QStringList localizationFiles;
  72. // get all the localization resources for PIDS
  73. foreach(ctkObjectClassDefinitionImplPtr ocd, _allPidOCDs)
  74. {
  75. QString loc = ocd->getLocalization();
  76. if (!loc.isEmpty() && !localizationFiles.contains(loc))
  77. {
  78. localizationFiles << loc;
  79. }
  80. }
  81. // get all the localization resources for FPIDS
  82. foreach(ctkObjectClassDefinitionImplPtr ocd, _allFPidOCDs)
  83. {
  84. QString loc = ocd->getLocalization();
  85. if (!loc.isEmpty() && !localizationFiles.contains(loc))
  86. {
  87. localizationFiles << loc;
  88. }
  89. }
  90. if (localizationFiles.isEmpty())
  91. {
  92. localizationFiles << ctkPluginConstants::PLUGIN_LOCALIZATION_DEFAULT_BASENAME;
  93. }
  94. QList<QLocale> locales;
  95. foreach (QString localizationFile, localizationFiles)
  96. {
  97. int iSlash = localizationFile.lastIndexOf(DIRECTORY_SEP);
  98. QString baseDir;
  99. QString baseFileName;
  100. if (iSlash < 0)
  101. {
  102. baseDir = "";
  103. }
  104. else
  105. {
  106. baseDir = localizationFile.left(iSlash);
  107. }
  108. baseFileName = localizationFile + RESOURCE_FILE_CONN;
  109. QStringList resources = _plugin->getResourceList(baseDir);
  110. foreach(QString resourceName, resources)
  111. {
  112. QString resource = baseDir + DIRECTORY_SEP + resourceName;
  113. if (resource.startsWith(baseFileName) && resource.toLower().endsWith(RESOURCE_FILE_EXT))
  114. {
  115. QString localeName = resource.mid(baseFileName.size(), resource.size() - baseFileName.size() - RESOURCE_FILE_EXT.length());
  116. locales.push_back(QLocale(localeName));
  117. }
  118. }
  119. }
  120. _locales = locales;
  121. return checkForDefault(_locales);
  122. }
  123. bool ctkMetaTypeProviderImpl::readMetaFiles(const QSharedPointer<ctkPlugin>& plugin)
  124. {
  125. bool isThereMetaHere = false;
  126. QStringList allFileKeys = plugin->getResourceList(ctkMetaTypeService::METATYPE_DOCUMENTS_LOCATION);
  127. if (allFileKeys.isEmpty())
  128. return isThereMetaHere;
  129. foreach (QString fileName, allFileKeys)
  130. {
  131. bool _isMetaDataFile = false;
  132. QHash<QString, ctkObjectClassDefinitionImplPtr> pidToOCD;
  133. QByteArray resourceContent = plugin->getResource(ctkMetaTypeService::METATYPE_DOCUMENTS_LOCATION + "/" + fileName);
  134. if (!resourceContent.isEmpty())
  135. {
  136. QBuffer metaData(&resourceContent);
  137. try
  138. {
  139. // Assume all XML files are what we want by default.
  140. _isMetaDataFile = true;
  141. metaData.open(QIODevice::ReadOnly);
  142. ctkMTDataParser parser(plugin, &metaData, logger);
  143. pidToOCD = parser.doParse();
  144. if (pidToOCD.isEmpty())
  145. {
  146. _isMetaDataFile = false;
  147. }
  148. }
  149. catch (const std::exception& )
  150. {
  151. // Ok, looks like it is not what we want.
  152. _isMetaDataFile = false;
  153. }
  154. if (_isMetaDataFile && !pidToOCD.isEmpty())
  155. {
  156. // We got some OCDs now.
  157. QHash<QString, ctkObjectClassDefinitionImplPtr>::ConstIterator end(pidToOCD.end());
  158. for (QHash<QString, ctkObjectClassDefinitionImplPtr>::ConstIterator it(pidToOCD.begin()); it != end; ++it)
  159. {
  160. QString pid = it.key();
  161. ctkObjectClassDefinitionImplPtr ocd = it.value();
  162. if (ocd->getType() == ctkObjectClassDefinitionImpl::PID)
  163. {
  164. isThereMetaHere = true;
  165. _allPidOCDs.insert(pid, ocd);
  166. }
  167. else
  168. {
  169. isThereMetaHere = true;
  170. _allFPidOCDs.insert(pid, ocd);
  171. }
  172. } // End of for
  173. }
  174. } // End of if(!resourceContent.isEmpty())
  175. } // End of foreach
  176. return isThereMetaHere;
  177. }
  178. QList<QLocale> ctkMetaTypeProviderImpl::checkForDefault(const QList<QLocale>& locales) const
  179. {
  180. if (locales.isEmpty() || (locales.size() == 1 && QLocale() == locales[0]))
  181. return QList<QLocale>();
  182. return locales;
  183. }