ctkCmdLineModuleDirectoryWatcher_p.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) University College London
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =============================================================================*/
  14. #ifndef __ctkCmdLineModuleDirectoryWatcherPrivate_h
  15. #define __ctkCmdLineModuleDirectoryWatcherPrivate_h
  16. #include <QHash>
  17. #include <QString>
  18. #include <QStringList>
  19. #include <QFileInfoList>
  20. #include "ctkCmdLineModuleReference.h"
  21. #include "ctkCmdLineModuleDirectoryWatcher.h"
  22. class QFileSystemWatcher;
  23. /**
  24. * \class ctkCmdLineModuleDirectoryWatcherPrivate
  25. * \brief Private implementation class implementing directory/file watching to
  26. * load new modules into a ctkCmdLineModuleManager.
  27. *
  28. * \ingroup CommandLineModulesCore_API
  29. * \author m.clarkson@ucl.ac.uk
  30. */
  31. class ctkCmdLineModuleDirectoryWatcherPrivate : public QObject
  32. {
  33. Q_OBJECT
  34. public:
  35. ctkCmdLineModuleDirectoryWatcherPrivate(ctkCmdLineModuleManager* ModuleManager);
  36. virtual ~ctkCmdLineModuleDirectoryWatcherPrivate();
  37. /**
  38. * \see ctkCmdLineModuleDirectoryWatcher::setDebug
  39. */
  40. void setDebug(bool debug);
  41. /**
  42. * \see ctkCmdLineModuleDirectoryWatcher::setDirectories
  43. */
  44. void setDirectories(const QStringList& directories);
  45. /**
  46. * \see ctkCmdLineModuleDirectoryWatcher::directories
  47. */
  48. QStringList directories() const;
  49. /**
  50. * \see ctkCmdLineModuleDirectoryWatcher::setAdditionalModules
  51. */
  52. void setAdditionalModules(const QStringList& files);
  53. /**
  54. * \see ctkCmdLineModuleDirectoryWatcher::additionalModules
  55. */
  56. QStringList additionalModules() const;
  57. /**
  58. * \see ctkCmdLineModuleDirectoryWatcher::commandLineModules
  59. */
  60. QStringList commandLineModules() const;
  61. public Q_SLOTS:
  62. /**
  63. * \brief We connect QFileSystemWatcher to here.
  64. */
  65. void onFileChanged(const QString& path);
  66. /**
  67. * \brief We connect QFileSystemWatcher to here.
  68. */
  69. void onDirectoryChanged(const QString &path);
  70. private:
  71. /**
  72. * \brief Used to update the QFileSystemWatcher with the right list of directories and files to watch.
  73. * This is the main method, called by others to update what is being watched in terms of both files and directories.
  74. *
  75. * \param directories list of absolute directory paths
  76. * \param files list of absolute file paths
  77. */
  78. void updateWatchedPaths(const QStringList& directories, const QStringList& files);
  79. /**
  80. * \brief Takes a list of directories, and only returns ones that are valid,
  81. * meaning that the directory name is non-null, non-empty, and the directory exists.
  82. *
  83. * \param directories a list of directories, relative or absolute.
  84. * \return a list of valid directories, denoted by their absolute path.
  85. */
  86. QStringList filterInvalidDirectories(const QStringList& directories) const;
  87. /**
  88. * \brief Takes a list of filenames, and only returns ones that are not contained within
  89. * the current list of directories that are being watched.
  90. *
  91. * \param filenames a list of filenames, relative or absolute.
  92. * \return a list of valid filenames, denoted by absolute path, that are not contained within
  93. * the current list of directories being scanned.
  94. */
  95. QStringList filterFilesNotInCurrentDirectories(const QStringList& filenames) const;
  96. /**
  97. * \brief Returns a list of executable files (not necessarily valid command line clients) in a directory.
  98. *
  99. * \param directory the absolute or relative path of a directory.
  100. * \return a list of absolute path names to executable files
  101. */
  102. QStringList getExecutablesInDirectory(const QString& directory) const;
  103. /**
  104. * \brief Uses the MapFileNameToReference to work out a list of valid command line modules in a given directory.
  105. *
  106. * \param directory the absolute or relative path of a directory.
  107. * \return a list of executables, denoted by their absolute path.
  108. */
  109. QStringList extractCurrentlyWatchedFilenamesInDirectory(const QString& directory) const;
  110. /**
  111. * \brief Main method to update the current list of watched executables in a given set of directories.
  112. * \param directories a list of directories, denoted by their absolute path.
  113. */
  114. void setModules(const QStringList &directories);
  115. /**
  116. * \brief Called from the onDirectoryChanged slot to update the current list of modules
  117. * by calling back to setModules.
  118. * \param directory denoted by its absolute path.
  119. */
  120. void updateModules(const QString &directory);
  121. /**
  122. * \brief Uses the ctkCmdLineModuleManager to try and add the executables to the list
  123. * of executables, and if successful it is added to this->MapFileNameToReference.
  124. *
  125. * \param executables A list of paths to executable files, denoted by an absolute path.
  126. */
  127. QList<ctkCmdLineModuleReference> loadModules(const QStringList& executables);
  128. /**
  129. * \brief Removes the executables from both the ctkCmdLineModuleManager and this->MapFileNameToReference.
  130. *
  131. * \param executables path to an executable file, denoted by its absolute path.
  132. */
  133. void unloadModules(const QStringList& executables);
  134. QHash<QString, ctkCmdLineModuleReference> MapFileNameToReference;
  135. ctkCmdLineModuleManager* ModuleManager;
  136. QFileSystemWatcher* FileSystemWatcher;
  137. QStringList AdditionalModules;
  138. bool Debug;
  139. };
  140. #endif