38 #include <QTextStream>
51 mConfigurationFilePath(absoluteConfigFilePath), mLoggingFolder(loggingFolder), mConfigTag(
52 "configuration"), mConfigTrackerTag(
"tracker"), mConfigTrackerToolFile(
"toolfile"), mTypeAttribute(
53 "type"), mClinicalAppAttribute(
"clinical_app"), mReferenceAttribute(
"reference")
55 this->setConfigDocument(mConfigurationFilePath);
64 if (!this->isConfigFileValid())
67 QDomNode configNode = mConfigureDoc.elementsByTagName(mConfigTag).at(0);
68 QString retval = configNode.toElement().attribute(mClinicalAppAttribute);
74 std::vector<ToolFileParser::TrackerInternalStructure> retval;
76 if (!this->isConfigFileValid())
79 QDomNodeList trackerNodes = mConfigureDoc.elementsByTagName(mConfigTrackerTag);
80 for (
int i = 0; i < trackerNodes.count(); ++i)
83 QString trackerType = trackerNodes.at(i).toElement().attribute(mTypeAttribute);
84 internalStructure.
mType = string2enum<TRACKING_SYSTEM>(trackerType);
87 retval.push_back(internalStructure);
90 if (retval.size() > 1)
92 "Config file " + mConfigurationFilePath
93 +
" has a invalid number of tracking systems, we only support 1 tracking system atm!");
100 std::vector<QString> retval;
102 if (!this->isConfigFileValid())
105 QDomNodeList toolFileNodes = mConfigureDoc.elementsByTagName(mConfigTrackerToolFile);
106 for (
int i = 0; i < toolFileNodes.count(); ++i)
108 QString absoluteToolFilePath = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
109 if (absoluteToolFilePath.isEmpty())
112 retval.push_back(absoluteToolFilePath);
122 if (!this->isConfigFileValid())
129 QDomNodeList toolFileNodes = mConfigureDoc.elementsByTagName(mConfigTrackerToolFile);
130 for (
int i = 0; i < toolFileNodes.count(); ++i)
132 QString reference = toolFileNodes.at(i).toElement().attribute(mReferenceAttribute);
133 if (reference.contains(
"yes", Qt::CaseInsensitive))
136 retval = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
148 QString ConfigurationFileParser::convertToRelativeToolFilePath(QString configFilename, QString absoluteToolFilePath)
150 foreach (QString root,
profile()->getAllRootConfigPaths())
152 QString configPath = getToolPathFromRoot(root);
153 if (!absoluteToolFilePath.contains(configPath))
155 absoluteToolFilePath.replace(configPath,
"");
156 if (absoluteToolFilePath.startsWith(
"/"))
157 absoluteToolFilePath.remove(0, 1);
158 return absoluteToolFilePath;
162 return absoluteToolFilePath;
165 QString ConfigurationFileParser::getToolPathFromRoot(QString root)
167 return root +
"/tool/Tools/";
173 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"\"1.0\""));
175 QDomElement configNode = doc.createElement(
"configuration");
176 configNode.setAttribute(
"clinical_app", config.
mClinical_app);
182 QDomElement trackerTagNode = doc.createElement(
"tracker");
183 trackerTagNode.setAttribute(
"type", trackerType);
185 ToolFilesAndReferenceVector::iterator it2 = it1->second.begin();
186 for (; it2 != it1->second.end(); ++it2)
188 QString absoluteToolFilePath = it2->first;
189 QString relativeToolFilePath = convertToRelativeToolFilePath(config.
mFileName, absoluteToolFilePath);
195 reportWarning(
"When saving configuration, skipping tool " + relativeToolFilePath +
" of type "
196 + toolTrackerType +
" because tracker is set to " + trackerType);
200 QDomElement toolFileNode = doc.createElement(
"toolfile");
201 toolFileNode.appendChild(doc.createTextNode(relativeToolFilePath));
202 toolFileNode.setAttribute(
"reference", (it2->second ?
"yes" :
"no"));
203 trackerTagNode.appendChild(toolFileNode);
205 configNode.appendChild(trackerTagNode);
208 doc.appendChild(configNode);
212 QDir().mkpath(QFileInfo(config.
mFileName).absolutePath());
214 if (!file.open(QIODevice::WriteOnly))
216 reportWarning(
"Could not open file " + file.fileName() +
", aborting writing of config.");
219 QTextStream stream(&file);
221 reportSuccess(
"Configuration file " + file.fileName() +
" is written.");
224 void ConfigurationFileParser::setConfigDocument(QString configAbsoluteFilePath)
226 QFile configFile(configAbsoluteFilePath);
227 if (!configFile.exists())
233 if (!mConfigureDoc.setContent(&configFile))
235 reportError(
"Could not set the xml content of the config file " + configAbsoluteFilePath);
240 bool ConfigurationFileParser::isConfigFileValid()
243 QDomNode configNode = mConfigureDoc.elementsByTagName(mConfigTag).item(0);
244 if (configNode.isNull())
252 QString ConfigurationFileParser::findXmlFileWithDirNameInPath(QString path)
256 filter << dir.dirName() +
".xml";
257 QStringList filepaths = dir.entryList(filter);
258 if (!filepaths.isEmpty())
259 return dir.absoluteFilePath(filter[0]);
263 QString ConfigurationFileParser::searchForExistingToolFilePath(QString relativeToolFilePath)
266 relativeToolFilePath.replace(
"../Tools/",
"");
268 foreach (QString root,
profile()->getAllRootConfigPaths())
270 QString configPath = this->getToolPathFromRoot(root);
271 QFileInfo guess(configPath +
"/" + relativeToolFilePath);
273 return guess.canonicalFilePath();
278 QString ConfigurationFileParser::getAbsoluteToolFilePath(QDomElement toolfileelement)
280 QString relativeToolFilePath = toolfileelement.text();
281 if (relativeToolFilePath.isEmpty())
284 QString absoluteToolFilePath = this->searchForExistingToolFilePath(relativeToolFilePath);
286 QFileInfo info(absoluteToolFilePath);
288 reportError(QString(
"Tool file %1 in configuration %2 not found. Skipping.")
289 .arg(relativeToolFilePath)
290 .arg(mConfigurationFilePath));
293 absoluteToolFilePath = this->findXmlFileWithDirNameInPath(absoluteToolFilePath);
294 return absoluteToolFilePath;
TrackersAndToolsMap mTrackersAndTools
the trackers and tools (relative path) that should be used in the config
cxResource_EXPORT ProfilePtr profile()
void reportError(QString msg)
~ConfigurationFileParser()
QString getApplicationapplication()
static QString getTemplatesAbsoluteFilePath()
QString mFileName
absolute path and filename for the new config file
ConfigurationFileParser(QString absoluteConfigFilePath, QString loggingFolder="")
QString getAbsoluteReferenceFilePath()
static void saveConfiguration(Configuration &config)
void reportWarning(QString msg)
void reportSuccess(QString msg)
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
std::vector< QString > getAbsoluteToolFilePaths()
QString mClinical_app
the clinical application this config is made for
std::vector< ToolFileParser::TrackerInternalStructure > getTrackers()
QString enum2string(const ENUM &val)