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 public 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 /* Launcher serial preference name */
59 final static String launcherSerialPreference = "LAUNCHER-SERIAL";
61 /* Launcher channel preference name */
62 final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
64 /* Default logdir is ~/TeleMetrum */
65 final static String logdirName = "TeleMetrum";
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 /* List of frequencies */
95 final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
96 static AltosFrequency[] common_frequencies;
98 final static String frequency_count = "COUNT";
99 final static String frequency_format = "FREQUENCY-%d";
100 final static String description_format = "DESCRIPTION-%d";
102 static AltosFrequency[] load_common_frequencies() {
103 AltosFrequency[] frequencies = null;
104 boolean existing = false;
106 existing = preferences.nodeExists(common_frequencies_node_name);
107 } catch (BackingStoreException be) {
111 Preferences node = preferences.node(common_frequencies_node_name);
112 int count = node.getInt(frequency_count, 0);
114 frequencies = new AltosFrequency[count];
115 for (int i = 0; i < count; i++) {
119 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
120 description = node.get(String.format(description_format, i), null);
121 frequencies[i] = new AltosFrequency(frequency, description);
124 frequencies = new AltosFrequency[10];
125 for (int i = 0; i < 10; i++) {
126 frequencies[i] = new AltosFrequency(434.550 + i * .1,
127 String.format("Channel %d", i));
133 static void save_common_frequencies(AltosFrequency[] frequencies) {
134 Preferences node = preferences.node(common_frequencies_node_name);
136 node.putInt(frequency_count, frequencies.length);
137 for (int i = 0; i < frequencies.length; i++) {
138 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
139 node.put(String.format(description_format, i), frequencies[i].description);
142 static int launcher_serial;
144 static int launcher_channel;
146 public static void init() {
147 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
149 /* Initialize logdir from preferences */
150 String logdir_string = preferences.get(logdirPreference, null);
151 if (logdir_string != null)
152 logdir = new File(logdir_string);
154 /* Use the file system view default directory */
155 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
156 if (!logdir.exists())
159 mapdir = new File(logdir, "maps");
160 if (!mapdir.exists())
163 frequencies = new Hashtable<Integer, Double>();
165 telemetries = new Hashtable<Integer,Integer>();
167 voice = preferences.getBoolean(voicePreference, true);
169 callsign = preferences.get(callsignPreference,"N0CALL");
171 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << Altos.ao_telemetry_standard));
173 launcher_serial = preferences.getInt(launcherSerialPreference, 0);
175 launcher_channel = preferences.getInt(launcherChannelPreference, 0);
177 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
178 if (firmwaredir_string != null)
179 firmwaredir = new File(firmwaredir_string);
183 serial_debug = preferences.getBoolean(serialDebugPreference, false);
184 AltosSerial.set_debug(serial_debug);
186 common_frequencies = load_common_frequencies();
192 static void flush_preferences() {
195 } catch (BackingStoreException ee) {
197 if (component != null)
198 JOptionPane.showMessageDialog(component,
199 preferences.absolutePath(),
200 "Cannot save prefernces",
201 JOptionPane.ERROR_MESSAGE);
204 System.err.printf("Cannot save preferences\n");
208 public static void set_logdir(File new_logdir) {
210 mapdir = new File(logdir, "maps");
211 if (!mapdir.exists())
213 synchronized (preferences) {
214 preferences.put(logdirPreference, logdir.getPath());
219 public static File logdir() {
223 public static File mapdir() {
227 public static void set_frequency(int serial, double new_frequency) {
228 frequencies.put(serial, new_frequency);
229 synchronized (preferences) {
230 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
235 public static double frequency(int serial) {
236 if (frequencies.containsKey(serial))
237 return frequencies.get(serial);
238 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
239 if (frequency == 0.0) {
240 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
241 frequency = AltosConvert.radio_channel_to_frequency(channel);
243 frequencies.put(serial, frequency);
247 public static void set_telemetry(int serial, int new_telemetry) {
248 telemetries.put(serial, new_telemetry);
249 synchronized (preferences) {
250 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
255 public static int telemetry(int serial) {
256 if (telemetries.containsKey(serial))
257 return telemetries.get(serial);
258 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
259 Altos.ao_telemetry_standard);
260 telemetries.put(serial, telemetry);
264 public static void set_scanning_telemetry(int new_scanning_telemetry) {
265 scanning_telemetry = new_scanning_telemetry;
266 synchronized (preferences) {
267 preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
272 public static int scanning_telemetry() {
273 return scanning_telemetry;
276 public static void set_voice(boolean new_voice) {
278 synchronized (preferences) {
279 preferences.putBoolean(voicePreference, voice);
284 public static boolean voice() {
288 public static void set_callsign(String new_callsign) {
289 callsign = new_callsign;
290 synchronized(preferences) {
291 preferences.put(callsignPreference, callsign);
296 public static String callsign() {
300 public static void set_firmwaredir(File new_firmwaredir) {
301 firmwaredir = new_firmwaredir;
302 synchronized (preferences) {
303 preferences.put(firmwaredirPreference, firmwaredir.getPath());
308 public static File firmwaredir() {
312 public static void set_serial_debug(boolean new_serial_debug) {
313 serial_debug = new_serial_debug;
314 AltosSerial.set_debug(serial_debug);
315 synchronized (preferences) {
316 preferences.putBoolean(serialDebugPreference, serial_debug);
321 public static boolean serial_debug() {
325 public static void set_launcher_serial(int new_launcher_serial) {
326 launcher_serial = new_launcher_serial;
327 System.out.printf("set launcher serial to %d\n", new_launcher_serial);
328 synchronized (preferences) {
329 preferences.putInt(launcherSerialPreference, launcher_serial);
334 public static int launcher_serial() {
335 return launcher_serial;
338 public static void set_launcher_channel(int new_launcher_channel) {
339 launcher_channel = new_launcher_channel;
340 System.out.printf("set launcher channel to %d\n", new_launcher_channel);
341 synchronized (preferences) {
342 preferences.putInt(launcherChannelPreference, launcher_channel);
347 public static int launcher_channel() {
348 return launcher_channel;
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);