b693bbf23b638c59ca1ddf790ea818d4d60f80a7
[idea/community.git] / platform / platform-impl / src / com / intellij / openapi / updateSettings / impl / UpdateSettings.java
1 /*
2  * Copyright 2000-2015 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.openapi.updateSettings.impl;
17
18 import com.intellij.openapi.application.ApplicationInfo;
19 import com.intellij.openapi.application.impl.ApplicationInfoImpl;
20 import com.intellij.openapi.components.*;
21 import com.intellij.openapi.util.text.StringUtil;
22 import com.intellij.util.SmartList;
23 import com.intellij.util.containers.ContainerUtil;
24 import com.intellij.util.net.NetUtils;
25 import com.intellij.util.xmlb.annotations.CollectionBean;
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 @State(
33   name = "UpdatesConfigurable",
34   storages = {
35     @Storage(file = StoragePathMacros.APP_CONFIG + "/updates.xml", roamingType = RoamingType.DISABLED),
36     @Storage(file = StoragePathMacros.APP_CONFIG + "/other.xml", deprecated = true)
37   }
38 )
39 public class UpdateSettings implements PersistentStateComponent<UpdateSettings.State>, UserUpdateSettings {
40   public static class State {
41     @CollectionBean public final List<String> pluginHosts = new SmartList<String>();
42     @CollectionBean public final List<String> knownUpdateChannels = new SmartList<String>();
43     @CollectionBean public final List<String> ignoredBuildNumbers = new SmartList<String>();
44     @CollectionBean public final List<String> outdatedPlugins = new SmartList<String>();
45
46     public boolean CHECK_NEEDED = true;
47     public long LAST_TIME_CHECKED = 0;
48
49     public String LAST_BUILD_CHECKED;
50     public String UPDATE_CHANNEL_TYPE = ChannelStatus.RELEASE_CODE;
51     public boolean SECURE_CONNECTION = true;
52   }
53
54   public static UpdateSettings getInstance() {
55     return ServiceManager.getService(UpdateSettings.class);
56   }
57
58   private State myState = new State();
59
60   public UpdateSettings() {
61     updateDefaultChannel();
62   }
63
64   @Nullable
65   public String getLasBuildChecked() {
66     return myState.LAST_BUILD_CHECKED;
67   }
68
69   @NotNull
70   public List<String> getStoredPluginHosts() {
71     return myState.pluginHosts;
72   }
73
74   public boolean isCheckNeeded() {
75     return myState.CHECK_NEEDED;
76   }
77
78   public void setCheckNeeded(boolean value) {
79     myState.CHECK_NEEDED = value;
80   }
81
82   public boolean isSecureConnection() {
83     return myState.SECURE_CONNECTION;
84   }
85
86   public void setSecureConnection(boolean value) {
87     myState.SECURE_CONNECTION = value;
88   }
89
90   @NotNull
91   public String getUpdateChannelType() {
92     return myState.UPDATE_CHANNEL_TYPE;
93   }
94
95   public long getLastTimeChecked() {
96     return myState.LAST_TIME_CHECKED;
97   }
98
99   public void setUpdateChannelType(@NotNull String value) {
100     myState.UPDATE_CHANNEL_TYPE = value;
101   }
102
103   @NotNull
104   public List<String> getOutdatedPlugins() {
105     return myState.outdatedPlugins;
106   }
107
108   private void updateDefaultChannel() {
109     if (ApplicationInfoImpl.getShadowInstance().isEAP()) {
110       myState.UPDATE_CHANNEL_TYPE = ChannelStatus.EAP_CODE;
111     }
112   }
113
114   @NotNull
115   @Override
116   public State getState() {
117     return myState;
118   }
119
120   @Override
121   public void loadState(@NotNull State state) {
122     myState = state;
123     myState.LAST_BUILD_CHECKED = StringUtil.nullize(myState.LAST_BUILD_CHECKED);
124     updateDefaultChannel();
125   }
126
127   @NotNull
128   @Override
129   public List<String> getKnownChannelsIds() {
130     return new ArrayList<String>(myState.knownUpdateChannels);
131   }
132
133   @Override
134   public void setKnownChannelIds(@NotNull List<String> ids) {
135     myState.knownUpdateChannels.clear();
136     for (String id : ids) {
137       myState.knownUpdateChannels.add(id);
138     }
139   }
140
141   public void forgetChannelId(String id) {
142     myState.knownUpdateChannels.remove(id);
143   }
144
145   @Override
146   public List<String> getIgnoredBuildNumbers() {
147     return myState.ignoredBuildNumbers;
148   }
149
150   @NotNull
151   @Override
152   public ChannelStatus getSelectedChannelStatus() {
153     return ChannelStatus.fromCode(myState.UPDATE_CHANNEL_TYPE);
154   }
155
156   public List<String> getPluginHosts() {
157     List<String> hosts = new ArrayList<String>(myState.pluginHosts);
158     String pluginHosts = System.getProperty("idea.plugin.hosts");
159     if (pluginHosts != null) {
160       ContainerUtil.addAll(hosts, pluginHosts.split(";"));
161     }
162     return hosts;
163   }
164
165   public void forceCheckForUpdateAfterRestart() {
166     myState.LAST_TIME_CHECKED = 0;
167   }
168
169   public void saveLastCheckedInfo() {
170     myState.LAST_TIME_CHECKED = System.currentTimeMillis();
171     myState.LAST_BUILD_CHECKED = ApplicationInfo.getInstance().getBuild().asString();
172   }
173
174   public boolean canUseSecureConnection() {
175     return myState.SECURE_CONNECTION && NetUtils.isSniEnabled();
176   }
177 }