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