Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / altosui / AltosPreferences.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19 import java.io.*;
20 import java.util.*;
21 import java.text.*;
22 import java.util.prefs.*;
23 import java.util.concurrent.LinkedBlockingQueue;
24 import java.awt.Component;
25 import javax.swing.*;
26 import javax.swing.filechooser.FileSystemView;
27
28 class AltosPreferences {
29         static Preferences preferences;
30
31         /* logdir preference name */
32         final static String logdirPreference = "LOGDIR";
33
34         /* channel preference name */
35         final static String channelPreferenceFormat = "CHANNEL-%d";
36
37         /* voice preference name */
38         final static String voicePreference = "VOICE";
39
40         /* callsign preference name */
41         final static String callsignPreference = "CALLSIGN";
42
43         /* firmware directory preference name */
44         final static String firmwaredirPreference = "FIRMWARE";
45
46         /* serial debug preference name */
47         final static String serialDebugPreference = "SERIAL-DEBUG";
48
49         /* Default logdir is ~/TeleMetrum */
50         final static String logdirName = "TeleMetrum";
51
52         /* UI Component to pop dialogs up */
53         static Component component;
54
55         /* Log directory */
56         static File logdir;
57
58         /* Map directory -- hangs of logdir */
59         static File mapdir;
60
61         /* Channel (map serial to channel) */
62         static Hashtable<Integer, Integer> channels;
63
64         /* Voice preference */
65         static boolean voice;
66
67         /* Callsign preference */
68         static String callsign;
69
70         /* Firmware directory */
71         static File firmwaredir;
72
73         /* Serial debug */
74         static boolean serial_debug;
75
76         public static void init(Component ui) {
77                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
78
79                 component = ui;
80
81                 /* Initialize logdir from preferences */
82                 String logdir_string = preferences.get(logdirPreference, null);
83                 if (logdir_string != null)
84                         logdir = new File(logdir_string);
85                 else {
86                         /* Use the file system view default directory */
87                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
88                         if (!logdir.exists())
89                                 logdir.mkdirs();
90                 }
91                 mapdir = new File(logdir, "maps");
92                 if (!mapdir.exists())
93                         mapdir.mkdirs();
94
95                 channels = new Hashtable<Integer,Integer>();
96
97                 voice = preferences.getBoolean(voicePreference, true);
98
99                 callsign = preferences.get(callsignPreference,"N0CALL");
100
101                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
102                 if (firmwaredir_string != null)
103                         firmwaredir = new File(firmwaredir_string);
104                 else
105                         firmwaredir = null;
106
107                 serial_debug = preferences.getBoolean(serialDebugPreference, false);
108                 AltosSerial.set_debug(serial_debug);
109         }
110
111         static void flush_preferences() {
112                 try {
113                         preferences.flush();
114                 } catch (BackingStoreException ee) {
115                         JOptionPane.showMessageDialog(component,
116                                                       preferences.absolutePath(),
117                                                       "Cannot save prefernces",
118                                                       JOptionPane.ERROR_MESSAGE);
119                 }
120         }
121
122         public static void set_logdir(File new_logdir) {
123                 logdir = new_logdir;
124                 mapdir = new File(logdir, "maps");
125                 if (!mapdir.exists())
126                         mapdir.mkdirs();
127                 synchronized (preferences) {
128                         preferences.put(logdirPreference, logdir.getPath());
129                         flush_preferences();
130                 }
131         }
132
133         private static boolean check_dir(File dir) {
134                 if (!dir.exists()) {
135                         if (!dir.mkdirs()) {
136                                 JOptionPane.showMessageDialog(component,
137                                                               dir.getName(),
138                                                               "Cannot create directory",
139                                                               JOptionPane.ERROR_MESSAGE);
140                                 return false;
141                         }
142                 } else if (!dir.isDirectory()) {
143                         JOptionPane.showMessageDialog(component,
144                                                       dir.getName(),
145                                                       "Is not a directory",
146                                                       JOptionPane.ERROR_MESSAGE);
147                         return false;
148                 }
149                 return true;
150         }
151
152         /* Configure the log directory. This is where all telemetry and eeprom files
153          * will be written to, and where replay will look for telemetry files
154          */
155         public static void ConfigureLog() {
156                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
157
158                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
159                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
160
161                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
162                         File dir = logdir_chooser.getSelectedFile();
163                         if (check_dir(dir))
164                                 set_logdir(dir);
165                 }
166         }
167
168         public static File logdir() {
169                 return logdir;
170         }
171
172         public static File mapdir() {
173                 return mapdir;
174         }
175
176         public static void set_channel(int serial, int new_channel) {
177                 channels.put(serial, new_channel);
178                 synchronized (preferences) {
179                         preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
180                         flush_preferences();
181                 }
182         }
183
184         public static int channel(int serial) {
185                 if (channels.containsKey(serial))
186                         return channels.get(serial);
187                 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
188                 channels.put(serial, channel);
189                 return channel;
190         }
191
192         public static void set_voice(boolean new_voice) {
193                 voice = new_voice;
194                 synchronized (preferences) {
195                         preferences.putBoolean(voicePreference, voice);
196                         flush_preferences();
197                 }
198         }
199
200         public static boolean voice() {
201                 return voice;
202         }
203
204         public static void set_callsign(String new_callsign) {
205                 callsign = new_callsign;
206                 synchronized(preferences) {
207                         preferences.put(callsignPreference, callsign);
208                         flush_preferences();
209                 }
210         }
211
212         public static String callsign() {
213                 return callsign;
214         }
215
216         public static void set_firmwaredir(File new_firmwaredir) {
217                 firmwaredir = new_firmwaredir;
218                 synchronized (preferences) {
219                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
220                         flush_preferences();
221                 }
222         }
223
224         public static File firmwaredir() {
225                 return firmwaredir;
226         }
227
228         public static void set_serial_debug(boolean new_serial_debug) {
229                 serial_debug = new_serial_debug;
230                 AltosSerial.set_debug(serial_debug);
231                 synchronized (preferences) {
232                         preferences.putBoolean(serialDebugPreference, serial_debug);
233                         flush_preferences();
234                 }
235         }
236
237         public static boolean serial_debug() {
238                 return serial_debug;
239         }
240 }