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 /* font size preferences name */
59 final static String fontSizePreference = "FONT-SIZE";
61 /* Default logdir is ~/TeleMetrum */
62 final static String logdirName = "TeleMetrum";
64 /* UI Component to pop dialogs up */
65 static Component component;
70 /* Map directory -- hangs of logdir */
73 /* Frequency (map serial to frequency) */
74 static Hashtable<Integer, Double> frequencies;
76 /* Telemetry (map serial to telemetry format) */
77 static Hashtable<Integer, Integer> telemetries;
79 /* Voice preference */
82 /* Callsign preference */
83 static String callsign;
85 /* Firmware directory */
86 static File firmwaredir;
89 static boolean serial_debug;
91 /* Scanning telemetry */
92 static int scanning_telemetry;
94 static LinkedList<AltosFontListener> font_listeners;
96 static int font_size = Altos.font_size_medium;
98 /* List of frequencies */
99 final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
100 static AltosFrequency[] common_frequencies;
102 final static String frequency_count = "COUNT";
103 final static String frequency_format = "FREQUENCY-%d";
104 final static String description_format = "DESCRIPTION-%d";
106 static AltosFrequency[] load_common_frequencies() {
107 AltosFrequency[] frequencies = null;
108 boolean existing = false;
110 existing = preferences.nodeExists(common_frequencies_node_name);
111 } catch (BackingStoreException be) {
115 Preferences node = preferences.node(common_frequencies_node_name);
116 int count = node.getInt(frequency_count, 0);
118 frequencies = new AltosFrequency[count];
119 for (int i = 0; i < count; i++) {
123 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
124 description = node.get(String.format(description_format, i), null);
125 frequencies[i] = new AltosFrequency(frequency, description);
128 frequencies = new AltosFrequency[10];
129 for (int i = 0; i < 10; i++) {
130 frequencies[i] = new AltosFrequency(434.550 + i * .1,
131 String.format("Channel %d", i));
137 static void save_common_frequencies(AltosFrequency[] frequencies) {
138 Preferences node = preferences.node(common_frequencies_node_name);
140 node.putInt(frequency_count, frequencies.length);
141 for (int i = 0; i < frequencies.length; i++) {
142 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
143 node.put(String.format(description_format, i), frequencies[i].description);
147 public static void init() {
148 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
150 /* Initialize logdir from preferences */
151 String logdir_string = preferences.get(logdirPreference, null);
152 if (logdir_string != null)
153 logdir = new File(logdir_string);
155 /* Use the file system view default directory */
156 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
157 if (!logdir.exists())
160 mapdir = new File(logdir, "maps");
161 if (!mapdir.exists())
164 frequencies = new Hashtable<Integer, Double>();
166 telemetries = new Hashtable<Integer,Integer>();
168 voice = preferences.getBoolean(voicePreference, true);
170 callsign = preferences.get(callsignPreference,"N0CALL");
172 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << Altos.ao_telemetry_standard));
174 font_listeners = new LinkedList<AltosFontListener>();
176 font_size = preferences.getInt(fontSizePreference, Altos.font_size_medium);
177 Altos.set_fonts(font_size);
179 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
180 if (firmwaredir_string != null)
181 firmwaredir = new File(firmwaredir_string);
185 serial_debug = preferences.getBoolean(serialDebugPreference, false);
186 AltosSerial.set_debug(serial_debug);
188 common_frequencies = load_common_frequencies();
193 static void set_component(Component in_component) {
194 component = in_component;
197 static void flush_preferences() {
200 } catch (BackingStoreException ee) {
201 if (component != null)
202 JOptionPane.showMessageDialog(component,
203 preferences.absolutePath(),
204 "Cannot save prefernces",
205 JOptionPane.ERROR_MESSAGE);
207 System.err.printf("Cannot save preferences\n");
211 public static void set_logdir(File new_logdir) {
213 mapdir = new File(logdir, "maps");
214 if (!mapdir.exists())
216 synchronized (preferences) {
217 preferences.put(logdirPreference, logdir.getPath());
222 private static boolean check_dir(File dir) {
225 JOptionPane.showMessageDialog(component,
227 "Cannot create directory",
228 JOptionPane.ERROR_MESSAGE);
231 } else if (!dir.isDirectory()) {
232 JOptionPane.showMessageDialog(component,
234 "Is not a directory",
235 JOptionPane.ERROR_MESSAGE);
241 /* Configure the log directory. This is where all telemetry and eeprom files
242 * will be written to, and where replay will look for telemetry files
244 public static void ConfigureLog() {
245 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
247 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
248 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
250 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
251 File dir = logdir_chooser.getSelectedFile();
257 public static File logdir() {
261 public static File mapdir() {
265 public static void set_frequency(int serial, double new_frequency) {
266 frequencies.put(serial, new_frequency);
267 synchronized (preferences) {
268 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
273 public static double frequency(int serial) {
274 if (frequencies.containsKey(serial))
275 return frequencies.get(serial);
276 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
277 if (frequency == 0.0) {
278 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
279 frequency = AltosConvert.radio_channel_to_frequency(channel);
281 frequencies.put(serial, frequency);
285 public static void set_telemetry(int serial, int new_telemetry) {
286 telemetries.put(serial, new_telemetry);
287 synchronized (preferences) {
288 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
293 public static int telemetry(int serial) {
294 if (telemetries.containsKey(serial))
295 return telemetries.get(serial);
296 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
297 Altos.ao_telemetry_standard);
298 telemetries.put(serial, telemetry);
302 public static void set_scanning_telemetry(int new_scanning_telemetry) {
303 scanning_telemetry = new_scanning_telemetry;
304 synchronized (preferences) {
305 preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
310 public static int scanning_telemetry() {
311 return scanning_telemetry;
314 public static void set_voice(boolean new_voice) {
316 synchronized (preferences) {
317 preferences.putBoolean(voicePreference, voice);
322 public static boolean voice() {
326 public static void set_callsign(String new_callsign) {
327 callsign = new_callsign;
328 synchronized(preferences) {
329 preferences.put(callsignPreference, callsign);
334 public static String callsign() {
338 public static void set_firmwaredir(File new_firmwaredir) {
339 firmwaredir = new_firmwaredir;
340 synchronized (preferences) {
341 preferences.put(firmwaredirPreference, firmwaredir.getPath());
346 public static File firmwaredir() {
350 public static int font_size() {
354 static void set_fonts() {
357 public static void set_font_size(int new_font_size) {
358 font_size = new_font_size;
359 synchronized (preferences) {
360 preferences.putInt(fontSizePreference, font_size);
362 Altos.set_fonts(font_size);
363 for (AltosFontListener l : font_listeners)
364 l.font_size_changed(font_size);
368 public static void register_font_listener(AltosFontListener l) {
369 synchronized (preferences) {
370 font_listeners.add(l);
374 public static void unregister_font_listener(AltosFontListener l) {
375 synchronized (preferences) {
376 font_listeners.remove(l);
380 public static void set_serial_debug(boolean new_serial_debug) {
381 serial_debug = new_serial_debug;
382 AltosSerial.set_debug(serial_debug);
383 synchronized (preferences) {
384 preferences.putBoolean(serialDebugPreference, serial_debug);
389 public static boolean serial_debug() {
393 public static Preferences bt_devices() {
394 return preferences.node("bt_devices");
397 public static AltosFrequency[] common_frequencies() {
398 return common_frequencies;
401 public static void set_common_frequencies(AltosFrequency[] frequencies) {
402 common_frequencies = frequencies;
403 synchronized(preferences) {
404 save_common_frequencies(frequencies);
409 public static void add_common_frequency(AltosFrequency frequency) {
410 AltosFrequency[] new_frequencies = new AltosFrequency[common_frequencies.length + 1];
413 for (i = 0; i < common_frequencies.length; i++) {
414 if (frequency.frequency == common_frequencies[i].frequency)
416 if (frequency.frequency < common_frequencies[i].frequency)
418 new_frequencies[i] = common_frequencies[i];
420 new_frequencies[i] = frequency;
421 for (; i < common_frequencies.length; i++)
422 new_frequencies[i+1] = common_frequencies[i];
423 set_common_frequencies(new_frequencies);