Source Code Cross Referenced for ProfileManager.java in  » Mail-Clients » columba-1.4 » org » columba » core » gui » profiles » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Mail Clients » columba 1.4 » org.columba.core.gui.profiles 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // The contents of this file are subject to the Mozilla Public License Version
002:        // 1.1
003:        //(the "License"); you may not use this file except in compliance with the
004:        //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005:        //
006:        //Software distributed under the License is distributed on an "AS IS" basis,
007:        //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008:        //for the specific language governing rights and
009:        //limitations under the License.
010:        //
011:        //The Original Code is "The Columba Project"
012:        //
013:        //The Initial Developers of the Original Code are Frederik Dietz and Timo
014:        // Stich.
015:        //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016:        //
017:        //All Rights Reserved.
018:        package org.columba.core.gui.profiles;
019:
020:        import java.io.File;
021:        import java.io.IOException;
022:        import java.net.MalformedURLException;
023:        import java.net.URL;
024:
025:        import org.columba.core.base.OSInfo;
026:        import org.columba.core.io.DiskIO;
027:        import org.columba.core.xml.XmlElement;
028:        import org.columba.core.xml.XmlIO;
029:
030:        /**
031:         * Manages profiles consisting of configuration folders.
032:         * <p>
033:         * Every profile has a name and a loation pointing to the configuration folder.
034:         * <p>
035:         * A profiles.xml configuration file is saved in the default config directory,
036:         * storing all profiles information.
037:         * 
038:         * @author fdietz
039:         */
040:        public class ProfileManager implements  IProfileManager {
041:
042:            /**
043:             * location of configuration directory
044:             */
045:            private File location;
046:
047:            /**
048:             * profiles.xml file
049:             */
050:            private File profilesConfig;
051:
052:            /**
053:             * top-level xml node
054:             */
055:            private XmlElement profiles;
056:
057:            /**
058:             * using singleton pattern to instanciate class
059:             */
060:            private static ProfileManager instance = new ProfileManager();
061:
062:            /**
063:             * Comment for <code>xml</code>
064:             */
065:            private XmlIO xml;
066:
067:            /**
068:             * Currently running profile.
069:             */
070:            private Profile currentProfile;
071:
072:            /**
073:             * default constructor
074:             */
075:            public ProfileManager() {
076:                super ();
077:
078:                if (OSInfo.isWindowsPlatform()) {
079:                    location = new File("config");
080:                } else {
081:                    location = new File(System.getProperty("user.home"),
082:                            ".columba");
083:                }
084:
085:                // create directory, if it doesn't already exist
086:                DiskIO.ensureDirectory(location);
087:
088:                profilesConfig = new File(location, "profiles.xml");
089:
090:            }
091:
092:            /**
093:             * Get instance of class
094:             * 
095:             * @return instance
096:             */
097:            public static ProfileManager getInstance() {
098:
099:                return instance;
100:            }
101:
102:            /*
103:             * (non-Javadoc)
104:             * 
105:             * @see org.columba.core.profiles.IProfileManager#getProfileForName(java.lang.String)
106:             */
107:            public Profile getProfileForName(String name) {
108:                if (name.equalsIgnoreCase("default")) {
109:                    return new Profile("Default", location);
110:                }
111:
112:                XmlElement profile = getXmlElementForName(name);
113:                if (profile == null)
114:                    return null;
115:
116:                String n = profile.getAttribute("name");
117:
118:                location = new File(profile.getAttribute("location"));
119:                return new Profile(n, location);
120:
121:            }
122:
123:            /**
124:             * Remove profile xml-element from "profiles.xml"
125:             * 
126:             * @param name
127:             *            name of profile
128:             */
129:            protected void removeProfileXmlElement(String name) {
130:                XmlElement child = getXmlElementForName(name);
131:                child.removeFromParent();
132:
133:            }
134:
135:            protected XmlElement getXmlElementForName(String name) {
136:                for (int i = 0; i < profiles.count(); i++) {
137:
138:                    XmlElement profile = profiles.getElement(i);
139:                    String n = profile.getAttribute("name");
140:                    if (name.equalsIgnoreCase(n)) {
141:
142:                        return profile;
143:                    }
144:                }
145:                return null;
146:            }
147:
148:            /**
149:             * Get profile with location.
150:             * 
151:             * @param path
152:             *            location of configuration directory
153:             * @return profile if available. Otherwise, return null.
154:             */
155:            protected Profile getProfileForLocation(String path) {
156:                for (int i = 0; i < profiles.count(); i++) {
157:                    XmlElement profile = profiles.getElement(i);
158:                    String location = profile.getAttribute("location");
159:                    if (path.equals(location)) {
160:                        String n = profile.getAttribute("name");
161:                        return new Profile(n, new File(location));
162:                    }
163:                }
164:                return null;
165:            }
166:
167:            /*
168:             * (non-Javadoc)
169:             * 
170:             * @see org.columba.core.profiles.IProfileManager#getProfile(java.lang.String)
171:             */
172:            public Profile getProfile(String location) {
173:                // load profiles.xml
174:                loadProfilesConfiguration();
175:
176:                currentProfile = null;
177:                if (location == null) {
178:                    // prompt user for profile
179:                    currentProfile = promptForProfile();
180:                } else {
181:                    // use commandline-specified location
182:
183:                    // try name first
184:                    currentProfile = getProfileForName(location);
185:
186:                    if (currentProfile == null) {
187:                        // try directory
188:                        currentProfile = getProfileForLocation(location);
189:                    }
190:
191:                    if (currentProfile == null) {
192:                        // create profile
193:                        XmlElement profileElement = new XmlElement("profile");
194:                        profileElement.addAttribute("name", location);
195:                        profileElement.addAttribute("location", location);
196:                        profiles.addElement(profileElement);
197:
198:                        // save to profiles.xml
199:                        try {
200:                            xml.save();
201:                        } catch (Exception e) {
202:                            e.printStackTrace();
203:                        }
204:
205:                        currentProfile = getProfileForLocation(location);
206:                    }
207:                }
208:
209:                return currentProfile;
210:            }
211:
212:            /*
213:             * (non-Javadoc)
214:             * 
215:             * @see org.columba.core.profiles.IProfileManager#getSelectedProfile()
216:             */
217:            public String getSelectedProfile() {
218:                String selected = null;
219:                selected = profiles.getAttribute("selected");
220:
221:                if (selected == null)
222:                    selected = "Default";
223:
224:                return selected;
225:            }
226:
227:            /**
228:             * Set always ask parameter.
229:             * 
230:             * @param alwaysAsk
231:             *            true, if user should be always asked which profile to use.
232:             *            False, otherwise.
233:             */
234:            public void setAlwaysAsk(boolean alwaysAsk) {
235:                if (alwaysAsk) {
236:                    profiles.addAttribute("dont_ask", "false");
237:                } else {
238:                    profiles.addAttribute("dont_ask", "true");
239:                }
240:
241:                // save to profiles.xml
242:                try {
243:                    xml.save();
244:                } catch (Exception e) {
245:                    e.printStackTrace();
246:                }
247:            }
248:
249:            /**
250:             * Check if user should always be prompted for a profile on startup.
251:             * 
252:             * @return true, if user should be always asked. False, otherwise.
253:             */
254:            public boolean isAlwaysAsk() {
255:                String s = profiles.getAttribute("dont_ask");
256:                if (s == null)
257:                    s = "true";
258:
259:                if (s.equals("true"))
260:                    return false;
261:
262:                return true;
263:            }
264:
265:            /**
266:             * Open dialog and prompt user for profile
267:             * 
268:             * @return profile
269:             */
270:            protected Profile promptForProfile() {
271:                String s = profiles.getAttribute("dont_ask");
272:                if (s == null)
273:                    s = "true";
274:
275:                boolean dontAsk = Boolean.valueOf(s).booleanValue();
276:
277:                // use preselected profile
278:                if (dontAsk) {
279:                    String selected = profiles.getAttribute("selected");
280:                    Profile p = null;
281:                    if (selected != null)
282:                        p = getProfileForName(selected);
283:
284:                    if (p == null) {
285:                        // fall back to default profile
286:
287:                        p = new Profile("Default", location);
288:                    }
289:
290:                    return p;
291:                }
292:
293:                // show profile choosing dialog
294:                ProfileChooserDialog d = new ProfileChooserDialog();
295:                String profileName = d.getSelection();
296:
297:                if (profileName.equals("Default")) {
298:                    profiles.addAttribute("selected", "Default");
299:                    profiles.addAttribute("dont_ask", new Boolean(d
300:                            .isDontAskedSelected()).toString());
301:
302:                    // save to profiles.xml
303:                    try {
304:                        xml.save();
305:                    } catch (Exception e) {
306:                        e.printStackTrace();
307:                    }
308:
309:                    return new Profile("Default", location);
310:                } else {
311:                    profiles.addAttribute("selected", profileName);
312:                    profiles.addAttribute("dont_ask", new Boolean(d
313:                            .isDontAskedSelected()).toString());
314:
315:                    // save to profiles.xml
316:                    try {
317:                        xml.save();
318:                    } catch (Exception e) {
319:                        e.printStackTrace();
320:                    }
321:
322:                    return getProfileForName(profileName);
323:                }
324:            }
325:
326:            /**
327:             * Returns top-level xml node
328:             *
329:             * @return top-level xml node
330:             */
331:            /*
332:             * (non-Javadoc)
333:             * 
334:             * @see org.columba.core.profiles.IProfileManager#getProfiles()
335:             */
336:            public XmlElement getProfiles() {
337:                return profiles;
338:            }
339:
340:            /**
341:             * Add new profile.
342:             * 
343:             * @param p
344:             *            new profile
345:             */
346:            public void addProfile(Profile p) {
347:                XmlElement profile = new XmlElement("profile");
348:                profile.addAttribute("name", p.getName());
349:                profile.addAttribute("location", p.getLocation().getPath());
350:
351:                profiles.addElement(profile);
352:
353:                // save to profiles.xml
354:                try {
355:                    xml.save();
356:                } catch (Exception e) {
357:                    e.printStackTrace();
358:                }
359:            }
360:
361:            public void renameProfile(String oldName, String newName) {
362:                XmlElement element = getXmlElementForName(oldName);
363:                element.addAttribute("name", newName);
364:
365:                // save to profiles.xml
366:                try {
367:                    xml.save();
368:                } catch (Exception e) {
369:                    e.printStackTrace();
370:                }
371:            }
372:
373:            /**
374:             * Load profile configuration.
375:             */
376:            protected void loadProfilesConfiguration() {
377:                if (!profilesConfig.exists()) {
378:                    // create profile config file
379:                    String hstr = "org/columba/core/config/profiles.xml";
380:                    try {
381:                        DiskIO.copyResource(hstr, profilesConfig);
382:                    } catch (IOException e) {
383:                    }
384:                }
385:
386:                // load profile config file
387:                try {
388:                    URL url = profilesConfig.toURL();
389:                    xml = new XmlIO(url);
390:                    xml.load();
391:                    profiles = xml.getRoot().getElement("profiles");
392:
393:                } catch (MalformedURLException mue) {
394:                }
395:            }
396:
397:            /**
398:             * Returns currently running profile.
399:             *
400:             * @return currently running profile.
401:             */
402:            /*
403:             * (non-Javadoc)
404:             * 
405:             * @see org.columba.core.profiles.IProfileManager#getCurrentProfile()
406:             */
407:            public Profile getCurrentProfile() {
408:                return currentProfile;
409:            }
410:
411:            /**
412:             * Sets currently running profile.
413:             *
414:             * @param path currently running profile.
415:             */
416:            public void setCurrentProfile(String path) {
417:                currentProfile = getProfile(path);
418:            }
419:
420:            /**
421:             * Remove all Columba-config files and skip everything else
422:             * <p>
423:             * This way a user could use his Windows directory as config-folder and
424:             * wouldn't risk to loose files when deleting his profile.
425:             * 
426:             * @param profile
427:             *            name of profile
428:             */
429:            public void removeProfile(String profile) {
430:                Profile p = getProfileForName(profile);
431:
432:                String[] folders = new String[] { "mail", "addressbook",
433:                        "chat", "log" };
434:                String[] files = new String[] { "options.xml",
435:                        "external_tools.xml" };
436:
437:                File location = p.getLocation();
438:
439:                // Is the location still existing?
440:                if (location.exists()) {
441:
442:                    // delete all directories
443:                    for (int i = 0; i < folders.length; i++) {
444:                        // delete directory recursivly
445:                        DiskIO.deleteDirectory(new File(location, folders[i]));
446:                    }
447:
448:                    // delete all files
449:                    for (int i = 0; i < files.length; i++) {
450:                        new File(location, files[i]).delete();
451:                    }
452:
453:                    // if config-folder is really empty
454:                    // -> delete folder
455:                    if (location.listFiles().length == 0)
456:                        location.delete();
457:                }
458:
459:                // remove profile xml-element
460:                removeProfileXmlElement(profile);
461:            }
462:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.