#!/usr/bin/python

## system-config-printer
## Simple print dialog
  
## Copyright (C) 2003 Tim Waugh <twaugh@redhat.com>
  
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
  
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
  
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import getopt
import gtk
import os
import pycups
import sys

class PrintDialog:
    def __init__ (self):
        self.dialog = gtk.Dialog ()
        self.dialog.set_title ("Print dialog")
        table = gtk.Table (2,3)
        select = gtk.Label ("Select printer:")
        select.set_alignment (0, 0.5)
        self.optionmenu = gtk.OptionMenu ()
        self.optionmenu.connect ('changed', self.optionmenu_changed)
        self.default_active = gtk.FALSE
        location = gtk.Label ("Location:")
        location.set_alignment (0, 0.5)
        self.location = gtk.Label ("")
        self.location.set_alignment (0, 0.5)
        checkbutton = gtk.CheckButton ()
        checkbutton.set_use_underline (gtk.TRUE)
        checkbutton.set_label ("Make this my _default printer")
        checkbutton.connect ('toggled', self.default_toggled)
        table.attach (select, 0, 1, 0, 1, gtk.FILL, 0, 5, 5)
        table.attach (self.optionmenu, 2, 3, 0, 1, gtk.FILL, 0, 5, 5)
        table.attach (location, 0, 1, 2, 3, gtk.FILL, 0, 5, 5)
        table.attach (self.location, 2, 3, 2, 3, gtk.FILL, 0, 5, 5)
        table.attach (checkbutton, 0, 3, 3, 4, gtk.FILL, 0, 5, 5)
        self.dialog.vbox.pack_start (table)
        self.dialog.add_button (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
        self.dialog.add_button (gtk.STOCK_PRINT, gtk.RESPONSE_OK)
        self.dialog.vbox.show_all ()

    def optionmenu_changed (self, optionmenu):
        chosen = self.queue_names[optionmenu.get_history ()]
        self.location.set_text (self.queues[chosen]['printer-location'])
        if not self.default_active:
            return

        if os.fork () != 0:
            return

        args = ["lpoptions", "-d", chosen]
        os.close (1)
        os.execvp (args[0], args)

    def default_toggled (self, checkbutton):
        self.default_active = checkbutton.get_active ()
        if self.default_active:
            self.optionmenu_changed (self.optionmenu)

    def populate_queue_list (self):
        menu = gtk.Menu ()
        self.queues = pycups.get_queues ()
        self.queue_names = self.queues.keys ()
	self.queue_names.sort ()
        for queue in self.queue_names:
            menuitem = gtk.MenuItem (queue)
            menuitem.child.set_use_underline (gtk.FALSE)
            menu.add (menuitem)
            menuitem.show ()
            menuitem.set_sensitive (gtk.TRUE)

        self.optionmenu.set_menu (menu)

        # Find this user's default queue.
        try:
            output = os.popen ("lpstat -d").readlines ()[0]
            default = output.split (": ")[1].strip ()
            self.optionmenu.set_history (self.queue_names.index (default))
        except:
            pass

    def run (self, files = None):
        self.populate_queue_list ()
        if self.dialog.run () != gtk.RESPONSE_OK:
            return

        chosen = self.queue_names[self.optionmenu.get_history ()]
        args = ["lp", "-d", chosen]
        if file:
            args.extend (file)
        os.execvp (args[0], args)

try:
    file = sys.argv[1:]
except:
    file = None

PrintDialog ().run (file)
