summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/config/config_file.java
blob: 3d562b35f2771d44187da04db1dca81217e6165a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package config;

import java.io.File;
import java.io.IOException;

import org.ini4j.Wini;

public class config_file {
	
	// Nur die Wini als member
	private static Wini ini = null;
	
	// Konstruktor ermittelt der Pfad zur Konfigurationsdatei.
	// Unterscheidet dabei Windows/Linux
	public static void init() throws IOException {
		
		// Hauptvariablen nur lokal notwendig
		String configPath = null;
		File configFile = null;
		// Unterscheide zwischen Windows/Unix
		String OSName = System.getProperty("os.name").toLowerCase();
		System.out.println("Machine's OS: " + OSName);
		if (OSName.contains("windows")) {
			// Windows machine. Use the environment variable 'APPDATA' which
			// should point to a path similar to:
			//		C:\Users\<user>\AppData\Roaming
			String appDataPath = System.getenv("APPDATA");
			if (!appDataPath.isEmpty()) {
				configPath = appDataPath + "\\bwSuite\\config.ini";
			} else {
				// APPDATA was empty, let's build it ourselves...
				System.out.println("APPDATA ist leer.");
				configPath = System.getProperty("user.home") + "\\AppData\\Roaming\\bwSuite\\config.ini";	
			}
			
		} else if (OSName.contains("linux")) {
			configPath=System.getProperty("user.home") + "/.config/bwSuite/config.ini";
		} else {
			// Nicht Windows oder Linux, TODO MacOS Support?
			configPath = null;
		}
		
		//File fuer den Ordner erzeugen
		if (!(configPath.isEmpty()||configPath == null)) {
			configFile = new File(configPath);
		} else {
			throw new IOException("Konnte kein Pfad für die Konfigurationsdatei ermitteln.");
		}
		
		// Checke, ob der Ordner existiert.
		if (!configFile.getParentFile().exists()) {
			System.out.println("Ordner " + configFile.getParentFile() + " exisitiert nicht - lege ihn jetzt an.");
			// Wenn nicht, erzeuge Ordner
			if (!configFile.getParentFile().mkdirs()) {
				throw new IOException("Konnte '" + configFile.getParentFile() + "' nicht erstellen.");
			}
		}
		
		// Pruefen, ob Datei schon existiert
		if (!configFile.exists()) {
			// Erzeuge eine neue Datei
			configFile.createNewFile();

			// Wenn in die Datei geschrieben werden kann
			if(configFile.canWrite()) {
				ini = new Wini(configFile);
				System.out.println("Erzeuge '" + configFile + "'...");
				//Schreibe Config
				ini.put("main", "BillOfRights", false);
				ini.put("main", "vmware", false);
				ini.put("main", "Benutzername speichern", false);
				ini.put("main", "Benutzername", "");
				ini.put("main", "Letzter Downloadpfad", "");
				ini.put("main", "Letzter Uploadpfad", "");
				ini.store();
				
			} else {
				throw new IOException("Konnte nicht in '" + configFile + "' schreiben. Haben Sie Rechte dazu?");
			}
		} else {
			System.out.println("'" + configFile + "' existiert bereits - keine weitere Aktion.");
			ini = new Wini(configFile);
		}
	}
	// Ende Konstruktor.
	
	// Public funktion zur Abfrage der Boolean-Werte
	// Gibt Wert von Attribut 'key' in Sektion 'section', falls es existiert.
	// Sonst gibt es 'defaultValue' zurück.
	//
	// Usage: getBoolean(<section>, <key>, <defaultValue>)
	// Ex.:		getBoolean("main", "Benutzername", "NichtGesetzt");
	public static boolean getBoolean(String section, String key, Boolean defaultValue) {
		if (ini.containsKey(section) && ini.get(section).containsKey(key)) {
			return ini.get(section, key, Boolean.class);
		} else {
			return defaultValue;
		}
	}
	
	// Public funktion zur Abfrage der String-Werte
	// Usage: getBoolean(<key>)
	public static String getString(String section, String key, String defaultValue) {
		if (ini.containsKey(section) && ini.get(section).containsKey(key)) {
			return ini.get(section, key);
		} else {
			return defaultValue;
		}
	}
	
	public static boolean setBoolean(String section, String key, Boolean value) {
		return ini.put(section, key, value) != null;
	}
	
	public static boolean setString(String section, String key, String value) {
		return ini.put(section, key, value) != null;
	}
	
	public static boolean store() {
		try {
			ini.store();
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
}