1 // Copyright (C) 2013 James Turner - zakalawe@mac.com
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.
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.
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.
18 #ifndef SG_PACKAGE_INSTALL_HXX
19 #define SG_PACKAGE_INSTALL_HXX
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/misc/sg_dir.hxx>
25 #include <simgear/package/Delegate.hxx>
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>
32 #include <boost/bind.hpp>
45 typedef SGSharedPtr<Package> PackageRef;
46 typedef SGSharedPtr<Catalog> CatalogRef;
47 typedef SGSharedPtr<Install> InstallRef;
52 class Install : public SGReferenced
57 typedef boost::function<void(Install*)> Callback;
58 typedef boost::function<void(Install*, unsigned int, unsigned int)>
62 * create from a directory on disk, or fail.
64 static InstallRef createFromPath(const SGPath& aPath, CatalogRef aCat);
66 unsigned int revsion() const
67 { return m_revision; }
69 PackageRef package() const
75 bool hasUpdate() const;
81 bool isDownloading() const;
83 bool isQueued() const;
85 int downloadedPercent() const;
87 size_t downloadedBytes() const;
90 * full path to the primary -set.xml file for this install
92 SGPath primarySetPath() const;
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.
99 void cancelDownload();
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()
106 PathList thumbnailPaths() const;
109 * Set the handler to be called when the installation successfully
112 * @note If the installation is already complete, the handler is called
115 Install* done(const Callback& cb);
118 Install* done(C* instance, void (C::*mem_func)(Install*))
120 return done(boost::bind(mem_func, instance, _1));
124 * Set the handler to be called when the installation fails or is aborted.
126 * @note If the installation has already failed, the handler is called
129 Install* fail(const Callback& cb);
132 Install* fail(C* instance, void (C::*mem_func)(Install*))
134 return fail(boost::bind(mem_func, instance, _1));
138 * Set the handler to be called when the installation either successfully
139 * completes or fails.
141 * @note If the installation is already complete or has already failed, the
142 * handler is called immediately.
144 Install* always(const Callback& cb);
147 Install* always(C* instance, void (C::*mem_func)(Install*))
149 return always(boost::bind(mem_func, instance, _1));
153 * Set the handler to be called during downloading the installation file
154 * indicating the progress of the download.
157 Install* progress(const ProgressCallback& cb);
160 Install* progress(C* instance,
161 void (C::*mem_func)(Install*, unsigned int, unsigned int))
163 return progress(boost::bind(mem_func, instance, _1, _2, _3));
167 friend class Package;
169 class PackageArchiveDownloader;
170 friend class PackageArchiveDownloader;
172 Install(PackageRef aPkg, const SGPath& aPath);
174 void parseRevision();
175 void writeRevisionFile();
177 void installResult(Delegate::StatusCode aReason);
178 void installProgress(unsigned int aBytes, unsigned int aTotal);
180 PackageRef m_package;
181 unsigned int m_revision; ///< revision on disk
182 SGPath m_path; ///< installation point on disk
184 HTTP::Request_ptr m_download;
186 Delegate::StatusCode m_status;
188 function_list<Callback> _cb_done,
191 function_list<ProgressCallback> _cb_progress;
195 } // of namespace pkg
197 } // of namespace simgear
199 #endif // of SG_PACKAGE_CATALOG_HXX