ctkExampleHostedAppMain.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  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 <ctkPluginFrameworkFactory.h>
  16. #include <ctkPluginFramework.h>
  17. #include <ctkPluginException.h>
  18. // replace "//$" with nothing as soon as ctkDicomAppServer/ctkDicomHostService exist
  19. //$#include <ctkDicomAppServer.h>
  20. //$#include <ctkDicomHostService.h>
  21. #include <QApplication>
  22. #include <QString>
  23. #include <QStringList>
  24. #include <QDirIterator>
  25. #include <QWidget>
  26. #include <QFileInfo>
  27. void print_usage()
  28. {
  29. qCritical() << "Usage:";
  30. qCritical() << " " << QFileInfo(qApp->arguments().at(0)).fileName() << " --hostURL url1 --applicationURL url2 <plugin-name>";
  31. }
  32. int main(int argv, char** argc)
  33. {
  34. QApplication app(argv, argc);
  35. qApp->setOrganizationName("CTK");
  36. qApp->setOrganizationDomain("commontk.org");
  37. qApp->setApplicationName("ctkExampleHost");
  38. // parse the command line
  39. if(qApp->arguments().size() < 5)
  40. {
  41. qCritical() << "Wrong number of command line arguments.";
  42. print_usage();
  43. exit(1);
  44. }
  45. if(qApp->arguments().at(1) != "--hostURL")
  46. {
  47. qCritical() << "First argument must be --hostURL.";
  48. print_usage();
  49. exit(1);
  50. }
  51. QString hostURL = qApp->arguments().at(2);
  52. if(qApp->arguments().at(3) != "--applicationURL")
  53. {
  54. qCritical() << "First argument must be --applicationURL.";
  55. print_usage();
  56. exit(1);
  57. }
  58. QString appURL = qApp->arguments().at(4);
  59. // setup the plugin framework
  60. ctkPluginFrameworkFactory fwFactory;
  61. ctkPluginFramework* framework = fwFactory.getFramework();
  62. try {
  63. framework->init();
  64. }
  65. catch (const ctkPluginException& exc)
  66. {
  67. qCritical() << "Failed to initialize the plug-in framework:" << exc;
  68. exit(2);
  69. }
  70. #ifdef CMAKE_INTDIR
  71. QString pluginPath = qApp->applicationDirPath() + "/../plugins/" CMAKE_INTDIR "/";
  72. #else
  73. QString pluginPath = qApp->applicationDirPath() + "/plugins/";
  74. #endif
  75. qApp->addLibraryPath(pluginPath);
  76. // construct the name of the plugin with the business logic
  77. // (thus the actual logic of the hosted app)
  78. QString pluginName;
  79. if(qApp->arguments().size()>5)
  80. {
  81. pluginName = qApp->arguments().at(5);
  82. }
  83. else
  84. {
  85. pluginName = "org_commontk_dicomhosting_app_";
  86. pluginName += QFileInfo(qApp->arguments().at(0)).fileName();
  87. }
  88. // try to find the plugin and install all plugins available in
  89. // pluginPath (but do not start them)
  90. QStringList libFilter;
  91. libFilter << "*.dll" << "*.so" << "*.dylib";
  92. QDirIterator dirIter(pluginPath, libFilter, QDir::Files);
  93. bool pluginFound = false;
  94. QString pluginFileLocation;
  95. while(dirIter.hasNext())
  96. {
  97. try
  98. {
  99. QString fileLocation = dirIter.next();
  100. if (fileLocation.contains("org_commontk_dicom_wg23"))
  101. {
  102. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(fileLocation));
  103. }
  104. if (fileLocation.contains(pluginName))
  105. {
  106. pluginFound = true;
  107. pluginFileLocation = fileLocation;
  108. }
  109. }
  110. catch (const ctkPluginException& e)
  111. {
  112. qCritical() << e.what();
  113. }
  114. }
  115. // if we did not find the business logic: abort
  116. if(!pluginFound)
  117. {
  118. qCritical() << "Could not find plugin.";
  119. qCritical() << " Plugin name: " << pluginName;
  120. qCritical() << " Plugin path: " << pluginPath;
  121. exit(3);
  122. }
  123. // setup the communication infrastructure: DicomAppServer and DicomHostService
  124. //$ ctkDicomAppServer * appServer = new ctkDicomAppServer(appURL);
  125. //$ ctkDicomHostInterface * hostInterface = new ctkDicomHostService(hostURL);
  126. //$ framework->getPluginContext()->registerService(QStringList("ctkDicomHostInterface"), hostInterface);
  127. // start the plugin with the business logic
  128. try
  129. {
  130. ctkPlugin* plugin = framework->getPluginContext()->installPlugin(QUrl::fromLocalFile(pluginFileLocation));
  131. plugin->start(ctkPlugin::START_TRANSIENT);
  132. }
  133. catch (const ctkPluginException& e)
  134. {
  135. qCritical() << e.what();
  136. }
  137. framework->start();
  138. QWidget placeholder;
  139. placeholder.show();
  140. return app.exec();
  141. }