View Javadoc
1   package com.nilhcem.fakesmtp.model;
2   
3   import com.nilhcem.fakesmtp.core.I18n;
4   import com.nilhcem.fakesmtp.core.exception.BindPortException;
5   import com.nilhcem.fakesmtp.core.exception.InvalidHostException;
6   import com.nilhcem.fakesmtp.core.exception.InvalidPortException;
7   import com.nilhcem.fakesmtp.core.exception.OutOfRangePortException;
8   import com.nilhcem.fakesmtp.server.SMTPServerHandler;
9   
10  import java.net.InetAddress;
11  import java.net.UnknownHostException;
12  import java.util.HashMap;
13  import java.util.List;
14  import java.util.Map;
15  
16  /**
17   * UI presentation model of the application.
18   * <p>
19   * The essence of a Presentation Model is of a fully self-contained class that represents all the data
20   * and behavior of the UI window, but without any of the controls used to render that UI on the screen.
21   * </p>
22   *
23   * @author Nilhcem
24   * @since 1.0
25   * @see <a href="link">http://martinfowler.com/eaaDev/PresentationModel.html</a>
26   */
27  public enum UIModel {
28  	INSTANCE;
29  
30  	private boolean started = false; // server is not started by default
31  	private String portStr;
32  	private String hostStr;
33  	private int nbMessageReceived = 0;
34  	private String savePath = I18n.INSTANCE.get("emails.default.dir");
35  	private final Map<Integer, String> listMailsMap = new HashMap<Integer, String>();
36  	private List<String> relayDomains;
37  
38  	UIModel() {
39  	}
40  
41  	/**
42  	 * Happens when a user clicks on the start button.
43  	 * <p>
44  	 * This method will notify the {@code SMTPServerHandler} to start the server.
45  	 * </p>
46  	 *
47  	 * @throws InvalidPortException when the port is invalid.
48  	 * @throws BindPortException when the port cannot be bound.
49  	 * @throws OutOfRangePortException when the port is out of range.
50  	 * @throws InvalidHostException when the address cannot be resolved.
51  	 * @throws RuntimeException when an unknown exception happened.
52  	 */
53  	public void toggleButton() throws BindPortException, OutOfRangePortException, InvalidPortException, InvalidHostException {
54  		if (started) {
55  			// Do nothing. We can't stop the server. User has to quit the app (issue with SubethaSMTP)
56  		} else {
57  			try {
58  				int port = Integer.parseInt(portStr);
59  				InetAddress host = null;
60  				if (hostStr != null && !hostStr.isEmpty()) {
61  					host = InetAddress.getByName(hostStr);
62  				}
63  
64  				SMTPServerHandler.INSTANCE.startServer(port, host);
65  			} catch (NumberFormatException e) {
66  				throw new InvalidPortException(e);
67  			} catch	(UnknownHostException e) {
68  				throw new InvalidHostException(e, hostStr);
69  			}
70  		}
71  		started = !started;
72  	}
73  
74  	/**
75  	 * Returns {@code true} if the server is started.
76  	 *
77  	 * @return {@code true} if the server is started.
78  	 */
79  	public boolean isStarted() {
80  		return started;
81  	}
82  
83  	public void setPort(String port) {
84  		this.portStr = port;
85  	}
86  
87  	public void setHost(String host) {
88  		this.hostStr = host;
89  	}
90  
91  	public int getNbMessageReceived() {
92  		return nbMessageReceived;
93  	}
94  
95  	public void setNbMessageReceived(int nbMessageReceived) {
96  		this.nbMessageReceived = nbMessageReceived;
97  	}
98  
99  	public String getSavePath() {
100 		return savePath;
101 	}
102 
103 	public void setSavePath(String savePath) {
104 		this.savePath = savePath;
105 	}
106 
107 	public Map<Integer, String> getListMailsMap() {
108 		return listMailsMap;
109 	}
110 
111 	public List<String> getRelayDomains() {
112 		return relayDomains;
113 	}
114 
115 	public void setRelayDomains(List<String> relayDomains) {
116 		this.relayDomains = relayDomains;
117 	}
118 }