; Module for the right side of the application
; Hollywood-like object programming :)
IvoR = {}
IvoR.parsers = CreateList()
Function IvoR:Load(feedid$)
Local err_code, xml$, _len = ?FileToString(p_GetXMLname(feedid$))
; když máme chybu, tak rovnou končíme
If err_code <> #ERR_NONE
mui.Set("status", "Contents", "\27b" .. GetErrorName(err_code))
Return()
EndIf
;č dále je třeba pozastavit případný parser "z minula"
local lastP = GetItem(IvoR.parsers, -1)
If Not isNil(lastP) Then lastP:Stop()
self.feedid = feedid$
self.link = feedid$
;č teprve teď uklízíme
Ivor:Clear()
; TODO: load icon
If AllowImages
self.ImageLoaded = False
self:LoadImage()
If Not self.ImageLoaded Then mui.Set("feedimage", "Brush", 1)
EndIf
;č a vytahujeme seznam přečteného
Local t = fd_Get(feedid$, "guids")
If GetType(t) = #TABLE
self.oldguids = t
Else
self.oldguids = {}
EndIf
;č na konci tohle nahradí už "staré" guids
;č Je to aby se staré, nejspíš i nedostupné zápisy nehromadily.
;č Funkce "pravé" strany budou rovnou psát do IvoR.guids
self.guids = {}
Local p = XMLParser.New({StartElement = StartElement, EndElement=EndElement, CharacterData = CharacterData})
InsertItem(Ivor.parsers, p)
DebugPrint("Created parser ", ListItems(Ivor.parsers))
p:setbase(feedid$)
p:Parse(xml$)
;Local lin, col, pos = p:pos()
;DebugPrint(lin, col, pos)
Local err_code = ?p:Close()
Local errorname = GetErrorName(err_code)
;DebugPrint(err_code, GetErrorName(err_code))
DebugPrint("Parser closed", ListItems(Ivor.parsers))
RemoveItem(Ivor.parsers)
;č moc se mně neptejte, ale zdá se,
;č že se vracejí odlišné chyby se stejným kódem
If errorname = "Error closing parser: unknown encoding!"
Ivor:Clear()
;č je to hodně nebezpečný, ale řekl bych, že spadl-li
;č parser kvůli kódování, tak nemohl žádný kólbek spustit
Local p = XMLParser.New({StartElement = StartElement, EndElement=EndElement, CharacterData = CharacterData})
InsertItem(Ivor.parsers, p)
DebugPrint("Created parser ", ListItems(Ivor.parsers))
p:setbase(feedid$)
;č Zoufalý pokus zobrazit aspoň něco
;č 1. Polski Portal Amigowy používá ISO-8859-2
;č 2. Ale spousta amigistů taky pochází z Polska
;č a budou nejspíš taky mít ISO-8859-2 v systému
;č 3. Polski Portal Amigowy je zahraněn!
xml$ = ConvertStr(xml$, #ENCODING_AMIGA, #ENCODING_UTF8)
p:setencoding("UTF-8")
p:Parse(xml$)
;Local lin, col, pos = p:pos()
;DebugPrint(lin, col, pos)
Local err_code = ?p:Close()
;Local errorname = GetErrorName(err_code)
;DebugPrint(err_code, GetErrorName(err_code))
DebugPrint("Parser closed", ListItems(Ivor.parsers))
RemoveItem(Ivor.parsers)
EndIf
;č zjednodušená kontrola místo adekvátního zpracování =]
If self.feedid = feedid$ Then fd_Set(feedid$, "guids", self.guids)
EndFunction
Function IvoR:ShowArticle(pos)
If pos >= 0
Local chosen_item = self.items_list[pos]
mui.Set("item_author", "Contents", chosen_item.author)
mui.Set("item_pubdate", "Contents", chosen_item.pubdate)
mui.Set("item_title", "Contents", tf_Label(chosen_item.title))
mui.Set("item_link", "Contents", chosen_item.link)
If StrLen(chosen_item.description) > StrLen(chosen_item.content)
mui.Set("textfield", "Text", tf_TextField(chosen_item.description))
Else
mui.Set("textfield", "Text", tf_TextField(chosen_item.content))
EndIf
If Not HaveItem(self.guids, chosen_item.guid) And chosen_item.guid <> ""
mui.DoMethod("articles", "Rename", pos,
chosen_item.title,
chosen_item.pubdate,
chosen_item.category)
self.guids[chosen_item.guid] = 1
EndIf
EndIf
EndFunction
Function IvoR:DownloadImage(str$)
If Not AllowImages Or self.ImageLoaded Then Return()
Local extension$ = "image"
If StrLen(str$) > 4
Switch LowerStr(RightStr(str$, 4))
Case "ilbm": FallThrough
Case ".lbm": extension$ = "ilbm"
Case ".png": extension$ = "png"
Case ".gif": extension$ = "gif"
Case "jpeg": FallThrough
Case ".jpg": extension$ = "jpg"
Case ".bmp": extension$ = "bmp"
EndSwitch
EndIf
Local url$ = p_reconstructURL(self.link, str$)
Local err_code, _empty_str$, count = ?DownloadFile(url$,
{File=p_GetLogoName(self.feedid, extension$), Adapter=#HTTP_ADAPTER,
Fail404=True, Encoded=FindStr(url$, "%", True) <> -1})
p_Replay(err_code, "Image logo is downloaded")
fd_Set(self.feedid, "imageformat", extension$)
self:LoadImage()
EndFunction
Function IvoR:LoadImage()
If Not AllowImages Or self.ImageLoaded Then Return()
Local extension$ = fd_Get(self.feedid, "imageformat")
If IsNil(extension$) Then Return()
Local filename$ = p_GetLogoName(self.feedid, extension$)
Local ret, t = IsPicture(filename$, {Loader="Inbuilt"})
If Not ret Then Return()
Local err_code, _id = ?LoadBrush(2, filename$,
{LoadAlpha = t.Alpha, LoadTransparency=t.Transparency})
If err_code <> #ERR_NONE
mui.Set("status", "Contents", "\27b" .. GetErrorName(err_code))
Return()
EndIf
If t.Height > 32 Then ScaleBrush(2, #KEEPASPRAT, 32)
mui.Set("feedimage", "Brush", 2)
self.ImageLoaded = True
EndFunction
Function IvoR:OpenLink(muid)
;č Vostuda, ale nemáme to nikde uloženo. Jen na formě =]
Local url$ = mui.Get(muid, "Contents")
If url$ Then OpenURL(url$)
EndFunction
Function IvoR:Clear()
mui.Set("channel_title", "Contents", "")
;mui.Set("channel_title", "ContextMenu", "(none)")
mui.Set("channel_description", "Contents", "")
;mui.Set("channel_description", "ContextMenu", "(none)")
mui.Set("channel_link", "Contents", "")
mui.DoMethod("articles", "Clear")
mui.Set("item_author", "Contents", "")
mui.Set("item_pubdate", "Contents", "")
mui.Set("item_title", "Contents", "")
mui.Set("item_link", "Contents", "")
mui.Set("textfield", "Text", "")
self.items_list = CreateList()
channel_level = True
current_element$ = Nil
current_item = {author="", title="", link="", content="",
pubdate="", category="", description="", guid=""}
EndFunction
Function StartElement(p, name$, attrs)
current_element$ = name$
If name$ = "item" Or name$ = "entry" Then channel_level = False
EndFunction
Function EndElement(p, name$)
current_element$ = nil
If name$ = "item" Or name$ = "entry"
Local preparse = ""
If current_item.guid <> ""
;č mezery v indexu zboří serializator
;č prázdný řetězec zboří MD5 kalkulačku
current_item.guid = MD5Str(current_item.guid)
If HaveItem(IvoR.oldguids, current_item.guid)
IvoR.guids[current_item.guid] = 1
Else
preparse = "\27b"
EndIf
EndIf
mui.DoMethod("articles", "Insert", "Bottom",
preparse .. current_item.title,
preparse .. current_item.pubdate,
preparse .. current_item.category)
InsertItem(IvoR.items_list, current_item)
CheckEvents()
current_item = {author="", title="", link="", content="",
pubdate="", category="", description="", guid=""}
EndIf
EndFunction
Function CharacterData(p, str$)
If channel_level
Switch current_element$
Case "title": mui.Set("channel_title", "Contents", tf_Label(str$))
Case "description":
mui.Set("channel_description", "Contents", tf_Label(str$))
Case "id": FallThrough
Case "link":
IvoR.link = StripStr(str$)
mui.Set("channel_link", "Contents", StripStr(str$))
;č url podle RSS specifikace musí být pouze u obrázků.
Case "url": IvoR:DownloadImage(StripStr(str$))
Case "logo": IvoR:DownloadImage(StripStr(str$))
EndSwitch
Else
current_item.guid = current_item.guid .. str$
Switch current_element$
Case "title":
current_item.title = tf_Label(str$)
;current_item.guid = current_item.guid .. "title:" .. str$
Case "dc:creator": FallThrough
Case "author": current_item.author = StripStr(str$)
;č číselné hodnoty se nějak špatně načítají.
;č fakt je nějaký problém v tom serializatoru
Case "guid":
If current_item.link = "" Then current_item.link = StripStr(str$)
;current_item.guid = current_item.guid .. "guid:" .. str$
Case "updated":
;current_item.guid = current_item.guid .. "updated:" .. str$
current_item.pubdate = StripStr(str$)
Case "pubDate": current_item.pubdate = StripStr(str$)
Case "summary": FallThrough
Case "atom:summary": FallThrough
Case "description":
current_item.description = current_item.description ..StripStr(str$)
Case "content": FallThrough
Case "content:encoded":
current_item.content = current_item.content ..StripStr(str$)
Case "id": FallThrough
Case "link":
current_item.link = StripStr(str$)
Case "category":
If current_item.category = ""
current_item.category = tf_Label(str$)
Else
current_item.category = current_item.category .. ", " .. tf_Label(str$)
EndIf
EndSwitch
EndIf
EndFunction