Debian Codegraph @

troubleshoot/ChooseNetworkPrinter.py

raw
1 #!/usr/bin/python3
2
3 ## Printing troubleshooter
4
5 ## Copyright (C) 2008, 2009, 2011, 2012 Red Hat, Inc.
6 ## Author: 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 import cups
25 from gi.repository import GObject
26 from timedops import TimedOperation
27 from .base import *
28 class ChooseNetworkPrinter(Question):
29 def __init__ (self, troubleshooter):
30 Question.__init__ (self, troubleshooter, "Choose network printer")
31 page1 = self.initial_vbox (_("Choose Network Printer"),
32 _("Please select the network printer you "
33 "are trying to use from the list below. "
34 "If it does not appear in the list, "
35 "select 'Not listed'."))
36 tv = Gtk.TreeView ()
37 name = Gtk.TreeViewColumn (_("Name"),
38 Gtk.CellRendererText (), text=0)
39 location = Gtk.TreeViewColumn (_("Location"),
40 Gtk.CellRendererText (), text=1)
41 info = Gtk.TreeViewColumn (_("Information"),
42 Gtk.CellRendererText (), text=2)
43 name.set_property ("resizable", True)
44 location.set_property ("resizable", True)
45 info.set_property ("resizable", True)
46 tv.append_column (name)
47 tv.append_column (location)
48 tv.append_column (info)
49 tv.set_rules_hint (True)
50 sw = Gtk.ScrolledWindow ()
51 sw.set_policy (Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
52 sw.set_shadow_type (Gtk.ShadowType.IN)
53 sw.add (tv)
54 page1.pack_start (sw, True, True, 0)
55 self.treeview = tv
56 troubleshooter.new_page (page1, self)
57
58 def display (self):
59 answers = self.troubleshooter.answers
60 if answers['cups_queue_listed']:
61 return False
62
63 if not answers.get ('remote_server_cups', False):
64 return False
65
66 server = answers['remote_server_try_connect']
67
68 model = Gtk.ListStore (str,
69 str,
70 str,
71 GObject.TYPE_PYOBJECT)
72 self.model = model
73 self.treeview.set_model (model)
74 iter = model.append (None)
75 model.set (iter, 0, _("Not listed"), 1, '', 2, '', 3, 0)
76
77 parent = self.troubleshooter.get_window ()
78
79 try:
80 self.op = TimedOperation (cups.Connection,
81 kwargs={"host": server},
82 parent=parent)
83 c = self.op.run ()
84 self.op = TimedOperation (c.getDests, parent=parent)
85 dests = self.op.run ()
86 printers = None
87 dests_list = []
88 for (name, instance), dest in dests.items ():
89 if name is None:
90 continue
91
92 if instance is not None:
93 queue = "%s/%s" % (name, instance)
94 else:
95 queue = name
96
97 if printers is None:
98 self.op = TimedOperation (c.getPrinters)
99 printers = self.op.run ()
100
101 if name not in printers:
102 info = _("Unknown")
103 location = _("Unknown")
104 else:
105 printer = printers[name]
106 info = printer.get('printer-info', _("Unknown"))
107 location = printer.get('printer-location', _("Unknown"))
108
109 dests_list.append ((queue, location, info, dest))
110
111 dests_list.sort (key=lambda x: x[0])
112 for queue, location, info, dest in dests_list:
113 iter = model.append (None)
114 model.set (iter, 0, queue, 1, location, 2, info, 3, dest)
115
116 except cups.HTTPError:
117 pass
118 except cups.IPPError:
119 pass
120 except RuntimeError:
121 pass
122
123 return True
124
125 def connect_signals (self, handler):
126 self.signal_id = self.treeview.connect ("cursor-changed", handler)
127
128 def disconnect_signals (self):
129 self.treeview.disconnect (self.signal_id)
130
131 def can_click_forward (self):
132 model, iter = self.treeview.get_selection ().get_selected ()
133 if iter is None:
134 return False
135 return True
136
137 def collect_answer (self):
138 if not self.troubleshooter.answers.get ('remote_server_cups', False):
139 return {}
140
141 model, iter = self.treeview.get_selection ().get_selected ()
142 if not model:
143 return {}
144
145 dest = model.get_value (iter, 3)
146 if dest == 0:
147 class enum_dests:
148 def __init__ (self, model):
149 self.dests = []
150 model.foreach (self.each, None)
151
152 def each (self, model, path, iter, user_data):
153 dest = model.get_value (iter, 3)
154 if dest:
155 self.dests.append ((dest.name, dest.instance))
156
157 return { 'remote_cups_queue_listed': False,
158 'remote_cups_dests_available': enum_dests (model).dests }
159 else:
160 return { 'remote_cups_queue_listed': True,
161 'remote_cups_dest': dest,
162 'remote_cups_queue': dest.name,
163 'remote_cups_instance': dest.instance }
164
165 def cancel_operation (self):
166 self.op.cancel ()