# HG changeset patch # User Dirk Olmes # Date 1411181219 -7200 # Node ID 8905b45cf9fb321474152e294012d8fc0475ce58 # Parent 77fa82dcc95686aa7b2ece6b9dea5df6ed426b8c load the settings file when starting, persist new connections properly diff -r 77fa82dcc956 -r 8905b45cf9fb RemoteViewer/Connection.py --- a/RemoteViewer/Connection.py Sat Sep 20 03:12:26 2014 +0200 +++ b/RemoteViewer/Connection.py Sat Sep 20 04:46:59 2014 +0200 @@ -1,5 +1,16 @@ + +KEYS = ['geometry', 'host', 'title', 'type', 'user'] class Connection(object): + @staticmethod + def parse(json): + connection = Connection() + for key in KEYS: + if key in json: + value = json[key] + setattr(connection, key, value) + return connection + def __init__(self): self.geometry = None self.host = None @@ -26,5 +37,18 @@ '''This method is connected to a slot in the GUI''' self.geometry = geometry + def displayString(self): + if self.title is not None: + return self.title + else: + return self.host + def validate(self): pass + + def toJson(self): + json = {} + for key in KEYS: + if hasattr(self, key): + json[key] = getattr(self, key) + return json diff -r 77fa82dcc956 -r 8905b45cf9fb RemoteViewer/ConnectionDialog.py --- a/RemoteViewer/ConnectionDialog.py Sat Sep 20 03:12:26 2014 +0200 +++ b/RemoteViewer/ConnectionDialog.py Sat Sep 20 04:46:59 2014 +0200 @@ -25,5 +25,4 @@ self.ui.geometry.textChanged.connect(self.connection.setGeometry) def accept(self): - print(self.connection) super(ConnectionDialog, self).accept() diff -r 77fa82dcc956 -r 8905b45cf9fb RemoteViewer/RemoteViewer.py --- a/RemoteViewer/RemoteViewer.py Sat Sep 20 03:12:26 2014 +0200 +++ b/RemoteViewer/RemoteViewer.py Sat Sep 20 04:46:59 2014 +0200 @@ -1,5 +1,6 @@ from PyQt4.QtGui import QMainWindow from PyQtLib.ExceptionSafeSlot import ExceptionSafeSlot +from PyQtLib.GenericListModel import GenericListModel from .ConnectionDialog import ConnectionDialog from .Settings import Settings @@ -9,14 +10,26 @@ def __init__(self): super(RemoteViewer, self).__init__() self.settings = Settings() + self.__initUi() + + def __initUi(self): self.ui = Ui_RemoteViewer() self.ui.setupUi(self) + self.__initHostList() + + def __initHostList(self): + connections = self.settings.allConnections() + model = GenericListModel(self, connections, 'displayString') + self.ui.hostList.setModel(model) @ExceptionSafeSlot() def newConnection(self): + # TODO: make the window title a ctor argument dialog = ConnectionDialog() dialog.setWindowTitle('New Connection') dialog.exec_() + connection = dialog.connection + self.settings.addConnection(connection) def close(self): self.settings.persist() diff -r 77fa82dcc956 -r 8905b45cf9fb RemoteViewer/RemoteViewer.ui --- a/RemoteViewer/RemoteViewer.ui Sat Sep 20 03:12:26 2014 +0200 +++ b/RemoteViewer/RemoteViewer.ui Sat Sep 20 04:46:59 2014 +0200 @@ -16,7 +16,7 @@ - + @@ -31,7 +31,7 @@ - File + Connection diff -r 77fa82dcc956 -r 8905b45cf9fb RemoteViewer/Settings.py --- a/RemoteViewer/Settings.py Sat Sep 20 03:12:26 2014 +0200 +++ b/RemoteViewer/Settings.py Sat Sep 20 04:46:59 2014 +0200 @@ -1,15 +1,28 @@ +from PyQtLib.AbstractSettings import AbstractSettings -from os.path import expanduser -from configparser import ConfigParser +from .Connection import Connection -class Settings(object): +CONNECTIONS_KEY = 'connections' + +class Settings(AbstractSettings): def __init__(self): - self.parser = ConfigParser() - self.parser.read(self.__filename()) + super(Settings, self).__init__('~/.remoteviewer') + + def allConnections(self): + connections = [] + rawConnections = self.__connections() + for connectionJson in rawConnections: + connection = Connection.parse(connectionJson) + connections.append(connection) + return connections - def __filename(self): - return expanduser('~/.remoteviewer') + def __connections(self): + if CONNECTIONS_KEY in self.config: + connections = self.config[CONNECTIONS_KEY] + if connections is not None: + return connections + return [] - def persist(self): - with open(self.__filename(), 'w') as rcfile: - self.parser.write(rcfile) + def addConnection(self, connection): + json = connection.toJson() + self.config[CONNECTIONS_KEY].append(json)