Debian Codegraph @

troubleshoot/DeviceListed.py

raw
1 #!/usr/bin/python3
2
3 ## Printing troubleshooter
4
5 ## Copyright (C) 2008, 2012 Red Hat, Inc.
6 ## Copyright (C) 2008 Tim Waugh <[email protected]>
7
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 2 of the License, or
11 ## (at your option) any later version.
12
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU General Public License for more details.
17
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
22 from gi.repository import Gtk
23
24 class NoDevice:
25 pass
26
27 NotListed = NoDevice()
28
29 import cups
30 from gi.repository import GObject
31 from timedops import TimedOperation
32 from .base import *
33 class DeviceListed(Question):
34 def __init__ (self, troubleshooter):
35 # Is the device listed?
36 Question.__init__ (self, troubleshooter, "Choose device")
37 page1 = self.initial_vbox (_("Choose Device"),
38 _("Please select the device you want "
39 "to use from the list below. "
40 "If it does not appear in the list, "
41 "select 'Not listed'."))
42 tv = Gtk.TreeView ()
43 name = Gtk.TreeViewColumn (_("Name"),
44 Gtk.CellRendererText (), text=0)
45 info = Gtk.TreeViewColumn (_("Information"),
46 Gtk.CellRendererText (), text=1)
47 uri = Gtk.TreeViewColumn (_("Device URI"),
48 Gtk.CellRendererText (), text=2)
49 name.set_property ("resizable", True)
50 info.set_property ("resizable", True)
51 uri.set_property ("resizable", True)
52 tv.append_column (name)
53 tv.append_column (info)
54 tv.append_column (uri)
55 tv.set_rules_hint (True)
56 sw = Gtk.ScrolledWindow ()
57 sw.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
58 sw.set_shadow_type (Gtk.ShadowType.IN)
59 sw.add (tv)
60 page1.pack_start (sw, True, True, 0)
61 self.treeview = tv
62 troubleshooter.new_page (page1, self)
63
64 def display (self):
65 self.answers = {}
66 answers = self.troubleshooter.answers
67 if (answers['printer_is_remote'] or
68 answers.get ('cups_printer_remote', False)):
69 return False
70
71 model = Gtk.ListStore (str,
72 str,
73 str,
74 GObject.TYPE_PYOBJECT)
75 self.treeview.set_model (model)
76 iter = model.append (None)
77 model.set (iter, 0, _("Not listed"), 1, '', 2, '', 3, NotListed)
78
79 devices = {}
80 parent = self.troubleshooter.get_window ()
81 # Skip device list if this page is hidden and we're skipping
82 # backwards past it.
83 if not (answers['cups_queue_listed'] and
84 self.troubleshooter.is_moving_backwards ()):
85 # Otherwise, fetch devices.
86 self.authconn = answers['_authenticated_connection']
87 try:
88 self.op = TimedOperation (self.authconn.getDevices,
89 kwargs={'signature': 'iiasas'},
90 parent=parent)
91 devices = self.op.run ()
92 devices_list = []
93 for uri, device in devices.items ():
94 if uri.find (':') == -1:
95 continue
96
97 if device.get('device-class') != 'direct':
98 continue
99
100 name = device.get('device-info', _("Unknown"))
101 info = device.get('device-make-and-model', _("Unknown"))
102 devices_list.append ((name, info, uri, device))
103
104 devices_list.sort (key=lambda x: x[0])
105 for name, info, uri, device in devices_list:
106 iter = model.append (None)
107 model.set (iter, 0, name, 1, info, 2, uri, 3, device)
108
109 except cups.HTTPError:
110 pass
111 except cups.IPPError:
112 pass
113 except RuntimeError:
114 pass
115
116 if answers['cups_queue_listed']:
117 try:
118 printer_dict = answers['cups_printer_dict']
119 uri = printer_dict['device-uri']
120 device = devices[uri]
121 self.answers['cups_device_dict'] = device
122 except KeyError:
123 pass
124
125 return False
126
127 return True
128
129 def connect_signals (self, handler):
130 self.signal_id = self.treeview.connect ("cursor-changed", handler)
131
132 def disconnect_signals (self):
133 self.treeview.disconnect (self.signal_id)
134
135 def can_click_forward (self):
136 model, iter = self.treeview.get_selection ().get_selected ()
137 if iter is None:
138 return False
139 return True
140
141 def collect_answer (self):
142 if not self.displayed:
143 return self.answers
144
145 model, iter = self.treeview.get_selection ().get_selected ()
146 device = model.get_value (iter, 3)
147 if device == NotListed:
148 class enum_devices:
149 def __init__ (self, model):
150 self.devices = {}
151 model.foreach (self.each, None)
152
153 def each (self, model, path, iter, user_data):
154 uri = model.get_value (iter, 2)
155 device = model.get_value (iter, 3)
156 if device != NotListed:
157 self.devices[uri] = device
158
159 self.answers['cups_device_listed'] = False
160 avail = enum_devices (model).devices
161 self.answers['cups_devices_available'] = avail
162 else:
163 uri = model.get_value (iter, 2)
164 self.answers['cups_device_listed'] = True
165 self.answers['cups_device_uri'] = uri
166 self.answers['cups_device_attributes'] = device
167
168 return self.answers
169
170 def cancel_operation (self):
171 self.op.cancel ()
172
173 # Abandon the CUPS connection and make another.
174 answers = self.troubleshooter.answers
175 factory = answers['_authenticated_connection_factory']
176 self.authconn = factory.get_connection ()
177 self.answers['_authenticated_connection'] = self.authconn
178