Move from global 'main_cfg' to be_app->cfg

This commit is contained in:
Jaidyn Ann 2020-11-16 20:32:58 -06:00
parent d6975dbc98
commit 093c90aed2
12 changed files with 75 additions and 89 deletions

View File

@ -20,8 +20,8 @@ main ( int argc, char** argv )
usageMsg.ReplaceAll("%app%", "Pogger");
feedMimeType();
main_cfg = new Config;
main_cfg->Load();
app->cfg = new Config;
app->cfg->Load();
if ( argc == 1 )
@ -30,8 +30,8 @@ main ( int argc, char** argv )
cliStart( argc, argv );
if ( main_cfg->will_save == true )
main_cfg->Save();
if ( app->cfg->will_save == true )
app->cfg->Save();
return 0;
}
@ -41,8 +41,8 @@ main ( int argc, char** argv )
void
cliStart ( int argc, char** argv )
{
invocation( argc, argv, &main_cfg );
main_cfg->targetFeeds.DoForEach( &processFeed );
invocation( argc, argv );
((App*)be_app)->cfg->targetFeeds.DoForEach( &processFeed );
}
App::App ( )
@ -55,8 +55,6 @@ App::App ( )
// ----------------------------------------------------------------------------
Config* main_cfg;
const char* configPath = "/boot/home/config/settings/Pogger/";
BString usageMsg =
"Usage: %app% [-hvDus] [-tT datetime] [-cCO path] \n"

View File

@ -3,12 +3,14 @@
#include <SupportDefs.h>
#include <Application.h>
#include "Config.h"
class App : public BApplication
{
public:
App(void);
Config* cfg;
};
// ----------------------------------------------------------------------------
@ -18,8 +20,6 @@ void cliStart ( int, char** );
// ----------------------------------------------------------------------------
extern Config* main_cfg;
extern const char* configPath;
extern BString usageMsg;

View File

@ -1,5 +1,6 @@
#include <tinyxml2.h>
#include "Entry.h"
#include "App.h"
#include "Config.h"
#include "Util.h"
#include "AtomFeed.h"
@ -11,30 +12,27 @@ AtomFeed::AtomFeed ( )
homeUrl = BString("");
xmlUrl = BString("");
filePath = BString("");
outputDir = ((App*)be_app)->cfg->outDir;
}
AtomFeed::AtomFeed ( Feed* feed ) : AtomFeed::AtomFeed()
{ filePath = feed->filePath; }
AtomFeed::AtomFeed ( Feed* feed, Config* cfg ) : AtomFeed::AtomFeed( feed )
{ outputDir = cfg->outDir; }
AtomFeed::AtomFeed ( Config* cfg ) : AtomFeed::AtomFeed( )
{ outputDir = cfg->outDir; }
// ----------------------------------------------------------------------------
void
AtomFeed::Parse ( Config* cfg )
AtomFeed::Parse ( )
{
entries = BList();
tinyxml2::XMLDocument xml;
xml.LoadFile( filePath.String() );
Feed::Parse( cfg );
Feed::Parse();
tinyxml2::XMLElement* xfeed = xml.FirstChildElement("feed");
RootParse( cfg, xfeed );
ParseEntries( cfg, xfeed );
RootParse( xfeed );
ParseEntries( xfeed );
BFile* feedFile = new BFile( filePath.String(), B_READ_WRITE );
time_t tt_lastDate = lastDate.Time_t();
@ -42,7 +40,7 @@ AtomFeed::Parse ( Config* cfg )
}
void
AtomFeed::RootParse( Config* cfg, tinyxml2::XMLElement* xfeed )
AtomFeed::RootParse( tinyxml2::XMLElement* xfeed )
{
tinyxml2::XMLElement* xauthor = xfeed->FirstChildElement("author");
tinyxml2::XMLElement* xentry = xfeed->FirstChildElement("entry");
@ -63,12 +61,12 @@ AtomFeed::RootParse( Config* cfg, tinyxml2::XMLElement* xfeed )
if ( !set && xauthor ) set = SetHomeUrl( xauthor->FirstChildElement("uri") );
if ( !set && xauthlink ) set = SetHomeUrl( xauthlink->Attribute( "href" ) );
if ( cfg->verbose )
if ( ((App*)be_app)->cfg->verbose )
printf("Channel '%s' at '%s':\n", title.String(), homeUrl.String());
}
void
AtomFeed::EntryParse ( Config* cfg, tinyxml2::XMLElement* xentry )
AtomFeed::EntryParse ( tinyxml2::XMLElement* xentry )
{
Entry* newEntry= (Entry*)malloc( sizeof(Entry) );
newEntry = new Entry( outputDir );
@ -97,11 +95,11 @@ AtomFeed::EntryParse ( Config* cfg, tinyxml2::XMLElement* xentry )
newEntry->SetContent( xprinter.CStr() );
}
AddEntry( cfg, newEntry );
AddEntry( newEntry );
}
void
AtomFeed::ParseEntries ( Config* cfg, tinyxml2::XMLElement* xfeed )
AtomFeed::ParseEntries ( tinyxml2::XMLElement* xfeed )
{
tinyxml2::XMLElement* xentry;
@ -110,11 +108,11 @@ AtomFeed::ParseEntries ( Config* cfg, tinyxml2::XMLElement* xfeed )
int entryCount = xmlCountSiblings( xentry, "entry" );
entries = BList(entryCount);
if ( cfg->verbose )
if ( ((App*)be_app)->cfg->verbose )
printf("\t-%i entries-\n", entryCount);
while ( xentry ) {
EntryParse( cfg, xentry );
EntryParse( xentry );
xentry = xentry->NextSiblingElement("entry");
}
}

View File

@ -12,14 +12,12 @@
class AtomFeed: public Feed {
public:
AtomFeed ( );
AtomFeed ( Feed*, Config* );
AtomFeed ( Feed* );
AtomFeed ( Config* );
void Parse ( Config* );
void RootParse ( Config*, tinyxml2::XMLElement* );
void EntryParse ( Config*, tinyxml2::XMLElement* );
void ParseEntries ( Config*, tinyxml2::XMLElement* );
void Parse ( );
void RootParse ( tinyxml2::XMLElement* );
void EntryParse ( tinyxml2::XMLElement* );
void ParseEntries ( tinyxml2::XMLElement* );
};
#endif

View File

@ -17,7 +17,7 @@ Entry::Entry ( BString outputPath )
}
bool
Entry::Filetize ( Config* cfg, bool onlyIfNew = false )
Entry::Filetize ( bool onlyIfNew = false )
{
BDirectory* dir = new BDirectory( outputDir );
BFile* file = new BFile( title.String(), B_READ_WRITE );

View File

@ -1,7 +1,6 @@
#ifndef ENTRY_H
#define ENTRY_H
#include <iostream>
#include <DateTime.h>
#include <String.h>
#include <List.h>
@ -20,7 +19,7 @@ public:
Entry ( BString );
bool Filetize ( Config*, bool );
bool Filetize ( bool );
bool SetTitle ( const char* );
bool SetTitle ( tinyxml2::XMLElement* );
@ -35,6 +34,4 @@ public:
bool SetDate ( tinyxml2::XMLElement* );
};
#endif

View File

@ -1,17 +1,18 @@
#include <tinyxml2.h>
#include "App.h"
#include "Entry.h"
#include "Config.h"
#include "Util.h"
#include "Feed.h"
Feed::Feed ( BString path, Config* cfg )
Feed::Feed ( BString path )
{
title = BString( "Untitled Feed" );
description = BString( "Nondescript, N/A." );
homeUrl = BString("");
xmlUrl = BString("");
updated = true;
filePath = GetCachePath( path, cfg );
filePath = GetCachePath( path );
}
Feed::Feed ( )
@ -25,14 +26,14 @@ Feed::Feed ( )
// ----------------------------------------------------------------------------
void
Feed::Parse ( Config* cfg )
Feed::Parse ( )
{
BFile* feedFile = new BFile( filePath.String(), B_READ_ONLY );
time_t tt_lastDate = 0;
BDateTime attrLastDate = BDateTime();
feedFile->ReadAttr( "LastDate", B_TIME_TYPE, 0, &tt_lastDate, sizeof(time_t) );
if ( tt_lastDate > 0 && cfg->updateFeeds == true ) {
if ( tt_lastDate > 0 && ((App*)be_app)->cfg->updateFeeds == true ) {
attrLastDate.SetTime_t( tt_lastDate );
minDate = attrLastDate;
}
@ -41,7 +42,7 @@ Feed::Parse ( Config* cfg )
// -------------------------------------
BString
Feed::GetCachePath ( BString givenPath, Config* cfg )
Feed::GetCachePath ( BString givenPath )
{
BUrl givenUrl = BUrl( givenPath );
BString protocol = givenUrl.Protocol().String();
@ -51,11 +52,11 @@ Feed::GetCachePath ( BString givenPath, Config* cfg )
if ( protocol != BString("http") && protocol != BString("https") )
return NULL;
return FetchRemoteFeed( givenPath, cfg );
return FetchRemoteFeed( givenPath );
}
BString
Feed::FetchRemoteFeed ( BString givenPath, Config* cfg )
Feed::FetchRemoteFeed ( BString givenPath )
{
BUrl givenUrl = BUrl( givenPath );
time_t tt_lastDate = 0;
@ -67,13 +68,13 @@ Feed::FetchRemoteFeed ( BString givenPath, Config* cfg )
splitName.Append( givenUrl.Path() );
splitName.ReplaceAll("/", "_");
BString filename = cfg->cacheDir;
BString filename = ((App*)be_app)->cfg->cacheDir;
filename.Append(splitName);
BFile* cacheFile = new BFile( filename, B_READ_WRITE | B_CREATE_FILE );
cacheFile->ReadAttr( "LastHash", B_STRING_TYPE, 0, oldHash, 41 );
if ( cfg->verbose )
if ( ((App*)be_app)->cfg->verbose )
printf( "Saving %s...\n", givenPath.String() );
webFetch( givenUrl, cacheFile, newHash );
@ -127,8 +128,9 @@ Feed::xmlCountSiblings ( tinyxml2::XMLElement* xsibling, const char* sibling_nam
// ----------------------------------------------------------------------------
bool
Feed::AddEntry ( Config* cfg, Entry* newEntry )
Feed::AddEntry ( Entry* newEntry )
{
Config* cfg = ((App*)be_app)->cfg;
if ( !withinDateRange( cfg->minDate, newEntry->date, cfg->maxDate ) ||
!withinDateRange( minDate, newEntry->date, maxDate ) )
return false;

View File

@ -7,11 +7,10 @@
#include <List.h>
#include <Url.h>
#include "Entry.h"
#include "Config.h"
class Feed {
public:
Feed ( BString, Config* );
Feed ( BString );
Feed ( );
BString title;
@ -27,9 +26,9 @@ public:
BList entries;
bool updated;
virtual void Parse ( Config* );
virtual void Parse ( );
bool AddEntry ( Config*, Entry* );
bool AddEntry ( Entry* );
bool SetTitle ( const char* );
bool SetTitle ( tinyxml2::XMLElement* );
bool SetDesc ( const char* );
@ -43,8 +42,8 @@ public:
bool IsAtom ( );
protected:
BString GetCachePath ( BString, Config* );
BString FetchRemoteFeed ( BString, Config* );
BString GetCachePath ( BString );
BString FetchRemoteFeed ( BString );
int xmlCountSiblings ( tinyxml2::XMLElement*, const char* );
};

View File

@ -19,9 +19,9 @@ usage ( )
}
int
invocation ( int argc, char** argv, Config** cfgPtr )
invocation ( int argc, char** argv )
{
Config* cfg = *(cfgPtr);
Config* cfg = ((App*)be_app)->cfg;
BDateTime maxDate;
BDateTime minDate;
@ -44,7 +44,7 @@ invocation ( int argc, char** argv, Config** cfgPtr )
switch (c) {
case -1:
freeargInvocation( argc, argv, optind, cfgPtr );
freeargInvocation( argc, argv, optind );
return 0;
case 'h':
return usage();
@ -100,9 +100,9 @@ invocation ( int argc, char** argv, Config** cfgPtr )
// -------------------------------------
void
freeargInvocation ( int argc, char** argv, int optind, Config** cfgPtr )
freeargInvocation ( int argc, char** argv, int optind )
{
Config* cfg = *(cfgPtr);
Config* cfg = ((App*)be_app)->cfg;
if ( optind < argc ) {
int freeargc = argc - optind;
cfg->targetFeeds = BList( freeargc );
@ -120,7 +120,7 @@ bool
processEntry ( void* entry )
{
Entry* entryPtr = (Entry*)entry;
entryPtr->Filetize( main_cfg, false );
entryPtr->Filetize( false );
return false;
}
@ -128,22 +128,22 @@ bool
processFeed ( void* feedArg )
{
BString* feedStr = (BString*)feedArg;
Feed* testFeed = new Feed( *(feedStr), main_cfg );
Feed* testFeed = new Feed( *(feedStr) );
BList entries;
if ( testFeed->updated == false && main_cfg->updateFeeds == true )
if ( testFeed->updated == false && ((App*)be_app)->cfg->updateFeeds == true )
return false;
if ( testFeed->IsAtom() ) {
AtomFeed* feed = (AtomFeed*)malloc( sizeof(AtomFeed) );
feed = new AtomFeed( testFeed, main_cfg );
feed->Parse(main_cfg);
feed = new AtomFeed( testFeed );
feed->Parse();
entries = feed->entries;
}
if ( testFeed->IsRss() ) {
RssFeed* feed = (RssFeed*)malloc( sizeof(RssFeed) );
feed = new RssFeed( testFeed, main_cfg );
feed->Parse(main_cfg);
feed = new RssFeed( testFeed );
feed->Parse();
entries = feed->entries;
}

View File

@ -4,8 +4,8 @@
#include <StorageKit.h>
int usage ( );
int invocation ( int, char**, Config** );
void freeargInvocation ( int, char**, int, Config** );
int invocation ( int, char** );
void freeargInvocation ( int, char**, int );
bool processItem ( void* );
bool processFeed ( void* );

View File

@ -1,5 +1,6 @@
#include <tinyxml2.h>
#include "Entry.h"
#include "App.h"
#include "Config.h"
#include "Util.h"
#include "RssFeed.h"
@ -10,31 +11,27 @@ RssFeed::RssFeed ( )
description = BString("");
homeUrl = BString("");
xmlUrl = BString("");
outputDir = ((App*)be_app)->cfg->outDir;
}
RssFeed::RssFeed ( Feed* feed ) : RssFeed::RssFeed()
{ filePath = feed->filePath; }
RssFeed::RssFeed ( Feed* feed, Config* cfg ) : RssFeed::RssFeed( feed )
{ outputDir = cfg->outDir; }
RssFeed::RssFeed ( Config* cfg ) : RssFeed::RssFeed( )
{ outputDir = cfg->outDir; }
// ----------------------------------------------------------------------------
void
RssFeed::Parse ( Config* cfg )
RssFeed::Parse ( )
{
tinyxml2::XMLDocument xml;
entries = BList();
Feed::Parse( cfg );
Feed::Parse();
xml.LoadFile( filePath.String() );
tinyxml2::XMLElement* xchan = xml.FirstChildElement("rss")->FirstChildElement("channel");
RootParse( cfg, xchan );
ParseEntries( cfg, xchan );
RootParse( xchan );
ParseEntries( xchan );
time_t tt_lastDate = lastDate.Time_t();
BFile* feedFile = new BFile( filePath.String(), B_READ_ONLY );
@ -44,19 +41,19 @@ RssFeed::Parse ( Config* cfg )
// -------------------------------------
void
RssFeed::RootParse ( Config* cfg, tinyxml2::XMLElement* xchan )
RssFeed::RootParse ( tinyxml2::XMLElement* xchan )
{
SetTitle ( xchan->FirstChildElement("title") );
SetDesc ( xchan->FirstChildElement("description") );
SetHomeUrl ( xchan->FirstChildElement("link") );
SetDate ( xchan->FirstChildElement("lastBuildDate") );
if ( cfg->verbose )
if ( ((App*)be_app)->cfg->verbose )
printf("Channel '%s' at '%s':\n", title.String(), homeUrl.String());
}
void
RssFeed::EntryParse ( Config* cfg, tinyxml2::XMLElement* xitem )
RssFeed::EntryParse ( tinyxml2::XMLElement* xitem )
{
Entry* newEntry = (Entry*)malloc( sizeof(Entry) );
newEntry = new Entry( outputDir );
@ -71,11 +68,11 @@ RssFeed::EntryParse ( Config* cfg, tinyxml2::XMLElement* xitem )
if ( lastDate == NULL || lastDate < newEntry->date )
lastDate = newEntry->date;
AddEntry( cfg, newEntry );
AddEntry( newEntry );
}
void
RssFeed::ParseEntries ( Config* cfg, tinyxml2::XMLElement* xchan )
RssFeed::ParseEntries ( tinyxml2::XMLElement* xchan )
{
tinyxml2::XMLElement* xitem;
@ -84,11 +81,11 @@ RssFeed::ParseEntries ( Config* cfg, tinyxml2::XMLElement* xchan )
int entryCount = xmlCountSiblings( xitem, "item" );
entries = BList(entryCount);
if ( cfg->verbose )
if ( ((App*)be_app)->cfg->verbose )
printf("\t-%i entries-\n", entryCount);
while ( xitem ) {
EntryParse( cfg, xitem );
EntryParse( xitem );
xitem = xitem->NextSiblingElement("item");
}
}

View File

@ -6,20 +6,17 @@
#include <String.h>
#include <List.h>
#include <Url.h>
#include "Config.h"
#include "Feed.h"
class RssFeed: public Feed {
public:
RssFeed ( );
RssFeed ( Feed*, Config* );
RssFeed ( Feed* );
RssFeed ( Config* );
void Parse ( Config* );
void RootParse ( Config*, tinyxml2::XMLElement* );
void EntryParse ( Config*, tinyxml2::XMLElement* );
void ParseEntries ( Config*, tinyxml2::XMLElement* );
void Parse ( );
void RootParse ( tinyxml2::XMLElement* );
void EntryParse ( tinyxml2::XMLElement* );
void ParseEntries ( tinyxml2::XMLElement* );
};
#endif