~doctormo/gnome-wallchanger/trunk

« back to all changes in this revision

Viewing changes to gwc/plugins/gnome.py

  • Committer: Martin Owens (DoctorMO)
  • Date: 2009-10-11 19:32:33 UTC
  • Revision ID: doctormo@gmail.com-20091011193233-15p70kim7zsjyhqx
Add new modules for ver 3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright 2009, Martin Owens.
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
#
 
17
"""
 
18
List wallpapers from gnome, skip self.
 
19
"""
 
20
 
 
21
from gwc.base import ListerBase, add_lister
 
22
 
 
23
class gnomeLister(ListerBase):
 
24
    """Pick a random wallpaper from the gnome backgrounds"""
 
25
    name       = 'default'
 
26
    can_delete = False
 
27
 
 
28
    def removeCurrent(self, filename, delete_file=True):
 
29
        """
 
30
        Deletes the background entry from backgrounds.xml
 
31
        and removes the file from the disk.
 
32
        """
 
33
        wpt = self.getWallpaperTag()
 
34
        for wp in wpt.childNodes:
 
35
            meta = self.decodeWP(wp)
 
36
            if wp.nodeType==1 and unicode(meta['filename'])==filename:
 
37
                wpt.removeChild(wp)
 
38
        if os.path.exists(filename):
 
39
            try:
 
40
                os.remove(filename)
 
41
            except OSError:
 
42
                sys.stderr.write(" ! Unable to remove file: %s\n" % filename)
 
43
 
 
44
    def addNew(self, filename, wp):
 
45
        """Adds a new background image file to the backgrounds.xml file."""
 
46
        wp['filename'] = unicode(filename)
 
47
        for ewp in self.getList():
 
48
            if unicode(ewp) == wp['filename']:
 
49
                print " & Already added %s, ignore settings for now." % wp['filename']
 
50
                return
 
51
        wpt = self.getWallpaperTag()
 
52
        wpt.appendChild(self.encodeWP(wp, self.openxml()))
 
53
        fh = open(gnome_settings, 'w')
 
54
        fh.write(self.openxml().toxml(encoding="utf-8"))
 
55
        fh.close()
 
56
 
 
57
    def getWallpaperTag(self):
 
58
        """Return specific wallpaper tag from wallpapers"""
 
59
        for child in self.openxml().childNodes:
 
60
            if child.nodeType == 1 and child.tagName == 'wallpapers':
 
61
                return child
 
62
        return None
 
63
 
 
64
    def getList(self):
 
65
        """Return a list of gnome backgrounds"""
 
66
        for wp in self.getWallpaperTag().childNodes:
 
67
            if wp.nodeType == 1 and wp.tagName == 'wallpaper':
 
68
                if wp.attributes.has_key('deleted') and wp.attributes['deleted']=='true':
 
69
                    continue
 
70
                item = self.decodeWP(wp)
 
71
                if item and os.path.lexists(item['filename']):
 
72
                    yield item['filename']
 
73
 
 
74
    def decodeWP(self, node):
 
75
        """Basic XML helper method for getting text values"""
 
76
        results = {}
 
77
        for value in node.childNodes:
 
78
            if value.nodeType==1:
 
79
                ndata = ''
 
80
                for val in value.childNodes:
 
81
                    if val.nodeType==3: # TEXT_NODE
 
82
                        ndata += val.data
 
83
                results[value.tagName] = ndata
 
84
        return results
 
85
 
 
86
    def encodeWP(self, wp, doc):
 
87
        """Basic XML helper method for setting text values"""
 
88
        node = doc.createElement('wallpaper')
 
89
        node.attributes['deleted'] = 'false'
 
90
        for key in wp.keys():
 
91
            tag = doc.createElement(key)
 
92
            tag.appendChild(doc.createTextNode(unicode(wp[key])))
 
93
            node.appendChild(tag)
 
94
 
 
95
        return node
 
96
 
 
97
    def openxml(self):
 
98
        """Return parsed gnome background xml document"""
 
99
        if not self._doc:
 
100
            self._doc = minidom.parse(gnome_settings)
 
101
        return self._doc
 
102