]> git.mxchange.org Git - simgear.git/commitdiff
Pkg: record live installs in Catalog.
authorJames Turner <zakalawe@mac.com>
Thu, 12 Jun 2014 14:26:05 +0000 (15:26 +0100)
committerJames Turner <zakalawe@mac.com>
Thu, 12 Jun 2014 17:43:27 +0000 (18:43 +0100)
Ensures active Installs can be found immediately after creation.

simgear/package/Catalog.cxx
simgear/package/Catalog.hxx
simgear/package/Install.cxx
simgear/package/Install.hxx
simgear/package/Package.cxx

index 6bc23efe5068b7785a9c3853062b8628f6f08d37..49b5acf4f1186b03fac54848db38fd50d183d52c 100644 (file)
@@ -216,6 +216,23 @@ Catalog::installedPackages() const
   return r;
 }
   
+InstallRef Catalog::installForPackage(PackageRef pkg) const
+{
+    PackageInstallDict::const_iterator it = m_installed.find(pkg);
+    if (it == m_installed.end()) {
+        // check if it exists on disk, create
+
+        SGPath p(pkg->pathOnDisk());
+        if (p.exists()) {
+            return Install::createFromPath(p, CatalogRef(const_cast<Catalog*>(this)));
+        }
+      
+        return NULL;
+    }
+  
+    return it->second;
+}
+  
 void Catalog::refresh()
 {
     Downloader* dl = new Downloader(this, url());
@@ -339,6 +356,23 @@ void Catalog::refreshComplete(Delegate::FailureCode aReason)
     m_root->catalogRefreshComplete(this, aReason);
 }
 
+void Catalog::registerInstall(InstallRef ins)
+{
+  if (!ins || ins->package()->catalog() != this) {
+    return;
+  }
+  
+  m_installed[ins->package()] = ins;
+}
+
+void Catalog::unregisterInstall(InstallRef ins)
+{
+  if (!ins || ins->package()->catalog() != this) {
+    return;
+  }
+  
+  m_installed.erase(ins->package());
+}
 
 } // of namespace pkg
 
index e38618193df250a7c47c951d164e588b38d6c382..4804af05d7972744d1b523b46e7b2d4ee3faaccf 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <vector>
 #include <ctime>
+#include <map>
 
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/props/props.hxx>
@@ -41,7 +42,8 @@ namespace pkg
 class Package;
 class Catalog;
 class Root;
-
+class Install;
+  
 typedef SGSharedPtr<Package> PackageRef;
 typedef SGSharedPtr<Catalog> CatalogRef;
 typedef SGSharedPtr<Install> InstallRef;
@@ -100,7 +102,9 @@ public:
     std::string description() const;
     
     PackageRef getPackageById(const std::string& aId) const;
-    
+  
+    InstallRef installForPackage(PackageRef pkg) const;
+  
     /**
      * test if the catalog data was retrieved longer ago than the
      * maximum permitted age for this catalog.
@@ -118,7 +122,11 @@ private:
     
     class Downloader;
     friend class Downloader;
-    
+  
+    friend class Install;
+    void registerInstall(InstallRef ins);
+    void unregisterInstall(InstallRef ins);
+  
     void parseProps(const SGPropertyNode* aProps);
     
     void refreshComplete(Delegate::FailureCode aReason);
@@ -135,7 +143,12 @@ private:
   
     PackageList m_packages;
     time_t m_retrievedTime;
-};  
+  
+  // important that this is a weak-ref to Installs,
+  // since it is only cleaned up in the Install destructor
+    typedef std::map<PackageRef, Install*> PackageInstallDict;
+    PackageInstallDict m_installed;
+};
     
 } // of namespace pkg
 
index 3d51629930d38440390b77b950d01e509666ad50..7d7b2fa9d31231846aff9ba437ee6342d5fc6d82 100644 (file)
@@ -262,6 +262,12 @@ Install::Install(PackageRef aPkg, const SGPath& aPath) :
     _status(Delegate::FAIL_IN_PROGRESS)
 {
     parseRevision();
+    m_package->catalog()->registerInstall(this);
+}
+  
+Install::~Install()
+{
+    m_package->catalog()->unregisterInstall(this);
 }
 
 InstallRef Install::createFromPath(const SGPath& aPath, CatalogRef aCat)
index f5458d69eb4c6fba749fceefa39142d8c3aadc1d..81cf580adee46d82e87f30f6aac878e39a453d92 100644 (file)
@@ -50,6 +50,8 @@ typedef SGSharedPtr<Install> InstallRef;
 class Install : public SGReferenced
 {
 public:
+    virtual ~Install();
+  
     typedef boost::function<void(Install*)> Callback;
     typedef boost::function<void(Install*, unsigned int, unsigned int)>
                                             ProgressCallback;
index 8e697c733dcc14ec3e8dce04f0f0016dbbf04d70..76ae4f0ec971687e5fb18a1cf1bc12acf11ed488 100644 (file)
@@ -110,24 +110,20 @@ SGPath Package::pathOnDisk() const
 
 InstallRef Package::install()
 {
-    SGPath p(pathOnDisk());
-    if (p.exists()) {
-        return Install::createFromPath(p, m_catalog);
+    InstallRef ins = existingInstall();
+    if (ins) {
+        return ins;
     }
-    
-    InstallRef ins(new Install(this, p));
+  
+  // start a new install
+    ins = new Install(this, pathOnDisk());
     m_catalog->root()->scheduleToUpdate(ins);
     return ins;
 }
 
 InstallRef Package::existingInstall() const
 {
-    SGPath p(pathOnDisk());
-    if (p.exists()) {
-        return Install::createFromPath(p, m_catalog);
-    }
-
-    return NULL;
+    return m_catalog->installForPackage(const_cast<Package*>(this));
 }
 
 std::string Package::id() const