altosui: Make set of telemetries to use while scanning configurable
[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         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         /* Default logdir is ~/TeleMetrum */
59         final static String logdirName = "TeleMetrum";
60
61         /* UI Component to pop dialogs up */
62         static Component component;
63
64         /* Log directory */
65         static File logdir;
66
67         /* Map directory -- hangs of logdir */
68         static File mapdir;
69
70         /* Frequency (map serial to frequency) */
71         static Hashtable<Integer, Double> frequencies;
72
73         /* Telemetry (map serial to telemetry format) */
74         static Hashtable<Integer, Integer> telemetries;
75
76         /* Voice preference */
77         static boolean voice;
78
79         /* Callsign preference */
80         static String callsign;
81
82         /* Firmware directory */
83         static File firmwaredir;
84
85         /* Serial debug */
86         static boolean serial_debug;
87
88         /* Scanning telemetry */
89         static int scanning_telemetry;
90
91         /* List of frequencies */
92         final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
93         static AltosFrequency[] common_frequencies;
94
95         final static String     frequency_count = "COUNT";
96         final static String     frequency_format = "FREQUENCY-%d";
97         final static String     description_format = "DESCRIPTION-%d";
98
99         static AltosFrequency[] load_common_frequencies() {
100                 AltosFrequency[] frequencies = null;
101                 boolean existing = false;
102                 try {
103                         existing = preferences.nodeExists(common_frequencies_node_name);
104                 } catch (BackingStoreException be) {
105                         existing = false;
106                 }
107                 if (existing) {
108                         Preferences     node = preferences.node(common_frequencies_node_name);
109                         int             count = node.getInt(frequency_count, 0);
110
111                         frequencies = new AltosFrequency[count];
112                         for (int i = 0; i < count; i++) {
113                                 double  frequency;
114                                 String  description;
115
116                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
117                                 description = node.get(String.format(description_format, i), null);
118                                 frequencies[i] = new AltosFrequency(frequency, description);
119                         }
120                 } else {
121                         frequencies = new AltosFrequency[10];
122                         for (int i = 0; i < 10; i++) {
123                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
124                                                                            String.format("Channel %d", i));
125                         }
126                 }
127                 return frequencies;
128         }
129
130         static void save_common_frequencies(AltosFrequency[] frequencies) {
131                 Preferences     node = preferences.node(common_frequencies_node_name);
132
133                 node.putInt(frequency_count, frequencies.length);
134                 for (int i = 0; i < frequencies.length; i++) {
135                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
136                         node.put(String.format(description_format, i), frequencies[i].description);
137                 }
138         }
139
140         public static void init() {
141                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
142
143                 /* Initialize logdir from preferences */
144                 String logdir_string = preferences.get(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 = preferences.getBoolean(voicePreference, true);
162
163                 callsign = preferences.get(callsignPreference,"N0CALL");
164
165                 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << Altos.ao_telemetry_standard));
166
167                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
168                 if (firmwaredir_string != null)
169                         firmwaredir = new File(firmwaredir_string);
170                 else
171                         firmwaredir = null;
172
173                 serial_debug = preferences.getBoolean(serialDebugPreference, false);
174                 AltosSerial.set_debug(serial_debug);
175
176                 common_frequencies = load_common_frequencies();
177         }
178
179         static { init(); }
180
181         static void set_component(Component in_component) {
182                 component = in_component;
183         }
184
185         static void flush_preferences() {
186                 try {
187                         preferences.flush();
188                 } catch (BackingStoreException ee) {
189                         if (component != null)
190                                 JOptionPane.showMessageDialog(component,
191                                                               preferences.absolutePath(),
192                                                               "Cannot save prefernces",
193                                                               JOptionPane.ERROR_MESSAGE);
194                         else
195                                 System.err.printf("Cannot save preferences\n");
196                 }
197         }
198
199         public static void set_logdir(File new_logdir) {
200                 logdir = new_logdir;
201                 mapdir = new File(logdir, "maps");
202                 if (!mapdir.exists())
203                         mapdir.mkdirs();
204                 synchronized (preferences) {
205                         preferences.put(logdirPreference, logdir.getPath());
206                         flush_preferences();
207                 }
208         }
209
210         private static boolean check_dir(File dir) {
211                 if (!dir.exists()) {
212                         if (!dir.mkdirs()) {
213                                 JOptionPane.showMessageDialog(component,
214                                                               dir.getName(),
215                                                               "Cannot create directory",
216                                                               JOptionPane.ERROR_MESSAGE);
217                                 return false;
218                         }
219                 } else if (!dir.isDirectory()) {
220                         JOptionPane.showMessageDialog(component,
221                                                       dir.getName(),
222                                                       "Is not a directory",
223                                                       JOptionPane.ERROR_MESSAGE);
224                         return false;
225                 }
226                 return true;
227         }
228
229         /* Configure the log directory. This is where all telemetry and eeprom files
230          * will be written to, and where replay will look for telemetry files
231          */
232         public static void ConfigureLog() {
233                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
234
235                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
236                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
237
238                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
239                         File dir = logdir_chooser.getSelectedFile();
240                         if (check_dir(dir))
241                                 set_logdir(dir);
242                 }
243         }
244
245         public static File logdir() {
246                 return logdir;
247         }
248
249         public static File mapdir() {
250                 return mapdir;
251         }
252
253         public static void set_frequency(int serial, double new_frequency) {
254                 frequencies.put(serial, new_frequency);
255                 synchronized (preferences) {
256                         preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
257                         flush_preferences();
258                 }
259         }
260
261         public static double frequency(int serial) {
262                 if (frequencies.containsKey(serial))
263                         return frequencies.get(serial);
264                 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
265                 if (frequency == 0.0) {
266                         int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
267                         frequency = AltosConvert.radio_channel_to_frequency(channel);
268                 }
269                 frequencies.put(serial, frequency);
270                 return frequency;
271         }
272
273         public static void set_telemetry(int serial, int new_telemetry) {
274                 telemetries.put(serial, new_telemetry);
275                 synchronized (preferences) {
276                         preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
277                         flush_preferences();
278                 }
279         }
280
281         public static int telemetry(int serial) {
282                 if (telemetries.containsKey(serial))
283                         return telemetries.get(serial);
284                 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
285                                                    Altos.ao_telemetry_standard);
286                 telemetries.put(serial, telemetry);
287                 return telemetry;
288         }
289
290         public static void set_scanning_telemetry(int new_scanning_telemetry) {
291                 scanning_telemetry = new_scanning_telemetry;
292                 synchronized (preferences) {
293                         preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
294                         flush_preferences();
295                 }
296         }
297
298         public static int scanning_telemetry() {
299                 return scanning_telemetry;
300         }
301
302         public static void set_voice(boolean new_voice) {
303                 voice = new_voice;
304                 synchronized (preferences) {
305                         preferences.putBoolean(voicePreference, voice);
306                         flush_preferences();
307                 }
308         }
309
310         public static boolean voice() {
311                 return voice;
312         }
313
314         public static void set_callsign(String new_callsign) {
315                 callsign = new_callsign;
316                 synchronized(preferences) {
317                         preferences.put(callsignPreference, callsign);
318                         flush_preferences();
319                 }
320         }
321
322         public static String callsign() {
323                 return callsign;
324         }
325
326         public static void set_firmwaredir(File new_firmwaredir) {
327                 firmwaredir = new_firmwaredir;
328                 synchronized (preferences) {
329                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
330                         flush_preferences();
331                 }
332         }
333
334         public static File firmwaredir() {
335                 return firmwaredir;
336         }
337
338         public static void set_serial_debug(boolean new_serial_debug) {
339                 serial_debug = new_serial_debug;
340                 AltosSerial.set_debug(serial_debug);
341                 synchronized (preferences) {
342                         preferences.putBoolean(serialDebugPreference, serial_debug);
343                         flush_preferences();
344                 }
345         }
346
347         public static boolean serial_debug() {
348                 return serial_debug;
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 }