]> git.mxchange.org Git - simgear.git/blob - simgear/package/pkgutil.cxx
Tweak HTTP code to always sleep.
[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 refreshComplete()
38     {
39     }
40     
41     virtual void failedRefresh(pkg::Catalog* aCatalog, FailureCode aReason)
42     {
43         cerr << "failed refresh of " << aCatalog->description() << ":" << aReason << endl;
44     }
45     
46     virtual void startInstall(pkg::Install* aInstall)
47     {
48         _lastPercent = 999;
49         cout << "starting install of " << aInstall->package()->name() << endl;
50     }
51     
52     virtual void installProgress(pkg::Install* aInstall, unsigned int bytes, unsigned int total)
53     {
54         unsigned int percent = (bytes * 100) / total;
55         if (percent == _lastPercent) {
56             return;
57         }
58         
59         _lastPercent = percent;
60         cout << percent << "%" << endl;
61     }
62     
63     virtual void finishInstall(pkg::Install* aInstall)
64     {
65         cout << "done install of " << aInstall->package()->name() << endl;
66     }
67
68     virtual void failedInstall(pkg::Install* aInstall, FailureCode aReason)
69     {
70         cerr << "failed install of " << aInstall->package()->name() << endl;
71     }
72 private:
73     unsigned int _lastPercent;
74     
75 };
76
77 void printRating(pkg::Package* pkg, const std::string& aRating, const std::string& aLabel)
78 {
79     SGPropertyNode* ratings = pkg->properties()->getChild("rating");
80     cout << "\t" << aLabel << ":" << ratings->getIntValue(aRating) << endl;
81 }
82
83 void printPackageInfo(pkg::Package* pkg)
84 {
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;
90     
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;
96                 
97             } else {
98                 // simple author structure
99                 cout << "\t" << author->getStringValue() << endl;
100             }
101         
102             
103         }
104         
105         cout << endl;
106     }
107     
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");
113 }
114
115 int main(int argc, char** argv)
116 {
117
118     HTTP::Client* http = new HTTP::Client();
119     pkg::Root* root = new pkg::Root(Dir::current().path(), "");
120     
121     MyDelegate dlg;
122     root->setDelegate(&dlg);
123     
124     cout << "Package root is:" << Dir::current().path() << endl;
125     cout << "have " << pkg::Catalog::allCatalogs().size() << " catalog(s)" << endl;
126         
127     root->setHTTPClient(http);
128     
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")) {
133         root->refresh(true);
134     } else if (!strcmp(argv[1], "install")) {
135         pkg::Package* pkg = root->getPackageById(argv[2]);
136         if (!pkg) {
137             cerr << "unknown package:" << argv[2] << endl;
138             return EXIT_FAILURE;
139         }
140         
141         if (pkg->isInstalled()) {
142             cout << "package " << pkg->id() << " is already installed at " << pkg->install()->path() << endl;
143             return EXIT_SUCCESS;
144         }
145         
146         pkg::Catalog* catalog = pkg->catalog();
147         cout << "Will install:" << pkg->id() << " from " << catalog->id() <<
148                 "(" << catalog->description() << ")" << endl;
149         pkg->install();
150     } else if (!strcmp(argv[1], "uninstall") || !strcmp(argv[1], "remove")) {
151         pkg::Package* pkg = root->getPackageById(argv[2]);
152         if (!pkg) {
153             cerr << "unknown package:" << argv[2] << endl;
154             return EXIT_FAILURE;
155         }
156         
157         if (!pkg->isInstalled()) {
158             cerr << "package " << argv[2] << " not installed" << endl;
159             return EXIT_FAILURE;
160         }
161         
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());
168         }
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;
173             return EXIT_SUCCESS;
174         }
175         
176         cout << updates.size() << " packages have updates" << endl;
177         BOOST_FOREACH(pkg::Package* p, updates) {
178             cout << "\t" << p->id() << " " << p->getLocalisedProp("name") << endl;
179         }
180     } else if (!strcmp(argv[1], "info")) {
181         pkg::Package* pkg = root->getPackageById(argv[2]);
182         if (!pkg) {
183             cerr << "unknown package:" << argv[2] << endl;
184             return EXIT_FAILURE;
185         }
186     
187         printPackageInfo(pkg);
188     } else {
189         cerr << "unknown command:" << argv[1] << endl;
190         return EXIT_FAILURE;
191     }
192     
193     while (http->hasActiveRequests()) {
194         http->update();
195     }
196
197     return EXIT_SUCCESS;
198 }