def refreshFileList(self): req = request.FileListRequest() transfer.send(self.connection, req) resp = response.FileListResponse.fromJSON(transfer.recieve(self.connection)) if resp.type == MessageType.ERR: QMessageBox.question(self, 'Error', resp.description, QMessageBox.Ok, QMessageBox.Ok) else: files = [data["name"] for fileID, data in resp.files.items() if data['owner'] == self.user] self.shared_files = [data["name"] for fileID, data in resp.files.items() if data['owner'] != self.user] self.filesID = [[data["name"], fileID] for fileID, data in resp.files.items() if data['owner'] == self.user] self.files = files files_dict = {f'Files for user {self.user}': files, 'Shared': self.shared_files} items = [] for key, values in files_dict.items(): item = QTreeWidgetItem([key]) for value in values: fileInfo = self.getFileInfo(value) #created, last_edit, last_edited_user child = QTreeWidgetItem([value, fileInfo[0], fileInfo[1], fileInfo[2]]) item.addChild(child) items.append(item) self.fileTree.clear() self.fileTree.insertTopLevelItems(0, items)
def loginButtonClicked(self): loginBoxValue = self.loginTextBox.text() passwordBoxValue = self.passwordTextBox.text() if (not loginBoxValue) or (not passwordBoxValue): FunnyClassForErrorMsg().showMsg(self, "Type Login and Password") else: self.username = self.loginTextBox.text() self.passwd = self.passwordTextBox.text() s = Connection().getSocket() req = request.AuthRequest(user=self.username, passwd=self.passwd) transfer.send(s, req) resp = response.AuthResponse.fromJSON(transfer.recieve(s)) if resp.type == MessageType.ERR: FunnyClassForErrorMsg().showMsg(self, resp.description) else: if self.operatingWindow is None: self.operatingWindow = OperatingWindow(self.username) self.operatingWindow.set_connection(s) self.operatingWindow.refreshFileList() self.operatingWindow.show() self.hide() else: self.operatingWindow.close() self.operatingWindow = None
def addButtonClicked(self): text, ok = QInputDialog.getText(self, 'Creating new file', 'Type filename') if ok: req = request.NewFileRequest(fileName=text) transfer.send(self.connection, req) resp = response.OkResponse.fromJSON(transfer.recieve(self.connection)) FunnyClassForErrorMsg().showMsg(self, resp.description) self.refreshFileList()
def saveButtonPressed(self): content = self.textEdit.toPlainText() req = request.PushRequest(fileID=self.fileID, content=content) transfer.send(self.connection, req) resp = response.OkResponse.fromJSON(transfer.recieve(self.connection)) FunnyClassForErrorMsg().showMsg(self, resp.description) self.close()
def getFileContent(self, fileID): req = request.PullRequest(fileID) transfer.send(self.connection, req) resp = response.FileListResponse.fromJSON(transfer.recieve(self.connection)) if resp.type == MessageType.ERR: QMessageBox.question(self, 'Error', resp.description, QMessageBox.Ok, QMessageBox.Ok) else: content = resp.content return content
def getFileID(self, filename): req = request.FileListRequest() transfer.send(self.connection, req) resp = response.FileListResponse.fromJSON(transfer.recieve(self.connection)) if resp.type == MessageType.ERR: QMessageBox.question(self, 'Error', resp.description, QMessageBox.Ok, QMessageBox.Ok) else: fileID = [fileID for fileID, data in resp.files.items() if data["name"] == filename] return fileID[0]
def deleteButtonClicked(self): seletedFile = self.fileTree.selectedItems() if seletedFile: filename = seletedFile[0].text(0) fileID = self.getFileID(filename) req = request.DeleteFileRequest(fileID) transfer.send(self.connection, req) resp = response.ErrResponse.fromJSON(transfer.recieve(self.connection)) FunnyClassForErrorMsg().showMsg(self, resp.description) self.refreshFileList()
def setup(self): # Try to authenticate try: data = transfer.recieve(self.request) req = request.AuthRequest.fromJSON(data) if req.type != MessageType.AUTH: raise DatabaseException('Authenticate first') # Authenticate user = req.user passwd = req.passwd self.dataBase.authenticate(user, passwd) # Save the username and the session ID hash sessionID = str(time.time()) + user + self.client_address[0] sessionID = hashlib.sha1(sessionID.encode('utf-8')) self.sessionID = sessionID.hexdigest()[:12] self.username = user resp = response.AuthResponse( sessionID=self.sessionID, user=user, ) log.info( 'New session %s of user %s', self.sessionID, self.username) except (JSONDecodeError, TypeError, AttributeError) as ex: resp = response.ErrResponse(err=f'Could not parse request: {ex}') log.warning( 'Unable to authenticate %s: %s', self.client_address[0], resp.description, ) except DatabaseException as ex: resp = response.ErrResponse(err=ex) log.warning( 'Unable to authenticate %s: %s', self.client_address[0], resp.description, ) except ( ConnectionAbortedError, UnicodeDecodeError, ConnectionResetError, ): return transfer.send(self.request, resp)
def handle(self): if self.sessionID is None: return while True: # Handle incoming requests try: data = transfer.recieve(self.request) req = request.Request.fromJSON(data) if req.type == MessageType.LIST_FILES: req = request.FileListRequest.fromJSON(data) resp = response.FileListResponse( files=self.dataBase.listFiles(self.username), user=self.username, ) elif req.type == MessageType.PULL: req = request.PullRequest.fromJSON(data) resp = response.PullResponse( fileID=req.fileID, content=self.dataBase.pullFile( self.username, req.fileID, ), ) elif req.type == MessageType.PUSH: req = request.PushRequest.fromJSON(data) self.dataBase.pushFile( self.username, req.fileID, req.content, ) resp = response.OkResponse( action=f'Push file {req.fileID}') elif req.type == MessageType.NEW_FILE: req = request.NewFileRequest.fromJSON(data) self.dataBase.newFile( self.username, req.fileName, ) resp = response.OkResponse( action=f'Create new file {req.fileName}') elif req.type == MessageType.DELETE_FILE: req = request.DeleteFileRequest.fromJSON(data) ownedOrShared = self.dataBase.deleteFile( self.username, req.fileID, ) resp = response.OkResponse( action=f'Delete {ownedOrShared} file { req.fileID }') elif req.type == MessageType.NEW_SHARE: req = request.NewShareRequest.fromJSON(data) self.dataBase.newShare( req.fileID, self.username, req.user, ) resp = response.OkResponse( action=f'Share file {req.fileID} to {req.user}') elif req.type == MessageType.DELETE_SHARE: req = request.DeleteShareRequest.fromJSON(data) self.dataBase.deleteShare( req.fileID, self.username, req.user, ) resp = response.OkResponse( action=f'Unshare file {req.fileID} from {req.user}') else: raise DatabaseException(f'Unknown action: {req.type}') except DatabaseException as ex: resp = response.ErrResponse(err=f'{ex}') log.error( 'Session %s of user %s: Request:%s Response:%s', self.sessionID, self.username, req, resp, ) except (JSONDecodeError, TypeError, AttributeError) as ex: resp = response.ErrResponse(err=f'Malformed request: {ex}') log.error( 'Session %s of user %s: %s', self.sessionID, self.username, resp.description, ) break except (ConnectionAbortedError, ConnectionResetError): break else: log.info( 'Session %s of user %s: Request:%s Response:%s', self.sessionID, self.username, req, resp, ) transfer.send(self.request, resp) log.info('Closed session %s of user %s', self.sessionID, self.username)