Example #1
0
    def __init__(self, item, treeController, variable, rootEntry, editOverCreate):
        """Constructor of VariableCreationController:
                Called by a click on the tree view of all variables.

                @type item: Gtk.Menuitem
                @param item: the menu item that calls this constructor.
                @type treeController: VariableTreeController
                @param treeController: the controller of the tree view which cause this controller's view to appear.
                @type variable: netzob.Common.MMSTD.Dictionary.Variables.AbstractVariable
                @param variable: the variable that will be modified/to which we will add a child.
                @type rootEntry: Gtk.treerow
                @param rootEntry: the root entry associates to the variable in the treestore.
                @type editOverCreate: boolean
                @param editOverCreate: True if we want to edit the selected variable, false if we want to create a new variable.
        """
        self.treeController = treeController
        self.netzob = self.treeController.netzob
        self.variable = variable
        self.rootEntry = rootEntry
        self.editOverCreate = editOverCreate
        self.view = VariableCreationView(self)
        self.initCallBacks()
        self.runDialog()
Example #2
0
    def __init__(self, item, treeController, variable, rootEntry, editOverCreate):
        """Constructor of DataVariable:
                Called by a click on the tree view of all variables.

                @type item: Gtk.Menuitem
                @param item: the menu item that calls this constructor.
                @type treeController: VariableTreeController
                @param treeController: the controller of the tree view which cause this controller's view to appear.
                @type variable: netzob.Common.MMSTD.Dictionary.Variables.AbstractVariable
                @param variable: the variable that will be modified/to which we will add a child.
                @type rootEntry: Gtk.treerow
                @param rootEntry: the root entry associates to the variable in the treestore.
                @type editOverCreate: boolean
                @param editOverCreate: True if we want to edit the selected variable, false if we want to create a new variable.
        """
        self.treeController = treeController
        self.netzob = self.treeController.netzob
        self.variable = variable
        self.rootEntry = rootEntry
        self.editOverCreate = editOverCreate
        self.view = VariableCreationView(self)
        self.initCallBacks()
        self.runDialog()
Example #3
0
class VariableCreationController(object):
    """VariableCreationController:
            Manage a view that allows the user to modify/create a variable by specifying each of its field.
    """

    def __init__(self, item, treeController, variable, rootEntry, editOverCreate):
        """Constructor of DataVariable:
                Called by a click on the tree view of all variables.

                @type item: Gtk.Menuitem
                @param item: the menu item that calls this constructor.
                @type treeController: VariableTreeController
                @param treeController: the controller of the tree view which cause this controller's view to appear.
                @type variable: netzob.Common.MMSTD.Dictionary.Variables.AbstractVariable
                @param variable: the variable that will be modified/to which we will add a child.
                @type rootEntry: Gtk.treerow
                @param rootEntry: the root entry associates to the variable in the treestore.
                @type editOverCreate: boolean
                @param editOverCreate: True if we want to edit the selected variable, false if we want to create a new variable.
        """
        self.treeController = treeController
        self.netzob = self.treeController.netzob
        self.variable = variable
        self.rootEntry = rootEntry
        self.editOverCreate = editOverCreate
        self.view = VariableCreationView(self)
        self.initCallBacks()
        self.runDialog()

    def initCallBacks(self):
        """initCallbacks:
                Init the callbacks.
        """
        self.view.getWidg("variableTypeCombo").connect('changed', self.updateOptions)
        self.view.getWidg("minSpin").connect('changed', self.updateMaxSpin)
        self.view.getWidg("applyButton").connect("clicked", self.validateChanges)
        self.view.getWidg("IDButton").connect("clicked", self.chooseSelectedVariable)
        self.view.getWidg("sizedCheck").connect('toggled', self.updateDelimiterAndSize)

    def updateOptions(self, widget=None):
        """updateOptions:
                Update the shown options of the variable creation view to set visible only those which are useful.

                @type widget: Gtk.widget
                @param widget: the widget which modification calls this function.
        """
        strVarType = self.view.getWidg("variableTypeCombo").get_active_text()
        handler_id = None

        # Node variable
        if strVarType == AggregateVariable.TYPE or strVarType == AlternateVariable.TYPE:
            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(False)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(False)

            self.updateDelimiterAndSize(default=True)

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

        # Data variable
        elif strVarType == DataVariable.TYPE:
            self.view.getWidg("valueLabel").set_text("Original Value")
            self.view.getWidg("minLabel").set_text("Minimum number of characters")
            self.view.getWidg("maxLabel").set_text("Maximum number of characters")

            self.view.getWidg("valueLabel").set_visible(True)
            self.view.getWidg("typeLabel").set_visible(True)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(True)

            self.view.getWidg("valueEntry").set_visible(True)
            self.view.getWidg("typeCombo").set_visible(True)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(True)

            self.updateDelimiterAndSize()

            handler_id = self.view.getWidg("valueEntry").connect('changed', self.updateMinSpin)

        # Repeat variable
        elif strVarType == RepeatVariable.TYPE:
            self.view.getWidg("minLabel").set_text("Minimum number of iterations")
            self.view.getWidg("maxLabel").set_text("Maximum number of iterations")

            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(False)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(False)

            # Min/Max/Delimiter.
            self.view.getWidg("minLabel").set_visible(True)
            self.view.getWidg("maxLabel").set_visible(True)
            self.view.getWidg("delimiterLabel").set_visible(False)

            self.view.getWidg("minSpin").set_visible(True)
            self.view.getWidg("maxSpin").set_visible(True)
            self.view.getWidg("delimiterEntry").set_visible(False)

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

#===============================================================================
#        # Direct Relation variable
#        elif strVarType == DirectRelationVariable.TYPE:
#
#            self.view.getWidg("valueLabel").set_visible(False)
#            self.view.getWidg("typeLabel").set_visible(False)
#            self.view.getWidg("relationTypeLabel").set_visible(False)
#            self.view.getWidg("sizedLabel").set_visible(False)
#
#            self.view.getWidg("valueEntry").set_visible(False)
#            self.view.getWidg("typeCombo").set_visible(False)
#
#            self.view.getWidg("relationTypeCombo").set_visible(False)
#            self.view.getWidg("IDGrid").set_visible(True)
#            self.view.getWidg("IDLabel").set_visible(True)
#            self.view.getWidg("IDEntry").set_visible(True)
#            self.view.getWidg("IDButton").set_visible(True)
#            self.view.getWidg("sizedCheck").set_visible(False)
#
#            self.updateDelimiterAndSize(default=True)
#
#            if handler_id is not None:
#                object.disconnect(handler_id)
#                handler_id = None
#===============================================================================

        # Computed Relation variable
        elif strVarType == ComputedRelationVariable.TYPE:
            self.view.getWidg("minLabel").set_text("Minimum number of characters")
            self.view.getWidg("maxLabel").set_text("Maximum number of characters")

            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(True)
            self.view.getWidg("sizedLabel").set_visible(True)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(True)
            self.view.getWidg("IDGrid").set_visible(True)
            self.view.getWidg("IDLabel").set_visible(True)
            self.view.getWidg("IDEntry").set_visible(True)
            self.view.getWidg("IDButton").set_visible(True)
            self.view.getWidg("sizedCheck").set_visible(True)

            self.updateDelimiterAndSize()

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

        # Default case
        else:
            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(False)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(False)

            self.updateDelimiterAndSize(default=True)

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

    def updateDelimiterAndSize(self, widget=None, default=False):
        """updateDelimiterAndSize:
                Update the shown options (among min/max/deilmiter) of the variable creation view to set visible only those which are useful.
                For DataVariable and ComputedRelationVariable.
                Called by a toggling of the sizedCheck checkbox.

                @type widget: Gtk.widget
                @param widget: the widget which modification calls this function.
        """
        if self.view.getWidg("sizedCheck").get_active():
            self.view.getWidg("minLabel").set_visible(True)
            self.view.getWidg("maxLabel").set_visible(True)
            self.view.getWidg("delimiterLabel").set_visible(False)

            self.view.getWidg("minSpin").set_visible(True)
            self.view.getWidg("maxSpin").set_visible(True)
            self.view.getWidg("delimiterEntry").set_visible(False)
        else:
            self.view.getWidg("minLabel").set_visible(False)
            self.view.getWidg("maxLabel").set_visible(False)
            self.view.getWidg("delimiterLabel").set_visible(True)

            self.view.getWidg("minSpin").set_visible(False)
            self.view.getWidg("maxSpin").set_visible(False)
            self.view.getWidg("delimiterEntry").set_visible(True)

        if default:
            self.view.getWidg("minLabel").set_visible(False)
            self.view.getWidg("maxLabel").set_visible(False)
            self.view.getWidg("delimiterLabel").set_visible(False)

            self.view.getWidg("minSpin").set_visible(False)
            self.view.getWidg("maxSpin").set_visible(False)
            self.view.getWidg("delimiterEntry").set_visible(False)

    def updateMinSpin(self, widget):
        """updateMinSpin:
                Fix the value of minspin to len(value) in order to help the user to know the size of the value he entered.
        """
        strVarType = self.view.getWidg("variableTypeCombo").get_active_text()
        if strVarType == DataVariable.TYPE:
            size = len(str(self.view.getWidg("valueEntry").get_text()))
            # Protect previously defined minValue.
            # if self.view.getWidg("minSpin").get_text() is None or str(self.view.getWidg("minSpin").get_text()) == '' or size < int(self.view.getWidg("minSpin").get_text()):
            self.view.getWidg("minSpin").set_text(str(size))

    def updateMaxSpin(self, widget):
        """updateMaxSpin:
                Fix the value of maxSpin to at least minSpin.
                Called when minSpin is modified to a greater value than maxspin.
        """
        if self.view.getWidg("minSpin").get_text() is None or str(self.view.getWidg("minSpin").get_text()) == '':
            minSpin = 0
        else:
            minSpin = int(self.view.getWidg("minSpin").get_text())
        if self.view.getWidg("maxSpin").get_text() is None or str(self.view.getWidg("maxSpin").get_text()) == '':
            maxSpin = 0
        else:
            maxSpin = int(self.view.getWidg("maxSpin").get_text())
        if minSpin > maxSpin:
            self.view.getWidg("maxSpin").set_text(self.view.getWidg("minSpin").get_text())

    def runDialog(self):
        """runDialog:
                Run a dialog that allows to modify each definition field of a variable.
        """
        dialog = self.view.getWidg("dialog")
        dialog.show_all()

        if self.editOverCreate:
            # We writes the former values of the variable.
            self.view.getWidg("nameEntry").set_text(self.variable.getName())
            self.view.getWidg("mutableCheck").set_active(self.variable.isMutable())
            self.view.getWidg("learnableCheck").set_active(self.variable.isLearnable())
            self.setComboText(self.view.getWidg("variableTypeCombo"), self.variable.getVariableType())

            # Data Variable
            if self.variable.getVariableType() == DataVariable.TYPE:
                if self.variable.getOriginalValue() is not None:
                    self.view.getWidg("valueEntry").set_text(self.variable.bin2str(self.variable.getOriginalValue()))
                else:
                    self.view.getWidg("valueEntry").set_text('')

                if self.variable.type.isSized():
                    self.view.getWidg("sizedCheck").set_active(True)
                    self.view.getWidg("minSpin").set_text(str(self.variable.getType().getMinChars()))
                    self.view.getWidg("maxSpin").set_text(str(self.variable.getType().getMaxChars()))
                    self.view.getWidg("delimiterEntry").set_text('')
                else:
                    self.view.getWidg("sizedCheck").set_active(False)
                    self.view.getWidg("minSpin").set_text('0')
                    self.view.getWidg("maxSpin").set_text('0')
                    self.view.getWidg("delimiterEntry").set_text(self.variable.bin2str(self.variable.getDelimiter()))

                self.setComboText(self.view.getWidg("typeCombo"), self.variable.getType().getType())

            # Repeat Variable
            elif self.variable.getVariableType() == RepeatVariable.TYPE:
                self.view.getWidg("minSpin").set_text(str(self.variable.getNumberIterations()[0]))
                self.view.getWidg("maxSpin").set_text(str(self.variable.getNumberIterations()[1]))

            #===================================================================
            # # Direct Relation Variable
            # elif self.variable.getVariableType() == DirectRelationVariable.TYPE:
            #    self.view.getWidg("IDEntry").set_text(self.variable.getPointedVariable().getID())
            #===================================================================

            # Computed Relation Variable
            elif self.variable.getVariableType() == ComputedRelationVariable.TYPE:
                self.view.getWidg("IDEntry").set_text(self.variable.getPointedVariable().getID())
                self.setComboText(self.view.getWidg("relationTypeCombo"), self.variable.getType().getType())

                if self.variable.type.isSized():
                    self.view.getWidg("sizedCheck").set_active(True)
                    self.view.getWidg("minSpin").set_text(str(self.variable.getType().getMinChars()))
                    self.view.getWidg("maxSpin").set_text(str(self.variable.getType().getMaxChars()))
                    self.view.getWidg("delimiterEntry").set_text('')
                else:
                    self.view.getWidg("sizedCheck").set_active(False)
                    self.view.getWidg("minSpin").set_text('0')
                    self.view.getWidg("maxSpin").set_text('0')
                    self.view.getWidg("delimiterEntry").set_text(self.variable.bin2str(self.variable.getDelimiter()))

        self.updateOptions()
        dialog.run()

    def validateChanges(self, widget):
        """validateChanges:
                Validate the changes that a user has done on a variable.
                Called by a click on the apply button.

                @type widget: Gtk.widget
                @param widget: the widget which calls this function.
        """
        dialog = self.view.getWidg("dialog")
        if self.editOverCreate:
            anid = self.variable.getID()
        else:
            anid = str(uuid.uuid4())

        name = self.view.getWidg("nameEntry").get_text()
        mutable = self.view.getWidg("mutableCheck").get_active()
        learnable = self.view.getWidg("learnableCheck").get_active()
        strVarType = self.view.getWidg("variableTypeCombo").get_active_text()

        variable = None
        # Aggregate variable
        if strVarType == AggregateVariable.TYPE:
            variable = AggregateVariable(anid, name, mutable, learnable)

        # Alternate Variable
        elif strVarType == AlternateVariable.TYPE:
            variable = AlternateVariable(anid, name, mutable, learnable)

        # Repeat Variable
        elif strVarType == RepeatVariable.TYPE:
            minIterations = int(self.view.getWidg("minSpin").get_text())
            maxIterations = int(self.view.getWidg("maxSpin").get_text())
            variable = RepeatVariable(anid, name, mutable, learnable, None, minIterations, maxIterations)

        # Data Variable
        elif strVarType == DataVariable.TYPE:
            originalValue = str(self.view.getWidg("valueEntry").get_text())
            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()
            vtype = AbstractType.makeType(self.view.getWidg("typeCombo").get_active_text(), sized, minChars, maxChars, delimiter)
            variable = DataVariable(anid, name, mutable, learnable, vtype, originalValue)

#===============================================================================
#        # Direct Relation Variable
#        elif strVarType == DirectRelationVariable.TYPE:
#
#            # We find the variable by its ID.
#            pointedID = str(self.view.getWidg("IDEntry").get_text())
#
#            variable = DirectRelationVariable(anid, name, mutable, learnable, pointedID, self.treeController.symbol)
#===============================================================================

        # Computed Relation Variable
        elif strVarType == ComputedRelationVariable.TYPE:

            # We find the variable by its ID.
            pointedID = str(self.view.getWidg("IDEntry").get_text())

            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()
            vtype = AbstractRelationType.makeType(self.view.getWidg("relationTypeCombo").get_active_text(), sized, minChars, maxChars, delimiter)
            variable = ComputedRelationVariable(anid, name, mutable, learnable, vtype, pointedID, self.treeController.symbol)

        if variable is not None:
            # This part is for saving and transfering children when transforming a node variable into an other kind of node variable.
            if self.editOverCreate:
                father = self.variable.getFathers()[0]
                # We transform a node variable into a node variable.
                if (self.variable.getVariableType() == AggregateVariable.TYPE or self.variable.getVariableType() == AlternateVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    children = self.variable.getChildren()
                    self.variable = variable
                    for child in children:
                        self.variable.addChild(child)

                # We transform a repeat variable into a node variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.addChild(child)

                # We transform a repeat variable into a repeat variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == RepeatVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.addChild(child)

                # We do not manage/save children.
                else:
                    self.variable = variable
                self.variable.addFather(father)
                self.treeController.editVariable(self.variable)

            else:
                self.variable.addChild(variable)
                self.treeController.registerVariable(self.rootEntry, variable)
        dialog.destroy()

    def setComboText(self, combo, text):
        """setComboText:
                Fix the displayed value of a combo box to the item which value is "text".
                This value must be one of the possible values of the combobox.

                @type combo: Gtk.Combobox
                @param combo: the combobox which displayed value we want to change.
                @type text: string
                @param text: the string value we want that the combobox displays.
        """
        model = combo.get_model()
        aniter = model.get_iter_first()
        while True:
            if str(model[aniter][0]) == text:
                combo.set_active_iter(aniter)
                break
            if model.iter_next(aniter) is not None:
                aniter = model.iter_next(aniter)
            else:
                break

    def chooseSelectedVariable(self, widget):
        """chooseSelectedVariable:
                Manage the creation and showing of a VariableIDTreeController that allows the user to choose one variable and get its ID.

                @param widget: the widget which is connected to this function through the event event.
        """
        VariableIDTreeController(self)
Example #4
0
class VariableCreationController(object):
    """VariableCreationController:
            Manage a view that allows the user to modify/create a variable by specifying each of its field.
    """

    def __init__(self, item, treeController, variable, rootEntry, editOverCreate):
        """Constructor of VariableCreationController:
                Called by a click on the tree view of all variables.

                @type item: Gtk.Menuitem
                @param item: the menu item that calls this constructor.
                @type treeController: VariableTreeController
                @param treeController: the controller of the tree view which cause this controller's view to appear.
                @type variable: netzob.Common.MMSTD.Dictionary.Variables.AbstractVariable
                @param variable: the variable that will be modified/to which we will add a child.
                @type rootEntry: Gtk.treerow
                @param rootEntry: the root entry associates to the variable in the treestore.
                @type editOverCreate: boolean
                @param editOverCreate: True if we want to edit the selected variable, false if we want to create a new variable.
        """
        self.treeController = treeController
        self.netzob = self.treeController.netzob
        self.variable = variable
        self.rootEntry = rootEntry
        self.editOverCreate = editOverCreate
        self.view = VariableCreationView(self)
        self.initCallBacks()
        self.runDialog()

    def initCallBacks(self):
        """initCallbacks:
                Init the callbacks.
        """
        self.view.getWidg("variableTypeCombo").connect('changed', self.updateOptions)
        self.view.getWidg("minSpin").connect('changed', self.updateMaxSpin)
        self.view.getWidg("applyButton").connect("clicked", self.validateChanges)
        self.view.getWidg("IDButton").connect("clicked", self.chooseSelectedVariable)
        self.view.getWidg("sizedCheck").connect('toggled', self.updateDelimiterAndSize)

    def updateOptions(self, widget=None):
        """updateOptions:
                Update the shown options of the variable creation view to set visible only those which are useful.

                @type widget: Gtk.widget
                @param widget: the widget which modification calls this function.
        """
        varTypeIndex = self.view.getWidg("variableTypeCombo").get_active()
        handler_id = None

        # Aggregate or alternate variable
        if varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AggregateVariable.TYPE) or varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AlternateVariable.TYPE):
            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(False)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(False)

            self.view.getWidg("factorLabel").set_visible(False)
            self.view.getWidg("factorEntry").set_visible(False)
            self.view.getWidg("offsetLabel").set_visible(False)
            self.view.getWidg("offsetEntry").set_visible(False)

            self.updateDelimiterAndSize(default=True)

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

        # Computed Relation variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(ComputedRelationVariable.TYPE):
            self.view.getWidg("minLabel").set_text("Minimum number of characters")
            self.view.getWidg("maxLabel").set_text("Maximum number of characters")

            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(True)
            self.view.getWidg("sizedLabel").set_visible(True)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(True)
            self.view.getWidg("IDGrid").set_visible(True)
            self.view.getWidg("IDLabel").set_visible(True)
            self.view.getWidg("IDEntry").set_visible(True)
            self.view.getWidg("IDButton").set_visible(True)
            self.view.getWidg("sizedCheck").set_visible(True)

            self.view.getWidg("factorLabel").set_visible(True)
            self.view.getWidg("factorEntry").set_visible(True)
            self.view.getWidg("offsetLabel").set_visible(True)
            self.view.getWidg("offsetEntry").set_visible(True)

            self.updateDelimiterAndSize()

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

        # Data variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(DataVariable.TYPE):
            self.view.getWidg("valueLabel").set_text("Original Value")
            self.view.getWidg("minLabel").set_text("Minimum number of characters")
            self.view.getWidg("maxLabel").set_text("Maximum number of characters")

            self.view.getWidg("valueLabel").set_visible(True)
            self.view.getWidg("typeLabel").set_visible(True)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(True)

            self.view.getWidg("valueEntry").set_visible(True)
            self.view.getWidg("typeCombo").set_visible(True)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(True)

            self.view.getWidg("factorLabel").set_visible(False)
            self.view.getWidg("factorEntry").set_visible(False)
            self.view.getWidg("offsetLabel").set_visible(False)
            self.view.getWidg("offsetEntry").set_visible(False)

            self.updateDelimiterAndSize()

            handler_id = self.view.getWidg("valueEntry").connect('changed', self.updateMinSpin)

        # Repeat variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(RepeatVariable.TYPE):
            self.view.getWidg("minLabel").set_text("Minimum number of iterations")
            self.view.getWidg("maxLabel").set_text("Maximum number of iterations")

            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(False)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(False)

            # Min/Max/Delimiter.
            self.view.getWidg("minLabel").set_visible(True)
            self.view.getWidg("maxLabel").set_visible(True)
            self.view.getWidg("delimiterLabel").set_visible(False)

            self.view.getWidg("minSpin").set_visible(True)
            self.view.getWidg("maxSpin").set_visible(True)
            self.view.getWidg("delimiterEntry").set_visible(False)

            self.view.getWidg("factorLabel").set_visible(False)
            self.view.getWidg("factorEntry").set_visible(False)
            self.view.getWidg("offsetLabel").set_visible(False)
            self.view.getWidg("offsetEntry").set_visible(False)

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

#===============================================================================
#        # Direct Relation variable
#        elif strVarType == DirectRelationVariable.TYPE:
#
#            self.view.getWidg("valueLabel").set_visible(False)
#            self.view.getWidg("typeLabel").set_visible(False)
#            self.view.getWidg("relationTypeLabel").set_visible(False)
#            self.view.getWidg("sizedLabel").set_visible(False)
#
#            self.view.getWidg("valueEntry").set_visible(False)
#            self.view.getWidg("typeCombo").set_visible(False)
#
#            self.view.getWidg("relationTypeCombo").set_visible(False)
#            self.view.getWidg("IDGrid").set_visible(True)
#            self.view.getWidg("IDLabel").set_visible(True)
#            self.view.getWidg("IDEntry").set_visible(True)
#            self.view.getWidg("IDButton").set_visible(True)
#            self.view.getWidg("sizedCheck").set_visible(False)
#
#            self.updateDelimiterAndSize(default=True)
#
#            if handler_id is not None:
#                object.disconnect(handler_id)
#                handler_id = None
#===============================================================================

        # Default case
        else:
            self.view.getWidg("valueLabel").set_visible(False)
            self.view.getWidg("typeLabel").set_visible(False)
            self.view.getWidg("relationTypeLabel").set_visible(False)
            self.view.getWidg("sizedLabel").set_visible(False)

            self.view.getWidg("valueEntry").set_visible(False)
            self.view.getWidg("typeCombo").set_visible(False)

            self.view.getWidg("relationTypeCombo").set_visible(False)
            self.view.getWidg("IDGrid").set_visible(False)
            self.view.getWidg("IDLabel").set_visible(False)
            self.view.getWidg("IDEntry").set_visible(False)
            self.view.getWidg("IDButton").set_visible(False)
            self.view.getWidg("sizedCheck").set_visible(False)

            self.view.getWidg("factorLabel").set_visible(False)
            self.view.getWidg("factorEntry").set_visible(False)
            self.view.getWidg("offsetLabel").set_visible(False)
            self.view.getWidg("offsetEntry").set_visible(False)

            self.updateDelimiterAndSize(default=True)

            if handler_id is not None:
                object.disconnect(handler_id)
                handler_id = None

    def updateDelimiterAndSize(self, widget=None, default=False):
        """updateDelimiterAndSize:
                Update the shown options (among min/max/deilmiter) of the variable creation view to set visible only those which are useful.
                For DataVariable and ComputedRelationVariable.
                Called by a toggling of the sizedCheck checkbox.

                @type widget: Gtk.widget
                @param widget: the widget which modification calls this function.
        """
        if self.view.getWidg("sizedCheck").get_active():
            self.view.getWidg("minLabel").set_visible(True)
            self.view.getWidg("maxLabel").set_visible(True)
            self.view.getWidg("delimiterLabel").set_visible(False)

            self.view.getWidg("minSpin").set_visible(True)
            self.view.getWidg("maxSpin").set_visible(True)
            self.view.getWidg("delimiterEntry").set_visible(False)
        else:
            self.view.getWidg("minLabel").set_visible(False)
            self.view.getWidg("maxLabel").set_visible(False)
            self.view.getWidg("delimiterLabel").set_visible(True)

            self.view.getWidg("minSpin").set_visible(False)
            self.view.getWidg("maxSpin").set_visible(False)
            self.view.getWidg("delimiterEntry").set_visible(True)

        if default:
            self.view.getWidg("minLabel").set_visible(False)
            self.view.getWidg("maxLabel").set_visible(False)
            self.view.getWidg("delimiterLabel").set_visible(False)

            self.view.getWidg("minSpin").set_visible(False)
            self.view.getWidg("maxSpin").set_visible(False)
            self.view.getWidg("delimiterEntry").set_visible(False)

    def updateMinSpin(self, widget):
        """updateMinSpin:
                Fix the value of minspin to len(value) in order to help the user to know the size of the value he entered.
        """
        size = len(str(self.view.getWidg("valueEntry").get_text()))
        # Protect previously defined minValue.
        # if self.view.getWidg("minSpin").get_text() is None or str(self.view.getWidg("minSpin").get_text()) == '' or size < int(self.view.getWidg("minSpin").get_text()):
        self.view.getWidg("minSpin").set_text(str(size))

    def updateMaxSpin(self, widget):
        """updateMaxSpin:
                Fix the value of maxSpin to at least minSpin.
                Called when minSpin is modified to a greater value than maxspin.
        """
        if self.view.getWidg("minSpin").get_text() is None or str(self.view.getWidg("minSpin").get_text()) == '':
            minSpin = 0
        else:
            minSpin = int(self.view.getWidg("minSpin").get_text())
        if self.view.getWidg("maxSpin").get_text() is None or str(self.view.getWidg("maxSpin").get_text()) == '':
            maxSpin = 0
        else:
            maxSpin = int(self.view.getWidg("maxSpin").get_text())
        if minSpin > maxSpin:
            self.view.getWidg("maxSpin").set_text(self.view.getWidg("minSpin").get_text())

    def runDialog(self):
        """runDialog:
                Run a dialog that allows to modify each definition field of a variable.
        """
        dialog = self.view.getWidg("dialog")
        dialog.show_all()

        # Edition of an existing variable.
        if self.editOverCreate:
            # We write the former values of the variable.
            self.view.getWidg("nameEntry").set_text(self.variable.getName())
            self.view.getWidg("mutableCheck").set_active(self.variable.isMutable())
            self.view.getWidg("learnableCheck").set_active(self.variable.isLearnable())
            self.view.getWidg("variableTypeCombo").set_active(VariableTreeController.VARIABLE_INDEX_LIST.index(self.variable.getVariableType()))

            # Data Variable
            if self.variable.getVariableType() == DataVariable.TYPE:
                if self.variable.getOriginalValue() is not None:
                    self.view.getWidg("valueEntry").set_text(self.variable.bin2str(self.variable.getOriginalValue()))
                else:
                    self.view.getWidg("valueEntry").set_text('')

                if self.variable.type.isSized():
                    self.view.getWidg("sizedCheck").set_active(True)
                    self.view.getWidg("minSpin").set_text(str(self.variable.getType().getMinChars()))
                    self.view.getWidg("maxSpin").set_text(str(self.variable.getType().getMaxChars()))
                    self.view.getWidg("delimiterEntry").set_text('')
                else:
                    self.view.getWidg("sizedCheck").set_active(False)
                    self.view.getWidg("minSpin").set_text('0')
                    self.view.getWidg("maxSpin").set_text('0')
                    self.view.getWidg("delimiterEntry").set_text(self.variable.bin2str(self.variable.getDelimiter()))

                self.view.getWidg("typeCombo").set_active(VariableTreeController.TYPE_INDEX_LIST.index(self.variable.getType().getType()))

            # Repeat Variable
            elif self.variable.getVariableType() == RepeatVariable.TYPE:
                self.view.getWidg("minSpin").set_text(str(self.variable.getNumberIterations()[0]))
                self.view.getWidg("maxSpin").set_text(str(self.variable.getNumberIterations()[1]))

            #===================================================================
            # # Direct Relation Variable
            # elif self.variable.getVariableType() == DirectRelationVariable.TYPE:
            #    self.view.getWidg("IDEntry").set_text(self.variable.getPointedVariable().getID())
            #===================================================================

            # Computed Relation Variable
            elif self.variable.getVariableType() == ComputedRelationVariable.TYPE:
                self.view.getWidg("IDEntry").set_text(self.variable.getPointedID())
                self.view.getWidg("relationTypeCombo").set_active(VariableTreeController.RELATION_TYPE_INDEX_LIST.index(self.variable.getRelationType().getType()))

                if self.variable.getRelationType().getAssociatedDataType().isSized():
                    self.view.getWidg("sizedCheck").set_active(True)
                    self.view.getWidg("minSpin").set_text(str(self.variable.getRelationType().getAssociatedDataType().getMinChars()))
                    self.view.getWidg("maxSpin").set_text(str(self.variable.getRelationType().getAssociatedDataType().getMaxChars()))
                    self.view.getWidg("delimiterEntry").set_text('')
                else:
                    self.view.getWidg("sizedCheck").set_active(False)
                    self.view.getWidg("minSpin").set_text('0')
                    self.view.getWidg("maxSpin").set_text('0')
                    self.view.getWidg("delimiterEntry").set_text(self.variable.bin2str(self.variable.getRelationType().getAssociatedDataType().getDelimiter()))

                self.view.getWidg("factorEntry").set_text(str(self.variable.getRelationType().getFactor()))
                self.view.getWidg("offsetEntry").set_text(str(self.variable.getRelationType().getOffset()))

        self.updateOptions()
        dialog.run()

    def validateChanges(self, widget):
        """validateChanges:
                Validate the changes that a user has done on a variable.
                Called by a click on the apply button.

                @type widget: Gtk.widget
                @param widget: the widget which calls this function.
        """

        dialog = self.view.getWidg("dialog")
        if self.editOverCreate:
            anid = self.variable.getID()
        else:
            anid = str(uuid.uuid4())

        name = self.view.getWidg("nameEntry").get_text()
        mutable = self.view.getWidg("mutableCheck").get_active()
        learnable = self.view.getWidg("learnableCheck").get_active()
        varTypeIndex = self.view.getWidg("variableTypeCombo").get_active()

        variable = None
        # Aggregate variable
        if varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AggregateVariable.TYPE):
            variable = AggregateVariable(anid, name, mutable, learnable)

        # Alternate Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(AlternateVariable.TYPE):
            variable = AlternateVariable(anid, name, mutable, learnable)

        # Repeat Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(RepeatVariable.TYPE):
            minIterations = int(self.view.getWidg("minSpin").get_text())
            maxIterations = int(self.view.getWidg("maxSpin").get_text())
            variable = RepeatVariable(anid, name, mutable, learnable, None, minIterations, maxIterations)

        # Data Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(DataVariable.TYPE):
            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()
            vtype = AbstractType.makeType(VariableTreeController.TYPE_INDEX_LIST[self.view.getWidg("typeCombo").get_active()], sized, minChars, maxChars, delimiter)
            originalValue = vtype.str2bin(self.view.getWidg("valueEntry").get_text())
            variable = DataVariable(anid, name, mutable, learnable, vtype, originalValue)

#===============================================================================
#        # Direct Relation Variable
#        elif strVarType == DirectRelationVariable.TYPE:
#
#            # We find the variable by its ID.
#            pointedID = str(self.view.getWidg("IDEntry").get_text())
#
#            variable = DirectRelationVariable(anid, name, mutable, learnable, pointedID, self.treeController.symbol)
#===============================================================================

        # Computed Relation Variable
        elif varTypeIndex == VariableTreeController.VARIABLE_INDEX_LIST.index(ComputedRelationVariable.TYPE):

            # We find the variable by its ID.
            pointedID = str(self.view.getWidg("IDEntry").get_text())

            sized = self.view.getWidg("sizedCheck").get_active()
            if sized:
                # If the variable is defined by a size.
                minChars = int(self.view.getWidg("minSpin").get_text())
                maxChars = int(self.view.getWidg("maxSpin").get_text())
                delimiter = None
            else:
                # The variable is defined by a delimiter.
                minChars = 0
                maxChars = 0
                delimiter = self.view.getWidg("delimiterEntry").get_text()

            factor = float(self.view.getWidg("factorEntry").get_text())
            offset = float(self.view.getWidg("offsetEntry").get_text())
            vtype = AbstractRelationType.makeType(VariableTreeController.RELATION_TYPE_INDEX_LIST[self.view.getWidg("relationTypeCombo").get_active()], sized, minChars, maxChars, delimiter, factor, offset)
            variable = ComputedRelationVariable(anid, name, mutable, learnable, vtype, pointedID, self.treeController.symbol)

        if variable is not None:
            # This part is for saving and transfering children when transforming a node variable into an other kind of node variable.
            if self.editOverCreate:
                father = None
                if len(self.variable.getFathers()) > 0:
                    father = self.variable.getFathers()[0]
                # We transform a node variable into a node variable.
                if (self.variable.getVariableType() == AggregateVariable.TYPE or self.variable.getVariableType() == AlternateVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    children = self.variable.getChildren()
                    self.variable = variable
                    for child in children:
                        self.variable.addChild(child)

                # We transform a repeat variable into a node variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == AggregateVariable.TYPE or variable.getVariableType() == AlternateVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.addChild(child)

                # We transform a repeat variable into a repeat variable.
                elif (self.variable.getVariableType() == RepeatVariable.TYPE) and (variable.getVariableType() == RepeatVariable.TYPE):
                    child = self.variable.getChild()
                    self.variable = variable
                    self.variable.setChild(child)

                # We do not manage/save children.
                else:
                    self.variable = variable
                if father is not None:
                    self.variable.addFather(father)
                self.treeController.editVariable(self.variable)

            else:
                if self.variable.getVariableType() == RepeatVariable.TYPE:
                    self.variable.setChild(variable)
                else:
                    self.variable.addChild(variable)
                self.treeController.registerVariable(self.rootEntry, variable)
        dialog.destroy()

    def chooseSelectedVariable(self, widget):
        """chooseSelectedVariable:
                Manage the creation and showing of a VariableIDTreeController that allows the user to choose one variable and get its ID.

                @param widget: the widget which is connected to this function through the event event.
        """
        VariableIDTreeController(self)