Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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_5;
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         /* telemetry rate format preference name */
39         public final static String telemetryRatePreferenceFormat = "RATE-%d";
40
41         /* log file format preference name */
42         public final static String logfilePreferenceFormat = "LOGFILE-%d";
43
44         /* voice preference name */
45         public final static String voicePreference = "VOICE";
46
47         /* callsign preference name */
48         public final static String callsignPreference = "CALLSIGN";
49
50         /* firmware directory preference name */
51         public final static String firmwaredirPreference = "FIRMWARE";
52
53         /* serial debug preference name */
54         public final static String serialDebugPreference = "SERIAL-DEBUG";
55
56         /* scanning telemetry preferences name */
57         public final static String scanningTelemetryPreference = "SCANNING-TELEMETRY";
58
59         /* scanning telemetry rate preferences name */
60         public final static String scanningTelemetryRatePreference = "SCANNING-RATE";
61
62         /* Launcher serial preference name */
63         public final static String launcherSerialPreference = "LAUNCHER-SERIAL";
64
65         /* Launcher channel preference name */
66         public final static String launcherChannelPreference = "LAUNCHER-CHANNEL";
67
68         /* Default logdir is ~/TeleMetrum */
69         public final static String logdirName = "TeleMetrum";
70
71         /* Log directory */
72         public static File logdir;
73
74         /* Last log directory - use this next time we open or save something */
75         public static File last_logdir;
76
77         /* Map directory -- hangs of logdir */
78         public static File mapdir;
79
80         /* Frequency (map serial to frequency) */
81         public static Hashtable<Integer, Double> frequencies;
82
83         /* Telemetry (map serial to telemetry format) */
84         public static Hashtable<Integer, Integer> telemetries;
85
86         /* Telemetry rate (map serial to telemetry format) */
87         public static Hashtable<Integer, Integer> telemetry_rates;
88
89         /* Log file (map serial to logfile name) */
90         public static Hashtable<Integer, File> logfiles;
91
92         /* Voice preference */
93         public static boolean voice;
94
95         /* Callsign preference */
96         public static String callsign;
97
98         /* Firmware directory */
99         public static File firmwaredir;
100
101         /* Scanning telemetry */
102         public static int scanning_telemetry;
103
104         public static int scanning_telemetry_rate;
105
106         /* List of frequencies */
107         public final static String common_frequencies_node_name = "COMMON-FREQUENCIES";
108         public static AltosFrequency[] common_frequencies;
109
110         public final static String      frequency_count = "COUNT";
111         public final static String      frequency_format = "FREQUENCY-%d";
112         public final static String      description_format = "DESCRIPTION-%d";
113
114         /* Units preference */
115
116         public final static String      unitsPreference = "IMPERIAL-UNITS";
117
118         public static AltosFrequency[] load_common_frequencies() {
119                 AltosFrequency[] frequencies = null;
120                 boolean existing = false;
121                 existing = backend.nodeExists(common_frequencies_node_name);
122
123                 if (existing) {
124                         AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
125                         int             count = node.getInt(frequency_count, 0);
126
127                         frequencies = new AltosFrequency[count];
128                         for (int i = 0; i < count; i++) {
129                                 double  frequency;
130                                 String  description;
131
132                                 frequency = node.getDouble(String.format(frequency_format, i), 0.0);
133                                 description = node.getString(String.format(description_format, i), null);
134                                 frequencies[i] = new AltosFrequency(frequency, description);
135                         }
136                 } else {
137                         frequencies = new AltosFrequency[10];
138                         for (int i = 0; i < 10; i++) {
139                                 frequencies[i] = new AltosFrequency(434.550 + i * .1,
140                                                                            String.format("Channel %d", i));
141                         }
142                 }
143                 return frequencies;
144         }
145
146         public static void save_common_frequencies(AltosFrequency[] frequencies) {
147                 AltosPreferencesBackend node = backend.node(common_frequencies_node_name);
148
149                 node.putInt(frequency_count, frequencies.length);
150                 for (int i = 0; i < frequencies.length; i++) {
151                         node.putDouble(String.format(frequency_format, i), frequencies[i].frequency);
152                         node.putString(String.format(description_format, i), frequencies[i].description);
153                 }
154         }
155         public static int launcher_serial;
156
157         public static int launcher_channel;
158
159         public static void init(AltosPreferencesBackend in_backend) {
160
161                 if (backend != null)
162                         return;
163
164                 backend = in_backend;
165
166                 /* Initialize logdir from preferences */
167                 String logdir_string = backend.getString(logdirPreference, null);
168                 if (logdir_string != null)
169                         logdir = new File(logdir_string);
170                 else {
171                         logdir = new File(backend.homeDirectory(), logdirName);
172                         if (!logdir.exists())
173                                 logdir.mkdirs();
174                 }
175                 mapdir = new File(logdir, "maps");
176                 if (!mapdir.exists())
177                         mapdir.mkdirs();
178
179                 frequencies = new Hashtable<Integer, Double>();
180
181                 telemetries = new Hashtable<Integer,Integer>();
182
183                 telemetry_rates = new Hashtable<Integer,Integer>();
184
185                 logfiles = new Hashtable<Integer,File>();
186
187                 voice = backend.getBoolean(voicePreference, true);
188
189                 callsign = backend.getString(callsignPreference,"N0CALL");
190
191                 scanning_telemetry = backend.getInt(scanningTelemetryPreference,(1 << AltosLib.ao_telemetry_standard));
192
193                 scanning_telemetry_rate = backend.getInt(scanningTelemetryRatePreference,(1 << AltosLib.ao_telemetry_rate_38400));
194
195                 launcher_serial = backend.getInt(launcherSerialPreference, 0);
196
197                 launcher_channel = backend.getInt(launcherChannelPreference, 0);
198
199                 String firmwaredir_string = backend.getString(firmwaredirPreference, null);
200                 if (firmwaredir_string != null)
201                         firmwaredir = new File(firmwaredir_string);
202                 else
203                         firmwaredir = null;
204
205                 common_frequencies = load_common_frequencies();
206
207                 AltosConvert.imperial_units = backend.getBoolean(unitsPreference, false);
208         }
209
210         public static void flush_preferences() {
211                 backend.flush();
212         }
213
214         public static void set_logdir(File new_logdir) {
215                 synchronized (backend) {
216                         logdir = new_logdir;
217                         mapdir = new File(logdir, "maps");
218                         if (!mapdir.exists())
219                                 mapdir.mkdirs();
220                         backend.putString(logdirPreference, logdir.getPath());
221                         flush_preferences();
222                 }
223         }
224
225         public static File logdir() {
226                 synchronized (backend) {
227                         return logdir;
228                 }
229         }
230
231         public static File last_logdir() {
232                 synchronized (backend) {
233                         if (last_logdir == null)
234                                 last_logdir = logdir;
235                         return last_logdir;
236                 }
237         }
238
239         public static void set_last_logdir(File file) {
240                 synchronized(backend) {
241                         if (file != null && !file.isDirectory())
242                                 file = file.getParentFile();
243                         if (file == null)
244                                 file = new File(".");
245                         last_logdir = file;
246                 }
247         }
248
249         public static File mapdir() {
250                 synchronized (backend) {
251                         return mapdir;
252                 }
253         }
254
255         public static void set_frequency(int serial, double new_frequency) {
256                 synchronized (backend) {
257                         frequencies.put(serial, new_frequency);
258                         backend.putDouble(String.format(frequencyPreferenceFormat, serial), new_frequency);
259                         flush_preferences();
260                 }
261         }
262
263         public static double frequency(int serial) {
264                 synchronized (backend) {
265                         if (frequencies.containsKey(serial))
266                                 return frequencies.get(serial);
267                         double frequency = backend.getDouble(String.format(frequencyPreferenceFormat, serial), 0);
268                         if (frequency == 0.0) {
269                                 int channel = backend.getInt(String.format(channelPreferenceFormat, serial), 0);
270                                 frequency = AltosConvert.radio_channel_to_frequency(channel);
271                         }
272                         frequencies.put(serial, frequency);
273                         return frequency;
274                 }
275         }
276
277         public static void set_telemetry(int serial, int new_telemetry) {
278                 synchronized (backend) {
279                         telemetries.put(serial, new_telemetry);
280                         backend.putInt(String.format(telemetryPreferenceFormat, serial), new_telemetry);
281                         flush_preferences();
282                 }
283         }
284
285         public static int telemetry(int serial) {
286                 synchronized (backend) {
287                         if (telemetries.containsKey(serial))
288                                 return telemetries.get(serial);
289                         int telemetry = backend.getInt(String.format(telemetryPreferenceFormat, serial),
290                                                    AltosLib.ao_telemetry_standard);
291                         telemetries.put(serial, telemetry);
292                         return telemetry;
293                 }
294         }
295
296         public static void set_telemetry_rate(int serial, int new_telemetry_rate) {
297                 synchronized (backend) {
298                         telemetry_rates.put(serial, new_telemetry_rate);
299                         backend.putInt(String.format(telemetryRatePreferenceFormat, serial), new_telemetry_rate);
300                         flush_preferences();
301                 }
302         }
303
304         public static int telemetry_rate(int serial) {
305                 synchronized (backend) {
306                         if (telemetry_rates.containsKey(serial))
307                                 return telemetry_rates.get(serial);
308                         int telemetry_rate = backend.getInt(String.format(telemetryRatePreferenceFormat, serial),
309                                                             AltosLib.ao_telemetry_rate_38400);
310                         telemetry_rates.put(serial, telemetry_rate);
311                         return telemetry_rate;
312                 }
313         }
314
315         public static void set_logfile(int serial, File new_logfile) {
316                 synchronized(backend) {
317                         logfiles.put(serial, new_logfile);
318                         backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath());
319                         flush_preferences();
320                 }
321         }
322
323         public static File logfile(int serial) {
324                 synchronized(backend) {
325                         if (logfiles.containsKey(serial))
326                                 return logfiles.get(serial);
327                         String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null);
328                         if (logfile_string == null)
329                                 return null;
330                         File logfile = new File(logfile_string);
331                         logfiles.put(serial, logfile);
332                         return logfile;
333                 }
334         }
335
336         public static void set_scanning_telemetry(int new_scanning_telemetry) {
337                 synchronized (backend) {
338                         scanning_telemetry = new_scanning_telemetry;
339                         backend.putInt(scanningTelemetryPreference, scanning_telemetry);
340                         flush_preferences();
341                 }
342         }
343
344         public static int scanning_telemetry() {
345                 synchronized (backend) {
346                         return scanning_telemetry;
347                 }
348         }
349
350         public static void set_scanning_telemetry_rate(int new_scanning_telemetry_rate) {
351                 synchronized (backend) {
352                         scanning_telemetry_rate = new_scanning_telemetry_rate;
353                         backend.putInt(scanningTelemetryRatePreference, scanning_telemetry_rate);
354                         flush_preferences();
355                 }
356         }
357
358         public static int scanning_telemetry_rate() {
359                 synchronized(backend) {
360                         return scanning_telemetry_rate;
361                 }
362         }
363
364         public static void set_voice(boolean new_voice) {
365                 synchronized (backend) {
366                         voice = new_voice;
367                         backend.putBoolean(voicePreference, voice);
368                         flush_preferences();
369                 }
370         }
371
372         public static boolean voice() {
373                 synchronized (backend) {
374                         return voice;
375                 }
376         }
377
378         public static void set_callsign(String new_callsign) {
379                 synchronized(backend) {
380                         callsign = new_callsign;
381                         backend.putString(callsignPreference, callsign);
382                         flush_preferences();
383                 }
384         }
385
386         public static String callsign() {
387                 synchronized(backend) {
388                         return callsign;
389                 }
390         }
391
392         public static void set_firmwaredir(File new_firmwaredir) {
393                 synchronized (backend) {
394                         firmwaredir = new_firmwaredir;
395                         backend.putString(firmwaredirPreference, firmwaredir.getPath());
396                         flush_preferences();
397                 }
398         }
399
400         public static File firmwaredir() {
401                 synchronized (backend) {
402                         return firmwaredir;
403                 }
404         }
405
406         public static void set_launcher_serial(int new_launcher_serial) {
407                 synchronized (backend) {
408                         launcher_serial = new_launcher_serial;
409                         backend.putInt(launcherSerialPreference, launcher_serial);
410                         flush_preferences();
411                 }
412         }
413
414         public static int launcher_serial() {
415                 synchronized (backend) {
416                         return launcher_serial;
417                 }
418         }
419
420         public static void set_launcher_channel(int new_launcher_channel) {
421                 synchronized (backend) {
422                         launcher_channel = new_launcher_channel;
423                         backend.putInt(launcherChannelPreference, launcher_channel);
424                         flush_preferences();
425                 }
426         }
427
428         public static int launcher_channel() {
429                 synchronized (backend) {
430                         return launcher_channel;
431                 }
432         }
433
434         public static AltosPreferencesBackend bt_devices() {
435                 synchronized (backend) {
436                         return backend.node("bt_devices");
437                 }
438         }
439
440         public static AltosFrequency[] common_frequencies() {
441                 synchronized (backend) {
442                         return common_frequencies;
443                 }
444         }
445
446         public static void set_common_frequencies(AltosFrequency[] frequencies) {
447                 synchronized(backend) {
448                         common_frequencies = frequencies;
449                         save_common_frequencies(frequencies);
450                         flush_preferences();
451                 }
452         }
453
454         public static void add_common_frequency(AltosFrequency frequency) {
455                 AltosFrequency[]        old_frequencies = common_frequencies();
456                 AltosFrequency[]        new_frequencies = new AltosFrequency[old_frequencies.length + 1];
457                 int                     i;
458
459                 for (i = 0; i < old_frequencies.length; i++) {
460                         if (frequency.frequency == old_frequencies[i].frequency)
461                                 return;
462                         if (frequency.frequency < old_frequencies[i].frequency)
463                                 break;
464                         new_frequencies[i] = old_frequencies[i];
465                 }
466                 new_frequencies[i] = frequency;
467                 for (; i < old_frequencies.length; i++)
468                         new_frequencies[i+1] = old_frequencies[i];
469                 set_common_frequencies(new_frequencies);
470         }
471
472         static LinkedList<AltosUnitsListener> units_listeners;
473
474         public static boolean imperial_units() {
475                 synchronized(backend) {
476                         return AltosConvert.imperial_units;
477                 }
478         }
479
480         public static void set_imperial_units(boolean imperial_units) {
481                 synchronized (backend) {
482                         AltosConvert.imperial_units = imperial_units;
483                         backend.putBoolean(unitsPreference, imperial_units);
484                         flush_preferences();
485                 }
486                 if (units_listeners != null) {
487                         for (AltosUnitsListener l : units_listeners) {
488                                 l.units_changed(imperial_units);
489                         }
490                 }
491         }
492
493         public static void register_units_listener(AltosUnitsListener l) {
494                 synchronized(backend) {
495                         if (units_listeners == null)
496                                 units_listeners = new LinkedList<AltosUnitsListener>();
497                         units_listeners.add(l);
498                 }
499         }
500
501         public static void unregister_units_listener(AltosUnitsListener l) {
502                 synchronized(backend) {
503                         units_listeners.remove(l);
504                 }
505         }
506 }