Back to index

system-config-printer  1.3.9+20120706
userdefault.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Copyright (C) 2006, 2007, 2008, 2010 Red Hat, Inc.
00004 ## Author: Tim Waugh <twaugh@redhat.com>
00005 
00006 ## This program is free software; you can redistribute it and/or modify
00007 ## it under the terms of the GNU General Public License as published by
00008 ## the Free Software Foundation; either version 2 of the License, or
00009 ## (at your option) any later version.
00010 
00011 ## This program is distributed in the hope that it will be useful,
00012 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 ## GNU General Public License for more details.
00015 
00016 ## You should have received a copy of the GNU General Public License
00017 ## along with this program; if not, write to the Free Software
00018 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00020 import gtk
00021 import os
00022 import subprocess
00023 
00024 class UserDefaultPrinter:
00025     def __init__ (self):
00026         try:
00027             lpoptions = os.environ["HOME"]
00028         except KeyError:
00029             try:
00030                 lpoptions = "/home/" + os.environ["USER"]
00031             except KeyError:
00032                 lpoptions = None
00033 
00034         if lpoptions:
00035             lpoptions += "/.cups/lpoptions"
00036 
00037         self.lpoptions = lpoptions
00038 
00039     def clear (self):
00040         if not self.lpoptions:
00041             return
00042 
00043         try:
00044             opts = file (self.lpoptions).readlines ()
00045         except IOError:
00046             return
00047 
00048         for i in range (len (opts)):
00049             if opts[i].startswith ("Default "):
00050                 opts[i] = "Dest " + opts[i][8:]
00051         file (self.lpoptions, "w").writelines (opts)
00052 
00053     def get (self):
00054         if not self.lpoptions:
00055             return None
00056 
00057         try:
00058             opts = file (self.lpoptions).readlines ()
00059         except IOError:
00060             return None
00061 
00062         for i in range (len (opts)):
00063             if opts[i].startswith ("Default "):
00064                 rest = opts[i][8:]
00065                 slash = rest.find ("/")
00066                 if slash != -1:
00067                     space = rest[:slash].find (" ")
00068                 else:
00069                     space = rest.find (" ")
00070                 return rest[:space]
00071         return None
00072 
00073     def set (self, default):
00074         p = subprocess.Popen ([ "lpoptions", "-d", default ],
00075                               close_fds=True,
00076                               stdin=file ("/dev/null"),
00077                               stdout=file ("/dev/null", "w"),
00078                               stderr=subprocess.PIPE)
00079         (stdout, stderr) = p.communicate ()
00080         exitcode = p.wait ()
00081         if exitcode != 0:
00082             raise RuntimeError (exitcode, stderr.strip ())
00083         return
00084 
00085     def __repr__ (self):
00086         return "<UserDefaultPrinter (%s)>" % repr (self.get ())
00087 
00088 class UserDefaultPrompt:
00089     def __init__ (self,
00090                   set_default_fn,
00091                   refresh_fn,
00092                   name,
00093                   title,
00094                   parent,
00095                   primarylabel,
00096                   systemwidelabel,
00097                   clearpersonallabel,
00098                   personallabel):
00099         self.set_default_fn = set_default_fn
00100         self.refresh_fn = refresh_fn
00101         self.name = name
00102         dialog = gtk.Dialog (title,
00103                              parent,
00104                              gtk.DIALOG_MODAL |
00105                              gtk.DIALOG_DESTROY_WITH_PARENT |
00106                              gtk.DIALOG_NO_SEPARATOR,
00107                              (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
00108                               gtk.STOCK_OK, gtk.RESPONSE_OK))
00109         dialog.set_default_response (gtk.RESPONSE_OK)
00110         dialog.set_border_width (6)
00111         dialog.set_resizable (False)
00112         hbox = gtk.HBox (False, 12)
00113         hbox.set_border_width (6)
00114         image = gtk.Image ()
00115         image.set_from_stock (gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
00116         image.set_alignment (0.0, 0.0)
00117         hbox.pack_start (image, False, False, 0)
00118         vboxouter = gtk.VBox (False, 6)
00119         primary = gtk.Label ()
00120         primary.set_markup ('<span weight="bold" size="larger">' +
00121                             primarylabel + '</span>')
00122         primary.set_line_wrap (True)
00123         primary.set_alignment (0.0, 0.0)
00124         vboxouter.pack_start (primary, False, False, 0)
00125         vboxradio = gtk.VBox (False, 0)
00126         systemwide = gtk.RadioButton (label=systemwidelabel)
00127         vboxradio.pack_start (systemwide, False, False, 0)
00128         clearpersonal = gtk.CheckButton (clearpersonallabel)
00129         alignment = gtk.Alignment (0, 0, 0, 0)
00130         alignment.set_padding (0, 0, 12, 0)
00131         alignment.add (clearpersonal)
00132         vboxradio.pack_start (alignment)
00133         vboxouter.pack_start (vboxradio, False, False, 0)
00134         personal = gtk.RadioButton (group=systemwide,
00135                                     label=personallabel)
00136         vboxouter.pack_start (personal, False, False, 0)
00137         hbox.pack_start (vboxouter, False, False, 0)
00138         dialog.vbox.pack_start (hbox, False, False, 0)
00139         systemwide.set_active (True)
00140         clearpersonal.set_active (True)
00141         self.userdef = UserDefaultPrinter ()
00142         clearpersonal.set_sensitive (self.userdef.get () != None)
00143 
00144         self.systemwide = systemwide
00145         self.clearpersonal = clearpersonal
00146         self.personal = personal
00147         systemwide.connect ("toggled", self.on_toggled)
00148         dialog.connect ("response", self.on_response)
00149         dialog.show_all ()
00150 
00151     def on_toggled (self, button):
00152         self.clearpersonal.set_sensitive (self.userdef.get () != None and
00153                                           self.systemwide.get_active ())
00154 
00155     def on_response (self, dialog, response_id):
00156         if response_id != gtk.RESPONSE_OK:
00157             dialog.destroy ()
00158             return
00159 
00160         if self.systemwide.get_active ():
00161             if self.clearpersonal.get_active ():
00162                 self.userdef.clear ()
00163             self.set_default_fn (self.name)
00164         else:
00165             try:
00166                 self.userdef.set (self.name)
00167             except Exception, e:
00168                 print "Error setting default: %s" % repr (e)
00169 
00170             self.refresh_fn ()
00171 
00172         dialog.destroy ()