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 #include <simgear/io/HTTPClient.hxx>
19 #include <simgear/package/Catalog.hxx>
20 #include <simgear/package/Package.hxx>
21 #include <simgear/package/Install.hxx>
22 #include <simgear/package/Root.hxx>
23 #include <simgear/misc/sg_dir.hxx>
25 #include <boost/foreach.hpp>
29 using namespace simgear;
32 bool keepRunning = true;
34 class MyDelegate : public pkg::Delegate
37 virtual void refreshComplete()
41 virtual void failedRefresh(pkg::Catalog* aCatalog, FailureCode aReason)
43 cerr << "failed refresh of " << aCatalog->description() << ":" << aReason << endl;
46 virtual void startInstall(pkg::Install* aInstall)
49 cout << "starting install of " << aInstall->package()->name() << endl;
52 virtual void installProgress(pkg::Install* aInstall, unsigned int bytes, unsigned int total)
54 unsigned int percent = (bytes * 100) / total;
55 if (percent == _lastPercent) {
59 _lastPercent = percent;
60 cout << percent << "%" << endl;
63 virtual void finishInstall(pkg::Install* aInstall)
65 cout << "done install of " << aInstall->package()->name() << endl;
68 virtual void failedInstall(pkg::Install* aInstall, FailureCode aReason)
70 cerr << "failed install of " << aInstall->package()->name() << endl;
73 unsigned int _lastPercent;
77 void printRating(pkg::Package* pkg, const std::string& aRating, const std::string& aLabel)
79 SGPropertyNode* ratings = pkg->properties()->getChild("rating");
80 cout << "\t" << aLabel << ":" << ratings->getIntValue(aRating) << endl;
83 void printPackageInfo(pkg::Package* pkg)
85 cout << "Package:" << pkg->catalog()->id() << "." << pkg->id() << endl;
86 cout << "Revision:" << pkg->revision() << endl;
87 cout << "Name:" << pkg->name() << endl;
88 cout << "Description:" << pkg->description() << endl;
89 cout << "Long description:\n" << pkg->getLocalisedProp("long-description") << endl << endl;
91 if (pkg->properties()->hasChild("author")) {
92 cout << "Authors:" << endl;
93 BOOST_FOREACH(SGPropertyNode* author, pkg->properties()->getChildren("author")) {
94 if (author->hasChild("name")) {
95 cout << "\t" << author->getStringValue("name") << endl;
98 // simple author structure
99 cout << "\t" << author->getStringValue() << endl;
108 cout << "Ratings:" << endl;
109 printRating(pkg, "fdm", "Flight-model ");
110 printRating(pkg, "cockpit", "Cockpit ");
111 printRating(pkg, "model", "3D model ");
112 printRating(pkg, "systems", "Aircraft systems");
115 int main(int argc, char** argv)
118 HTTP::Client* http = new HTTP::Client();
119 pkg::Root* root = new pkg::Root(Dir::current().path(), "");
122 root->setDelegate(&dlg);
124 cout << "Package root is:" << Dir::current().path() << endl;
125 cout << "have " << pkg::Catalog::allCatalogs().size() << " catalog(s)" << endl;
127 root->setHTTPClient(http);
129 if (!strcmp(argv[1], "add")) {
130 std::string url(argv[2]);
131 pkg::Catalog::createFromUrl(root, url);
132 } else if (!strcmp(argv[1], "refresh")) {
134 } else if (!strcmp(argv[1], "install")) {
135 pkg::PackageRef pkg = root->getPackageById(argv[2]);
137 cerr << "unknown package:" << argv[2] << endl;
141 if (pkg->isInstalled()) {
142 cout << "package " << pkg->id() << " is already installed at " << pkg->install()->path() << endl;
146 pkg::CatalogRef catalog = pkg->catalog();
147 cout << "Will install:" << pkg->id() << " from " << catalog->id() <<
148 "(" << catalog->description() << ")" << endl;
150 } else if (!strcmp(argv[1], "uninstall") || !strcmp(argv[1], "remove")) {
151 pkg::PackageRef pkg = root->getPackageById(argv[2]);
153 cerr << "unknown package:" << argv[2] << endl;
157 if (!pkg->isInstalled()) {
158 cerr << "package " << argv[2] << " not installed" << endl;
162 cout << "Will uninstall:" << pkg->id() << endl;
163 pkg->install()->uninstall();
164 } else if (!strcmp(argv[1], "update-all")) {
165 pkg::PackageList updates = root->packagesNeedingUpdate();
166 BOOST_FOREACH(pkg::Package* p, updates) {
167 root->scheduleToUpdate(p->install());
169 } else if (!strcmp(argv[1], "list-updated")) {
170 pkg::PackageList updates = root->packagesNeedingUpdate();
171 if (updates.empty()) {
172 cout << "no packages with updates" << endl;
176 cout << updates.size() << " packages have updates" << endl;
177 BOOST_FOREACH(pkg::Package* p, updates) {
178 cout << "\t" << p->id() << " " << p->getLocalisedProp("name") << endl;
180 } else if (!strcmp(argv[1], "info")) {
181 pkg::PackageRef pkg = root->getPackageById(argv[2]);
183 cerr << "unknown package:" << argv[2] << endl;
187 printPackageInfo(pkg);
189 cerr << "unknown command:" << argv[1] << endl;
193 while (http->hasActiveRequests()) {