]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/cache/TileCacheModel.java
Version 13, August 2011
[GpsPrune.git] / tim / prune / function / cache / TileCacheModel.java
1 package tim.prune.function.cache;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6 import tim.prune.gui.map.MapSource;
7 import tim.prune.gui.map.MapSourceLibrary;
8
9 /**
10  * Class to obtain and hold information about the current
11  * tile cache including its subdirectories
12  */
13 public class TileCacheModel
14 {
15         /** Cache directory */
16         private File _cacheDir = null;
17         /** Array of tilesets */
18         private ArrayList<TileSet> _tileSets = null;
19         /** Summary information */
20         private RowInfo _summaryRow = null;
21         /** Cancelled flag */
22         private boolean _cancelled = false;
23
24
25         /**
26          * Constructor
27          * @param inDir start directory
28          */
29         public TileCacheModel(File inDir)
30         {
31                 if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead()) {
32                         _cacheDir = inDir;
33                 }
34                 _cancelled = false;
35         }
36
37         /**
38          * Build the tilesets by searching recursively
39          */
40         public void buildTileSets()
41         {
42                 if (_cacheDir == null) return;
43
44                 _tileSets = new ArrayList<TileSet>();
45                 // go through subdirectories, if any
46                 File[] subdirs = _cacheDir.listFiles();
47                 for (File subdir : subdirs)
48                 {
49                         if (subdir != null && subdir.isDirectory() && subdir.exists() && subdir.canRead()
50                                 && !_cancelled)
51                         {
52                                 getTileSets(subdir, null, _tileSets);
53                         }
54                 }
55                 // Loop over found tile sets and create summary rowinfo
56                 _summaryRow = new RowInfo();
57                 for (TileSet ts : _tileSets)
58                 {
59                         _summaryRow.addRow(ts.getRowInfo());
60                 }
61         }
62
63         /**
64          * Get all the tilesets from the given directory
65          * @param inDir directory to search
66          * @return array of TileSet objects
67          */
68         private static void getTileSets(File inDir, String inParentPath, ArrayList<TileSet> inTsList)
69         {
70                 final String wholePath = (inParentPath == null ? "" : inParentPath)
71                         + inDir.getName() + File.separator;
72                 // See if any configured backgrounds use this directory
73                 // or if the directories match OSM structure
74                 String usedByDesc = matchConfig(wholePath);
75                 boolean tsFound = false;
76                 if (usedByDesc != null || looksLikeCacheDir(inDir))
77                 {
78                         TileSet ts = new TileSet(inDir, wholePath, usedByDesc);
79                         if (usedByDesc != null || ts.getRowInfo().getNumTiles() > 0)
80                         {
81                                 tsFound = true;
82                                 inTsList.add(ts);
83                         }
84                 }
85                 // If a tileset wasn't found, look through subdirectories
86                 if (!tsFound)
87                 {
88                         // Go through subdirectories and look at each of them too
89                         File[] subdirs = inDir.listFiles();
90                         if (subdirs != null) {
91                                 for (File subdir : subdirs)
92                                 {
93                                         if (subdir != null && subdir.exists() && subdir.isDirectory()
94                                                 && subdir.canRead())
95                                         {
96                                                 getTileSets(subdir, wholePath, inTsList);
97                                         }
98                                 }
99                         }
100                 }
101         }
102
103         /**
104          * Match the given directory name to find the configs which use it
105          * @param inName name of directory to match
106          * @return null if not used, otherwise comma-separated list of background names
107          */
108         private static String matchConfig(String inName)
109         {
110                 if (inName == null || inName.equals(""))
111                         return null;
112                 String usedBy = null;
113                 for (int i=0; i<MapSourceLibrary.getNumSources(); i++)
114                 {
115                         MapSource ms = MapSourceLibrary.getSource(i);
116                         for (int l=0; l<2; l++)
117                         {
118                                 String msdir = ms.getSiteName(l);
119                                 if (msdir != null && msdir.equals(inName))
120                                 {
121                                         if (usedBy == null)
122                                                 usedBy = ms.getName();
123                                         else
124                                                 usedBy = usedBy + ", " + ms.getName();
125                                 }
126                         }
127                 }
128                 return usedBy;
129         }
130
131         /**
132          * @param inDir directory to test
133          * @return true if the subdirectories meet the normal osm layout
134          */
135         private static boolean looksLikeCacheDir(File inDir)
136         {
137                 // look for at least one numeric directory, nothing else
138                 boolean numFound = false;
139                 if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead())
140                 {
141                         for (File subdir : inDir.listFiles())
142                         {
143                                 // Only consider readable things which exist
144                                 if (subdir != null && subdir.exists() && subdir.canRead())
145                                 {
146                                         // subdirectories should have numeric names (for the zoom levels)
147                                         if (subdir.isDirectory() && TileSet.isNumeric(subdir.getName())
148                                                 && subdir.getName().length() < 3)
149                                         {
150                                                 numFound = true;
151                                         }
152                                         else return false; // either a file or non-numeric directory
153                                 }
154                         }
155                 }
156                 return numFound;
157         }
158
159         /**
160          * @return cache directory
161          */
162         public File getCacheDir() {
163                 return _cacheDir;
164         }
165
166         /**
167          * @return number of tile sets
168          */
169         public int getNumTileSets()
170         {
171                 if (_tileSets == null) return 0;
172                 return _tileSets.size();
173         }
174
175         /**
176          * @return the total number of tile images found
177          */
178         public int getTotalTiles()
179         {
180                 return _summaryRow.getNumTiles();
181         }
182
183         /**
184          * @return the total number of bytes taken up with tile images
185          */
186         public long getTotalBytes()
187         {
188                 return _summaryRow.getTotalSize();
189         }
190
191         /**
192          * @param inIndex index of tileset
193          * @return requested tileset
194          */
195         public TileSet getTileSet(int inIndex)
196         {
197                 if (inIndex >= 0 && inIndex < getNumTileSets()) {
198                         return _tileSets.get(inIndex);
199                 }
200                 return null;
201         }
202
203         /**
204          * Cancel the search
205          */
206         public void cancel() {
207                 _cancelled = true;
208         }
209
210         /**
211          * @return true if search was cancelled
212          */
213         public boolean isAborted() {
214                 return _cancelled;
215         }
216 }