001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.preferences; 020 021import icy.network.AuthenticationInfo; 022import icy.network.NetworkUtil; 023import icy.util.StringUtil; 024 025import java.util.ArrayList; 026import java.util.HashSet; 027import java.util.List; 028import java.util.Set; 029 030/** 031 * @author Stephane 032 */ 033public class RepositoryPreferences 034{ 035 public static class RepositoryInfo 036 { 037 private static final String ID_NAME = "name"; 038 private static final String ID_LOCATION = "location"; 039 private static final String ID_ENABLED = "enabled"; 040 private static final String ID_SUPPORTPARAM = "supportParam"; 041 private static final String ID_AUTHENTICATION = "authentication"; 042 043 private String name; 044 private String location; 045 private boolean supportParam; 046 private boolean enabled; 047 private final AuthenticationInfo authInf; 048 049 public RepositoryInfo(String name, String location, String login, String password, boolean authEnabled, 050 boolean enabled, boolean supportParam) 051 { 052 super(); 053 054 this.name = name; 055 this.location = location; 056 this.supportParam = supportParam; 057 this.enabled = enabled; 058 authInf = new AuthenticationInfo(login, password, authEnabled); 059 } 060 061 public RepositoryInfo(String name, String location, String login, String password, boolean authEnabled, 062 boolean enabled) 063 { 064 this(name, location, login, password, authEnabled, enabled, true); 065 } 066 067 public RepositoryInfo(String name, String location, boolean enabled) 068 { 069 this(name, location, "", "", false, enabled); 070 } 071 072 public RepositoryInfo(String name, String location) 073 { 074 this(name, location, "", "", false, true); 075 } 076 077 public RepositoryInfo(XMLPreferences node) 078 { 079 this("", "", "", "", false, false); 080 081 load(node); 082 } 083 084 public boolean isEmpty() 085 { 086 return StringUtil.isEmpty(location); 087 } 088 089 /** 090 * @return the name 091 */ 092 public String getName() 093 { 094 return name; 095 } 096 097 /** 098 * @param name 099 * the name to set 100 */ 101 public void setName(String name) 102 { 103 this.name = name; 104 } 105 106 /** 107 * @return the location 108 */ 109 public String getLocation() 110 { 111 return location; 112 } 113 114 /** 115 * @param location 116 * the location to set 117 */ 118 public void setLocation(String location) 119 { 120 this.location = location; 121 } 122 123 /** 124 * @return the supportParam 125 */ 126 public boolean getSupportParam() 127 { 128 return supportParam; 129 } 130 131 /** 132 * @param supportParam 133 * the supportParam to set 134 */ 135 public void setSupportParam(boolean supportParam) 136 { 137 this.supportParam = supportParam; 138 } 139 140 /** 141 * /** 142 * 143 * @return the enabled 144 */ 145 public boolean isEnabled() 146 { 147 return enabled; 148 } 149 150 /** 151 * @param enabled 152 * the enabled to set 153 */ 154 public void setEnabled(boolean enabled) 155 { 156 this.enabled = enabled; 157 } 158 159 /** 160 * @return the login 161 */ 162 public String getLogin() 163 { 164 return authInf.getLogin(); 165 } 166 167 /** 168 * @param login 169 * the login to set 170 */ 171 public void setLogin(String login) 172 { 173 authInf.setLogin(login); 174 } 175 176 /** 177 * @return the password 178 */ 179 public String getPassword() 180 { 181 return authInf.getPassword(); 182 } 183 184 /** 185 * @param password 186 * the password to set 187 */ 188 public void setPassword(String password) 189 { 190 authInf.setPassword(password); 191 } 192 193 /** 194 * @return the authentication enabled 195 */ 196 public boolean isAuthenticationEnabled() 197 { 198 return authInf.isEnabled(); 199 } 200 201 /** 202 * @param value 203 * the authentication enabled to set 204 */ 205 public void setAuthenticationEnabled(boolean value) 206 { 207 authInf.setEnabled(value); 208 } 209 210 /** 211 * @return the authentication informations 212 */ 213 public AuthenticationInfo getAuthenticationInfo() 214 { 215 return authInf; 216 } 217 218 public void load(XMLPreferences node) 219 { 220 if (node != null) 221 { 222 name = node.get(ID_NAME, ""); 223 location = node.get(ID_LOCATION, ""); 224 enabled = node.getBoolean(ID_ENABLED, false); 225 supportParam = node.getBoolean(ID_SUPPORTPARAM, true); 226 authInf.load(node.node(ID_AUTHENTICATION)); 227 } 228 } 229 230 public void save(XMLPreferences node) 231 { 232 if (node != null) 233 { 234 node.put(ID_NAME, name); 235 node.put(ID_LOCATION, location); 236 node.putBoolean(ID_ENABLED, enabled); 237 node.putBoolean(ID_SUPPORTPARAM, supportParam); 238 authInf.save(node.node(ID_AUTHENTICATION)); 239 } 240 } 241 242 public boolean isDefault() 243 { 244 return RepositoryPreferences.DEFAULT_REPOSITERY_NAME.equals(name) 245 && RepositoryPreferences.DEFAULT_REPOSITERY_LOCATION.equals(location); 246 } 247 248 public boolean isOldDefault() 249 { 250 return RepositoryPreferences.DEFAULT_REPOSITERY_NAME.equals(name) 251 && RepositoryPreferences.DEFAULT_REPOSITERY_LOCATION_OLD.equals(location); 252 } 253 254 @Override 255 public String toString() 256 { 257 return name + " - " + location; 258 } 259 260 @Override 261 public int hashCode() 262 { 263 return name.hashCode() ^ location.hashCode() ^ authInf.hashCode(); 264 } 265 266 @Override 267 public boolean equals(Object obj) 268 { 269 if (obj instanceof RepositoryInfo) 270 { 271 final RepositoryInfo repoInf = (RepositoryInfo) obj; 272 273 return StringUtil.equals(repoInf.toString(), toString()) && repoInf.authInf.equals(authInf); 274 } 275 276 return super.equals(obj); 277 } 278 } 279 280 /** 281 * pref id 282 */ 283 private static final String PREF_ID = "repositories"; 284 285 /** 286 * preferences 287 */ 288 private static XMLPreferences preferences; 289 290 public static final String DEFAULT_REPOSITERY_NAME = "default"; 291 public static final String DEFAULT_REPOSITERY_LOCATION = NetworkUtil.WEBSITE_URL + "repository/"; 292 public static final String DEFAULT_REPOSITERY_LOCATION_OLD = "http://www.bioimageanalysis.org/icy/repository/"; 293 294 public static void load() 295 { 296 // load preference 297 preferences = ApplicationPreferences.getPreferences().node(PREF_ID); 298 } 299 300 /** 301 * @return the preferences 302 */ 303 public static XMLPreferences getPreferences() 304 { 305 return preferences; 306 } 307 308 public static ArrayList<RepositoryInfo> getRepositeries() 309 { 310 final Set<RepositoryInfo> result = new HashSet<RepositoryInfo>(); 311 final List<String> childs = preferences.childrenNames(); 312 313 for (String child : childs) 314 { 315 final RepositoryInfo reposInf = new RepositoryInfo(preferences.node(child)); 316 317 // ignore empty and old default repository 318 if (!reposInf.isEmpty() && !reposInf.isOldDefault()) 319 result.add(reposInf); 320 } 321 322 // add default repository if needed 323 result.add(new RepositoryInfo(DEFAULT_REPOSITERY_NAME, DEFAULT_REPOSITERY_LOCATION, true)); 324 325 return new ArrayList<RepositoryInfo>(result); 326 } 327 328 public static void setRepositeries(ArrayList<RepositoryInfo> values) 329 { 330 // remove all child nodes 331 preferences.removeChildren(); 332 333 int i = 0; 334 for (RepositoryInfo reposInf : values) 335 { 336 if (!reposInf.isEmpty()) 337 reposInf.save(preferences.node("repos" + i)); 338 339 i++; 340 } 341 342 // clean up all non element nodes 343 preferences.clean(); 344 } 345}