~doctormo/gnome-wallchanger/trunk

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
#
# Copyright 2009, Martin Owens.
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
"""
List wallpapers from gnome, skip self.
"""

from gwc.base import ListerBase, add_lister

class gnomeLister(ListerBase):
    """Pick a random wallpaper from the gnome backgrounds"""
    name       = 'default'
    can_delete = False

    def removeCurrent(self, filename, delete_file=True):
        """
        Deletes the background entry from backgrounds.xml
        and removes the file from the disk.
        """
        wpt = self.getWallpaperTag()
        for wp in wpt.childNodes:
            meta = self.decodeWP(wp)
            if wp.nodeType==1 and unicode(meta['filename'])==filename:
                wpt.removeChild(wp)
        if os.path.exists(filename):
            try:
                os.remove(filename)
            except OSError:
                sys.stderr.write(" ! Unable to remove file: %s\n" % filename)

    def addNew(self, filename, wp):
        """Adds a new background image file to the backgrounds.xml file."""
        wp['filename'] = unicode(filename)
        for ewp in self.getList():
            if unicode(ewp) == wp['filename']:
                print " & Already added %s, ignore settings for now." % wp['filename']
                return
        wpt = self.getWallpaperTag()
        wpt.appendChild(self.encodeWP(wp, self.openxml()))
        fh = open(gnome_settings, 'w')
        fh.write(self.openxml().toxml(encoding="utf-8"))
        fh.close()

    def getWallpaperTag(self):
        """Return specific wallpaper tag from wallpapers"""
        for child in self.openxml().childNodes:
            if child.nodeType == 1 and child.tagName == 'wallpapers':
                return child
        return None

    def getList(self):
        """Return a list of gnome backgrounds"""
        for wp in self.getWallpaperTag().childNodes:
            if wp.nodeType == 1 and wp.tagName == 'wallpaper':
                if wp.attributes.has_key('deleted') and wp.attributes['deleted']=='true':
                    continue
                item = self.decodeWP(wp)
                if item and os.path.lexists(item['filename']):
                    yield item['filename']

    def decodeWP(self, node):
        """Basic XML helper method for getting text values"""
        results = {}
        for value in node.childNodes:
            if value.nodeType==1:
                ndata = ''
                for val in value.childNodes:
                    if val.nodeType==3: # TEXT_NODE
                        ndata += val.data
                results[value.tagName] = ndata
        return results

    def encodeWP(self, wp, doc):
        """Basic XML helper method for setting text values"""
        node = doc.createElement('wallpaper')
        node.attributes['deleted'] = 'false'
        for key in wp.keys():
            tag = doc.createElement(key)
            tag.appendChild(doc.createTextNode(unicode(wp[key])))
            node.appendChild(tag)

        return node

    def openxml(self):
        """Return parsed gnome background xml document"""
        if not self._doc:
            self._doc = minidom.parse(gnome_settings)
        return self._doc