2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 import java.util.prefs.*;
23 import java.util.concurrent.LinkedBlockingQueue;
24 import java.awt.Component;
26 import javax.swing.filechooser.FileSystemView;
28 class AltosPreferences {
29 static Preferences preferences;
31 /* logdir preference name */
32 final static String logdirPreference = "LOGDIR";
34 /* channel preference name */
35 final static String channelPreferenceFormat = "CHANNEL-%d";
37 /* frequency preference name */
38 final static String frequencyPreferenceFormat = "FREQUENCY-%d";
40 /* telemetry format preference name */
41 final static String telemetryPreferenceFormat = "TELEMETRY-%d";
43 /* voice preference name */
44 final static String voicePreference = "VOICE";
46 /* callsign preference name */
47 final static String callsignPreference = "CALLSIGN";
49 /* firmware directory preference name */
50 final static String firmwaredirPreference = "FIRMWARE";
52 /* serial debug preference name */
53 final static String serialDebugPreference = "SERIAL-DEBUG";
55 /* scanning telemetry preferences name */
56 final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
58 /* Default logdir is ~/TeleMetrum */
59 final static String logdirName = "TeleMetrum";
61 /* UI Component to pop dialogs up */
62 static Component component;
67 /* Map directory -- hangs of logdir */
70 /* Frequency (map serial to frequency) */
71 static Hashtable<Integer, Double> frequencies;
73 /* Telemetry (map serial to telemetry format) */
74 static Hashtable<Integer, Integer> telemetries;
76 /* Voice preference */
79 /* Callsign preference */
80 static String callsign;
82 /* Firmware directory */
83 static File firmwaredir;
86 static boolean serial_debug;
88 /* Scanning telemetry */
89 static int scanning_telemetry;
91 /* List of frequencies */
92 final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
93 static AltosFrequency[] common_frequencies;
95 final static String frequency_count = "COUNT";
96 final static String frequency_format = "FREQUENCY-%d";
97 final static String description_format = "DESCRIPTION-%d";
99 static AltosFrequency[] load_common_frequencies() {
100 AltosFrequency[] frequencies = null;
101 boolean existing = false;
103 existing = preferences.nodeExists(common_frequencies_node_name);
104 } catch (BackingStoreException be) {
108 Preferences node = preferences.node(common_frequencies_node_name);
109 int count = node.getInt(frequency_count, 0);
111 frequencies = new AltosFrequency[count];
112 for (int i = 0; i < count; i++) {
116 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
117 description = node.get(String.format(description_format, i), null);
118 frequencies[i] = new AltosFrequency(frequency, description);
121 frequencies = new AltosFrequency[10];
122 for (int i = 0; i < 10; i++) {
123 frequencies[i] = new AltosFrequency(434.550 + i * .1,
124 String.format("Channel %d", i));
130 static void save_common_frequencies(AltosFrequency[] frequencies) {
131 Preferences node = preferences.node(common_frequencies_node_name);
133 node.putInt(frequency_count, frequencies.length);
134 for (int i = 0; i < frequencies.length; i++) {
135 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
136 node.put(String.format(description_format, i), frequencies[i].description);
140 public static void init() {
141 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
143 /* Initialize logdir from preferences */
144 String logdir_string = preferences.get(logdirPreference, null);
145 if (logdir_string != null)
146 logdir = new File(logdir_string);
148 /* Use the file system view default directory */
149 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
150 if (!logdir.exists())
153 mapdir = new File(logdir, "maps");
154 if (!mapdir.exists())
157 frequencies = new Hashtable<Integer, Double>();
159 telemetries = new Hashtable<Integer,Integer>();
161 voice = preferences.getBoolean(voicePreference, true);
163 callsign = preferences.get(callsignPreference,"N0CALL");
165 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << Altos.ao_telemetry_standard));
167 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
168 if (firmwaredir_string != null)
169 firmwaredir = new File(firmwaredir_string);
173 serial_debug = preferences.getBoolean(serialDebugPreference, false);
174 AltosSerial.set_debug(serial_debug);
176 common_frequencies = load_common_frequencies();
181 static void set_component(Component in_component) {
182 component = in_component;
185 static void flush_preferences() {
188 } catch (BackingStoreException ee) {
189 if (component != null)
190 JOptionPane.showMessageDialog(component,
191 preferences.absolutePath(),
192 "Cannot save prefernces",
193 JOptionPane.ERROR_MESSAGE);
195 System.err.printf("Cannot save preferences\n");
199 public static void set_logdir(File new_logdir) {
201 mapdir = new File(logdir, "maps");
202 if (!mapdir.exists())
204 synchronized (preferences) {
205 preferences.put(logdirPreference, logdir.getPath());
210 private static boolean check_dir(File dir) {
213 JOptionPane.showMessageDialog(component,
215 "Cannot create directory",
216 JOptionPane.ERROR_MESSAGE);
219 } else if (!dir.isDirectory()) {
220 JOptionPane.showMessageDialog(component,
222 "Is not a directory",
223 JOptionPane.ERROR_MESSAGE);
229 /* Configure the log directory. This is where all telemetry and eeprom files
230 * will be written to, and where replay will look for telemetry files
232 public static void ConfigureLog() {
233 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
235 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
236 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
238 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
239 File dir = logdir_chooser.getSelectedFile();
245 public static File logdir() {
249 public static File mapdir() {
253 public static void set_frequency(int serial, double new_frequency) {
254 frequencies.put(serial, new_frequency);
255 synchronized (preferences) {
256 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
261 public static double frequency(int serial) {
262 if (frequencies.containsKey(serial))
263 return frequencies.get(serial);
264 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
265 if (frequency == 0.0) {
266 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
267 frequency = AltosConvert.radio_channel_to_frequency(channel);
269 frequencies.put(serial, frequency);
273 public static void set_telemetry(int serial, int new_telemetry) {
274 telemetries.put(serial, new_telemetry);
275 synchronized (preferences) {
276 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
281 public static int telemetry(int serial) {
282 if (telemetries.containsKey(serial))
283 return telemetries.get(serial);
284 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
285 Altos.ao_telemetry_standard);
286 telemetries.put(serial, telemetry);
290 public static void set_scanning_telemetry(int new_scanning_telemetry) {
291 scanning_telemetry = new_scanning_telemetry;
292 synchronized (preferences) {
293 preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
298 public static int scanning_telemetry() {
299 return scanning_telemetry;
302 public static void set_voice(boolean new_voice) {
304 synchronized (preferences) {
305 preferences.putBoolean(voicePreference, voice);
310 public static boolean voice() {
314 public static void set_callsign(String new_callsign) {
315 callsign = new_callsign;
316 synchronized(preferences) {
317 preferences.put(callsignPreference, callsign);
322 public static String callsign() {
326 public static void set_firmwaredir(File new_firmwaredir) {
327 firmwaredir = new_firmwaredir;
328 synchronized (preferences) {
329 preferences.put(firmwaredirPreference, firmwaredir.getPath());
334 public static File firmwaredir() {
338 public static void set_serial_debug(boolean new_serial_debug) {
339 serial_debug = new_serial_debug;
340 AltosSerial.set_debug(serial_debug);
341 synchronized (preferences) {
342 preferences.putBoolean(serialDebugPreference, serial_debug);
347 public static boolean serial_debug() {
351 public static Preferences bt_devices() {
352 return preferences.node("bt_devices");
355 public static AltosFrequency[] common_frequencies() {
356 return common_frequencies;
359 public static void set_common_frequencies(AltosFrequency[] frequencies) {
360 common_frequencies = frequencies;
361 synchronized(preferences) {
362 save_common_frequencies(frequencies);
367 public static void add_common_frequency(AltosFrequency frequency) {
368 AltosFrequency[] new_frequencies = new AltosFrequency[common_frequencies.length + 1];
371 for (i = 0; i < common_frequencies.length; i++) {
372 if (frequency.frequency == common_frequencies[i].frequency)
374 if (frequency.frequency < common_frequencies[i].frequency)
376 new_frequencies[i] = common_frequencies[i];
378 new_frequencies[i] = frequency;
379 for (; i < common_frequencies.length; i++)
380 new_frequencies[i+1] = common_frequencies[i];
381 set_common_frequencies(new_frequencies);