comparison src/viewer_swing/java/com/glavsoft/viewer/ConnectionPresenter.java @ 52:472a9bcacb21 draft default tip

TightVNC 2.7.1.0
author you@cr.ie.u-ryukyu.ac.jp
date Wed, 07 Aug 2013 19:01:17 +0900
parents
children
comparison
equal deleted inserted replaced
0:4689cc86d6cb 52:472a9bcacb21
1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
2 // All rights reserved.
3 //
4 //-------------------------------------------------------------------------
5 // This file is part of the TightVNC software. Please visit our Web site:
6 //
7 // http://www.tightvnc.com/
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License along
20 // with this program; if not, write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 //-------------------------------------------------------------------------
23 //
24
25 package com.glavsoft.viewer;
26
27 import com.glavsoft.rfb.protocol.ProtocolSettings;
28 import com.glavsoft.utils.Strings;
29 import com.glavsoft.viewer.mvp.Presenter;
30 import com.glavsoft.viewer.swing.ConnectionParams;
31 import com.glavsoft.viewer.swing.WrongParameterException;
32 import com.glavsoft.viewer.swing.gui.ConnectionView;
33 import com.glavsoft.viewer.swing.gui.ConnectionsHistory;
34
35 import java.net.Socket;
36 import java.util.logging.Logger;
37
38 /**
39 * @author dime at tightvnc.com
40 */
41 public class ConnectionPresenter extends Presenter {
42 public static final String PROPERTY_HOST_NAME = "HostName";
43 public static final String PROPERTY_RFB_PORT_NUMBER = "PortNumber";
44 public static final String PROPERTY_USE_SSH = "UseSsh";
45 private static final String PROPERTY_SSH_USER_NAME = "SshUserName";
46 private static final String PROPERTY_SSH_HOST_NAME = "SshHostName";
47 private static final String PROPERTY_SSH_PORT_NUMBER = "SshPortNumber";
48 private static final String PROPERTY_STATUS_BAR_MESSAGE = "Message";
49 private static final String PROPERTY_CONNECTION_IN_PROGRESS = "ConnectionInProgress";
50 public static final String CONNECTION_PARAMS_MODEL = "ConnectionParamsModel";
51 public static final String CONNECTIONS_HISTORY_MODEL = "ConnectionsHistoryModel";
52 public static final String CONNECTION_VIEW = "ConnectionView";
53
54 private final boolean hasSshSupport;
55 private final boolean allowInteractive;
56 private ConnectionsHistory connectionsHistory;
57 private ProtocolSettings rfbSettings;
58 private UiSettings uiSettings;
59 private final Logger logger;
60 private RfbConnectionWorker rfbConnectionWorker;
61 private AbstractConnectionWorkerFactory connectionWorkerFactory;
62 private NetworkConnectionWorker networkConnectionWorker;
63 private boolean needReconnection = true;
64
65 public ConnectionPresenter(boolean hasSshSupport, boolean allowInteractive) {
66 this.hasSshSupport = hasSshSupport;
67 this.allowInteractive = allowInteractive;
68 logger = Logger.getLogger(getClass().getName());
69 }
70
71 public void startConnection(ProtocolSettings rfbSettings, UiSettings uiSettings, int paramSettingsMask)
72 throws IllegalStateException {
73 this.rfbSettings = rfbSettings;
74 this.uiSettings = uiSettings;
75 if ( ! isModelRegisteredByName(CONNECTION_PARAMS_MODEL)) {
76 throw new IllegalStateException("No Connection Params model added.");
77 }
78 connectionsHistory = new ConnectionsHistory();
79 addModel(CONNECTIONS_HISTORY_MODEL, connectionsHistory);
80 syncModels(paramSettingsMask);
81 if (allowInteractive) {
82 show();
83 populate();
84 } else {
85 connect();
86 }
87 }
88
89 public void setUseSsh(boolean useSsh) {
90 setModelProperty(PROPERTY_USE_SSH, useSsh, boolean.class);
91 }
92
93 public void submitConnection(String hostName) throws WrongParameterException {
94 if (Strings.isTrimmedEmpty(hostName)) {
95 throw new WrongParameterException("Host name is empty", PROPERTY_HOST_NAME);
96 }
97 setModelProperty(PROPERTY_HOST_NAME, hostName);
98
99 final String rfbPort = (String) getViewPropertyOrNull(PROPERTY_RFB_PORT_NUMBER);
100 setModelProperty(PROPERTY_RFB_PORT_NUMBER, rfbPort);
101 try {
102 throwPossiblyHappenedException();
103 } catch (Throwable e) {
104 throw new WrongParameterException("Wrong Port", PROPERTY_HOST_NAME);
105 }
106 setSshOptions();
107
108 saveHistory();
109 populateFrom(CONNECTIONS_HISTORY_MODEL);
110
111 connect();
112 }
113
114 public void saveHistory() {
115 final ConnectionParams cp = (ConnectionParams) getModel(CONNECTION_PARAMS_MODEL);
116 connectionsHistory.reorder(cp, rfbSettings, uiSettings);
117 connectionsHistory.save();
118 }
119
120 private void connect() {
121 final ConnectionParams connectionParams = (ConnectionParams) getModel(CONNECTION_PARAMS_MODEL);
122 // TODO check connectionWorkerFactory is init
123 networkConnectionWorker = connectionWorkerFactory.createNetworkConnectionWorker();
124 networkConnectionWorker.setConnectionParams(connectionParams);
125 networkConnectionWorker.setPresenter(this);
126 networkConnectionWorker.setHasSshSupport(hasSshSupport);
127 networkConnectionWorker.execute();
128 }
129
130 public void connectionFailed() {
131 cancelConnection();
132 if (allowInteractive) {
133 enableConnectionDialog();
134 } else {
135 connect();
136 }
137 }
138
139 public void connectionCancelled() {
140 cancelConnection();
141 if (allowInteractive) {
142 enableConnectionDialog();
143 } else {
144 final ConnectionView connectionView = (ConnectionView) getView(CONNECTION_VIEW);
145 if (connectionView != null) {
146 connectionView.closeApp();
147 }
148 }
149 }
150
151 private void enableConnectionDialog() {
152 setViewProperty(PROPERTY_CONNECTION_IN_PROGRESS, false, boolean.class);
153 }
154
155 public void successfulNetworkConnection(Socket workingSocket) { // EDT
156 logger.info("Connected");
157 showMessage("Connected");
158 rfbConnectionWorker = connectionWorkerFactory.createRfbConnectionWorker();
159 rfbConnectionWorker.setWorkingSocket(workingSocket);
160 rfbConnectionWorker.setRfbSettings(rfbSettings);
161 rfbConnectionWorker.setUiSettings(uiSettings);
162 rfbConnectionWorker.setConnectionString(
163 getModelProperty(PROPERTY_HOST_NAME) + ":" + getModelProperty(PROPERTY_RFB_PORT_NUMBER));
164 rfbConnectionWorker.execute();
165 }
166
167 public void successfulRfbConnection() {
168 enableConnectionDialog();
169 getView(CONNECTION_VIEW).closeView();
170 }
171
172 public void cancelConnection() {
173 if (networkConnectionWorker != null) {
174 networkConnectionWorker.cancel();
175 }
176 if (rfbConnectionWorker != null) {
177 rfbConnectionWorker.cancel();
178 }
179 }
180
181 public void showConnectionErrorDialog(String message) {
182 final ConnectionView connectionView = (ConnectionView) getView(CONNECTION_VIEW);
183 if (connectionView != null) {
184 connectionView.showConnectionErrorDialog(message);
185 }
186 }
187
188 public void showReconnectDialog(String errorTitle, String errorMessage) {
189 final ConnectionView connectionView = (ConnectionView) getView(CONNECTION_VIEW);
190 if (connectionView != null) {
191 connectionView.showReconnectDialog(errorTitle, errorMessage);
192 }
193 }
194
195 private void setSshOptions() {
196 if (hasSshSupport) {
197 try {
198 final boolean useSsh = (Boolean)getViewProperty(PROPERTY_USE_SSH);
199 setModelProperty(PROPERTY_USE_SSH, useSsh, boolean.class);
200 } catch (PropertyNotFoundException e) {
201 //nop
202 }
203 setModelProperty(PROPERTY_SSH_USER_NAME, getViewPropertyOrNull(PROPERTY_SSH_USER_NAME));
204 setModelProperty(PROPERTY_SSH_HOST_NAME, getViewPropertyOrNull(PROPERTY_SSH_HOST_NAME));
205 setModelProperty(PROPERTY_SSH_PORT_NUMBER, getViewPropertyOrNull(PROPERTY_SSH_PORT_NUMBER));
206 setViewProperty(PROPERTY_SSH_PORT_NUMBER, getModelProperty(PROPERTY_SSH_PORT_NUMBER));
207 }
208 }
209
210 private void syncModels(int paramSettingsMask) {
211 final ConnectionParams cp = (ConnectionParams) getModel(CONNECTION_PARAMS_MODEL);
212 final ConnectionParams mostSuitableConnection = connectionsHistory.getMostSuitableConnection(cp);
213 cp.completeEmptyFieldsFrom(mostSuitableConnection);
214 rfbSettings.copyDataFrom(connectionsHistory.getProtocolSettings(mostSuitableConnection), paramSettingsMask & 0xffff);
215 uiSettings.copyDataFrom(connectionsHistory.getUiSettingsData(mostSuitableConnection), (paramSettingsMask >> 16) & 0xffff);
216 if ( ! cp.isHostNameEmpty()) {
217 connectionsHistory.reorder(cp, rfbSettings, uiSettings);
218 }
219
220 // protocolSettings.addListener(connectionsHistory);
221 // uiSettings.addListener(connectionsHistory);
222 }
223
224 public void populateFromHistoryItem(ConnectionParams connectionParams) {
225 setModelProperty(PROPERTY_HOST_NAME, connectionParams.hostName);
226 setModelProperty(PROPERTY_RFB_PORT_NUMBER, connectionParams.getPortNumber(), int.class);
227 setModelProperty(PROPERTY_USE_SSH, connectionParams.useSsh(), boolean.class);
228 setModelProperty(PROPERTY_SSH_HOST_NAME, connectionParams.sshHostName);
229 setModelProperty(PROPERTY_SSH_PORT_NUMBER, connectionParams.getSshPortNumber(), int.class);
230 setModelProperty(PROPERTY_SSH_USER_NAME, connectionParams.sshUserName);
231 populateFrom(CONNECTION_PARAMS_MODEL);
232 rfbSettings.copyDataFrom(connectionsHistory.getProtocolSettings(connectionParams));
233 uiSettings.copyDataFrom(connectionsHistory.getUiSettingsData(connectionParams));
234 }
235
236 public void clearHistory() {
237 connectionsHistory.clear();
238 connectionsHistory.reorder((ConnectionParams) getModel(CONNECTION_PARAMS_MODEL), rfbSettings, uiSettings);
239 populateFrom(CONNECTIONS_HISTORY_MODEL);
240 clearMessage();
241 }
242
243 public void showMessage(String message) {
244 setViewProperty(PROPERTY_STATUS_BAR_MESSAGE, message);
245 }
246
247 public void clearMessage() {
248 showMessage("");
249 }
250
251 public void setConnectionWorkerFactory(AbstractConnectionWorkerFactory connectionWorkerFactory) {
252 this.connectionWorkerFactory = connectionWorkerFactory;
253 }
254
255 public void reconnect(String predefinedPassword) {
256 connectionWorkerFactory.setPredefinedPassword(predefinedPassword);
257 if (allowInteractive) {
258 clearMessage();
259 enableConnectionDialog();
260 show();
261 populate();
262 } else if (needReconnection) {
263 connect();
264 }
265 }
266
267 public void clearPredefinedPassword() {
268 connectionWorkerFactory.setPredefinedPassword(null);
269 }
270
271 public UiSettings getUiSettings() {
272 return uiSettings;
273 }
274
275 public ProtocolSettings getRfbSettings() {
276 return rfbSettings;
277 }
278
279 public boolean needReconnection() {
280 return needReconnection;
281 }
282
283 public void setNeedReconnection(boolean need) {
284 needReconnection = need;
285 }
286
287 public boolean allowInteractive() {
288 return allowInteractive;
289 }
290 }