home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / system-config-printer / optionwidgets.py < prev    next >
Encoding:
Python Source  |  2010-09-28  |  8.7 KB  |  240 lines

  1. ## system-config-printer
  2.  
  3. ## Copyright (C) 2006, 2007, 2008, 2009 Red Hat, Inc.
  4. ## Copyright (C) 2006 Florian Festi <ffesti@redhat.com>
  5. ## Copyright (C) 2007, 2008, 2009 Tim Waugh <twaugh@redhat.com>
  6.  
  7. ## This program is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11.  
  12. ## This program is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16.  
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with this program; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21. import gtk
  22. import cups
  23. from gettext import gettext as _
  24. import ppdippstr
  25.  
  26. def OptionWidget(option, ppd, gui, tab_label=None):
  27.     """Factory function"""
  28.     ui = option.ui
  29.     if (ui == cups.PPD_UI_BOOLEAN and
  30.         len (option.choices) != 2):
  31.         # This option is advertised as a Boolean but in fact has more
  32.         # than two choices.
  33.         print "Treating Boolean option %s as PickOne" % option.keyword
  34.         ui = cups.PPD_UI_PICKONE
  35.  
  36.     if ui == cups.PPD_UI_BOOLEAN:
  37.         return OptionBool(option, ppd, gui, tab_label=tab_label)
  38.     elif ui == cups.PPD_UI_PICKONE:
  39.         return OptionPickOne(option, ppd, gui, tab_label=tab_label)
  40.     elif ui == cups.PPD_UI_PICKMANY:
  41.         return OptionPickMany(option, ppd, gui, tab_label=tab_label)
  42.  
  43. # ---------------------------------------------------------------------------
  44.  
  45. class Option:
  46.     def __init__(self, option, ppd, gui, tab_label=None):
  47.         self.option = option
  48.         self.ppd = ppd
  49.         self.gui = gui
  50.         self.enabled = True
  51.         self.tab_label = tab_label
  52.  
  53.         vbox = gtk.VBox()
  54.         
  55.         self.btnConflict = gtk.Button()
  56.         icon = gtk.image_new_from_stock(gtk.STOCK_DIALOG_WARNING,
  57.                                         gtk.ICON_SIZE_SMALL_TOOLBAR)
  58.         self.btnConflict.add(icon)
  59.         self.btnConflict.set_no_show_all(True) #avoid the button taking
  60.                                                # over control again
  61.         vbox.add(self.btnConflict)    # vbox reserves space while button
  62.         #vbox.set_size_request(32, 28) # is hidden
  63.         self.conflictIcon = vbox
  64.  
  65.         self.btnConflict.connect("clicked", self.on_btnConflict_clicked)
  66.         icon.show()
  67.  
  68.         self.constraints = [c for c in ppd.constraints
  69.                             if (c.option1 == option.keyword or
  70.                                 c.option2 == option.keyword)]
  71.         #for c in self.constraints:
  72.         #    if not c.choice1 or not c.choice2:
  73.         #        print c.option1, repr(c.choice1), c.option2, repr(c.choice2)
  74.         self.conflicts = set()
  75.         self.conflict_message = ""
  76.  
  77.     def enable(self, enabled=True):
  78.         self.selector.set_sensitive (enabled)
  79.         self.enabled = enabled
  80.  
  81.     def disable(self):
  82.         self.enable (False)
  83.  
  84.     def is_enabled(self):
  85.         return self.enabled
  86.  
  87.     def get_current_value(self):
  88.         raise NotImplemented
  89.  
  90.     def is_changed(self):
  91.         return self.get_current_value()!= self.option.defchoice
  92.     
  93.     def writeback(self):
  94.         #print repr(self.option.keyword), repr(self.get_current_value())
  95.         if self.enabled:
  96.             self.ppd.markOption(self.option.keyword, self.get_current_value())
  97.  
  98.     def checkConflicts(self, update_others=True):
  99.         value = self.get_current_value()
  100.         for constraint in self.constraints:
  101.             if constraint.option1 == self.option.keyword:
  102.                 option2 = self.gui.options.get(constraint.option2, None)
  103.                 choice1 = constraint.choice1
  104.                 choice2 = constraint.choice2
  105.             else:
  106.                 option2 = self.gui.options.get(constraint.option1, None)
  107.                 choice1 = constraint.choice2
  108.                 choice2 = constraint.choice1
  109.  
  110.             if option2 is None: continue
  111.  
  112.             def matches (constraint_choice, value):
  113.                 if constraint_choice != '':
  114.                     return constraint_choice == value
  115.                 return value not in ['None', 'False', 'Off']
  116.  
  117.             if (matches (choice1, value) and
  118.                 matches (choice2, option2.get_current_value())):
  119.                 # conflict
  120.                 self.conflicts.add(constraint)
  121.                 if update_others:
  122.                     option2.checkConflicts(update_others=False)
  123.             elif constraint in self.conflicts:
  124.                 # remove conflict
  125.                 self.conflicts.remove(constraint)
  126.                 option2.checkConflicts(update_others=False)
  127.  
  128.  
  129.         tooltip = [_("Conflicts with:")]
  130.         conflicting_options = dict()
  131.         for c in self.conflicts:
  132.             if c.option1 == self.option.keyword:
  133.                 option = self.gui.options.get(c.option2)
  134.             else:
  135.                 option = self.gui.options.get(c.option1)
  136.  
  137.             conflicting_options[option.option.keyword] = option
  138.  
  139.         for option in conflicting_options.values ():
  140.             opt = option.option.text
  141.             val = option.get_current_value ()
  142.             for choice in option.option.choices:
  143.                 if choice['choice'] == val:
  144.                     val = ppdippstr.ppd.get (choice['text'])
  145.  
  146.             tooltip.append ("%s: %s" % (opt, val))
  147.             
  148.         tooltip = "\n".join(tooltip)
  149.  
  150.         self.conflict_message = tooltip # XXX more verbose
  151.             
  152.         if self.conflicts:
  153.             self.btnConflict.set_tooltip_text (tooltip)
  154.             self.btnConflict.show()
  155.         else:
  156.             self.btnConflict.hide()
  157.  
  158.         self.gui.option_changed(self)
  159.         return self.conflicts
  160.             
  161.     def on_change(self, widget):
  162.         self.checkConflicts()
  163.  
  164.     def on_btnConflict_clicked(self, button):
  165.         parent = self.btnConflict
  166.         while parent != None and not isinstance (parent, gtk.Window):
  167.             parent = parent.get_parent ()
  168.  
  169.         dialog = gtk.MessageDialog (parent,
  170.                                     gtk.DIALOG_DESTROY_WITH_PARENT |
  171.                                     gtk.DIALOG_MODAL,
  172.                                     gtk.MESSAGE_WARNING,
  173.                                     gtk.BUTTONS_CLOSE,
  174.                                     self.conflict_message)
  175.         dialog.run()
  176.         dialog.destroy()
  177.         
  178. # ---------------------------------------------------------------------------
  179.  
  180. class OptionBool(Option):
  181.  
  182.     def __init__(self, option, ppd, gui, tab_label=None):
  183.         self.selector = gtk.CheckButton(ppdippstr.ppd.get (option.text))
  184.         self.label = None
  185.         self.false = u"False" # hack to allow "None" instead of "False"
  186.         self.true = u"True"
  187.         for c in option.choices:
  188.             if c["choice"] in ("None", "False", "Off"):
  189.                 self.false = c["choice"]
  190.             if c["choice"] in ("True", "On"):
  191.                 self.true = c["choice"]
  192.         self.selector.set_active(option.defchoice == self.true)
  193.         self.selector.set_alignment(0.0, 0.5)
  194.         self.selector.connect("toggled", self.on_change)
  195.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  196.  
  197.     def get_current_value(self):
  198.         return (self.false, self.true)[self.selector.get_active()]
  199.  
  200. # ---------------------------------------------------------------------------
  201.  
  202. class OptionPickOne(Option):
  203.     widget_name = "OptionPickOne"
  204.  
  205.     def __init__(self, option, ppd, gui, tab_label=None):
  206.         self.selector = gtk.combo_box_new_text()
  207.         #self.selector.set_alignment(0.0, 0.5)
  208.  
  209.         label = ppdippstr.ppd.get (option.text)
  210.         if not label.endswith (':'):
  211.             label += ':'
  212.         self.label = gtk.Label(label)
  213.         self.label.set_alignment(0.0, 0.5)
  214.         
  215.         selected = None
  216.         for nr, choice in enumerate(option.choices):
  217.             self.selector.append_text(ppdippstr.ppd.get (choice['text']))
  218.             if option.defchoice == choice['choice']:
  219.                 selected = nr
  220.         if selected is not None:
  221.             self.selector.set_active(selected)
  222.         else:
  223.             print option.text, "unknown value:", option.defchoice
  224.         self.selector.connect("changed", self.on_change)
  225.  
  226.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  227.  
  228.     def get_current_value(self):
  229.         return self.option.choices[self.selector.get_active()]['choice']
  230.         
  231. # ---------------------------------------------------------------------------
  232.  
  233. class OptionPickMany(OptionPickOne):
  234.     widget_name = "OptionPickMany"
  235.  
  236.     def __init__(self, option, ppd, gui, tab_label=None):
  237.         raise NotImplemented
  238.         Option.__init__(self, option, ppd, gui, tab_label=tab_label)
  239.         
  240.