altosui: Deal with serial port exceptions a bit better
[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         /* font size preferences name */
59         final static String fontSizePreference = "FONT-SIZE";
60
61         /* Launcher serial preference name */
62         final static String launcherSerialPreference = "LAUNCHER-SERIAL";
63
64         /* Launcher channel preference name */
65         final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
66         
67         /* Look&Feel preference name */
68         final static String lookAndFeelPreference = "LOOK-AND-FEEL";
69
70         /* Default logdir is ~/TeleMetrum */
71         final static String logdirName = "TeleMetrum";
72
73         /* UI Component to pop dialogs up */
74         static Component component;
75
76         /* Log directory */
77         static File logdir;
78
79         /* Map directory -- hangs of logdir */
80         static File mapdir;
81
82         /* Frequency (map serial to frequency) */
83         static Hashtable<Integer, Double> frequencies;
84
85         /* Telemetry (map serial to telemetry format) */
86         static Hashtable<Integer, Integer> telemetries;
87
88         /* Voice preference */
89         static boolean voice;
90
91         /* Callsign preference */
92         static String callsign;
93
94         /* Firmware directory */
95         static File firmwaredir;
96
97         /* Serial debug */
98         static boolean serial_debug;
99
100         /* Scanning telemetry */
101         static int scanning_telemetry;
102
103         static LinkedList<AltosFontListener> font_listeners;
104
105         static int font_size = Altos.font_size_medium;
106
107         static LinkedList<AltosUIListener> ui_listeners;
108
109         static String look_and_feel = null;
110
111         /* List of frequencies */
112         final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
113         static AltosFrequency[] common_frequencies;
114
115         final static String     frequency_count = "COUNT";
116         final static String     frequency_format = "FREQUENCY-%d";
117         final static String     description_format = "DESCRIPTION-%d";
118
119         static AltosFrequency[] load_common_frequencies() {
120                 AltosFrequency[] frequencies = null;
121                 boolean existing = false;
122                 try {
123                         existing = preferences.nodeExists(common_frequencies_node_name);
124                 } catch (BackingStoreException be) {
125                         existing = false;
126                 }
127                 if (existing) {
128                         Preferences     node = preferences.node(common_frequencies_node_name);
129                         int             count = node.getInt(frequency_count, 0);
130
131                         frequencies = new AltosFrequency[count];
132                         for (int i = 0; i < count; i++) {
133                                 double  frequency;
134                                 String  description;
135
136                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
137                                 description = node.get(String.format(description_format, i), null);
138                                 frequencies[i] = new AltosFrequency(frequency, description);
139                         }
140                 } else {
141                         frequencies = new AltosFrequency[10];
142                         for (int i = 0; i < 10; i++) {
143                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
144                                                                            String.format("Channel %d", i));
145                         }
146                 }
147                 return frequencies;
148         }
149
150         static void save_common_frequencies(AltosFrequency[] frequencies) {
151                 Preferences     node = preferences.node(common_frequencies_node_name);
152
153                 node.putInt(frequency_count, frequencies.length);
154                 for (int i = 0; i < frequencies.length; i++) {
155                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
156                         node.put(String.format(description_format, i), frequencies[i].description);
157                 }
158         }
159         static int launcher_serial;
160
161         static int launcher_channel;
162
163         public static void init() {
164                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
165
166                 /* Initialize logdir from preferences */
167                 String logdir_string = preferences.get(logdirPreference, null);
168                 if (logdir_string != null)
169                         logdir = new File(logdir_string);
170                 else {
171                         /* Use the file system view default directory */
172                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
173                         if (!logdir.exists())
174                                 logdir.mkdirs();
175                 }
176                 mapdir = new File(logdir, "maps");
177                 if (!mapdir.exists())
178                         mapdir.mkdirs();
179
180                 frequencies = new Hashtable<Integer, Double>();
181
182                 telemetries = new Hashtable<Integer,Integer>();
183
184                 voice = preferences.getBoolean(voicePreference, true);
185
186                 callsign = preferences.get(callsignPreference,"N0CALL");
187
188                 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << Altos.ao_telemetry_standard));
189
190                 font_listeners = new LinkedList<AltosFontListener>();
191
192                 font_size = preferences.getInt(fontSizePreference, Altos.font_size_medium);
193                 Altos.set_fonts(font_size);
194
195                 launcher_serial = preferences.getInt(launcherSerialPreference, 0);
196
197                 launcher_channel = preferences.getInt(launcherChannelPreference, 0);
198
199                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
200                 if (firmwaredir_string != null)
201                         firmwaredir = new File(firmwaredir_string);
202                 else
203                         firmwaredir = null;
204
205                 serial_debug = preferences.getBoolean(serialDebugPreference, false);
206                 AltosSerial.set_debug(serial_debug);
207
208                 common_frequencies = load_common_frequencies();
209
210                 look_and_feel = preferences.get(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
211
212                 ui_listeners = new LinkedList<AltosUIListener>();
213         }
214
215         static { init(); }
216
217         static void set_component(Component in_component) {
218                 component = in_component;
219         }
220
221         static void flush_preferences() {
222                 try {
223                         preferences.flush();
224                 } catch (BackingStoreException ee) {
225                         if (component != null)
226                                 JOptionPane.showMessageDialog(component,
227                                                               preferences.absolutePath(),
228                                                               "Cannot save prefernces",
229                                                               JOptionPane.ERROR_MESSAGE);
230                         else
231                                 System.err.printf("Cannot save preferences\n");
232                 }
233         }
234
235         public static void set_logdir(File new_logdir) {
236                 logdir = new_logdir;
237                 mapdir = new File(logdir, "maps");
238                 if (!mapdir.exists())
239                         mapdir.mkdirs();
240                 synchronized (preferences) {
241                         preferences.put(logdirPreference, logdir.getPath());
242                         flush_preferences();
243                 }
244         }
245
246         private static boolean check_dir(File dir) {
247                 if (!dir.exists()) {
248                         if (!dir.mkdirs()) {
249                                 JOptionPane.showMessageDialog(component,
250                                                               dir.getName(),
251                                                               "Cannot create directory",
252                                                               JOptionPane.ERROR_MESSAGE);
253                                 return false;
254                         }
255                 } else if (!dir.isDirectory()) {
256                         JOptionPane.showMessageDialog(component,
257                                                       dir.getName(),
258                                                       "Is not a directory",
259                                                       JOptionPane.ERROR_MESSAGE);
260                         return false;
261                 }
262                 return true;
263         }
264
265         /* Configure the log directory. This is where all telemetry and eeprom files
266          * will be written to, and where replay will look for telemetry files
267          */
268         public static void ConfigureLog() {
269                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
270
271                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
272                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
273
274                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
275                         File dir = logdir_chooser.getSelectedFile();
276                         if (check_dir(dir))
277                                 set_logdir(dir);
278                 }
279         }
280
281         public static File logdir() {
282                 return logdir;
283         }
284
285         public static File mapdir() {
286                 return mapdir;
287         }
288
289         public static void set_frequency(int serial, double new_frequency) {
290                 frequencies.put(serial, new_frequency);
291                 synchronized (preferences) {
292                         preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
293                         flush_preferences();
294                 }
295         }
296
297         public static double frequency(int serial) {
298                 if (frequencies.containsKey(serial))
299                         return frequencies.get(serial);
300                 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
301                 if (frequency == 0.0) {
302                         int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
303                         frequency = AltosConvert.radio_channel_to_frequency(channel);
304                 }
305                 frequencies.put(serial, frequency);
306                 return frequency;
307         }
308
309         public static void set_telemetry(int serial, int new_telemetry) {
310                 telemetries.put(serial, new_telemetry);
311                 synchronized (preferences) {
312                         preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
313                         flush_preferences();
314                 }
315         }
316
317         public static int telemetry(int serial) {
318                 if (telemetries.containsKey(serial))
319                         return telemetries.get(serial);
320                 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
321                                                    Altos.ao_telemetry_standard);
322                 telemetries.put(serial, telemetry);
323                 return telemetry;
324         }
325
326         public static void set_scanning_telemetry(int new_scanning_telemetry) {
327                 scanning_telemetry = new_scanning_telemetry;
328                 synchronized (preferences) {
329                         preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
330                         flush_preferences();
331                 }
332         }
333
334         public static int scanning_telemetry() {
335                 return scanning_telemetry;
336         }
337
338         public static void set_voice(boolean new_voice) {
339                 voice = new_voice;
340                 synchronized (preferences) {
341                         preferences.putBoolean(voicePreference, voice);
342                         flush_preferences();
343                 }
344         }
345
346         public static boolean voice() {
347                 return voice;
348         }
349
350         public static void set_callsign(String new_callsign) {
351                 callsign = new_callsign;
352                 synchronized(preferences) {
353                         preferences.put(callsignPreference, callsign);
354                         flush_preferences();
355                 }
356         }
357
358         public static String callsign() {
359                 return callsign;
360         }
361
362         public static void set_firmwaredir(File new_firmwaredir) {
363                 firmwaredir = new_firmwaredir;
364                 synchronized (preferences) {
365                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
366                         flush_preferences();
367                 }
368         }
369
370         public static File firmwaredir() {
371                 return firmwaredir;
372         }
373
374         public static int font_size() {
375                 return font_size;
376         }
377
378         static void set_fonts() {
379         }
380
381         public static void set_font_size(int new_font_size) {
382                 font_size = new_font_size;
383                 synchronized (preferences) {
384                         preferences.putInt(fontSizePreference, font_size);
385                         flush_preferences();
386                         Altos.set_fonts(font_size);
387                         for (AltosFontListener l : font_listeners)
388                                 l.font_size_changed(font_size);
389                 }
390         }
391
392         public static void register_font_listener(AltosFontListener l) {
393                 synchronized (preferences) {
394                         font_listeners.add(l);
395                 }
396         }
397
398         public static void unregister_font_listener(AltosFontListener l) {
399                 synchronized (preferences) {
400                         font_listeners.remove(l);
401                 }
402         }
403
404         public static void set_look_and_feel(String new_look_and_feel) {
405                 look_and_feel = new_look_and_feel;
406                 try {
407                         UIManager.setLookAndFeel(look_and_feel);
408                 } catch (Exception e) {
409                 }
410                 synchronized(preferences) {
411                         preferences.put(lookAndFeelPreference, look_and_feel);
412                         flush_preferences();
413                         for (AltosUIListener l : ui_listeners)
414                                 l.ui_changed(look_and_feel);
415                 }
416         }
417
418         public static String look_and_feel() {
419                 return look_and_feel;
420         }
421
422         public static void register_ui_listener(AltosUIListener l) {
423                 synchronized(preferences) {
424                         ui_listeners.add(l);
425                 }
426         }
427
428         public static void unregister_ui_listener(AltosUIListener l) {
429                 synchronized (preferences) {
430                         ui_listeners.remove(l);
431                 }
432         }
433         public static void set_serial_debug(boolean new_serial_debug) {
434                 serial_debug = new_serial_debug;
435                 AltosSerial.set_debug(serial_debug);
436                 synchronized (preferences) {
437                         preferences.putBoolean(serialDebugPreference, serial_debug);
438                         flush_preferences();
439                 }
440         }
441
442         public static boolean serial_debug() {
443                 return serial_debug;
444         }
445
446         public static void set_launcher_serial(int new_launcher_serial) {
447                 launcher_serial = new_launcher_serial;
448                 System.out.printf("set launcher serial to %d\n", new_launcher_serial);
449                 synchronized (preferences) {
450                         preferences.putInt(launcherSerialPreference, launcher_serial);
451                         flush_preferences();
452                 }
453         }
454
455         public static int launcher_serial() {
456                 return launcher_serial;
457         }
458
459         public static void set_launcher_channel(int new_launcher_channel) {
460                 launcher_channel = new_launcher_channel;
461                 System.out.printf("set launcher channel to %d\n", new_launcher_channel);
462                 synchronized (preferences) {
463                         preferences.putInt(launcherChannelPreference, launcher_channel);
464                         flush_preferences();
465                 }
466         }
467
468         public static int launcher_channel() {
469                 return launcher_channel;
470         }
471         
472         public static Preferences bt_devices() {
473                 return preferences.node("bt_devices");
474         }
475
476         public static AltosFrequency[] common_frequencies() {
477                 return common_frequencies;
478         }
479
480         public static void set_common_frequencies(AltosFrequency[] frequencies) {
481                 common_frequencies = frequencies;
482                 synchronized(preferences) {
483                         save_common_frequencies(frequencies);
484                         flush_preferences();
485                 }
486         }
487
488         public static void add_common_frequency(AltosFrequency frequency) {
489                 AltosFrequency[]        new_frequencies = new AltosFrequency[common_frequencies.length + 1];
490                 int                     i;
491
492                 for (i = 0; i < common_frequencies.length; i++) {
493                         if (frequency.frequency == common_frequencies[i].frequency)
494                                 return;
495                         if (frequency.frequency < common_frequencies[i].frequency)
496                                 break;
497                         new_frequencies[i] = common_frequencies[i];
498                 }
499                 new_frequencies[i] = frequency;
500                 for (; i < common_frequencies.length; i++)
501                         new_frequencies[i+1] = common_frequencies[i];
502                 set_common_frequencies(new_frequencies);
503         }
504 }