diff --git a/client/dialog.py b/client/dialog.py index c2aefdf..0ac0e1e 100644 --- a/client/dialog.py +++ b/client/dialog.py @@ -73,14 +73,15 @@ class ConnectionDialog(QDialog, Ui_ConnectionDialog): @property def settings(self) -> ConnectionOptions: - return ConnectionOptions(ip=self.server_address_input.text(), - port=int(self.port_input.text()), + return ConnectionOptions(ip=self.server_address_input.text() or constants.DEFAULT_IP, + port=int(self.port_input.text() or constants.DEFAULT_PORT), nickname=self.nickname_input.text(), password=self.password_input.text(), remember=self.remember_checkbox.checkState()) def validation(self, full: bool = True) -> None: address, port = self.validate_address() + nickname = self.validate_nickname() if not address and not port: self.status_bar.showMessage('Please fill in a valid server address and port.', 3000) @@ -88,10 +89,11 @@ class ConnectionDialog(QDialog, Ui_ConnectionDialog): self.status_bar.showMessage('Please fill in a valid server address.', 3000) elif not port: self.status_bar.showMessage('Please fill in a valid port number.', 3000) - elif full and not self.validate_nickname(): + elif full and not nickname: self.status_bar.showMessage('Please use a valid nickname. Letters and digits, 3-15 characters long.', 3000) - self.connect_button.setDisabled(not (self.validate_address() and self.validate_nickname())) + self.connect_button.setDisabled(not (address and port and nickname)) + self.test_connection_button.setDisabled(not (address and port)) def validate_nickname(self) -> bool: """Returns True if the nickname follows the nickname guidelines requested.""" @@ -100,7 +102,7 @@ class ConnectionDialog(QDialog, Ui_ConnectionDialog): def validate_address(self) -> Tuple[bool, bool]: """Returns True if the server address and port combination is valid""" address = self.server_address_input.text() or constants.DEFAULT_IP - port = self.port_input.text() or constants.DEFAULT_PORT + port = self.port_input.text() or str(constants.DEFAULT_PORT) valid_address = len(address) > 0 and re.match(r'^\d{1,4}\.\d{1,4}\.\d{1,4}\.\d{1,4}|localhost$', address) is not None diff --git a/client/gui.py b/client/gui.py index acd20af..120dc35 100644 --- a/client/gui.py +++ b/client/gui.py @@ -82,14 +82,7 @@ class MainWindow(QMainWindow, Ui_MainWindow): self.get_message_history() def closeEvent(self, event): - if self.nicknameDialog and not self.closed: - logger.debug('Closing nickname dialog before main window') - self.closed = True - self.nicknameDialog.close() - else: - self.receiveThread.stop() - self.connectionsListTimer.stop() - + self.receiveThread.stop() event.accept() # let the window close def eventFilter(self, obj, event) -> bool: diff --git a/client/main.py b/client/main.py index d18349e..e48237c 100644 --- a/client/main.py +++ b/client/main.py @@ -1,3 +1,4 @@ +import logging from typing import Tuple from PyQt5.QtWidgets import QApplication @@ -5,16 +6,16 @@ from PyQt5.QtWidgets import QApplication from client.dialog import ConnectionDialog from client.gui import MainWindow - -# def connection_dialog() -> Tuple[str, int, str, str, bool]: -# connect_dialog = ConnectionDialog() +logger = logging.getLogger(__file__) def main(): app = QApplication([]) app.setApplicationName("TCPChat Client") connect_dialog = ConnectionDialog() - # m = MainWindow() app.exec_() + if connect_dialog.connect_pressed: - print(connect_dialog.settings) + settings = connect_dialog.settings + m = MainWindow(settings.ip, settings.port, settings.nickname) + app.exec_() diff --git a/constants.py b/constants.py index 7fbd75b..d3b075e 100644 --- a/constants.py +++ b/constants.py @@ -11,7 +11,7 @@ MINIMUM_CONTRAST = 4.65 DATABASE = os.path.join(__BASE_DIR, 'messages.db') DEFAULT_IP = "127.0.0.1" -DEFAULT_PORT = "5555" +DEFAULT_PORT = 5555 ConnectionOptions = namedtuple('ConnectionOptions', ['ip', 'port', 'nickname', 'password', 'remember'])