<?xml version="1.0" encoding="UTF-8"?>

  <display id="win" anchor="ne"
           window-flags="sticky, below">

  <meta author="Robert Pastierovic"
        version="1.1"
        name="Quote of the Day"
        category="Misc"
        description="Desklet that displays quote for each day of the year"
        preview="quote.png"/>

  <!--  Retrieve the date/time control -->
  <control id="time" interface="ITime:9y703dqtfnv4w373caserz68r"/>

  
   <frame id="quoteframe" width="147pt">
    <group id="displaygroup" relative-to="quoteframe,y" width="100%">
     <image id="bg_image" image-width="100%" uri=""/>

      <group id="quotegroupsh" relative-to="displaygroup,y" anchor="n" x="49.5%" y="-1pt" width="95%">
        <label id="quote2" font="Sans Bold 10" color="#a0a0a0" wrap-at="95%"/>
      </group>

      <group id="quotegroup" relative-to="displaygroup,y" anchor="n" x="50%" width="95%">
        <label id="quote" font="Sans Bold 10" color="#a0a0a0" wrap-at="95%"/>
      </group>

      <group id="qauthorgroupsh" relative-to="quotegroup,y" anchor="ne" x="47%" y="4.5pt">
       <label id="qauthor2" font="Sans Bold 10" color="#a0a0a0"/>
      </group>

      <group id="qauthorgroup" relative-to="quotegroup,y" anchor="ne" x="47.5%" y="5pt">
       <label id="qauthor" font="Sans Bold 10" color="#a0a0a0"/>
      </group>

    </group>
   </frame>
   
  <prefs callback="prefs_cb">
    <page label="Image">
      <title label="Background"/>
        <boolean label="Show Background Image" bind="background_image_visible"
                 help="Show the background image?"/>
        <uri id="bg_image_uri" label="Background Image:" bind="bg_image"/>
        <color id="bg_color_color" label="Background color" bind="bg_color"/>
      <title label="Frame"/>
        <color label="Frame color:" bind="frame_color"/>
    </page>
    
    <page label="Text">
      <title label="Fonts"/>
        <font label="Quote Font:" bind="quote_font"/>
        <color label="Font Color:" bind="quote_font_color"/>
        <font label="Author Font:" bind="qauthor_font"/>
        <color label="Font Color:" bind="qauthor_font_color"/>
      <title label="Text Shadow"/>
        <color id="shadow_color" label="Shadow Color:" bind="quote2_font_color"/>
    </page>

    <page label="Other">
     <float label="Desklet Width:" bind="size" min="100" max="500" help="The width of the desklet."/>
     <enum label="Quote change interval:" bind="interval">
       <item label="hourly" value="hourly"/>
       <item label="twice a day" value="twice"/>
       <item label="daily" value="daily"/>
     </enum>
     <enum label="Source database:" bind="database">
       <item label="Famous Quotes" value="0" />
       <item label="Bible Verses" value="1" />
     </enum>
    </page>

  </prefs>

  <!-- This loads the quotes data -->
  <script uri="quotes.script"/>

 <script>
    #<![CDATA[
    bg_color                = "#00000000"
    frame_color             = "#00000000"
    background_image_visible = False
    bg_image                 = "../../Additions/bg/glass.svg"
    quote_font               = "Serif Bold Italic 9"
    quote_font_color        = "#bbbbbb"
    quote2_font_color       = "#000000"
    qauthor_font             = "Sans Bold 8"
    qauthor_font_color      = "#a0a0a0"
    qauthor2_font_color      = "#000000"
    size                     = 175
    olddate                  = time.date
    interval                 = "daily"
    database                 = "0"

    #This defines behaviour of preferences keys
    def prefs_cb(key,value):
     
      # background image
      if (key == "background_image_visible"):
        Dsp.bg_image.uri              = bg_image
        Dsp.bg_image.visible          = value
        Dsp.bg_image.image_height     = Unit(100,PERCENT)
        Prefs.bg_image_uri.enabled    = value
        Prefs.bg_color_color.enabled = not value
        if (value):
          Dsp.displaygroup.bg_color     = "#00000000"
        else:
          Dsp.displaygroup.bg_color     = bg_color

      if (key == "bg_image"):
        if background_image_visible:
          Dsp.bg_image.uri              = bg_image
          Dsp.bg_image.visible          = True
          Dsp.bg_image.image_height     = Unit(100,PERCENT)
          Dsp.displaygroup.bg_color     = "#00000000"
        else:
          Dsp.bg_image.visible      = False
          Dsp.displaygroup.bg_color = bg_color

      if (key == "bg_color"):
        if not background_image_visible:
          Dsp.displaygroup.bg_color   = value
        
      if (key == "frame_color"):
        Dsp.quoteframe.color          = value

      if (key == "quote_font"):
        Dsp.quote.font = value
        Dsp.quote2.font = value

      if (key == "quote_font_color"):
        Dsp.quote.color = value

      if (key == "quote2_font_color"):
        Dsp.quote2.color = value
        Dsp.qauthor2.color = value

      if (key == "qauthor_font"):
        Dsp.qauthor.font = value
        Dsp.qauthor2.font = value
        
      if (key == "qauthor_font_color"):
        Dsp.qauthor.color = value

      if (key == "qauthor2_font_color"):
        Dsp.qauthor2.color = value

      if (key == "size"):
        Dsp.quoteframe.width = Unit(value, PT)
        Dsp.quote.font = "Serif Bold Italic 9"
        Dsp.quote.font = quote_font
        Dsp.quote2.font = "Serif Bold Italic 9"
        Dsp.quote2.font = quote_font

      if (key == "database"):
        force_quote()

    def show_quote(value):
     hh,mm,ss                 = time.time
     if (interval=="daily"):
       Dsp.quote.value = "  " + quote("tquote", database)
       Dsp.quote2.value = "  " + quote("tquote", database)
       Dsp.qauthor.value = "-- " + quote("tqauthor", database)
       Dsp.qauthor2.value = "-- " + quote("tqauthor", database)
     #change quote at given minute and second
     if (interval=="hourly") and (str(mm)=="1") and (str(ss)=="1"):
       Dsp.quote.value   = "  " + quote("tqrandom", database)
       Dsp.quote2.value   = "  " + quote("tqrandom", database)
       Dsp.qauthor.value = "-- " + quote("tqarandom", database)
       Dsp.qauthor2.value = "-- " + quote("tqarandom", database)
     if (interval=="twice") and ((str(hh)=="12") or (str(hh)=="0")) and (str(mm)=="1") and (str(ss)=="1"):
       Dsp.quote.value   = "  " + quote("tqrandom", database)
       Dsp.quote2.value   = "  " + quote("tqrandom", database)
       Dsp.qauthor.value = "-- " + quote("tqarandom", database)
       Dsp.qauthor2.value = "-- " + quote("tqarandom", database)

    def force_quote():
     Dsp.quote.value = "  " + quote("tquote", database)
     Dsp.quote2.value = "  " + quote("tquote", database)
     Dsp.qauthor.value = "-- " + quote("tqauthor", database)
     Dsp.qauthor2.value = "-- " + quote("tqauthor", database)

    #watch time
    time.bind("time", show_quote)
    show_quote(time.date)

  ]]>
 </script>
</display>
