#!/usr/bin/python -O

## system-config-printer
## Update the configuration to reflect any necessary changes in its
## format.

## Copyright (C) 2002, 2003 Red Hat, Inc.
## Copyright (C) 2002, 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 gettext
import sys
if not "/usr/share/printconf/util" in sys.path:
    sys.path.append ("/usr/share/printconf/util")

import printconf_conf as conf

domain = 'system-config-printer'
from rhpl.translate import _, N_

try:
    conf.init_queue_edit_or_die ()
except IOError:
    sys.stderr.write(_("You must be root to run this program.\n"))
    sys.exit (1)

import os
import signal

import printconf_version

pkgdata = "/usr/share/printconf/"

## Several types of changes are necessary:
## - drivers sometimes change names (e.g. stp -> gimp-print)
##
## - options sometimes changes names
##
## - queue types may need changing: an example is the PostScript
##   queue type.  Previously it was a special type, but now it is just
##   another foomatic queue, with model 'Generic-PostScript_Printer' and
##   driver 'Postscript'.  That way the options are controlled by foomatic.

# dict (by old-name) of new-names
changed_driver_names = {
    'stp': 'gimp-print-ijs',
    'gimp-print': 'gimp-print-ijs',
    'hpijs-rss': 'hpijs'
    }

if os.access("/usr/share/foomatic/db/source/driver/omni-compiled.xml",os.R_OK):
    changed_driver_names['omni'] = 'omni-compiled'

# dict (by driver name) of list of tuples (old-name, new-name)
changed_option_names = {
    'gimp-print-ijs': [ ( 'StpBrightness', 'stpBrightness' ),
                        ( 'StpGamma', 'stpGamma' ),
                        ( 'StpSaturation', 'stpSaturation' ) ]
    }

made_changes = 0

oldprinterids = {}
def load_foomatic_oldprinterids ():
    """Load the foomatic list of changed printer IDs."""

    try:
        ids = file ("/usr/share/foomatic/db/oldprinterids").readlines ()
    except IOError:
        return

    # Each line is of the form: '100576 HP-LaserJet_4000\n'
    # where 100576 is the old ID.
    for each in ids:
        try:
            (old, new) = each.strip ().split (' ')
        except:
            # Bogus line
            continue

        oldprinterids[old] = new

foomatic_ready = 0
def use_foomatic ():
    """Demand-load the foomatic database.  If it is already loaded,
    this does nothing; otherwise it is loaded."""
    global foomatic_ready
    if not foomatic_ready:
        conf.foomatic_init_overview ()
        foomatic_ready = 1

oldprinterids_loaded = 0
name_dict, alias_dict = conf.get_queues ()
for name in name_dict.keys ():
    if not oldprinterids_loaded:
        load_foomatic_oldprinterids ()
        oldprinterids_loaded = 1

    queue_dict = name_dict[name]
    if not queue_dict["editable"]:
        continue

    queue = queue_dict["queue"]
    ps = conf.drivers.postscript
    if (queue["filter_type"].value == ps.filter_type and
        queue["filter_data"]["mf_type"].value == ps.mf_type):
        # Convert PostScript magicfilter type to new-style foomatic
        # 'Generic PostScript' type.
        use_foomatic ()
        namespace = conf.NameSpace ()
        conf.driverspace_setup (queue, namespace)
        namespace.f_type = conf.drivers.foomatic.filter_type
        namespace.foomatic.mf_type = conf.drivers.foomatic.mf_type
        f = conf.foomatic.make_model_dict_dict\
            ["Generic"]["PostScript Printer"]
        namespace.foomatic.printer_id = f.id
        namespace.foomatic.gs_driver = f.driver
        conf.driverspace_apply (queue, namespace)
        made_changes = 1

    fm = conf.drivers.foomatic
    if (queue["filter_type"].value == fm.filter_type and
        queue["filter_data"]["mf_type"].value == fm.mf_type):
        # Foomatic queue.  Is it a driver that's changed name?
        driver = queue["filter_data"]["gs_driver"].value
        try:
            driver = changed_driver_names[driver]
            queue["filter_data"]["gs_driver"].value = driver
            made_changes = 1
        except:
            pass

        # Have any of this driver's options changed names?
        try:
            for (old, new) in changed_option_names[driver]:
                for opt in queue["filter_data"]["foomatic_defaults"]:
                    if opt["name"].value == old:
                        opt["name"].value = new
                        made_changes = 1
        except:
            pass

        # What about the printer ID; has that changed?
        printer_id = queue["filter_data"]["printer_id"].value
        try:
            new_id = oldprinterids[printer_id]
            queue["filter_data"]["printer_id"].value = new_id
            made_changes = 1
        except:
            pass

if made_changes:
    conf.save_queues ()

sys.exit (0)

# Local Variables:
# py-indent-offset: 4
# End:
