Merge remote-tracking branch 'origin/master'
[fw/altos] / altosui / AltosSiteMapCache.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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
20 import javax.swing.*;
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
23 import java.io.*;
24 import java.net.URL;
25 import java.net.URLConnection;
26
27 public class AltosSiteMapCache extends JLabel {
28         static final long google_maps_ratelimit_ms = 1200;
29         // Google limits static map queries to 50 per minute per IP, so
30         // each query should take at least 1.2 seconds.
31
32         public static boolean fetchMap(File file, String url) {
33                 URL u;
34                 long startTime = System.nanoTime();
35
36                 try {
37                         u = new URL(url);
38                 } catch (java.net.MalformedURLException e) {
39                         return false;
40                 }
41
42                 byte[] data;
43                 try {
44                         URLConnection uc = u.openConnection();
45                         int contentLength = uc.getContentLength();
46                         InputStream in = new BufferedInputStream(uc.getInputStream());
47                         int bytesRead = 0;
48                         int offset = 0;
49                         data = new byte[contentLength];
50                         while (offset < contentLength) {
51                                 bytesRead = in.read(data, offset, data.length - offset);
52                                 if (bytesRead == -1)
53                                         break;
54                                 offset += bytesRead;
55                         }
56                         in.close();
57
58                         if (offset != contentLength) {
59                                 return false;
60                         }
61                 } catch (IOException e) {
62                         return false;
63                 }
64
65                 try {
66                         FileOutputStream out = new FileOutputStream(file);
67                         out.write(data);
68                         out.flush();
69                         out.close();
70                 } catch (FileNotFoundException e) {
71                         return false;
72                 } catch (IOException e) {
73                         if (file.exists()) {
74                                 file.delete();
75                         }
76                         return false;
77                 }
78
79                 long duration_ms = (System.nanoTime() - startTime) / 1000000;
80                 if (duration_ms < google_maps_ratelimit_ms) {
81                         try {
82                                 Thread.sleep(google_maps_ratelimit_ms - duration_ms);
83                         } catch (InterruptedException e) {
84                                 Thread.currentThread().interrupt();
85                         }
86                 }
87
88                 return true;
89         }
90
91         public static ImageIcon fetchAndLoadMap(File pngfile, String url) {
92                 if (!pngfile.exists()) {
93                         if (!fetchMap(pngfile, url)) {
94                                 return null;
95                         }
96                 }
97                 return loadMap(pngfile, url);
98         }
99
100         public static ImageIcon loadMap(File pngfile, String url) {
101                 if (!pngfile.exists()) {
102                         return null;
103                 }
104
105                 try {
106                         BufferedImage   img;
107
108                         img = ImageIO.read(pngfile);
109                         if (img == null) {
110                                 System.out.printf("# Can't read pngfile %s\n", pngfile);
111                                 return null;
112                         }
113                         return new ImageIcon(img);
114                 } catch (IOException e) {
115                         System.out.printf("# IO error trying to load %s\n", pngfile);
116                         return null;
117                 }
118         }
119 }