# Licensed under the MIT license
### LICENSE #####################################################################
# Copyright (c) 2008 Christoph Budzinski
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#################################################################################
#
# Description:
# ------------
# Grabs the Shoes mailing list feed from gmane.org and displays it in a Shoes app.
# Click on a topic to see the email's body and any replies to it. Click on the text
# of an email and a webbrowser will open with a link to the topic on gmane
#
# Status:
# -------
# Working, but the RSS feed on gmane.org is limited to 21 items, haven't
# found a way around that yet

require 'rexml/document'
require 'open-uri'

Shoes.app do
  background "#201D1D"
  title "Hotshoes - Hot Shoes News", :stroke => white
  @doc = REXML::Document.new open("http://rss.gmane.org/messages/complete/gmane.comp.lib.shoes").read
  stack do
    @messages = []
    @doc.elements.each("rdf:RDF/item") do |item|
      @messages << {:title => item.elements["title"].text.gsub(/^Re\:\s/, ""),
                    :date => item.elements["dc:date"].text.gsub("T", " "),
                    :creator => item.elements["dc:creator"].text,
                    :description => item.elements["description"].text,
                    :link => item.elements["link"].text}
    end
    
    groups = Hash.new { |h,k| h[k] = [] }
    @messages.each do |msg|
      groups[msg[:title]] << msg
    end
    groups.each do |title, group|
      groups[title] = group.sort_by { |msg| msg[:date] }
    end
    @groups = (groups.values.sort_by { |msgs| msgs.first[:date] }).reverse
    
    @remsgs = []
    
    @groups.each_with_index do |item, count|
      stack(:margin_left => 15, :margin_right => 30, :margin_top => 15) do
        background black
        stack do
          background "#464646".."#000000"
          flow do
            para item[0][:date], :stroke => orange
            para item[0][:creator], :stroke => red
          end
          para item[0][:title], :stroke => white
        end
        
        click do
          @remsgs.each do |msg|
            if msg[:group] == count
              msg[:stack].toggle
            end
          end
        end
        
        item.length.times do |msgcount|
          @remsgs << {:group => count, :stack => (stack(:margin_left => 15, :margin_right => 15, :margin_top => 15) do
            background white
            stack(:margin_left => 15, :margin_right => 15, :margin_top => 15) do
              flow do
                para item[msgcount][:creator], :stroke => red
                para " - "
                para item[msgcount][:date], :stroke => orange
              end
              para item[msgcount][:description]
              click do
                visit(item[msgcount][:link])
              end
            end
          end).hide }
        end
      end
    end
  end
end