Merge branch 'develop' into f/actual_settings
Conflicts: src/main/java/envoy/client/LocalDB.java src/main/java/envoy/client/ui/ChatWindow.java
This commit is contained in:
		@@ -33,21 +33,23 @@ public class LocalDB {
 | 
			
		||||
 | 
			
		||||
	private File			localDB;
 | 
			
		||||
	private User			sender;
 | 
			
		||||
	private final long		id;
 | 
			
		||||
	private List<Chat>		chats			= new ArrayList<>();
 | 
			
		||||
	private ObjectFactory	objectFactory	= new ObjectFactory();
 | 
			
		||||
	private DatatypeFactory	datatypeFactory;
 | 
			
		||||
 | 
			
		||||
	private Sync	unreadMessagesSync	= objectFactory.createSync();
 | 
			
		||||
	private Sync	sync				= objectFactory.createSync();
 | 
			
		||||
	private Sync	readMessages		= objectFactory.createSync();
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Constructs an empty local database.
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param sender the user that is logged in with this client
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 **/
 | 
			
		||||
	 */
 | 
			
		||||
 | 
			
		||||
	public LocalDB(User sender) {
 | 
			
		||||
		this.sender	= sender;
 | 
			
		||||
		id			= sender.getID();
 | 
			
		||||
		try {
 | 
			
		||||
			datatypeFactory = DatatypeFactory.newInstance();
 | 
			
		||||
		} catch (DatatypeConfigurationException e) {
 | 
			
		||||
@@ -62,11 +64,11 @@ public class LocalDB {
 | 
			
		||||
	 * @param localDBDir the directory where we wish to save/load the database from.
 | 
			
		||||
	 * @throws EnvoyException if the directory selected is not an actual directory.
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 **/
 | 
			
		||||
	public void initializeDBFile(File localDBDir) throws EnvoyException {
 | 
			
		||||
	 */
 | 
			
		||||
	public void initializeDBFile(File localDBDir) throws EnvoyException {
 | 
			
		||||
		if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException(
 | 
			
		||||
				String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath()));
 | 
			
		||||
		localDB = new File(localDBDir, id + ".db");
 | 
			
		||||
		localDB = new File(localDBDir, sender.getID() + ".db");
 | 
			
		||||
		if (localDB.exists()) loadFromLocalDB();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -75,10 +77,11 @@ public class LocalDB {
 | 
			
		||||
	 * It is theoretically possible to fail due to unknown causes.<br>
 | 
			
		||||
	 * In such a case, every message sent/ received during that session will be
 | 
			
		||||
	 * lost.
 | 
			
		||||
	 *
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @throws IOException gets thrown, if saving failed for some reason
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 **/
 | 
			
		||||
	public void saveToLocalDB() {
 | 
			
		||||
	 */
 | 
			
		||||
	public void saveToLocalDB() throws IOException{
 | 
			
		||||
		try {
 | 
			
		||||
			localDB.getParentFile().mkdirs();
 | 
			
		||||
			localDB.createNewFile();
 | 
			
		||||
@@ -99,7 +102,7 @@ public class LocalDB {
 | 
			
		||||
	 *
 | 
			
		||||
	 * @throws EnvoyException if something fails while loading.
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 **/
 | 
			
		||||
	 */
 | 
			
		||||
	@SuppressWarnings("unchecked")
 | 
			
		||||
	private void loadFromLocalDB() throws EnvoyException {
 | 
			
		||||
		try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) {
 | 
			
		||||
@@ -135,18 +138,10 @@ public class LocalDB {
 | 
			
		||||
		return message;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private Sync	unreadMessagesSync	= objectFactory.createSync();
 | 
			
		||||
	public Sync		sync				= objectFactory.createSync();
 | 
			
		||||
	public Sync		readMessages		= objectFactory.createSync();
 | 
			
		||||
 | 
			
		||||
	public Sync fillSync(long userId) {
 | 
			
		||||
 | 
			
		||||
		addWaitingMessagesToSync();
 | 
			
		||||
 | 
			
		||||
		getSentStateMessagesFromLocalDB();
 | 
			
		||||
		for (int i = 0; i < readMessages.getMessages().size(); i++) {
 | 
			
		||||
			sync.getMessages().add(readMessages.getMessages().get(i));
 | 
			
		||||
		}
 | 
			
		||||
		sync.getMessages().addAll(readMessages.getMessages());
 | 
			
		||||
		readMessages.getMessages().clear();
 | 
			
		||||
 | 
			
		||||
		System.out.println(sync.getMessages().size());
 | 
			
		||||
@@ -161,47 +156,29 @@ public class LocalDB {
 | 
			
		||||
				// SENT)
 | 
			
		||||
				for (int j = 0; j < sync.getMessages().size(); j++) {
 | 
			
		||||
					if (j == i) {
 | 
			
		||||
						sync.getMessages()
 | 
			
		||||
							.get(j)
 | 
			
		||||
							.getMetadata()
 | 
			
		||||
							.setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
 | 
			
		||||
						sync.getMessages()
 | 
			
		||||
							.get(j)
 | 
			
		||||
							.getMetadata()
 | 
			
		||||
							.setState(returnSync.getMessages().get(j).getMetadata().getState());
 | 
			
		||||
						sync.getMessages().get(j).getMetadata().setMessageId(returnSync.getMessages().get(j).getMetadata().getMessageId());
 | 
			
		||||
						sync.getMessages().get(j).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState());
 | 
			
		||||
					}
 | 
			
		||||
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
			if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
 | 
			
		||||
					&& returnSync.getMessages().get(i).getMetadata().getSender() != 0
 | 
			
		||||
			if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() != 0
 | 
			
		||||
					&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
 | 
			
		||||
				// these are the unread Messages from the server
 | 
			
		||||
				unreadMessagesSync.getMessages().add(returnSync.getMessages().get(i));
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0
 | 
			
		||||
					&& returnSync.getMessages().get(i).getMetadata().getSender() == 0
 | 
			
		||||
			if (returnSync.getMessages().get(i).getMetadata().getMessageId() != 0 && returnSync.getMessages().get(i).getMetadata().getSender() == 0
 | 
			
		||||
					&& returnSync.getMessages().get(i).getMetadata().getState() == MessageState.RECEIVED) {
 | 
			
		||||
				// Update Messages in localDB to state RECEIVED
 | 
			
		||||
				for (int j = 0; j < getChats().size(); j++) {
 | 
			
		||||
					if (getChats().get(j)
 | 
			
		||||
						.getRecipient()
 | 
			
		||||
						.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
 | 
			
		||||
					if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
 | 
			
		||||
						for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
 | 
			
		||||
							if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
 | 
			
		||||
								.getMessages()
 | 
			
		||||
							if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
 | 
			
		||||
								.get(i)
 | 
			
		||||
								.getMetadata()
 | 
			
		||||
								.getMessageId()) {
 | 
			
		||||
								// Update Message in LocalDB
 | 
			
		||||
								getChats().get(j)
 | 
			
		||||
									.getModel()
 | 
			
		||||
									.get(k)
 | 
			
		||||
									.getMetadata()
 | 
			
		||||
									.setState(returnSync.getMessages().get(j).getMetadata().getState());
 | 
			
		||||
								getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(j).getMetadata().getState());
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
@@ -215,31 +192,22 @@ public class LocalDB {
 | 
			
		||||
				System.out.println("Message with ID: " + returnSync.getMessages().get(i).getMetadata().getMessageId()
 | 
			
		||||
						+ "was initialized to be set to READ in localDB.");
 | 
			
		||||
				for (int j = 0; j < getChats().size(); j++) {
 | 
			
		||||
					if (getChats().get(j)
 | 
			
		||||
						.getRecipient()
 | 
			
		||||
						.getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
 | 
			
		||||
					if (getChats().get(j).getRecipient().getID() == returnSync.getMessages().get(i).getMetadata().getRecipient()) {
 | 
			
		||||
						System.out.println("Chat with: " + getChats().get(j).getRecipient().getID() + "was selected.");
 | 
			
		||||
						for (int k = 0; k < getChats().get(j).getModel().getSize(); k++) {
 | 
			
		||||
							if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync
 | 
			
		||||
								.getMessages()
 | 
			
		||||
							if (getChats().get(j).getModel().get(k).getMetadata().getMessageId() == returnSync.getMessages()
 | 
			
		||||
								.get(i)
 | 
			
		||||
								.getMetadata()
 | 
			
		||||
								.getMessageId()) {
 | 
			
		||||
								System.out.println("Message with ID: "
 | 
			
		||||
										+ getChats().get(j).getModel().get(k).getMetadata().getMessageId()
 | 
			
		||||
										+ "was selected.");
 | 
			
		||||
								getChats().get(j)
 | 
			
		||||
									.getModel()
 | 
			
		||||
									.get(k)
 | 
			
		||||
									.getMetadata()
 | 
			
		||||
									.setState(returnSync.getMessages().get(i).getMetadata().getState());
 | 
			
		||||
								System.out.println("Message State is now: "
 | 
			
		||||
										+ getChats().get(j).getModel().get(k).getMetadata().getState().toString());
 | 
			
		||||
								System.out.println(
 | 
			
		||||
										"Message with ID: " + getChats().get(j).getModel().get(k).getMetadata().getMessageId() + "was selected.");
 | 
			
		||||
								getChats().get(j).getModel().get(k).getMetadata().setState(returnSync.getMessages().get(i).getMetadata().getState());
 | 
			
		||||
								System.out
 | 
			
		||||
									.println("Message State is now: " + getChats().get(j).getModel().get(k).getMetadata().getState().toString());
 | 
			
		||||
							}
 | 
			
		||||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@@ -256,10 +224,9 @@ public class LocalDB {
 | 
			
		||||
 | 
			
		||||
		sync.getMessages().clear();
 | 
			
		||||
		sync.getUsers().clear();
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	/**
 | 
			
		||||
	 * Adds a message to the "sync" Sync object.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param message the message to send
 | 
			
		||||
@@ -267,15 +234,7 @@ public class LocalDB {
 | 
			
		||||
	 */
 | 
			
		||||
	public void addMessageToSync(Message message) { sync.getMessages().add(message); }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Adds a user to the {@code sync} {@link Sync} object.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param user the user who should be added to the {@link Sync} object
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public void addUserToSync(User user) { sync.getUsers().add(user); }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	/**
 | 
			
		||||
	 * Adds the unread messages returned from the server in the latest sync to the
 | 
			
		||||
	 * right chats in the LocalDB.
 | 
			
		||||
	 * 
 | 
			
		||||
@@ -285,33 +244,16 @@ public class LocalDB {
 | 
			
		||||
		Sync unreadMessages = unreadMessagesSync;
 | 
			
		||||
		for (int i = 0; i < unreadMessages.getMessages().size(); i++)
 | 
			
		||||
			for (int j = 0; j < getChats().size(); j++)
 | 
			
		||||
				if (getChats().get(j)
 | 
			
		||||
					.getRecipient()
 | 
			
		||||
					.getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
 | 
			
		||||
				if (getChats().get(j).getRecipient().getID() == unreadMessages.getMessages().get(i).getMetadata().getSender()) {
 | 
			
		||||
					getChats().get(j).appendMessage(unreadMessages.getMessages().get(i));
 | 
			
		||||
				}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Gets all messages with state SENT from the LocalDB and adds them to the
 | 
			
		||||
	 * "sync" Sync object.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public void getSentStateMessagesFromLocalDB() {
 | 
			
		||||
		for (int i = 0; i < getChats().size(); i++) {
 | 
			
		||||
			for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
 | 
			
		||||
				if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.SENT) {
 | 
			
		||||
					addMessageToSync(getChats().get(i).getModel().get(j));
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Changes all messages with State RECEIVED of a specific chat to State READ.
 | 
			
		||||
	/**
 | 
			
		||||
	 * Changes all messages with state {@code RECEIVED} of a specific chat to state
 | 
			
		||||
	 * {@code READ}.
 | 
			
		||||
	 * <br>
 | 
			
		||||
	 * Adds these Messages to the {@code readMessages} {@link Sync} object.
 | 
			
		||||
	 * Adds these messages to the {@code readMessages} {@link Sync} object.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param currentChat the chat that was just opened
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
@@ -326,29 +268,27 @@ public class LocalDB {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Adds a message with State WAITING to a specific chat in the LocalDB.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * Adds all messages with state {@code WAITING} from the {@link LocalDB} to the
 | 
			
		||||
	 * {@link Sync} object.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param message     the message that is not yet received by the other user
 | 
			
		||||
	 * @param currentChat the chat that is currently open
 | 
			
		||||
	 * @param currentChat the chat that is currently open
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	 */
 | 
			
		||||
	public void addWaitingMessageToLocalDB(Message message, Chat currentChat) { currentChat.appendMessage(message); }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Adds all messages with State WAITING from the {@link LocalDB} to the Sync.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public void addWaitingMessagesToSync() {
 | 
			
		||||
		for (int i = 0; i < getChats().size(); i++) {
 | 
			
		||||
			for (int j = 0; j < getChats().get(i).getModel().getSize(); j++) {
 | 
			
		||||
				if (getChats().get(i).getModel().get(j).getMetadata().getState() == MessageState.WAITING) {
 | 
			
		||||
					// addMessageToSync(localDB.getChats().get(i).getModel().get(j));
 | 
			
		||||
	 */
 | 
			
		||||
	private void addWaitingMessagesToSync() {
 | 
			
		||||
		for (Chat chat : getChats())
 | 
			
		||||
			for (int i = 0; i < chat.getModel().size(); i++)
 | 
			
		||||
				if (chat.getModel().get(i).getMetadata().getState() == MessageState.WAITING) {
 | 
			
		||||
					System.out.println("Got Waiting Message");
 | 
			
		||||
					sync.getMessages().add(0, getChats().get(i).getModel().get(j));
 | 
			
		||||
					sync.getMessages().add(chat.getModel().get(i));
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
@@ -366,15 +306,8 @@ public class LocalDB {
 | 
			
		||||
	public List<Chat> getChats() { return chats; }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return the User who initialised the local Database
 | 
			
		||||
	 * @return the {@link User} who initialized the local database
 | 
			
		||||
	 * @since Envoy v0.1-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public User getUser() { return sender; }
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * @return the id of the client. Used as shortcut for quicker information
 | 
			
		||||
	 *         retrieval
 | 
			
		||||
	 * @since Envoy v0.2-alpha
 | 
			
		||||
	 */
 | 
			
		||||
	public long getId() { return id; }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ import java.awt.event.KeyAdapter;
 | 
			
		||||
import java.awt.event.KeyEvent;
 | 
			
		||||
import java.awt.event.WindowAdapter;
 | 
			
		||||
import java.awt.event.WindowEvent;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import javax.swing.DefaultListModel;
 | 
			
		||||
import javax.swing.JButton;
 | 
			
		||||
@@ -81,7 +82,12 @@ public class ChatWindow extends JFrame {
 | 
			
		||||
		addWindowListener(new WindowAdapter() {
 | 
			
		||||
 | 
			
		||||
			@Override
 | 
			
		||||
			public void windowClosing(WindowEvent e) { localDB.saveToLocalDB(); }
 | 
			
		||||
			public void windowClosing(WindowEvent e) { try {
 | 
			
		||||
				localDB.saveToLocalDB();
 | 
			
		||||
			} catch (IOException e1) {
 | 
			
		||||
				e1.printStackTrace();
 | 
			
		||||
				System.err.println("Could nnot save localDB");
 | 
			
		||||
			} }
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		contentPane.setBorder(new EmptyBorder(space, space, space, space));
 | 
			
		||||
@@ -207,7 +213,7 @@ public class ChatWindow extends JFrame {
 | 
			
		||||
					.get();
 | 
			
		||||
 | 
			
		||||
				// Set all unread messages in the chat to read
 | 
			
		||||
				if (currentChat != null) { localDB.setMessagesToRead(currentChat); }
 | 
			
		||||
				readCurrentChat();
 | 
			
		||||
 | 
			
		||||
				client.setRecipient(user);
 | 
			
		||||
 | 
			
		||||
@@ -289,9 +295,9 @@ public class ChatWindow extends JFrame {
 | 
			
		||||
 | 
			
		||||
		if (!messageEnterTextArea.getText().isEmpty()) try {
 | 
			
		||||
 | 
			
		||||
			// Create and send message object
 | 
			
		||||
			// Create and send message object
 | 
			
		||||
			final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID());
 | 
			
		||||
			localDB.addWaitingMessageToLocalDB(message, currentChat);
 | 
			
		||||
			localDB.addWaitingMessageToLocalDB(message, currentChat);
 | 
			
		||||
			messageList.setModel(currentChat.getModel());
 | 
			
		||||
 | 
			
		||||
			// Clear text field
 | 
			
		||||
@@ -328,7 +334,8 @@ public class ChatWindow extends JFrame {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Updates the data model and the ui every x seconds.
 | 
			
		||||
	 * Updates the data model and the UI repeatedly after a certain amount of
 | 
			
		||||
	 * time.
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @param timeout the amount of time that passes between two requests sent to
 | 
			
		||||
	 *                the server
 | 
			
		||||
@@ -341,9 +348,14 @@ public class ChatWindow extends JFrame {
 | 
			
		||||
				// Synchronize
 | 
			
		||||
				localDB.applySync(
 | 
			
		||||
						client.sendSync(client.getSender().getID(), localDB.fillSync(client.getSender().getID())));
 | 
			
		||||
 | 
			
		||||
				// Process unread messages
 | 
			
		||||
				localDB.addUnreadMessagesToLocalDB();
 | 
			
		||||
				localDB.clearUnreadMessagesSync();
 | 
			
		||||
 | 
			
		||||
				// Mark unread messages as read when they are in the current chat
 | 
			
		||||
				readCurrentChat();
 | 
			
		||||
 | 
			
		||||
				// Update UI
 | 
			
		||||
				SwingUtilities
 | 
			
		||||
					.invokeLater(() -> { updateUserStates(); contentPane.revalidate(); contentPane.repaint(); });
 | 
			
		||||
@@ -357,4 +369,9 @@ public class ChatWindow extends JFrame {
 | 
			
		||||
				if (userList.getModel().getElementAt(i).getID() == localDB.getChats().get(j).getRecipient().getID())
 | 
			
		||||
					userList.getModel().getElementAt(i).setStatus(localDB.getChats().get(j).getRecipient().getStatus());
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Marks messages in the current chat as {@code READ}.
 | 
			
		||||
	 */
 | 
			
		||||
	private void readCurrentChat() { if (currentChat != null) { localDB.setMessagesToRead(currentChat); } }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user