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 /* Default logdir is ~/TeleMetrum */
56 final static String logdirName = "TeleMetrum";
58 /* UI Component to pop dialogs up */
59 static Component component;
64 /* Map directory -- hangs of logdir */
67 /* Frequency (map serial to frequency) */
68 static Hashtable<Integer, Double> frequencies;
70 /* Telemetry (map serial to telemetry format) */
71 static Hashtable<Integer, Integer> telemetries;
73 /* Voice preference */
76 /* Callsign preference */
77 static String callsign;
79 /* Firmware directory */
80 static File firmwaredir;
83 static boolean serial_debug;
85 /* List of frequencies */
86 final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
87 static AltosFrequency[] common_frequencies;
89 final static String frequency_count = "COUNT";
90 final static String frequency_format = "FREQUENCY-%d";
91 final static String description_format = "DESCRIPTION-%d";
93 static AltosFrequency[] load_common_frequencies() {
94 AltosFrequency[] frequencies = null;
95 boolean existing = false;
97 existing = preferences.nodeExists(common_frequencies_node_name);
98 } catch (BackingStoreException be) {
102 Preferences node = preferences.node(common_frequencies_node_name);
103 int count = node.getInt(frequency_count, 0);
105 frequencies = new AltosFrequency[count];
106 for (int i = 0; i < count; i++) {
110 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
111 description = node.get(String.format(description_format, i), null);
112 frequencies[i] = new AltosFrequency(frequency, description);
115 frequencies = new AltosFrequency[10];
116 for (int i = 0; i < 10; i++) {
117 frequencies[i] = new AltosFrequency(434.550 + i * .1,
118 String.format("Channel %d", i));
124 static void save_common_frequencies(AltosFrequency[] frequencies) {
125 Preferences node = preferences.node(common_frequencies_node_name);
127 node.putInt(frequency_count, frequencies.length);
128 for (int i = 0; i < frequencies.length; i++) {
129 node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
130 node.put(String.format(description_format, i), frequencies[i].description);
134 public static void init() {
135 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
137 /* Initialize logdir from preferences */
138 String logdir_string = preferences.get(logdirPreference, null);
139 if (logdir_string != null)
140 logdir = new File(logdir_string);
142 /* Use the file system view default directory */
143 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
144 if (!logdir.exists())
147 mapdir = new File(logdir, "maps");
148 if (!mapdir.exists())
151 frequencies = new Hashtable<Integer, Double>();
153 telemetries = new Hashtable<Integer,Integer>();
155 voice = preferences.getBoolean(voicePreference, true);
157 callsign = preferences.get(callsignPreference,"N0CALL");
159 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
160 if (firmwaredir_string != null)
161 firmwaredir = new File(firmwaredir_string);
165 serial_debug = preferences.getBoolean(serialDebugPreference, false);
166 AltosSerial.set_debug(serial_debug);
168 common_frequencies = load_common_frequencies();
173 static void set_component(Component in_component) {
174 component = in_component;
177 static void flush_preferences() {
180 } catch (BackingStoreException ee) {
181 if (component != null)
182 JOptionPane.showMessageDialog(component,
183 preferences.absolutePath(),
184 "Cannot save prefernces",
185 JOptionPane.ERROR_MESSAGE);
187 System.err.printf("Cannot save preferences\n");
191 public static void set_logdir(File new_logdir) {
193 mapdir = new File(logdir, "maps");
194 if (!mapdir.exists())
196 synchronized (preferences) {
197 preferences.put(logdirPreference, logdir.getPath());
202 private static boolean check_dir(File dir) {
205 JOptionPane.showMessageDialog(component,
207 "Cannot create directory",
208 JOptionPane.ERROR_MESSAGE);
211 } else if (!dir.isDirectory()) {
212 JOptionPane.showMessageDialog(component,
214 "Is not a directory",
215 JOptionPane.ERROR_MESSAGE);
221 /* Configure the log directory. This is where all telemetry and eeprom files
222 * will be written to, and where replay will look for telemetry files
224 public static void ConfigureLog() {
225 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
227 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
228 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
230 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
231 File dir = logdir_chooser.getSelectedFile();
237 public static File logdir() {
241 public static File mapdir() {
245 public static void set_frequency(int serial, double new_frequency) {
246 frequencies.put(serial, new_frequency);
247 synchronized (preferences) {
248 preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
253 public static double frequency(int serial) {
254 if (frequencies.containsKey(serial))
255 return frequencies.get(serial);
256 double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
257 if (frequency == 0.0) {
258 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
259 frequency = AltosConvert.radio_channel_to_frequency(channel);
261 frequencies.put(serial, frequency);
265 public static void set_telemetry(int serial, int new_telemetry) {
266 telemetries.put(serial, new_telemetry);
267 synchronized (preferences) {
268 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
273 public static int telemetry(int serial) {
274 if (telemetries.containsKey(serial))
275 return telemetries.get(serial);
276 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
277 Altos.ao_telemetry_standard);
278 telemetries.put(serial, telemetry);
282 public static void set_voice(boolean new_voice) {
284 synchronized (preferences) {
285 preferences.putBoolean(voicePreference, voice);
290 public static boolean voice() {
294 public static void set_callsign(String new_callsign) {
295 callsign = new_callsign;
296 synchronized(preferences) {
297 preferences.put(callsignPreference, callsign);
302 public static String callsign() {
306 public static void set_firmwaredir(File new_firmwaredir) {
307 firmwaredir = new_firmwaredir;
308 synchronized (preferences) {
309 preferences.put(firmwaredirPreference, firmwaredir.getPath());
314 public static File firmwaredir() {
318 public static void set_serial_debug(boolean new_serial_debug) {
319 serial_debug = new_serial_debug;
320 AltosSerial.set_debug(serial_debug);
321 synchronized (preferences) {
322 preferences.putBoolean(serialDebugPreference, serial_debug);
327 public static boolean serial_debug() {
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);