From: James Turner Date: Thu, 12 Jun 2014 14:26:05 +0000 (+0100) Subject: Pkg: record live installs in Catalog. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=dc60cf0e67541212d05c9176a5f34324628d2c8e;p=simgear.git Pkg: record live installs in Catalog. Ensures active Installs can be found immediately after creation. --- diff --git a/simgear/package/Catalog.cxx b/simgear/package/Catalog.cxx index 6bc23efe..49b5acf4 100644 --- a/simgear/package/Catalog.cxx +++ b/simgear/package/Catalog.cxx @@ -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(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 diff --git a/simgear/package/Catalog.hxx b/simgear/package/Catalog.hxx index e3861819..4804af05 100644 --- a/simgear/package/Catalog.hxx +++ b/simgear/package/Catalog.hxx @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -41,7 +42,8 @@ namespace pkg class Package; class Catalog; class Root; - +class Install; + typedef SGSharedPtr PackageRef; typedef SGSharedPtr CatalogRef; typedef SGSharedPtr 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 PackageInstallDict; + PackageInstallDict m_installed; +}; } // of namespace pkg diff --git a/simgear/package/Install.cxx b/simgear/package/Install.cxx index 3d516299..7d7b2fa9 100644 --- a/simgear/package/Install.cxx +++ b/simgear/package/Install.cxx @@ -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) diff --git a/simgear/package/Install.hxx b/simgear/package/Install.hxx index f5458d69..81cf580a 100644 --- a/simgear/package/Install.hxx +++ b/simgear/package/Install.hxx @@ -50,6 +50,8 @@ typedef SGSharedPtr InstallRef; class Install : public SGReferenced { public: + virtual ~Install(); + typedef boost::function Callback; typedef boost::function ProgressCallback; diff --git a/simgear/package/Package.cxx b/simgear/package/Package.cxx index 8e697c73..76ae4f0e 100644 --- a/simgear/package/Package.cxx +++ b/simgear/package/Package.cxx @@ -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(this)); } std::string Package::id() const