~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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
## Copyright (C) 2008 Rui Matos <tiagomatos@gmail.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 gobject
import gtk
import libxml2
from XmlHelper import xml_helper
from SearchCriterion import *
from gettext import gettext as _

from debug import *

class GroupsPaneItem (gobject.GObject):
    def __init__ (self):
        super (GroupsPaneItem, self).__init__ ()

        self.icon = None
        self.name = None
        self.separator = False

    def load_icon (self, icon_name):
        theme = gtk.icon_theme_get_default ()
        try:
            return theme.load_icon (icon_name,
                                    gtk.ICON_SIZE_MENU, 0)
        except gobject.GError:
            return None

class AllPrintersItem (GroupsPaneItem):
    def __init__ (self):
        super (AllPrintersItem, self).__init__ ()

        self.icon = self.load_icon ('printer')
        self.name = _("All Printers")

class SeparatorItem (GroupsPaneItem):
    def __init__ (self):
        super (SeparatorItem, self).__init__ ()

        self.separator = True

class FavouritesItem (GroupsPaneItem):
    def __init__ (self):
        super (FavouritesItem, self).__init__ ()

        self.icon = self.load_icon ('emblem-favorite')
        self.name = _("Favorites")

# Helper common base class, do not instantiate
class MutableItem (GroupsPaneItem):
    def __init__ (self, name, xml_node = None):
        super (MutableItem, self).__init__ ()

        self.name = name
        self.xml_node = xml_node

    def rename (self, new_name):
        self.xml_node.setProp ("name", new_name)
        xml_helper.write ()

        self.name = new_name

    def delete (self):
        self.xml_node.unlinkNode ()
        self.xml_node.freeNode ()
        xml_helper.write ()

class StaticGroupItem (MutableItem):
    def __init__ (self, name, xml_node = None):
        super (StaticGroupItem, self).__init__ (name, xml_node)

        self.icon = self.load_icon ('folder')
        self.printer_queues = []

        if not self.xml_node:
            self.xml_node = libxml2.newNode ("static-group")
            self.xml_node.newProp ("name", self.name)
            self.xml_node.newChild (None, "queues", None)
            xml_helper.add_group (self.xml_node)
        else:
            if not self.xml_node.children.children:
                # no queues
                return
            else:
                queue_node = self.xml_node.children.children
                while queue_node:
                    self.printer_queues.append (queue_node.prop ("name"))
                    queue_node = queue_node.next

    def add_queues (self, queue_list):
        queues_node = self.xml_node.children

        for queue_name in queue_list:
            if queue_name not in self.printer_queues:
                queue_node = libxml2.newNode ("queue")
                queue_node.newProp ("name", queue_name)
                queues_node.addChild (queue_node)
                self.printer_queues.append (queue_name)

        xml_helper.write ()

    def remove_queues (self, queue_list):
        queues_node = self.xml_node.children

        for queue_name in queue_list:
            if queue_name in self.printer_queues:
                queue_node = self.xml_node.children.children
                while queue_node:
                    if queue_node.prop ("name") == queue_name:
                        break
                    queue_node = queue_node.next
                queue_node.unlinkNode ()
                queue_node.freeNode ()
                self.printer_queues.remove (queue_name)

        xml_helper.write ()

class SavedSearchGroupItem (MutableItem):
    def __init__ (self, name, criteria = [],
                  match_all = False, xml_node = None):
        super (SavedSearchGroupItem, self).__init__ (name, xml_node)

        self.icon = self.load_icon ('folder-saved-search')
        self.criteria = criteria
        self.match_all = match_all

        if not self.xml_node:
            self.xml_node = libxml2.newNode ("search-group")
            self.xml_node.newProp ("name", self.name)
            criteria_node = self.xml_node.newChild (None, "criterias", None)
            criteria_node.newProp ("match", self.match_all and "all" or "any")
            for criterion in self.criteria:
                criterion_node = criteria_node.newChild (None, "criteria", None)
                criterion_node.newChild (None, "subject",
                                         str (criterion.subject))
                criterion_node.newChild (None, "rule",
                                         str (criterion.rule))
                criterion_node.newChild (None, "value",
                                         str (criterion.value))
            xml_helper.add_group (self.xml_node)
        else:
            criteria_node = self.xml_node.children
            self.match_all = criteria_node.prop ("match") == "all"
            criterion_node = criteria_node.children
            while criterion_node:
                criterion = SearchCriterion ()

                crit_child = criterion_node.children
                while crit_child:
                    if crit_child.name == "subject":
                        criterion.subject = int (crit_child.content)
                    elif crit_child.name == "rule":
                        criterion.rule = int (crit_child.content)
                    elif crit_child.name == "value":
                        criterion.value = crit_child.content
                    else:
                        pass
                    crit_child = crit_child.next

                self.criteria.append (criterion)
                criterion_node = criterion_node.next

class GroupsPaneModel (gtk.ListStore):
    def __init__ (self):
        super (GroupsPaneModel, self).__init__ (GroupsPaneItem)

    def append (self, item):
        return super (GroupsPaneModel, self).append ([item])

    def get (self, iter_or_path):
        return self[iter_or_path][0]

    def lookup_by_name (self, name):
        for item in self:
            if name == item[0].name:
                return item[0]

        return None

    def append_by_type (self, new_item):
        new_item_type = type (new_item)

        titer = self.get_iter_first ()
        while titer:
            if type (self.get_value (titer, 0)) == new_item_type:
                break

            titer = self.iter_next (titer)

        while titer:
            if type (self.get_value (titer, 0)) != new_item_type:
                break

            titer = self.iter_next (titer)

        return self.insert_before (titer, [new_item])