gnome-wallpaper-slideshow 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,173 @@
1
+ require 'nokogiri'
2
+ require 'pathname'
3
+
4
+ # Represents an XML file defining a wallpaper slideshow in GNOME
5
+ #
6
+ # @author Andrew Johnson
7
+ class GnomeWallpaperSlideshow
8
+ # Create a new GnomeWallpaperSlideshow
9
+ def initialize(&block)
10
+ instance_eval(&block) if block_given?
11
+ end
12
+
13
+ # Creates a new wallpaper slideshow
14
+ # @param [String] filename The path to the new file to create.
15
+ # Defaults to slideshow.xml in the current working directory.
16
+ # If the file already exists it will be overwritten
17
+ def create_new_slideshow(filename = "slideshow.xml")
18
+ @path = Pathname.new filename
19
+ end
20
+
21
+ # Loads an existing wallpaper slideshow
22
+ # @param [String] filename The path to the slideshow file
23
+ def load_slideshow(filename)
24
+ @path = Pathname.new filename
25
+ load_slideshow_xml
26
+
27
+ # Load the content from the XML doc
28
+ load_start_time
29
+ load_wallpapers
30
+ end
31
+
32
+ # Gets or sets the start time of the slideshow
33
+ # @param [Time] time The start time for the slideshow
34
+ # @return The current start time if one is not given
35
+ def start_time(time = nil)
36
+ return @start_time unless time
37
+
38
+ @start_time = time
39
+ end
40
+
41
+ # Gets or sets the path of the slideshow XML file
42
+ # @param [String] filename The path to the new location of the XML file
43
+ # @return The current path if one is not given
44
+ def path(filename = nil)
45
+ return @path unless filename
46
+
47
+ @path = Pathname.new filename
48
+ end
49
+
50
+ # Gets the list of wallpapers in this slideshow
51
+ # @return The list of wallpapers in the slideshow, or the empty list if none exist
52
+ def wallpapers
53
+ return @wallpapers = @wallpapers || []
54
+ end
55
+
56
+ # Adds a new wallpaper to the slideshow
57
+ # @param [GnomeWallpaperSlideshow::Wallpaper] wallpaper The wallpaper to add
58
+ def add_wallpaper(wallpaper)
59
+ wallpapers << wallpaper
60
+ end
61
+
62
+ # Removes a wallpaper from the slideshow
63
+ # @param [String] filename The filename of the wallpaper to remove
64
+ def remove_wallpaper(filename)
65
+ wallpapers.delete_if do |wallpaper|
66
+ wallpaper.filename == filename
67
+ end
68
+ end
69
+
70
+ # Generates and saves the slideshow XML to the current @path
71
+ def save_xml
72
+ return "" if start_time.nil? or wallpapers.none?
73
+
74
+ builder = Nokogiri::XML::Builder.new do |xml|
75
+ xml.background {
76
+ # The start time comes first
77
+ xml.starttime {
78
+ xml.year start_time.year
79
+ xml.month start_time.month
80
+ xml.day start_time.day
81
+ xml.hour start_time.hour
82
+ xml.minute start_time.min
83
+ xml.second start_time.sec
84
+ }
85
+ # Transitions are in pairs of wallpapers
86
+ wallpapers.each_cons(2) do |transition|
87
+ # The static tag defines a wallpaper and how long it is displayed
88
+ xml.static {
89
+ xml.duration transition.first.duration
90
+ xml.file transition.first.filename
91
+ }
92
+ # The transition tag defines how long the transition between this and the next takes
93
+ xml.transition {
94
+ xml.duration transition.first.transition_time
95
+ xml.from transition.first.filename
96
+ xml.to transition.last.filename
97
+ }
98
+ end
99
+ # Now insert the last wallpaper and the transition back to the beginning
100
+ xml.static {
101
+ xml.duration wallpapers.last.duration
102
+ xml.file wallpapers.last.filename
103
+ }
104
+ xml.transition {
105
+ xml.duration wallpapers.last.transition_time
106
+ xml.from wallpapers.last.filename
107
+ xml.to wallpapers.first.filename
108
+ }
109
+ }
110
+ end
111
+
112
+ write_xml builder.to_xml
113
+ end
114
+
115
+ private
116
+
117
+ # Writes an XML string to a file
118
+ # @param [String] xml_string The XML to write out
119
+ def write_xml(xml_string)
120
+ File.open(self.path, "w") do |slideshow_file|
121
+ slideshow_file.write xml_string
122
+ end
123
+ end
124
+
125
+ # Loads a wallpaper slideshow XML file
126
+ def load_slideshow_xml
127
+ File.open(self.path, "r") do |slideshow_file|
128
+ # We want strict parsing to ensure we are given a valid XML file
129
+ @xml_doc = Nokogiri::XML(slideshow_file) do |config|
130
+ config.strict
131
+ end
132
+ end
133
+ end
134
+
135
+ # Loads the start time from the XML file
136
+ def load_start_time
137
+ year = starttime_xpath_query "year"
138
+ month = starttime_xpath_query "month"
139
+ day = starttime_xpath_query "day"
140
+ hour = starttime_xpath_query "hour"
141
+ minute = starttime_xpath_query "minute"
142
+ second = starttime_xpath_query "second"
143
+
144
+ start_time Time.local year, month, day, hour, minute, second
145
+ end
146
+
147
+ # Load the wallpapers and transitions from the XML file
148
+ def load_wallpapers
149
+ files = wallpaper_xpath_query "//static/file"
150
+ durations = wallpaper_xpath_query "//static/duration"
151
+ transitions = wallpaper_xpath_query "//transition/duration"
152
+
153
+ @wallpapers = files.zip(durations,transitions).map do |file, duration, transition|
154
+ GnomeWallpaperSlideshow::Wallpaper.new file, duration, transition
155
+ end
156
+ end
157
+
158
+ # Performs an XPath query for wallpapers
159
+ # @param [String] query The XPath query to perform
160
+ def wallpaper_xpath_query(query)
161
+ @xml_doc.xpath(query).children.map do |child|
162
+ child.content
163
+ end
164
+ end
165
+
166
+ # Performs an XPath query for an element of the starttime
167
+ # @param [String] field The starttime field to extract
168
+ def starttime_xpath_query(field)
169
+ @xml_doc.at_xpath("//starttime/#{field}").children.first.content
170
+ end
171
+ end
172
+
173
+ require 'gnome-wallpaper-slideshow/wallpaper'
@@ -0,0 +1,15 @@
1
+ # Represents a wallpaper entry in a slideshow
2
+ class GnomeWallpaperSlideshow::Wallpaper
3
+ attr_accessor :filename, :duration, :transition_time
4
+
5
+ # Create a new wallpaper
6
+ # @param [String] filename The path to the image file
7
+ # @param [Float] duration The length of time this wallpaper will be displayed in seconds
8
+ # @param [Float] transition_time The amount of time spent transitioning
9
+ # to the next wallpaper in seconds
10
+ def initialize(filename, duration = 1795.0, transition_time = 5.0)
11
+ @filename = filename
12
+ @duration = duration
13
+ @transition_time = transition_time
14
+ end
15
+ end
@@ -0,0 +1,58 @@
1
+ require 'test/unit'
2
+ require 'gnome-wallpaper-slideshow'
3
+
4
+ class GnomeWallpaperSlideshowTest < Test::Unit::TestCase
5
+ def test_create_new_slideshow
6
+ slideshow = GnomeWallpaperSlideshow.new do
7
+ create_new_slideshow "xml_slideshow.xml"
8
+ end
9
+
10
+ path = Pathname.new "xml_slideshow.xml"
11
+ assert_equal path, slideshow.path
12
+ end
13
+
14
+ def test_create_new_slideshow_default_arg
15
+ slideshow = GnomeWallpaperSlideshow.new do
16
+ create_new_slideshow
17
+ end
18
+
19
+ path = Pathname.new "slideshow.xml"
20
+ assert_equal path, slideshow.path
21
+ end
22
+
23
+ def test_start_time
24
+ slideshow = GnomeWallpaperSlideshow.new
25
+
26
+ time = Time.local 2009,8,4,0,0,0
27
+
28
+ assert_equal time, slideshow.start_time(time) # Test setting the time
29
+ assert_equal time, slideshow.start_time # Test getting the time
30
+ end
31
+
32
+ def test_path
33
+ slideshow = GnomeWallpaperSlideshow.new
34
+
35
+ path = Pathname.new "xml_slideshow.xml"
36
+ assert_equal path, slideshow.path(path) # Test setting the path
37
+ assert_equal path, slideshow.path # Test getting the path
38
+ end
39
+
40
+ def test_wallpapers
41
+ slideshow = GnomeWallpaperSlideshow.new
42
+ wallpaper_one = GnomeWallpaperSlideshow::Wallpaper.new "test.jpg", 100, 5
43
+ wallpaper_two = GnomeWallpaperSlideshow::Wallpaper.new "other.jpg", 500, 3
44
+
45
+ assert_equal [], slideshow.wallpapers # Test handling of no wallpapers
46
+
47
+ # Test adding wallpapers
48
+ slideshow.add_wallpaper wallpaper_one
49
+ slideshow.add_wallpaper wallpaper_two
50
+
51
+ assert_equal [wallpaper_one, wallpaper_two], slideshow.wallpapers
52
+
53
+ # Test removing wallpapers
54
+ slideshow.remove_wallpaper wallpaper_one.filename
55
+
56
+ assert_equal [wallpaper_two], slideshow.wallpapers
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gnome-wallpaper-slideshow
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Johnson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.6
30
+ description: A gem for creating and interacting with the XML files that define wallpaper
31
+ slideshows in GNOME
32
+ email: andrew@andrewjamesjohnson.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/gnome-wallpaper-slideshow.rb
38
+ - lib/gnome-wallpaper-slideshow/wallpaper.rb
39
+ - Rakefile
40
+ - test/test-gnome-wallpaper-slideshow.rb
41
+ homepage: https://github.com/ajsquared/gnome-wallpaper-slideshow
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.23
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Gnome Wallpaper Slideshow
65
+ test_files:
66
+ - test/test-gnome-wallpaper-slideshow.rb
67
+ has_rdoc: