Pogger/src/Notifier.cpp

194 lines
4.1 KiB
C++
Raw Normal View History

/*
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "Notifier.h"
2021-03-21 18:47:58 -05:00
#include <Catalog.h>
2021-03-01 19:13:22 -06:00
#include <List.h>
#include <Message.h>
2021-03-21 18:47:58 -05:00
#include <StringFormat.h>
#include <Notification.h>
#include "App.h"
#include "FeedController.h"
2021-03-21 18:47:58 -05:00
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Notifier"
Notifier::Notifier()
:
2021-03-01 19:13:22 -06:00
fFailedFeeds(new BList()),
fUpdatedFeeds(new BList()),
fTotalEntries(0)
{
}
void
Notifier::MessageReceived(BMessage* msg)
{
switch (msg->what)
{
2021-03-01 19:13:22 -06:00
case kProgress:
{
2021-03-01 19:13:22 -06:00
int32 total, current = 0;
if (msg->FindInt32("total", &total) == B_OK
&& msg->FindInt32("current", &current) == B_OK
&& total == current)
{
_SendNotifications();
}
break;
}
case kParseComplete:
{
BString feedName;
2021-03-01 19:13:22 -06:00
BString feedUrl;
int32 entryCount;
2021-03-01 19:13:22 -06:00
if ((msg->FindString("feed_name", &feedName) == B_OK
|| msg->FindString("feed_url", &feedUrl) == B_OK)
&& msg->FindInt32("entry_count", &entryCount) == B_OK)
{
2021-01-15 22:22:33 -06:00
if (entryCount > 0)
2021-03-01 19:13:22 -06:00
_SaveUpdated(feedName, feedUrl, entryCount);
}
break;
}
case kDownloadFail:
2021-03-01 19:13:22 -06:00
case kParseFail:
{
2021-03-01 19:13:22 -06:00
BString feedName;
BString feedUrl;
2021-03-01 19:13:22 -06:00
if (msg->FindString("feed_name", &feedName) == B_OK
|| msg->FindString("feed_url", &feedUrl) == B_OK)
{
_SaveFailed(feedName, feedUrl);
}
break;
}
}
}
void
2021-03-01 19:13:22 -06:00
Notifier::_SendNotifications()
{
_SendUpdatedNotification();
_SendFailedNotification();
fFailedFeeds->MakeEmpty();
fUpdatedFeeds->MakeEmpty();
fTotalEntries = 0;
}
void
Notifier::_SendUpdatedNotification()
{
2021-03-01 19:13:22 -06:00
if (((App*)be_app)->fPreferences->fNewNotify == false
|| fTotalEntries == 0) {
2021-01-25 19:39:31 -06:00
return;
2021-03-01 19:13:22 -06:00
}
2021-01-25 19:39:31 -06:00
BNotification notifyNew(B_INFORMATION_NOTIFICATION);
2021-03-21 18:47:58 -05:00
BString notifyLabel(B_TRANSLATE("Feed Updates"));
BString notifyText;
static BStringFormat oneSourceFormat(B_TRANSLATE("{0, plural,"
"=1{One new entry from %source%.}"
"other{# new entries from %source%.}}"));
2021-03-21 18:47:58 -05:00
static BStringFormat multiSourceFormat(B_TRANSLATE("{0, plural,"
"=1{%n% new entries from %source% and one other.}"
"other{%n% new entries from %source% and # others.}}"));
2021-03-01 19:13:22 -06:00
2021-03-21 18:47:58 -05:00
if (fUpdatedFeeds->CountItems() > 1)
multiSourceFormat.Format(notifyText, fUpdatedFeeds->CountItems() - 1);
else
oneSourceFormat.Format(notifyText, fTotalEntries);
BString entryNum = "";
2021-03-01 19:13:22 -06:00
entryNum << fTotalEntries;
2021-03-21 18:47:58 -05:00
BString* feedTitle = (BString*)fUpdatedFeeds->ItemAt(0);
if (feedTitle->IsEmpty())
feedTitle->SetTo(B_TRANSLATE("Untitled Feed"));
2021-03-01 19:13:22 -06:00
notifyText.ReplaceAll("%n%", entryNum);
2021-03-21 18:47:58 -05:00
notifyText.ReplaceAll("%source%", feedTitle->String());
notifyNew.SetTitle(notifyLabel);
notifyNew.SetContent(notifyText);
2021-03-04 18:18:39 -06:00
entry_ref feedsRef;
BEntry(((App*)be_app)->fPreferences->EntryDir()).GetRef(&feedsRef);
notifyNew.SetOnClickFile(&feedsRef);
notifyNew.Send();
}
void
2021-03-01 19:13:22 -06:00
Notifier::_SendFailedNotification()
{
2021-03-01 19:13:22 -06:00
if (((App*)be_app)->fPreferences->fFailureNotify == false
|| fFailedFeeds->CountItems() == 0) {
2021-01-25 19:39:31 -06:00
return;
2021-03-01 19:13:22 -06:00
}
2021-01-25 19:39:31 -06:00
BNotification notifyError(B_ERROR_NOTIFICATION);
2021-03-21 18:47:58 -05:00
BString notifyLabel(B_TRANSLATE("Update Failure"));
BString notifyText;
static BStringFormat multiSourceFormat(B_TRANSLATE("{0, plural,"
"=1{Failed to update %source% and one other.}"
"other{Failed to update %source% and # others.}}"));
2021-03-01 19:13:22 -06:00
if (fFailedFeeds->CountItems() > 1)
2021-03-21 18:47:58 -05:00
multiSourceFormat.Format(notifyText, fFailedFeeds->CountItems() - 1);
else
notifyText = B_TRANSLATE("Failed to update %source%.");
2021-03-21 18:47:58 -05:00
BString* feedTitle = (BString*)fFailedFeeds->ItemAt(0);
if (feedTitle->IsEmpty())
feedTitle->SetTo(B_TRANSLATE("Untitled Feed"));
2021-03-21 18:47:58 -05:00
notifyText.ReplaceAll("%source%", feedTitle->String());
2021-03-01 19:13:22 -06:00
notifyError.SetTitle(notifyLabel);
notifyError.SetContent(notifyText);
notifyError.Send();
}
void
2021-03-01 19:13:22 -06:00
Notifier::_SaveUpdated(BString feedName, BString feedUrl, int32 entryCount)
{
2021-03-01 19:13:22 -06:00
BString name = feedName;
if (feedName.IsEmpty())
name = feedUrl;
2021-03-01 19:13:22 -06:00
fTotalEntries += entryCount;
fUpdatedFeeds->AddItem(new BString(feedName));
}
void
2021-03-01 19:13:22 -06:00
Notifier::_SaveFailed(BString feedName, BString feedUrl)
{
2021-03-01 19:13:22 -06:00
BString name = feedName;
if (feedName.IsEmpty())
name = feedUrl;
2021-03-01 19:13:22 -06:00
fFailedFeeds->AddItem(new BString(feedName));
}