summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/ide/plugins/InstalledPluginsTableModel.java
blob: d05622504565d6a8001b1c790bee7cce191a0499 (plain)
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
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ide.plugins;

import com.intellij.ide.IdeBundle;
import com.intellij.openapi.application.ex.ApplicationInfoEx;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Pair;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.nio.file.FileVisitResult;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

public class InstalledPluginsTableModel {

  protected static final boolean HIDE_IMPLEMENTATION_DETAILS = !Boolean.getBoolean("startup.performance.framework");

  protected final List<IdeaPluginDescriptor> view = new ArrayList<>();
  private final Map<PluginId, PluginEnabledState> myEnabled = new HashMap<>();
  private final @Nullable Project myProject;

  public InstalledPluginsTableModel(@Nullable Project project) {
    myProject = project;

    ApplicationInfoEx appInfo = ApplicationInfoEx.getInstanceEx();
    for (IdeaPluginDescriptor plugin : PluginManagerCore.getPlugins()) {
      PluginId pluginId = plugin.getPluginId();
      if (appInfo.isEssentialPlugin(pluginId)) {
        setEnabled(pluginId, PluginEnabledState.ENABLED);
      }
      else {
        view.add(plugin);
      }
    }
    view.addAll(InstalledPluginsState.getInstance().getInstalledPlugins());

    ProjectPluginTracker pluginTracker = myProject != null ? DynamicPluginEnabler.findPluginTracker(myProject) : null;
    for (IdeaPluginDescriptor descriptor : view) {
      setEnabled(descriptor, pluginTracker);
    }
  }

  protected final @Nullable Project getProject() {
    return myProject;
  }

  public final boolean isLoaded(@NotNull PluginId pluginId) {
    return isLoaded(pluginId, getEnabledMap());
  }

  private void setEnabled(@NotNull IdeaPluginDescriptor ideaPluginDescriptor,
                          @Nullable ProjectPluginTracker pluginTracker) {
    PluginId pluginId = ideaPluginDescriptor.getPluginId();

    PluginEnabledState enabled = pluginTracker != null && pluginTracker.isEnabled(pluginId) ?
                                 PluginEnabledState.ENABLED_FOR_PROJECT :
                                 pluginTracker != null && pluginTracker.isDisabled(pluginId) ?
                                 PluginEnabledState.DISABLED_FOR_PROJECT :
                                 PluginManagerCore.isDisabled(pluginId) ?
                                 PluginEnabledState.DISABLED :
                                 ideaPluginDescriptor.isEnabled() ?
                                 PluginEnabledState.ENABLED :
                                 null;

    setEnabled(pluginId, enabled);
  }

  protected final void setEnabled(@NotNull PluginId pluginId,
                                  @Nullable PluginEnabledState enabled) {
    myEnabled.put(pluginId, enabled);
  }

  protected void updatePluginDependencies(@Nullable Map<PluginId, IdeaPluginDescriptorImpl> pluginIdMap) {
  }

  protected final void enableRows(@NotNull Collection<? extends IdeaPluginDescriptor> descriptors,
                                  @NotNull PluginEnableDisableAction action) {
    Map<PluginId, PluginEnabledState> tempEnabled = new HashMap<>(myEnabled);

    setNewEnabled(descriptors,
                  tempEnabled,
                  action,
                  (descriptor, pair) -> {
                  });

    List<IdeaPluginDescriptorImpl> impls = descriptors.stream()
      .filter(IdeaPluginDescriptorImpl.class::isInstance)
      .map(IdeaPluginDescriptorImpl.class::cast)
      .collect(Collectors.toCollection(ArrayList::new));

    Map<PluginId, IdeaPluginDescriptorImpl> pluginIdMap = PluginManagerCore.buildPluginIdMap();
    List<IdeaPluginDescriptorImpl> descriptorsToUpdate = action.isEnable() ?
                                                         getDependenciesToEnable(impls, tempEnabled, pluginIdMap) :
                                                         getDependentsToDisable(impls, tempEnabled, pluginIdMap);

    Set<String> pluginNamesToUpdate = descriptorsToUpdate.stream()
      .filter(descriptor -> !isHiddenImplementationDetail(descriptor))
      .map(IdeaPluginDescriptorImpl::getName)
      .collect(Collectors.toCollection(TreeSet::new));
    if (HIDE_IMPLEMENTATION_DETAILS &&
        !createUpdateDependenciesDialog(pluginNamesToUpdate, action)) {
      return;
    }

    impls.addAll(descriptorsToUpdate);
    setNewEnabled(impls,
                  myEnabled,
                  action,
                  this::handleBeforeChangeEnableState);
    updatePluginDependencies(pluginIdMap);
  }

  private static void setNewEnabled(@NotNull Collection<? extends IdeaPluginDescriptor> descriptors,
                                    @NotNull Map<PluginId, PluginEnabledState> enabledMap,
                                    @NotNull PluginEnableDisableAction action,
                                    @NotNull BiConsumer<? super IdeaPluginDescriptor, @NotNull Pair<PluginEnableDisableAction, PluginEnabledState>> beforeHandler) {
    for (IdeaPluginDescriptor descriptor : descriptors) {
      PluginId pluginId = descriptor.getPluginId();
      PluginEnabledState oldState = enabledMap.get(pluginId);

      PluginEnabledState newState = oldState == null ?
                                    PluginEnabledState.DISABLED :
                                    action.apply(oldState);
      if (newState != null) {
        beforeHandler.accept(descriptor, Pair.create(action, newState));
        enabledMap.put(pluginId, newState);
      }
    }
  }

  protected final @NotNull Map<PluginId, PluginEnabledState> getEnabledMap() {
    return myEnabled;
  }

  private static @NotNull List<IdeaPluginDescriptorImpl> getDependenciesToEnable(@NotNull Collection<IdeaPluginDescriptorImpl> descriptors,
                                                                                 @NotNull Map<PluginId, PluginEnabledState> enabledMap,
                                                                                 @NotNull Map<PluginId, IdeaPluginDescriptorImpl> pluginIdMap) {
    ArrayList<IdeaPluginDescriptorImpl> result = new ArrayList<>();
    for (IdeaPluginDescriptorImpl descriptor : descriptors) {
      PluginManagerCore.processAllNonOptionalDependencies(descriptor, pluginIdMap, dependency -> {
        PluginId dependencyId = dependency.getPluginId();
        PluginEnabledState state = enabledMap.get(dependencyId);

        if (!dependencyId.equals(descriptor.getPluginId()) &&
            !(state != null && state.isEnabled())) {
          result.add(dependency);
        }
        return FileVisitResult.CONTINUE;
      });
    }

    return Collections.unmodifiableList(result);
  }

  private static @NotNull List<IdeaPluginDescriptorImpl> getDependentsToDisable(@NotNull Collection<IdeaPluginDescriptorImpl> descriptors,
                                                                                @NotNull Map<PluginId, PluginEnabledState> enabledMap,
                                                                                @NotNull Map<PluginId, IdeaPluginDescriptorImpl> pluginIdMap) {
    ArrayList<IdeaPluginDescriptorImpl> result = new ArrayList<>();
    Set<PluginId> pluginIds = descriptors.stream()
      .map(IdeaPluginDescriptorImpl::getPluginId)
      .collect(Collectors.toUnmodifiableSet());

    for (IdeaPluginDescriptorImpl descriptor : PluginManagerCore.getPluginSet().allPlugins) {
      PluginId pluginId = descriptor.getPluginId();
      if (pluginIds.contains(pluginId) ||
          isDisabled(pluginId, enabledMap)) {
        continue;
      }

      PluginManagerCore.processAllNonOptionalDependencies(descriptor, pluginIdMap, dependency -> {
        PluginId dependencyId = dependency.getPluginId();
        if (!isLoaded(dependencyId, enabledMap)) {
          return FileVisitResult.TERMINATE;
        }

        if (!dependencyId.equals(pluginId) &&
            pluginIds.contains(dependencyId)) {
          result.add(descriptor);
        }
        return FileVisitResult.CONTINUE;
      });
    }

    return Collections.unmodifiableList(result);
  }

  private boolean createUpdateDependenciesDialog(@NotNull Collection<String> dependencies,
                                                 @NotNull PluginEnableDisableAction action) {
    int size = dependencies.size();
    if (size == 0) {
      return true;
    }
    boolean hasOnlyOneDependency = size == 1;

    String key;
    switch (action) {
      case ENABLE_GLOBALLY:
        key = hasOnlyOneDependency ?
              "dialog.message.enable.required.plugin" :
              "dialog.message.enable.required.plugins";
        break;
      case ENABLE_FOR_PROJECT:
        key = hasOnlyOneDependency ?
              "dialog.message.enable.required.plugin.for.current.project" :
              "dialog.message.enable.required.plugins.for.current.project";
        break;
      case ENABLE_FOR_PROJECT_DISABLE_GLOBALLY:
        key = hasOnlyOneDependency ?
              "dialog.message.enable.dependent.plugin.for.current.project.only" :
              "dialog.message.enable.dependent.plugins.for.current.project.only";
        break;
      case DISABLE_GLOBALLY:
        key = hasOnlyOneDependency ?
              "dialog.message.disable.dependent.plugin" :
              "dialog.message.disable.dependent.plugins";
        break;
      case DISABLE_FOR_PROJECT:
        key = hasOnlyOneDependency ?
              "dialog.message.disable.dependent.plugin.for.current.project" :
              "dialog.message.disable.dependent.plugins.for.current.project";
        break;
      case DISABLE_FOR_PROJECT_ENABLE_GLOBALLY:
        key = hasOnlyOneDependency ?
              "dialog.message.disable.required.plugin.for.current.project.only" :
              "dialog.message.disable.required.plugins.for.current.project.only";
        break;
      default:
        throw new IllegalStateException("Unexpected value: " + action);
    }

    String dependenciesText = hasOnlyOneDependency ?
                              dependencies.iterator().next() :
                              dependencies.stream()
                                .map("&nbsp;".repeat(5)::concat)
                                .collect(Collectors.joining("<br>"));

    boolean enabled = action.isEnable();
    return MessageDialogBuilder
      .okCancel(IdeBundle.message(enabled ? "dialog.title.enable.required.plugins" : "dialog.title.disable.dependent.plugins"),
                IdeBundle.message(key, dependenciesText))
      .yesText(IdeBundle.message(enabled ? "plugins.configurable.enable" : "plugins.configurable.disable"))
      .noText(Messages.getCancelButton())
      .ask(getProject());
  }

  protected void handleBeforeChangeEnableState(@NotNull IdeaPluginDescriptor descriptor,
                                               @NotNull Pair<PluginEnableDisableAction, PluginEnabledState> pair) {
  }

  protected static boolean isEnabled(@NotNull PluginId pluginId,
                                     @NotNull Map<PluginId, PluginEnabledState> enabledMap) {
    PluginEnabledState state = enabledMap.get(pluginId);
    return state == null || state.isEnabled();
  }

  protected static boolean isDisabled(@NotNull PluginId pluginId,
                                      @NotNull Map<PluginId, PluginEnabledState> enabledMap) {
    PluginEnabledState state = enabledMap.get(pluginId);
    return state == null || state.isDisabled();
  }

  protected static boolean isLoaded(@NotNull PluginId pluginId,
                                    @NotNull Map<PluginId, PluginEnabledState> enabledMap) {
    return enabledMap.get(pluginId) != null;
  }

  protected static boolean isDeleted(@NotNull IdeaPluginDescriptor descriptor) {
    return descriptor instanceof IdeaPluginDescriptorImpl && ((IdeaPluginDescriptorImpl)descriptor).isDeleted();
  }

  protected static boolean isHiddenImplementationDetail(@NotNull IdeaPluginDescriptor descriptor) {
    return HIDE_IMPLEMENTATION_DETAILS && descriptor.isImplementationDetail();
  }

  protected static boolean isHidden(@NotNull IdeaPluginDescriptor descriptor) {
    return isDeleted(descriptor) ||
           isHiddenImplementationDetail(descriptor);
  }

  protected static @NotNull @NonNls String getPluginNameOrId(@NotNull PluginId pluginId,
                                                             @Nullable IdeaPluginDescriptor descriptor) {
    return descriptor != null ? descriptor.getName() : pluginId.getIdString();
  }
}