~ubuntu-branches/ubuntu/lucid/system-config-printer/lucid-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python

## Copyright (C) 2006, 2007, 2008, 2010 Red Hat, Inc.
## Author: 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 gtk
import os
import subprocess

class UserDefaultPrinter:
    def __init__ (self):
        try:
            lpoptions = os.environ["HOME"]
        except KeyError:
            try:
                lpoptions = "/home/" + os.environ["USER"]
            except KeyError:
                lpoptions = None

        if lpoptions:
            lpoptions += "/.cups/lpoptions"

        self.lpoptions = lpoptions

    def clear (self):
        if not self.lpoptions:
            return

        try:
            opts = file (self.lpoptions).readlines ()
        except IOError:
            return

        for i in range (len (opts)):
            if opts[i].startswith ("Default "):
                opts[i] = "Dest " + opts[i][8:]
        file (self.lpoptions, "w").writelines (opts)

    def get (self):
        if not self.lpoptions:
            return None

        try:
            opts = file (self.lpoptions).readlines ()
        except IOError:
            return None

        for i in range (len (opts)):
            if opts[i].startswith ("Default "):
                rest = opts[i][8:]
                slash = rest.find ("/")
                if slash != -1:
                    space = rest[:slash].find (" ")
                else:
                    space = rest.find (" ")
                return rest[:space]
        return None

    def set (self, default):
        p = subprocess.Popen ([ "lpoptions", "-d", default ],
                              stdin=file ("/dev/null"),
                              stdout=file ("/dev/null", "w"),
                              stderr=subprocess.PIPE)
        (stdout, stderr) = p.communicate ()
        exitcode = p.wait ()
        if exitcode != 0:
            raise RuntimeError (exitcode, stderr.strip ())
        return

    def __repr__ (self):
        return "<UserDefaultPrinter (%s)>" % repr (self.get ())

class UserDefaultPrompt:
    def __init__ (self,
                  set_default_fn,
                  refresh_fn,
                  name,
                  title,
                  parent,
                  primarylabel,
                  systemwidelabel,
                  clearpersonallabel,
                  personallabel):
        self.set_default_fn = set_default_fn
        self.refresh_fn = refresh_fn
        self.name = name
        dialog = gtk.Dialog (title,
                             parent,
                             gtk.DIALOG_MODAL |
                             gtk.DIALOG_DESTROY_WITH_PARENT |
                             gtk.DIALOG_NO_SEPARATOR,
                             (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                              gtk.STOCK_OK, gtk.RESPONSE_OK))
        dialog.set_default_response (gtk.RESPONSE_OK)
        dialog.set_border_width (6)
        dialog.set_resizable (False)
        hbox = gtk.HBox (False, 12)
        hbox.set_border_width (6)
        image = gtk.Image ()
        image.set_from_stock (gtk.STOCK_DIALOG_QUESTION, gtk.ICON_SIZE_DIALOG)
        image.set_alignment (0.0, 0.0)
        hbox.pack_start (image, False, False, 0)
        vboxouter = gtk.VBox (False, 6)
        primary = gtk.Label ()
        primary.set_markup ('<span weight="bold" size="larger">' +
                            primarylabel + '</span>')
        primary.set_line_wrap (True)
        primary.set_alignment (0.0, 0.0)
        vboxouter.pack_start (primary, False, False, 0)
        vboxradio = gtk.VBox (False, 0)
        systemwide = gtk.RadioButton (label=systemwidelabel)
        vboxradio.pack_start (systemwide, False, False, 0)
        clearpersonal = gtk.CheckButton (clearpersonallabel)
        alignment = gtk.Alignment (0, 0, 0, 0)
        alignment.set_padding (0, 0, 12, 0)
        alignment.add (clearpersonal)
        vboxradio.pack_start (alignment)
        vboxouter.pack_start (vboxradio, False, False, 0)
        personal = gtk.RadioButton (group=systemwide,
                                    label=personallabel)
        vboxouter.pack_start (personal, False, False, 0)
        hbox.pack_start (vboxouter, False, False, 0)
        dialog.vbox.pack_start (hbox, False, False, 0)
        systemwide.set_active (True)
        clearpersonal.set_active (True)
        self.userdef = UserDefaultPrinter ()
        clearpersonal.set_sensitive (self.userdef.get () != None)

        self.systemwide = systemwide
        self.clearpersonal = clearpersonal
        self.personal = personal
        systemwide.connect ("toggled", self.on_toggled)
        dialog.connect ("response", self.on_response)
        dialog.show_all ()

    def on_toggled (self, button):
        self.clearpersonal.set_sensitive (self.userdef.get () != None and
                                          self.systemwide.get_active ())

    def on_response (self, dialog, response_id):
        if response_id != gtk.RESPONSE_OK:
            dialog.destroy ()
            return

        if self.systemwide.get_active ():
            if self.clearpersonal.get_active ():
                self.userdef.clear ()
            self.set_default_fn (self.name)
        else:
            try:
                self.userdef.set (self.name)
            except Exception, e:
                print "Error setting default: %s" % repr (e)

            self.refresh_fn ()

        dialog.destroy ()