altosui: Add ability to create CSV file from telem or eeprom files
[fw/altos] / ao-tools / 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 channelPreference = "CHANNEL";
36
37         /* voice preference name */
38         final static String voicePreference = "VOICE";
39
40         /* callsign preference name */
41         final static String callsignPreference = "CALLSIGN";
42
43         /* Default logdir is ~/TeleMetrum */
44         final static String logdirName = "TeleMetrum";
45
46         /* UI Component to pop dialogs up */
47         static Component component;
48
49         /* Log directory */
50         static File logdir;
51
52         /* Telemetry channel */
53         static int channel;
54
55         /* Voice preference */
56         static boolean voice;
57
58         static String callsign;
59
60         public static void init(Component ui) {
61                 preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
62
63                 component = ui;
64
65                 /* Initialize logdir from preferences */
66                 String logdir_string = preferences.get(logdirPreference, null);
67                 if (logdir_string != null)
68                         logdir = new File(logdir_string);
69                 else {
70                         /* Use the file system view default directory */
71                         logdir = new File(FileSystemView.getFileSystemView().getDefaultDirectory(), logdirName);
72                         if (!logdir.exists())
73                                 logdir.mkdirs();
74                 }
75
76                 channel = preferences.getInt(channelPreference, 0);
77
78                 voice = preferences.getBoolean(voicePreference, true);
79
80                 callsign = preferences.get(callsignPreference,"N0CALL");
81         }
82
83         static void flush_preferences() {
84                 try {
85                         preferences.flush();
86                 } catch (BackingStoreException ee) {
87                         JOptionPane.showMessageDialog(component,
88                                                       preferences.absolutePath(),
89                                                       "Cannot save prefernces",
90                                                       JOptionPane.ERROR_MESSAGE);
91                 }
92         }
93
94         public static void set_logdir(File new_logdir) {
95                 logdir = new_logdir;
96                 synchronized (preferences) {
97                         preferences.put(logdirPreference, logdir.getPath());
98                         flush_preferences();
99                 }
100         }
101
102         private static boolean check_dir(File dir) {
103                 if (!dir.exists()) {
104                         if (!dir.mkdirs()) {
105                                 JOptionPane.showMessageDialog(component,
106                                                               dir.getName(),
107                                                               "Cannot create directory",
108                                                               JOptionPane.ERROR_MESSAGE);
109                                 return false;
110                         }
111                 } else if (!dir.isDirectory()) {
112                         JOptionPane.showMessageDialog(component,
113                                                       dir.getName(),
114                                                       "Is not a directory",
115                                                       JOptionPane.ERROR_MESSAGE);
116                         return false;
117                 }
118                 return true;
119         }
120
121         /* Configure the log directory. This is where all telemetry and eeprom files
122          * will be written to, and where replay will look for telemetry files
123          */
124         public static void ConfigureLog() {
125                 JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
126
127                 logdir_chooser.setDialogTitle("Configure Data Logging Directory");
128                 logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
129
130                 if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
131                         File dir = logdir_chooser.getSelectedFile();
132                         if (check_dir(dir))
133                                 set_logdir(dir);
134                 }
135         }
136
137         public static File logdir() {
138                 return logdir;
139         }
140
141         public static void set_channel(int new_channel) {
142                 channel = new_channel;
143                 synchronized (preferences) {
144                         preferences.putInt(channelPreference, channel);
145                         flush_preferences();
146                 }
147         }
148
149         public static int channel() {
150                 return channel;
151         }
152
153         public static void set_voice(boolean new_voice) {
154                 voice = new_voice;
155                 synchronized (preferences) {
156                         preferences.putBoolean(voicePreference, voice);
157                         flush_preferences();
158                 }
159         }
160
161         public static boolean voice() {
162                 return voice;
163         }
164
165         public static void set_callsign(String new_callsign) {
166                 callsign = new_callsign;
167                 synchronized(preferences) {
168                         preferences.put(callsignPreference, callsign);
169                         flush_preferences();
170                 }
171         }
172
173         public static String callsign() {
174                 return callsign;
175         }
176 }