Pogger/src/Notifier.cpp

172 lines
3.4 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-01 19:13:22 -06:00
#include <List.h>
#include <Message.h>
#include <Notification.h>
#include "App.h"
#include "FeedController.h"
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-01 19:13:22 -06:00
BString notifyLabel("Feed Updates");
BString notifyText("%n% new entries from %source%");
2021-03-01 19:13:22 -06:00
if (fUpdatedFeeds->CountItems() > 1)
notifyText = "%n% new entries from %source% and %m% others";
BString entryNum,feedNum = "";
entryNum << fTotalEntries;
2021-03-07 14:08:47 -06:00
feedNum << fUpdatedFeeds->CountItems() - 1;
2021-03-01 19:13:22 -06:00
notifyText.ReplaceAll("%n%", entryNum);
2021-03-07 14:08:47 -06:00
notifyText.ReplaceAll("%m%", feedNum);
2021-03-01 19:13:22 -06:00
notifyText.ReplaceAll("%source%",
((BString*)fUpdatedFeeds->ItemAt(0))->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-01 19:13:22 -06:00
BString notifyLabel("Update Failure");
BString notifyText("Failed to update %source%");
if (fFailedFeeds->CountItems() > 1)
notifyText = "Failed to update %source% and %m% others";
2021-03-01 19:13:22 -06:00
BString feedNum = "";
2021-03-07 14:08:47 -06:00
feedNum << fFailedFeeds->CountItems() - 1;
2021-03-07 14:08:47 -06:00
notifyText.ReplaceAll("%m%", feedNum);
2021-03-01 19:13:22 -06:00
notifyText.ReplaceAll("%source%",
((BString*)fFailedFeeds->ItemAt(0))->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));
}