summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/search/desktop/kde/KdeSettingsParser.java
blob: 904528e40d052f042050e4263292c5ce69e332b1 (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
130
131
132
package com.btr.proxy.search.desktop.kde;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import com.btr.proxy.util.Logger;
import com.btr.proxy.util.Logger.LogLevel;

/*****************************************************************************
 * Parser for the KDE settings file.
 * The KDE proxy settings are stored in the file:
 * <p>
 * <i>.kde/share/config/kioslaverc</i>
 * </p>
 * in the users home directory.
 *
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class KdeSettingsParser {

    private File settingsFile;

	/*************************************************************************
	 * Constructor
	 ************************************************************************/

	public KdeSettingsParser() {
		this(null);
	}

	/*************************************************************************
	 * Constructor
	 ************************************************************************/

	public KdeSettingsParser(File settingsFile) {
		super();
        this.settingsFile = settingsFile;
	}

	/*************************************************************************
	 * Parse the settings file and extract all network.proxy.* settings from it.
	 * @return the parsed properties.
	 * @throws IOException on read error.
	 ************************************************************************/

	public Properties parseSettings() throws IOException {
		// Search for existing settings.
        if (this.settingsFile == null) {
            this.settingsFile = findSettingsFile();
        }
		if (this.settingsFile == null) {
			return null;
		}

		// Read settings from file.
		BufferedReader fin = new BufferedReader(
				new InputStreamReader(
					new FileInputStream(this.settingsFile)));

		Properties result = new Properties();
		try {
			String line = fin.readLine();

			// Find section start.
			while (line != null && !"[Proxy Settings]".equals(line.trim())) {
				line = fin.readLine();
			}
			if (line == null) {
				return result;
			}

			// Read full section
			line = "";
			while (line != null && !line.trim().startsWith("[")) {
				line = line.trim();
				int index = line.indexOf('=');
				if (index > 0) {
					String key = line.substring(0, index).trim();
					String value = line.substring(index+1).trim();
					result.setProperty(key, value);
				}

				line = fin.readLine();
			}
		} finally {
			fin.close();
		}

		return result;
	}

	/*************************************************************************
	 * Finds all the KDE network settings file.
	 * @return a file or null if does not exist.
	 ************************************************************************/

	private File findSettingsFile() {
		File userDir = new File(System.getProperty("user.home"));
		if ("4".equals(System.getenv("KDE_SESSION_VERSION"))) {
	        this.settingsFile = findSettingsFile(
	                new File(userDir, ".kde4"+File.separator+"share"+File.separator+"config"+File.separator+"kioslaverc"));
		}
        if (this.settingsFile == null) {
            return findSettingsFile(
                new File(userDir, ".kde"+File.separator+"share"+File.separator+"config"+File.separator+"kioslaverc"));
        } else {
            return this.settingsFile;
        }
	}

    /*************************************************************************
     * Internal method to test if the settings file is at the given place.
     * @param settingsFile the path to test.
     * @return the file or null if it does not exist.
     ************************************************************************/
    
    private File findSettingsFile(File settingsFile) {
        Logger.log(getClass(), LogLevel.TRACE, "Searching Kde settings in {0}", settingsFile);
        if (!settingsFile.exists()) {
            Logger.log(getClass(), LogLevel.DEBUG, "Settings not found");
            return null;
        }
        Logger.log(getClass(), LogLevel.TRACE, "Settings found");
        return settingsFile;
    }

}