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