-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppList.cpp
334 lines (282 loc) · 10.5 KB
/
AppList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/////////////////////////////////////////////////////////////////////////////
// Name: AppList.cpp
// Purpose: Maintains the list of applications
// Author: Dave Page
// Created: 2007-02-13
// RCS-ID: $Id: AppList.cpp,v 1.24 2010/06/03 10:45:11 sachin Exp $
// Copyright: (c) EnterpriseDB
// Licence: BSD Licence
/////////////////////////////////////////////////////////////////////////////
#include "StackBuilder.h"
// wxWindows headers
#include <wx/wx.h>
#include <wx/arrimpl.cpp>
#include <wx/treectrl.h>
#include <wx/url.h>
#include <wx/xml/xml.h>
#include <wx/tokenzr.h>
// Application headers
#include "App.h"
#include "AppList.h"
#include "Mirror.h"
#include "ProxyDialog.h"
#include "CurlHandler.h"
class Server;
// Define the AppArray type
WX_DEFINE_OBJARRAY(AppArray);
bool AppList::LoadAppList()
{
wxString readBuffer = wxEmptyString;
// Fetch the application list using libcurl
CurlHandler curlObj;
curlObj.SetApplicationUrl(m_applicationListUrl);
bool bIsSuccess = curlObj.ParseApplicationList();
if (!bIsSuccess)
return false;
readBuffer = curlObj.GetApplicationList();
wxStringInputStream ip(readBuffer);
wxXmlDocument xml;
bool xmlLoaded = false;
{
wxLogNull noLog;
xmlLoaded = xml.Load(ip);
}
if (!xmlLoaded)
{
wxLogError(wxString::Format(_("Failed to load application list: %s"), m_applicationListUrl.c_str()));
return false;
}
// Clear the applist
m_apps.Clear();
// Iterate through the applications and build the list
wxXmlNode *application, *properties;
application = xml.GetRoot()->GetChildren();
while (application)
{
if (application->GetName() == wxT("application"))
{
App *newApplication = new App(this, m_server);
properties = application->GetChildren();
while (properties)
{
if (properties->GetName() == wxT("id"))
newApplication->id = properties->GetNodeContent();
else if (properties->GetName() == wxT("platform"))
newApplication->platform = DeHTMLise(properties->GetNodeContent());
else if (properties->GetName() == wxT("secondaryplatform"))
newApplication->secondaryplatform = DeHTMLise(properties->GetNodeContent());
else if (properties->GetName() == wxT("name"))
newApplication->name = DeHTMLise(properties->GetNodeContent());
else if (properties->GetName() == wxT("description"))
newApplication->description = DeHTMLise(properties->GetNodeContent());
else if (properties->GetName() == wxT("version"))
newApplication->version = properties->GetNodeContent();
else if (properties->GetName() == wxT("category"))
newApplication->category = DeHTMLise(properties->GetNodeContent());
else if (properties->GetName() == wxT("pgversion"))
newApplication->pgversion = properties->GetNodeContent();
else if (properties->GetName() == wxT("edbversion"))
newApplication->edbversion = properties->GetNodeContent();
else if (properties->GetName() == wxT("format"))
newApplication->format = properties->GetNodeContent();
else if (properties->GetName() == wxT("installoptions"))
newApplication->installoptions = DeHTMLise(properties->GetNodeContent());
else if (properties->GetName() == wxT("upgradeoptions"))
newApplication->upgradeoptions = DeHTMLise(properties->GetNodeContent());
else if (properties->GetName() == wxT("checksum"))
newApplication->checksum = properties->GetNodeContent();
else if (properties->GetName() == wxT("mirrorpath"))
newApplication->mirrorpath = properties->GetNodeContent();
else if (properties->GetName() == wxT("dependency"))
newApplication->dependencies.Add(DeHTMLise(properties->GetNodeContent()));
else if (properties->GetName() == wxT("versionkey"))
newApplication->versionkey = properties->GetNodeContent();
else if (properties->GetName() == wxT("alturl"))
newApplication->alturl = properties->GetNodeContent();
properties = properties->GetNext();
}
// Cleanup the mirrorpath
if (newApplication->mirrorpath.StartsWith(wxT("/")))
newApplication->mirrorpath = newApplication->mirrorpath.Right(newApplication->mirrorpath.Length() -1);
if (newApplication->IsValid() && newApplication->WorksWithDB() && newApplication->WorksWithPlatform())
m_apps.Add(newApplication);
}
application = application->GetNext();
}
return true;
}
wxString AppList::DeHTMLise(const wxString &string)
{
wxString ret = string;
ret.Replace(wxT("""), wxT("\""));
ret.Replace(wxT("<"), wxT("<"));
ret.Replace(wxT(">"), wxT(">"));
ret.Replace(wxT("“"), wxT("\""));
ret.Replace(wxT("”"), wxT("\""));
ret.Replace(wxT("'"), wxT("'"));
return ret;
}
wxTreeItemId AppList::FindCategory(wxTreeItemId parent, wxString &category)
{
wxTreeItemIdValue cookie;
wxTreeItemId node;
node = m_treectrl->GetFirstChild(parent, cookie);
while (node)
{
if (m_treectrl->GetItemText(node) == category)
return node;
node = m_treectrl->GetNextChild(parent, cookie);
}
return node;
}
bool AppList::PopulateTreeCtrl()
{
wxTreeItemId node, root, category, application, parentNode;
root = m_treectrl->AddRoot(_("Categories"), 3);
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
parentNode = root;
wxStringTokenizer tokens(m_apps[i].category, wxT("\\"));
while (tokens.HasMoreTokens())
{
wxString ctg = tokens.GetNextToken();
node = FindCategory(parentNode, ctg);
if (!node)
node = m_treectrl->AppendItem(parentNode, ctg, 3);
parentNode = node;
}
category = node;
// We used to disable installed apps here (by using image #2), but that
// may not be the best strategy as it prevents us from reinstalling apps.
if (m_apps[i].IsVersionInstalled())
application = m_treectrl->AppendItem(category, wxString::Format(_("%s v%s (installed)"), m_apps[i].name.c_str(), m_apps[i].version.c_str()), 0, -1, &m_apps[i]);
else if (m_apps[i].IsInstalled())
application = m_treectrl->AppendItem(category, wxString::Format(_("%s v%s (v%s installed)"), m_apps[i].name.c_str(), m_apps[i].version.c_str(), m_apps[i].GetInstalledVersion().c_str()), 0, -1, &m_apps[i]);
else
application = m_treectrl->AppendItem(category, wxString::Format(_("%s v%s"), m_apps[i].name.c_str(), m_apps[i].version.c_str()), 0, -1, &m_apps[i]);
m_apps[i].SelectForDownload(false, false);
m_apps[i].m_tree = m_treectrl;
m_apps[i].m_treeitem = application;
m_treectrl->SortChildren(category);
}
m_treectrl->SortChildren(root);
m_treectrl->Expand(root);
return true;
}
// Check to see if any downloads have been selected.
bool AppList::HaveDownloads()
{
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
if (m_apps[i].IsSelectedForDownload() == true)
return true;
}
return false;
}
// Check to see if any downloads are coming from mirrors.
bool AppList::UsingMirrors()
{
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
if (m_apps[i].IsSelectedForDownload() == true && m_apps[i].alturl == wxEmptyString)
return true;
}
return false;
}
// Figure out what order to download and install
void AppList::RankDownloads()
{
int rank = 1;
// Clear any existing rankings
for (unsigned int i=0; i<m_apps.GetCount(); i++)
m_apps[i].sequence = 0;
// For each app in the list, if it's selected for download,
// scan to the bottom of the dependency tree and rank the
// downloads from the bottom level back up. If a package has
// already been ranked, we can ignore it as we must have
// ranked it and it's dependencies already.
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
// If the app isn't selected for download, or has already
// been ranked, ignore it.
if (!m_apps[i].IsSelectedForDownload() || m_apps[i].sequence > 0)
continue;
rank = m_apps[i].RankDependencies(rank, 0);
}
}
// Clear the list, but unlink the tree items first.
void AppList::DeleteAllItems()
{
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
m_treectrl->SetItemData(m_apps[i].m_treeitem, NULL);
m_apps[i].m_treeitem = 0;
}
m_apps.Clear();
}
App *AppList::GetItem(const wxString &appid)
{
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
if (m_apps[i].id == appid)
return &m_apps[i];
}
return NULL;
}
bool AppList::DownloadFiles(const wxString& downloadPath, const Mirror *mirror)
{
unsigned int x = 1;
// Loop round once for every app. For each loop, search the
// applist for a matching download.
while(x <= m_apps.GetCount())
{
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
if (m_apps[i].sequence == x)
{
if (!m_apps[i].Download(downloadPath, mirror))
return false;
}
}
x++;
}
return true;
}
bool AppList::InstallApps()
{
unsigned int x = 1;
// Loop round once for every app. For each loop, search the
// applist for a matching download.
while(x <= m_apps.GetCount())
{
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
if (m_apps[i].sequence == x)
{
if (!m_apps[i].Install())
return false;
}
}
x++;
}
return true;
}
wxArrayString AppList::GetSummary()
{
wxArrayString summary;
unsigned int x = 1;
// Loop round once for every app. For each loop, search the
// applist for a matching download.
while(x <= m_apps.GetCount())
{
for (unsigned int i=0; i<m_apps.GetCount(); i++)
{
if (m_apps[i].sequence == x)
{
summary.Add(m_apps[i].name + wxT(" v") + m_apps[i].version);
}
}
x++;
}
return summary;
}