Back to index

system-config-printer  1.3.9+20120706
DeviceListed.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Printing troubleshooter
00004 
00005 ## Copyright (C) 2008 Red Hat, Inc.
00006 ## Copyright (C) 2008 Tim Waugh <twaugh@redhat.com>
00007 
00008 ## This program is free software; you can redistribute it and/or modify
00009 ## it under the terms of the GNU General Public License as published by
00010 ## the Free Software Foundation; either version 2 of the License, or
00011 ## (at your option) any later version.
00012 
00013 ## This program is distributed in the hope that it will be useful,
00014 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 ## GNU General Public License for more details.
00017 
00018 ## You should have received a copy of the GNU General Public License
00019 ## along with this program; if not, write to the Free Software
00020 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 
00022 import cups
00023 import gobject
00024 from timedops import TimedOperation
00025 from base import *
00026 class DeviceListed(Question):
00027     def __init__ (self, troubleshooter):
00028         # Is the device listed?
00029         Question.__init__ (self, troubleshooter, "Choose device")
00030         page1 = self.initial_vbox (_("Choose Device"),
00031                                    _("Please select the device you want "
00032                                      "to use from the list below. "
00033                                      "If it does not appear in the list, "
00034                                      "select 'Not listed'."))
00035         tv = gtk.TreeView ()
00036         name = gtk.TreeViewColumn (_("Name"),
00037                                    gtk.CellRendererText (), text=0)
00038         info = gtk.TreeViewColumn (_("Information"),
00039                                    gtk.CellRendererText (), text=1)
00040         uri = gtk.TreeViewColumn (_("Device URI"),
00041                                   gtk.CellRendererText (), text=2)
00042         name.set_property ("resizable", True)
00043         info.set_property ("resizable", True)
00044         uri.set_property ("resizable", True)
00045         tv.append_column (name)
00046         tv.append_column (info)
00047         tv.append_column (uri)
00048         tv.set_rules_hint (True)
00049         sw = gtk.ScrolledWindow ()
00050         sw.set_policy (gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
00051         sw.set_shadow_type (gtk.SHADOW_IN)
00052         sw.add (tv)
00053         page1.pack_start (sw, True, True, 0)
00054         self.treeview = tv
00055         troubleshooter.new_page (page1, self)
00056 
00057     def display (self):
00058         self.answers = {}
00059         answers = self.troubleshooter.answers
00060         if (answers['printer_is_remote'] or
00061             answers.get ('cups_printer_remote', False)):
00062             return False
00063 
00064         model = gtk.ListStore (gobject.TYPE_STRING,
00065                                gobject.TYPE_STRING,
00066                                gobject.TYPE_STRING,
00067                                gobject.TYPE_PYOBJECT)
00068         self.treeview.set_model (model)
00069         iter = model.append (None)
00070         model.set (iter, 0, _("Not listed"), 1, '', 2, '', 3, None)
00071 
00072         devices = {}
00073         parent = self.troubleshooter.get_window ()
00074         # Skip device list if this page is hidden and we're skipping
00075         # backwards past it.
00076         if not (answers['cups_queue_listed'] and
00077                 self.troubleshooter.is_moving_backwards ()):
00078             # Otherwise, fetch devices.
00079             self.authconn = answers['_authenticated_connection']
00080             try:
00081                 self.op = TimedOperation (self.authconn.getDevices,
00082                                           parent=parent)
00083                 devices = self.op.run ()
00084                 devices_list = []
00085                 for uri, device in devices.iteritems ():
00086                     if uri.find (':') == -1:
00087                         continue
00088 
00089                     if device.get('device-class') != 'direct':
00090                         continue
00091 
00092                     name = device.get('device-info', _("Unknown"))
00093                     info = device.get('device-make-and-model', _("Unknown"))
00094                     devices_list.append ((name, info, uri, device))
00095 
00096                 devices_list.sort (lambda x, y: cmp (x[0], y[0]))
00097                 for name, info, uri, device in devices_list:
00098                     iter = model.append (None)
00099                     model.set (iter, 0, name, 1, info, 2, uri, 3, device)
00100 
00101             except cups.HTTPError:
00102                 pass
00103             except cups.IPPError:
00104                 pass
00105             except RuntimeError:
00106                 pass
00107 
00108         if answers['cups_queue_listed']:
00109             try:
00110                 printer_dict = answers['cups_printer_dict']
00111                 uri = printer_dict['device-uri']
00112                 device = devices[uri]
00113                 self.answers['cups_device_dict'] = device
00114             except KeyError:
00115                 pass
00116 
00117             return False
00118 
00119         return True
00120 
00121     def connect_signals (self, handler):
00122         self.signal_id = self.treeview.connect ("cursor-changed", handler)
00123 
00124     def disconnect_signals (self):
00125         self.treeview.disconnect (self.signal_id)
00126 
00127     def can_click_forward (self):
00128         model, iter = self.treeview.get_selection ().get_selected ()
00129         if iter == None:
00130             return False
00131         return True
00132 
00133     def collect_answer (self):
00134         if not self.displayed:
00135             return self.answers
00136 
00137         model, iter = self.treeview.get_selection ().get_selected ()
00138         device = model.get_value (iter, 3)
00139         if device == None:
00140             class enum_devices:
00141                 def __init__ (self, model):
00142                     self.devices = {}
00143                     model.foreach (self.each, None)
00144 
00145                 def each (self, model, path, iter, user_data):
00146                     uri = model.get_value (iter, 2)
00147                     device = model.get_value (iter, 3)
00148                     if device:
00149                         self.devices[uri] = device
00150 
00151             self.answers['cups_device_listed'] = False
00152             avail = enum_devices (model).devices
00153             self.answers['cups_devices_available'] = avail
00154         else:
00155             uri = model.get_value (iter, 2)
00156             self.answers['cups_device_listed'] = True
00157             self.answers['cups_device_uri'] = uri
00158             self.answers['cups_device_attributes'] = device
00159 
00160         return self.answers
00161 
00162     def cancel_operation (self):
00163         self.op.cancel ()
00164 
00165         # Abandon the CUPS connection and make another.
00166         answers = self.troubleshooter.answers
00167         factory = answers['_authenticated_connection_factory']
00168         self.authconn = factory.get_connection ()
00169         self.answers['_authenticated_connection'] = self.authconn
00170