altos: Need to use 16-bit counts for ao_xmem functions
[fw/altos] / altoslib / 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 org.altusmetrum.AltosLib;
19
20 import java.io.*;
21 import java.util.*;
22 import java.util.prefs.*;
23 import javax.swing.filechooser.FileSystemView;
24
25 public class AltosPreferences {
26         public static Preferences preferences;
27
28         /* logdir preference name */
29         public final static String logdirPreference = "LOGDIR";
30
31         /* channel preference name */
32         public final static String channelPreferenceFormat = "CHANNEL-%d";
33
34         /* frequency preference name */
35         public final static String frequencyPreferenceFormat = "FREQUENCY-%d";
36
37         /* telemetry format preference name */
38         public final static String telemetryPreferenceFormat = "TELEMETRY-%d";
39
40         /* voice preference name */
41         public final static String voicePreference = "VOICE";
42
43         /* callsign preference name */
44         public final static String callsignPreference = "CALLSIGN";
45
46         /* firmware directory preference name */
47         public final static String firmwaredirPreference = "FIRMWARE";
48
49         /* serial debug preference name */
50         public final static String serialDebugPreference = "SERIAL-DEBUG";
51
52         /* scanning telemetry preferences name */
53         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
54
55         /* Launcher serial preference name */
56         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
57
58         /* Launcher channel preference name */
59         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
60         
61         /* Default logdir is ~/TeleMetrum */
62         public final static String logdirName = "TeleMetrum";
63
64         /* Log directory */
65         public static File logdir;
66
67         /* Map directory -- hangs of logdir */
68         public static File mapdir;
69
70         /* Frequency (map serial to frequency) */
71         public static Hashtable<Integer, Double> frequencies;
72
73         /* Telemetry (map serial to telemetry format) */
74         public static Hashtable<Integer, Integer> telemetries;
75
76         /* Voice preference */
77         public static boolean voice;
78
79         /* Callsign preference */
80         public static String callsign;
81
82         /* Firmware directory */
83         public static File firmwaredir;
84
85         /* Scanning telemetry */
86         public static int scanning_telemetry;
87
88         /* List of frequencies */
89         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
90         public static AltosFrequency[] common_frequencies;
91
92         public final static String      frequency_count = "COUNT";
93         public final static String      frequency_format = "FREQUENCY-%d";
94         public final static String      description_format = "DESCRIPTION-%d";
95
96         /* Units preference */
97
98         public final static String      unitsPreference = "IMPERIAL-UNITS";
99
100         public static AltosFrequency[] load_common_frequencies() {
101                 AltosFrequency[] frequencies = null;
102                 boolean existing = false;
103                 try {
104                         existing = preferences.nodeExists(common_frequencies_node_name);
105                 } catch (BackingStoreException be) {
106                         existing = false;
107                 }
108                 if (existing) {
109                         Preferences     node = preferences.node(common_frequencies_node_name);
110                         int             count = node.getInt(frequency_count, 0);
111
112                         frequencies = new AltosFrequency[count];
113                         for (int i = 0; i < count; i++) {
114                                 double  frequency;
115                                 String  description;
116
117                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
118                                 description = node.get(String.format(description_format, i), null);
119                                 frequencies[i] = new AltosFrequency(frequency, description);
120                         }
121                 } else {
122                         frequencies = new AltosFrequency[10];
123                         for (int i = 0; i < 10; i++) {
124                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
125                                                                            String.format("Channel %d", i));
126                         }
127                 }
128                 return frequencies;
129         }
130
131         public static void save_common_frequencies(AltosFrequency[] frequencies) {
132                 Preferences     node = preferences.node(common_frequencies_node_name);
133
134                 node.putInt(frequency_count, frequencies.length);
135                 for (int i = 0; i < frequencies.length; i++) {
136                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
137                         node.put(String.format(description_format, i), frequencies[i].description);
138                 }
139         }
140         public static int launcher_serial;
141
142         public static int launcher_channel;
143
144         public static void init() {
145                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
146
147                 /* Initialize logdir from preferences */
148                 String logdir_string = preferences.get(logdirPreference, null);
149                 if (logdir_string != null)
150                         logdir = new File(logdir_string);
151                 else {
152                         /* Use the file system view default directory */
153                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
154                         if (!logdir.exists())
155                                 logdir.mkdirs();
156                 }
157                 mapdir = new File(logdir, "maps");
158                 if (!mapdir.exists())
159                         mapdir.mkdirs();
160
161                 frequencies = new Hashtable<Integer, Double>();
162
163                 telemetries = new Hashtable<Integer,Integer>();
164
165                 voice = preferences.getBoolean(voicePreference, true);
166
167                 callsign = preferences.get(callsignPreference,"N0CALL");
168
169                 scanning_telemetry = preferences.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
170
171                 launcher_serial = preferences.getInt(launcherSerialPreference, 0);
172
173                 launcher_channel = preferences.getInt(launcherChannelPreference, 0);
174
175                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
176                 if (firmwaredir_string != null)
177                         firmwaredir = new File(firmwaredir_string);
178                 else
179                         firmwaredir = null;
180
181                 common_frequencies = load_common_frequencies();
182
183                 AltosConvert.imperial_units = preferences.getBoolean(unitsPreference, false);
184         }
185
186         static { init(); }
187
188         public static void flush_preferences() {
189                 try {
190                         preferences.flush();
191                 } catch (BackingStoreException ee) {
192 /*
193                         if (component != null)
194                                 JOptionPane.showMessageDialog(component,
195                                                               preferences.absolutePath(),
196                                                               "Cannot save prefernces",
197                                                               JOptionPane.ERROR_MESSAGE);
198                         else
199 */
200                                 System.err.printf("Cannot save preferences\n");
201                 }
202         }
203
204         public static void set_logdir(File new_logdir) {
205                 synchronized (preferences) {
206                         logdir = new_logdir;
207                         mapdir = new File(logdir, "maps");
208                         if (!mapdir.exists())
209                                 mapdir.mkdirs();
210                         preferences.put(logdirPreference, logdir.getPath());
211                         flush_preferences();
212                 }
213         }
214
215         public static File logdir() {
216                 synchronized (preferences) {
217                         return logdir;
218                 }
219         }
220
221         public static File mapdir() {
222                 synchronized (preferences) {
223                         return mapdir;
224                 }
225         }
226
227         public static void set_frequency(int serial, double new_frequency) {
228                 synchronized (preferences) {
229                         frequencies.put(serial, new_frequency);
230                         preferences.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
231                         flush_preferences();
232                 }
233         }
234
235         public static double frequency(int serial) {
236                 synchronized (preferences) {
237                         if (frequencies.containsKey(serial))
238                                 return frequencies.get(serial);
239                         double frequency = preferences.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
240                         if (frequency == 0.0) {
241                                 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
242                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
243                         }
244                         frequencies.put(serial, frequency);
245                         return frequency;
246                 }
247         }
248
249         public static void set_telemetry(int serial, int new_telemetry) {
250                 synchronized (preferences) {
251                         telemetries.put(serial, new_telemetry);
252                         preferences.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
253                         flush_preferences();
254                 }
255         }
256
257         public static int telemetry(int serial) {
258                 synchronized (preferences) {
259                         if (telemetries.containsKey(serial))
260                                 return telemetries.get(serial);
261                         int telemetry = preferences.getInt(String.format(telemetryPreferenceFormat, serial),
262                                                            AltosLib.ao_telemetry_standard);
263                         telemetries.put(serial, telemetry);
264                         return telemetry;
265                 }
266         }
267
268         public static void set_scanning_telemetry(int new_scanning_telemetry) {
269                 synchronized (preferences) {
270                         scanning_telemetry = new_scanning_telemetry;
271                         preferences.putInt(scanningTelemetryPreference, scanning_telemetry);
272                         flush_preferences();
273                 }
274         }
275
276         public static int scanning_telemetry() {
277                 synchronized (preferences) {
278                         return scanning_telemetry;
279                 }
280         }
281
282         public static void set_voice(boolean new_voice) {
283                 synchronized (preferences) {
284                         voice = new_voice;
285                         preferences.putBoolean(voicePreference, voice);
286                         flush_preferences();
287                 }
288         }
289
290         public static boolean voice() {
291                 synchronized (preferences) {
292                         return voice;
293                 }
294         }
295
296         public static void set_callsign(String new_callsign) {
297                 synchronized(preferences) {
298                         callsign = new_callsign;
299                         preferences.put(callsignPreference, callsign);
300                         flush_preferences();
301                 }
302         }
303
304         public static String callsign() {
305                 synchronized(preferences) {
306                         return callsign;
307                 }
308         }
309
310         public static void set_firmwaredir(File new_firmwaredir) {
311                 synchronized (preferences) {
312                         firmwaredir = new_firmwaredir;
313                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
314                         flush_preferences();
315                 }
316         }
317
318         public static File firmwaredir() {
319                 synchronized (preferences) {
320                         return firmwaredir;
321                 }
322         }
323
324         public static void set_launcher_serial(int new_launcher_serial) {
325                 synchronized (preferences) {
326                         launcher_serial = new_launcher_serial;
327                         preferences.putInt(launcherSerialPreference, launcher_serial);
328                         flush_preferences();
329                 }
330         }
331
332         public static int launcher_serial() {
333                 synchronized (preferences) {
334                         return launcher_serial;
335                 }
336         }
337
338         public static void set_launcher_channel(int new_launcher_channel) {
339                 synchronized (preferences) {
340                         launcher_channel = new_launcher_channel;
341                         preferences.putInt(launcherChannelPreference, launcher_channel);
342                         flush_preferences();
343                 }
344         }
345
346         public static int launcher_channel() {
347                 synchronized (preferences) {
348                         return launcher_channel;
349                 }
350         }
351         
352         public static Preferences bt_devices() {
353                 synchronized (preferences) {
354                         return preferences.node("bt_devices");
355                 }
356         }
357
358         public static AltosFrequency[] common_frequencies() {
359                 synchronized (preferences) {
360                         return common_frequencies;
361                 }
362         }
363
364         public static void set_common_frequencies(AltosFrequency[] frequencies) {
365                 synchronized(preferences) {
366                         common_frequencies = frequencies;
367                         save_common_frequencies(frequencies);
368                         flush_preferences();
369                 }
370         }
371
372         public static void add_common_frequency(AltosFrequency frequency) {
373                 AltosFrequency[]        old_frequencies = common_frequencies();
374                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
375                 int                     i;
376
377                 for (i = 0; i < old_frequencies.length; i++) {
378                         if (frequency.frequency == old_frequencies[i].frequency)
379                                 return;
380                         if (frequency.frequency < old_frequencies[i].frequency)
381                                 break;
382                         new_frequencies[i] = old_frequencies[i];
383                 }
384                 new_frequencies[i] = frequency;
385                 for (; i < old_frequencies.length; i++)
386                         new_frequencies[i+1] = old_frequencies[i];
387                 set_common_frequencies(new_frequencies);
388         }
389
390         public static boolean imperial_units() {
391                 synchronized(preferences) {
392                         return AltosConvert.imperial_units;
393                 }
394         }
395
396         public static void set_imperial_units(boolean imperial_units) {
397                 synchronized (preferences) {
398                         AltosConvert.imperial_units = imperial_units;
399                         preferences.putBoolean(unitsPreference, imperial_units);
400                         flush_preferences();
401                 }
402         }
403 }