ctkMetaTypeProviderImpl.cpp 6.7 KB

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