diff --git a/src/Config.cpp b/src/Config.cpp index 55abba2..cfbbc0a 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,10 +1,63 @@ +#include #include #include "Config.h" Config::Config () { verbose = false; daemon = true; - mimetype = BString("text/xml"); - outDir = BString("/boot/home/feeds/"); - targetFeeds = NULL; + will_save = false; } + +// !! 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 ); +} + diff --git a/src/Config.h b/src/Config.h index eaf5a94..328582a 100644 --- a/src/Config.h +++ b/src/Config.h @@ -11,12 +11,18 @@ public: bool daemon; BString mimetype; BString outDir; - BList targetFeeds; // file or url + BList targetFeeds; // strings of files or urls BDateTime minDate; BDateTime maxDate; + BString configPath; + bool will_save; + Config ( ); + + void Load ( ); + void Save ( ); }; #endif diff --git a/src/Pogger.cpp b/src/Pogger.cpp index faf4cf1..b61d05a 100644 --- a/src/Pogger.cpp +++ b/src/Pogger.cpp @@ -8,18 +8,19 @@ #include "Util.h" #include "Pogger.h" -#include - -Config* main_cfg; - int main ( int argc, char** argv ) { main_cfg = new Config; usageMsg.ReplaceAll("%app%", "Pogger"); + 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; } @@ -43,6 +44,9 @@ invocation ( int argc, char** argv, Config** cfgPtr ) static struct option sLongOptions[] = { { "help", no_argument, 0, 'h' }, { "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' }, { "mimetype", required_argument, 0, 'm' }, { "foreground", no_argument, 0, 'D' }, @@ -51,7 +55,7 @@ invocation ( int argc, char** argv, Config** cfgPtr ) while (true) { 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) { case -1: @@ -59,6 +63,11 @@ invocation ( int argc, char** argv, Config** cfgPtr ) return 0; case 'h': return usage(); + case 'c': + cfg->configPath = BString( optarg ); + break; + case 's': + cfg->will_save = true; case 'm': cfg->mimetype = BString( optarg ); break; diff --git a/src/Pogger.h b/src/Pogger.h index 514aa9d..c279574 100644 --- a/src/Pogger.h +++ b/src/Pogger.h @@ -9,9 +9,15 @@ bool processFeed ( void* ); // ---------------------------------------------------------------------------- +Config* main_cfg; +const char* configPath = "/boot/home/config/settings/Pogger/"; + +// ---------------------------------------------------------------------------- + + BString usageMsg = - "Usage: %app% [-hvD] [-m mimetype] [-tT datetime] [-O directory] \n" - " %app% [-hv] [-mtTO] ( | | )\n" + "Usage: %app% [-hvDus] [-m mimetype] [-tT datetime] [-cCO path] \n" + " %app% [-hvs] [-mtTcCO] ( | | )\n" "\n" "%app%, a RSS and Atom feed parser/daemon.\n" "\n" @@ -22,8 +28,12 @@ BString usageMsg = " -O, --output - Output dir for item files. (Default: ~/feeds/)\n" " -t, --before - Only return items published before 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" - " Only applies when running without file/url arg.\n" + " `-u` and `-D` only apply when running without file/url arg.\n" "\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" @@ -42,7 +52,7 @@ BString usageMsg = "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" " 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" ;