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