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;
22 import java.util.prefs.*;
23 import javax.swing.filechooser.FileSystemView;
25 public class AltosPreferences {
26 public static Preferences preferences;
28 /* logdir preference name */
29 public final static String logdirPreference = "LOGDIR";
31 /* channel preference name */
32 public final static String channelPreferenceFormat = "CHANNEL-%d";
34 /* frequency preference name */
35 public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
37 /* telemetry format preference name */
38 public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
40 /* voice preference name */
41 public final static String voicePreference = "VOICE";
43 /* callsign preference name */
44 public final static String callsignPreference = "CALLSIGN";
46 /* firmware directory preference name */
47 public final static String firmwaredirPreference = "FIRMWARE";
49 /* serial debug preference name */
50 public final static String serialDebugPreference = "SERIAL-DEBUG";
52 /* scanning telemetry preferences name */
53 public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
55 /* Launcher serial preference name */
56 public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
58 /* Launcher channel preference name */
59 public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
61 /* Default logdir is ~/TeleMetrum */
62 public final static String logdirName = "TeleMetrum";
65 public static File logdir;
67 /* Map directory -- hangs of logdir */
68 public static File mapdir;
70 /* Frequency (map serial to frequency) */
71 public static Hashtable<Integer, Double> frequencies;
73 /* Telemetry (map serial to telemetry format) */
74 public static Hashtable<Integer, Integer> telemetries;
76 /* Voice preference */
77 public static boolean voice;
79 /* Callsign preference */
80 public static String callsign;
82 /* Firmware directory */
83 public static File firmwaredir;
85 /* Scanning telemetry */
86 public static int scanning_telemetry;
88 /* List of frequencies */
89 public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
90 public static AltosFrequency[] common_frequencies;
92 public final static String frequency_count = "COUNT";
93 public final static String frequency_format = "FREQUENCY-%d";
94 public final static String description_format = "DESCRIPTION-%d";
96 /* Units preference */
98 public final static String unitsPreference = "IMPERIAL-UNITS";
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();
183 AltosConvert.imperial_units = preferences.getBoolean(unitsPreference, false);
188 public static void flush_preferences() {
191 } catch (BackingStoreException ee) {
193 if (component != null)
194 JOptionPane.showMessageDialog(component,
195 preferences.absolutePath(),
196 "Cannot save prefernces",
197 JOptionPane.ERROR_MESSAGE);
200 System.err.printf("Cannot save preferences\n");
204 public static void set_logdir(File new_logdir) {
206 mapdir = new File(logdir, "maps");
207 if (!mapdir.exists())
209 synchronized (preferences) {
210 preferences.put(logdirPreference, logdir.getPath());
215 public static File logdir() {
219 public static File mapdir() {
223 public static void set_frequency(int serial, double new_frequency) {
224 frequencies.put(serial, new_frequency);
225 synchronized (preferences) {
226 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
231 public static double frequency(int serial) {
232 if (frequencies.containsKey(serial))
233 return frequencies.get(serial);
234 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
235 if (frequency == 0.0) {
236 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
237 frequency = AltosConvert.radio_channel_to_frequency(channel);
239 frequencies.put(serial, frequency);
243 public static void set_telemetry(int serial, int new_telemetry) {
244 telemetries.put(serial, new_telemetry);
245 synchronized (preferences) {
246 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
251 public static int telemetry(int serial) {
252 if (telemetries.containsKey(serial))
253 return telemetries.get(serial);
254 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
255 AltosLib.ao_telemetry_standard);
256 telemetries.put(serial, telemetry);
260 public static void set_scanning_telemetry(int new_scanning_telemetry) {
261 scanning_telemetry = new_scanning_telemetry;
262 synchronized (preferences) {
263 preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
268 public static int scanning_telemetry() {
269 return scanning_telemetry;
272 public static void set_voice(boolean new_voice) {
274 synchronized (preferences) {
275 preferences.putBoolean(voicePreference, voice);
280 public static boolean voice() {
284 public static void set_callsign(String new_callsign) {
285 callsign = new_callsign;
286 synchronized(preferences) {
287 preferences.put(callsignPreference, callsign);
292 public static String callsign() {
296 public static void set_firmwaredir(File new_firmwaredir) {
297 firmwaredir = new_firmwaredir;
298 synchronized (preferences) {
299 preferences.put(firmwaredirPreference, firmwaredir.getPath());
304 public static File firmwaredir() {
308 public static void set_launcher_serial(int new_launcher_serial) {
309 launcher_serial = new_launcher_serial;
310 synchronized (preferences) {
311 preferences.putInt(launcherSerialPreference, launcher_serial);
316 public static int launcher_serial() {
317 return launcher_serial;
320 public static void set_launcher_channel(int new_launcher_channel) {
321 launcher_channel = new_launcher_channel;
322 synchronized (preferences) {
323 preferences.putInt(launcherChannelPreference, launcher_channel);
328 public static int launcher_channel() {
329 return launcher_channel;
332 public static Preferences bt_devices() {
333 return preferences.node("bt_devices");
336 public static AltosFrequency[] common_frequencies() {
337 return common_frequencies;
340 public static void set_common_frequencies(AltosFrequency[] frequencies) {
341 common_frequencies = frequencies;
342 synchronized(preferences) {
343 save_common_frequencies(frequencies);
348 public static void add_common_frequency(AltosFrequency frequency) {
349 AltosFrequency[] new_frequencies = new AltosFrequency[common_frequencies.length + 1];
352 for (i = 0; i < common_frequencies.length; i++) {
353 if (frequency.frequency == common_frequencies[i].frequency)
355 if (frequency.frequency < common_frequencies[i].frequency)
357 new_frequencies[i] = common_frequencies[i];
359 new_frequencies[i] = frequency;
360 for (; i < common_frequencies.length; i++)
361 new_frequencies[i+1] = common_frequencies[i];
362 set_common_frequencies(new_frequencies);
365 public static boolean imperial_units() {
366 return AltosConvert.imperial_units;
369 public static void set_imperial_units(boolean imperial_units) {
370 AltosConvert.imperial_units = imperial_units;
371 synchronized (preferences) {
372 preferences.putBoolean(unitsPreference, imperial_units);