]> git.mxchange.org Git - simgear.git/blob - simgear/package/pkgutil.cxx
Fix missing <cstdint> on non-Mac
[simgear.git] / simgear / package / pkgutil.cxx
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 #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>
24
25 #include <boost/foreach.hpp>
26 #include <iostream>
27 #include <cstring>
28
29 using namespace simgear;
30 using namespace std;
31
32 bool keepRunning = true;
33
34 class MyDelegate : public pkg::Delegate
35 {
36 public:
37     virtual void catalogRefreshed(pkg::CatalogRef aCatalog, StatusCode aReason)
38     {
39         if (aReason == STATUS_REFRESHED) {
40             if (aCatalog.ptr() == NULL) {
41                 cout << "refreshed all catalogs" << endl;
42             } else {
43                 cout << "refreshed catalog " << aCatalog->url() << endl;
44             }
45         } else if (aReason == STATUS_IN_PROGRESS) {
46             cout << "started refresh of " << aCatalog->url() << endl;
47         } else {
48             cerr << "failed refresh of " << aCatalog->url() << ":" << aReason << endl;
49         }
50     }
51
52     virtual void startInstall(pkg::InstallRef aInstall)
53     {
54         _lastPercent = 999;
55         cout << "starting install of " << aInstall->package()->name() << endl;
56     }
57
58     virtual void installProgress(pkg::InstallRef aInstall, unsigned int bytes, unsigned int total)
59     {
60         unsigned int percent = (bytes * 100) / total;
61         if (percent == _lastPercent) {
62             return;
63         }
64
65         _lastPercent = percent;
66         cout << percent << "%" << endl;
67     }
68
69     virtual void finishInstall(pkg::InstallRef aInstall, StatusCode aReason)
70     {
71         if (aReason == STATUS_SUCCESS) {
72             cout << "done install of " << aInstall->package()->name() << endl;
73         } else {
74             cerr << "failed install of " << aInstall->package()->name() << endl;
75         }
76     }
77
78 private:
79     unsigned int _lastPercent;
80
81 };
82
83 void printRating(pkg::Package* pkg, const std::string& aRating, const std::string& aLabel)
84 {
85     SGPropertyNode* ratings = pkg->properties()->getChild("rating");
86     cout << "\t" << aLabel << ":" << ratings->getIntValue(aRating) << endl;
87 }
88
89 void printPackageInfo(pkg::Package* pkg)
90 {
91     cout << "Package:" << pkg->catalog()->id() << "." << pkg->id() << endl;
92     cout << "Revision:" << pkg->revision() << endl;
93     cout << "Name:" << pkg->name() << endl;
94     cout << "Description:" << pkg->description() << endl;
95     cout << "Long description:\n" << pkg->getLocalisedProp("long-description") << endl << endl;
96
97     if (pkg->properties()->hasChild("author")) {
98         cout << "Authors:" << endl;
99         BOOST_FOREACH(SGPropertyNode* author, pkg->properties()->getChildren("author")) {
100             if (author->hasChild("name")) {
101                 cout << "\t" << author->getStringValue("name") << endl;
102
103             } else {
104                 // simple author structure
105                 cout << "\t" << author->getStringValue() << endl;
106             }
107
108
109         }
110
111         cout << endl;
112     }
113
114     cout << "Ratings:" << endl;
115     printRating(pkg, "fdm",     "Flight-model    ");
116     printRating(pkg, "cockpit", "Cockpit         ");
117     printRating(pkg, "model",   "3D model        ");
118     printRating(pkg, "systems", "Aircraft systems");
119 }
120
121 int main(int argc, char** argv)
122 {
123
124     HTTP::Client* http = new HTTP::Client();
125     pkg::Root* root = new pkg::Root(Dir::current().path(), "");
126
127     MyDelegate dlg;
128     root->addDelegate(&dlg);
129
130     cout << "Package root is:" << Dir::current().path() << endl;
131     cout << "have " << root->catalogs().size() << " catalog(s)" << endl;
132
133     root->setHTTPClient(http);
134
135     if (!strcmp(argv[1], "add")) {
136         std::string url(argv[2]);
137         pkg::Catalog::createFromUrl(root, url);
138     } else if (!strcmp(argv[1], "refresh")) {
139         root->refresh(true);
140     } else if (!strcmp(argv[1], "install")) {
141         pkg::PackageRef pkg = root->getPackageById(argv[2]);
142         if (!pkg) {
143             cerr << "unknown package:" << argv[2] << endl;
144             return EXIT_FAILURE;
145         }
146
147         if (pkg->isInstalled()) {
148             cout << "package " << pkg->id() << " is already installed at " << pkg->install()->path() << endl;
149             return EXIT_SUCCESS;
150         }
151
152         pkg::CatalogRef catalog = pkg->catalog();
153         cout << "Will install:" << pkg->id() << " from " << catalog->id() <<
154                 "(" << catalog->description() << ")" << endl;
155         pkg->install();
156     } else if (!strcmp(argv[1], "uninstall") || !strcmp(argv[1], "remove")) {
157         pkg::PackageRef pkg = root->getPackageById(argv[2]);
158         if (!pkg) {
159             cerr << "unknown package:" << argv[2] << endl;
160             return EXIT_FAILURE;
161         }
162
163         if (!pkg->isInstalled()) {
164             cerr << "package " << argv[2] << " not installed" << endl;
165             return EXIT_FAILURE;
166         }
167
168         cout << "Will uninstall:" << pkg->id() << endl;
169         pkg->install()->uninstall();
170     } else if (!strcmp(argv[1], "update-all")) {
171         pkg::PackageList updates = root->packagesNeedingUpdate();
172         BOOST_FOREACH(pkg::Package* p, updates) {
173             root->scheduleToUpdate(p->install());
174         }
175     } else if (!strcmp(argv[1], "list-updated")) {
176         pkg::PackageList updates = root->packagesNeedingUpdate();
177         if (updates.empty()) {
178             cout << "no packages with updates" << endl;
179             return EXIT_SUCCESS;
180         }
181
182         cout << updates.size() << " packages have updates" << endl;
183         BOOST_FOREACH(pkg::Package* p, updates) {
184             cout << "\t" << p->id() << " " << p->getLocalisedProp("name") << endl;
185         }
186     } else if (!strcmp(argv[1], "info")) {
187         pkg::PackageRef pkg = root->getPackageById(argv[2]);
188         if (!pkg) {
189             cerr << "unknown package:" << argv[2] << endl;
190             return EXIT_FAILURE;
191         }
192
193         printPackageInfo(pkg);
194     } else {
195         cerr << "unknown command:" << argv[1] << endl;
196         return EXIT_FAILURE;
197     }
198
199     while (http->hasActiveRequests()) {
200         http->update();
201     }
202
203     return EXIT_SUCCESS;
204 }