altos: Check for full log and complain
[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         /* Default logdir is ~/TeleMetrum */
47         final static String logdirName = "TeleMetrum";
48
49         /* UI Component to pop dialogs up */
50         static Component component;
51
52         /* Log directory */
53         static File logdir;
54
55         /* Map directory -- hangs of logdir */
56         static File mapdir;
57
58         /* Channel (map serial to channel) */
59         static Hashtable<Integer, Integer> channels;
60
61         /* Voice preference */
62         static boolean voice;
63
64         /* Callsign preference */
65         static String callsign;
66
67         /* Firmware directory */
68         static File firmwaredir;
69
70         public static void init(Component ui) {
71                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
72
73                 component = ui;
74
75                 /* Initialize logdir from preferences */
76                 String logdir_string = preferences.get(logdirPreference, null);
77                 if (logdir_string != null)
78                         logdir = new File(logdir_string);
79                 else {
80                         /* Use the file system view default directory */
81                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
82                         if (!logdir.exists())
83                                 logdir.mkdirs();
84                 }
85                 mapdir = new File(logdir, "maps");
86                 if (!mapdir.exists())
87                         mapdir.mkdirs();
88
89                 channels = new Hashtable<Integer,Integer>();
90
91                 voice = preferences.getBoolean(voicePreference, true);
92
93                 callsign = preferences.get(callsignPreference,"N0CALL");
94
95                 String firmwaredir_string = preferences.get(firmwaredirPreference, null);
96                 if (firmwaredir_string != null)
97                         firmwaredir = new File(firmwaredir_string);
98                 else
99                         firmwaredir = null;
100         }
101
102         static void flush_preferences() {
103                 try {
104                         preferences.flush();
105                 } catch (BackingStoreException ee) {
106                         JOptionPane.showMessageDialog(component,
107                                                       preferences.absolutePath(),
108                                                       "Cannot save prefernces",
109                                                       JOptionPane.ERROR_MESSAGE);
110                 }
111         }
112
113         public static void set_logdir(File new_logdir) {
114                 logdir = new_logdir;
115                 mapdir = new File(logdir, "maps");
116                 if (!mapdir.exists())
117                         mapdir.mkdirs();
118                 synchronized (preferences) {
119                         preferences.put(logdirPreference, logdir.getPath());
120                         flush_preferences();
121                 }
122         }
123
124         private static boolean check_dir(File dir) {
125                 if (!dir.exists()) {
126                         if (!dir.mkdirs()) {
127                                 JOptionPane.showMessageDialog(component,
128                                                               dir.getName(),
129                                                               "Cannot create directory",
130                                                               JOptionPane.ERROR_MESSAGE);
131                                 return false;
132                         }
133                 } else if (!dir.isDirectory()) {
134                         JOptionPane.showMessageDialog(component,
135                                                       dir.getName(),
136                                                       "Is not a directory",
137                                                       JOptionPane.ERROR_MESSAGE);
138                         return false;
139                 }
140                 return true;
141         }
142
143         /* Configure the log directory. This is where all telemetry and eeprom files
144          * will be written to, and where replay will look for telemetry files
145          */
146         public static void ConfigureLog() {
147                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
148
149                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
150                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
151
152                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
153                         File dir = logdir_chooser.getSelectedFile();
154                         if (check_dir(dir))
155                                 set_logdir(dir);
156                 }
157         }
158
159         public static File logdir() {
160                 return logdir;
161         }
162
163         public static File mapdir() {
164                 return mapdir;
165         }
166
167         public static void set_channel(int serial, int new_channel) {
168                 channels.put(serial, new_channel);
169                 synchronized (preferences) {
170                         preferences.putInt(String.format(channelPreferenceFormat, serial), new_channel);
171                         flush_preferences();
172                 }
173         }
174
175         public static int channel(int serial) {
176                 if (channels.containsKey(serial))
177                         return channels.get(serial);
178                 int channel = preferences.getInt(String.format(channelPreferenceFormat, serial), 0);
179                 channels.put(serial, channel);
180                 return channel;
181         }
182
183         public static void set_voice(boolean new_voice) {
184                 voice = new_voice;
185                 synchronized (preferences) {
186                         preferences.putBoolean(voicePreference, voice);
187                         flush_preferences();
188                 }
189         }
190
191         public static boolean voice() {
192                 return voice;
193         }
194
195         public static void set_callsign(String new_callsign) {
196                 callsign = new_callsign;
197                 synchronized(preferences) {
198                         preferences.put(callsignPreference, callsign);
199                         flush_preferences();
200                 }
201         }
202
203         public static String callsign() {
204                 return callsign;
205         }
206
207         public static void set_firmwaredir(File new_firmwaredir) {
208                 firmwaredir = new_firmwaredir;
209                 synchronized (preferences) {
210                         preferences.put(firmwaredirPreference, firmwaredir.getPath());
211                         flush_preferences();
212                 }
213         }
214
215         public static File firmwaredir() {
216                 return firmwaredir;
217         }
218 }