]> git.mxchange.org Git - simgear.git/blob - simgear/package/Install.hxx
Allow updating a Catalog URL explicitly.
[simgear.git] / simgear / package / Install.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_INSTALL_HXX
19 #define SG_PACKAGE_INSTALL_HXX
20
21 #include <vector>
22
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/misc/sg_dir.hxx>
25 #include <simgear/package/Delegate.hxx>
26
27 #include <simgear/structure/function_list.hxx>
28 #include <simgear/structure/SGReferenced.hxx>
29 #include <simgear/structure/SGSharedPtr.hxx>
30 #include <simgear/io/HTTPRequest.hxx>
31
32 #include <boost/bind.hpp>
33
34 namespace simgear
35 {
36     
37 namespace pkg
38 {
39
40 // forward decls
41 class Package;
42 class Catalog;
43 class Install;
44   
45 typedef SGSharedPtr<Package> PackageRef;
46 typedef SGSharedPtr<Catalog> CatalogRef;  
47 typedef SGSharedPtr<Install> InstallRef;
48   
49 /**
50  *
51  */
52 class Install : public SGReferenced
53 {
54 public:
55     virtual ~Install();
56   
57     typedef boost::function<void(Install*)> Callback;
58     typedef boost::function<void(Install*, unsigned int, unsigned int)>
59                                             ProgressCallback;
60
61     /**
62      * create from a directory on disk, or fail.
63      */
64     static InstallRef createFromPath(const SGPath& aPath, CatalogRef aCat);
65     
66     unsigned int revsion() const
67         { return m_revision; }
68     
69     PackageRef package() const
70         { return m_package; } 
71     
72     SGPath path() const
73         { return m_path; }
74     
75     bool hasUpdate() const;
76     
77     void startUpdate();
78
79     bool uninstall();
80
81     bool isDownloading() const;
82     
83     bool isQueued() const;
84     
85     int downloadedPercent() const;
86     
87     size_t downloadedBytes() const;
88     
89     /**
90      * full path to the primary -set.xml file for this install
91      */
92     SGPath primarySetPath() const;
93     
94     /**
95      * if a download is in progress, cancel it. If this is the first install
96      * of the package (as opposed to an update), the install will be cleaned
97      * up once the last reference is gone.
98      */
99     void cancelDownload();
100     
101     /**
102      * return the thumbnails associated with this install, but as locations
103      * on the file system, not URLs. It is assumed the order of thumbnails
104      * is consistent with the URLs returned from Package::thumbnailUrls()
105      */
106     PathList thumbnailPaths() const;
107     
108     /**
109      * Set the handler to be called when the installation successfully
110      * completes.
111      *
112      * @note If the installation is already complete, the handler is called
113      *       immediately.
114      */
115     Install* done(const Callback& cb);
116
117     template<class C>
118     Install* done(C* instance, void (C::*mem_func)(Install*))
119     {
120       return done(boost::bind(mem_func, instance, _1));
121     }
122
123     /**
124      * Set the handler to be called when the installation fails or is aborted.
125      *
126      * @note If the installation has already failed, the handler is called
127      *       immediately.
128      */
129     Install* fail(const Callback& cb);
130
131     template<class C>
132     Install* fail(C* instance, void (C::*mem_func)(Install*))
133     {
134       return fail(boost::bind(mem_func, instance, _1));
135     }
136
137     /**
138      * Set the handler to be called when the installation either successfully
139      * completes or fails.
140      *
141      * @note If the installation is already complete or has already failed, the
142      *       handler is called immediately.
143      */
144     Install* always(const Callback& cb);
145
146     template<class C>
147     Install* always(C* instance, void (C::*mem_func)(Install*))
148     {
149       return always(boost::bind(mem_func, instance, _1));
150     }
151     
152     /**
153      * Set the handler to be called during downloading the installation file
154      * indicating the progress of the download.
155      *
156      */
157     Install* progress(const ProgressCallback& cb);
158
159     template<class C>
160     Install* progress(C* instance,
161                       void (C::*mem_func)(Install*, unsigned int, unsigned int))
162     {
163       return progress(boost::bind(mem_func, instance, _1, _2, _3));
164     }
165
166 private:
167     friend class Package;
168     
169     class PackageArchiveDownloader;
170     friend class PackageArchiveDownloader;
171     
172     Install(PackageRef aPkg, const SGPath& aPath);
173     
174     void parseRevision();
175     void writeRevisionFile();
176     
177     void installResult(Delegate::StatusCode aReason);
178     void installProgress(unsigned int aBytes, unsigned int aTotal);
179     
180     PackageRef m_package;
181     unsigned int m_revision; ///< revision on disk
182     SGPath m_path; ///< installation point on disk
183     
184     HTTP::Request_ptr m_download;
185
186     Delegate::StatusCode m_status;
187
188     function_list<Callback>         _cb_done,
189                                     _cb_fail,
190                                     _cb_always;
191     function_list<ProgressCallback> _cb_progress;
192 };
193     
194     
195 } // of namespace pkg
196
197 } // of namespace simgear
198
199 #endif // of SG_PACKAGE_CATALOG_HXX