| 1 |
package com.github.markusbernhardt.proxy.search.desktop.kde; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.File; |
| 5 |
import java.io.FileInputStream; |
| 6 |
import java.io.IOException; |
| 7 |
import java.io.InputStreamReader; |
| 8 |
import java.util.Properties; |
| 9 |
|
| 10 |
import com.github.markusbernhardt.proxy.util.Logger; |
| 11 |
import com.github.markusbernhardt.proxy.util.Logger.LogLevel; |
| 12 |
import com.github.markusbernhardt.proxy.util.PlatformUtil; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class KdeSettingsParser { |
| 27 |
|
| 28 |
private File settingsFile; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public KdeSettingsParser() { |
| 35 |
this(null); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public KdeSettingsParser(File settingsFile) { |
| 46 |
super(); |
| 47 |
this.settingsFile = settingsFile; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public Properties parseSettings() throws IOException { |
| 59 |
|
| 60 |
if (this.settingsFile == null) { |
| 61 |
this.settingsFile = findSettingsFile(); |
| 62 |
} |
| 63 |
if (this.settingsFile == null) { |
| 64 |
return null; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
BufferedReader fin = new BufferedReader(new InputStreamReader(new FileInputStream(this.settingsFile))); |
| 69 |
|
| 70 |
Properties result = new Properties(); |
| 71 |
try { |
| 72 |
String line = fin.readLine(); |
| 73 |
|
| 74 |
|
| 75 |
while (line != null && !"[Proxy Settings]".equals(line.trim())) { |
| 76 |
line = fin.readLine(); |
| 77 |
} |
| 78 |
if (line == null) { |
| 79 |
return result; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
line = ""; |
| 84 |
while (line != null && !line.trim().startsWith("[")) { |
| 85 |
line = line.trim(); |
| 86 |
int index = line.indexOf('='); |
| 87 |
if (index > 0) { |
| 88 |
String key = line.substring(0, index).trim(); |
| 89 |
String value = line.substring(index + 1).trim(); |
| 90 |
result.setProperty(key, value); |
| 91 |
} |
| 92 |
|
| 93 |
line = fin.readLine(); |
| 94 |
} |
| 95 |
} finally { |
| 96 |
fin.close(); |
| 97 |
} |
| 98 |
|
| 99 |
return result; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private File findSettingsFile() { |
| 109 |
File userDir = new File(PlatformUtil.getUserHomeDir()); |
| 110 |
if ("4".equals(System.getenv("KDE_SESSION_VERSION"))) { |
| 111 |
this.settingsFile = findSettingsFile(new File(userDir, |
| 112 |
".kde4" + File.separator + "share" + File.separator + "config" + File.separator + "kioslaverc")); |
| 113 |
} |
| 114 |
if (this.settingsFile == null) { |
| 115 |
return findSettingsFile(new File(userDir, |
| 116 |
".kde" + File.separator + "share" + File.separator + "config" + File.separator + "kioslaverc")); |
| 117 |
} else { |
| 118 |
return this.settingsFile; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private File findSettingsFile(File settingsFile) { |
| 131 |
Logger.log(getClass(), LogLevel.TRACE, "Searching Kde settings in {0}", settingsFile); |
| 132 |
if (!settingsFile.exists()) { |
| 133 |
Logger.log(getClass(), LogLevel.DEBUG, "Settings not found"); |
| 134 |
return null; |
| 135 |
} |
| 136 |
Logger.log(getClass(), LogLevel.TRACE, "Settings found"); |
| 137 |
return settingsFile; |
| 138 |
} |
| 139 |
|
| 140 |
} |