Ensures active Installs can be found immediately after creation.
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());
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
#include <vector>
#include <ctime>
+#include <map>
#include <simgear/misc/sg_path.hxx>
#include <simgear/props/props.hxx>
class Package;
class Catalog;
class Root;
-
+class Install;
+
typedef SGSharedPtr<Package> PackageRef;
typedef SGSharedPtr<Catalog> CatalogRef;
typedef SGSharedPtr<Install> InstallRef;
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.
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);
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
_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)
class Install : public SGReferenced
{
public:
+ virtual ~Install();
+
typedef boost::function<void(Install*)> Callback;
typedef boost::function<void(Install*, unsigned int, unsigned int)>
ProgressCallback;
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