~gdesklets-desklet-team/gdesklets/0.36

« back to all changes in this revision

Viewing changes to config/DisplayConfigger.py

  • Committer: Robert Pastierovic
  • Date: 2007-10-07 10:08:42 UTC
  • Revision ID: pastierovic@gmail.com-20071007100842-fdvp2vzmqgh1j87k
merged 0.3x branch and basic documentation and some other changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from ConfigDialog import ConfigDialog
 
2
from StateSaver import StateSaver
 
3
from utils.datatypes import *
 
4
 
 
5
 
 
6
class DisplayConfigger(ConfigDialog):
 
7
    """
 
8
      Configuration Dialog for the displays. This class handles loading / saving
 
9
      the configuration.
 
10
    """
 
11
 
 
12
    def __init__(self, ident, path):
 
13
 
 
14
        # init the StateSaver
 
15
        self.__backend = StateSaver(ident + "CONFIG")
 
16
 
 
17
        # may we save already, or is it too early?
 
18
        self.__may_save = False
 
19
 
 
20
        # ID for storing the configuration
 
21
        self.__ident = ident
 
22
 
 
23
        # the scripting environment with which the config interacts
 
24
        self.__scripting_environment = None
 
25
 
 
26
        ConfigDialog.__init__(self, path)
 
27
        self.set_property("title", _("Configuration"))
 
28
 
 
29
 
 
30
 
 
31
    #
 
32
    # Sets the scripting environment to be used.
 
33
    #
 
34
    def set_scripting_environment(self, script):
 
35
 
 
36
        self.__scripting_environment = script
 
37
        self._set_setter(self.__setter_wrapper)
 
38
        self._set_getter(self.__scripting_environment.get_value)
 
39
        self._set_caller(self.__scripting_environment.call_function)
 
40
 
 
41
 
 
42
 
 
43
    #
 
44
    # Wraps the config setter handler to include saving config.
 
45
    #
 
46
    def __setter_wrapper(self, key, value, datatype):
 
47
        assert key
 
48
 
 
49
        self.__scripting_environment.set_value(key, value)
 
50
 
 
51
        # save config, but not too early
 
52
        if (self.__may_save):
 
53
            rep = dtype_repr(datatype, value)
 
54
            self.__backend.set_key(key, (rep, dtype_name(datatype)))
 
55
 
 
56
 
 
57
 
 
58
    #
 
59
    # Build preferences and put elements into the scripting environment
 
60
    #
 
61
    def build(self, items):
 
62
 
 
63
        ConfigDialog.build(self, items)
 
64
 
 
65
        for c in self.get_config_items():
 
66
            ident = c.get_prop("id")
 
67
            if (ident):
 
68
                self.__scripting_environment.add_element("Prefs", ident, c)
 
69
 
 
70
 
 
71
 
 
72
    #
 
73
    # Loads the initial configuration.
 
74
    #
 
75
    def load_config(self):
 
76
 
 
77
        # read stored configuration
 
78
        for key in self.__backend.list():
 
79
            if (key.endswith("_TYPE")): continue
 
80
 
 
81
            rep, dtype = self.__backend.get_key(key)
 
82
            datatype = dtype_get_type(dtype)
 
83
            value = dtype_build(datatype, rep)
 
84
 
 
85
            try:
 
86
                self.__setter_wrapper(key, value, None)
 
87
            except:
 
88
                log("Couldn't pass arguments (%s, %s) to setter."
 
89
                    % (key, value))
 
90
 
 
91
        # have the children update themselves
 
92
        for c in self.get_config_items(): c.update()
 
93
 
 
94
 
 
95
 
 
96
    #
 
97
    # Removes the configuration.
 
98
    #
 
99
    def remove_config(self):
 
100
 
 
101
        self.__backend.remove()
 
102
 
 
103
 
 
104
 
 
105
    def show(self):
 
106
 
 
107
        self.__may_save = True
 
108
        ConfigDialog.show(self)