altoslib/altosui: begin moving preferences "backend" into interface
[fw/altos] / altoslib / AltosPreferences.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.io.*;
21 import java.util.*;
22 import javax.swing.filechooser.FileSystemView;
23
24 public class AltosPreferences {
25         public static AltosPreferencesBackend backend = null;
26
27         /* logdir preference name */
28         public final static String logdirPreference = "LOGDIR";
29
30         /* channel preference name */
31         public final static String channelPreferenceFormat = "CHANNEL-%d";
32
33         /* frequency preference name */
34         public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
35
36         /* telemetry format preference name */
37         public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
38
39         /* voice preference name */
40         public final static String voicePreference = "VOICE";
41
42         /* callsign preference name */
43         public final static String callsignPreference = "CALLSIGN";
44
45         /* firmware directory preference name */
46         public final static String firmwaredirPreference = "FIRMWARE";
47
48         /* serial debug preference name */
49         public final static String serialDebugPreference = "SERIAL-DEBUG";
50
51         /* scanning telemetry preferences name */
52         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
53
54         /* Launcher serial preference name */
55         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
56
57         /* Launcher channel preference name */
58         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
59         
60         /* Default logdir is ~/TeleMetrum */
61         public final static String logdirName = "TeleMetrum";
62
63         /* Log directory */
64         public static File logdir;
65
66         /* Map directory -- hangs of logdir */
67         public static File mapdir;
68
69         /* Frequency (map serial to frequency) */
70         public static Hashtable<Integer, Double> frequencies;
71
72         /* Telemetry (map serial to telemetry format) */
73         public static Hashtable<Integer, Integer> telemetries;
74
75         /* Voice preference */
76         public static boolean voice;
77
78         /* Callsign preference */
79         public static String callsign;
80
81         /* Firmware directory */
82         public static File firmwaredir;
83
84         /* Scanning telemetry */
85         public static int scanning_telemetry;
86
87         /* List of frequencies */
88         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
89         public static AltosFrequency[] common_frequencies;
90
91         public final static String      frequency_count = "COUNT";
92         public final static String      frequency_format = "FREQUENCY-%d";
93         public final static String      description_format = "DESCRIPTION-%d";
94
95         /* Units preference */
96
97         public final static String      unitsPreference = "IMPERIAL-UNITS";
98
99         public static AltosFrequency[] load_common_frequencies() {
100                 AltosFrequency[] frequencies = null;
101                 boolean existing = false;
102                 existing = backend.nodeExists(common_frequencies_node_name);
103
104                 if (existing) {
105                         AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
106                         int             count = node.getInt(frequency_count, 0);
107
108                         frequencies = new AltosFrequency[count];
109                         for (int i = 0; i < count; i++) {
110                                 double  frequency;
111                                 String  description;
112
113                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
114                                 description = node.getString(String.format(description_format, i), null);
115                                 frequencies[i] = new AltosFrequency(frequency, description);
116                         }
117                 } else {
118                         frequencies = new AltosFrequency[10];
119                         for (int i = 0; i < 10; i++) {
120                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
121                                                                            String.format("Channel %d", i));
122                         }
123                 }
124                 return frequencies;
125         }
126
127         public static void save_common_frequencies(AltosFrequency[] frequencies) {
128                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
129
130                 node.putInt(frequency_count, frequencies.length);
131                 for (int i = 0; i < frequencies.length; i++) {
132                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
133                         node.putString(String.format(description_format, i), frequencies[i].description);
134                 }
135         }
136         public static int launcher_serial;
137
138         public static int launcher_channel;
139
140         public static void init() {
141                 //preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
142
143                 /* Initialize logdir from preferences */
144                 String logdir_string = backend.getString(logdirPreference, null);
145                 if (logdir_string != null)
146                         logdir = new File(logdir_string);
147                 else {
148                         /* Use the file system view default directory */
149                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
150                         if (!logdir.exists())
151                                 logdir.mkdirs();
152                 }
153                 mapdir = new File(logdir, "maps");
154                 if (!mapdir.exists())
155                         mapdir.mkdirs();
156
157                 frequencies = new Hashtable<Integer, Double>();
158
159                 telemetries = new Hashtable<Integer,Integer>();
160
161                 voice = backend.getBoolean(voicePreference, true);
162
163                 callsign = backend.getString(callsignPreference,"N0CALL");
164
165                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
166
167                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
168
169                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
170
171                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
172                 if (firmwaredir_string != null)
173                         firmwaredir = new File(firmwaredir_string);
174                 else
175                         firmwaredir = null;
176
177                 common_frequencies = load_common_frequencies();
178
179                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
180         }
181
182         static { init(); }
183
184         public static void flush_preferences() {
185                 backend.flush();
186         }
187
188         public static void set_logdir(File new_logdir) {
189                 synchronized (backend) {
190                         logdir = new_logdir;
191                         mapdir = new File(logdir, "maps");
192                         if (!mapdir.exists())
193                                 mapdir.mkdirs();
194                         backend.putString(logdirPreference, logdir.getPath());
195                         flush_preferences();
196                 }
197         }
198
199         public static File logdir() {
200                 synchronized (backend) {
201                         return logdir;
202                 }
203         }
204
205         public static File mapdir() {
206                 synchronized (backend) {
207                         return mapdir;
208                 }
209         }
210
211         public static void set_frequency(int serial, double new_frequency) {
212                 synchronized (backend) {
213                         frequencies.put(serial, new_frequency);
214                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
215                         flush_preferences();
216                 }
217         }
218
219         public static double frequency(int serial) {
220                 synchronized (backend) {
221                         if (frequencies.containsKey(serial))
222                                 return frequencies.get(serial);
223                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
224                         if (frequency == 0.0) {
225                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
226                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
227                         }
228                         frequencies.put(serial, frequency);
229                         return frequency;
230                 }
231         }
232
233         public static void set_telemetry(int serial, int new_telemetry) {
234                 synchronized (backend) {
235                         telemetries.put(serial, new_telemetry);
236                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
237                         flush_preferences();
238                 }
239         }
240
241         public static int telemetry(int serial) {
242                 synchronized (backend) {
243                         if (telemetries.containsKey(serial))
244                                 return telemetries.get(serial);
245                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
246                                                    AltosLib.ao_telemetry_standard);
247                         telemetries.put(serial, telemetry);
248                         return telemetry;
249                 }
250         }
251
252         public static void set_scanning_telemetry(int new_scanning_telemetry) {
253                 synchronized (backend) {
254                         scanning_telemetry = new_scanning_telemetry;
255                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
256                         flush_preferences();
257                 }
258         }
259
260         public static int scanning_telemetry() {
261                 synchronized (backend) {
262                         return scanning_telemetry;
263                 }
264         }
265
266         public static void set_voice(boolean new_voice) {
267                 synchronized (backend) {
268                         voice = new_voice;
269                         backend.putBoolean(voicePreference, voice);
270                         flush_preferences();
271                 }
272         }
273
274         public static boolean voice() {
275                 synchronized (backend) {
276                         return voice;
277                 }
278         }
279
280         public static void set_callsign(String new_callsign) {
281                 synchronized(backend) {
282                         callsign = new_callsign;
283                         backend.putString(callsignPreference, callsign);
284                         flush_preferences();
285                 }
286         }
287
288         public static String callsign() {
289                 synchronized(backend) {
290                         return callsign;
291                 }
292         }
293
294         public static void set_firmwaredir(File new_firmwaredir) {
295                 synchronized (backend) {
296                         firmwaredir = new_firmwaredir;
297                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
298                         flush_preferences();
299                 }
300         }
301
302         public static File firmwaredir() {
303                 synchronized (backend) {
304                         return firmwaredir;
305                 }
306         }
307
308         public static void set_launcher_serial(int new_launcher_serial) {
309                 synchronized (backend) {
310                         launcher_serial = new_launcher_serial;
311                         backend.putInt(launcherSerialPreference, launcher_serial);
312                         flush_preferences();
313                 }
314         }
315
316         public static int launcher_serial() {
317                 synchronized (backend) {
318                         return launcher_serial;
319                 }
320         }
321
322         public static void set_launcher_channel(int new_launcher_channel) {
323                 synchronized (backend) {
324                         launcher_channel = new_launcher_channel;
325                         backend.putInt(launcherChannelPreference, launcher_channel);
326                         flush_preferences();
327                 }
328         }
329
330         public static int launcher_channel() {
331                 synchronized (backend) {
332                         return launcher_channel;
333                 }
334         }
335         
336         public static AltosPreferencesBackend bt_devices() {
337                 synchronized (backend) {
338                         return backend.node("bt_devices");
339                 }
340         }
341
342         public static AltosFrequency[] common_frequencies() {
343                 synchronized (backend) {
344                         return common_frequencies;
345                 }
346         }
347
348         public static void set_common_frequencies(AltosFrequency[] frequencies) {
349                 synchronized(backend) {
350                         common_frequencies = frequencies;
351                         save_common_frequencies(frequencies);
352                         flush_preferences();
353                 }
354         }
355
356         public static void add_common_frequency(AltosFrequency frequency) {
357                 AltosFrequency[]        old_frequencies = common_frequencies();
358                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
359                 int                     i;
360
361                 for (i = 0; i < old_frequencies.length; i++) {
362                         if (frequency.frequency == old_frequencies[i].frequency)
363                                 return;
364                         if (frequency.frequency < old_frequencies[i].frequency)
365                                 break;
366                         new_frequencies[i] = old_frequencies[i];
367                 }
368                 new_frequencies[i] = frequency;
369                 for (; i < old_frequencies.length; i++)
370                         new_frequencies[i+1] = old_frequencies[i];
371                 set_common_frequencies(new_frequencies);
372         }
373
374         public static boolean imperial_units() {
375                 synchronized(backend) {
376                         return AltosConvert.imperial_units;
377                 }
378         }
379
380         public static void set_imperial_units(boolean imperial_units) {
381                 synchronized (backend) {
382                         AltosConvert.imperial_units = imperial_units;
383                         backend.putBoolean(unitsPreference, imperial_units);
384                         flush_preferences();
385                 }
386         }
387 }