1
2
3
4
5
6
7
8
9
10 package ch.braincell.stickbackup.config;
11
12 import java.awt.Color;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.util.Properties;
19
20 import sun.awt.shell.ShellFolder;
21
22 /***
23 *
24 * @author Andreas Studer
25 */
26 public class Config {
27
28 protected static final String sourceDir = "ch.braincell.stickbackup.sourcedir";
29
30 protected static final String sourceDrive = "ch.braincell.stickbackup.sourcedrive";
31
32 protected static final String sourceSearchDrive = "ch.braincell.stickbackup.search.sourcedrive";
33
34 protected static final String destinationDir = "ch.braincell.stickbackup.backupdir";
35
36 protected static final String destinationDrive = "ch.braincell.stickbackup.backupdrive";
37
38 protected static final String destinationSearchDrive = "ch.braincell.stickbackup.search.backupdrive";
39
40 protected static final String sourceColor = "ch.braincell.stickbackup.color.source";
41
42 protected static final int defaultSourceColor = 0x00AA00;
43
44 protected static final String destinationColor = "ch.braincell.stickbackup.color.destination";
45
46 protected static final int defaultDestinationColor = 0x0000AA;
47
48 protected static final String changedColor = "ch.braincell.stickbackup.color.changed";
49
50 protected static final int defaultChangedColor = 0xAAAA00;
51
52 protected static final String workingColor = "ch.braincell.stickbackup.color.working";
53
54 protected static final int defaultWorkingColor = 0xAA0000;
55
56 protected static final String finishedColor = "ch.braincell.stickbackup.color.finished";
57
58 protected static final int defaultFinishedColor = 0xCCCCCC;
59
60 private Properties prop = new Properties();
61
62 File propfile = new File(System.getProperty("user.home") + "/.stickbackup");
63
64 protected static Config config = null;
65
66 /*** Creates a new instance of Config */
67 protected Config() {
68 }
69
70 /***
71 * Singleton-pattern: get an instance of Config.
72 *
73 * @return the instance of Config.
74 */
75 public static Config getInstance() {
76 if (config == null) {
77 config = new Config();
78 try {
79 config.load();
80 } catch (IOException ex) {
81 throw new ConfigException(
82 "Config initialization loading failed", ex);
83 }
84 }
85 return config;
86 }
87
88 /***
89 * Load configuration from disk.
90 *
91 * @throws IOException
92 */
93 public void load() throws IOException {
94 if (propfile.exists()) {
95 prop.loadFromXML(new FileInputStream(propfile));
96 } else {
97 save();
98 }
99 }
100
101 /***
102 * Save the configuration to the disk.
103 *
104 * @throws IOException
105 */
106 public void save() throws IOException {
107 prop.storeToXML(new FileOutputStream(propfile), "Saved config from "
108 + Config.class.getName());
109 }
110
111 /***
112 * Tests if the system is a Microsoft Windows system.
113 *
114 * @return true if it is windows, false otherwise.
115 */
116 public boolean isWindows() {
117 return System.getProperty("os.name").toLowerCase().contains("windows");
118 }
119
120 /***
121 * Get the source directory.
122 *
123 * @return the source directory.
124 */
125 public File getSourceDir() {
126 return getCorrectDrive(sourceDir, sourceDrive, isSourceDriveSearch());
127 }
128
129 /***
130 * Sets the source directory.
131 *
132 * @param stickDir
133 * sets the source directory.
134 */
135 public void setSourceDir(File stickDir) {
136 prop.setProperty(sourceDir, stickDir.getAbsolutePath());
137 prop.setProperty(sourceDrive, getWindowsDrive(stickDir));
138 }
139
140 /***
141 * Flag to switch source drive name search on or off.
142 *
143 * @return true if it should search the drive name sensitive, false if the
144 * drive letter should be used.
145 */
146 public boolean isSourceDriveSearch() {
147 return prop.getProperty(sourceSearchDrive, "false").equals("true") ? true
148 : false;
149 }
150
151 /***
152 * Flag to set source drive name search on or off.
153 *
154 * @param search
155 * set true if the drive should be searched by name, false
156 * otherwise.
157 */
158 public void setSourceDriveSearch(boolean search) {
159 prop.setProperty(sourceSearchDrive, search ? "true" : "false");
160 }
161
162 /***
163 * Get the destination directory.
164 *
165 * @return the deatination directory.
166 */
167 public File getDestinationDir() {
168 return getCorrectDrive(destinationDir, destinationDrive,
169 isDestinationDriveSearch());
170 }
171
172 /***
173 * Sets the destination directory.
174 *
175 * @param stickDir
176 * sets the destination directory.
177 */
178 public void setDestinationDir(File backupDir) {
179 prop.setProperty(destinationDir, backupDir.getAbsolutePath());
180 prop.setProperty(destinationDrive, getWindowsDrive(backupDir));
181 }
182
183 /***
184 * Flag to switch destination drive name search on or off.
185 *
186 * @return true if it should search the drive name sensitive, false if the
187 * drive letter should be used.
188 */
189 public boolean isDestinationDriveSearch() {
190 return prop.getProperty(destinationSearchDrive, "false").equals("true") ? true
191 : false;
192 }
193
194 /***
195 * Flag to set destination drive name search on or off.
196 *
197 * @param search
198 * set true if the drive should be searched by name, false
199 * otherwise.
200 */
201 public void setDestinationDriveSearch(boolean search) {
202 prop.setProperty(destinationSearchDrive, search ? "true" : "false");
203 }
204
205 /***
206 * Get the color of new source files and directories.
207 *
208 * @return color for new files.
209 */
210 public Color getSourceColor() {
211 return new Color(Integer.parseInt(prop.getProperty(sourceColor, String
212 .valueOf(defaultSourceColor))));
213 }
214
215 /***
216 * Sets the color for new source files.
217 *
218 * @param color
219 * color for new files.
220 */
221 public void setSourceColor(Color color) {
222 prop.setProperty(sourceColor, String.valueOf(color.getRGB()));
223 }
224
225 /***
226 * Get the color of destination files and directories to archive.
227 *
228 * @return color for archiving files.
229 */
230 public Color getDestinationColor() {
231 return new Color(Integer.parseInt(prop.getProperty(destinationColor,
232 String.valueOf(defaultDestinationColor))));
233 }
234
235 /***
236 * Set the color of destination files and directories to archive.
237 *
238 * @param color
239 * color for archiving files.
240 */
241 public void setDestinationColor(Color color) {
242 prop.setProperty(destinationColor, String.valueOf(color.getRGB()));
243 }
244
245 /***
246 * Get the color of changed files and directories.
247 *
248 * @return color for changed files.
249 */
250 public Color getChangedColor() {
251 return new Color(Integer.parseInt(prop.getProperty(changedColor, String
252 .valueOf(defaultChangedColor))));
253 }
254
255 /***
256 * Set the color of changed files and directories.
257 *
258 * @param color
259 * color for changed files.
260 */
261 public void setChangedColor(Color color) {
262 prop.setProperty(changedColor, String.valueOf(color.getRGB()));
263 }
264
265 /***
266 * Get the color for files and directories on which the process is working
267 * on.
268 *
269 * @return color for "working on" files.
270 */
271 public Color getWorkingColor() {
272 return new Color(Integer.parseInt(prop.getProperty(workingColor, String
273 .valueOf(defaultWorkingColor))));
274 }
275
276 /***
277 * Set the color for files and directories on which the process is working
278 * on.
279 *
280 * @param color
281 * color for "working on" files.
282 */
283 public void setWorkingColor(Color color) {
284 prop.setProperty(workingColor, String.valueOf(color.getRGB()));
285 }
286
287 /***
288 * Gets the color for finished files and direcories.
289 *
290 * @return color for finished files.
291 */
292 public Color getFinishedColor() {
293 return new Color(Integer.parseInt(prop.getProperty(finishedColor,
294 String.valueOf(defaultFinishedColor))));
295 }
296
297 /***
298 * Sets the color for finished files and direcories.
299 *
300 * @param color
301 * color for finished files.
302 */
303 public void setFinishedColor(Color color) {
304 prop.setProperty(finishedColor, String.valueOf(color.getRGB()));
305 }
306
307 /***
308 * Gets the name of the drive of this file or directory.
309 *
310 * @param file
311 * the file or directory to look at.
312 * @return the name of the drive.
313 */
314 private String getWindowsDrive(File file) {
315 String result = null;
316 if (isWindows()) {
317 try {
318 String drive = file.getAbsolutePath().substring(0, 3);
319 result = ShellFolder.getShellFolder(new File(drive))
320 .getDisplayName();
321 } catch (FileNotFoundException e) {
322
323 }
324 }
325 return result;
326 }
327
328 /***
329 * Searches the letter of the drive with the name and returns the directory /
330 * file with the matching drive name.
331 * TODO: Test if name of drive is more than once there...
332 *
333 * @param dirkey
334 * configured key of the directory (search or destination)
335 * @param drivekey
336 * configured key of drive name to search.
337 * @param searchkey
338 * true if it should search the drive letter with the drive name,
339 * false otherwise.
340 * @return
341 */
342 private File getCorrectDrive(String dirkey, String drivekey,
343 boolean searchkey) {
344 String drivename = null;
345 String absolutepath = prop.getProperty(dirkey, getDefaultDirectory());
346 String property = prop.getProperty(drivekey, getWindowsDrive(new File(
347 getDefaultDirectory())));
348 if (searchkey && isWindows() && null != property) {
349 try {
350 drivename = ShellFolder.getShellFolder(
351 new File(absolutepath.substring(0, 3)))
352 .getDisplayName();
353 if (drivename.equals(property))
354 return new File(absolutepath);
355 } catch (FileNotFoundException e) {
356
357
358
359
360 }
361 property = property.substring(0, property.indexOf('('));
362 for (File drive : ShellFolder.listRoots()) {
363 try {
364 drivename = ShellFolder.getShellFolder(drive)
365 .getDisplayName();
366 drivename = drivename.substring(0, drivename.indexOf('('));
367 if (drivename.equals(property)) {
368 prop.setProperty(drivekey, getWindowsDrive(drive));
369 prop.setProperty(dirkey, drive.getAbsolutePath()
370 + absolutepath.substring(3));
371 save();
372 return new File(prop.getProperty(dirkey));
373 }
374 } catch (FileNotFoundException e) {
375
376
377
378 } catch (IOException e) {
379 throw new ConfigException("Config saving failed.", e);
380 }
381 }
382 }
383 return new File(absolutepath);
384 }
385
386 /***
387 * Gets the home directory of the user as the default directory if no
388 * directory is set.
389 *
390 * @return default directory (user home).
391 */
392 private String getDefaultDirectory() {
393 return System.getProperty("user.home");
394 }
395 }