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 /* Telemetry (map serial to telemetry format) */
68 static Hashtable<Integer, Integer> telemetries;
70 /* Voice preference */
73 /* Callsign preference */
74 static String callsign;
76 /* Firmware directory */
77 static File firmwaredir;
80 static boolean serial_debug;
82 public static void init() {
83 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
85 /* Initialize logdir from preferences */
86 String logdir_string = preferences.get(logdirPreference, null);
87 if (logdir_string != null)
88 logdir = new File(logdir_string);
90 /* Use the file system view default directory */
91 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
95 mapdir = new File(logdir, "maps");
99 channels = new Hashtable<Integer,Integer>();
101 telemetries = new Hashtable<Integer,Integer>();
103 voice = preferences.getBoolean(voicePreference, true);
105 callsign = preferences.get(callsignPreference,"N0CALL");
107 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
108 if (firmwaredir_string != null)
109 firmwaredir = new File(firmwaredir_string);
113 serial_debug = preferences.getBoolean(serialDebugPreference, false);
114 AltosSerial.set_debug(serial_debug);
119 static void set_component(Component in_component) {
120 component = in_component;
123 static void flush_preferences() {
126 } catch (BackingStoreException ee) {
127 if (component != null)
128 JOptionPane.showMessageDialog(component,
129 preferences.absolutePath(),
130 "Cannot save prefernces",
131 JOptionPane.ERROR_MESSAGE);
133 System.err.printf("Cannot save preferences\n");
137 public static void set_logdir(File new_logdir) {
139 mapdir = new File(logdir, "maps");
140 if (!mapdir.exists())
142 synchronized (preferences) {
143 preferences.put(logdirPreference, logdir.getPath());
148 private static boolean check_dir(File dir) {
151 JOptionPane.showMessageDialog(component,
153 "Cannot create directory",
154 JOptionPane.ERROR_MESSAGE);
157 } else if (!dir.isDirectory()) {
158 JOptionPane.showMessageDialog(component,
160 "Is not a directory",
161 JOptionPane.ERROR_MESSAGE);
167 /* Configure the log directory. This is where all telemetry and eeprom files
168 * will be written to, and where replay will look for telemetry files
170 public static void ConfigureLog() {
171 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
173 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
174 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
176 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
177 File dir = logdir_chooser.getSelectedFile();
183 public static File logdir() {
187 public static File mapdir() {
191 public static void set_channel(int serial, int new_channel) {
192 channels.put(serial, new_channel);
193 synchronized (preferences) {
194 preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
199 public static int channel(int serial) {
200 if (channels.containsKey(serial))
201 return channels.get(serial);
202 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
203 channels.put(serial, channel);
207 public static void set_telemetry(int serial, int new_telemetry) {
208 telemetries.put(serial, new_telemetry);
209 synchronized (preferences) {
210 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
215 public static int telemetry(int serial) {
216 if (telemetries.containsKey(serial))
217 return telemetries.get(serial);
218 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
219 Altos.ao_telemetry_split);
220 telemetries.put(serial, telemetry);
224 public static void set_voice(boolean new_voice) {
226 synchronized (preferences) {
227 preferences.putBoolean(voicePreference, voice);
232 public static boolean voice() {
236 public static void set_callsign(String new_callsign) {
237 callsign = new_callsign;
238 synchronized(preferences) {
239 preferences.put(callsignPreference, callsign);
244 public static String callsign() {
248 public static void set_firmwaredir(File new_firmwaredir) {
249 firmwaredir = new_firmwaredir;
250 synchronized (preferences) {
251 preferences.put(firmwaredirPreference, firmwaredir.getPath());
256 public static File firmwaredir() {
260 public static void set_serial_debug(boolean new_serial_debug) {
261 serial_debug = new_serial_debug;
262 AltosSerial.set_debug(serial_debug);
263 synchronized (preferences) {
264 preferences.putBoolean(serialDebugPreference, serial_debug);
269 public static boolean serial_debug() {
273 public static Preferences bt_devices() {
274 return preferences.node("bt_devices");