Back to index

system-config-printer  1.3.9+20120706
ChooseNetworkPrinter.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Printing troubleshooter
00004 
00005 ## Copyright (C) 2008, 2009, 2011 Red Hat, Inc.
00006 ## Author: 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 ChooseNetworkPrinter(Question):
00027     def __init__ (self, troubleshooter):
00028         Question.__init__ (self, troubleshooter, "Choose network printer")
00029         page1 = self.initial_vbox (_("Choose Network Printer"),
00030                                    _("Please select the network printer you "
00031                                      "are trying to use from the list below. "
00032                                      "If it does not appear in the list, "
00033                                      "select 'Not listed'."))
00034         tv = gtk.TreeView ()
00035         name = gtk.TreeViewColumn (_("Name"),
00036                                    gtk.CellRendererText (), text=0)
00037         location = gtk.TreeViewColumn (_("Location"),
00038                                        gtk.CellRendererText (), text=1)
00039         info = gtk.TreeViewColumn (_("Information"),
00040                                    gtk.CellRendererText (), text=2)
00041         name.set_property ("resizable", True)
00042         location.set_property ("resizable", True)
00043         info.set_property ("resizable", True)
00044         tv.append_column (name)
00045         tv.append_column (location)
00046         tv.append_column (info)
00047         tv.set_rules_hint (True)
00048         sw = gtk.ScrolledWindow ()
00049         sw.set_policy (gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
00050         sw.set_shadow_type (gtk.SHADOW_IN)
00051         sw.add (tv)
00052         page1.pack_start (sw, True, True, 0)
00053         self.treeview = tv
00054         troubleshooter.new_page (page1, self)
00055 
00056     def display (self):
00057         answers = self.troubleshooter.answers
00058         if answers['cups_queue_listed']:
00059             return False
00060 
00061         if not answers.get ('remote_server_cups', False):
00062             return False
00063 
00064         server = answers['remote_server_try_connect']
00065 
00066         model = gtk.ListStore (gobject.TYPE_STRING,
00067                                gobject.TYPE_STRING,
00068                                gobject.TYPE_STRING,
00069                                gobject.TYPE_PYOBJECT)
00070         self.model = model
00071         self.treeview.set_model (model)
00072         iter = model.append (None)
00073         model.set (iter, 0, _("Not listed"), 1, '', 2, '', 3, None)
00074 
00075         parent = self.troubleshooter.get_window ()
00076 
00077         try:
00078             self.op = TimedOperation (cups.Connection, 
00079                                       kwargs={"host": server},
00080                                       parent=parent)
00081             c = self.op.run ()
00082             self.op = TimedOperation (c.getDests, parent=parent)
00083             dests = self.op.run ()
00084             printers = None
00085             dests_list = []
00086             for (name, instance), dest in dests.iteritems ():
00087                 if name == None:
00088                     continue
00089 
00090                 if instance != None:
00091                     queue = "%s/%s" % (name, instance)
00092                 else:
00093                     queue = name
00094 
00095                 if printers == None:
00096                     self.op = TimedOperation (c.getPrinters)
00097                     printers = self.op.run ()
00098 
00099                 if not printers.has_key (name):
00100                     info = _("Unknown")
00101                     location = _("Unknown")
00102                 else:
00103                     printer = printers[name]
00104                     info = printer.get('printer-info', _("Unknown"))
00105                     location = printer.get('printer-location', _("Unknown"))
00106 
00107                 dests_list.append ((queue, location, info, dest))
00108 
00109             dests_list.sort (lambda x, y: cmp (x[0], y[0]))
00110             for queue, location, info, dest in dests_list:
00111                 iter = model.append (None)
00112                 model.set (iter, 0, queue, 1, location, 2, info, 3, dest)
00113 
00114         except cups.HTTPError:
00115             pass
00116         except cups.IPPError:
00117             pass
00118         except RuntimeError:
00119             pass
00120 
00121         return True
00122 
00123     def connect_signals (self, handler):
00124         self.signal_id = self.treeview.connect ("cursor-changed", handler)
00125 
00126     def disconnect_signals (self):
00127         self.treeview.disconnect (self.signal_id)
00128 
00129     def can_click_forward (self):
00130         model, iter = self.treeview.get_selection ().get_selected ()
00131         if iter == None:
00132             return False
00133         return True
00134 
00135     def collect_answer (self):
00136         if not self.troubleshooter.answers.get ('remote_server_cups', False):
00137             return {}
00138 
00139         model, iter = self.treeview.get_selection ().get_selected ()
00140         if not model:
00141             return {}
00142 
00143         dest = model.get_value (iter, 3)
00144         if dest == None:
00145             class enum_dests:
00146                 def __init__ (self, model):
00147                     self.dests = []
00148                     model.foreach (self.each, None)
00149 
00150                 def each (self, model, path, iter, user_data):
00151                     dest = model.get_value (iter, 3)
00152                     if dest:
00153                         self.dests.append ((dest.name, dest.instance))
00154 
00155             return { 'remote_cups_queue_listed': False,
00156                      'remote_cups_dests_available': enum_dests (model).dests }
00157         else:
00158             return { 'remote_cups_queue_listed': True,
00159                      'remote_cups_dest': dest,
00160                      'remote_cups_queue': dest.name,
00161                      'remote_cups_instance': dest.instance }
00162 
00163     def cancel_operation (self):
00164         self.op.cancel ()