]> git.mxchange.org Git - simgear.git/blob - simgear/package/Catalog.hxx
Threadsafe terrasync state updates/reading.
[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 <boost/bind.hpp>
26
27 #include <simgear/misc/sg_path.hxx>
28 #include <simgear/props/props.hxx>
29
30 #include <simgear/structure/SGReferenced.hxx>
31 #include <simgear/structure/SGSharedPtr.hxx>
32 #include <simgear/structure/function_list.hxx>
33 #include <simgear/io/HTTPRequest.hxx>
34
35 #include <simgear/package/Delegate.hxx>
36
37 namespace simgear
38 {
39
40 namespace HTTP { class Client; }
41
42 namespace pkg
43 {
44
45 // forward decls
46 class Package;
47 class Catalog;
48 class Root;
49 class Install;
50
51 typedef SGSharedPtr<Package> PackageRef;
52 typedef SGSharedPtr<Catalog> CatalogRef;
53 typedef SGSharedPtr<Install> InstallRef;
54
55 typedef std::vector<PackageRef> PackageList;
56 typedef std::vector<CatalogRef> CatalogList;
57
58 class Catalog : public SGReferenced
59 {
60 public:
61     virtual ~Catalog();
62
63     static CatalogRef createFromUrl(Root* aRoot, const std::string& aUrl);
64
65     static CatalogRef createFromPath(Root* aRoot, const SGPath& aPath);
66
67     Root* root() const
68         { return m_root;};
69
70     /**
71      * uninstall this catalog entirely, including all installed packages
72      */
73     bool uninstall();
74
75     /**
76      * perform a refresh of the catalog contents
77      */
78     void refresh();
79
80     /**
81      * Get all packages in this catalog.
82      */
83     PackageList const& packages() const;
84
85     /**
86      * retrieve packages in this catalog matching a filter.
87      * filter consists of required / minimum values, AND-ed together.
88      */
89     PackageList packagesMatching(const SGPropertyNode* aFilter) const;
90
91     /**
92      * packages which are locally installed
93      */
94     PackageList installedPackages() const;
95
96     /**
97      * retrieve all the packages in the catalog which are installed
98      * and have a pendig update
99      */
100     PackageList packagesNeedingUpdate() const;
101
102     SGPath installRoot() const
103          { return m_installRoot; }
104
105     std::string id() const;
106
107     std::string url() const;
108
109     /**
110      * update the URL of a package. Does not trigger a refresh, but resets
111      * error state if the previous URL was not found.
112      */
113     void setUrl(const std::string& url);
114
115     std::string name() const;
116
117     std::string description() const;
118
119     PackageRef getPackageById(const std::string& aId) const;
120
121     PackageRef getPackageByPath(const std::string& aPath) const;
122
123     /**
124      * test if the catalog data was retrieved longer ago than the
125      * maximum permitted age for this catalog.
126      */
127     bool needsRefresh() const;
128
129     unsigned int ageInSeconds() const;
130
131     /**
132      * access the raw property data in the catalog
133      */
134     SGPropertyNode* properties() const;
135
136     Delegate::StatusCode status() const;
137
138     typedef boost::function<void(Catalog*)> Callback;
139
140     void addStatusCallback(const Callback& cb);
141
142     template<class C>
143     void addStatusCallback(C* instance, void (C::*mem_func)(Catalog*))
144     {
145       return addStatusCallback(boost::bind(mem_func, instance, _1));
146     }
147 private:
148     Catalog(Root* aRoot);
149
150     class Downloader;
151     friend class Downloader;
152
153     void parseProps(const SGPropertyNode* aProps);
154
155     void refreshComplete(Delegate::StatusCode aReason);
156
157     void parseTimestamp();
158     void writeTimestamp();
159
160     std::string getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const;
161
162     void changeStatus(Delegate::StatusCode newStatus);
163
164     Root* m_root;
165     SGPropertyNode_ptr m_props;
166     SGPath m_installRoot;
167     std::string m_url;
168     Delegate::StatusCode m_status;
169     HTTP::Request_ptr m_refreshRequest;
170
171     PackageList m_packages;
172     time_t m_retrievedTime;
173
174     typedef std::map<std::string, Package*> PackageWeakMap;
175     PackageWeakMap m_variantDict;
176
177     function_list<Callback> m_statusCallbacks;
178 };
179
180 } // of namespace pkg
181
182 } // of namespace simgear
183
184 #endif // of SG_PACKAGE_CATALOG_HXX