File: UploadGpsiesFunction.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (331 lines) | stat: -rw-r--r-- 11,129 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package tim.prune.function.gpsies;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import tim.prune.App;
import tim.prune.GenericFunction;
import tim.prune.I18nManager;
import tim.prune.function.browser.BrowserLauncher;
import tim.prune.gui.GuiGridLayout;
import tim.prune.save.GpxExporter;

/**
 * Function to upload track information up to Gpsies.com
 */
public class UploadGpsiesFunction extends GenericFunction
{
	/** Dialog object */
	private JDialog _dialog = null;
	/** Edit box for user name */
	private JTextField _usernameField = null;
	/** Edit box for password */
	private JPasswordField _passwordField = null;
	/** Name of track */
	private JTextField _nameField = null;
	/** Description */
	private JTextArea _descField = null;
	/** Private checkbox */
	private JCheckBox _privateCheckbox = null;
	/** Activity checkboxes */
	private JCheckBox[] _activityCheckboxes = null;
	/** Writer object for GPX export */
	private OutputStreamWriter _writer = null;
	/** upload button */
	private JButton _uploadButton = null;

	/** URL to post form to */
	private static final String GPSIES_URL = "http://www.gpsies.com/upload.do";
	/** Keys for describing activities */
	private static final String[] ACTIVITY_KEYS = {"trekking", "walking", "jogging",
		"biking", "motorbiking", "snowshoe", "sailing", "skating"};

	/**
	 * Constructor
	 * @param inApp App object
	 */
	public UploadGpsiesFunction(App inApp) {
		super(inApp);
	}

	/**
	 * @return name key
	 */
	public String getNameKey() {
		return "function.uploadgpsies";
	}

	/**
	 * Begin the function
	 */
	public void begin()
	{
		// Initialise dialog, show empty list
		if (_dialog == null)
		{
			_dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
			_dialog.setLocationRelativeTo(_parentFrame);
			_dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			_dialog.getContentPane().add(makeDialogComponents());
			_dialog.pack();
		}
		// Show dialog
		_dialog.setVisible(true);
	}


	/**
	 * Create dialog components
	 * @return Panel containing all gui elements in dialog
	 */
	private Component makeDialogComponents()
	{
		JPanel dialogPanel = new JPanel();
		dialogPanel.setLayout(new BorderLayout());

		JPanel gridPanel = new JPanel();
		GuiGridLayout grid = new GuiGridLayout(gridPanel);
		grid.add(new JLabel(I18nManager.getText("dialog.gpsies.username")));
		_usernameField = new JTextField(15);
		grid.add(_usernameField);
		grid.add(new JLabel(I18nManager.getText("dialog.gpsies.password")));
		_passwordField = new JPasswordField(15);
		grid.add(_passwordField);
		// Track name and description
		grid.add(new JLabel(I18nManager.getText("dialog.gpsies.column.name")));
		_nameField = new JTextField(15);
		grid.add(_nameField);
		grid.add(new JLabel(I18nManager.getText("dialog.gpsies.description")));
		_descField = new JTextArea(5, 15);
		_descField.setLineWrap(true);
		_descField.setWrapStyleWord(true);
		grid.add(new JScrollPane(_descField));
		// Listener on all these text fields to enable/disable the ok button
		KeyAdapter keyListener = new KeyAdapter() {
			/** Key released */
			public void keyReleased(KeyEvent inE) {
				enableOK();
				if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
					_dialog.dispose();
				}
			}
		};
		_usernameField.addKeyListener(keyListener);
		_passwordField.addKeyListener(keyListener);
		_nameField.addKeyListener(keyListener);
		// Listen for tabs on description field, to change focus not enter tabs
		_descField.addKeyListener(new KeyAdapter() {
			/** Key pressed */
			public void keyPressed(KeyEvent inE) {
				if (inE.getKeyCode() == KeyEvent.VK_TAB) {
					inE.consume();
					if (inE.isShiftDown()) {
						_nameField.requestFocusInWindow();
					}
					else {
						_privateCheckbox.requestFocusInWindow();
					}
				}
			}
		});
		// Listen for Ctrl-backspace on password field (delete contents)
		_passwordField.addKeyListener(new KeyAdapter() {
			/** Key released */
			public void keyReleased(KeyEvent inE) {
				if (inE.isControlDown() && (inE.getKeyCode() == KeyEvent.VK_BACK_SPACE
					|| inE.getKeyCode() == KeyEvent.VK_DELETE)) {
					_passwordField.setText("");
				}
			}
		});
		// Checkbox for private / public
		grid.add(new JLabel(I18nManager.getText("dialog.gpsies.keepprivate")));
		_privateCheckbox = new JCheckBox();
		_privateCheckbox.setSelected(true);
		grid.add(_privateCheckbox);

		// panel for activity type checkboxes
		JPanel activityPanel = new JPanel();
		activityPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		ChangeListener checkListener = new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				enableOK();
			}
		};
		// Why not a simple grid layout here?
		GuiGridLayout actGrid = new GuiGridLayout(activityPanel, new double[] {1.0, 1.0}, new boolean[] {false, false});
		final int numActivities = ACTIVITY_KEYS.length;
		_activityCheckboxes = new JCheckBox[numActivities];
		for (int i=0; i<numActivities; i++)
		{
			_activityCheckboxes[i] = new JCheckBox(I18nManager.getText("dialog.gpsies.activity." + ACTIVITY_KEYS[i]));
			_activityCheckboxes[i].addChangeListener(checkListener);
			actGrid.add(_activityCheckboxes[i]);
		}
		grid.add(new JLabel(I18nManager.getText("dialog.gpsies.activities")));
		grid.add(activityPanel);
		JPanel midPanel = new JPanel();
		midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
		midPanel.add(gridPanel);
		dialogPanel.add(midPanel, BorderLayout.CENTER);

		// button panel at bottom
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		_uploadButton = new JButton(I18nManager.getText("button.upload"));
		_uploadButton.setEnabled(false);
		_uploadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				startUpload();
			}
		});
		buttonPanel.add(_uploadButton);
		JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				_dialog.dispose();
			}
		});
		buttonPanel.add(cancelButton);
		dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
		dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
		return dialogPanel;
	}


	/**
	 * Check the inputs and enable or disable the upload button
	 */
	private void enableOK()
	{
		// Check for lengths of input fields - only username, password and filename are required
		boolean ok = (_usernameField.getText().length() > 0 && _nameField.getText().length() > 0);
		if (ok) {
			// also check password field
			char[] pass = _passwordField.getPassword();
			ok = pass.length > 0;
			for (int i=0; i<pass.length; i++) {pass[i] = '0';} // recommended by javadoc
			if (ok) {
				ok = false;
				for (int i=0; i<_activityCheckboxes.length; i++) {
					ok = ok || _activityCheckboxes[i].isSelected();
				}
			}
		}
		_uploadButton.setEnabled(ok);
	}


	/**
	 * Start the upload process (require separate thread?)
	 */
	private void startUpload()
	{
		BufferedReader reader = null;
		try
		{
			FormPoster poster = new FormPoster(new URL(GPSIES_URL));
			poster.setParameter("device", "Prune");
			poster.setParameter("username", _usernameField.getText());
			poster.setParameter("password", new String(_passwordField.getPassword()));
			boolean hasActivity = false;
			for (int i=0; i<ACTIVITY_KEYS.length; i++)
			{
				if (_activityCheckboxes[i].isSelected()) {
					hasActivity = true;
					poster.setParameter("trackTypes", ACTIVITY_KEYS[i]);
				}
			}
			if (!hasActivity) {poster.setParameter("trackTypes", "walking");} // default if none given
			poster.setParameter("filename", _nameField.getText());
			poster.setParameter("fileDescription", _descField.getText());
			poster.setParameter("startpointCountry", "DE");
			poster.setParameter("endpointCountry", "DE"); // both those will be corrected by gpsies
			poster.setParameter("status", (_privateCheckbox.isSelected()?"3":"1"));
			poster.setParameter("submit", "speichern"); // required
			// Use Pipes to connect the GpxExporter's output with the FormPoster's input
			PipedInputStream iStream = new PipedInputStream();
			PipedOutputStream oStream = new PipedOutputStream(iStream);
			_writer = new OutputStreamWriter(oStream);
			new Thread(new Runnable() {
				public void run() {
					boolean[] saveFlags = {true, true, true, true, false, true}; // export everything
					try {
						GpxExporter.exportData(_writer, _app.getTrackInfo(), _nameField.getText(), null, saveFlags, null);
					} catch (IOException e) {}
					finally {
						try {_writer.close();} catch (IOException e) {}
					}
				}
			}).start();
			poster.setParameter("formFile", "filename.gpx", iStream);

			BufferedInputStream answer = new BufferedInputStream(poster.post());
			int response = poster.getResponseCode();
			reader = new BufferedReader(new InputStreamReader(answer));
			String line = reader.readLine();
			// Try to extract gpsies page url from the returned message
			String pageUrl = null;
			if (response == 200 && line.substring(0, 2).toUpperCase().equals("OK"))
			{
				final int bracketPos = line.indexOf('[');
				if (bracketPos > 0 && line.endsWith("]")) {
					pageUrl = line.substring(bracketPos + 1, line.length()-1);
				}
			}
			if (pageUrl != null)
			{
				// OK received and managed to extract a Url from the return message.
				int userChoice = JOptionPane.showConfirmDialog(_app.getFrame(),
					I18nManager.getText("dialog.gpsies.confirmopenpage"),
					I18nManager.getText(getNameKey()), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (userChoice == JOptionPane.OK_OPTION) {
					BrowserLauncher.launchBrowser(pageUrl);
				}
			}
			else {
				_app.showErrorMessageNoLookup(getNameKey(), I18nManager.getText("error.gpsies.uploadnotok")
					+ ": " + line);
			}
		}
		catch (MalformedURLException e) {}
		catch (IOException ioe) {
			_app.showErrorMessageNoLookup(getNameKey(), I18nManager.getText("error.gpsies.uploadfailed") + ": "
				+ ioe.getClass().getName() + " : " + ioe.getMessage());
		}
		finally {
			try {if (reader != null) reader.close();} catch (IOException e) {}
		}
		_dialog.dispose();
	}
}