1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """\
21 L{X2goClientPrintingFILE} class is one of Python X2go's public API classes.
22
23 Retrieve an instance of this class from your L{X2goClient} instance.
24 Use this class in your Python X2go based applications to access the »printing«
25 configuration of your X2go client application.
26
27 """
28 __NAME__ = 'x2goprinting-pylib'
29
30
31 import types
32 import ConfigParser
33
34
35 import x2go.log as log
36 import x2go.printactions as printactions
37
38 from x2go.defaults import X2GO_CLIENTPRINTING_DEFAULTS as _X2GO_CLIENTPRINTING_DEFAULTS
39 from x2go.defaults import X2GO_PRINTING_CONFIGFILES as _X2GO_PRINTING_CONFIGFILES
40 import x2go.inifiles as inifiles
41 import x2go.x2go_exceptions as x2go_exceptions
42
43 _print_property_map = {
44 'pdfview_cmd': {
45 'ini_section': 'view',
46 'ini_option': 'command',
47 },
48 'save_to_folder': {
49 'ini_section': 'save',
50 'ini_option': 'folder',
51 },
52 'printer': {
53 'ini_section': 'CUPS',
54 'ini_option': 'defaultprinter',
55 },
56 'print_cmd': {
57 'ini_section': 'print',
58 'ini_option': 'command',
59 },
60 }
63 """\
64 L{X2goClientPrinting} provides access to the X2go ini-like file
65 »printing« as stored in C{~/.x2goclient/printing} resp. globally
66 C{/etc/x2goclient/printing}.
67
68 An instance of L{X2goClientPrinting} is created on each incoming
69 print job. This facilitates that on every print job the print action
70 for this job is derived from the »printing« configuration file.
71
72 Thus, changes on the file are active for the next incoming print job.
73
74 """
75 config_files = []
76 _print_action = None
77 defaultValues = _X2GO_CLIENTPRINTING_DEFAULTS
78
79 - def __init__(self, config_files=_X2GO_PRINTING_CONFIGFILES, defaults=None, client_instance=None, logger=None, loglevel=log.loglevel_DEFAULT):
80 """\
81 @param config_files: a list of configuration files names (e.g. a global filename and a user's home
82 directory filename)
83 @type config_files: C{list}
84 @param defaults: a cascaded Python dicitionary structure with ini file defaults (to override
85 Python X2go's hard coded defaults in L{defaults}
86 @type defaults: C{dict}
87 @param logger: you can pass an L{X2goLogger} object to the
88 L{X2goPrintAction} constructor
89 @type logger: C{instance}
90 @param loglevel: if no L{X2goLogger} object has been supplied a new one will be
91 constructed with the given loglevel
92 @type loglevel: C{int}
93
94 """
95 self.client_instance = client_instance
96 inifiles.X2goIniFile.__init__(self, config_files, defaults=defaults, logger=logger, loglevel=loglevel)
97
98 self._detect_print_action()
99
101 """\
102 Derive a print action from sections, keys and their values in a typical
103 X2go client »printing« configuration file.
104
105 """
106 _general_pdfview = self.get('General', 'pdfview', key_type=types.BooleanType)
107 _view_open = self.get('view', 'open', key_type=types.BooleanType)
108 _print_startcmd = self.get('print', 'startcmd', key_type=types.BooleanType)
109 _show_dialog = self.get('General', 'showdialog', key_type=types.BooleanType)
110
111 if _show_dialog and self.client_instance is not None:
112 self._print_action = printactions.X2goPrintActionDIALOG(client_instance=self.client_instance, logger=self.logger)
113
114 elif _general_pdfview and _view_open:
115 _view_command = self.get('view', 'command')
116 self._print_action = printactions.X2goPrintActionPDFVIEW(client_instance=self.client_instance, pdfview_cmd=_view_command, logger=self.logger)
117
118 elif _general_pdfview and not _view_open:
119 _safe_folder = self.get('save', 'folder')
120 self._print_action = printactions.X2goPrintActionPDFSAVE(client_instance=self.client_instance, save_to_folder=_safe_folder, logger=self.logger)
121
122 elif not _general_pdfview and not _print_startcmd:
123 _cups_defaultprinter = self.get('CUPS', 'defaultprinter')
124 self._print_action = printactions.X2goPrintActionPRINT(client_instance=self.client_instance, printer=_cups_defaultprinter, logger=self.logger)
125
126 elif not _general_pdfview and _print_startcmd:
127 _print_command = self.get('print', 'command')
128 self._print_action = printactions.X2goPrintActionPRINTCMD(client_instance=self.client_instance, print_cmd=_print_command, logger=self.logger)
129
130 @property
132 """\
133 Return the print action described by the »printing« configuration file.
134
135 This method has property status and wraps around the L{get_print_action}
136 method.
137
138 """
139 return self.get_print_action()
140
142 """\
143 Return the print action described by the »printing« configuration file.
144
145 """
146 if reload:
147 self.load()
148
149 if reinit:
150 self._detect_print_action()
151
152 if return_name:
153 return self._print_action.__name__
154 else:
155 return self._print_action
156
168
170 """\
171 STILL UNDOCUMENTED
172
173 """
174 if print_property in _print_property_map.keys():
175 _ini_section = _print_property_map[print_property]['ini_section']
176 _ini_option = _print_property_map[print_property]['ini_option']
177 _default_type = self.get_type(_ini_section, _ini_option)
178 if type(value) is types.UnicodeType:
179 value = value.encode('utf-8')
180 if _default_type != type(value):
181 raise x2go_exceptions.X2goClientPrintingException('Type mismatch error for property ,,%s\'\' - is: %s, should be: %s' % (print_property, str(type(value)), str(_default_type)))
182 self.update_value(_ini_section, _ini_option, value)
183 else:
184 raise x2go_exceptions.X2goClientPrintingException('No such X2go client printing property ,,%s\'\'' % print_property)
185
187 """\
188 STILL UNDOCUMENTED
189
190 """
191 if print_action == 'DIALOG':
192 self.update_value('General', 'showdialog', True)
193 else:
194 self.update_value('General', 'showdialog', False)
195
196 if print_action == 'PDFVIEW':
197 self.update_value('General', 'pdfview', True)
198 self.update_value('view', 'open', True)
199
200 elif print_action == 'PDFSAVE':
201 self.update_value('General', 'pdfview', True)
202 self.update_value('view', 'open', False)
203
204 elif print_action == 'PRINT':
205 self.update_value('General', 'pdfview', False)
206 self.update_value('print', 'startcmd', False)
207
208 elif print_action == 'PRINTCMD':
209 self.update_value('General', 'pdfview', False)
210 self.update_value('print', 'startcmd', True)
211
212 for print_property in print_properties.keys():
213 self.set_property(print_property, print_properties[print_property])
214