Browse Source

Merge branch '384-clear-CLI-cache'

Matt Clarkson 11 years ago
parent
commit
18f1840cdf

+ 10 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.cpp

@@ -267,6 +267,16 @@ void ctkCLModuleExplorerMainWindow::on_actionReset_triggered()
   this->tabList->activeTab()->resetValues();
 }
 
+void ctkCLModuleExplorerMainWindow::on_actionClear_Cache_triggered()
+{
+  moduleManager.clearCache();
+}
+
+void ctkCLModuleExplorerMainWindow::on_actionReload_Modules_triggered()
+{
+  moduleManager.reloadModules();
+}
+
 void ctkCLModuleExplorerMainWindow::checkModulePaused()
 {
   if (this->currentFutureWatcher.future().isPaused())

+ 2 - 0
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.h

@@ -68,6 +68,8 @@ protected Q_SLOTS:
   void on_actionQuit_triggered();
 
   void on_actionReset_triggered();
+  void on_actionClear_Cache_triggered();
+  void on_actionReload_Modules_triggered();
 
   void checkModulePaused();
   void currentModuleResumed();

+ 14 - 2
Applications/ctkCommandLineModuleExplorer/ctkCmdLineModuleExplorerMainWindow.ui

@@ -45,7 +45,7 @@
      <x>0</x>
      <y>0</y>
      <width>1126</width>
-     <height>25</height>
+     <height>22</height>
     </rect>
    </property>
    <widget class="QMenu" name="menuFile">
@@ -66,6 +66,8 @@
     <addaction name="actionReset"/>
     <addaction name="separator"/>
     <addaction name="actionOptions"/>
+    <addaction name="actionClear_Cache"/>
+    <addaction name="actionReload_Modules"/>
    </widget>
    <addaction name="menuFile"/>
    <addaction name="menuModule"/>
@@ -201,7 +203,7 @@
              <x>0</x>
              <y>0</y>
              <width>250</width>
-             <height>194</height>
+             <height>208</height>
             </rect>
            </property>
            <layout class="QVBoxLayout" name="verticalLayout_5">
@@ -349,6 +351,16 @@
     <string>Reset to default values</string>
    </property>
   </action>
+  <action name="actionClear_Cache">
+   <property name="text">
+    <string>Clear Cache</string>
+   </property>
+  </action>
+  <action name="actionReload_Modules">
+   <property name="text">
+    <string>Reload Modules</string>
+   </property>
+  </action>
  </widget>
  <customwidgets>
   <customwidget>

+ 8 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleCache.cpp

@@ -181,3 +181,11 @@ void ctkCmdLineModuleCache::removeCacheEntry(const QUrl& moduleLocation)
     xmlFile.remove();
   }
 }
+
+void ctkCmdLineModuleCache::clearCache()
+{
+  foreach(const QUrl &url, d->LocationToXmlDescription.keys())
+  {
+    removeCacheEntry(url);
+  }
+}

+ 10 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleCache_p.h

@@ -32,6 +32,11 @@ class QUrl;
  * \class ctkCmdLineModuleCache
  * \brief Private non-exported class to contain a cache of
  * XML descriptions and time-stamps.
+ *
+ * The intention is that this Cache is an in-memory representation
+ * of a file-system directory containing XML files and a corresponding
+ * timestamp. Hence these should always be in synch.
+ *
  * \ingroup CommandLineModulesCore_API
  */
 class ctkCmdLineModuleCache
@@ -80,6 +85,11 @@ public:
    */
   void removeCacheEntry(const QUrl& moduleLocation);
 
+  /**
+   * @brief Clears all entries from the XML/timestamp cache.
+   */
+  void clearCache();
+
 private:
 
   QScopedPointer<ctkCmdLineModuleCachePrivate> d;

+ 25 - 2
Libs/CommandLineModules/Core/ctkCmdLineModuleManager.cpp

@@ -151,10 +151,13 @@ ctkCmdLineModuleManager::registerModule(const QUrl &location)
 
   bool fromCache = false;
   qint64 newTimeStamp = 0;
+  qint64 cacheTimeStamp = 0;
   if (d->ModuleCache)
   {
     newTimeStamp = backend->timeStamp(location);
-    if (d->ModuleCache->timeStamp(location) < newTimeStamp)
+    cacheTimeStamp = d->ModuleCache->timeStamp(location);
+    if (cacheTimeStamp < 0                // i.e. timestamp is invalid
+        || cacheTimeStamp < newTimeStamp) // i.e. timestamp is genuinely out of date
     {
       // newly fetch the XML description
       try
@@ -221,7 +224,7 @@ ctkCmdLineModuleManager::registerModule(const QUrl &location)
     }
     else
     {
-      if (d->ModuleCache && newTimeStamp > 0)
+      if (d->ModuleCache && newTimeStamp > 0 && !fromCache)
       {
         // successfully validated the xml, cache it
         d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
@@ -272,6 +275,26 @@ void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference&
   emit moduleUnregistered(ref);
 }
 
+
+//----------------------------------------------------------------------------
+void ctkCmdLineModuleManager::clearCache()
+{
+  d->ModuleCache->clearCache();
+}
+
+
+//----------------------------------------------------------------------------
+void ctkCmdLineModuleManager::reloadModules()
+{
+  foreach(const QUrl &location, d->LocationToRef.keys())
+  {
+    ctkCmdLineModuleReference ref = d->LocationToRef[location];
+    this->unregisterModule(ref);
+    this->registerModule(location);
+  }
+}
+
+
 //----------------------------------------------------------------------------
 ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QUrl &location) const
 {

+ 10 - 0
Libs/CommandLineModules/Core/ctkCmdLineModuleManager.h

@@ -132,6 +132,16 @@ public:
   void unregisterModule(const ctkCmdLineModuleReference& moduleRef);
 
   /**
+   * @brief Clears the XML/timestamp cache.
+   */
+  void clearCache();
+
+  /**
+   * @brief Reloads all currently valid modules, forcing the cache to be refreshed.
+   */
+  void reloadModules();
+
+  /**
    * @brief Returns a ctkCmdLineModuleReference object for the given URL.
    * @param location The location URL for which to get a module reference.
    * @return The module reference for the location or an invalid module reference