7510c7c2b289e51782aef2fa8ed48df668122a4b
[fw/altos] / altosui / 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 altosui;
19 import java.io.*;
20 import java.util.*;
21 import java.text.*;
22 import java.util.prefs.*;
23 import java.util.concurrent.LinkedBlockingQueue;
24 import java.awt.Component;
25 import javax.swing.*;
26 import javax.swing.filechooser.FileSystemView;
27
28 class AltosPreferences {
29         public static Preferences preferences;
30
31         /* logdir preference name */
32         final static String logdirPreference = "LOGDIR";
33
34         /* channel preference name */
35         final static String channelPreferenceFormat = "CHANNEL-%d";
36
37         /* frequency preference name */
38         final static String frequencyPreferenceFormat = "FREQUENCY-%d";
39
40         /* telemetry format preference name */
41         final static String telemetryPreferenceFormat = "TELEMETRY-%d";
42
43         /* voice preference name */
44         final static String voicePreference = "VOICE";
45
46         /* callsign preference name */
47         final static String callsignPreference = "CALLSIGN";
48
49         /* firmware directory preference name */
50         final static String firmwaredirPreference = "FIRMWARE";
51
52         /* serial debug preference name */
53         final static String serialDebugPreference = "SERIAL-DEBUG";
54
55         /* scanning telemetry preferences name */
56         final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
57
58         /* Launcher serial preference name */
59         final static String launcherSerialPreference = "LAUNCHER-SERIAL";
60
61         /* Launcher channel preference name */
62         final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
63         
64         /* Default logdir is ~/TeleMetrum */
65         final static String logdirName = "TeleMetrum";
66
67         /* Log directory */
68         static File logdir;
69
70         /* Map directory -- hangs of logdir */
71         static File mapdir;
72
73         /* Frequency (map serial to frequency) */
74         static Hashtable<Integer, Double> frequencies;
75
76         /* Telemetry (map serial to telemetry format) */
77         static Hashtable<Integer, Integer> telemetries;
78
79         /* Voice preference */
80         static boolean voice;
81
82         /* Callsign preference */
83         static String callsign;
84
85         /* Firmware directory */
86         static File firmwaredir;
87
88         /* Serial debug */
89         static boolean serial_debug;
90
91         /* Scanning telemetry */
92         static int scanning_telemetry;
93
94         /* List of frequencies */
95         final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
96         static AltosFrequency[] common_frequencies;
97
98         final static String     frequency_count = "COUNT";
99         final static String     frequency_format = "FREQUENCY-%d";
100         final static String     description_format = "DESCRIPTION-%d";
101
102         static AltosFrequency[] load_common_frequencies() {
103                 AltosFrequency[] frequencies = null;
104                 boolean existing = false;
105                 try {
106                         existing = preferences.nodeExists(common_frequencies_node_name);
107                 } catch (BackingStoreException be) {
108                         existing = false;
109                 }
110                 if (existing) {
111                         Preferences     node = preferences.node(common_frequencies_node_name);
112                         int             count = node.getInt(frequency_count, 0);
113
114                         frequencies = new AltosFrequency[count];
115                         for (int i = 0; i < count; i++) {
116                                 double  frequency;
117                                 String  description;
118
119                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
120                                 description = node.get(String.format(description_format, i), null);
121                                 frequencies[i] = new AltosFrequency(frequency, description);
122                         }
123                 } else {
124                         frequencies = new AltosFrequency[10];
125                         for (int i = 0; i < 10; i++) {
126                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
127                                                                            String.format("Channel %d", i));
128                         }
129                 }
130                 return frequencies;
131         }
132
133         static void save_common_frequencies(AltosFrequency[] frequencies) {
134                 Preferences     node = preferences.node(common_frequencies_node_name);
135
136                 node.putInt(frequency_count, frequencies.length);
137                 for (int i = 0; i < frequencies.length; i++) {
138                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
139                         node.put(String.format(description_format, i), frequencies[i].description);
140                 }
141         }
142         static int launcher_serial;
143
144         static int launcher_channel;
145
146         public static void init() {
147                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
148
149                 /* Initialize logdir from preferences */
150                 String logdir_string = preferences.get(logdirPreference, null);
151                 if (logdir_string != null)
152                         logdir = new File(logdir_string);
153                 else {
154                         /* Use the file system view default directory */
155                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
156                         if (!logdir.exists())
157                                 logdir.mkdirs();
158                 }
159                 mapdir = new File(logdir, "maps");
160                 if (!mapdir.exists())
161                         mapdir.mkdirs();
162
163                 frequencies = new Hashtable<Integer, Double>();
164
165                 telemetries = new Hashtable<Integer,Integer>();
166
167                 voice = preferences.getBoolean(voicePreference, true);
168
169                 callsign = preferences.get(callsignPreference,"N0CALL");
170
171                 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << Altos.ao_telemetry_standard));
172
173                 launcher_serial = preferences.getInt(launcherSerialPreference, 0);
174
175                 launcher_channel = preferences.getInt(launcherChannelPreference, 0);
176
177                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
178                 if (firmwaredir_string != null)
179                         firmwaredir = new File(firmwaredir_string);
180                 else
181                         firmwaredir = null;
182
183                 serial_debug = preferences.getBoolean(serialDebugPreference, false);
184                 AltosSerial.set_debug(serial_debug);
185
186                 common_frequencies = load_common_frequencies();
187
188         }
189
190         static { init(); }
191
192         static void flush_preferences() {
193                 try {
194                         preferences.flush();
195                 } catch (BackingStoreException ee) {
196 /*
197                         if (component != null)
198                                 JOptionPane.showMessageDialog(component,
199                                                               preferences.absolutePath(),
200                                                               "Cannot save prefernces",
201                                                               JOptionPane.ERROR_MESSAGE);
202                         else
203 */
204                                 System.err.printf("Cannot save preferences\n");
205                 }
206         }
207
208         public static void set_logdir(File new_logdir) {
209                 logdir = new_logdir;
210                 mapdir = new File(logdir, "maps");
211                 if (!mapdir.exists())
212                         mapdir.mkdirs();
213                 synchronized (preferences) {
214                         preferences.put(logdirPreference, logdir.getPath());
215                         flush_preferences();
216                 }
217         }
218
219         public static File logdir() {
220                 return logdir;
221         }
222
223         public static File mapdir() {
224                 return mapdir;
225         }
226
227         public static void set_frequency(int serial, double new_frequency) {
228                 frequencies.put(serial, new_frequency);
229                 synchronized (preferences) {
230                         preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
231                         flush_preferences();
232                 }
233         }
234
235         public static double frequency(int serial) {
236                 if (frequencies.containsKey(serial))
237                         return frequencies.get(serial);
238                 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
239                 if (frequency == 0.0) {
240                         int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
241                         frequency = AltosConvert.radio_channel_to_frequency(channel);
242                 }
243                 frequencies.put(serial, frequency);
244                 return frequency;
245         }
246
247         public static void set_telemetry(int serial, int new_telemetry) {
248                 telemetries.put(serial, new_telemetry);
249                 synchronized (preferences) {
250                         preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
251                         flush_preferences();
252                 }
253         }
254
255         public static int telemetry(int serial) {
256                 if (telemetries.containsKey(serial))
257                         return telemetries.get(serial);
258                 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
259                                                    Altos.ao_telemetry_standard);
260                 telemetries.put(serial, telemetry);
261                 return telemetry;
262         }
263
264         public static void set_scanning_telemetry(int new_scanning_telemetry) {
265                 scanning_telemetry = new_scanning_telemetry;
266                 synchronized (preferences) {
267                         preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
268                         flush_preferences();
269                 }
270         }
271
272         public static int scanning_telemetry() {
273                 return scanning_telemetry;
274         }
275
276         public static void set_voice(boolean new_voice) {
277                 voice = new_voice;
278                 synchronized (preferences) {
279                         preferences.putBoolean(voicePreference, voice);
280                         flush_preferences();
281                 }
282         }
283
284         public static boolean voice() {
285                 return voice;
286         }
287
288         public static void set_callsign(String new_callsign) {
289                 callsign = new_callsign;
290                 synchronized(preferences) {
291                         preferences.put(callsignPreference, callsign);
292                         flush_preferences();
293                 }
294         }
295
296         public static String callsign() {
297                 return callsign;
298         }
299
300         public static void set_firmwaredir(File new_firmwaredir) {
301                 firmwaredir = new_firmwaredir;
302                 synchronized (preferences) {
303                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
304                         flush_preferences();
305                 }
306         }
307
308         public static File firmwaredir() {
309                 return firmwaredir;
310         }
311
312         public static void set_serial_debug(boolean new_serial_debug) {
313                 serial_debug = new_serial_debug;
314                 AltosSerial.set_debug(serial_debug);
315                 synchronized (preferences) {
316                         preferences.putBoolean(serialDebugPreference, serial_debug);
317                         flush_preferences();
318                 }
319         }
320
321         public static boolean serial_debug() {
322                 return serial_debug;
323         }
324
325         public static void set_launcher_serial(int new_launcher_serial) {
326                 launcher_serial = new_launcher_serial;
327                 System.out.printf("set launcher serial to %d\n", new_launcher_serial);
328                 synchronized (preferences) {
329                         preferences.putInt(launcherSerialPreference, launcher_serial);
330                         flush_preferences();
331                 }
332         }
333
334         public static int launcher_serial() {
335                 return launcher_serial;
336         }
337
338         public static void set_launcher_channel(int new_launcher_channel) {
339                 launcher_channel = new_launcher_channel;
340                 System.out.printf("set launcher channel to %d\n", new_launcher_channel);
341                 synchronized (preferences) {
342                         preferences.putInt(launcherChannelPreference, launcher_channel);
343                         flush_preferences();
344                 }
345         }
346
347         public static int launcher_channel() {
348                 return launcher_channel;
349         }
350         
351         public static Preferences bt_devices() {
352                 return preferences.node("bt_devices");
353         }
354
355         public static AltosFrequency[] common_frequencies() {
356                 return common_frequencies;
357         }
358
359         public static void set_common_frequencies(AltosFrequency[] frequencies) {
360                 common_frequencies = frequencies;
361                 synchronized(preferences) {
362                         save_common_frequencies(frequencies);
363                         flush_preferences();
364                 }
365         }
366
367         public static void add_common_frequency(AltosFrequency frequency) {
368                 AltosFrequency[]        new_frequencies = new AltosFrequency[common_frequencies.length + 1];
369                 int                     i;
370
371                 for (i = 0; i < common_frequencies.length; i++) {
372                         if (frequency.frequency == common_frequencies[i].frequency)
373                                 return;
374                         if (frequency.frequency < common_frequencies[i].frequency)
375                                 break;
376                         new_frequencies[i] = common_frequencies[i];
377                 }
378                 new_frequencies[i] = frequency;
379                 for (; i < common_frequencies.length; i++)
380                         new_frequencies[i+1] = common_frequencies[i];
381                 set_common_frequencies(new_frequencies);
382         }
383 }