2021-01-09 16:53:39 -06:00
|
|
|
/*
|
|
|
|
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
|
|
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "FeedController.h"
|
|
|
|
|
|
|
|
#include <Message.h>
|
2021-01-13 14:52:09 -06:00
|
|
|
#include <Notification.h>
|
2021-01-09 16:53:39 -06:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
#include "App.h"
|
|
|
|
#include "AtomFeed.h"
|
|
|
|
#include "Entry.h"
|
|
|
|
#include "RssFeed.h"
|
|
|
|
|
|
|
|
|
|
|
|
FeedController::FeedController()
|
|
|
|
:
|
|
|
|
fDownloadThread(0),
|
|
|
|
fParseThread(0)
|
|
|
|
{
|
|
|
|
fDownloadThread = spawn_thread(_DownloadLoop, "here, eat this",
|
|
|
|
B_NORMAL_PRIORITY, NULL);
|
|
|
|
fParseThread = spawn_thread(_ParseLoop, "oki tnx nomnomnom",
|
|
|
|
B_NORMAL_PRIORITY, NULL);
|
|
|
|
resume_thread(fDownloadThread);
|
|
|
|
resume_thread(fParseThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FeedController::~FeedController()
|
|
|
|
{
|
|
|
|
kill_thread(fDownloadThread);
|
|
|
|
kill_thread(fParseThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
FeedController::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what)
|
|
|
|
{
|
|
|
|
case kEnqueueFeed:
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const void* data;
|
|
|
|
ssize_t size = sizeof(Feed);
|
|
|
|
|
|
|
|
while (msg->HasData("feeds", B_RAW_TYPE, i)) {
|
|
|
|
msg->FindData("feeds", B_RAW_TYPE, i, &data, &size);
|
|
|
|
send_data(fDownloadThread, msg->what, data, size);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-01-12 15:30:21 -06:00
|
|
|
case kUpdateSubscribed:
|
|
|
|
{
|
2021-01-13 20:23:21 -06:00
|
|
|
BList subFeeds = SubscribedFeeds();
|
|
|
|
for (int i = 0; i < subFeeds.CountItems(); i++) {
|
2021-01-12 15:30:21 -06:00
|
|
|
BMessage* getFeed = new BMessage(kEnqueueFeed);
|
2021-01-13 20:23:21 -06:00
|
|
|
getFeed->AddData("feeds", B_RAW_TYPE, subFeeds.ItemAt(i),
|
|
|
|
sizeof(Feed));
|
2021-01-12 15:30:21 -06:00
|
|
|
((App*)be_app)->MessageReceived(getFeed);
|
|
|
|
}
|
2021-01-13 20:23:21 -06:00
|
|
|
break;
|
2021-01-12 15:30:21 -06:00
|
|
|
}
|
2021-01-09 16:53:39 -06:00
|
|
|
case kClearQueue:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kDownloadComplete:
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const void* data;
|
|
|
|
ssize_t size = sizeof(Feed);
|
|
|
|
|
|
|
|
while (msg->HasData("feeds", B_RAW_TYPE, i)) {
|
|
|
|
msg->FindData("feeds", B_RAW_TYPE, i, &data, &size);
|
|
|
|
if (((Feed*)data)->IsUpdated() == true)
|
|
|
|
send_data(fParseThread, msg->what, data, size);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// BWindow::MessageReceived(msg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 20:23:21 -06:00
|
|
|
BList
|
|
|
|
FeedController::SubscribedFeeds()
|
|
|
|
{
|
|
|
|
BDirectory subDir("/boot/home/config/settings/Pogger/Subscriptions");
|
|
|
|
BEntry feedEntry;
|
|
|
|
BList feeds;
|
|
|
|
|
|
|
|
while (subDir.GetNextEntry(&feedEntry) == B_OK)
|
|
|
|
feeds.AddItem(new Feed(feedEntry));
|
|
|
|
return feeds;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-09 16:53:39 -06:00
|
|
|
int32
|
|
|
|
FeedController::_DownloadLoop(void* ignored)
|
|
|
|
{
|
|
|
|
thread_id sender;
|
|
|
|
Feed* feedBuffer = new Feed();
|
|
|
|
|
|
|
|
while (receive_data(&sender, (void*)feedBuffer, sizeof(Feed)) != 0) {
|
2021-01-12 15:30:21 -06:00
|
|
|
printf( "Downloading feed from %s...\n",
|
|
|
|
feedBuffer->GetXmlUrl().UrlString().String());
|
|
|
|
|
2021-01-13 17:28:07 -06:00
|
|
|
if (feedBuffer->Fetch()) {
|
|
|
|
BMessage* downloaded = new BMessage(kDownloadComplete);
|
|
|
|
downloaded->AddData("feeds", B_RAW_TYPE, feedBuffer, sizeof(Feed));
|
|
|
|
((App*)be_app)->MessageReceived(downloaded);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BMessage* failure = new BMessage(kDownloadFail);
|
|
|
|
failure->AddString("feed_url", feedBuffer->GetXmlUrl().UrlString());
|
|
|
|
((App*)be_app)->MessageReceived(failure);
|
|
|
|
}
|
2021-01-09 16:53:39 -06:00
|
|
|
}
|
|
|
|
delete(feedBuffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int32
|
|
|
|
FeedController::_ParseLoop(void* ignored)
|
|
|
|
{
|
|
|
|
thread_id sender;
|
|
|
|
Feed* feedBuffer = new Feed();
|
|
|
|
|
|
|
|
while (receive_data(&sender, (void*)feedBuffer, sizeof(Feed)) != 0) {
|
|
|
|
BList entries;
|
2021-01-13 17:28:07 -06:00
|
|
|
BString feedTitle;
|
|
|
|
BUrl feedUrl = feedBuffer->GetXmlUrl();
|
2021-01-11 18:08:15 -06:00
|
|
|
BDirectory outDir = BDirectory(((App*)be_app)->cfg->outDir);
|
2021-01-09 16:53:39 -06:00
|
|
|
|
|
|
|
if (feedBuffer->IsAtom()) {
|
|
|
|
AtomFeed* feed = (AtomFeed*)malloc(sizeof(AtomFeed));
|
|
|
|
feed = new AtomFeed(feedBuffer);
|
|
|
|
feed->Parse();
|
2021-01-11 18:08:15 -06:00
|
|
|
entries = feed->GetNewEntries();
|
2021-01-13 17:28:07 -06:00
|
|
|
feedTitle = feed->GetTitle();
|
2021-01-09 16:53:39 -06:00
|
|
|
delete(feed);
|
|
|
|
}
|
2021-01-11 18:08:15 -06:00
|
|
|
else if (feedBuffer->IsRss()) {
|
2021-01-09 16:53:39 -06:00
|
|
|
RssFeed* feed = (RssFeed*)malloc(sizeof(RssFeed));
|
|
|
|
feed = new RssFeed(feedBuffer);
|
|
|
|
feed->Parse();
|
2021-01-11 18:08:15 -06:00
|
|
|
entries = feed->GetNewEntries();
|
2021-01-13 17:28:07 -06:00
|
|
|
feedTitle = feed->GetTitle();
|
2021-01-09 16:53:39 -06:00
|
|
|
delete(feed);
|
|
|
|
}
|
|
|
|
|
2021-01-13 17:28:07 -06:00
|
|
|
|
|
|
|
if (feedBuffer->IsAtom() || feedBuffer->IsRss()) {
|
2021-01-11 18:08:15 -06:00
|
|
|
for (int i = 0; i < entries.CountItems(); i++)
|
|
|
|
((Entry*)entries.ItemAt(i))->Filetize(outDir);
|
2021-01-13 14:52:09 -06:00
|
|
|
|
2021-01-13 17:28:07 -06:00
|
|
|
BMessage* complete = new BMessage(kParseComplete);
|
|
|
|
complete->AddString("feed_name", feedTitle);
|
|
|
|
complete->AddInt32("entry_count", entries.CountItems());
|
|
|
|
((App*)be_app)->MessageReceived(complete);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
BMessage* failure = new BMessage(kParseFail);
|
|
|
|
failure->AddString("feed_url", feedUrl.UrlString());
|
|
|
|
((App*)be_app)->MessageReceived(failure);
|
2021-01-13 14:52:09 -06:00
|
|
|
}
|
2021-01-09 16:53:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
delete (feedBuffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-11 11:41:46 -06:00
|
|
|
|
|
|
|
|