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.
18 package org.altusmetrum.AltosLib;
23 import java.util.prefs.*;
24 import java.util.concurrent.LinkedBlockingQueue;
25 import java.awt.Component;
27 import javax.swing.filechooser.FileSystemView;
29 public class AltosPreferences {
30 public static Preferences preferences;
32 /* logdir preference name */
33 public final static String logdirPreference = "LOGDIR";
35 /* channel preference name */
36 public final static String channelPreferenceFormat = "CHANNEL-%d";
38 /* frequency preference name */
39 public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
41 /* telemetry format preference name */
42 public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
44 /* voice preference name */
45 public final static String voicePreference = "VOICE";
47 /* callsign preference name */
48 public final static String callsignPreference = "CALLSIGN";
50 /* firmware directory preference name */
51 public final static String firmwaredirPreference = "FIRMWARE";
53 /* serial debug preference name */
54 public final static String serialDebugPreference = "SERIAL-DEBUG";
56 /* scanning telemetry preferences name */
57 public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
59 /* Launcher serial preference name */
60 public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
62 /* Launcher channel preference name */
63 public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
65 /* Default logdir is ~/TeleMetrum */
66 public final static String logdirName = "TeleMetrum";
69 public static File logdir;
71 /* Map directory -- hangs of logdir */
72 public static File mapdir;
74 /* Frequency (map serial to frequency) */
75 public static Hashtable<Integer, Double> frequencies;
77 /* Telemetry (map serial to telemetry format) */
78 public static Hashtable<Integer, Integer> telemetries;
80 /* Voice preference */
81 public static boolean voice;
83 /* Callsign preference */
84 public static String callsign;
86 /* Firmware directory */
87 public static File firmwaredir;
89 /* Scanning telemetry */
90 public static int scanning_telemetry;
92 /* List of frequencies */
93 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
94 public static AltosFrequency[] common_frequencies;
96 public final static String frequency_count = "COUNT";
97 public final static String frequency_format = "FREQUENCY-%d";
98 public final static String description_format = "DESCRIPTION-%d";
100 public static AltosFrequency[] load_common_frequencies() {
101 AltosFrequency[] frequencies = null;
102 boolean existing = false;
104 existing = preferences.nodeExists(common_frequencies_node_name);
105 } catch (BackingStoreException be) {
109 Preferences node = preferences.node(common_frequencies_node_name);
110 int count = node.getInt(frequency_count, 0);
112 frequencies = new AltosFrequency[count];
113 for (int i = 0; i < count; i++) {
117 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
118 description = node.get(String.format(description_format, i), null);
119 frequencies[i] = new AltosFrequency(frequency, description);
122 frequencies = new AltosFrequency[10];
123 for (int i = 0; i < 10; i++) {
124 frequencies[i] = new AltosFrequency(434.550 + i * .1,
125 String.format("Channel %d", i));
131 public static void save_common_frequencies(AltosFrequency[] frequencies) {
132 Preferences node = preferences.node(common_frequencies_node_name);
134 node.putInt(frequency_count, frequencies.length);
135 for (int i = 0; i < frequencies.length; i++) {
136 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
137 node.put(String.format(description_format, i), frequencies[i].description);
140 public static int launcher_serial;
142 public static int launcher_channel;
144 public static void init() {
145 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
147 /* Initialize logdir from preferences */
148 String logdir_string = preferences.get(logdirPreference, null);
149 if (logdir_string != null)
150 logdir = new File(logdir_string);
152 /* Use the file system view default directory */
153 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
154 if (!logdir.exists())
157 mapdir = new File(logdir, "maps");
158 if (!mapdir.exists())
161 frequencies = new Hashtable<Integer, Double>();
163 telemetries = new Hashtable<Integer,Integer>();
165 voice = preferences.getBoolean(voicePreference, true);
167 callsign = preferences.get(callsignPreference,"N0CALL");
169 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
171 launcher_serial = preferences.getInt(launcherSerialPreference, 0);
173 launcher_channel = preferences.getInt(launcherChannelPreference, 0);
175 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
176 if (firmwaredir_string != null)
177 firmwaredir = new File(firmwaredir_string);
181 common_frequencies = load_common_frequencies();
187 public static void flush_preferences() {
190 } catch (BackingStoreException ee) {
192 if (component != null)
193 JOptionPane.showMessageDialog(component,
194 preferences.absolutePath(),
195 "Cannot save prefernces",
196 JOptionPane.ERROR_MESSAGE);
199 System.err.printf("Cannot save preferences\n");
203 public static void set_logdir(File new_logdir) {
205 mapdir = new File(logdir, "maps");
206 if (!mapdir.exists())
208 synchronized (preferences) {
209 preferences.put(logdirPreference, logdir.getPath());
214 public static File logdir() {
218 public static File mapdir() {
222 public static void set_frequency(int serial, double new_frequency) {
223 frequencies.put(serial, new_frequency);
224 synchronized (preferences) {
225 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
230 public static double frequency(int serial) {
231 if (frequencies.containsKey(serial))
232 return frequencies.get(serial);
233 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
234 if (frequency == 0.0) {
235 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
236 frequency = AltosConvert.radio_channel_to_frequency(channel);
238 frequencies.put(serial, frequency);
242 public static void set_telemetry(int serial, int new_telemetry) {
243 telemetries.put(serial, new_telemetry);
244 synchronized (preferences) {
245 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
250 public static int telemetry(int serial) {
251 if (telemetries.containsKey(serial))
252 return telemetries.get(serial);
253 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
254 AltosLib.ao_telemetry_standard);
255 telemetries.put(serial, telemetry);
259 public static void set_scanning_telemetry(int new_scanning_telemetry) {
260 scanning_telemetry = new_scanning_telemetry;
261 synchronized (preferences) {
262 preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
267 public static int scanning_telemetry() {
268 return scanning_telemetry;
271 public static void set_voice(boolean new_voice) {
273 synchronized (preferences) {
274 preferences.putBoolean(voicePreference, voice);
279 public static boolean voice() {
283 public static void set_callsign(String new_callsign) {
284 callsign = new_callsign;
285 synchronized(preferences) {
286 preferences.put(callsignPreference, callsign);
291 public static String callsign() {
295 public static void set_firmwaredir(File new_firmwaredir) {
296 firmwaredir = new_firmwaredir;
297 synchronized (preferences) {
298 preferences.put(firmwaredirPreference, firmwaredir.getPath());
303 public static File firmwaredir() {
307 public static void set_launcher_serial(int new_launcher_serial) {
308 launcher_serial = new_launcher_serial;
309 synchronized (preferences) {
310 preferences.putInt(launcherSerialPreference, launcher_serial);
315 public static int launcher_serial() {
316 return launcher_serial;
319 public static void set_launcher_channel(int new_launcher_channel) {
320 launcher_channel = new_launcher_channel;
321 synchronized (preferences) {
322 preferences.putInt(launcherChannelPreference, launcher_channel);
327 public static int launcher_channel() {
328 return launcher_channel;
331 public static Preferences bt_devices() {
332 return preferences.node("bt_devices");
335 public static AltosFrequency[] common_frequencies() {
336 return common_frequencies;
339 public static void set_common_frequencies(AltosFrequency[] frequencies) {
340 common_frequencies = frequencies;
341 synchronized(preferences) {
342 save_common_frequencies(frequencies);
347 public static void add_common_frequency(AltosFrequency frequency) {
348 AltosFrequency[] new_frequencies = new AltosFrequency[common_frequencies.length + 1];
351 for (i = 0; i < common_frequencies.length; i++) {
352 if (frequency.frequency == common_frequencies[i].frequency)
354 if (frequency.frequency < common_frequencies[i].frequency)
356 new_frequencies[i] = common_frequencies[i];
358 new_frequencies[i] = frequency;
359 for (; i < common_frequencies.length; i++)
360 new_frequencies[i+1] = common_frequencies[i];
361 set_common_frequencies(new_frequencies);