Add user preference file

This commit is contained in:
Jaidyn Ann 2020-07-07 20:17:52 -05:00
parent 7e6b2d5671
commit f2f6f10496
4 changed files with 92 additions and 14 deletions

View File

@ -1,10 +1,63 @@
#include <iostream>
#include <String.h> #include <String.h>
#include "Config.h" #include "Config.h"
Config::Config () { Config::Config () {
verbose = false; verbose = false;
daemon = true; daemon = true;
mimetype = BString("text/xml"); will_save = false;
outDir = BString("/boot/home/feeds/");
targetFeeds = NULL;
} }
// !! handle file status
void
Config::Load ()
{
if ( configPath == NULL )
configPath = BString( "/boot/home/config/settings/Pogger/" );
BString filename = BString(configPath);
filename.Append("settings");
BFile* file = new BFile( filename.String(), B_READ_ONLY );
status_t result = file->InitCheck();
BMessage storage;
storage.Unflatten( file );
if ( mimetype == NULL)
mimetype = BString( storage.GetString("mimetype", "text/xml") );
if ( outDir == NULL)
outDir = BString( storage.GetString("outDir", "/boot/home/feeds/") );
// if ( cacheDir == NULL)
// cacheDir = BString( storage.GetString("cacheDir", "/boot/home/config/cache/Pogger/") );
delete file;
}
// !! handle file status
void
Config::Save ()
{
if ( configPath == NULL )
configPath = BString( "/boot/home/config/settings/Pogger/" );
BPath* cfgPath = new BPath( configPath.String(), NULL, true );
BEntry* cfgEntry = new BEntry( cfgPath->Path() );
BDirectory* cfgDir = new BDirectory;
cfgDir->CreateDirectory(cfgPath->Path(), NULL);
if ( cfgEntry->Exists() == false )
cfgDir->CreateDirectory( cfgPath->Path(), NULL );
BMessage storage;
BString filename = BString( configPath ).Append("/settings");
BFile* file = new BFile( filename.String(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE );
status_t result = file->InitCheck();
storage.AddString( "mimetype", mimetype.String() );
storage.AddString( "outDir", outDir.String() );
// storage.AddString( "cacheDir", cacheDir.String() );
storage.Flatten( file );
}

View File

@ -11,12 +11,18 @@ public:
bool daemon; bool daemon;
BString mimetype; BString mimetype;
BString outDir; BString outDir;
BList targetFeeds; // file or url BList targetFeeds; // strings of files or urls
BDateTime minDate; BDateTime minDate;
BDateTime maxDate; BDateTime maxDate;
BString configPath;
bool will_save;
Config ( ); Config ( );
void Load ( );
void Save ( );
}; };
#endif #endif

View File

@ -8,18 +8,19 @@
#include "Util.h" #include "Util.h"
#include "Pogger.h" #include "Pogger.h"
#include <StorageKit.h>
Config* main_cfg;
int int
main ( int argc, char** argv ) main ( int argc, char** argv )
{ {
main_cfg = new Config; main_cfg = new Config;
usageMsg.ReplaceAll("%app%", "Pogger"); usageMsg.ReplaceAll("%app%", "Pogger");
invocation( argc, argv, &main_cfg ); invocation( argc, argv, &main_cfg );
main_cfg->targetFeeds.DoForEach(&processFeed); main_cfg->Load();
main_cfg->targetFeeds.DoForEach( &processFeed );
if ( main_cfg->will_save == true )
main_cfg->Save();
return 0; return 0;
} }
@ -43,6 +44,9 @@ invocation ( int argc, char** argv, Config** cfgPtr )
static struct option sLongOptions[] = { static struct option sLongOptions[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'v' }, { "verbose", no_argument, 0, 'v' },
{ "config", required_argument, 0, 'c' },
{ "before", required_argument, 0, 't' },
{ "after", required_argument, 0, 'T' },
{ "output", required_argument, 0, 'O' }, { "output", required_argument, 0, 'O' },
{ "mimetype", required_argument, 0, 'm' }, { "mimetype", required_argument, 0, 'm' },
{ "foreground", no_argument, 0, 'D' }, { "foreground", no_argument, 0, 'D' },
@ -51,7 +55,7 @@ invocation ( int argc, char** argv, Config** cfgPtr )
while (true) { while (true) {
opterr = 0; opterr = 0;
int c = getopt_long(argc, argv, "+hvDm:O:T:t:", sLongOptions, NULL); int c = getopt_long(argc, argv, "+hsvDm:O:T:t:c:", sLongOptions, NULL);
switch (c) { switch (c) {
case -1: case -1:
@ -59,6 +63,11 @@ invocation ( int argc, char** argv, Config** cfgPtr )
return 0; return 0;
case 'h': case 'h':
return usage(); return usage();
case 'c':
cfg->configPath = BString( optarg );
break;
case 's':
cfg->will_save = true;
case 'm': case 'm':
cfg->mimetype = BString( optarg ); cfg->mimetype = BString( optarg );
break; break;

View File

@ -9,9 +9,15 @@ bool processFeed ( void* );
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
Config* main_cfg;
const char* configPath = "/boot/home/config/settings/Pogger/";
// ----------------------------------------------------------------------------
BString usageMsg = BString usageMsg =
"Usage: %app% [-hvD] [-m mimetype] [-tT datetime] [-O directory] \n" "Usage: %app% [-hvDus] [-m mimetype] [-tT datetime] [-cCO path] \n"
" %app% [-hv] [-mtTO] ( <text/xml file> | <META:url file> | <url> )\n" " %app% [-hvs] [-mtTcCO] ( <text/xml file> | <META:url file> | <url> )\n"
"\n" "\n"
"%app%, a RSS and Atom feed parser/daemon.\n" "%app%, a RSS and Atom feed parser/daemon.\n"
"\n" "\n"
@ -22,8 +28,12 @@ BString usageMsg =
" -O, --output - Output dir for item files. (Default: ~/feeds/)\n" " -O, --output - Output dir for item files. (Default: ~/feeds/)\n"
" -t, --before - Only return items published before this datetime.\n" " -t, --before - Only return items published before this datetime.\n"
" -T, --after - Only return items published after this datetime.\n" " -T, --after - Only return items published after this datetime.\n"
" -c, --config - Path to config dir. (Default: ~/config/settings/Pogger/)\n"
" -C, --cache - Path to cache. (Default: ~/config/cache/Pogger/)\n"
" -s, --save - Save the args of `-m`, `-C`, and `-O` to config.\n"
" -u, --update - Update all feeds, but don't start daemon.\n"
" -D, --foreground - Run in the foreground, do not daemonize.\n" " -D, --foreground - Run in the foreground, do not daemonize.\n"
" Only applies when running without file/url arg.\n" " `-u` and `-D` only apply when running without file/url arg.\n"
"\n" "\n"
"When invoked without a file or URL, will search for any new feed items\n" "When invoked without a file or URL, will search for any new feed items\n"
"published since last check by by any 'feed file' placed in the config\n" "published since last check by by any 'feed file' placed in the config\n"
@ -42,7 +52,7 @@ BString usageMsg =
"NOTE: This message doesn't reflect reality. This is more of a spec of\n" "NOTE: This message doesn't reflect reality. This is more of a spec of\n"
" what I hope this program will be. As of now, running %app%\n" " what I hope this program will be. As of now, running %app%\n"
" without a file/url free-argument is invalid, as the daemon\n" " without a file/url free-argument is invalid, as the daemon\n"
" isn't implemented at all. As such, -D is also non-functional.\n" " isn't implemented at all. As such, -D -u and -C are non-functional.\n"
" But it sure can turn an XML feed into files! Lol.\n" " But it sure can turn an XML feed into files! Lol.\n"
; ;