Browse Source

Cleaned up code

Andreas Fetzer 10 years ago
parent
commit
aa22a6870b

+ 1 - 1
Applications/ctkXnatTreeBrowser/ctkXnatTreeBrowserMainWindow.cpp

@@ -147,7 +147,7 @@ void ctkXnatTreeBrowserMainWindow::addResourceClicked()
 {
   const QModelIndex index = ui->treeView->selectionModel()->currentIndex();
   ctkXnatObject* parentObject = m_TreeModel->xnatObject(index);
-  parentObject->addResource("data", "ctk format", "custom content", "tag1, tag2");
+  parentObject->addResourceFolder("data", "ctk format", "custom content", "tag1, tag2");
 }
 
 void ctkXnatTreeBrowserMainWindow::uploadFileClicked()

+ 3 - 22
Libs/XNAT/Core/ctkXnatFile.cpp

@@ -25,9 +25,7 @@
 #include "ctkXnatObjectPrivate.h"
 #include "ctkXnatSession.h"
 
-#include <QCryptographicHash>
 #include <QDebug>
-#include <QFile>
 
 const QString ctkXnatFile::FILE_NAME = "Name";
 const QString ctkXnatFile::FILE_TAGS = "file_tags";
@@ -155,23 +153,12 @@ void ctkXnatFile::saveImpl(bool overwrite)
 {
   Q_D(ctkXnatFile);
 
-  QMap<QString, QString> urlParams;
+  ctkXnatSession::UrlParameters urlParams;
   urlParams["xsi:type"] = this->schemaType();
+  // Flag needed for file upload
   urlParams["inbody"] = "true";
-  QString query = this->resourceUri();
-  QString filename = this->localFilePath();
-
-  QFile file(filename);
-
-  if (!file.exists())
-  {
-    QString msg = "Error uploading file! ";
-    msg.append(QString("File \"%1\" does not exist!").arg(filename));
-    throw ctkXnatException(msg);
-  }
 
   // Creating the update query
-  query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
   const QMap<QString, QString>& properties = this->properties();
   QMapIterator<QString, QString> itProperties(properties);
   while (itProperties.hasNext())
@@ -185,8 +172,8 @@ void ctkXnatFile::saveImpl(bool overwrite)
       continue;
 
     urlParams[itProperties.key()] = itProperties.value();
-    query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
   }
+
   if (!this->fileFormat().isNull())
     urlParams["format"] = this->fileFormat();
   if (!this->fileContent().isNull())
@@ -197,11 +184,5 @@ void ctkXnatFile::saveImpl(bool overwrite)
   if (this->exists() && overwrite)
     urlParams["overwrite"] = "true";
 
-  // Flag needed for file upload
-  query.append(QString("&%1=%2").arg("inbody", "true"));
-
   this->session()->upload(this, urlParams);
-
-//  d->setIsModified();
-//  this->parent()->fetch();
 }

+ 7 - 7
Libs/XNAT/Core/ctkXnatObject.cpp

@@ -372,10 +372,11 @@ bool ctkXnatObject::exists() const
 void ctkXnatObject::saveImpl(bool /*overwrite*/)
 {
   Q_D(ctkXnatObject);
-  QString query = this->resourceUri();
+  ctkXnatSession::UrlParameters urlParams;
+  urlParams["xsi:type"] = this->schemaType();
 
-  // If there is already a valid last-modification-time, otherwise the
-  // object is not yet on the server!
+  // Just do this if there is already a valid last-modification-time,
+  // otherwise the object is not yet on the server!
   QDateTime remoteModTime;
   if (d->lastModifiedTime.isValid())
   {
@@ -391,8 +392,6 @@ void ctkXnatObject::saveImpl(bool /*overwrite*/)
     }
   }
 
-  // Creating the update query
-  query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
   const QMap<QString, QString>& properties = this->properties();
   QMapIterator<QString, QString> itProperties(properties);
   while (itProperties.hasNext())
@@ -400,11 +399,12 @@ void ctkXnatObject::saveImpl(bool /*overwrite*/)
     itProperties.next();
     if (itProperties.key() == "ID")
       continue;
-    query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
+
+    urlParams[itProperties.key()] = itProperties.value();
   }
 
   // Execute the update
-  QUuid queryID = this->session()->httpPut(query);
+  QUuid queryID = this->session()->httpPut(this->resourceUri(), urlParams);
   const QList<QVariantMap> results = this->session()->httpSync(queryID);
 
   // If this xnat object did not exist before on the server set the ID returned by Xnat

+ 7 - 1
Libs/XNAT/Core/ctkXnatObject.h

@@ -132,7 +132,13 @@ public:
 
   void download(const QString&);
 
-  /// Creates the object on the XNAT server and sets the new ID.
+  /// Creates a new resource folder with the given foldername, format, content and tags
+  /// for this ctkXnatObject on the server.
+  /// @param foldername the name of the resource folder on the server (mandatory)
+  /// @param format the text of the format field of a resource (optional)
+  /// @param content the text of the content field of a resource (optional)
+  /// @param tags the content of the tags field of a resource (optional)
+  /// @returns ctkXnatResource the created resource
   virtual ctkXnatResource* addResourceFolder(QString foldername,
                            QString format = "", QString content = "", QString tags = "");
 

+ 5 - 4
Libs/XNAT/Core/ctkXnatResource.cpp

@@ -169,9 +169,9 @@ void ctkXnatResource::downloadImpl(const QString& filename)
 //----------------------------------------------------------------------------
 void ctkXnatResource::saveImpl(bool /*overwrite*/)
 {
-  QString query = this->resourceUri();
+  ctkXnatSession::UrlParameters urlParams;
+  urlParams["xsi:type"] = this->schemaType();
 
-  query.append(QString("?%1=%2").arg("xsi:type", this->schemaType()));
   const QMap<QString, QString>& properties = this->properties();
   QMapIterator<QString, QString> itProperties(properties);
   while (itProperties.hasNext())
@@ -179,8 +179,9 @@ void ctkXnatResource::saveImpl(bool /*overwrite*/)
     itProperties.next();
     if (itProperties.key() == ID || itProperties.key() == "xsiType")
       continue;
-    query.append(QString("&%1=%2").arg(itProperties.key(), itProperties.value()));
+
+    urlParams[itProperties.key()] = itProperties.value();
   }
-  QUuid queryId = this->session()->httpPut(query);
+  QUuid queryId = this->session()->httpPut(this->resourceUri(), urlParams);
   session()->httpResults(queryId, ctkXnatDefaultSchemaTypes::XSI_RESOURCE);
 }

+ 0 - 7
Libs/XNAT/Core/ctkXnatResourceCatalogXmlParser.cpp

@@ -33,7 +33,6 @@ public:
   {
   }
 
-  QList<QVariantMap> result;
   QXmlStreamReader xmlReader;
 };
 
@@ -80,9 +79,3 @@ void ctkXnatResourceCatalogXmlParser::parseXml(QList<QVariantMap>& result)
     qWarning()<<"Error parsing XNAT resource catalog xml!";
   }
 }
-
-const QList<QVariantMap>& ctkXnatResourceCatalogXmlParser::md5Hashes()
-{
-  Q_D(ctkXnatResourceCatalogXmlParser);
-  return d->result;
-}

+ 0 - 6
Libs/XNAT/Core/ctkXnatResourceCatalogXmlParser.h

@@ -55,12 +55,6 @@ public:
   void setData(const QByteArray& xmlInput);
 
   /**
-   * @brief Returns the md5 hashes of the resource files
-   * @return A list of QVariantMaps, which contain the md5 hashes
-   */
-  const QList<QVariantMap>& md5Hashes();
-
-  /**
    * @brief Parses the xml input and extracts the md5 hashes of the resource catalog
    * @param result the QList in which the md5 hashes will be stored
    */

+ 19 - 9
Libs/XNAT/Core/ctkXnatSession.cpp

@@ -333,7 +333,7 @@ ctkXnatSession::ctkXnatSession(const ctkXnatLoginProfile& loginProfile)
   QString url = d->loginProfile.serverUrl().toString();
   d->xnat->setServerUrl(url);
 
-  QObject::connect(d->xnat.data(), SIGNAL(uploadFinished()), this, SIGNAL(uploadFinished()));
+//  QObject::connect(d->xnat.data(), SIGNAL(uploadFinished()), this, SIGNAL(uploadFinished()));
   QObject::connect(d->xnat.data(), SIGNAL(progress(QUuid,double)),
           this, SIGNAL(progress(QUuid,double)));
 //  QObject::connect(d->xnat.data(), SIGNAL(progress(QUuid,double)),
@@ -531,7 +531,8 @@ QList<ctkXnatObject*> ctkXnatSession::httpResults(const QUuid& uuid, const QStri
   return d->results(restResult.data(), schemaType);
 }
 
-QUuid ctkXnatSession::httpPut(const QString& resource, const ctkXnatSession::UrlParameters& parameters, const ctkXnatSession::HttpRawHeaders& rawHeaders)
+QUuid ctkXnatSession::httpPut(const QString& resource, const ctkXnatSession::UrlParameters& /*parameters*/,
+                              const ctkXnatSession::HttpRawHeaders& /*rawHeaders*/)
 {
   Q_D(ctkXnatSession);
   d->checkSession();
@@ -616,19 +617,28 @@ void ctkXnatSession::download(const QString& fileName,
 }
 
 //----------------------------------------------------------------------------
-void ctkXnatSession::upload(ctkXnatFile *file,
+void ctkXnatSession::upload(ctkXnatFile *xnatFile,
                             const UrlParameters &parameters,
                             const HttpRawHeaders &/*rawHeaders*/)
 {
   Q_D(ctkXnatSession);
-  QUuid queryId = d->xnat->upload(file->localFilePath(), file->resourceUri(), parameters);
+
+  QFile file(xnatFile->localFilePath());
+
+  if (!file.exists())
+  {
+    QString msg = "Error uploading file! ";
+    msg.append(QString("File \"%1\" does not exist!").arg(xnatFile->localFilePath()));
+    throw ctkXnatException(msg);
+  }
+
+  QUuid queryId = d->xnat->upload(xnatFile->localFilePath(), xnatFile->resourceUri(), parameters);
   d->xnat->sync(queryId);
 
-  // TODO this into session!!!
   // Validating the file upload by requesting the catalog XML
   // of the parent resource. Unfortunately for XNAT versions <= 1.6.4
   // this is the only way to get the file's MD5 hash form the server.
-  QString md5Query = file->parent()->resourceUri();
+  QString md5Query = xnatFile->parent()->resourceUri();
   QUuid md5ID = this->httpGet(md5Query);
   QList<QVariantMap> result = this->httpSync(md5ID);
 
@@ -639,7 +649,7 @@ void ctkXnatSession::upload(ctkXnatFile *file,
   QList<QVariantMap>::const_iterator it = result.constEnd()-1;
   while (it != result.constBegin()-1)
   {
-    QVariantMap::const_iterator it2 = (*it).find(file->name());
+    QVariantMap::const_iterator it2 = (*it).find(xnatFile->name());
     if (it2 != (*it).constEnd())
     {
       md5ChecksumRemote = it2.value().toString();
@@ -648,7 +658,7 @@ void ctkXnatSession::upload(ctkXnatFile *file,
     --it;
   }
 
-  QFile localFile(file->localFilePath());
+  QFile localFile(xnatFile->localFilePath());
   if (localFile.open(QFile::ReadOnly) && md5ChecksumRemote != "0")
   {
     QCryptographicHash hash(QCryptographicHash::Md5);
@@ -665,7 +675,7 @@ void ctkXnatSession::upload(ctkXnatFile *file,
     if (md5ChecksumLocal != md5ChecksumRemote)
     {
       // Remove corrupted file from server
-      file->erase();
+      xnatFile->erase();
       throw ctkXnatException("Upload failed! An error occurred during file upload.");
     }
   }

+ 2 - 2
Libs/XNAT/Core/ctkXnatSession.h

@@ -235,13 +235,13 @@ public:
     const UrlParameters& parameters = UrlParameters(),
     const HttpRawHeaders& rawHeaders = HttpRawHeaders());
 
-  /// Uploads a file to the web service.
+  /// Uploads a file to the server.
   /// \a fileName is the name of the file.
   /// The \a resource and \parameters are used to compose the URL.
   /// \a rawHeaders can be used to set the raw headers of the request to send.
   /// These headers will be set additionally to those defined by the
   /// \a defaultRawHeaders property.
-  void upload(ctkXnatFile *,
+  void upload(ctkXnatFile *xnatFile,
     const UrlParameters& parameters = UrlParameters(),
     const HttpRawHeaders& rawHeaders = HttpRawHeaders());