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