]> git.mxchange.org Git - simgear.git/blob - simgear/package/Install.hxx
Packages: increased test coverage.
[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/function_list.hxx>
27 #include <simgear/structure/SGReferenced.hxx>
28 #include <simgear/structure/SGSharedPtr.hxx>
29
30 #include <boost/bind.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     bool uninstall();
78
79     bool isDownloading() const;
80     
81     /**
82      * Set the handler to be called when the installation successfully
83      * completes.
84      *
85      * @note If the installation is already complete, the handler is called
86      *       immediately.
87      */
88     Install* done(const Callback& cb);
89
90     template<class C>
91     Install* done(C* instance, void (C::*mem_func)(Install*))
92     {
93       return done(boost::bind(mem_func, instance, _1));
94     }
95
96     /**
97      * Set the handler to be called when the installation fails or is aborted.
98      *
99      * @note If the installation has already failed, the handler is called
100      *       immediately.
101      */
102     Install* fail(const Callback& cb);
103
104     template<class C>
105     Install* fail(C* instance, void (C::*mem_func)(Install*))
106     {
107       return fail(boost::bind(mem_func, instance, _1));
108     }
109
110     /**
111      * Set the handler to be called when the installation either successfully
112      * completes or fails.
113      *
114      * @note If the installation is already complete or has already failed, the
115      *       handler is called immediately.
116      */
117     Install* always(const Callback& cb);
118
119     template<class C>
120     Install* always(C* instance, void (C::*mem_func)(Install*))
121     {
122       return always(boost::bind(mem_func, instance, _1));
123     }
124     
125     /**
126      * Set the handler to be called during downloading the installation file
127      * indicating the progress of the download.
128      *
129      */
130     Install* progress(const ProgressCallback& cb);
131
132     template<class C>
133     Install* progress(C* instance,
134                       void (C::*mem_func)(Install*, unsigned int, unsigned int))
135     {
136       return progress(boost::bind(mem_func, instance, _1, _2, _3));
137     }
138
139 private:
140     friend class Package;
141     
142     class PackageArchiveDownloader;
143     friend class PackageArchiveDownloader;
144     
145     Install(PackageRef aPkg, const SGPath& aPath);
146     
147     void parseRevision();
148     void writeRevisionFile();
149     
150     void installResult(Delegate::FailureCode aReason);
151     void installProgress(unsigned int aBytes, unsigned int aTotal);
152     
153     PackageRef m_package;
154     unsigned int m_revision; ///< revision on disk
155     SGPath m_path; ///< installation point on disk
156     
157     PackageArchiveDownloader* m_download;
158
159     Delegate::FailureCode _status;
160
161     function_list<Callback>         _cb_done,
162                                     _cb_fail,
163                                     _cb_always;
164     function_list<ProgressCallback> _cb_progress;
165 };
166     
167     
168 } // of namespace pkg
169
170 } // of namespace simgear
171
172 #endif // of SG_PACKAGE_CATALOG_HXX