Debian Codegraph @

troubleshoot/ChoosePrinter.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 NoPrinter:
25 pass
26
27 NotListed = NoPrinter()
28
29 import cups
30 from gi.repository import GObject
31 from timedops import TimedOperation
32 from .base import *
33 class ChoosePrinter(Question):
34 def __init__ (self, troubleshooter):
35 # First question: which printer? (page 1)
36 Question.__init__ (self, troubleshooter, "Choose printer")
37 page1 = self.initial_vbox (_("Choose Printer"),
38 _("Please select the printer you are "
39 "trying 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 location = Gtk.TreeViewColumn (_("Location"),
46 Gtk.CellRendererText (), text=1)
47 info = Gtk.TreeViewColumn (_("Information"),
48 Gtk.CellRendererText (), text=2)
49 name.set_property ("resizable", True)
50 location.set_property ("resizable", True)
51 info.set_property ("resizable", True)
52 tv.append_column (name)
53 tv.append_column (location)
54 tv.append_column (info)
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 model = Gtk.ListStore (str,
66 str,
67 str,
68 GObject.TYPE_PYOBJECT)
69 self.treeview.set_model (model)
70 iter = model.append (None)
71 model.set (iter, 0, _("Not listed"), 1, '', 2, '', 3, NotListed)
72
73 parent = self.troubleshooter.get_window ()
74 try:
75 cups.setServer ('')
76 c = self.timedop (cups.Connection, parent=parent).run ()
77 dests = self.timedop (c.getDests, parent=parent).run ()
78 printers = None
79 dests_list = []
80 for (name, instance), dest in dests.items ():
81 if name is None:
82 continue
83
84 if instance is not None:
85 queue = "%s/%s" % (name, instance)
86 else:
87 queue = name
88
89 if printers is None:
90 printers = self.timedop (c.getPrinters,
91 parent=parent).run ()
92
93 if name not in printers:
94 info = _("Unknown")
95 location = _("Unknown")
96 else:
97 printer = printers[name]
98 info = printer.get('printer-info', _("Unknown"))
99 location = printer.get('printer-location', _("Unknown"))
100
101 dests_list.append ((queue, location, info, dest))
102
103 dests_list.sort (key=lambda x: x[0])
104 for queue, location, info, dest in dests_list:
105 iter = model.append (None)
106 model.set (iter, 0, queue, 1, location, 2, info, 3, dest)
107
108 except cups.HTTPError:
109 pass
110 except cups.IPPError:
111 pass
112 except RuntimeError:
113 pass
114
115 return True
116
117 def connect_signals (self, handler):
118 self.signal_id = self.treeview.connect ("cursor-changed", handler)
119
120 def disconnect_signals (self):
121 self.treeview.disconnect (self.signal_id)
122
123 def can_click_forward (self):
124 model, iter = self.treeview.get_selection ().get_selected ()
125 if iter is None:
126 return False
127 return True
128
129 def collect_answer (self):
130 model, iter = self.treeview.get_selection ().get_selected ()
131 dest = model.get_value (iter, 3)
132 if dest == NotListed:
133 class enum_dests:
134 def __init__ (self, model):
135 self.dests = []
136 model.foreach (self.each, None)
137
138 def each (self, model, path, iter, user_data):
139 dest = model.get_value (iter, 3)
140 if dest != NotListed:
141 self.dests.append ((dest.name,
142 dest.instance))
143
144 return { 'cups_queue_listed': False,
145 'cups_dests_available': enum_dests (model).dests }
146 else:
147 return { 'cups_queue_listed': True,
148 'cups_dest': dest,
149 'cups_queue': dest.name,
150 'cups_instance': dest.instance }
151
152 def cancel_operation (self):
153 self.op.cancel ()
154
155 def timedop (self, *args, **kwargs):
156 self.op = TimedOperation (*args, **kwargs)
157 return self.op