Back to index

system-config-printer  1.3.9+20120706
ChoosePrinter.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 ChoosePrinter(Question):
00027     def __init__ (self, troubleshooter):
00028         # First question: which printer? (page 1)
00029         Question.__init__ (self, troubleshooter, "Choose printer")
00030         page1 = self.initial_vbox (_("Choose Printer"),
00031                                    _("Please select the printer you are "
00032                                      "trying 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         location = gtk.TreeViewColumn (_("Location"),
00039                                        gtk.CellRendererText (), text=1)
00040         info = gtk.TreeViewColumn (_("Information"),
00041                                    gtk.CellRendererText (), text=2)
00042         name.set_property ("resizable", True)
00043         location.set_property ("resizable", True)
00044         info.set_property ("resizable", True)
00045         tv.append_column (name)
00046         tv.append_column (location)
00047         tv.append_column (info)
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         model = gtk.ListStore (gobject.TYPE_STRING,
00059                                gobject.TYPE_STRING,
00060                                gobject.TYPE_STRING,
00061                                gobject.TYPE_PYOBJECT)
00062         self.treeview.set_model (model)
00063         iter = model.append (None)
00064         model.set (iter, 0, _("Not listed"), 1, '', 2, '', 3, None)
00065 
00066         parent = self.troubleshooter.get_window ()
00067         try:
00068             cups.setServer ('')
00069             c = self.timedop (cups.Connection, parent=parent).run ()
00070             dests = self.timedop (c.getDests, parent=parent).run ()
00071             printers = None
00072             dests_list = []
00073             for (name, instance), dest in dests.iteritems ():
00074                 if name == None:
00075                     continue
00076 
00077                 if instance != None:
00078                     queue = "%s/%s" % (name, instance)
00079                 else:
00080                     queue = name
00081 
00082                 if printers == None:
00083                     printers = self.timedop (c.getPrinters,
00084                                              parent=parent).run ()
00085 
00086                 if not printers.has_key (name):
00087                     info = _("Unknown")
00088                     location = _("Unknown")
00089                 else:
00090                     printer = printers[name]
00091                     info = printer.get('printer-info', _("Unknown"))
00092                     location = printer.get('printer-location', _("Unknown"))
00093 
00094                 dests_list.append ((queue, location, info, dest))
00095 
00096             dests_list.sort (lambda x, y: cmp (x[0], y[0]))
00097             for queue, location, info, dest in dests_list:
00098                 iter = model.append (None)
00099                 model.set (iter, 0, queue, 1, location, 2, info, 3, dest)
00100 
00101         except cups.HTTPError:
00102             pass
00103         except cups.IPPError:
00104             pass
00105         except RuntimeError:
00106             pass
00107 
00108         return True
00109 
00110     def connect_signals (self, handler):
00111         self.signal_id = self.treeview.connect ("cursor-changed", handler)
00112 
00113     def disconnect_signals (self):
00114         self.treeview.disconnect (self.signal_id)
00115 
00116     def can_click_forward (self):
00117         model, iter = self.treeview.get_selection ().get_selected ()
00118         if iter == None:
00119             return False
00120         return True
00121 
00122     def collect_answer (self):
00123         model, iter = self.treeview.get_selection ().get_selected ()
00124         dest = model.get_value (iter, 3)
00125         if dest == None:
00126             class enum_dests:
00127                 def __init__ (self, model):
00128                     self.dests = []
00129                     model.foreach (self.each, None)
00130 
00131                 def each (self, model, path, iter, user_data):
00132                     dest = model.get_value (iter, 3)
00133                     if dest:
00134                         self.dests.append ((dest.name, dest.instance))
00135 
00136             return { 'cups_queue_listed': False,
00137                      'cups_dests_available': enum_dests (model).dests }
00138         else:
00139             return { 'cups_queue_listed': True,
00140                      'cups_dest': dest,
00141                      'cups_queue': dest.name,
00142                      'cups_instance': dest.instance }
00143 
00144     def cancel_operation (self):
00145         self.op.cancel ()
00146 
00147     def timedop (self, *args, **kwargs):
00148         self.op = TimedOperation (*args, **kwargs)
00149         return self.op