Pogger/src/Feed.cpp

250 lines
3.3 KiB
C++
Raw Normal View History

2020-12-30 22:07:54 -06:00
/*
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "Feed.h"
#include <tinyxml2.h>
2020-12-30 22:07:54 -06:00
#include "App.h"
#include "Entry.h"
#include "Config.h"
#include "Util.h"
2020-12-30 22:07:54 -06:00
2021-01-11 18:08:15 -06:00
Feed::Feed(BUrl xml, BString path)
: Feed()
{
xmlUrl = xml;
cachePath = path;
}
Feed::Feed(BUrl xml)
: Feed()
{
2021-01-11 18:08:15 -06:00
xmlUrl = xml;
BString cache("/boot/home/config/cache/Pogger/");
cache.Append(urlToFilename(xmlUrl));
SetCachePath(cache);
}
2020-12-30 22:07:54 -06:00
2021-01-09 16:53:39 -06:00
Feed::Feed(Feed* feed)
: Feed()
{
2021-01-11 18:08:15 -06:00
SetTitle(feed->GetTitle());
SetXmlUrl(feed->GetXmlUrl());
2021-01-09 16:53:39 -06:00
}
2020-12-30 22:07:54 -06:00
Feed::Feed()
2021-01-11 18:08:15 -06:00
:
title(BString("Untitled Feed"))
{
2021-01-11 18:08:15 -06:00
lastDate = BDateTime::CurrentDateTime(B_LOCAL_TIME);
}
2020-08-14 14:00:07 -05:00
void
2020-12-30 22:07:54 -06:00
Feed::Parse()
2020-08-14 14:00:07 -05:00
{
2021-01-11 18:08:15 -06:00
BFile feedFile = BFile(cachePath, B_READ_ONLY);
2020-12-30 22:07:54 -06:00
time_t tt_lastDate = 0;
2020-08-14 14:00:07 -05:00
2021-01-11 18:08:15 -06:00
feedFile.ReadAttr("LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t));
2020-12-30 22:07:54 -06:00
2021-01-11 18:08:15 -06:00
if (tt_lastDate > 0)
lastDate.SetTime_t(tt_lastDate);
2020-08-14 14:00:07 -05:00
}
// Download a remote feed's XML to the cache path.
BString
2021-01-11 18:08:15 -06:00
Feed::Fetch()
{
2021-01-11 18:08:15 -06:00
BFile cacheFile = BFile(cachePath, B_READ_WRITE | B_CREATE_FILE);
2021-01-11 18:08:15 -06:00
cacheFile.ReadAttrString("LastHash", &lastHash);
2021-01-11 18:08:15 -06:00
fetch(xmlUrl, &cacheFile, &hash, 30);
cacheFile.WriteAttrString("LastHash", &hash);
2021-01-11 18:08:15 -06:00
if (hash == lastHash)
updated = false;
2020-07-13 12:31:52 -05:00
fetched = true;
2021-01-11 18:08:15 -06:00
return cachePath;
}
2020-12-30 22:07:54 -06:00
bool
2021-01-11 18:08:15 -06:00
Feed::IsRss()
{
tinyxml2::XMLDocument xml;
2021-01-11 18:08:15 -06:00
xml.LoadFile(cachePath.String());
2020-12-30 22:07:54 -06:00
if (xml.FirstChildElement("rss"))
return true;
return false;
}
2020-12-30 22:07:54 -06:00
bool
2021-01-11 18:08:15 -06:00
Feed::IsAtom()
{
tinyxml2::XMLDocument xml;
2021-01-11 18:08:15 -06:00
xml.LoadFile(cachePath.String());
2020-12-30 22:07:54 -06:00
if (xml.FirstChildElement("feed"))
return true;
return false;
}
2021-01-11 18:08:15 -06:00
bool
Feed::IsUpdated()
{
2021-01-11 18:08:15 -06:00
return lastHash != hash;
}
2021-01-09 16:53:39 -06:00
// Count the amount of siblings to an element of given type name
int
2020-12-30 22:07:54 -06:00
Feed::xmlCountSiblings (tinyxml2::XMLElement* xsibling, const char* sibling_name)
{
int count = 0;
2020-12-30 22:07:54 -06:00
while (xsibling) {
count++;
xsibling = xsibling->NextSiblingElement(sibling_name);
}
return count;
}
2020-08-14 01:59:15 -05:00
bool
2020-12-30 22:07:54 -06:00
Feed::AddEntry (Entry* newEntry)
2020-08-14 01:59:15 -05:00
{
Config* cfg = ((App*)be_app)->cfg;
2020-08-14 01:59:15 -05:00
2020-12-30 22:07:54 -06:00
entries.AddItem(newEntry);
2020-08-14 01:59:15 -05:00
return true;
}
2020-12-30 22:07:54 -06:00
BList
Feed::GetEntries()
{
return entries;
}
2021-01-11 18:08:15 -06:00
BList
Feed::GetNewEntries()
{
BList newEntries;
for (int i = 0; i < entries.CountItems(); i++) {
Entry* entry = ((Entry*)entries.ItemAt(i));
if (entry->GetDate() > lastDate)
newEntries.AddItem(entry);
}
return newEntries;
}
2020-12-30 22:07:54 -06:00
bool
Feed::SetTitle(const char* titleStr)
{
if (titleStr != NULL)
title = BString(titleStr);
else return false;
return true;
}
2020-12-30 22:07:54 -06:00
bool
Feed::SetTitle(tinyxml2::XMLElement* elem)
{
if (elem != NULL)
return SetTitle(elem->GetText());
else return false;
}
2020-12-30 22:07:54 -06:00
BString
Feed::GetTitle()
{
return title;
}
bool
2021-01-11 18:08:15 -06:00
Feed::SetXmlUrl(BUrl newUrl)
2020-12-30 22:07:54 -06:00
{
2021-01-11 18:08:15 -06:00
xmlUrl = newUrl;
return true;
}
2020-12-30 22:07:54 -06:00
2021-01-11 18:08:15 -06:00
BUrl
Feed::GetXmlUrl()
2020-12-30 22:07:54 -06:00
{
2021-01-11 18:08:15 -06:00
return xmlUrl;
2020-12-30 22:07:54 -06:00
}
2020-12-30 22:07:54 -06:00
bool
2021-01-11 18:08:15 -06:00
Feed::SetCachePath(BString path)
2020-12-30 22:07:54 -06:00
{
2021-01-11 18:08:15 -06:00
cachePath = path;
return true;
}
2020-12-30 22:07:54 -06:00
BString
2021-01-11 18:08:15 -06:00
Feed::GetCachePath()
2020-12-30 22:07:54 -06:00
{
2021-01-11 18:08:15 -06:00
return cachePath;
2020-12-30 22:07:54 -06:00
}
2021-01-11 18:08:15 -06:00
// Set the latest date given by feed (from entry or from root)
bool
Feed::SetDate(BDateTime newDate)
2021-01-09 16:53:39 -06:00
{
2021-01-11 18:08:15 -06:00
if (newDate == NULL)
return false;
date = newDate;
return true;
2021-01-09 16:53:39 -06:00
}
2020-12-30 22:07:54 -06:00
bool
Feed::SetDate(const char* dateCStr)
{
if (dateCStr == NULL)
return false;
2021-01-11 18:08:15 -06:00
SetDate(feedDateToBDate(dateCStr));
return true;
}
2020-12-30 22:07:54 -06:00
bool
Feed::SetDate(tinyxml2::XMLElement* elem)
{
if (elem != NULL)
return SetDate(elem->GetText());
else return false;
}
2020-12-30 22:07:54 -06:00
BDateTime
Feed::GetDate()
{
return date;
}