コード例 #1
0
ファイル: client.py プロジェクト: SergeyBurik/python_projects
    def setupMainUI(self):
        # greetings
        hour = datetime.datetime.now().hour
        if 7 < hour < 12:
            greetings = 'Good Morning,'
        elif 12 < hour < 18:
            greetings = 'Good Day,'
        elif 18 < hour < 23:
            greetings = 'Good Evening,'
        else:
            greetings = 'Good Night,'

        self.ui.greetingsLabel.setText(
            greetings.replace(',', f', {self.username}.'))

        # list of contacts
        # get list of contacts
        utils.send_message(
            self.transport, {
                'action': 'get_contacts',
                'time': time.time(),
                'username': self.username
            })
        contacts = utils.get_message(self.transport)
        self.contacts = contacts
        print(contacts)
        for contact in contacts['response']:
            self.ui.contactsListWidget.addItem(contact['username'])

        # new contact
        self.ui.newContactBtn.clicked.connect(self.new_contact)

        self.ui.contactsListWidget.itemDoubleClicked.connect(
            lambda x: self.changeChat(x))
        self.ui.sendMessageBtn.clicked.connect(self.sendMessage)
コード例 #2
0
ファイル: client.py プロジェクト: SergeyBurik/python_projects
    def new_contact(self):
        # get list of users
        try:
            utils.send_message(
                self.transport, {
                    'action': 'get_users',
                    'username': self.username,
                    'time': time.time()
                })

            users = utils.get_message(self.transport)

            new_contact_d = NewContactDialog(
                [client['user'] for client in users['response']])

            new_contact_d.exec_()
            utils.send_message(
                self.transport, {
                    'action': 'create_contact',
                    'username': self.username,
                    'contact': new_contact_d.current,
                    'time': time.time()
                })

            if utils.get_message(self.transport)['code'] == 200:
                print(new_contact_d.current)
                self.ui.contactsListWidget.addItem(new_contact_d.current)
        except Exception as e:
            print(e)
コード例 #3
0
ファイル: client.py プロジェクト: SergeyBurik/python_projects
    def sendMessage(self):
        print(self.chat)
        textInput = self.ui.messageTextEdit
        if self.chat and textInput.toPlainText():
            utils.send_message(
                self.transport, {
                    "action": "message",
                    "time": time.time(),
                    "username": self.username,
                    "message_text": textInput.toPlainText(),
                    "to_user": self.chat
                })

            data = utils.get_message(self.transport)
            if data["code"] == 200:
                self.ui.messagesListWidget.addItem(
                    f"{self.username}\n{textInput.toPlainText()}")

        textInput.clear()
コード例 #4
0
ファイル: client.py プロジェクト: SergeyBurik/python_projects
    def changeChat(self, chat):
        try:
            self.chat = chat.text()
            print(chat.text())

            self.ui.messagesListWidget.clear()

            utils.send_message(
                self.transport, {
                    "action": "get_messages",
                    "contact": self.chat,
                    "time": time.time(),
                    "username": self.username
                })
            messages = utils.get_message(self.transport)

            for message in messages['response']:
                self.ui.messagesListWidget.addItem(
                    f'{message["sender"]}\n{message["message_text"]}')
        except Exception as e:
            print(e)
コード例 #5
0
ファイル: client.py プロジェクト: SergeyBurik/python_projects
    def init_connection(self):
        self.transport = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.transport.connect((HOST, PORT))

        # login user
        login = {
            'action': 'login',
            'time': time.time(),
            'username': self.username,
            'password': self.password
        }

        utils.send_message(self.transport, login)
        data = utils.get_message(self.transport)

        if data['code'] == 200:
            print('Successful login')
        elif data['code'] == 404:
            # if there is no such account, it will be created automatically
            print(data['message'])
        elif data['code'] == 403:
            print(data['message'])

        # presence request
        if data['code'] == 200 or data['code'] == 404:
            presence = {
                'action': 'presence',
                'time': time.time(),
                'username': self.username
            }
            utils.send_message(self.transport, presence)

            if utils.get_message(self.transport)['code'] == 200:
                pass
            else:
                self.transport.close()
                sys.exit()