class DisplayConfigger(ConfigDialog):
    """
      Configuration Dialog for the displays. This class handles loading / saving
      the configuration.
    """

    def __init__(self, ident, path):

        # init the StateSaver
        self.__backend = StateSaver(ident + "CONFIG")

        # may we save already, or is it too early?
        self.__may_save = False

        # ID for storing the configuration
        self.__ident = ident

        # the scripting environment with which the config interacts
        self.__scripting_environment = None

        ConfigDialog.__init__(self, path)
        self.set_property("title", _("Configuration"))



    #
    # Sets the scripting environment to be used.
    #
    def set_scripting_environment(self, script):

        self.__scripting_environment = script
        self._set_setter(self.__setter_wrapper)
        self._set_getter(self.__scripting_environment.get_value)
        self._set_caller(self.__scripting_environment.call_function)



    #
    # Wraps the config setter handler to include saving config.
    #
    def __setter_wrapper(self, key, value, datatype):
        assert key

        self.__scripting_environment.set_value(key, value)

        # save config, but not too early
        if (self.__may_save):
            rep = dtype_repr(datatype, value)
            self.__backend.set_key(key, (rep, dtype_name(datatype)))



    #
    # Build preferences and put elements into the scripting environment
    #
    def build(self, items):

        ConfigDialog.build(self, items)

        for c in self.get_config_items():
            ident = c.get_prop("id")
            if (ident):
                self.__scripting_environment.add_element("Prefs", ident, c)



    #
    # Loads the initial configuration.
    #
    def load_config(self):

        # read stored configuration
        for key in self.__backend.list():
            if (key.endswith("_TYPE")): continue

            rep, dtype = self.__backend.get_key(key)
            datatype = dtype_get_type(dtype)
            value = dtype_build(datatype, rep)

            try:
                self.__setter_wrapper(key, value, None)
            except:
                log("Couldn't pass arguments (%s, %s) to setter."
                    % (key, value))

        # have the children update themselves
        for c in self.get_config_items(): c.update()



    #
    # Removes the configuration.
    #
    def remove_config(self):

        self.__backend.remove()



    def show(self):

        self.__may_save = True
        ConfigDialog.show(self)
class _ConfigManager(Observable):
    """
    This class acts as a compatibility adaptor for deprecated sensors and can
    be removed as soon as support for sensors is being dropped.
    """

    UNDEF = "-- undef --"

    OBS_UPDATE = 1
    

    def __init__(self):

        self.__db = StateSaver("sensors")

        # hashtable for config change handlers: path -> handler
        self.__handlers = {}

        self.add_observer(self.__on_observe_backend)



    def __on_observe_backend(self, src, cmd, *args):

        if (cmd == src.OBS_UPDATE):
            path, value = args

            parts = path.split(".")
            for i in range(len(parts)):
                handler = self.__handlers.get(tuple(parts[:i + 1]))
            
                if (handler):
                    handler(parts, value)
                    break
            #end for



    def watch(self, *args):

        """Sets a callback handler for watching changes for the given
        configuration entry."""

        path = args[:-1]
        print "ADD WATCH", path
        handler = args[-1]
        self.__handlers[path] = handler



    def remove_watcher(self, *args):

        """Removes a watch callback handler."""

        try:
            path = args[:-1]
            del self.__handlers[path]
        except KeyError:
            pass


    def set(self, *args):

        path = ".".join(args[:-1])
        value = args[-1]

        self.__db.set_key(path, value)
        utils.request_call(self.update_observer, self.OBS_UPDATE, path, value)



    def get(self, *args):

        path = ".".join(args)
        value = self.__db.get_key(path, self.UNDEF)

        return value