summaryrefslogtreecommitdiff
path: root/cmpcj/src/de/schwarzrot/control/config/ConfigFactory.java
blob: edf438a7114fc852b4facdb998789a46345bed55 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
 * ======================== legal notice ======================
 * 
 * File: ConfigFactory.java Created: 13. June 2012, 04:57 Author: <a
 * href="mailto:geronimo013@gmx.de">Geronimo</a> Project: cmpc - a java frontend
 * (client) part of compound media player uses external players to play the
 * media
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any
 * workstation without the need to export or mount shares. cmps is an easy to
 * use backend with a (ready to use) HTML-interface. Additionally the backend
 * supports authentication via HTTP-digest authorization. cmpc is a client with
 * vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved! published under
 * Creative Commons by-sa For details see
 * http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at
 * http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
package de.schwarzrot.control.config;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.schwarzrot.media.domain.Config;
import de.schwarzrot.media.domain.Media;
import de.schwarzrot.media.domain.MediaServer;
import de.schwarzrot.media.domain.PlayerDefinition;


public class ConfigFactory {
    protected static final Pattern SETUP_MASK = Pattern.compile("^\\s*(\\S+)\\s*=\\s*(.+)$");


    public ConfigFactory(File configDirectory, String applicationID) {
        baseDir = configDirectory;
        appID = applicationID;
    }


    public Config getConfig() {
        if (config == null) {
            config = readConfig();
        }
        return config;
    }


    public File getConfigDirectory() {
        return baseDir;
    }


    public void putConfig(Config config) {
        if (config != null)
            writeConfig(config);
    }


    protected Config readConfig() {
        File setupFile = new File(baseDir, "srclient.conf");
        BufferedReader br = null;
        Config rv = new Config();

        if (setupFile.exists()) {
            try {
                br = new BufferedReader(new FileReader(setupFile));
                String line, trimmedLine;

                while ((line = br.readLine()) != null) {
                    trimmedLine = line.trim();

                    if (trimmedLine.isEmpty() || trimmedLine.startsWith("#"))
                        continue;
                    Matcher m = SETUP_MASK.matcher(trimmedLine);

                    if (m.matches()) {
                        String key = m.group(1);
                        String value = m.group(2);

                        if (key.compareToIgnoreCase("server") == 0) {
                            String[] parts = value.split("\\s*\\:\\s*");
                            MediaServer ms = null;

                            if (parts.length > 1) {
                                ms = new MediaServer();
                                ms.setHostName(parts[0]);
                                ms.setPort(Integer.decode(parts[1]));

                                rv.addServer(ms);
                            }
                        } else if (key.compareToIgnoreCase("player") == 0) {
                            String[] parts = value.split("\\s*\\:\\s*");
                            PlayerDefinition pd = null;

                            if (parts.length > 1) {
                                pd = new PlayerDefinition();

                                pd.setCommandLine(parts[1]);
                                if (parts[0].compareTo("*") == 0) {
                                    // default player
                                    for (Media.SupportedMediaType cmt : Media.SupportedMediaType.values())
                                        rv.putPlayer(cmt, pd);
                                } else {
                                    for (Media.SupportedMediaType cmt : Media.SupportedMediaType.values()) {
                                        if (parts[0].compareTo(cmt.name()) == 0)
                                            rv.putPlayer(cmt, pd);
                                    }
                                }
                            }
                        } else if (key.compareToIgnoreCase("size") == 0) {
                            String[] parts = value.split("\\s*x\\s*");

                            if (parts.length > 1) {
                                try {
                                    rv.setMainWidth(Integer.decode(parts[0]));
                                } catch (Throwable t) {
                                }
                                try {
                                    rv.setMainHeight(Integer.decode(parts[1]));
                                } catch (Throwable t) {
                                }
                            }
                        } else if (key.compareToIgnoreCase("pos") == 0) {
                            String[] parts = value.split("\\s*\\/\\s*");

                            if (parts.length > 1) {
                                try {
                                    rv.setPosX(Integer.decode(parts[0]));
                                } catch (Throwable t) {
                                }
                                try {
                                    rv.setPosY(Integer.decode(parts[1]));
                                } catch (Throwable t) {
                                }
                            }
                        }
                    }
                }
            } catch (Throwable t) {
                t.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
        return rv;
    }


    protected void writeConfig(Config config) {
        File setupFile = new File(baseDir, "srclient.conf");
        PrintWriter out = null;
        StringBuilder sb = null;

        if (!setupFile.exists())
            setupFile.getParentFile().mkdirs();
        try {
            out = new PrintWriter(new FileWriter(setupFile));

            sb = new StringBuilder("#\n# configuration for ");
            sb.append(appID).append("\n#");
            out.println(sb.toString());
            for (MediaServer server : config.getKnownServers()) {
                sb = new StringBuilder("server = ");
                sb.append(server.getHostName()).append(":").append(server.getPort());
                out.println(sb.toString());
            }
            out.println();
            sb = new StringBuilder("#\n# currently supported media types (");
            sb.append(Media.SupportedMediaType.LegacyVdrRecording.name());
            sb.append(" is the old PES format):\n");
            sb.append("# ");
            sb.append("#");
            out.println(sb.toString());
            for (Media.SupportedMediaType mt : config.getPlayerMap().keySet()) {
                PlayerDefinition player = config.getPlayerMap().get(mt);

                sb = new StringBuilder("player = ");
                sb.append(mt.name()).append(" : ").append(player.getExecutable());
                for (String p : player.getParameters()) {
                    sb.append(" ").append(p);
                }
                out.println(sb.toString());
            }
            out.println();
            sb = new StringBuilder("size = ");
            sb.append(config.getMainWidth()).append(" x ").append(config.getMainHeight());
            out.println(sb.toString());

            sb = new StringBuilder("pos = ");
            sb.append(config.getPosX()).append(" / ").append(config.getPosY());
            out.println(sb.toString());
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            if (out != null)
                out.close();
        }
    }
    private Config config;
    private File baseDir;
    private final String appID;
}