]> git.mxchange.org Git - simgear.git/blob - simgear/package/Catalog.hxx
Make nasal/iolib.h available to flightgear (for io.open)
[simgear.git] / simgear / package / Catalog.hxx
1 // Copyright (C) 2013  James Turner - zakalawe@mac.com
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef SG_PACKAGE_CATALOG_HXX
19 #define SG_PACKAGE_CATALOG_HXX
20
21 #include <vector>
22 #include <ctime>
23 #include <map>
24
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/props/props.hxx>
27
28 #include <simgear/structure/SGReferenced.hxx>
29 #include <simgear/structure/SGSharedPtr.hxx>
30
31 #include <simgear/package/Delegate.hxx>
32
33 namespace simgear
34 {
35
36 namespace HTTP { class Client; }
37
38 namespace pkg
39 {
40
41 // forward decls
42 class Package;
43 class Catalog;
44 class Root;
45 class Install;
46
47 typedef SGSharedPtr<Package> PackageRef;
48 typedef SGSharedPtr<Catalog> CatalogRef;
49 typedef SGSharedPtr<Install> InstallRef;
50
51 typedef std::vector<PackageRef> PackageList;
52 typedef std::vector<CatalogRef> CatalogList;
53
54 class Catalog : public SGReferenced
55 {
56 public:
57     virtual ~Catalog();
58
59     static CatalogRef createFromUrl(Root* aRoot, const std::string& aUrl);
60
61     static CatalogRef createFromPath(Root* aRoot, const SGPath& aPath);
62
63     Root* root() const
64         { return m_root;};
65
66     /**
67      * perform a refresh of the catalog contents
68      */
69     void refresh();
70
71     /**
72      * Get all packages in this catalog.
73      */
74     PackageList const& packages() const;
75
76     /**
77      * retrieve packages in this catalog matching a filter.
78      * filter consists of required / minimum values, AND-ed together.
79      */
80     PackageList packagesMatching(const SGPropertyNode* aFilter) const;
81
82     /**
83      * packages which are locally installed
84      */
85     PackageList installedPackages() const;
86
87     /**
88      * retrieve all the packages in the catalog which are installed
89      * and have a pendig update
90      */
91     PackageList packagesNeedingUpdate() const;
92
93     SGPath installRoot() const
94          { return m_installRoot; }
95
96     std::string id() const;
97
98     std::string url() const;
99
100     std::string description() const;
101
102     PackageRef getPackageById(const std::string& aId) const;
103
104     InstallRef installForPackage(PackageRef pkg) const;
105
106     /**
107      * test if the catalog data was retrieved longer ago than the
108      * maximum permitted age for this catalog.
109      */
110     bool needsRefresh() const;
111
112     unsigned int ageInSeconds() const;
113
114     /**
115      * access the raw property data in the catalog
116      */
117     SGPropertyNode* properties() const;
118 private:
119     Catalog(Root* aRoot);
120
121     class Downloader;
122     friend class Downloader;
123
124     friend class Install;
125     void registerInstall(Install* ins);
126     void unregisterInstall(Install* ins);
127
128     void parseProps(const SGPropertyNode* aProps);
129
130     void refreshComplete(Delegate::FailureCode aReason);
131
132     void parseTimestamp();
133     void writeTimestamp();
134
135     std::string getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const;
136
137     Root* m_root;
138     SGPropertyNode_ptr m_props;
139     SGPath m_installRoot;
140     std::string m_url;
141
142     PackageList m_packages;
143     time_t m_retrievedTime;
144
145     typedef std::map<std::string, Package*> PackageWeakMap;
146     PackageWeakMap m_variantDict;
147
148   // important that this is a weak-ref to Installs,
149   // since it is only cleaned up in the Install destructor
150     typedef std::map<PackageRef, Install*> PackageInstallDict;
151     PackageInstallDict m_installed;
152 };
153
154 } // of namespace pkg
155
156 } // of namespace simgear
157
158 #endif // of SG_PACKAGE_CATALOG_HXX