altosui: Separate out flash debug code to separate thread
[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         /* telemetry format preference name */
38         final static String telemetryPreferenceFormat = "TELEMETRY-%d";
39
40         /* voice preference name */
41         final static String voicePreference = "VOICE";
42
43         /* callsign preference name */
44         final static String callsignPreference = "CALLSIGN";
45
46         /* firmware directory preference name */
47         final static String firmwaredirPreference = "FIRMWARE";
48
49         /* serial debug preference name */
50         final static String serialDebugPreference = "SERIAL-DEBUG";
51
52         /* Default logdir is ~/TeleMetrum */
53         final static String logdirName = "TeleMetrum";
54
55         /* UI Component to pop dialogs up */
56         static Component component;
57
58         /* Log directory */
59         static File logdir;
60
61         /* Map directory -- hangs of logdir */
62         static File mapdir;
63
64         /* Channel (map serial to channel) */
65         static Hashtable<Integer, Integer> channels;
66
67         /* Telemetry (map serial to telemetry format) */
68         static Hashtable<Integer, Integer> telemetries;
69
70         /* Voice preference */
71         static boolean voice;
72
73         /* Callsign preference */
74         static String callsign;
75
76         /* Firmware directory */
77         static File firmwaredir;
78
79         /* Serial debug */
80         static boolean serial_debug;
81
82         public static void init() {
83                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
84
85                 /* Initialize logdir from preferences */
86                 String logdir_string = preferences.get(logdirPreference, null);
87                 if (logdir_string != null)
88                         logdir = new File(logdir_string);
89                 else {
90                         /* Use the file system view default directory */
91                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
92                         if (!logdir.exists())
93                                 logdir.mkdirs();
94                 }
95                 mapdir = new File(logdir, "maps");
96                 if (!mapdir.exists())
97                         mapdir.mkdirs();
98
99                 channels = new Hashtable<Integer,Integer>();
100
101                 telemetries = new Hashtable<Integer,Integer>();
102
103                 voice = preferences.getBoolean(voicePreference, true);
104
105                 callsign = preferences.get(callsignPreference,"N0CALL");
106
107                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
108                 if (firmwaredir_string != null)
109                         firmwaredir = new File(firmwaredir_string);
110                 else
111                         firmwaredir = null;
112
113                 serial_debug = preferences.getBoolean(serialDebugPreference, false);
114                 AltosSerial.set_debug(serial_debug);
115         }
116
117         static { init(); }
118
119         static void set_component(Component in_component) {
120                 component = in_component;
121         }
122
123         static void flush_preferences() {
124                 try {
125                         preferences.flush();
126                 } catch (BackingStoreException ee) {
127                         if (component != null)
128                                 JOptionPane.showMessageDialog(component,
129                                                               preferences.absolutePath(),
130                                                               "Cannot save prefernces",
131                                                               JOptionPane.ERROR_MESSAGE);
132                         else
133                                 System.err.printf("Cannot save preferences\n");
134                 }
135         }
136
137         public static void set_logdir(File new_logdir) {
138                 logdir = new_logdir;
139                 mapdir = new File(logdir, "maps");
140                 if (!mapdir.exists())
141                         mapdir.mkdirs();
142                 synchronized (preferences) {
143                         preferences.put(logdirPreference, logdir.getPath());
144                         flush_preferences();
145                 }
146         }
147
148         private static boolean check_dir(File dir) {
149                 if (!dir.exists()) {
150                         if (!dir.mkdirs()) {
151                                 JOptionPane.showMessageDialog(component,
152                                                               dir.getName(),
153                                                               "Cannot create directory",
154                                                               JOptionPane.ERROR_MESSAGE);
155                                 return false;
156                         }
157                 } else if (!dir.isDirectory()) {
158                         JOptionPane.showMessageDialog(component,
159                                                       dir.getName(),
160                                                       "Is not a directory",
161                                                       JOptionPane.ERROR_MESSAGE);
162                         return false;
163                 }
164                 return true;
165         }
166
167         /* Configure the log directory. This is where all telemetry and eeprom files
168          * will be written to, and where replay will look for telemetry files
169          */
170         public static void ConfigureLog() {
171                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
172
173                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
174                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
175
176                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
177                         File dir = logdir_chooser.getSelectedFile();
178                         if (check_dir(dir))
179                                 set_logdir(dir);
180                 }
181         }
182
183         public static File logdir() {
184                 return logdir;
185         }
186
187         public static File mapdir() {
188                 return mapdir;
189         }
190
191         public static void set_channel(int serial, int new_channel) {
192                 channels.put(serial, new_channel);
193                 synchronized (preferences) {
194                         preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
195                         flush_preferences();
196                 }
197         }
198
199         public static int channel(int serial) {
200                 if (channels.containsKey(serial))
201                         return channels.get(serial);
202                 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
203                 channels.put(serial, channel);
204                 return channel;
205         }
206
207         public static void set_telemetry(int serial, int new_telemetry) {
208                 telemetries.put(serial, new_telemetry);
209                 synchronized (preferences) {
210                         preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
211                         flush_preferences();
212                 }
213         }
214
215         public static int telemetry(int serial) {
216                 if (telemetries.containsKey(serial))
217                         return telemetries.get(serial);
218                 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
219                                                    Altos.ao_telemetry_full);
220                 telemetries.put(serial, telemetry);
221                 return telemetry;
222         }
223
224         public static void set_voice(boolean new_voice) {
225                 voice = new_voice;
226                 synchronized (preferences) {
227                         preferences.putBoolean(voicePreference, voice);
228                         flush_preferences();
229                 }
230         }
231
232         public static boolean voice() {
233                 return voice;
234         }
235
236         public static void set_callsign(String new_callsign) {
237                 callsign = new_callsign;
238                 synchronized(preferences) {
239                         preferences.put(callsignPreference, callsign);
240                         flush_preferences();
241                 }
242         }
243
244         public static String callsign() {
245                 return callsign;
246         }
247
248         public static void set_firmwaredir(File new_firmwaredir) {
249                 firmwaredir = new_firmwaredir;
250                 synchronized (preferences) {
251                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
252                         flush_preferences();
253                 }
254         }
255
256         public static File firmwaredir() {
257                 return firmwaredir;
258         }
259
260         public static void set_serial_debug(boolean new_serial_debug) {
261                 serial_debug = new_serial_debug;
262                 AltosSerial.set_debug(serial_debug);
263                 synchronized (preferences) {
264                         preferences.putBoolean(serialDebugPreference, serial_debug);
265                         flush_preferences();
266                 }
267         }
268
269         public static boolean serial_debug() {
270                 return serial_debug;
271         }
272
273         public static Preferences bt_devices() {
274                 return preferences.node("bt_devices");
275         }
276 }