2021-01-13 17:28:07 -06:00
|
|
|
/*
|
2021-01-22 20:25:54 -06:00
|
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
2021-01-13 17:28:07 -06:00
|
|
|
* All rights reserved. Distributed under the terms of the MIT license.
|
|
|
|
*/
|
|
|
|
|
2021-01-22 20:25:54 -06:00
|
|
|
#include "Notifier.h"
|
|
|
|
|
2021-03-01 19:13:22 -06:00
|
|
|
#include <List.h>
|
2021-01-13 17:28:07 -06:00
|
|
|
#include <Message.h>
|
|
|
|
#include <Notification.h>
|
|
|
|
|
2021-01-22 20:25:54 -06:00
|
|
|
#include "App.h"
|
2021-01-13 17:28:07 -06:00
|
|
|
#include "FeedController.h"
|
|
|
|
|
2021-01-22 20:25:54 -06:00
|
|
|
|
|
|
|
Notifier::Notifier()
|
|
|
|
:
|
2021-03-01 19:13:22 -06:00
|
|
|
fFailedFeeds(new BList()),
|
|
|
|
fUpdatedFeeds(new BList()),
|
|
|
|
fTotalEntries(0)
|
2021-01-22 20:25:54 -06:00
|
|
|
{
|
|
|
|
}
|
2021-01-13 17:28:07 -06:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Notifier::MessageReceived(BMessage* msg)
|
|
|
|
{
|
|
|
|
switch (msg->what)
|
|
|
|
{
|
2021-03-01 19:13:22 -06:00
|
|
|
case kProgress:
|
2021-01-22 20:25:54 -06:00
|
|
|
{
|
2021-03-01 19:13:22 -06:00
|
|
|
int32 total, current = 0;
|
|
|
|
|
|
|
|
if (msg->FindInt32("total", &total) == B_OK
|
|
|
|
&& msg->FindInt32("current", ¤t) == B_OK
|
|
|
|
&& total == current)
|
|
|
|
{
|
|
|
|
_SendNotifications();
|
|
|
|
}
|
2021-01-22 20:25:54 -06:00
|
|
|
break;
|
|
|
|
}
|
2021-01-13 17:28:07 -06:00
|
|
|
case kParseComplete:
|
|
|
|
{
|
|
|
|
BString feedName;
|
2021-03-01 19:13:22 -06:00
|
|
|
BString feedUrl;
|
2021-01-13 17:28:07 -06:00
|
|
|
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)
|
2021-01-13 17:28:07 -06:00
|
|
|
&& 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);
|
2021-01-13 17:28:07 -06:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kDownloadFail:
|
2021-03-01 19:13:22 -06:00
|
|
|
case kParseFail:
|
2021-01-13 17:28:07 -06:00
|
|
|
{
|
2021-03-01 19:13:22 -06:00
|
|
|
BString feedName;
|
|
|
|
BString feedUrl;
|
2021-01-22 20:25:54 -06:00
|
|
|
|
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);
|
|
|
|
}
|
2021-01-22 20:25:54 -06:00
|
|
|
break;
|
2021-01-13 17:28:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-03-01 19:13:22 -06:00
|
|
|
Notifier::_SendNotifications()
|
|
|
|
{
|
|
|
|
_SendUpdatedNotification();
|
|
|
|
_SendFailedNotification();
|
|
|
|
|
|
|
|
fFailedFeeds->MakeEmpty();
|
|
|
|
fUpdatedFeeds->MakeEmpty();
|
|
|
|
fTotalEntries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Notifier::_SendUpdatedNotification()
|
2021-01-13 17:28:07 -06:00
|
|
|
{
|
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
|
|
|
|
2021-01-13 17:28:07 -06:00
|
|
|
BNotification notifyNew(B_INFORMATION_NOTIFICATION);
|
2021-03-01 19:13:22 -06:00
|
|
|
BString notifyLabel("Feed Updates");
|
2021-01-13 17:28:07 -06:00
|
|
|
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());
|
2021-01-13 17:28:07 -06:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-01-13 17:28:07 -06:00
|
|
|
notifyNew.Send();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-03-01 19:13:22 -06:00
|
|
|
Notifier::_SendFailedNotification()
|
2021-01-13 17:28:07 -06:00
|
|
|
{
|
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
|
|
|
|
2021-01-13 17:28:07 -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-01-13 17:28:07 -06:00
|
|
|
|
2021-03-01 19:13:22 -06:00
|
|
|
BString feedNum = "";
|
2021-03-07 14:08:47 -06:00
|
|
|
feedNum << fFailedFeeds->CountItems() - 1;
|
2021-01-13 17:28:07 -06:00
|
|
|
|
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-01-13 17:28:07 -06:00
|
|
|
|
2021-03-01 19:13:22 -06:00
|
|
|
notifyError.SetTitle(notifyLabel);
|
2021-01-13 17:28:07 -06:00
|
|
|
notifyError.SetContent(notifyText);
|
|
|
|
notifyError.Send();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-03-01 19:13:22 -06:00
|
|
|
Notifier::_SaveUpdated(BString feedName, BString feedUrl, int32 entryCount)
|
2021-01-13 17:28:07 -06:00
|
|
|
{
|
2021-03-01 19:13:22 -06:00
|
|
|
BString name = feedName;
|
|
|
|
if (feedName.IsEmpty())
|
|
|
|
name = feedUrl;
|
2021-01-13 17:28:07 -06:00
|
|
|
|
2021-03-01 19:13:22 -06:00
|
|
|
fTotalEntries += entryCount;
|
|
|
|
fUpdatedFeeds->AddItem(new BString(feedName));
|
2021-01-13 17:28:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-22 20:25:54 -06:00
|
|
|
void
|
2021-03-01 19:13:22 -06:00
|
|
|
Notifier::_SaveFailed(BString feedName, BString feedUrl)
|
2021-01-22 20:25:54 -06:00
|
|
|
{
|
2021-03-01 19:13:22 -06:00
|
|
|
BString name = feedName;
|
|
|
|
if (feedName.IsEmpty())
|
|
|
|
name = feedUrl;
|
2021-01-22 20:25:54 -06:00
|
|
|
|
2021-03-01 19:13:22 -06:00
|
|
|
fFailedFeeds->AddItem(new BString(feedName));
|
2021-01-22 20:25:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|