------------------------------------------------------------------------------ -- VCLua Rss reader demo -- (c)2006 hi-project ltd. ------------------------------------------------------------------------------ require "vcl" local lom = require "lxp.lom" local http = require "socket.http" local file = require "file" -- begin RSS parser -- local RSS,index,maxindex = nil,0,0 local function parseRSS(t,level) level = level or 0 table.foreach(t, function(k,v) if type(v)=="table" then parseRSS(v,level+1) else if k=="tag" and v=="item" then index = index + 1 end if k=="tag" and v~="item" then if not RSS[level+index-1] then RSS[level+index-1]={} end maxindex = math.max(maxindex,level+index-1) RSS[level+index-1][v]=t[1] end end end) end function getRSSitem(index) local tt=nil table.foreachi(RSS, function(i,d) if type(d)=="table" and i==index then tt = d return true end end) return tt end function readRSS(buf) RSS, index,maxindex = {},0,0 local t = lom.parse(buf) if t then parseRSS(t) end end -- end RSS parser -- -- begin rssForm definition -- local rssForm = VCL.Form("rssForm") rssForm._ = { left=100, top=100, height=480, width=640, position="poDesktopCenter", caption="RSS reader sample application", } rssForm.rssStatusBar = VCL.StatusBar(rssForm,"rssStatusBar") rssForm.rssStatusBar.ondblclick = "onStatusBarDblClick" rssForm.rssStatusBar:Add(10, "") -- last rssForm.rssStatusBar:Insert(1, 180, "") rssForm.rssStatusBar:Insert(1, 180, "") rssForm.rssStatusBar:Insert(1, 75, "")-- first rssForm.topPanel = VCL.Panel(rssForm,"topPanel") rssForm.topPanel._ = { caption = "", align="alTop", height = 30, bevelinner="bvLowered", bevelouter="bvNone"} rssForm.lblRSSname = VCL.Label(rssForm.topPanel, "lblRSSname") rssForm.lblRSSname._ = { left=7, top= 7, caption="Url:" } rssForm.edRSSname = VCL.Edit(rssForm.topPanel, "edRSSname") rssForm.edRSSname._ = { left=30, top= 4, width=65, text="http://www.lua.org/news.rss", anchors="[akTop,akLeft,akRight]"} rssForm.btnRSSGet = VCL.SpeedButton(rssForm.topPanel, "btnRSSGet") rssForm.btnRSSGet._ = { flat="true", left=100, top= 4, width=80, onclick = "btnRSSClick", caption = "Get RSS",anchors="[akTop,akRight]"} rssForm.bottomPanel = VCL.Panel(rssForm,"bottomPanel") rssForm.bottomPanel._ = { caption = "", align="alClient", bevelinner="bvNone", bevelouter="bvLowered" } rssForm.bottomPanel.Font._ = {name="Courier", size=14} rssForm.bottomPanel.rssListBox = VCL.ListBox(rssForm.bottomPanel,"rssListBox") rssForm.bottomPanel.rssListBox._ = { align="alClient", borderstyle="bsNone", onclick="listClick", ondblClick="listDblClick"} -- end rssForm definition -- -- begin rssForm events -- local mainlink = nil function btnRSSClick(Sender) local b, c, h = http.request(rssForm.edRSSname.text) if c==200 then readRSS(b) if RSS and maxindex>0 then showtitle() showrssitems() rssForm.bottomPanel.rssListBox:SetFocus() end end end function listClick(Sender) rssForm.bottomPanel.rssListBox.hint = "" rssForm.bottomPanel.rssListBox.showhint = "false" local i = rssForm.bottomPanel.rssListBox:Index() if i>0 then local t = getRSSitem(i+1) if t then rssForm.bottomPanel.rssListBox.hint = "DESC:" ..tostring(t.description) .. "LINK: (doubleclick)\n" .. tostring(t.link).."\nDATE:"..tostring(t.pubDate) rssForm.bottomPanel.rssListBox.showhint = "true" end end end function listDblClick(Sender) local i = rssForm.bottomPanel.rssListBox:Index() if i>0 then local t = getRSSitem(i+1) if t and t.link then os.execute("explorer".. " \""..t.link.."\"") end end end function onStatusBarDblClick(Sender) if mainlink then os.execute("explorer".. " \""..mainlink.."\"") end end -- end rssForm events -- function showtitle() mainlink = nil local t = getRSSitem(1) if t then rssForm.rssStatusBar:Update(1,t.title) rssForm.rssStatusBar:Update(2,t.link) rssForm.rssStatusBar:Update(3,t.description) rssForm.rssStatusBar:Update(4,t.lastBuildDate) mainlink = t.link end end function showrssitems() rssForm.bottomPanel.rssListBox:Clear() for i=2,maxindex do local t = getRSSitem(i) if t then rssForm.bottomPanel.rssListBox:Add(t.title) end end end -- mainloop rssForm:ShowModal() rssForm:Free()