view RemoteViewer/Connection.py @ 16:6a62de088c00

implement editing existing connections
author Dirk Olmes <dirk@xanthippe.ping.de>
date Wed, 24 Sep 2014 03:51:33 +0200
parents d8958cb11043
children 3190c60927e5
line wrap: on
line source

from uuid import uuid4
from .Viewer import VncViewer, FreeRdp

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
    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.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, 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

    def setType(self, type):
        '''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

    @filter_string
    def setGeometry(self, geometry):
        '''This is a slot that is connected to a signal 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):
        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):
                value = getattr(self,  key)
                if value is not None:
                    json[key] = value
        return json

    def open(self, parent):
        if self.type == 'vnc':
            VncViewer(self).open(parent)
        elif self.type == 'rdp':
            FreeRdp(self).open(parent)
        else:
            raise ValueError('unknown type: ' + self.type)