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