Class: GnomeWallpaperSlideshow

Inherits:
Object
  • Object
show all
Defined in:
lib/gnome-wallpaper-slideshow.rb

Overview

Represents an XML file defining a wallpaper slideshow in GNOME

Author:

  • Andrew Johnson

Defined Under Namespace

Classes: Wallpaper

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ GnomeWallpaperSlideshow

Create a new GnomeWallpaperSlideshow



9
10
11
# File 'lib/gnome-wallpaper-slideshow.rb', line 9

def initialize(&block)
  instance_eval(&block) if block_given?
end

Instance Method Details

#add_wallpaper(wallpaper) ⇒ Object

Adds a new wallpaper to the slideshow

Parameters:



58
59
60
# File 'lib/gnome-wallpaper-slideshow.rb', line 58

def add_wallpaper(wallpaper)
  wallpapers << wallpaper
end

#create_new_slideshow(filename = "slideshow.xml") ⇒ Object

Creates a new wallpaper slideshow

Parameters:

  • filename (String) (defaults to: "slideshow.xml")

    The path to the new file to create. Defaults to slideshow.xml in the current working directory. If the file already exists it will be overwritten



17
18
19
# File 'lib/gnome-wallpaper-slideshow.rb', line 17

def create_new_slideshow(filename = "slideshow.xml")
  @path = Pathname.new filename
end

#load_slideshow(filename) ⇒ Object

Loads an existing wallpaper slideshow

Parameters:

  • filename (String)

    The path to the slideshow file



23
24
25
26
27
28
29
30
# File 'lib/gnome-wallpaper-slideshow.rb', line 23

def load_slideshow(filename)
  @path = Pathname.new filename
  load_slideshow_xml

  # Load the content from the XML doc
  load_start_time
  load_wallpapers
end

#path(filename = nil) ⇒ Object

Gets or sets the path of the slideshow XML file

Parameters:

  • filename (String) (defaults to: nil)

    The path to the new location of the XML file

Returns:

  • The current path if one is not given



44
45
46
47
48
# File 'lib/gnome-wallpaper-slideshow.rb', line 44

def path(filename = nil)
  return @path unless filename

  @path = Pathname.new filename
end

#remove_wallpaper(filename) ⇒ Object

Removes a wallpaper from the slideshow

Parameters:

  • filename (String)

    The filename of the wallpaper to remove



64
65
66
67
68
# File 'lib/gnome-wallpaper-slideshow.rb', line 64

def remove_wallpaper(filename)
  wallpapers.delete_if do |wallpaper|
    wallpaper.filename == filename
  end
end

#save_xmlObject

Generates and saves the slideshow XML to the current @path



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
# File 'lib/gnome-wallpaper-slideshow.rb', line 71

def save_xml
  return "" if start_time.nil? or wallpapers.none?
  
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.background {
      # The start time comes first
      xml.starttime {
        xml.year start_time.year
        xml.month start_time.month
        xml.day start_time.day
        xml.hour start_time.hour
        xml.minute start_time.min
        xml.second start_time.sec
      }
      # Transitions are in pairs of wallpapers
      wallpapers.each_cons(2) do |transition|
        # The static tag defines a wallpaper and how long it is displayed
        xml.static {
          xml.duration transition.first.duration
          xml.file transition.first.filename
        }
        # The transition tag defines how long the transition between this and the next takes
        xml.transition {
          xml.duration transition.first.transition_time
          xml.from transition.first.filename
          xml.to transition.last.filename
        }
      end
      # Now insert the last wallpaper and the transition back to the beginning
      xml.static {
        xml.duration wallpapers.last.duration
        xml.file wallpapers.last.filename
      }
      xml.transition {
        xml.duration wallpapers.last.transition_time
        xml.from wallpapers.last.filename
        xml.to wallpapers.first.filename
      }
    }
  end

  write_xml builder.to_xml
end

#start_time(time = nil) ⇒ Object

Gets or sets the start time of the slideshow

Parameters:

  • time (Time) (defaults to: nil)

    The start time for the slideshow

Returns:

  • The current start time if one is not given



35
36
37
38
39
# File 'lib/gnome-wallpaper-slideshow.rb', line 35

def start_time(time = nil)
  return @start_time unless time

  @start_time = time
end

#wallpapersObject

Gets the list of wallpapers in this slideshow

Returns:

  • The list of wallpapers in the slideshow, or the empty list if none exist



52
53
54
# File 'lib/gnome-wallpaper-slideshow.rb', line 52

def wallpapers
  return @wallpapers = @wallpapers || []
end