altos: Clean up serial initialization
[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(Component ui) {
83                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
84
85                 component = ui;
86
87                 /* Initialize logdir from preferences */
88                 String logdir_string = preferences.get(logdirPreference, null);
89                 if (logdir_string != null)
90                         logdir = new File(logdir_string);
91                 else {
92                         /* Use the file system view default directory */
93                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
94                         if (!logdir.exists())
95                                 logdir.mkdirs();
96                 }
97                 mapdir = new File(logdir, "maps");
98                 if (!mapdir.exists())
99                         mapdir.mkdirs();
100
101                 channels = new Hashtable<Integer,Integer>();
102
103                 telemetries = new Hashtable<Integer,Integer>();
104
105                 voice = preferences.getBoolean(voicePreference, true);
106
107                 callsign = preferences.get(callsignPreference,"N0CALL");
108
109                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
110                 if (firmwaredir_string != null)
111                         firmwaredir = new File(firmwaredir_string);
112                 else
113                         firmwaredir = null;
114
115                 serial_debug = preferences.getBoolean(serialDebugPreference, false);
116                 AltosSerial.set_debug(serial_debug);
117         }
118
119         static void flush_preferences() {
120                 try {
121                         preferences.flush();
122                 } catch (BackingStoreException ee) {
123                         JOptionPane.showMessageDialog(component,
124                                                       preferences.absolutePath(),
125                                                       "Cannot save prefernces",
126                                                       JOptionPane.ERROR_MESSAGE);
127                 }
128         }
129
130         public static void set_logdir(File new_logdir) {
131                 logdir = new_logdir;
132                 mapdir = new File(logdir, "maps");
133                 if (!mapdir.exists())
134                         mapdir.mkdirs();
135                 synchronized (preferences) {
136                         preferences.put(logdirPreference, logdir.getPath());
137                         flush_preferences();
138                 }
139         }
140
141         private static boolean check_dir(File dir) {
142                 if (!dir.exists()) {
143                         if (!dir.mkdirs()) {
144                                 JOptionPane.showMessageDialog(component,
145                                                               dir.getName(),
146                                                               "Cannot create directory",
147                                                               JOptionPane.ERROR_MESSAGE);
148                                 return false;
149                         }
150                 } else if (!dir.isDirectory()) {
151                         JOptionPane.showMessageDialog(component,
152                                                       dir.getName(),
153                                                       "Is not a directory",
154                                                       JOptionPane.ERROR_MESSAGE);
155                         return false;
156                 }
157                 return true;
158         }
159
160         /* Configure the log directory. This is where all telemetry and eeprom files
161          * will be written to, and where replay will look for telemetry files
162          */
163         public static void ConfigureLog() {
164                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
165
166                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
167                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
168
169                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
170                         File dir = logdir_chooser.getSelectedFile();
171                         if (check_dir(dir))
172                                 set_logdir(dir);
173                 }
174         }
175
176         public static File logdir() {
177                 return logdir;
178         }
179
180         public static File mapdir() {
181                 return mapdir;
182         }
183
184         public static void set_channel(int serial, int new_channel) {
185                 channels.put(serial, new_channel);
186                 synchronized (preferences) {
187                         preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
188                         flush_preferences();
189                 }
190         }
191
192         public static int channel(int serial) {
193                 if (channels.containsKey(serial))
194                         return channels.get(serial);
195                 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
196                 channels.put(serial, channel);
197                 return channel;
198         }
199
200         public static void set_telemetry(int serial, int new_telemetry) {
201                 telemetries.put(serial, new_telemetry);
202                 synchronized (preferences) {
203                         preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
204                         flush_preferences();
205                 }
206         }
207
208         public static int telemetry(int serial) {
209                 if (telemetries.containsKey(serial))
210                         return telemetries.get(serial);
211                 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
212                                                    Altos.ao_telemetry_full);
213                 telemetries.put(serial, telemetry);
214                 return telemetry;
215         }
216
217         public static void set_voice(boolean new_voice) {
218                 voice = new_voice;
219                 synchronized (preferences) {
220                         preferences.putBoolean(voicePreference, voice);
221                         flush_preferences();
222                 }
223         }
224
225         public static boolean voice() {
226                 return voice;
227         }
228
229         public static void set_callsign(String new_callsign) {
230                 callsign = new_callsign;
231                 synchronized(preferences) {
232                         preferences.put(callsignPreference, callsign);
233                         flush_preferences();
234                 }
235         }
236
237         public static String callsign() {
238                 return callsign;
239         }
240
241         public static void set_firmwaredir(File new_firmwaredir) {
242                 firmwaredir = new_firmwaredir;
243                 synchronized (preferences) {
244                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
245                         flush_preferences();
246                 }
247         }
248
249         public static File firmwaredir() {
250                 return firmwaredir;
251         }
252
253         public static void set_serial_debug(boolean new_serial_debug) {
254                 serial_debug = new_serial_debug;
255                 AltosSerial.set_debug(serial_debug);
256                 synchronized (preferences) {
257                         preferences.putBoolean(serialDebugPreference, serial_debug);
258                         flush_preferences();
259                 }
260         }
261
262         public static boolean serial_debug() {
263                 return serial_debug;
264         }
265 }