altosui: Match directories in hex file matcher
[fw/altos] / altosui / AltosBTKnown.java
1 /*
2  * Copyright © 2011 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.util.*;
20 import org.altusmetrum.altoslib_2.*;
21 import org.altusmetrum.altosuilib_1.*;
22
23 public class AltosBTKnown implements Iterable<AltosBTDevice> {
24         LinkedList<AltosBTDevice>       devices = new LinkedList<AltosBTDevice>();
25         AltosPreferencesBackend         bt_pref = AltosUIPreferences.bt_devices();
26
27         private String get_address(String name) {
28                 return bt_pref.getString(name, "");
29         }
30
31         private void set_address(String name, String addr) {
32                 bt_pref.putString(name, addr);
33         }
34
35         private void remove(String name) {
36                 bt_pref.remove(name);
37         }
38
39         private void load() {
40                 try {
41                         String[] names = bt_pref.keys();
42                         for (int i = 0; i < names.length; i++) {
43                                 String  name = names[i];
44                                 String  addr = get_address(name);
45                                 devices.add(new AltosBTDevice(name, addr));
46                         }
47                 } catch (IllegalStateException ie) {
48                 }
49         }
50
51         public Iterator<AltosBTDevice> iterator() {
52                 return devices.iterator();
53         }
54
55         private void flush() {
56                 AltosUIPreferences.flush_preferences();
57         }
58
59         public void set(Iterable<AltosBTDevice> new_devices) {
60                 for (AltosBTDevice old : devices) {
61                         boolean found = false;
62                         for (AltosBTDevice new_device : new_devices) {
63                                 if (new_device.equals(old)) {
64                                         found = true;
65                                         break;
66                                 }
67                         }
68                         if (!found)
69                                 remove(old.getName());
70                 }
71                 devices = new LinkedList<AltosBTDevice>();
72                 for (AltosBTDevice new_device : new_devices) {
73                         devices.add(new_device);
74                         set_address(new_device.getName(), new_device.getAddr());
75                 }
76                 flush();
77         }
78
79         public List<AltosDevice> list(int product) {
80                 LinkedList<AltosDevice> list = new LinkedList<AltosDevice>();
81                 for (AltosBTDevice device : devices) {
82                         if (device.matchProduct(product))
83                                 list.add(device);
84                 }
85                 return list;
86         }
87
88         public AltosBTKnown() {
89                 devices = new LinkedList<AltosBTDevice>();
90                 bt_pref = AltosUIPreferences.bt_devices();
91                 load();
92         }
93
94         static AltosBTKnown     known;
95
96         static public AltosBTKnown bt_known() {
97                 if (known == null)
98                         known = new AltosBTKnown();
99                 return known;
100         }
101 }