Browse Source

ctkXnatSession now requires a ctkXnatLoginProfile in its constructor.

Sascha Zelzer 11 years ago
parent
commit
579b0b7fb4

+ 1 - 4
Applications/ctkXNATBrowser/ctkXNATBrowserMainWindow.cpp

@@ -25,7 +25,6 @@
 #include "ctkXnatLoginDialog.h"
 #include "ctkXnatProjectListModel.h"
 #include "ctkXnatSession.h"
-#include "ctkXnatSessionFactory.h"
 #include "ctkXnatDataModel.h"
 #include "ctkXnatProject.h"
 
@@ -34,7 +33,6 @@
 ctkXNATBrowserMainWindow::ctkXNATBrowserMainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::ctkXNATBrowserMainWindow),
-  m_SessionFactory(new ctkXnatSessionFactory()),
   m_Session(0),
   m_ProjectsModel(new ctkXnatProjectListModel()),
   m_SubjectsModel(new ctkXnatProjectListModel())
@@ -54,7 +52,6 @@ ctkXNATBrowserMainWindow::~ctkXNATBrowserMainWindow()
   {
     delete m_Session;
   }
-  delete m_SessionFactory;
   delete ui;
 
   delete m_SubjectsModel;
@@ -82,7 +79,7 @@ void ctkXNATBrowserMainWindow::loginButtonPushed()
       if (m_Session)
       {
         ui->loginButton->setText("Logout");
-        ui->loginLabel->setText(QString("Connected: %1").arg(m_Session->url()));
+        ui->loginLabel->setText(QString("Connected: %1").arg(m_Session->url().toString()));
 
         ctkXnatDataModel* dataModel = m_Session->dataModel();
         //xnatConnection->fetch(server);

+ 1 - 4
Applications/ctkXnatTreeBrowser/ctkXnatTreeBrowserMainWindow.cpp

@@ -25,7 +25,6 @@
 #include "ctkXnatLoginDialog.h"
 #include "ctkXnatTreeModel.h"
 #include "ctkXnatSession.h"
-#include "ctkXnatSessionFactory.h"
 #include "ctkXnatDataModel.h"
 #include "ctkXnatProject.h"
 #include "ctkXnatFile.h"
@@ -33,7 +32,6 @@
 ctkXnatTreeBrowserMainWindow::ctkXnatTreeBrowserMainWindow(QWidget *parent) :
   QMainWindow(parent),
   ui(new Ui::ctkXnatTreeBrowserMainWindow),
-  m_SessionFactory(new ctkXnatSessionFactory()),
   m_Session(0),
   m_TreeModel(new ctkXnatTreeModel())
 {
@@ -53,7 +51,6 @@ ctkXnatTreeBrowserMainWindow::~ctkXnatTreeBrowserMainWindow()
   {
     delete m_Session;
   }
-  delete m_SessionFactory;
   delete ui;
 
   delete m_TreeModel;
@@ -81,7 +78,7 @@ void ctkXnatTreeBrowserMainWindow::loginButtonPushed()
       if (m_Session)
       {
         ui->loginButton->setText("Logout");
-        ui->loginLabel->setText(QString("Connected: %1").arg(m_Session->url()));
+        ui->loginLabel->setText(QString("Connected: %1").arg(m_Session->url().toString()));
 
         ctkXnatDataModel* dataModel = m_Session->dataModel();
         m_TreeModel->addDataModel(dataModel);

+ 0 - 1
Libs/XNAT/Core/CMakeLists.txt

@@ -23,7 +23,6 @@ set(KIT_SRCS
   ctkXnatScanFolder.cpp
   ctkXnatScanResource.cpp
   ctkXnatSession.cpp
-  ctkXnatSessionFactory.cpp
   ctkXnatSettings.cpp
   ctkXnatSubject.cpp
 )

+ 7 - 12
Libs/XNAT/Core/Testing/ctkXnatSessionTest.cpp

@@ -32,20 +32,17 @@
 #include <QUuid>
 
 #include <ctkXnatDataModel.h>
+#include <ctkXnatLoginProfile.h>
 #include <ctkXnatSession.h>
-#include <ctkXnatSessionFactory.h>
 #include <ctkXnatProject.h>
 #include <ctkXnatSubject.h>
 
 class ctkXnatSessionTestCasePrivate
 {
 public:
-  ctkXnatSessionFactory* SessionFactory;
   ctkXnatSession* Session;
 
-  QString ServerUri;
-  QString UserName;
-  QString Password;
+  ctkXnatLoginProfile LoginProfile;
 
   QString Project;
   QString Subject;
@@ -68,13 +65,12 @@ void ctkXnatSessionTestCase::initTestCase()
 {
   Q_D(ctkXnatSessionTestCase);
 
-  d->ServerUri = "https://central.xnat.org";
-  d->UserName = "ctk";
-  d->Password = "ctk";
+  d->LoginProfile.setName("ctk");
+  d->LoginProfile.setServerUrl(QString("https://central.xnat.org"));
+  d->LoginProfile.setUserName("ctk");
+  d->LoginProfile.setPassword("ctk");
 
-  d->SessionFactory = new ctkXnatSessionFactory();
-  d->Session = d->SessionFactory->makeConnection(d->ServerUri, d->UserName, d->Password);
-  d->Session->setProfileName("ctk");
+  d->Session = new ctkXnatSession(d->LoginProfile);
 }
 
 void ctkXnatSessionTestCase::cleanupTestCase()
@@ -82,7 +78,6 @@ void ctkXnatSessionTestCase::cleanupTestCase()
   Q_D(ctkXnatSessionTestCase);
 
   delete d->Session;
-  delete d->SessionFactory;
 }
 
 void ctkXnatSessionTestCase::testProjectList()

+ 45 - 62
Libs/XNAT/Core/ctkXnatSession.cpp

@@ -25,6 +25,7 @@
 #include "ctkXnatException.h"
 #include "ctkXnatExperiment.h"
 #include "ctkXnatFile.h"
+#include "ctkXnatLoginProfile.h"
 #include "ctkXnatObject.h"
 #include "ctkXnatProject.h"
 #include "ctkXnatReconstruction.h"
@@ -45,19 +46,33 @@
 class ctkXnatSessionPrivate
 {
 public:
-  QString profileName;
-  QString url;
-  QString userName;
-  QString password;
+  const ctkXnatLoginProfile loginProfile;
 
   ctkXnatAPI* xnat;
-  ctkXnatAPI::RawHeaders rawHeaders;
-
   ctkXnatDataModel* dataModel;
 
+  ctkXnatSessionPrivate(const ctkXnatLoginProfile& loginProfile, ctkXnatSession* q);
+  ~ctkXnatSessionPrivate();
+
   void throwXnatException(const QString& msg);
 };
 
+ctkXnatSessionPrivate::ctkXnatSessionPrivate(const ctkXnatLoginProfile& loginProfile, ctkXnatSession* q)
+  : loginProfile(loginProfile)
+  , xnat(new ctkXnatAPI())
+  , dataModel(new ctkXnatDataModel(q))
+{
+  // TODO This is a workaround for connecting to sites with self-signed
+  // certificate. Should be replaced with something more clever.
+  xnat->setSuppressSslErrors(true);
+}
+
+ctkXnatSessionPrivate::~ctkXnatSessionPrivate()
+{
+  delete dataModel;
+  delete xnat;
+}
+
 void ctkXnatSessionPrivate::throwXnatException(const QString& msg)
 {
   QString errorMsg = msg.trimmed();
@@ -80,27 +95,28 @@ void ctkXnatSessionPrivate::throwXnatException(const QString& msg)
 
 // ctkXnatSession class
 
-ctkXnatSession::ctkXnatSession()
-: d_ptr(new ctkXnatSessionPrivate())
+ctkXnatSession::ctkXnatSession(const ctkXnatLoginProfile& loginProfile)
+: d_ptr(new ctkXnatSessionPrivate(loginProfile, this))
 {
   Q_D(ctkXnatSession);
-  d->xnat = new ctkXnatAPI();
 
-  // TODO This is a workaround for connecting to sites with self-signed
-  // certificate. Should be replaced with something more clever.
-  d->xnat->setSuppressSslErrors(true);
-  d->rawHeaders["User-Agent"] = "Qt";
-  d->xnat->setDefaultRawHeaders(d->rawHeaders);
+  ctkXnatAPI::RawHeaders rawHeaders;
+  rawHeaders["User-Agent"] = "Qt";
+  rawHeaders["Authorization"] = "Basic " +
+      QByteArray(QString("%1:%2").arg(d->loginProfile.userName())
+                 .arg(d->loginProfile.password()).toAscii()).toBase64();
+  d->xnat->setDefaultRawHeaders(rawHeaders);
+
+  QString url = d->loginProfile.serverUrl().toString();
+  d->xnat->setServerUrl(url);
+
+  d->dataModel->setProperty("ID", url);
 
-  d->dataModel = new ctkXnatDataModel(this);
   createConnections();
 }
 
 ctkXnatSession::~ctkXnatSession()
 {
-  Q_D(ctkXnatSession);
-  delete d->dataModel;
-  delete d->xnat;
 }
 
 void ctkXnatSession::createConnections()
@@ -109,71 +125,38 @@ void ctkXnatSession::createConnections()
 //  connect(d->xnat, SIGNAL(resultReceived(QUuid,QList<QVariantMap>)),
 //           this, SLOT(processResult(QUuid,QList<QVariantMap>)));
 //  connect(d->xnat, SIGNAL(progress(QUuid,double)),
-//           this, SLOT(progress(QUuid,double)));
+  //           this, SLOT(progress(QUuid,double)));
 }
 
-void ctkXnatSession::progress(QUuid /*queryId*/, double /*progress*/)
-{
-//  qDebug() << "ctkXnatSession::progress(QUuid queryId, double progress)";
-//  qDebug() << "query id:" << queryId;
-//  qDebug() << "progress:" << (progress * 100.0) << "%";
-}
-
-QString ctkXnatSession::profileName() const
+ctkXnatLoginProfile ctkXnatSession::loginProfile() const
 {
   Q_D(const ctkXnatSession);
-  return d->profileName;
+  return d->loginProfile;
 }
 
-void ctkXnatSession::setProfileName(const QString& profileName)
+void ctkXnatSession::progress(QUuid /*queryId*/, double /*progress*/)
 {
-  Q_D(ctkXnatSession);
-  d->profileName = profileName;
-  d->dataModel->setProperty("name", profileName);
+//  qDebug() << "ctkXnatSession::progress(QUuid queryId, double progress)";
+//  qDebug() << "query id:" << queryId;
+//  qDebug() << "progress:" << (progress * 100.0) << "%";
 }
 
-QString ctkXnatSession::url() const
+QUrl ctkXnatSession::url() const
 {
   Q_D(const ctkXnatSession);
-  return d->url;
-}
-
-void ctkXnatSession::setUrl(const QString& url)
-{
-  Q_D(ctkXnatSession);
-  d->url = url;
-  d->xnat->setServerUrl(d->url);
-  d->dataModel->setProperty("ID", url);
+  return d->loginProfile.serverUrl();
 }
 
 QString ctkXnatSession::userName() const
 {
   Q_D(const ctkXnatSession);
-  return d->userName;
-}
-
-void ctkXnatSession::setUserName(const QString& userName)
-{
-  Q_D(ctkXnatSession);
-  d->userName = userName;
-  d->rawHeaders["Authorization"] = "Basic " +
-      QByteArray(QString("%1:%2").arg(d->userName).arg(d->password).toAscii()).toBase64();
-  d->xnat->setDefaultRawHeaders(d->rawHeaders);
+  return d->loginProfile.userName();
 }
 
 QString ctkXnatSession::password() const
 {
   Q_D(const ctkXnatSession);
-  return d->password;
-}
-
-void ctkXnatSession::setPassword(const QString& password)
-{
-  Q_D(ctkXnatSession);
-  d->password = password;
-  d->rawHeaders["Authorization"] = "Basic " +
-      QByteArray(QString("%1:%2").arg(d->userName).arg(d->password).toAscii()).toBase64();
-  d->xnat->setDefaultRawHeaders(d->rawHeaders);
+  return d->loginProfile.password();
 }
 
 ctkXnatDataModel* ctkXnatSession::dataModel() const

+ 31 - 12
Libs/XNAT/Core/ctkXnatSession.h

@@ -36,6 +36,7 @@ class ctkXnatSessionPrivate;
 class ctkXnatDataModel;
 class ctkXnatExperiment;
 class ctkXnatFile;
+class ctkXnatLoginProfile;
 class ctkXnatObject;
 class ctkXnatProject;
 class ctkXnatReconstruction;
@@ -54,7 +55,7 @@ public:
 
   //********
   // Add ctkXnatLoginProfile argument
-  ctkXnatSession();
+  ctkXnatSession(const ctkXnatLoginProfile& loginProfile);
   ~ctkXnatSession();
 
   // **********
@@ -62,20 +63,38 @@ public:
   // For Qt singal/slots; should go into the constructor or private impl.
   void createConnections();
 
-  // **********
-  // Remove / replase
-
-  QString profileName() const;
-  void setProfileName(const QString& profileName);
-
-  QString url() const;
-  void setUrl(const QString& url);
-
+  /**
+   * @brief Get the current login profile for this session object.
+   * @return A copy of the currently used login profile.
+   */
+  ctkXnatLoginProfile loginProfile() const;
+
+  /**
+   * @brief Get XNAT server url.
+   *
+   * The url is the one specified by the login profile.
+   *
+   * @return The XNAT server url.
+   */
+  QUrl url() const;
+
+  /**
+   * @brief Get the user name for this XNAT session.
+   *
+   * The user name is the one specified by the login profile.
+   *
+   * @return The XNAT session user name.
+   */
   QString userName() const;
-  void setUserName(const QString& userName);
 
+  /**
+   * @brief Get the password for this XNAT session.
+   *
+   * The password is the one specified by the login profile.
+   *
+   * @return The XNAT session password.
+   */
   QString password() const;
-  void setPassword(const QString& password);
 
   ctkXnatDataModel* dataModel() const;
 

+ 0 - 63
Libs/XNAT/Core/ctkXnatSessionFactory.cpp

@@ -1,63 +0,0 @@
-/*=============================================================================
-
-  Plugin: org.commontk.xnat
-
-  Copyright (c) University College London,
-    Centre for Medical Image Computing
-
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
-
-=============================================================================*/
-
-#include "ctkXnatSessionFactory.h"
-
-#include "ctkXnatSession.h"
-#include "ctkXnatObject.h"
-#include "ctkXnatDataModel.h"
-
-#include <QDebug>
-
-// ctkXnatSessionFactory class
-
-ctkXnatSession* ctkXnatSessionFactory::makeConnection(const QString& url, const QString& user, const QString& password)
-{
-  // create XNAT connection
-  ctkXnatSession* session = new ctkXnatSession;
-
-  // test XNAT connection
-  try
-  {
-    testConnection(session);
-  }
-  catch (...)
-  {
-    delete session;
-    throw;
-  }
-
-  session->setUrl(url);
-  qDebug() << "ctkXnatSessionFactory::makeConnection(const QString& url, const QString& user, const QString& password) url:" << url;
-  session->setUserName(user);
-  session->setPassword(password);
-
-  // return XNAT connection
-  return session;
-}
-
-void ctkXnatSessionFactory::testConnection(ctkXnatSession* session)
-{
-  // test connection by retrieving project names from XNAT
-  session->dataModel();
-
-  // TODO E.g. get version
-}

+ 0 - 42
Libs/XNAT/Core/ctkXnatSessionFactory.h

@@ -1,42 +0,0 @@
-/*=============================================================================
-
-  Plugin: org.commontk.xnat
-
-  Copyright (c) University College London,
-    Centre for Medical Image Computing
-
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
-
-=============================================================================*/
-
-#ifndef CTKXNATSESSIONFACTORY_H
-#define CTKXNATSESSIONFACTORY_H
-
-#include "ctkXNATCoreExport.h"
-
-#include <QString>
-
-class ctkXnatSession;
-
-class CTK_XNAT_CORE_EXPORT ctkXnatSessionFactory
-{
-public:
-
-  ctkXnatSession* makeConnection(const QString& url, const QString& user, const QString& password);
-
-private:
-
-  void testConnection(ctkXnatSession* conn);
-};
-
-#endif

+ 8 - 4
Libs/XNAT/Widgets/ctkXnatLoginDialog.cpp

@@ -28,7 +28,6 @@
 #include <QTimer>
 
 #include <ctkXnatSession.h>
-#include <ctkXnatSessionFactory.h>
 #include <ctkException.h>
 #include "ctkXnatLoginProfile.h"
 #include "ctkXnatSettings.h"
@@ -187,11 +186,15 @@ void ctkXnatLoginDialog::accept()
   QString password = ui->edtPassword->text();
 
   // create XNAT connection
+  QScopedPointer<ctkXnatSession> session;
   try
     {
-    d->Session = d->Factory->makeConnection(url.toAscii().constData(), userName.toAscii().constData(),
-                                        password.toAscii().constData());
-    d->Session->setProfileName(ui->edtProfileName->text());
+    ctkXnatLoginProfile loginProfile;
+    loginProfile.setName(ui->edtProfileName->text());
+    loginProfile.setServerUrl(url);
+    loginProfile.setUserName(userName);
+    loginProfile.setPassword(password);
+    session.reset(new ctkXnatSession(loginProfile));
     }
   catch (const ctkException& e)
     {
@@ -200,6 +203,7 @@ void ctkXnatLoginDialog::accept()
     ui->edtServerUri->setFocus();
     return;
     }
+  d->Session = session.take();
 
   QDialog::accept();
 }