]> git.mxchange.org Git - simgear.git/blob - simgear/package/Install.hxx
81cf580adee46d82e87f30f6aac878e39a453d92
[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/package/Delegate.hxx>
25
26 #include <simgear/structure/SGReferenced.hxx>
27 #include <simgear/structure/SGSharedPtr.hxx>
28
29 #include <boost/bind.hpp>
30 #include <boost/function.hpp>
31
32 namespace simgear
33 {
34     
35 namespace pkg
36 {
37
38 // forward decls
39 class Package;
40 class Catalog;
41 class Install;
42   
43 typedef SGSharedPtr<Package> PackageRef;
44 typedef SGSharedPtr<Catalog> CatalogRef;  
45 typedef SGSharedPtr<Install> InstallRef;
46   
47 /**
48  *
49  */
50 class Install : public SGReferenced
51 {
52 public:
53     virtual ~Install();
54   
55     typedef boost::function<void(Install*)> Callback;
56     typedef boost::function<void(Install*, unsigned int, unsigned int)>
57                                             ProgressCallback;
58
59     /**
60      * create from a directory on disk, or fail.
61      */
62     static InstallRef createFromPath(const SGPath& aPath, CatalogRef aCat);
63     
64     unsigned int revsion() const
65         { return m_revision; }
66     
67     PackageRef package() const
68         { return m_package; } 
69     
70     SGPath path() const
71         { return m_path; }
72     
73     bool hasUpdate() const;
74     
75     void startUpdate();
76     
77     void uninstall();
78
79     /**
80      * Set the handler to be called when the installation successfully
81      * completes.
82      *
83      * @note If the installation is already complete, the handler is called
84      *       immediately.
85      */
86     Install* done(const Callback& cb);
87
88     template<class C>
89     Install* done(C* instance, void (C::*mem_func)(Install*))
90     {
91       return done(boost::bind(mem_func, instance, _1));
92     }
93
94     /**
95      * Set the handler to be called when the installation fails or is aborted.
96      *
97      * @note If the installation has already failed, the handler is called
98      *       immediately.
99      */
100     Install* fail(const Callback& cb);
101
102     template<class C>
103     Install* fail(C* instance, void (C::*mem_func)(Install*))
104     {
105       return fail(boost::bind(mem_func, instance, _1));
106     }
107
108     /**
109      * Set the handler to be called when the installation either successfully
110      * completes or fails.
111      *
112      * @note If the installation is already complete or has already failed, the
113      *       handler is called immediately.
114      */
115     Install* always(const Callback& cb);
116
117     template<class C>
118     Install* always(C* instance, void (C::*mem_func)(Install*))
119     {
120       return always(boost::bind(mem_func, instance, _1));
121     }
122     
123     /**
124      * Set the handler to be called during downloading the installation file
125      * indicating the progress of the download.
126      *
127      */
128     Install* progress(const ProgressCallback& cb);
129
130     template<class C>
131     Install* progress(C* instance,
132                       void (C::*mem_func)(Install*, unsigned int, unsigned int))
133     {
134       return progress(boost::bind(mem_func, instance, _1, _2, _3));
135     }
136
137 private:
138     friend class Package;
139     
140     class PackageArchiveDownloader;
141     friend class PackageArchiveDownloader;
142     
143     Install(PackageRef aPkg, const SGPath& aPath);
144     
145     void parseRevision();
146     void writeRevisionFile();
147     
148     void installResult(Delegate::FailureCode aReason);
149     void installProgress(unsigned int aBytes, unsigned int aTotal);
150     
151     PackageRef m_package;
152     unsigned int m_revision; ///< revision on disk
153     SGPath m_path; ///< installation point on disk
154     
155     PackageArchiveDownloader* m_download;
156
157     Delegate::FailureCode _status;
158
159     Callback          _cb_done,
160                       _cb_fail,
161                       _cb_always;
162     ProgressCallback  _cb_progress;
163
164 };
165     
166     
167 } // of namespace pkg
168
169 } // of namespace simgear
170
171 #endif // of SG_PACKAGE_CATALOG_HXX