]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/gpsies/GetGpsiesFunction.java
cdcc9a9f4f1dfc613bcb752395fa2dae4853ba18
[GpsPrune.git] / src / tim / prune / function / gpsies / GetGpsiesFunction.java
1 package tim.prune.function.gpsies;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.net.URLConnection;
7 import java.util.ArrayList;
8
9 import javax.xml.parsers.SAXParser;
10 import javax.xml.parsers.SAXParserFactory;
11
12 import tim.prune.App;
13 import tim.prune.GpsPrune;
14 import tim.prune.I18nManager;
15 import tim.prune.function.search.GenericDownloaderFunction;
16 import tim.prune.function.search.SearchResult;
17 import tim.prune.load.xml.XmlFileLoader;
18 import tim.prune.load.xml.ZipFileLoader;
19
20 /**
21  * Function to load track information from Gpsies.com
22  * according to the currently viewed area
23  */
24 public class GetGpsiesFunction extends GenericDownloaderFunction
25 {
26         /** Number of results per page */
27         private static final int RESULTS_PER_PAGE = 20;
28         /** Maximum number of results to get */
29         private static final int MAX_RESULTS = 60;
30         /** New API key (specific to this program) */
31         private static final String GPSIES_API_KEY = "oumgvvbckiwpvsnb";
32
33
34         /**
35          * Constructor
36          * @param inApp App object
37          */
38         public GetGpsiesFunction(App inApp) {
39                 super(inApp);
40         }
41
42         /**
43          * @return name key
44          */
45         public String getNameKey() {
46                 return "function.getgpsies";
47         }
48
49         /**
50          * @param inColNum index of column, 0 or 1
51          * @return key for this column
52          */
53         protected String getColumnKey(int inColNum)
54         {
55                 if (inColNum == 0) return "dialog.gpsies.column.name";
56                 return "dialog.gpsies.column.length";
57         }
58
59
60         /**
61          * Run method to call gpsies.com in separate thread
62          */
63         public void run()
64         {
65                 _statusLabel.setText(I18nManager.getText("confirm.running"));
66                 // Act on callback to update list and send another request if necessary
67                 double[] coords = _app.getViewport().getBounds();
68                 int currPage = 1;
69
70                 ArrayList<SearchResult> trackList = null;
71                 URL url = null;
72                 String descMessage = "";
73                 InputStream inStream = null;
74                 // Loop for each page of the results
75                 do
76                 {
77                         // Example http://ws.gpsies.com/api.do?BBOX=10,51,12,53&limit=20&resultPage=1&key=oumgvvbckiwpvsnb
78                         String urlString = "http://ws.gpsies.com/api.do?BBOX=" +
79                                 coords[1] + "," + coords[0] + "," + coords[3] + "," + coords[2] +
80                                 "&limit=" + RESULTS_PER_PAGE + "&resultPage=" + currPage +
81                                 "&key=" + GPSIES_API_KEY;
82                         // Parse the returned XML with a special handler
83                         GpsiesXmlHandler xmlHandler = new GpsiesXmlHandler();
84                         try
85                         {
86                                 url = new URL(urlString);
87                                 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
88                                 URLConnection conn = url.openConnection();
89                                 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
90                                 inStream = conn.getInputStream();
91                                 saxParser.parse(inStream, xmlHandler);
92                         }
93                         catch (Exception e) {
94                                 descMessage = e.getClass().getName() + " - " + e.getMessage();
95                         }
96                         // Close stream and ignore errors
97                         try {
98                                 inStream.close();
99                         } catch (Exception e) {}
100                         // Add track list to model
101                         trackList = xmlHandler.getTrackList();
102                         _trackListModel.addTracks(trackList);
103
104                         // Compare number of results with results per page and call again if necessary
105                         currPage++;
106                 }
107                 while (trackList != null && trackList.size() == RESULTS_PER_PAGE
108                         && _trackListModel.getRowCount() < MAX_RESULTS && !_cancelled);
109                 // Set status label according to error or "none found", leave blank if ok
110                 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
111                         descMessage = I18nManager.getText("dialog.gpsies.nonefound");
112                 }
113                 _statusLabel.setText(descMessage);
114         }
115
116         /**
117          * Load the selected track(s)
118          */
119         protected void loadSelected()
120         {
121                 // Find the row(s) selected in the table and get the corresponding track
122                 int numSelected = _trackTable.getSelectedRowCount();
123                 if (numSelected < 1) return;
124                 int[] rowNums = _trackTable.getSelectedRows();
125                 for (int i=0; i<numSelected; i++)
126                 {
127                         int rowNum = rowNums[i];
128                         if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
129                         {
130                                 String url = _trackListModel.getTrack(rowNum).getDownloadLink();
131                                 XmlFileLoader xmlLoader = new XmlFileLoader(_app);
132                                 ZipFileLoader loader = new ZipFileLoader(_app, xmlLoader);
133                                 if (i>0) _app.autoAppendNextFile();
134                                 try
135                                 {
136                                         loader.openStream(new URL(url).openStream());
137                                 }
138                                 catch (IOException ioe) {
139                                         System.err.println("IO Exception : " + ioe.getMessage());
140                                 }
141                         }
142                 }
143                 // Close the dialog
144                 _cancelled = true;
145                 _dialog.dispose();
146         }
147 }