KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > profiles > ProfileManager


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.profiles;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
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 /**
31  * Manages profiles consisting of configuration folders.
32  * <p>
33  * Every profile has a name and a loation pointing to the configuration folder.
34  * <p>
35  * A profiles.xml configuration file is saved in the default config directory,
36  * storing all profiles information.
37  *
38  * @author fdietz
39  */

40 public class ProfileManager implements IProfileManager {
41
42     /**
43      * location of configuration directory
44      */

45     private File JavaDoc location;
46
47     /**
48      * profiles.xml file
49      */

50     private File JavaDoc profilesConfig;
51
52     /**
53      * top-level xml node
54      */

55     private XmlElement profiles;
56
57     /**
58      * using singleton pattern to instanciate class
59      */

60     private static ProfileManager instance = new ProfileManager();
61
62     /**
63      * Comment for <code>xml</code>
64      */

65     private XmlIO xml;
66
67     /**
68      * Currently running profile.
69      */

70     private Profile currentProfile;
71
72     /**
73      * default constructor
74      */

75     public ProfileManager() {
76         super();
77
78         if (OSInfo.isWindowsPlatform()) {
79             location = new File JavaDoc("config");
80         } else {
81             location = new File JavaDoc(System.getProperty("user.home"), ".columba");
82         }
83
84         // create directory, if it doesn't already exist
85
DiskIO.ensureDirectory(location);
86
87         profilesConfig = new File JavaDoc(location, "profiles.xml");
88
89     }
90
91     /**
92      * Get instance of class
93      *
94      * @return instance
95      */

96     public static ProfileManager getInstance() {
97
98         return instance;
99     }
100
101     /*
102      * (non-Javadoc)
103      *
104      * @see org.columba.core.profiles.IProfileManager#getProfileForName(java.lang.String)
105      */

106     public Profile getProfileForName(String JavaDoc 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 JavaDoc n = profile.getAttribute("name");
116
117         location = new File JavaDoc(profile.getAttribute("location"));
118         return new Profile(n, location);
119
120     }
121
122     /**
123      * Remove profile xml-element from "profiles.xml"
124      *
125      * @param name
126      * name of profile
127      */

128     protected void removeProfileXmlElement(String JavaDoc name) {
129         XmlElement child = getXmlElementForName(name);
130         child.removeFromParent();
131
132     }
133
134     protected XmlElement getXmlElementForName(String JavaDoc name) {
135         for (int i = 0; i < profiles.count(); i++) {
136
137             XmlElement profile = profiles.getElement(i);
138             String JavaDoc n = profile.getAttribute("name");
139             if (name.equalsIgnoreCase(n)) {
140
141                 return profile;
142             }
143         }
144         return null;
145     }
146
147     /**
148      * Get profile with location.
149      *
150      * @param path
151      * location of configuration directory
152      * @return profile if available. Otherwise, return null.
153      */

154     protected Profile getProfileForLocation(String JavaDoc path) {
155         for (int i = 0; i < profiles.count(); i++) {
156             XmlElement profile = profiles.getElement(i);
157             String JavaDoc location = profile.getAttribute("location");
158             if (path.equals(location)) {
159                 String JavaDoc n = profile.getAttribute("name");
160                 return new Profile(n, new File JavaDoc(location));
161             }
162         }
163         return null;
164     }
165
166     /*
167      * (non-Javadoc)
168      *
169      * @see org.columba.core.profiles.IProfileManager#getProfile(java.lang.String)
170      */

171     public Profile getProfile(String JavaDoc location) {
172         // load profiles.xml
173
loadProfilesConfiguration();
174
175         currentProfile = null;
176         if (location == null) {
177             // prompt user for profile
178
currentProfile = promptForProfile();
179         } else {
180             // use commandline-specified location
181

182             // try name first
183
currentProfile = getProfileForName(location);
184
185             if (currentProfile == null) {
186                 // try directory
187
currentProfile = getProfileForLocation(location);
188             }
189
190             if (currentProfile == null) {
191                 // create profile
192
XmlElement profileElement = new XmlElement("profile");
193                 profileElement.addAttribute("name", location);
194                 profileElement.addAttribute("location", location);
195                 profiles.addElement(profileElement);
196
197                 // save to profiles.xml
198
try {
199                     xml.save();
200                 } catch (Exception JavaDoc e) {
201                     e.printStackTrace();
202                 }
203
204                 currentProfile = getProfileForLocation(location);
205             }
206         }
207
208         return currentProfile;
209     }
210
211     /*
212      * (non-Javadoc)
213      *
214      * @see org.columba.core.profiles.IProfileManager#getSelectedProfile()
215      */

216     public String JavaDoc getSelectedProfile() {
217         String JavaDoc selected = null;
218         selected = profiles.getAttribute("selected");
219
220         if (selected == null)
221             selected = "Default";
222
223         return selected;
224     }
225
226     /**
227      * Set always ask parameter.
228      *
229      * @param alwaysAsk
230      * true, if user should be always asked which profile to use.
231      * False, otherwise.
232      */

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         // save to profiles.xml
241
try {
242             xml.save();
243         } catch (Exception JavaDoc e) {
244             e.printStackTrace();
245         }
246     }
247
248     /**
249      * Check if user should always be prompted for a profile on startup.
250      *
251      * @return true, if user should be always asked. False, otherwise.
252      */

253     public boolean isAlwaysAsk() {
254         String JavaDoc 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     /**
265      * Open dialog and prompt user for profile
266      *
267      * @return profile
268      */

269     protected Profile promptForProfile() {
270         String JavaDoc s = profiles.getAttribute("dont_ask");
271         if (s == null)
272             s = "true";
273
274         boolean dontAsk = Boolean.valueOf(s).booleanValue();
275
276         // use preselected profile
277
if (dontAsk) {
278             String JavaDoc selected = profiles.getAttribute("selected");
279             Profile p = null;
280             if (selected != null)
281                 p = getProfileForName(selected);
282
283             if (p == null) {
284                 // fall back to default profile
285

286                 p = new Profile("Default", location);
287             }
288
289             return p;
290         }
291
292         // show profile choosing dialog
293
ProfileChooserDialog d = new ProfileChooserDialog();
294         String JavaDoc profileName = d.getSelection();
295
296         if (profileName.equals("Default")) {
297             profiles.addAttribute("selected", "Default");
298             profiles.addAttribute("dont_ask", new Boolean JavaDoc(d
299                     .isDontAskedSelected()).toString());
300
301             // save to profiles.xml
302
try {
303                 xml.save();
304             } catch (Exception JavaDoc 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 JavaDoc(d
312                     .isDontAskedSelected()).toString());
313
314             // save to profiles.xml
315
try {
316                 xml.save();
317             } catch (Exception JavaDoc e) {
318                 e.printStackTrace();
319             }
320
321             return getProfileForName(profileName);
322         }
323     }
324
325     /**
326      * Returns top-level xml node
327      *
328      * @return top-level xml node
329      */
/*
330      * (non-Javadoc)
331      *
332      * @see org.columba.core.profiles.IProfileManager#getProfiles()
333      */

334     public XmlElement getProfiles() {
335         return profiles;
336     }
337
338     /**
339      * Add new profile.
340      *
341      * @param p
342      * new profile
343      */

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         // save to profiles.xml
352
try {
353             xml.save();
354         } catch (Exception JavaDoc e) {
355             e.printStackTrace();
356         }
357     }
358
359     public void renameProfile(String JavaDoc oldName, String JavaDoc newName) {
360         XmlElement element = getXmlElementForName(oldName);
361         element.addAttribute("name", newName);
362
363         // save to profiles.xml
364
try {
365             xml.save();
366         } catch (Exception JavaDoc e) {
367             e.printStackTrace();
368         }
369     }
370
371     /**
372      * Load profile configuration.
373      */

374     protected void loadProfilesConfiguration() {
375         if (!profilesConfig.exists()) {
376             // create profile config file
377
String JavaDoc hstr = "org/columba/core/config/profiles.xml";
378             try {
379                 DiskIO.copyResource(hstr, profilesConfig);
380             } catch (IOException JavaDoc e) {
381             }
382         }
383
384         // load profile config file
385
try {
386             URL JavaDoc url = profilesConfig.toURL();
387             xml = new XmlIO(url);
388             xml.load();
389             profiles = xml.getRoot().getElement("profiles");
390
391         } catch (MalformedURLException JavaDoc mue) {
392         }
393     }
394
395     /**
396      * Returns currently running profile.
397      *
398      * @return currently running profile.
399      */
/*
400      * (non-Javadoc)
401      *
402      * @see org.columba.core.profiles.IProfileManager#getCurrentProfile()
403      */

404     public Profile getCurrentProfile() {
405         return currentProfile;
406     }
407
408     /**
409      * Sets currently running profile.
410      *
411      * @param path currently running profile.
412      */

413     public void setCurrentProfile(String JavaDoc path) {
414         currentProfile = getProfile(path);
415     }
416
417     /**
418      * Remove all Columba-config files and skip everything else
419      * <p>
420      * This way a user could use his Windows directory as config-folder and
421      * wouldn't risk to loose files when deleting his profile.
422      *
423      * @param profile
424      * name of profile
425      */

426     public void removeProfile(String JavaDoc profile) {
427         Profile p = getProfileForName(profile);
428
429         String JavaDoc[] folders = new String JavaDoc[] { "mail", "addressbook", "chat", "log" };
430         String JavaDoc[] files = new String JavaDoc[] { "options.xml", "external_tools.xml" };
431
432         File JavaDoc location = p.getLocation();
433
434         // Is the location still existing?
435
if (location.exists()) {
436
437             // delete all directories
438
for (int i = 0; i < folders.length; i++) {
439                 // delete directory recursivly
440
DiskIO.deleteDirectory(new File JavaDoc(location, folders[i]));
441             }
442
443             // delete all files
444
for (int i = 0; i < files.length; i++) {
445                 new File JavaDoc(location, files[i]).delete();
446             }
447
448             // if config-folder is really empty
449
// -> delete folder
450
if (location.listFiles().length == 0)
451                 location.delete();
452         }
453
454         // remove profile xml-element
455
removeProfileXmlElement(profile);
456     }
457 }
Popular Tags