Tuesday, June 14, 2011

Parsing a Slideshow XML file for the active wallpaper image.

Querying the sytem for information on the background will return the name of the *.xml file controlling the slideshow, if there is one running. tDom didn't like the code sample given below, but its easy enough to parse and strip out what we want on the Tcl side. The slideshow controller will correctly(?) determine which slide to show given the commencement date. So, next step, a bit of arithmatic!

set xml {<background>
  <starttime>
    <year>2009</year>
    <month>08</month>
    <day>04</day>
    <hour>00</hour>
    <minute>00</minute>
    <second>00</second>
  </starttime>
<!-- This animation will start at midnight. -->
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/cloud.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/cloud.jpg</from>
    <to>/usr/share/backgrounds/cosmos/comet.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/comet.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/comet.jpg</from>
    <to>/usr/share/backgrounds/cosmos/earth-horizon.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/earth-horizon.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/earth-horizon.jpg</from>
    <to>/usr/share/backgrounds/cosmos/blue-marble-west.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/blue-marble-west.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/blue-marble-west.jpg</from>
    <to>/usr/share/backgrounds/cosmos/galaxy-ngc3370.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/galaxy-ngc3370.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/galaxy-ngc3370.jpg</from>
    <to>/usr/share/backgrounds/cosmos/helix-nebula.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/helix-nebula.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/helix-nebula.jpg</from>
    <to>/usr/share/backgrounds/cosmos/jupiter.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/jupiter.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/jupiter.jpg</from>
    <to>/usr/share/backgrounds/cosmos/sombrero.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/sombrero.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/sombrero.jpg</from>
    <to>/usr/share/backgrounds/cosmos/whirlpool.jpg</to>
  </transition>
  <static>
    <duration>1795.0</duration>
    <file>/usr/share/backgrounds/cosmos/whirlpool.jpg</file>
  </static>
  <transition>
    <duration>5.0</duration>
    <from>/usr/share/backgrounds/cosmos/whirlpool.jpg</from>
    <to>/usr/share/backgrounds/cosmos/cloud.jpg</to>
  </transition>
</background>}


proc getImages {xml} {
    set pics ""
    foreach line $xml {
        if { [string first <file> $line ] ==-1 } { continue }
        lappend pics [string map [list <file> "" </file> ""] $line]
    }
    return $pics
}

No comments: