changeset 4:8905b45cf9fb

load the settings file when starting, persist new connections properly
author Dirk Olmes <dirk@xanthippe.ping.de>
date Sat, 20 Sep 2014 04:46:59 +0200
parents 77fa82dcc956
children f48a0a320815
files RemoteViewer/Connection.py RemoteViewer/ConnectionDialog.py RemoteViewer/RemoteViewer.py RemoteViewer/RemoteViewer.ui RemoteViewer/Settings.py
diffstat 5 files changed, 62 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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()
--- 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()
--- 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 @@
   <widget class="QWidget" name="centralwidget">
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
-     <widget class="QListView" name="listView"/>
+     <widget class="QListView" name="hostList"/>
     </item>
    </layout>
   </widget>
@@ -31,7 +31,7 @@
    </property>
    <widget class="QMenu" name="menuFile">
     <property name="title">
-     <string>File</string>
+     <string>Connection</string>
     </property>
     <addaction name="actionNew"/>
     <addaction name="separator"/>
--- 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)