From 4383c4b9068c2041d0a9e1674d8d3360967e2b82 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Wed, 13 Jan 2021 17:28:07 -0600 Subject: [PATCH] Error notifications and dedicated Notifier object --- Makefile | 1 + src/App.cpp | 15 ++++++- src/App.h | 2 + src/Feed.cpp | 12 +++--- src/Feed.h | 2 +- src/FeedController.cpp | 51 +++++++++++----------- src/FeedController.h | 6 ++- src/Notifier.cpp | 97 ++++++++++++++++++++++++++++++++++++++++++ src/Notifier.h | 26 +++++++++++ 9 files changed, 174 insertions(+), 38 deletions(-) create mode 100644 src/Notifier.cpp create mode 100644 src/Notifier.h diff --git a/Makefile b/Makefile index 1a586a7..10a36ce 100644 --- a/Makefile +++ b/Makefile @@ -39,6 +39,7 @@ SRCS = \ src/FeedsView.cpp, \ src/MainWindow.cpp, \ src/Mimetypes.cpp, \ + src/Notifier.cpp, \ src/ProtocolListener.cpp, \ src/RssFeed.cpp, \ src/UpdatesView.cpp, \ diff --git a/src/App.cpp b/src/App.cpp index 07040d3..07af227 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -18,6 +18,7 @@ #include "FeedController.h" #include "MainWindow.h" #include "Mimetypes.h" +#include "Notifier.h" #include "RssFeed.h" #include "Util.h" @@ -40,6 +41,7 @@ App::App() : BApplication("application/x-vnd.Pogger") cfg->Load(); fMainWindow = new MainWindow(); + fNotifier = new Notifier(); fFeedController = new FeedController(); fMainWindow->Show(); @@ -69,13 +71,22 @@ App::MessageReceived(BMessage* msg) { break; } - case kQueueProgress: + case kParseFail: { - fMainWindow->MessageReceived(msg); + fNotifier->MessageReceived(msg); + fFeedController->MessageReceived(msg); + break; + } + case kDownloadFail: + { + fNotifier->MessageReceived(msg); + fFeedController->MessageReceived(msg); + break; } case kDownloadComplete: { fFeedController->MessageReceived(msg); + break; } default: { diff --git a/src/App.h b/src/App.h index ecdb489..067734b 100644 --- a/src/App.h +++ b/src/App.h @@ -15,6 +15,7 @@ class BMessageRunner; class Config; class FeedController; class MainWindow; +class Notifier; class App : public BApplication @@ -28,6 +29,7 @@ public: Config* cfg; MainWindow* fMainWindow; BMessageRunner* fUpdateRunner; + Notifier* fNotifier; private: FeedController* fFeedController; diff --git a/src/Feed.cpp b/src/Feed.cpp index 70a61e7..2822a5e 100644 --- a/src/Feed.cpp +++ b/src/Feed.cpp @@ -93,21 +93,19 @@ Feed::_PostParse() // Download a remote feed's XML to the cache path. -BString +bool Feed::Fetch() { BFile cacheFile = BFile(cachePath, B_READ_WRITE | B_CREATE_FILE); cacheFile.ReadAttrString("Feed:hash", &lastHash); - fetch(xmlUrl, &cacheFile, &hash, 30); + int32 result = fetch(xmlUrl, &cacheFile, &hash, 30); cacheFile.WriteAttrString("Feed:hash", &hash); - if (hash == lastHash) - updated = false; - - fetched = true; - return cachePath; + if (result == 0) + return true; + return false; } diff --git a/src/Feed.h b/src/Feed.h index 9dfe2b9..77e7322 100644 --- a/src/Feed.h +++ b/src/Feed.h @@ -31,7 +31,7 @@ public: BList GetEntries(); BList GetNewEntries(); - BString Fetch(); + bool Fetch(); bool IsRss(); bool IsAtom(); diff --git a/src/FeedController.cpp b/src/FeedController.cpp index a249b47..c9e3660 100644 --- a/src/FeedController.cpp +++ b/src/FeedController.cpp @@ -105,14 +105,17 @@ FeedController::_DownloadLoop(void* ignored) printf( "Downloading feed from %s...\n", feedBuffer->GetXmlUrl().UrlString().String()); - feedBuffer->Fetch(); - - BMessage* downloaded = new BMessage(kDownloadComplete); - downloaded->AddData("feeds", B_RAW_TYPE, feedBuffer, sizeof(Feed)); - - ((App*)be_app)->MessageReceived(downloaded); + 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); + } } - delete(feedBuffer); return 0; } @@ -126,7 +129,8 @@ FeedController::_ParseLoop(void* ignored) while (receive_data(&sender, (void*)feedBuffer, sizeof(Feed)) != 0) { BList entries; - BString title; + BString feedTitle; + BUrl feedUrl = feedBuffer->GetXmlUrl(); BDirectory outDir = BDirectory(((App*)be_app)->cfg->outDir); if (feedBuffer->IsAtom()) { @@ -134,7 +138,7 @@ FeedController::_ParseLoop(void* ignored) feed = new AtomFeed(feedBuffer); feed->Parse(); entries = feed->GetNewEntries(); - title = feed->GetTitle(); + feedTitle = feed->GetTitle(); delete(feed); } else if (feedBuffer->IsRss()) { @@ -142,29 +146,24 @@ FeedController::_ParseLoop(void* ignored) feed = new RssFeed(feedBuffer); feed->Parse(); entries = feed->GetNewEntries(); - title = feed->GetTitle(); + feedTitle = feed->GetTitle(); delete(feed); } - if ((feedBuffer->IsAtom() || feedBuffer->IsRss()) - && entries.CountItems() > 0) - { + + if (feedBuffer->IsAtom() || feedBuffer->IsRss()) { for (int i = 0; i < entries.CountItems(); i++) ((Entry*)entries.ItemAt(i))->Filetize(outDir); - BNotification notifyNew = (B_INFORMATION_NOTIFICATION); - BString notifyLabel("New Feed Entries"); - BString notifyText("%n% new entries from %source%"); - - BString numStr(""); - numStr << entries.CountItems(); - notifyText.ReplaceAll("%source%", title); - notifyText.ReplaceAll("%n%", numStr); - - - notifyNew.SetTitle(notifyLabel); - notifyNew.SetContent(notifyText); - notifyNew.Send(); + 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); } } diff --git a/src/FeedController.h b/src/FeedController.h index f7e5286..f4f8eff 100644 --- a/src/FeedController.h +++ b/src/FeedController.h @@ -15,8 +15,10 @@ enum { kEnqueueFeed = 'fenq', kClearQueue = 'frmq', - kDownloadComplete = 'fdpr', - kQueueProgress = 'fqpr', + kDownloadComplete = 'fdlc', + kDownloadFail = 'fdlf', + kParseComplete = 'fpec', + kParseFail = 'fpef', kUpdateSubscribed = 'fups' }; diff --git a/src/Notifier.cpp b/src/Notifier.cpp new file mode 100644 index 0000000..c785c42 --- /dev/null +++ b/src/Notifier.cpp @@ -0,0 +1,97 @@ +/* + * Copyright 2020, Jaidyn Levesque + * All rights reserved. Distributed under the terms of the MIT license. + */ + +#include +#include + +#include "FeedController.h" + +#include "Notifier.h" + + +void +Notifier::MessageReceived(BMessage* msg) +{ + switch (msg->what) + { + case kParseComplete: + { + BString feedName; + int32 entryCount; + + if (msg->FindString("feed_name", &feedName) == B_OK + && msg->FindInt32("entry_count", &entryCount) == B_OK) + { + _NewEntryNotification(feedName, entryCount); + } + break; + } + case kParseFail: + { + BString feedUrl = msg->GetString("feed_url", ""); + _ParseFailNotification(feedUrl); + break; + } + case kDownloadFail: + { + BString feedUrl = msg->GetString("feed_url", ""); + _DownloadFailNotification(feedUrl); + } + } +} + + +void +Notifier::_NewEntryNotification(BString feedName, int32 entryCount) +{ + BNotification notifyNew(B_INFORMATION_NOTIFICATION); + BString notifyLabel("New Feed Entries"); + BString notifyText("%n% new entries from %source%"); + + BString numStr(""); + numStr << entryCount; + notifyText.ReplaceAll("%source%", feedName); + notifyText.ReplaceAll("%n%", numStr); + + notifyNew.SetTitle(notifyLabel); + notifyNew.SetContent(notifyText); + notifyNew.Send(); +} + + +void +Notifier::_ParseFailNotification(BString feedUrl) +{ + BNotification notifyError(B_ERROR_NOTIFICATION); + BString notifyText("Failed to parse feed from %url%"); + + if (feedUrl.IsEmpty()) + notifyText = "Failed to parse a feed"; + + notifyText.ReplaceAll("%url%", feedUrl); + + notifyError.SetTitle("Parse Failure"); + notifyError.SetContent(notifyText); + notifyError.Send(); +} + + +void +Notifier::_DownloadFailNotification(BString feedUrl) +{ + BNotification notifyError(B_ERROR_NOTIFICATION); + BString notifyText("Failed to download feed from %url%"); + + if (feedUrl.IsEmpty()) + notifyText = "Failed to download a feed"; + + notifyText.ReplaceAll("%url%", feedUrl); + + notifyError.SetTitle("Download Failure"); + notifyError.SetContent(notifyText); + notifyError.Send(); +} + + diff --git a/src/Notifier.h b/src/Notifier.h new file mode 100644 index 0000000..d74e4d4 --- /dev/null +++ b/src/Notifier.h @@ -0,0 +1,26 @@ +/* + * Copyright 2020, Jaidyn Levesque + * All rights reserved. Distributed under the terms of the MIT license. + */ +#ifndef NOTIFIER_H +#define NOTIFIER_H + +#include + +class BString; + + +class Notifier { +public: + void MessageReceived(BMessage* msg); + +private: + void _NewEntryNotification(BString feedName, int32 feedCount); + void _ParseFailNotification(BString feedUrl); + void _DownloadFailNotification(BString feedUrl); + +}; + + +#endif // NOTIFIER_H +