ctkXnatConnectionFactory.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*=============================================================================
  2. Plugin: org.commontk.xnat
  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 "ctkXnatConnectionFactory.h"
  16. #include "ctkXnatConnection.h"
  17. #include "ctkXnatException.h"
  18. #include "ctkXnatObject.h"
  19. #include "ctkXnatServer.h"
  20. // ctkXnatConnectionFactory class
  21. ctkXnatConnectionFactory::ctkXnatConnectionFactory()
  22. {
  23. }
  24. ctkXnatConnectionFactory::~ctkXnatConnectionFactory()
  25. {
  26. }
  27. ctkXnatConnectionFactory& ctkXnatConnectionFactory::instance()
  28. {
  29. static ctkXnatConnectionFactory connectionFactory;
  30. return connectionFactory;
  31. }
  32. ctkXnatConnection* ctkXnatConnectionFactory::makeConnection(const QString& url, const QString& user, const QString& password)
  33. {
  34. // create XNAT connection
  35. ctkXnatConnection* connection = new ctkXnatConnection;
  36. // test XNAT connection
  37. try
  38. {
  39. testConnection(connection);
  40. }
  41. catch (ctkXnatException& e)
  42. {
  43. delete connection;
  44. throw;
  45. }
  46. connection->setUrl(url);
  47. connection->setUserName(user);
  48. connection->setPassword(password);
  49. // return XNAT connection
  50. return connection;
  51. }
  52. void ctkXnatConnectionFactory::testConnection(ctkXnatConnection* connection)
  53. {
  54. // test connection by retrieving project names from XNAT
  55. ctkXnatServer* server = NULL;
  56. try
  57. {
  58. // create XNAT root
  59. server = connection->server();
  60. // TODO E.g. get version
  61. }
  62. catch (ctkXnatException& e)
  63. {
  64. if (server != NULL)
  65. {
  66. delete server;
  67. }
  68. throw;
  69. }
  70. }