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