/
ContributionListing.java
551 lines (445 loc) · 16.6 KB
/
ContributionListing.java
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013 The Processing Foundation
Copyright (c) 2011-12 Ben Fry and Casey Reas
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.contrib;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import processing.app.Base;
import processing.app.Library;
import processing.core.PApplet;
public class ContributionListing {
// Stable URL that will redirect to wherever we're hosting the file
static final String LISTING_URL =
"http://download.processing.org/contribs.txt";
static volatile ContributionListing singleInstance;
File listingFile;
ArrayList<ContributionChangeListener> listeners;
ArrayList<AvailableContribution> advertisedContributions;
Map<String, List<Contribution>> librariesByCategory;
ArrayList<Contribution> allContributions;
boolean hasDownloadedLatestList;
boolean hasListDownloadFailed;
ReentrantLock downloadingListingLock;
private ContributionListing() {
listeners = new ArrayList<ContributionChangeListener>();
advertisedContributions = new ArrayList<AvailableContribution>();
librariesByCategory = new HashMap<String, List<Contribution>>();
allContributions = new ArrayList<Contribution>();
downloadingListingLock = new ReentrantLock();
listingFile = Base.getSettingsFile("contributions.txt");
listingFile.setWritable(true);
if (listingFile.exists()) {
setAdvertisedList(listingFile);
}
}
static ContributionListing getInstance() {
if (singleInstance == null) {
synchronized (ContributionListing.class) {
if (singleInstance == null) {
singleInstance = new ContributionListing();
}
}
}
return singleInstance;
}
void setAdvertisedList(File file) {
listingFile = file;
advertisedContributions.clear();
advertisedContributions.addAll(parseContribList(listingFile));
for (Contribution contribution : advertisedContributions) {
addContribution(contribution);
}
Collections.sort(allContributions, nameComparator);
}
/**
* Adds the installed libraries to the listing of libraries, replacing any
* pre-existing libraries by the same name as one in the list.
*/
protected void updateInstalledList(List<Contribution> installedContributions) {
for (Contribution contribution : installedContributions) {
Contribution existingContribution = getContribution(contribution);
if (existingContribution != null) {
replaceContribution(existingContribution, contribution);
//} else if (contribution != null) { // 130925 why would this be necessary?
} else {
addContribution(contribution);
}
}
}
protected void replaceContribution(Contribution oldLib, Contribution newLib) {
if (oldLib != null && newLib != null) {
for (String category : oldLib.getCategories()) {
if (librariesByCategory.containsKey(category)) {
List<Contribution> list = librariesByCategory.get(category);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == oldLib) {
list.set(i, newLib);
}
}
}
}
for (int i = 0; i < allContributions.size(); i++) {
if (allContributions.get(i) == oldLib) {
allContributions.set(i, newLib);
}
}
notifyChange(oldLib, newLib);
}
}
private void addContribution(Contribution contribution) {
for (String category : contribution.getCategories()) {
if (librariesByCategory.containsKey(category)) {
List<Contribution> list = librariesByCategory.get(category);
list.add(contribution);
Collections.sort(list, nameComparator);
} else {
ArrayList<Contribution> list = new ArrayList<Contribution>();
list.add(contribution);
librariesByCategory.put(category, list);
}
allContributions.add(contribution);
notifyAdd(contribution);
Collections.sort(allContributions, nameComparator);
}
}
protected void removeContribution(Contribution contribution) {
for (String category : contribution.getCategories()) {
if (librariesByCategory.containsKey(category)) {
librariesByCategory.get(category).remove(contribution);
}
}
allContributions.remove(contribution);
notifyRemove(contribution);
}
private Contribution getContribution(Contribution contribution) {
for (Contribution c : allContributions) {
if (c.getName().equals(contribution.getName()) &&
c.getType() == contribution.getType()) {
return c;
}
}
return null;
}
protected AvailableContribution getAvailableContribution(Contribution info) {
Iterator<AvailableContribution> iter = advertisedContributions.iterator();
while(iter.hasNext()) {
AvailableContribution advertised = iter.next();
if (advertised.getType() == info.getType() &&
advertised.getName().equals(info.getName())) {
return advertised;
}
}
return null;
}
protected Set<String> getCategories(ContributionFilter filter) {
Set<String> outgoing = new HashSet<String>();
Set<String> categorySet = librariesByCategory.keySet();
for (String categoryName : categorySet) {
for (Contribution contrib : librariesByCategory.get(categoryName)) {
if (filter.matches(contrib)) {
// TODO still not sure why category would be coming back null [fry]
// http://code.google.com/p/processing/issues/detail?id=1387
if (categoryName != null && categoryName.trim().length() != 0) {
outgoing.add(categoryName);
}
break;
}
}
}
return outgoing;
}
// public List<Contribution> getAllContributions() {
// return new ArrayList<Contribution>(allContributions);
// }
// public List<Contribution> getLibararies(String category) {
// ArrayList<Contribution> libinfos =
// new ArrayList<Contribution>(librariesByCategory.get(category));
// Collections.sort(libinfos, nameComparator);
// return libinfos;
// }
protected List<Contribution> getFilteredLibraryList(String category, List<String> filters) {
ArrayList<Contribution> filteredList =
new ArrayList<Contribution>(allContributions);
Iterator<Contribution> it = filteredList.iterator();
while (it.hasNext()) {
Contribution libInfo = it.next();
//if (category != null && !category.equals(libInfo.getCategory())) {
if (category != null && !libInfo.hasCategory(category)) {
it.remove();
} else {
for (String filter : filters) {
if (!matches(libInfo, filter)) {
it.remove();
break;
}
}
}
}
return filteredList;
}
private boolean matches(Contribution contrib, String filter) {
int colon = filter.indexOf(":");
if (colon != -1) {
String isText = filter.substring(0, colon);
String property = filter.substring(colon + 1);
// Chances are the person is still typing the property, so rather than
// make the list flash empty (because nothing contains "is:" or "has:",
// just return true.
if (!isProperty(property)) {
return true;
}
if ("is".equals(isText) || "has".equals(isText)) {
return hasProperty(contrib, filter.substring(colon + 1));
} else if ("not".equals(isText)) {
return !hasProperty(contrib, filter.substring(colon + 1));
}
}
filter = ".*" + filter.toLowerCase() + ".*";
return contrib.getAuthorList() != null && contrib.getAuthorList().toLowerCase().matches(filter)
|| contrib.getSentence() != null && contrib.getSentence().toLowerCase().matches(filter)
|| contrib.getParagraph() != null && contrib.getParagraph().toLowerCase().matches(filter)
|| contrib.hasCategory(filter)
|| contrib.getName() != null && contrib.getName().toLowerCase().matches(filter);
}
private boolean isProperty(String property) {
return property.startsWith("updat") || property.startsWith("upgrad")
|| property.startsWith("instal") && !property.startsWith("installabl")
|| property.equals("tool") || property.startsWith("lib")
|| property.equals("mode") || property.equals("compilation");
}
/**
* Returns true if the contribution fits the given property, false otherwise.
* If the property is invalid, returns false.
*/
private boolean hasProperty(Contribution contrib, String property) {
// update, updates, updatable, upgrade
if (property.startsWith("updat") || property.startsWith("upgrad")) {
return hasUpdates(contrib);
}
if (property.startsWith("instal") && !property.startsWith("installabl")) {
return contrib.isInstalled();
}
if (property.equals("tool")) {
return contrib.getType() == ContributionType.TOOL;
}
if (property.startsWith("lib")) {
return contrib.getType() == ContributionType.LIBRARY;
// return contrib.getType() == Contribution.Type.LIBRARY
// || contrib.getType() == Contribution.Type.LIBRARY_COMPILATION;
}
if (property.equals("mode")) {
return contrib.getType() == ContributionType.MODE;
}
// if (property.equals("compilation")) {
// return contrib.getType() == Contribution.Type.LIBRARY_COMPILATION;
// }
return false;
}
protected List<Contribution> getCompatibleContributionList(List<Contribution> filteredLibraries, boolean filter) {
ArrayList<Contribution> filteredList =
new ArrayList<Contribution>(filteredLibraries);
if (!filter)
return filteredList;
Iterator<Contribution> it = filteredList.iterator();
while (it.hasNext()) {
Contribution libInfo = it.next();
if (!libInfo.isCompatible(Base.getRevision())) {
it.remove();
}
}
return filteredList;
}
private void notifyRemove(Contribution contribution) {
for (ContributionChangeListener listener : listeners) {
listener.contributionRemoved(contribution);
}
}
private void notifyAdd(Contribution contribution) {
for (ContributionChangeListener listener : listeners) {
listener.contributionAdded(contribution);
}
}
private void notifyChange(Contribution oldLib, Contribution newLib) {
for (ContributionChangeListener listener : listeners) {
listener.contributionChanged(oldLib, newLib);
}
}
protected void addContributionListener(ContributionChangeListener listener) {
for (Contribution contrib : allContributions) {
listener.contributionAdded(contrib);
}
listeners.add(listener);
}
/*
private void removeContributionListener(ContributionChangeListener listener) {
listeners.remove(listener);
}
private ArrayList<ContributionChangeListener> getContributionListeners() {
return new ArrayList<ContributionChangeListener>(listeners);
}
*/
/**
* Starts a new thread to download the advertised list of contributions.
* Only one instance will run at a time.
*/
protected void downloadAvailableList(final ProgressMonitor progress) {
new Thread(new Runnable() {
public void run() {
downloadingListingLock.lock();
URL url = null;
try {
url = new URL(LISTING_URL);
} catch (MalformedURLException e) {
progress.error(e);
progress.finished();
}
if (!progress.isFinished()) {
ContributionManager.download(url, listingFile, progress);
if (!progress.isCanceled() && !progress.isError()) {
hasDownloadedLatestList = true;
hasListDownloadFailed = false;
setAdvertisedList(listingFile);
}
else
hasListDownloadFailed = true;
}
downloadingListingLock.unlock();
}
}, "Contribution List Downloader").start();
}
boolean hasUpdates() {
for (Contribution info : allContributions) {
if (hasUpdates(info)) {
return true;
}
}
return false;
}
boolean hasUpdates(Base base) {
for (ModeContribution m : base.getModeContribs())
if (hasUpdates(m))
return true;
for (Library l : base.getActiveEditor().getMode().contribLibraries)
if (hasUpdates(l))
return true;
for (ToolContribution t : base.getActiveEditor().contribTools)
if (hasUpdates(t))
return true;
return false;
}
boolean hasUpdates(Contribution contribution) {
if (contribution.isInstalled()) {
Contribution advertised = getAvailableContribution(contribution);
if (advertised == null) {
return false;
}
return advertised.getVersion() > contribution.getVersion();
}
return false;
}
String getLatestVersion(Contribution contribution) {
Contribution newestContrib = getAvailableContribution(contribution);
String latestVersion = newestContrib.getPrettyVersion();
if (latestVersion != null && !latestVersion.isEmpty()) {
if (latestVersion.toLowerCase().startsWith("build")) // For Python mode
return ("v" + latestVersion.substring(5, latestVersion.indexOf(','))
.trim());
else if (latestVersion.toLowerCase().startsWith("v")) // For ketai library
return latestVersion;
else
return ("v" + latestVersion);
}
else
return null;
}
boolean hasDownloadedLatestList() {
return hasDownloadedLatestList;
}
boolean hasListDownloadFailed() {
return hasListDownloadFailed;
}
// /**
// * @return a lowercase string with all non-alphabetic characters removed
// */
// static protected String normalize(String s) {
// return s.toLowerCase().replaceAll("^\\p{Lower}", "");
// }
// /**
// * @return the proper, valid name of this category to be displayed in the UI
// * (e.g. "Typography / Geometry"). "Unknown" if the category null.
// */
// static public String getCategory(String category) {
// if (category == null) {
// return "Unknown";
// }
// String normCatName = normalize(category);
//
// for (String validCatName : validCategories) {
// String normValidCatName = normalize(validCatName);
// if (normValidCatName.equals(normCatName)) {
// return validCatName;
// }
// }
// return category;
// }
ArrayList<AvailableContribution> parseContribList(File file) {
ArrayList<AvailableContribution> outgoing = new ArrayList<AvailableContribution>();
if (file != null && file.exists()) {
String[] lines = PApplet.loadStrings(file);
int start = 0;
while (start < lines.length) {
// // Only consider 'invalid' lines. These lines contain the type of
// // software: library, tool, mode
// if (!lines[start].contains("=")) {
String type = lines[start];
ContributionType contribType = ContributionType.fromName(type);
if (contribType == null) {
System.err.println("Error in contribution listing file on line " + (start+1));
return outgoing;
}
// Scan forward for the next blank line
int end = ++start;
while (end < lines.length && !lines[end].equals("")) {
end++;
}
int length = end - start;
String[] contribLines = new String[length];
System.arraycopy(lines, start, contribLines, 0, length);
HashMap<String,String> contribParams = new HashMap<String,String>();
Base.readSettings(file.getName(), contribLines, contribParams);
outgoing.add(new AvailableContribution(contribType, contribParams));
start = end + 1;
// } else {
// start++;
// }
}
}
return outgoing;
}
// boolean isDownloadingListing() {
// return downloadingListingLock.isLocked();
// }
public Comparator<? super Contribution> getComparator() {
return nameComparator;
}
static Comparator<Contribution> nameComparator = new Comparator<Contribution>() {
public int compare(Contribution o1, Contribution o2) {
return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase());
}
};
}