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 /* telemetry format preference name */
38 final static String telemetryPreferenceFormat = "TELEMETRY-%d";
40 /* voice preference name */
41 final static String voicePreference = "VOICE";
43 /* callsign preference name */
44 final static String callsignPreference = "CALLSIGN";
46 /* firmware directory preference name */
47 final static String firmwaredirPreference = "FIRMWARE";
49 /* serial debug preference name */
50 final static String serialDebugPreference = "SERIAL-DEBUG";
52 /* Default logdir is ~/TeleMetrum */
53 final static String logdirName = "TeleMetrum";
55 /* UI Component to pop dialogs up */
56 static Component component;
61 /* Map directory -- hangs of logdir */
64 /* Channel (map serial to channel) */
65 static Hashtable<Integer, Integer> channels;
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 channels = new Hashtable<Integer,Integer>();
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_channel(int serial, int new_channel) {
246 channels.put(serial, new_channel);
247 synchronized (preferences) {
248 preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
253 public static int channel(int serial) {
254 if (channels.containsKey(serial))
255 return channels.get(serial);
256 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
257 channels.put(serial, channel);
261 public static void set_telemetry(int serial, int new_telemetry) {
262 telemetries.put(serial, new_telemetry);
263 synchronized (preferences) {
264 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
269 public static int telemetry(int serial) {
270 if (telemetries.containsKey(serial))
271 return telemetries.get(serial);
272 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
273 Altos.ao_telemetry_standard);
274 telemetries.put(serial, telemetry);
278 public static void set_voice(boolean new_voice) {
280 synchronized (preferences) {
281 preferences.putBoolean(voicePreference, voice);
286 public static boolean voice() {
290 public static void set_callsign(String new_callsign) {
291 callsign = new_callsign;
292 synchronized(preferences) {
293 preferences.put(callsignPreference, callsign);
298 public static String callsign() {
302 public static void set_firmwaredir(File new_firmwaredir) {
303 firmwaredir = new_firmwaredir;
304 synchronized (preferences) {
305 preferences.put(firmwaredirPreference, firmwaredir.getPath());
310 public static File firmwaredir() {
314 public static void set_serial_debug(boolean new_serial_debug) {
315 serial_debug = new_serial_debug;
316 AltosSerial.set_debug(serial_debug);
317 synchronized (preferences) {
318 preferences.putBoolean(serialDebugPreference, serial_debug);
323 public static boolean serial_debug() {
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);