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 public static AltosFrequency[] load_common_frequencies() {
97 AltosFrequency[] frequencies = null;
98 boolean existing = false;
100 existing = preferences.nodeExists(common_frequencies_node_name);
101 } catch (BackingStoreException be) {
105 Preferences node = preferences.node(common_frequencies_node_name);
106 int count = node.getInt(frequency_count, 0);
108 frequencies = new AltosFrequency[count];
109 for (int i = 0; i < count; i++) {
113 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
114 description = node.get(String.format(description_format, i), null);
115 frequencies[i] = new AltosFrequency(frequency, description);
118 frequencies = new AltosFrequency[10];
119 for (int i = 0; i < 10; i++) {
120 frequencies[i] = new AltosFrequency(434.550 + i * .1,
121 String.format("Channel %d", i));
127 public static void save_common_frequencies(AltosFrequency[] frequencies) {
128 Preferences node = preferences.node(common_frequencies_node_name);
130 node.putInt(frequency_count, frequencies.length);
131 for (int i = 0; i < frequencies.length; i++) {
132 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
133 node.put(String.format(description_format, i), frequencies[i].description);
136 public static int launcher_serial;
138 public static int launcher_channel;
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 << AltosLib.ao_telemetry_standard));
167 launcher_serial = preferences.getInt(launcherSerialPreference, 0);
169 launcher_channel = preferences.getInt(launcherChannelPreference, 0);
171 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
172 if (firmwaredir_string != null)
173 firmwaredir = new File(firmwaredir_string);
177 common_frequencies = load_common_frequencies();
183 public static void flush_preferences() {
186 } catch (BackingStoreException ee) {
188 if (component != null)
189 JOptionPane.showMessageDialog(component,
190 preferences.absolutePath(),
191 "Cannot save prefernces",
192 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 public static File logdir() {
214 public static File mapdir() {
218 public static void set_frequency(int serial, double new_frequency) {
219 frequencies.put(serial, new_frequency);
220 synchronized (preferences) {
221 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
226 public static double frequency(int serial) {
227 if (frequencies.containsKey(serial))
228 return frequencies.get(serial);
229 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
230 if (frequency == 0.0) {
231 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
232 frequency = AltosConvert.radio_channel_to_frequency(channel);
234 frequencies.put(serial, frequency);
238 public static void set_telemetry(int serial, int new_telemetry) {
239 telemetries.put(serial, new_telemetry);
240 synchronized (preferences) {
241 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
246 public static int telemetry(int serial) {
247 if (telemetries.containsKey(serial))
248 return telemetries.get(serial);
249 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
250 AltosLib.ao_telemetry_standard);
251 telemetries.put(serial, telemetry);
255 public static void set_scanning_telemetry(int new_scanning_telemetry) {
256 scanning_telemetry = new_scanning_telemetry;
257 synchronized (preferences) {
258 preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
263 public static int scanning_telemetry() {
264 return scanning_telemetry;
267 public static void set_voice(boolean new_voice) {
269 synchronized (preferences) {
270 preferences.putBoolean(voicePreference, voice);
275 public static boolean voice() {
279 public static void set_callsign(String new_callsign) {
280 callsign = new_callsign;
281 synchronized(preferences) {
282 preferences.put(callsignPreference, callsign);
287 public static String callsign() {
291 public static void set_firmwaredir(File new_firmwaredir) {
292 firmwaredir = new_firmwaredir;
293 synchronized (preferences) {
294 preferences.put(firmwaredirPreference, firmwaredir.getPath());
299 public static File firmwaredir() {
303 public static void set_launcher_serial(int new_launcher_serial) {
304 launcher_serial = new_launcher_serial;
305 synchronized (preferences) {
306 preferences.putInt(launcherSerialPreference, launcher_serial);
311 public static int launcher_serial() {
312 return launcher_serial;
315 public static void set_launcher_channel(int new_launcher_channel) {
316 launcher_channel = new_launcher_channel;
317 synchronized (preferences) {
318 preferences.putInt(launcherChannelPreference, launcher_channel);
323 public static int launcher_channel() {
324 return launcher_channel;
327 public static Preferences bt_devices() {
328 return preferences.node("bt_devices");
331 public static AltosFrequency[] common_frequencies() {
332 return common_frequencies;
335 public static void set_common_frequencies(AltosFrequency[] frequencies) {
336 common_frequencies = frequencies;
337 synchronized(preferences) {
338 save_common_frequencies(frequencies);
343 public static void add_common_frequency(AltosFrequency frequency) {
344 AltosFrequency[] new_frequencies = new AltosFrequency[common_frequencies.length + 1];
347 for (i = 0; i < common_frequencies.length; i++) {
348 if (frequency.frequency == common_frequencies[i].frequency)
350 if (frequency.frequency < common_frequencies[i].frequency)
352 new_frequencies[i] = common_frequencies[i];
354 new_frequencies[i] = frequency;
355 for (; i < common_frequencies.length; i++)
356 new_frequencies[i+1] = common_frequencies[i];
357 set_common_frequencies(new_frequencies);