1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.plugin;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.Comparator;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Locale;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.Map.Entry;
|
---|
15 | import java.util.Set;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.gui.util.ChangeNotifier;
|
---|
18 | import org.openstreetmap.josm.plugins.PluginException;
|
---|
19 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
20 | import org.openstreetmap.josm.plugins.PluginInformation;
|
---|
21 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
22 | import org.openstreetmap.josm.tools.Logging;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * The plugin model behind a {@code PluginListPanel}.
|
---|
26 | */
|
---|
27 | public class PluginPreferencesModel extends ChangeNotifier {
|
---|
28 | // remember the initial list of active plugins
|
---|
29 | private final Set<String> currentActivePlugins;
|
---|
30 | private final List<PluginInformation> availablePlugins = new ArrayList<>();
|
---|
31 | private PluginInstallation filterStatus;
|
---|
32 | private String filterExpression;
|
---|
33 | private final List<PluginInformation> displayedPlugins = new ArrayList<>();
|
---|
34 | private final Map<PluginInformation, Boolean> selectedPluginsMap = new HashMap<>();
|
---|
35 | // plugins that still require an update/download
|
---|
36 | private final Set<String> pendingDownloads = new HashSet<>();
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Constructs a new {@code PluginPreferencesModel}.
|
---|
40 | */
|
---|
41 | public PluginPreferencesModel() {
|
---|
42 | currentActivePlugins = new HashSet<>();
|
---|
43 | currentActivePlugins.addAll(Config.getPref().getList("plugins"));
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Filters the list of displayed plugins by installation status.
|
---|
48 | * @param status The filter used against installation status
|
---|
49 | * @since 13799
|
---|
50 | */
|
---|
51 | public void filterDisplayedPlugins(PluginInstallation status) {
|
---|
52 | if (status == null) {
|
---|
53 | displayedPlugins.clear();
|
---|
54 | displayedPlugins.addAll(availablePlugins);
|
---|
55 | this.filterStatus = null;
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | displayedPlugins.clear();
|
---|
59 | for (PluginInformation pi: availablePlugins) {
|
---|
60 | boolean installed = currentActivePlugins.contains(pi.getName());
|
---|
61 | if (PluginInstallation.ALL == status
|
---|
62 | || (PluginInstallation.INSTALLED == status && installed)
|
---|
63 | || (PluginInstallation.AVAILABLE == status && !installed)) {
|
---|
64 | displayedPlugins.add(pi);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | filterStatus = status;
|
---|
68 | fireStateChanged();
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Filters the list of displayed plugins by text.
|
---|
73 | * @param filter The filter used against plugin name, description or version
|
---|
74 | */
|
---|
75 | public void filterDisplayedPlugins(String filter) {
|
---|
76 | if (filter == null) {
|
---|
77 | displayedPlugins.clear();
|
---|
78 | displayedPlugins.addAll(availablePlugins);
|
---|
79 | this.filterExpression = null;
|
---|
80 | return;
|
---|
81 | }
|
---|
82 | displayedPlugins.clear();
|
---|
83 | for (PluginInformation pi: availablePlugins) {
|
---|
84 | if (pi.matches(filter)) {
|
---|
85 | displayedPlugins.add(pi);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | filterExpression = filter;
|
---|
89 | fireStateChanged();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Sets the list of available plugins.
|
---|
94 | * @param available The available plugins
|
---|
95 | */
|
---|
96 | public void setAvailablePlugins(Collection<PluginInformation> available) {
|
---|
97 | availablePlugins.clear();
|
---|
98 | if (available != null) {
|
---|
99 | availablePlugins.addAll(available);
|
---|
100 | }
|
---|
101 | availablePluginsModified();
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected final void availablePluginsModified() {
|
---|
105 | sort();
|
---|
106 | filterDisplayedPlugins(filterStatus);
|
---|
107 | filterDisplayedPlugins(filterExpression);
|
---|
108 | Set<String> activePlugins = new HashSet<>();
|
---|
109 | activePlugins.addAll(Config.getPref().getList("plugins"));
|
---|
110 | for (PluginInformation pi: availablePlugins) {
|
---|
111 | if (selectedPluginsMap.get(pi) == null && activePlugins.contains(pi.name)) {
|
---|
112 | selectedPluginsMap.put(pi, Boolean.TRUE);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | fireStateChanged();
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected void updateAvailablePlugin(PluginInformation other) {
|
---|
119 | if (other != null) {
|
---|
120 | PluginInformation pi = getPluginInformation(other.name);
|
---|
121 | if (pi == null) {
|
---|
122 | availablePlugins.add(other);
|
---|
123 | return;
|
---|
124 | }
|
---|
125 | pi.updateFromPluginSite(other);
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Updates the list of plugin information objects with new information from
|
---|
131 | * plugin update sites.
|
---|
132 | *
|
---|
133 | * @param fromPluginSite plugin information read from plugin update sites
|
---|
134 | */
|
---|
135 | public void updateAvailablePlugins(Collection<PluginInformation> fromPluginSite) {
|
---|
136 | for (PluginInformation other: fromPluginSite) {
|
---|
137 | updateAvailablePlugin(other);
|
---|
138 | }
|
---|
139 | availablePluginsModified();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Replies the list of selected plugin information objects
|
---|
144 | *
|
---|
145 | * @return the list of selected plugin information objects
|
---|
146 | */
|
---|
147 | public List<PluginInformation> getSelectedPlugins() {
|
---|
148 | List<PluginInformation> ret = new LinkedList<>();
|
---|
149 | for (PluginInformation pi: availablePlugins) {
|
---|
150 | if (selectedPluginsMap.get(pi) == null) {
|
---|
151 | continue;
|
---|
152 | }
|
---|
153 | if (selectedPluginsMap.get(pi)) {
|
---|
154 | ret.add(pi);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | return ret;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Replies the list of selected plugin information objects
|
---|
162 | *
|
---|
163 | * @return the list of selected plugin information objects
|
---|
164 | */
|
---|
165 | public Set<String> getSelectedPluginNames() {
|
---|
166 | Set<String> ret = new HashSet<>();
|
---|
167 | for (PluginInformation pi: getSelectedPlugins()) {
|
---|
168 | ret.add(pi.name);
|
---|
169 | }
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Sorts the list of available plugins
|
---|
175 | */
|
---|
176 | protected void sort() {
|
---|
177 | availablePlugins.sort(Comparator.comparing(
|
---|
178 | o -> o.getName() == null ? "" : o.getName().toLowerCase(Locale.ENGLISH)));
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Replies the list of plugin informations to display.
|
---|
183 | *
|
---|
184 | * @return the list of plugin informations to display
|
---|
185 | */
|
---|
186 | public List<PluginInformation> getDisplayedPlugins() {
|
---|
187 | return displayedPlugins;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Replies the set of plugins waiting for update or download.
|
---|
192 | *
|
---|
193 | * @return the set of plugins waiting for update or download
|
---|
194 | */
|
---|
195 | public Set<PluginInformation> getPluginsScheduledForUpdateOrDownload() {
|
---|
196 | Set<PluginInformation> ret = new HashSet<>();
|
---|
197 | for (String plugin: pendingDownloads) {
|
---|
198 | PluginInformation pi = getPluginInformation(plugin);
|
---|
199 | if (pi == null) {
|
---|
200 | continue;
|
---|
201 | }
|
---|
202 | ret.add(pi);
|
---|
203 | }
|
---|
204 | return ret;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Sets whether the plugin is selected or not.
|
---|
209 | *
|
---|
210 | * @param name the name of the plugin
|
---|
211 | * @param selected true, if selected; false, otherwise
|
---|
212 | */
|
---|
213 | public void setPluginSelected(String name, boolean selected) {
|
---|
214 | PluginInformation pi = getPluginInformation(name);
|
---|
215 | if (pi != null) {
|
---|
216 | selectedPluginsMap.put(pi, selected);
|
---|
217 | if (pi.isUpdateRequired()) {
|
---|
218 | pendingDownloads.add(pi.name);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | if (!selected) {
|
---|
222 | pendingDownloads.remove(name);
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Removes all the plugin in {@code plugins} from the list of plugins
|
---|
228 | * with a pending download
|
---|
229 | *
|
---|
230 | * @param plugins the list of plugins to clear for a pending download
|
---|
231 | */
|
---|
232 | public void clearPendingPlugins(Collection<PluginInformation> plugins) {
|
---|
233 | if (plugins != null) {
|
---|
234 | for (PluginInformation pi: plugins) {
|
---|
235 | pendingDownloads.remove(pi.name);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Replies the plugin info with the name <code>name</code>. null, if no
|
---|
242 | * such plugin info exists.
|
---|
243 | *
|
---|
244 | * @param name the name. If null, replies null.
|
---|
245 | * @return the plugin info.
|
---|
246 | */
|
---|
247 | public PluginInformation getPluginInformation(String name) {
|
---|
248 | for (PluginInformation pi: availablePlugins) {
|
---|
249 | if (pi.getName() != null && pi.getName().equals(name))
|
---|
250 | return pi;
|
---|
251 | }
|
---|
252 | return null;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Initializes the model from preferences
|
---|
257 | */
|
---|
258 | public void initFromPreferences() {
|
---|
259 | Collection<String> enabledPlugins = Config.getPref().getList("plugins", null);
|
---|
260 | if (enabledPlugins == null) {
|
---|
261 | this.selectedPluginsMap.clear();
|
---|
262 | return;
|
---|
263 | }
|
---|
264 | for (String name: enabledPlugins) {
|
---|
265 | PluginInformation pi = getPluginInformation(name);
|
---|
266 | if (pi == null) {
|
---|
267 | continue;
|
---|
268 | }
|
---|
269 | setPluginSelected(name, true);
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Replies true if the plugin with name <code>name</code> is currently
|
---|
275 | * selected in the plugin model
|
---|
276 | *
|
---|
277 | * @param name the plugin name
|
---|
278 | * @return true if the plugin is selected; false, otherwise
|
---|
279 | */
|
---|
280 | public boolean isSelectedPlugin(String name) {
|
---|
281 | PluginInformation pi = getPluginInformation(name);
|
---|
282 | if (pi == null || selectedPluginsMap.get(pi) == null)
|
---|
283 | return false;
|
---|
284 | return selectedPluginsMap.get(pi);
|
---|
285 | }
|
---|
286 |
|
---|
287 | /**
|
---|
288 | * Replies the set of plugins which have been added by the user to
|
---|
289 | * the set of activated plugins.
|
---|
290 | *
|
---|
291 | * @return the set of newly activated plugins
|
---|
292 | */
|
---|
293 | public List<PluginInformation> getNewlyActivatedPlugins() {
|
---|
294 | List<PluginInformation> ret = new LinkedList<>();
|
---|
295 | for (Entry<PluginInformation, Boolean> entry: selectedPluginsMap.entrySet()) {
|
---|
296 | PluginInformation pi = entry.getKey();
|
---|
297 | boolean selected = entry.getValue();
|
---|
298 | if (selected && !currentActivePlugins.contains(pi.name)) {
|
---|
299 | ret.add(pi);
|
---|
300 | }
|
---|
301 | }
|
---|
302 | return ret;
|
---|
303 | }
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Replies the set of plugins which have been removed by the user from
|
---|
307 | * the set of deactivated plugins.
|
---|
308 | *
|
---|
309 | * @return the set of newly deactivated plugins
|
---|
310 | */
|
---|
311 | public List<PluginInformation> getNewlyDeactivatedPlugins() {
|
---|
312 | List<PluginInformation> ret = new LinkedList<>();
|
---|
313 | for (PluginInformation pi: availablePlugins) {
|
---|
314 | if (!currentActivePlugins.contains(pi.name)) {
|
---|
315 | continue;
|
---|
316 | }
|
---|
317 | if (selectedPluginsMap.get(pi) == null || !selectedPluginsMap.get(pi)) {
|
---|
318 | ret.add(pi);
|
---|
319 | }
|
---|
320 | }
|
---|
321 | return ret;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | * Replies the set of all available plugins.
|
---|
326 | *
|
---|
327 | * @return the set of all available plugins
|
---|
328 | */
|
---|
329 | public List<PluginInformation> getAvailablePlugins() {
|
---|
330 | return new LinkedList<>(availablePlugins);
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Replies the set of plugin names which have been added by the user to
|
---|
335 | * the set of activated plugins.
|
---|
336 | *
|
---|
337 | * @return the set of newly activated plugin names
|
---|
338 | */
|
---|
339 | public Set<String> getNewlyActivatedPluginNames() {
|
---|
340 | Set<String> ret = new HashSet<>();
|
---|
341 | List<PluginInformation> plugins = getNewlyActivatedPlugins();
|
---|
342 | for (PluginInformation pi: plugins) {
|
---|
343 | ret.add(pi.name);
|
---|
344 | }
|
---|
345 | return ret;
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Replies true if the set of active plugins has been changed by the user
|
---|
350 | * in this preference model. He has either added plugins or removed plugins
|
---|
351 | * being active before.
|
---|
352 | *
|
---|
353 | * @return true if the collection of active plugins has changed
|
---|
354 | */
|
---|
355 | public boolean isActivePluginsChanged() {
|
---|
356 | Set<String> newActivePlugins = getSelectedPluginNames();
|
---|
357 | return !newActivePlugins.equals(currentActivePlugins);
|
---|
358 | }
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Refreshes the local version field on the plugins in <code>plugins</code> with
|
---|
362 | * the version in the manifest of the downloaded "jar.new"-file for this plugin.
|
---|
363 | *
|
---|
364 | * @param plugins the collections of plugins to refresh
|
---|
365 | */
|
---|
366 | public void refreshLocalPluginVersion(Collection<PluginInformation> plugins) {
|
---|
367 | if (plugins != null) {
|
---|
368 | for (PluginInformation pi : plugins) {
|
---|
369 | File downloadedPluginFile = PluginHandler.findUpdatedJar(pi.name);
|
---|
370 | if (downloadedPluginFile == null) {
|
---|
371 | continue;
|
---|
372 | }
|
---|
373 | try {
|
---|
374 | PluginInformation newinfo = new PluginInformation(downloadedPluginFile, pi.name);
|
---|
375 | PluginInformation oldinfo = getPluginInformation(pi.name);
|
---|
376 | if (oldinfo != null) {
|
---|
377 | oldinfo.updateLocalInfo(newinfo);
|
---|
378 | }
|
---|
379 | } catch (PluginException e) {
|
---|
380 | Logging.error(e);
|
---|
381 | }
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|