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(Component ui) {
83 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
87 /* Initialize logdir from preferences */
88 String logdir_string = preferences.get(logdirPreference, null);
89 if (logdir_string != null)
90 logdir = new File(logdir_string);
92 /* Use the file system view default directory */
93 logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
97 mapdir = new File(logdir, "maps");
101 channels = new Hashtable<Integer,Integer>();
103 telemetries = new Hashtable<Integer,Integer>();
105 voice = preferences.getBoolean(voicePreference, true);
107 callsign = preferences.get(callsignPreference,"N0CALL");
109 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
110 if (firmwaredir_string != null)
111 firmwaredir = new File(firmwaredir_string);
115 serial_debug = preferences.getBoolean(serialDebugPreference, false);
116 AltosSerial.set_debug(serial_debug);
119 static void flush_preferences() {
122 } catch (BackingStoreException ee) {
123 JOptionPane.showMessageDialog(component,
124 preferences.absolutePath(),
125 "Cannot save prefernces",
126 JOptionPane.ERROR_MESSAGE);
130 public static void set_logdir(File new_logdir) {
132 mapdir = new File(logdir, "maps");
133 if (!mapdir.exists())
135 synchronized (preferences) {
136 preferences.put(logdirPreference, logdir.getPath());
141 private static boolean check_dir(File dir) {
144 JOptionPane.showMessageDialog(component,
146 "Cannot create directory",
147 JOptionPane.ERROR_MESSAGE);
150 } else if (!dir.isDirectory()) {
151 JOptionPane.showMessageDialog(component,
153 "Is not a directory",
154 JOptionPane.ERROR_MESSAGE);
160 /* Configure the log directory. This is where all telemetry and eeprom files
161 * will be written to, and where replay will look for telemetry files
163 public static void ConfigureLog() {
164 JFileChooser logdir_chooser = new JFileChooser(logdir.getParentFile());
166 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
167 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
169 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
170 File dir = logdir_chooser.getSelectedFile();
176 public static File logdir() {
180 public static File mapdir() {
184 public static void set_channel(int serial, int new_channel) {
185 channels.put(serial, new_channel);
186 synchronized (preferences) {
187 preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
192 public static int channel(int serial) {
193 if (channels.containsKey(serial))
194 return channels.get(serial);
195 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
196 channels.put(serial, channel);
200 public static void set_telemetry(int serial, int new_telemetry) {
201 telemetries.put(serial, new_telemetry);
202 synchronized (preferences) {
203 preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
208 public static int telemetry(int serial) {
209 if (telemetries.containsKey(serial))
210 return telemetries.get(serial);
211 int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
212 Altos.ao_telemetry_full);
213 telemetries.put(serial, telemetry);
217 public static void set_voice(boolean new_voice) {
219 synchronized (preferences) {
220 preferences.putBoolean(voicePreference, voice);
225 public static boolean voice() {
229 public static void set_callsign(String new_callsign) {
230 callsign = new_callsign;
231 synchronized(preferences) {
232 preferences.put(callsignPreference, callsign);
237 public static String callsign() {
241 public static void set_firmwaredir(File new_firmwaredir) {
242 firmwaredir = new_firmwaredir;
243 synchronized (preferences) {
244 preferences.put(firmwaredirPreference, firmwaredir.getPath());
249 public static File firmwaredir() {
253 public static void set_serial_debug(boolean new_serial_debug) {
254 serial_debug = new_serial_debug;
255 AltosSerial.set_debug(serial_debug);
256 synchronized (preferences) {
257 preferences.putBoolean(serialDebugPreference, serial_debug);
262 public static boolean serial_debug() {