ctkCmdLineModuleDirectoryWatcher_p.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 scanning to load new modules into a ctkCmdLineModuleManager.
  26. * \ingroup CommandLineModulesCore
  27. * \author m.clarkson@ucl.ac.uk
  28. */
  29. class ctkCmdLineModuleDirectoryWatcherPrivate : public QObject
  30. {
  31. Q_OBJECT
  32. public:
  33. ctkCmdLineModuleDirectoryWatcherPrivate(ctkCmdLineModuleManager* ModuleManager);
  34. virtual ~ctkCmdLineModuleDirectoryWatcherPrivate();
  35. /**
  36. * \see ctkCmdLineModuleDirectoryWatcher::setDebug
  37. */
  38. void setDebug(bool debug);
  39. /**
  40. * \see ctkCmdLineModuleDirectoryWatcher::setDirectories
  41. */
  42. void setDirectories(const QStringList& directories);
  43. /**
  44. * \see ctkCmdLineModuleDirectoryWatcher::directories
  45. */
  46. QStringList directories() const;
  47. /**
  48. * \see ctkCmdLineModuleDirectoryWatcher::files
  49. */
  50. QStringList files() const;
  51. public Q_SLOTS:
  52. /**
  53. * \brief We connect QFileSystemWatcher to here.
  54. */
  55. void onFileChanged(const QString& path);
  56. /**
  57. * \brief We connect QFileSystemWatcher to here.
  58. */
  59. void onDirectoryChanged(const QString &path);
  60. private:
  61. /**
  62. * \brief Used to update the QFileSystemWatcher with the right list of directories and files to watch.
  63. * \param directories list of absolute directory paths
  64. * \param files list of absolute file paths
  65. */
  66. void updateWatchedPaths(const QStringList& directories, const QStringList& files);
  67. /**
  68. * \brief Takes a list of directories, and only returns ones that are valid,
  69. * meaning that the directory name is non-null, non-empty, and the directory exists.
  70. * \param directories a list of directories, relative or absolute.
  71. * \return a list of directories, denoted by their absolute path.
  72. */
  73. QStringList filterInvalidDirectories(const QStringList& directories) const;
  74. /**
  75. * \brief Uses the MapFileNameToReference to work out a list of valid command line modules in a given directory.
  76. * \param directory the absolute or relative path of a directory.
  77. * \return a list of executables, denoted by their absolute path.
  78. */
  79. QStringList extractCurrentlyWatchedFilenamesInDirectory(const QString& directory) const;
  80. /**
  81. * \brief Returns a list of executable files (not necessarily valid command line clients) in a directory.
  82. * \param directory A directory
  83. * \return QStringList a list of absolute path names to executable files
  84. */
  85. QStringList getExecutablesInDirectory(const QString& directory) const;
  86. /**
  87. * \brief Main method to update the current list of watched directories and files.
  88. * \param directories a list of directories, denoted by their absolute path.
  89. */
  90. void setModuleReferences(const QStringList &directories);
  91. /**
  92. * \brief Called from the onDirectoryChanged slot to update the current list by calling back to setModuleReferences.
  93. * \param directory denoted by its absolute path.
  94. */
  95. void updateModuleReferences(const QString &directory);
  96. /**
  97. * \brief Uses the ctkCmdLineModuleManager to try and add the executable to the list
  98. * of executables, and if successful it is added to this->MapFileNameToReference.
  99. * \param pathToExecutable path to an executable file, denoted by its absolute path.
  100. */
  101. ctkCmdLineModuleReference loadModule(const QString& pathToExecutable);
  102. /**
  103. * \brief Removes the executable from both the ctkCmdLineModuleManager and this->MapFileNameToReference.
  104. * \param pathToExecutable path to an executable file, denoted by its absolute path.
  105. */
  106. void unloadModule(const QString& pathToExecutable);
  107. QHash<QString, ctkCmdLineModuleReference> MapFileNameToReference;
  108. ctkCmdLineModuleManager* ModuleManager;
  109. QFileSystemWatcher* FileSystemWatcher;
  110. bool Debug;
  111. };
  112. #endif