1 package org.columba.core.gui.profiles; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 25 import org.columba.core.base.OSInfo; 26 import org.columba.core.io.DiskIO; 27 import org.columba.core.xml.XmlElement; 28 import org.columba.core.xml.XmlIO; 29 30 40 public class ProfileManager implements IProfileManager { 41 42 45 private File location; 46 47 50 private File profilesConfig; 51 52 55 private XmlElement profiles; 56 57 60 private static ProfileManager instance = new ProfileManager(); 61 62 65 private XmlIO xml; 66 67 70 private Profile currentProfile; 71 72 75 public ProfileManager() { 76 super(); 77 78 if (OSInfo.isWindowsPlatform()) { 79 location = new File ("config"); 80 } else { 81 location = new File (System.getProperty("user.home"), ".columba"); 82 } 83 84 DiskIO.ensureDirectory(location); 86 87 profilesConfig = new File (location, "profiles.xml"); 88 89 } 90 91 96 public static ProfileManager getInstance() { 97 98 return instance; 99 } 100 101 106 public Profile getProfileForName(String name) { 107 if (name.equalsIgnoreCase("default")) { 108 return new Profile("Default", location); 109 } 110 111 XmlElement profile = getXmlElementForName(name); 112 if (profile == null) 113 return null; 114 115 String n = profile.getAttribute("name"); 116 117 location = new File (profile.getAttribute("location")); 118 return new Profile(n, location); 119 120 } 121 122 128 protected void removeProfileXmlElement(String name) { 129 XmlElement child = getXmlElementForName(name); 130 child.removeFromParent(); 131 132 } 133 134 protected XmlElement getXmlElementForName(String name) { 135 for (int i = 0; i < profiles.count(); i++) { 136 137 XmlElement profile = profiles.getElement(i); 138 String n = profile.getAttribute("name"); 139 if (name.equalsIgnoreCase(n)) { 140 141 return profile; 142 } 143 } 144 return null; 145 } 146 147 154 protected Profile getProfileForLocation(String path) { 155 for (int i = 0; i < profiles.count(); i++) { 156 XmlElement profile = profiles.getElement(i); 157 String location = profile.getAttribute("location"); 158 if (path.equals(location)) { 159 String n = profile.getAttribute("name"); 160 return new Profile(n, new File (location)); 161 } 162 } 163 return null; 164 } 165 166 171 public Profile getProfile(String location) { 172 loadProfilesConfiguration(); 174 175 currentProfile = null; 176 if (location == null) { 177 currentProfile = promptForProfile(); 179 } else { 180 182 currentProfile = getProfileForName(location); 184 185 if (currentProfile == null) { 186 currentProfile = getProfileForLocation(location); 188 } 189 190 if (currentProfile == null) { 191 XmlElement profileElement = new XmlElement("profile"); 193 profileElement.addAttribute("name", location); 194 profileElement.addAttribute("location", location); 195 profiles.addElement(profileElement); 196 197 try { 199 xml.save(); 200 } catch (Exception e) { 201 e.printStackTrace(); 202 } 203 204 currentProfile = getProfileForLocation(location); 205 } 206 } 207 208 return currentProfile; 209 } 210 211 216 public String getSelectedProfile() { 217 String selected = null; 218 selected = profiles.getAttribute("selected"); 219 220 if (selected == null) 221 selected = "Default"; 222 223 return selected; 224 } 225 226 233 public void setAlwaysAsk(boolean alwaysAsk) { 234 if (alwaysAsk) { 235 profiles.addAttribute("dont_ask", "false"); 236 } else { 237 profiles.addAttribute("dont_ask", "true"); 238 } 239 240 try { 242 xml.save(); 243 } catch (Exception e) { 244 e.printStackTrace(); 245 } 246 } 247 248 253 public boolean isAlwaysAsk() { 254 String s = profiles.getAttribute("dont_ask"); 255 if (s == null) 256 s = "true"; 257 258 if (s.equals("true")) 259 return false; 260 261 return true; 262 } 263 264 269 protected Profile promptForProfile() { 270 String s = profiles.getAttribute("dont_ask"); 271 if (s == null) 272 s = "true"; 273 274 boolean dontAsk = Boolean.valueOf(s).booleanValue(); 275 276 if (dontAsk) { 278 String selected = profiles.getAttribute("selected"); 279 Profile p = null; 280 if (selected != null) 281 p = getProfileForName(selected); 282 283 if (p == null) { 284 286 p = new Profile("Default", location); 287 } 288 289 return p; 290 } 291 292 ProfileChooserDialog d = new ProfileChooserDialog(); 294 String profileName = d.getSelection(); 295 296 if (profileName.equals("Default")) { 297 profiles.addAttribute("selected", "Default"); 298 profiles.addAttribute("dont_ask", new Boolean (d 299 .isDontAskedSelected()).toString()); 300 301 try { 303 xml.save(); 304 } catch (Exception e) { 305 e.printStackTrace(); 306 } 307 308 return new Profile("Default", location); 309 } else { 310 profiles.addAttribute("selected", profileName); 311 profiles.addAttribute("dont_ask", new Boolean (d 312 .isDontAskedSelected()).toString()); 313 314 try { 316 xml.save(); 317 } catch (Exception e) { 318 e.printStackTrace(); 319 } 320 321 return getProfileForName(profileName); 322 } 323 } 324 325 334 public XmlElement getProfiles() { 335 return profiles; 336 } 337 338 344 public void addProfile(Profile p) { 345 XmlElement profile = new XmlElement("profile"); 346 profile.addAttribute("name", p.getName()); 347 profile.addAttribute("location", p.getLocation().getPath()); 348 349 profiles.addElement(profile); 350 351 try { 353 xml.save(); 354 } catch (Exception e) { 355 e.printStackTrace(); 356 } 357 } 358 359 public void renameProfile(String oldName, String newName) { 360 XmlElement element = getXmlElementForName(oldName); 361 element.addAttribute("name", newName); 362 363 try { 365 xml.save(); 366 } catch (Exception e) { 367 e.printStackTrace(); 368 } 369 } 370 371 374 protected void loadProfilesConfiguration() { 375 if (!profilesConfig.exists()) { 376 String hstr = "org/columba/core/config/profiles.xml"; 378 try { 379 DiskIO.copyResource(hstr, profilesConfig); 380 } catch (IOException e) { 381 } 382 } 383 384 try { 386 URL url = profilesConfig.toURL(); 387 xml = new XmlIO(url); 388 xml.load(); 389 profiles = xml.getRoot().getElement("profiles"); 390 391 } catch (MalformedURLException mue) { 392 } 393 } 394 395 404 public Profile getCurrentProfile() { 405 return currentProfile; 406 } 407 408 413 public void setCurrentProfile(String path) { 414 currentProfile = getProfile(path); 415 } 416 417 426 public void removeProfile(String profile) { 427 Profile p = getProfileForName(profile); 428 429 String [] folders = new String [] { "mail", "addressbook", "chat", "log" }; 430 String [] files = new String [] { "options.xml", "external_tools.xml" }; 431 432 File location = p.getLocation(); 433 434 if (location.exists()) { 436 437 for (int i = 0; i < folders.length; i++) { 439 DiskIO.deleteDirectory(new File (location, folders[i])); 441 } 442 443 for (int i = 0; i < files.length; i++) { 445 new File (location, files[i]).delete(); 446 } 447 448 if (location.listFiles().length == 0) 451 location.delete(); 452 } 453 454 removeProfileXmlElement(profile); 456 } 457 } | Popular Tags |