ctkXnatFile.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*=============================================================================
  2. Library: XNAT/Core
  3. Copyright (c) University College London,
  4. Centre for Medical Image Computing
  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 "ctkXnatFile.h"
  16. #include "ctkXnatException.h"
  17. #include "ctkXnatObjectPrivate.h"
  18. #include "ctkXnatSession.h"
  19. #include <QFile>
  20. const QString ctkXnatFile::FILE_NAME = "Name";
  21. const QString ctkXnatFile::FILE_TAGS = "file_tags";
  22. const QString ctkXnatFile::FILE_FORMAT = "file_format";
  23. const QString ctkXnatFile::FILE_CONTENT = "file_content";
  24. //----------------------------------------------------------------------------
  25. class ctkXnatFilePrivate : public ctkXnatObjectPrivate
  26. {
  27. public:
  28. ctkXnatFilePrivate()
  29. : ctkXnatObjectPrivate()
  30. {
  31. }
  32. void reset()
  33. {
  34. }
  35. QString localFilePath;
  36. };
  37. //----------------------------------------------------------------------------
  38. ctkXnatFile::ctkXnatFile(ctkXnatObject* parent, const QString& schemaType)
  39. : ctkXnatObject(*new ctkXnatFilePrivate(), parent, schemaType)
  40. {
  41. }
  42. //----------------------------------------------------------------------------
  43. ctkXnatFile::~ctkXnatFile()
  44. {
  45. }
  46. //----------------------------------------------------------------------------
  47. void ctkXnatFile::setName(const QString &name)
  48. {
  49. this->setProperty(FILE_NAME, name);
  50. }
  51. //----------------------------------------------------------------------------
  52. QString ctkXnatFile::name() const
  53. {
  54. return this->property(FILE_NAME);
  55. }
  56. //----------------------------------------------------------------------------
  57. void ctkXnatFile::setFileFormat(const QString &fileFormat)
  58. {
  59. this->setProperty(FILE_FORMAT, fileFormat);
  60. }
  61. //----------------------------------------------------------------------------
  62. QString ctkXnatFile::fileFormat() const
  63. {
  64. return this->property(FILE_FORMAT);
  65. }
  66. //----------------------------------------------------------------------------
  67. void ctkXnatFile::setFileContent(const QString &fileContent)
  68. {
  69. this->setProperty(FILE_CONTENT, fileContent);
  70. }
  71. //----------------------------------------------------------------------------
  72. QString ctkXnatFile::fileContent() const
  73. {
  74. return this->property(FILE_CONTENT);
  75. }
  76. //----------------------------------------------------------------------------
  77. void ctkXnatFile::setFileTags(const QString &fileTags)
  78. {
  79. this->setProperty(FILE_TAGS, fileTags);
  80. }
  81. //----------------------------------------------------------------------------
  82. QString ctkXnatFile::fileTags() const
  83. {
  84. return this->property(FILE_TAGS);
  85. }
  86. //----------------------------------------------------------------------------
  87. void ctkXnatFile::setLocalFilePath(const QString &filePath)
  88. {
  89. Q_D(ctkXnatFile);
  90. d->localFilePath = filePath;
  91. }
  92. //----------------------------------------------------------------------------
  93. QString ctkXnatFile::localFilePath() const
  94. {
  95. Q_D(const ctkXnatFile);
  96. return d->localFilePath;
  97. }
  98. //----------------------------------------------------------------------------
  99. QString ctkXnatFile::resourceUri() const
  100. {
  101. return QString("%1/files/%2").arg(parent()->resourceUri(), this->name());
  102. }
  103. //----------------------------------------------------------------------------
  104. void ctkXnatFile::reset()
  105. {
  106. ctkXnatObject::reset();
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkXnatFile::fetchImpl()
  110. {
  111. }
  112. //----------------------------------------------------------------------------
  113. void ctkXnatFile::downloadImpl(const QString& filename)
  114. {
  115. QString query = this->resourceUri();
  116. this->session()->download(filename, query);
  117. }
  118. //----------------------------------------------------------------------------
  119. void ctkXnatFile::saveImpl()
  120. {
  121. QString query = this->resourceUri();
  122. QString filename = this->localFilePath();
  123. QFile file(filename);
  124. if (!file.exists())
  125. {
  126. QString msg = "Error uploading file! ";
  127. msg.append(QString("File \"%1\" does not exist!").arg(filename));
  128. throw ctkXnatException(msg);
  129. }
  130. // Creating the update query
  131. query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
  132. const QMap<QString, QString>& properties = this->properties();
  133. QMapIterator<QString, QString> itProperties(properties);
  134. while (itProperties.hasNext())
  135. {
  136. itProperties.next();
  137. // Do not append these file specific properties since they require a slightly
  138. // different key for uploading a file (e.g. instead of "file_format" only "format")
  139. if (itProperties.key() == FILE_TAGS || itProperties.key() == FILE_FORMAT ||
  140. itProperties.key() == FILE_CONTENT)
  141. continue;
  142. query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
  143. }
  144. query.append(QString("&%1=%2").arg("format", this->fileFormat()));
  145. query.append(QString("&%1=%2").arg("content", this->fileContent()));
  146. query.append(QString("&%1=%2").arg("tags", this->fileTags()));
  147. query.append(QString("&%1=%2").arg("inbody", "true"));
  148. this->session()->upload(filename, query);
  149. }