Pogger/src/FeedController.cpp

140 lines
2.8 KiB
C++
Raw Normal View History

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>
#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;
}
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;
}
}
}
int32
FeedController::_DownloadLoop(void* ignored)
{
thread_id sender;
Feed* feedBuffer = new Feed();
while (receive_data(&sender, (void*)feedBuffer, sizeof(Feed)) != 0) {
2021-01-11 11:41:46 -06:00
printf( "%s\n\n", feedBuffer->GetCachePath().String());
2021-01-11 18:08:15 -06:00
feedBuffer->Fetch();
2021-01-09 16:53:39 -06:00
BMessage* downloaded = new BMessage(kDownloadComplete);
downloaded->AddData("feeds", B_RAW_TYPE, feedBuffer, sizeof(Feed));
((App*)be_app)->MessageReceived(downloaded);
}
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-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-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-09 16:53:39 -06:00
delete(feed);
}
2021-01-11 18:08:15 -06:00
if (feedBuffer->IsAtom() || feedBuffer->IsRss())
for (int i = 0; i < entries.CountItems(); i++)
((Entry*)entries.ItemAt(i))->Filetize(outDir);
2021-01-09 16:53:39 -06:00
}
delete (feedBuffer);
return 0;
}
2021-01-11 11:41:46 -06:00