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