2
# Copyright 2009, Martin Owens.
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.
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.
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/>.
18
List wallpapers from gnome, skip self.
21
from gwc.base import ListerBase, add_lister
23
class gnomeLister(ListerBase):
24
"""Pick a random wallpaper from the gnome backgrounds"""
28
def removeCurrent(self, filename, delete_file=True):
30
Deletes the background entry from backgrounds.xml
31
and removes the file from the disk.
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:
38
if os.path.exists(filename):
42
sys.stderr.write(" ! Unable to remove file: %s\n" % filename)
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']
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"))
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':
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':
70
item = self.decodeWP(wp)
71
if item and os.path.lexists(item['filename']):
72
yield item['filename']
74
def decodeWP(self, node):
75
"""Basic XML helper method for getting text values"""
77
for value in node.childNodes:
80
for val in value.childNodes:
81
if val.nodeType==3: # TEXT_NODE
83
results[value.tagName] = ndata
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'
91
tag = doc.createElement(key)
92
tag.appendChild(doc.createTextNode(unicode(wp[key])))
98
"""Return parsed gnome background xml document"""
100
self._doc = minidom.parse(gnome_settings)