# HG changeset patch # User Dirk Olmes # Date 1411523493 -7200 # Node ID 6a62de088c002405a646345db11f0f8ba2357183 # Parent d8958cb11043e503b971ef3ae3702a4d286b2300 implement editing existing connections diff -r d8958cb11043 -r 6a62de088c00 RemoteViewer/Connection.py --- a/RemoteViewer/Connection.py Tue Sep 23 01:44:24 2014 +0200 +++ b/RemoteViewer/Connection.py Wed Sep 24 03:51:33 2014 +0200 @@ -1,7 +1,15 @@ - +from uuid import uuid4 from .Viewer import VncViewer, FreeRdp -KEYS = ['geometry', 'host', 'title', 'type', 'user'] +KEYS = ['geometry', 'id', 'host', 'title', 'type', 'user'] + +def filter_string(func): + '''decorator function to filter turn empty strings into None''' + def __perform(object, string): + if len(string) == 0: + string = None + return func(object, string) + return __perform class Connection(object): @staticmethod @@ -15,14 +23,16 @@ def __init__(self): self.geometry = None + self.id = str(uuid4()) self.host = None self.title = None self.type = 'vnc' self.user = None def __str__(self): - return 'connection "{}" of type {} to {} with user {} and geometry {}'.format(self.title, self.type, self.host, self.user, self.geometry) + return 'connection@{} "{}" of type {} to {} with user {} and geometry {}'.format(self.title, id(self), self.type, self.host, self.user, self.geometry) + @filter_string def setTitle(self, title): '''This is a slot that is connected to a signal in the GUI''' self.title = title @@ -31,14 +41,17 @@ '''This is a slot that is connected to a signal in the GUI''' self.type = type + @filter_string def setHost(self, hostname): '''This is a slot that is connected to a signal in the GUI''' self.host = hostname + @filter_string def setUser(self, user): '''This is a slot that is connected to a signal in the GUI''' - self.user= user + self.user = user + @filter_string def setGeometry(self, geometry): '''This is a slot that is connected to a signal in the GUI''' self.geometry = geometry @@ -50,14 +63,21 @@ return self.host def validate(self): - # TODO: implement proper validation - pass + validationErrors = [] + if self.host is None: + validationErrors.append('host may not be empty') + if len(validationErrors) > 0: + return validationErrors + else: + return None def toJson(self): json = {} for key in KEYS: if hasattr(self, key): - json[key] = getattr(self, key) + value = getattr(self, key) + if value is not None: + json[key] = value return json def open(self, parent): diff -r d8958cb11043 -r 6a62de088c00 RemoteViewer/ConnectionDialog.py --- a/RemoteViewer/ConnectionDialog.py Tue Sep 23 01:44:24 2014 +0200 +++ b/RemoteViewer/ConnectionDialog.py Wed Sep 24 03:51:33 2014 +0200 @@ -3,23 +3,35 @@ from .Ui_ConnectionDialog import Ui_ConnectionDialog class ConnectionDialog(QDialog): - def __init__(self, windowTitle=''): + def __init__(self, windowTitle='', connection=Connection()): super(QDialog, self).__init__() - self.__initDefaultValues() + self.connection = connection self.__initUi(windowTitle) - self.__connectConnection() - - def __initDefaultValues(self): - self.connection = Connection() def __initUi(self, windowTitle): self.ui = Ui_ConnectionDialog() self.ui.setupUi(self) self.setWindowTitle(windowTitle) + self.__initTypeDropDown() + self.__loadConnection() + self.__connectWidgetSlots() + + def __initTypeDropDown(self): self.ui.type.addItem('rdp') self.ui.type.addItem('vnc') - def __connectConnection(self): + def __loadConnection(self): + self.ui.title.setText(self.connection.title) + if self.connection.type == 'rdp': + index = 0 + else: + index = 1 + self.ui.type.setCurrentIndex(index) + self.ui.hostname.setText(self.connection.host) + self.ui.username.setText(self.connection.user) + self.ui.geometry.setText(self.connection.geometry) + + def __connectWidgetSlots(self): self.ui.title.textChanged.connect(self.connection.setTitle) self.ui.type.activated[str].connect(self.connection.setType) self.ui.hostname.textChanged.connect(self.connection.setHost) @@ -27,4 +39,9 @@ self.ui.geometry.textChanged.connect(self.connection.setGeometry) def accept(self): - super(ConnectionDialog, self).accept() + validationErrors = self.connection.validate() + if validationErrors is None: + super(ConnectionDialog, self).accept() + else: + # TODO: display the error message + print('connection has validation errors:', validationErrors) diff -r d8958cb11043 -r 6a62de088c00 RemoteViewer/RemoteViewer.py --- a/RemoteViewer/RemoteViewer.py Tue Sep 23 01:44:24 2014 +0200 +++ b/RemoteViewer/RemoteViewer.py Wed Sep 24 03:51:33 2014 +0200 @@ -10,6 +10,7 @@ def __init__(self): super(RemoteViewer, self).__init__() self.settings = Settings() + self.activeConnection = None self.__initUi() def __initUi(self): @@ -22,6 +23,10 @@ model = GenericListModel(self, connections, 'displayString') self.ui.hostList.setModel(model) + def connectionSelected(self, index): + self.activeConnection = index.internalPointer() + self.ui.actionEdit.setEnabled(True) + @ExceptionSafeSlot() def newConnection(self): dialog = ConnectionDialog('New Connection') @@ -30,6 +35,13 @@ connection = dialog.connection self.settings.addConnection(connection) + @ExceptionSafeSlot() + def editConnection(self): + title = 'Edit {}'.format(self.activeConnection.displayString()) + dialog = ConnectionDialog(title, self.activeConnection) + dialog.exec_() + self.settings.update(self.activeConnection) + @ExceptionSafeSlot('QModelIndex') def openConnection(self, index): connection = index.internalPointer() diff -r d8958cb11043 -r 6a62de088c00 RemoteViewer/RemoteViewer.ui --- a/RemoteViewer/RemoteViewer.ui Tue Sep 23 01:44:24 2014 +0200 +++ b/RemoteViewer/RemoteViewer.ui Wed Sep 24 03:51:33 2014 +0200 @@ -34,6 +34,7 @@ Connection + @@ -56,6 +57,17 @@ Ctrl+N + + + false + + + Edit + + + Ctrl+E + + @@ -107,9 +119,43 @@ + + actionEdit + triggered() + RemoteViewer + editConnection() + + + -1 + -1 + + + 219 + 204 + + + + + hostList + clicked(QModelIndex) + RemoteViewer + connectionSelected(QModelIndex) + + + 143 + 263 + + + 120 + 383 + + + newConnection() openConnection(QModelIndex) + editConnection() + connectionSelected(QModelIndex) diff -r d8958cb11043 -r 6a62de088c00 RemoteViewer/Settings.py --- a/RemoteViewer/Settings.py Tue Sep 23 01:44:24 2014 +0200 +++ b/RemoteViewer/Settings.py Wed Sep 24 03:51:33 2014 +0200 @@ -3,6 +3,7 @@ from .Connection import Connection CONNECTIONS_KEY = 'connections' +ID_KEY = 'id' class Settings(AbstractSettings): def __init__(self): @@ -26,3 +27,15 @@ def addConnection(self, connection): json = connection.toJson() self.config[CONNECTIONS_KEY].append(json) + self.persist() + + def update(self, connection): + id = connection.id + self.__remove(id) + self.addConnection(connection) + + def __remove(self, id): + allConnections = self.config[CONNECTIONS_KEY] + for conn in allConnections: + if conn[ID_KEY] == id: + allConnections.remove(conn)