view RemoteViewer/Connection.py @ 29:56b2e8f9846e

introduce an object to represent a connection's type. Use this type object to determine if the password checkbox should be hidden in the connection dialog
author Dirk Olmes <dirk@xanthippe.ping.de>
date Fri, 03 Oct 2014 04:25:56 +0200
parents fb7442228526
children daf6c811d104
line wrap: on
line source

from uuid import uuid4
from .Viewer import VncViewer, FreeRdp

KEYS = ['geometry', 'id', 'host', 'lowColor', 'promptForPassword', '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]
                if key == 'type':
                    connection.type = Type.get(value)
                else:
                    setattr(connection, key, value)
        return connection

    def __init__(self):
        self.geometry = None
        self.host = None
        self.id = str(uuid4())
        self.lowColor = False
        self.promptForPassword = False
        self.title = None
        self.type = VncType()
        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

    def setPromptForPassword(self, flag):
        """This is a slot that is connected to a signal in the GUI"""
        self.promptForPassword = flag

    @filter_string
    def setGeometry(self, geometry):
        """This is a slot that is connected to a signal in the GUI"""
        self.geometry = geometry

    def setLowColor(self, lowColor):
        """This is a slot that is connected to a signal in the GUI"""
        self.lowColor = lowColor

    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:
                    if key == 'type':
                        # just serialize the 'name' attribute of the Type object
                        value = value.name
                    json[key] = value
        return json

    def open(self, parent):
        self.type.open(self, parent)


class Type(object):
    @staticmethod
    def all():
        return [RdpType(), VncType()]

    @staticmethod
    def get(type):
        if type == 'rdp':
            return RdpType()
        elif type == 'vnc':
            return VncType()
        else:
            raise ValueError('invalid type: ' + type)

    def __init__(self, name):
        self.name = name
        self.supportsPassword = False

    def __repr__(self):
        return self.name

    def __eq__(self, other):
        return self.name == other.name


class RdpType(Type):
    def __init__(self):
        super(RdpType, self).__init__('rdp')
        self.supportsPassword = True

    def open(self, connection, parent):
        FreeRdp(connection).open(parent)


class VncType(Type):
    def __init__(self):
        super(VncType, self).__init__('vnc')

    def open(self, connection, parent):
        VncViewer(connection).open(parent)