Debian Codegraph @

src/main/java/com/github/markusbernhardt/proxy/search/desktop/kde/KdeSettingsParser.java

raw symbols SCIP
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 * Parser for the KDE settings file. The KDE proxy settings are stored in the
16 * file:
17 * <p>
18 * <i>.kde/share/config/kioslaverc</i>
19 * </p>
20 * in the users home directory.
21 *
22 * @author Markus Bernhardt, Copyright 2016
23 * @author Bernd Rosstauscher, Copyright 2009
24 ****************************************************************************/
25
26 public class KdeSettingsParser {
27
28 private File settingsFile;
29
30 /*************************************************************************
31 * Constructor
32 ************************************************************************/
33
34 public KdeSettingsParser() {
35 this(null);
36 }
37
38 /*************************************************************************
39 * Constructor
40 *
41 * @param settingsFile
42 * the settings file to use.
43 ************************************************************************/
44
45 public KdeSettingsParser(File settingsFile) {
46 super();
47 this.settingsFile = settingsFile;
48 }
49
50 /*************************************************************************
51 * Parse the settings file and extract all network.proxy.* settings from it.
52 *
53 * @return the parsed properties.
54 * @throws IOException
55 * on read error.
56 ************************************************************************/
57
58 public Properties parseSettings() throws IOException {
59 // Search for existing settings.
60 if (this.settingsFile == null) {
61 this.settingsFile = findSettingsFile();
62 }
63 if (this.settingsFile == null) {
64 return null;
65 }
66
67 // Read settings from file.
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 // Find section start.
75 while (line != null && !"[Proxy Settings]".equals(line.trim())) {
76 line = fin.readLine();
77 }
78 if (line == null) {
79 return result;
80 }
81
82 // Read full section
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 * Finds all the KDE network settings file.
104 *
105 * @return a file or null if does not exist.
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 * Internal method to test if the settings file is at the given place.
124 *
125 * @param settingsFile
126 * the path to test.
127 * @return the file or null if it does not exist.
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 }