1 /*
2  * @(#)Settings.java - holds all data of interest to re-use
3  *
4  * Copyright (c) 2005
5  *
6  * This file is part of ProjectX, a free Java based demux utility.
7  * By the authors, ProjectX is intended for educational purposes only,
8  * as a non-commercial test project.
9  *
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  */
26 
27 /*
28  * initiated by pstorch
29  */
30 
31 package net.sourceforge.dvb.projectx.common;
32 
33 import java.io.BufferedReader;
34 import	java.io.ByteArrayOutputStream;
35 import java.io.InputStreamReader;
36 import java.io.FileReader;
37 import java.io.FileWriter;
38 import java.io.IOException;
39 import java.io.PrintWriter;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Properties;
46 import java.util.Set;
47 import java.util.TreeMap;
48 
49 import net.sourceforge.dvb.projectx.xinput.XInputDirectory;
50 
51 /**
52  * The Settings class handles the settings for Project-X.
53  *
54  * @author Peter Storch
55  */
56 public class Settings extends Object {
57 
58 	/** the default ini filename */
59 	private static final String DEFAULT_INI = "X.ini";
60 
61 	/** the current ini filename */
62 	private String inifile = "";
63 
64 	/** all settings are being hold in this properties object */
65 	private Properties props = new Properties();
66 
67 	/**  */
68 	private ArrayList input_directories = new ArrayList();
69 
70 	/**  */
71 	private ArrayList output_directories = new ArrayList();
72 
73 	/**
74 	 * Constructor
75 	 */
Settings()76 	public Settings()
77 	{
78 		this(Resource.workdir + Resource.filesep + DEFAULT_INI);
79 	}
80 
81 	/**
82 	 * Constructor
83 	 *
84 	 * @param filename
85 	 */
Settings(String filename)86 	public Settings(String filename)
87 	{
88 		inifile = filename;
89 		load();
90 		buildInputDirectories();
91 		buildOutputDirectories();
92 	}
93 
94 
95 	/**
96 	 *
97 	 */
loadProperties(java.io.ByteArrayInputStream is)98 	public void loadProperties(java.io.ByteArrayInputStream is) throws IOException
99 	{
100 		props.load(is);
101 	}
102 
103 	/**
104 	 *
105 	 */
storeProperties()106 	public byte[] storeProperties() throws IOException
107 	{
108 		ByteArrayOutputStream os = new ByteArrayOutputStream();
109 		props.store(os, null);
110 
111 		return os.toByteArray();
112 	}
113 
114 	/**
115 	 * Loads the ini file.
116 	 */
load()117 	public void load()
118 	{
119 		try {
120 			BufferedReader r = new BufferedReader(new FileReader(inifile));
121 
122 			String line = null;
123 
124 			while ((line = r.readLine()) != null)
125 			{
126 				if (line.startsWith("#"))
127 					continue;
128 
129 				int pos = line.indexOf('=');
130 
131 				if (pos != -1)
132 				{
133 					String key = line.substring(0, pos);
134 					String value = line.substring(pos + 1);
135 					props.put(key, value);
136 				}
137 			}
138 
139 			r.close();
140 
141 		} catch(IOException e) {
142 
143 			System.out.println(Resource.getString("msg.loadini.error") + " " + e);
144 		}
145 	}
146 
147 	/**
148 	 * Saves the ini file.
149 	 */
save()150 	public void save()
151 	{
152 		save(inifile);
153 	}
154 
155 	/**
156 	 * Saves the ini file (std or extra name)
157 	 */
save(String str)158 	public void save(String str)
159 	{
160 		if (str == null)
161 			str = inifile;
162 
163 		try {
164 			PrintWriter w = new PrintWriter(new FileWriter(str));
165 
166 			String base_key = "# Project-X INI";
167 
168 			w.println(base_key);
169 
170 			w.println("# " + Common.getVersionName() + " / " + Common.getVersionDate());
171 
172 			TreeMap map = new TreeMap(props);
173 
174 			Set keys = map.keySet();
175 
176 			for (Iterator iter = keys.iterator(); iter.hasNext(); )
177 			{
178 				String element = (String) iter.next();
179 
180 				String key = element.substring(0, element.indexOf("."));
181 
182 				if (!base_key.equals(key))
183 				{
184 					w.println();
185 					w.println("# " + key);
186 					base_key = key;
187 				}
188 
189 				w.println(element + "=" + map.get(element));
190 			}
191 
192 			w.close();
193 
194 		} catch(IOException e) {
195 
196 			Common.setExceptionMessage(e);
197 		}
198 	}
199 
200 	/**
201 	 * build input_directory Xinput object from property list (strings only)
202 	 */
buildInputDirectories()203 	public void buildInputDirectories()
204 	{
205 		String key = Keys.KEY_InputDirectories;
206 
207 		ArrayList input = (ArrayList)getListProperty(key);
208 
209 		int i = 0;
210 
211 		for ( ; i < input.size(); i++)
212 		{
213 			if (addInputDirectory(input.get(i)) == null)
214 			{
215 				input.remove(i);
216 				i--;
217 			}
218 		}
219 
220 		if (i != input.size())
221 			setListProperty(key, input);
222 	}
223 
224 
225 	/**
226 	 * updates property list (strings only)
227 	 */
updateInputDirectories()228 	public void updateInputDirectories()
229 	{
230 		ArrayList input = new ArrayList();
231 
232 		for (int i = 0; i < input_directories.size(); i++)
233 			input.add( input_directories.get(i).toString());
234 
235 		setListProperty(Keys.KEY_InputDirectories, input);
236 	}
237 
238 	/**
239 	 * adds input_directory Xinput object
240 	 */
addInputDirectory(Object value)241 	public String addInputDirectory(Object value)
242 	{
243 		if (value == null)
244 			return null;
245 
246 		try {
247 			XInputDirectory xInputDirectory = (XInputDirectory) value;
248 
249 			input_directories.add(xInputDirectory);
250 
251 			return xInputDirectory.toString();
252 
253 		} catch (Exception e) {
254 			// is not yet xinput object
255 		}
256 
257 		try {
258 			XInputDirectory xInputDirectory = new XInputDirectory(value);
259 
260 			if (xInputDirectory.test())
261 			{
262 				input_directories.add(xInputDirectory);
263 
264 				return xInputDirectory.toString();
265 			}
266 
267 		} catch (RuntimeException e) {
268 			// If there are problems with the directory simply ignore it and do nothing
269 		}
270 
271 		return null;
272 	}
273 
274 	/**
275 	 * removes input_directory Xinput object
276 	 */
removeInputDirectory(int index)277 	public void removeInputDirectory(int index)
278 	{
279 		if (index < 0 || input_directories.isEmpty() || input_directories.size() <= index)
280 			return;
281 
282 		input_directories.remove(index);
283 	}
284 
285 	/**
286 	 * returns input_directory Xinput objects
287 	 */
getInputDirectories()288 	public ArrayList getInputDirectories()
289 	{
290 		return input_directories;
291 	}
292 
293 
294 	/**
295 	 * build output_directory  from property list (strings only)
296 	 */
buildOutputDirectories()297 	public void buildOutputDirectories()
298 	{
299 		String key = Keys.KEY_OutputDirectories;
300 
301 		ArrayList output = (ArrayList)getListProperty(key);
302 
303 		int i = 0;
304 
305 		for ( ; i < output.size(); i++)
306 			addOutputDirectory(output.get(i));
307 
308 		if (i != output.size())
309 			setListProperty(key, output);
310 	}
311 
312 
313 	/**
314 	 * updates property list (strings only)
315 	 */
updateOutputDirectories()316 	public void updateOutputDirectories()
317 	{
318 		ArrayList output = new ArrayList();
319 
320 		for (int i = 0; i < output_directories.size(); i++)
321 			output.add( output_directories.get(i).toString());
322 
323 		setListProperty(Keys.KEY_OutputDirectories, output);
324 	}
325 
326 
327 	/**
328 	 * adds output_directory string
329 	 */
addOutputDirectory(Object value)330 	public void addOutputDirectory(Object value)
331 	{
332 		addOutputDirectory(value, -1);
333 	}
334 
335 	/**
336 	 * adds output_directory string
337 	 */
addOutputDirectory(Object value, int index)338 	public void addOutputDirectory(Object value, int index)
339 	{
340 		if (value == null)
341 			return;
342 
343 		if (index < 0 || index > output_directories.size())
344 			output_directories.add(value);
345 		else
346 			output_directories.add(index, value);
347 
348 		updateOutputDirectories();
349 	}
350 
351 	/**
352 	 * removes output_directory string
353 	 */
removeOutputDirectory(int index)354 	public void removeOutputDirectory(int index)
355 	{
356 		if (index < 0 || output_directories.size() < index - 1)
357 			return;
358 
359 		output_directories.remove(index);
360 
361 		updateOutputDirectories();
362 	}
363 
364 	/**
365 	 * returns output_directory strings
366 	 */
getOutputDirectories()367 	public ArrayList getOutputDirectories()
368 	{
369 		return output_directories;
370 	}
371 
372 	/**
373 	 * Sets a String property.
374 	 *
375 	 * @param key
376 	 * @param value
377 	 * @return
378 	 */
setProperty(String key, String value)379 	public void setProperty(String key, String value)
380 	{
381 		if (value != null)
382 			props.setProperty(key, value);
383 
384 		else
385 			props.remove(key);
386 	}
387 
388 	/**
389 	 * Sets a Object property.
390 	 *
391 	 * @param key
392 	 * @param value
393 	 * @return
394 	 */
setProperty(String key, Object value)395 	public void setProperty(String key, Object value)
396 	{
397 		String str = value == null ? null : String.valueOf(value);
398 
399 		setProperty(key, str);
400 	}
401 
402 	/**
403 	 * Sets a Object property.
404 	 *
405 	 * @param key of obj
406 	 * @param value
407 	 * @return
408 	 */
setProperty(Object[] key, Object value)409 	public void setProperty(Object[] key, Object value)
410 	{
411 		setProperty(key[0].toString(), String.valueOf(value));
412 	}
413 
414 	/**
415 	 * Returns a String property.
416 	 *
417 	 * @param key
418 	 * @return
419 	 */
getProperty(String key)420 	public String getProperty(String key)
421 	{
422 		return props.getProperty(key);
423 	}
424 
425 	/**
426 	 * Returns a String property. If not found it returns the given default value.
427 	 *
428 	 * @param key
429 	 * @param defaultValue
430 	 * @return
431 	 */
getProperty(String key, String defaultValue)432 	public String getProperty(String key, String defaultValue)
433 	{
434 		return props.getProperty(key, defaultValue);
435 	}
436 
437 	/**
438 	 * Returns a String property. If not found it returns the given default value.
439 	 *
440 	 * @param key
441 	 * @param defaultValue
442 	 * @return
443 	 */
getProperty(String[] key_defaultValue)444 	public String getProperty(String[] key_defaultValue)
445 	{
446 		String key = key_defaultValue[0];
447 		String defaultValue = key_defaultValue[1];
448 
449 		return props.getProperty(key, defaultValue);
450 	}
451 
452 	/**
453 	 * Returns an integer property.
454 	 *
455 	 * @param key
456 	 * @param value
457 	 * @return
458 	 */
setIntProperty(String key, int value)459 	public void setIntProperty(String key, int value)
460 	{
461 		props.setProperty(key, String.valueOf(value));
462 	}
463 
464 	/**
465 	 * Returns an integer property.
466 	 *
467 	 * @param key
468 	 * @return
469 	 */
getIntProperty(String key)470 	public int getIntProperty(String key)
471 	{
472 		return Integer.parseInt(props.getProperty(key));
473 	}
474 
475 	/**
476 	 * Returns an integer property. If not found or not parseable it returns the default value.
477 	 *
478 	 * @param key
479 	 * @param defaultValue
480 	 * @return
481 	 */
getIntProperty(String key, int defaultValue)482 	public int getIntProperty(String key, int defaultValue)
483 	{
484 		int value = defaultValue;
485 
486 		try {
487 			value = Integer.parseInt(getProperty(key));
488 
489 		} catch(Exception e) {
490 			// ok, then we stay with the default value
491 		}
492 
493 		return value;
494 	}
495 
496 	/**
497 	 * Returns an integer property. If not found or not parseable it returns the default value.
498 	 *
499 	 * @param key
500 	 * @param defaultValue
501 	 * @return
502 	 */
getIntProperty(String[] key_defaultValue)503 	public int getIntProperty(String[] key_defaultValue)
504 	{
505 		String key = key_defaultValue[0];
506 		int value = Integer.parseInt(key_defaultValue[1]);
507 
508 		try {
509 			value = Integer.parseInt(getProperty(key));
510 
511 		} catch(Exception e) {
512 			// ok, then we stay with the default value
513 		}
514 
515 		return value;
516 	}
517 
518 	/**
519 	 * Returns a boolean property.
520 	 * Default is false.
521 	 *
522 	 * @param key
523 	 * @return
524 	 */
setBooleanProperty(String key, boolean value)525 	public void setBooleanProperty(String key, boolean value)
526 	{
527 		props.setProperty(key, value ? "1" : "0");
528 	}
529 
530 	/**
531 	 * Returns a Boolean property.
532 	 * Null if key not found. Default is false.
533 	 *
534 	 * @param key
535 	 * @return
536 	 */
getBooleanProperty(String key)537 	public Boolean getBooleanProperty(String key)
538 	{
539 		String value = getProperty(key);
540 
541 		if (value == null)
542 			return null;
543 
544 		else if (value.equals("1") || value.equals("true") || value.equals("yes") || value.equals("on"))
545 			return Boolean.TRUE;
546 
547 		return Boolean.FALSE;
548 	}
549 
550 	/**
551 	 * Returns a Boolean property or the given defaultValue.
552 	 *
553 	 * @param key
554 	 * @param defaultValue
555 	 * @return
556 	 */
getBooleanProperty(String key, boolean defaultValue)557 	public boolean getBooleanProperty(String key, boolean defaultValue)
558 	{
559 		Boolean value = getBooleanProperty(key);
560 
561 		if (value == null)
562 			return defaultValue;
563 
564 		return value.booleanValue();
565 	}
566 
567 	/**
568 	 * Returns a Boolean property or the given defaultValue.
569 	 *
570 	 * @param key
571 	 * @param defaultValue
572 	 * @return
573 	 */
getBooleanProperty(String[] key_defaultValue)574 	public boolean getBooleanProperty(String[] key_defaultValue)
575 	{
576 		String key = key_defaultValue[0];
577 		String defaultValue = key_defaultValue[1];
578 
579 		Boolean value = getBooleanProperty(key);
580 
581 		if (value != null)
582 			return value.booleanValue();
583 
584 		if (defaultValue.equals("1") || defaultValue.equals("true") || defaultValue.equals("yes") || defaultValue.equals("on"))
585 			return true;
586 
587 		return false;
588 	}
589 
590 	/**
591 	 * Sets a List of properties starting with key.
592 	 *
593 	 * @param key
594 	 * @param list
595 	 * @return
596 	 */
setListProperty(String key, List list)597 	public void setListProperty(String key, List list)
598 	{
599 		removeListProperty(key);
600 
601 		for (int i = 0; i < list.size(); i++)
602 		{
603 			Object element = list.get(i);
604 			setProperty(key + i, element);
605 		}
606 	}
607 
608 	/**
609 	 * Gets a List of properties starting with key.
610 	 *
611 	 * @param key
612 	 * @return
613 	 */
getListProperty(String key)614 	public List getListProperty(String key)
615 	{
616 		Set keys = props.keySet();
617 
618 		List list = new ArrayList();
619 
620 		for (Iterator iter = keys.iterator(); iter.hasNext(); )
621 		{
622 			String element = (String) iter.next();
623 
624 			if (element.startsWith(key))
625 				list.add(props.getProperty(element));
626 		}
627 
628 		return list;
629 	}
630 
631 	/**
632 	 * removes a List of properties starting with key.
633 	 *
634 	 * @param key
635 	 * @return
636 	 */
removeListProperty(String key)637 	public void removeListProperty(String key)
638 	{
639 		Set keys = props.keySet();
640 
641 		ArrayList list = new ArrayList();
642 
643 		for (Iterator iter = keys.iterator(); iter.hasNext(); )
644 		{
645 			String element = (String) iter.next();
646 
647 			if (element.startsWith(key))
648 				list.add(element);
649 		}
650 
651 		for (int i = 0; i < list.size(); i++)
652 			remove(list.get(i).toString());
653 	}
654 
655 	/**
656 	 * Sets a Map of properties starting with key.
657 	 *
658 	 * @param key
659 	 * @param map
660 	 * @return
661 	 */
setHashMapProperty(String key, Map map)662 	public void setHashMapProperty(String key, Map map)
663 	{
664 		Set keys = map.keySet();
665 
666 		for (Iterator iter = keys.iterator(); iter.hasNext(); )
667 		{
668 			String element = (String) iter.next();
669 			setProperty(key + element, map.get(element));
670 		}
671 	}
672 
673 	/**
674 	 * Gets a Map of properties starting with key.
675 	 * The Map contains the entries with a new key like this:
676 	 * mapKey = propsKey - key.
677 	 *
678 	 * @param key
679 	 * @return
680 	 */
getHashMapProperty(String key)681 	public Map getHashMapProperty(String key)
682 	{
683 		Set keys = props.keySet();
684 		Map map = new HashMap();
685 
686 		for (Iterator iter = keys.iterator(); iter.hasNext();)
687 		{
688 			String element = (String) iter.next();
689 
690 			if (element.startsWith(key))
691 				map.put(element.substring(key.length()), props.getProperty(element));
692 		}
693 
694 		return map;
695 	}
696 
697 	/**
698 	 * Removes a property.
699 	 *
700 	 * @param key
701 	 */
remove(String key)702 	public void remove(String key)
703 	{
704 		props.remove(key);
705 	}
706 
707 	/**
708 	 * Returns the ini filename.
709 	 *
710 	 * @return
711 	 */
getInifile()712 	public String getInifile()
713 	{
714 		return inifile;
715 	}
716 
717 }