Pogger/src/FeedsView.cpp

265 lines
6.0 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 "FeedsView.h"
2021-03-24 13:06:00 -05:00
#include <Alert.h>
2021-03-21 18:47:58 -05:00
#include <Catalog.h>
#include <Message.h>
#include <GroupView.h>
#include <LayoutBuilder.h>
#include <ListView.h>
#include <ScrollView.h>
#include <SeparatorView.h>
2021-03-06 12:36:33 -06:00
#include <StringList.h>
2021-02-18 23:16:45 -06:00
#include <StringView.h>
2021-01-15 22:22:33 -06:00
#include "App.h"
#include "Feed.h"
2021-01-13 20:23:21 -06:00
#include "FeedController.h"
2021-01-15 22:22:33 -06:00
#include "FeedEditWindow.h"
2021-01-13 20:23:21 -06:00
#include "FeedListItem.h"
2021-02-18 23:16:45 -06:00
#include "Notifier.h"
2021-01-13 20:23:21 -06:00
2021-03-21 18:47:58 -05:00
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "FeedsView"
FeedsView::FeedsView(const char* name)
:
BGroupView(name, B_VERTICAL, B_USE_DEFAULT_SPACING)
{
_InitInterface();
}
void
FeedsView::MessageReceived(BMessage* msg)
{
switch (msg->what)
{
2021-01-15 22:22:33 -06:00
case kFeedsAddButton:
{
FeedEditWindow* edit = new FeedEditWindow();
edit->Show();
edit->Activate();
break;
}
case kFeedsRemoveButton:
{
_RemoveSelectedFeed();
_PopulateFeedList();
2021-01-15 22:22:33 -06:00
break;
}
case kFeedsEditButton:
{
2021-03-23 13:18:27 -05:00
if (fFeedsListView->CurrentSelection() >= 0)
2021-03-21 14:21:14 -05:00
_EditSelectedFeed();
2021-01-15 22:22:33 -06:00
break;
}
case kFeedsSelected:
{
bool enabled = msg->GetInt32("index", -1) >= 0;
fEditButton->SetEnabled(enabled);
fRemoveButton->SetEnabled(enabled);
2021-01-15 22:22:33 -06:00
break;
}
case kFeedsEdited:
{
_PopulateFeedList();
2021-01-15 22:22:33 -06:00
}
2021-02-18 23:16:45 -06:00
case kDownloadStart:
{
_UpdateProgress(msg, kDownloadingStatus);
break;
2021-02-18 23:16:45 -06:00
}
case kDownloadComplete:
{
_UpdateProgress(msg, kParsingStatus);
break;
}
case kDownloadFail:
case kParseFail:
{
_UpdateProgress(msg, kErrorStatus);
break;
}
case kParseComplete:
{
_UpdateProgress(msg, kClearStatus);
break;
}
case kProgress:
{
int32 total,current = 0;
if (msg->FindInt32("total", &total) == B_OK
&& msg->FindInt32("current", &current) == B_OK)
{
2021-03-21 14:21:14 -05:00
if (total == current) {
fProgressLabel->SetText("");
2021-03-21 14:21:14 -05:00
_PopulateFeedList();
}
}
break;
}
}
}
void
FeedsView::_InitInterface()
{
// Feeds list
fFeedsListView = new BListView("feedsList");
fFeedsScrollView = new BScrollView("feedsScroll", fFeedsListView,
B_WILL_DRAW, false, true);
2021-01-15 22:22:33 -06:00
fFeedsListView->SetSelectionMessage(new BMessage(kFeedsSelected));
fFeedsListView->SetInvocationMessage(new BMessage(kFeedsEditButton));
_PopulateFeedList();
2021-02-18 23:31:52 -06:00
fProgressLabel = new BStringView("progressLabel", "");
2021-02-18 23:16:45 -06:00
// Add, Remove, Edit
2021-01-15 22:22:33 -06:00
fAddButton = new BButton("addFeed", "+", new BMessage(kFeedsAddButton));
fRemoveButton = new BButton("removeFeed", "-", new BMessage(kFeedsRemoveButton));
fAddButton->SetToolTip(B_TRANSLATE("Add new feed"));
fRemoveButton->SetToolTip(B_TRANSLATE("Remove selected feed"));
2021-03-21 18:47:58 -05:00
fEditButton = new BButton("editFeed", B_TRANSLATE("Edit…"),
new BMessage(kFeedsEditButton));
2021-01-13 20:23:21 -06:00
font_height fontHeight;
GetFontHeight(&fontHeight);
int16 buttonHeight = int16(fontHeight.ascent + fontHeight.descent + 12);
BSize charButtonSize(buttonHeight, buttonHeight);
fAddButton->SetExplicitSize(charButtonSize);
2021-01-15 22:22:33 -06:00
fAddButton->SetEnabled(true);
fRemoveButton->SetExplicitSize(charButtonSize);
2021-01-15 22:22:33 -06:00
fRemoveButton->SetEnabled(false);
fEditButton->SetExplicitSize(
BSize(fEditButton->ExplicitPreferredSize().Width(),
charButtonSize.Height()));
fEditButton->SetEnabled(false);
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.SetInsets(B_USE_DEFAULT_SPACING)
.Add(fFeedsScrollView)
.AddGroup(B_HORIZONTAL, 0, 0.0)
2021-01-15 22:22:33 -06:00
// Add and Remove buttons
.Add(new BSeparatorView(B_VERTICAL))
.AddGroup(B_VERTICAL, 0, 0.0)
.AddGroup(B_HORIZONTAL, 1, 0.0)
.SetInsets(1)
.Add(fAddButton)
.Add(fRemoveButton)
.End()
.Add(new BSeparatorView(B_HORIZONTAL))
.End()
.Add(new BSeparatorView(B_VERTICAL))
2021-02-18 23:16:45 -06:00
.AddGlue()
.Add(fProgressLabel)
.AddGlue()
2021-01-15 22:22:33 -06:00
// Edit button
.Add(new BSeparatorView(B_VERTICAL))
.AddGroup(B_VERTICAL, 0, 0.0)
.AddGroup(B_HORIZONTAL, 1, 0.0)
.SetInsets(1)
.Add(fEditButton)
.End()
.Add(new BSeparatorView(B_HORIZONTAL))
.End()
.Add(new BSeparatorView(B_VERTICAL))
.End();
}
2021-01-15 22:22:33 -06:00
void
FeedsView::_EditSelectedFeed()
{
int32 selIndex = fFeedsListView->CurrentSelection();
FeedListItem* selected = (FeedListItem*)fFeedsListView->ItemAt(selIndex);
FeedEditWindow* edit = new FeedEditWindow(selected);
edit->Show();
edit->Activate();
}
void
FeedsView::_RemoveSelectedFeed()
{
2021-03-24 13:06:00 -05:00
BAlert* alert = new BAlert(B_TRANSLATE("Confirm removal"),
B_TRANSLATE("Are you sure you want to remove the selected feed?"),
NULL, B_TRANSLATE("OK"), B_TRANSLATE("Cancel"),
B_WIDTH_AS_USUAL, B_WARNING_ALERT);
alert->SetShortcut(1, B_ESCAPE);
int32 button = alert->Go();
if (button != 0)
return;
2021-01-15 22:22:33 -06:00
int32 selIndex = fFeedsListView->CurrentSelection();
FeedListItem* selected = (FeedListItem*)fFeedsListView->ItemAt(selIndex);
2021-03-20 18:45:37 -05:00
Feed delFeed = Feed(BEntry(selected->FeedPath()));
2021-01-15 22:22:33 -06:00
delFeed.Unfiletize();
}
void
FeedsView::_PopulateFeedList()
{
2021-03-06 12:36:33 -06:00
BStringList feeds = FeedController::SubscribedFeeds();
int32 selected = fFeedsListView->CurrentSelection();
for (int i = fFeedsListView->CountItems(); i >= 0; i--)
delete ((FeedListItem*)fFeedsListView->RemoveItem(i));
2021-03-06 12:36:33 -06:00
for (int i = 0; i < feeds.CountStrings(); i++) {
Feed feed = Feed(feeds.StringAt(i).String());
FeedListItem* item = new FeedListItem(&feed);
fFeedsListView->AddItem(item);
}
if (fFeedsListView->CountItems() < selected)
selected = fFeedsListView->CountItems();
fFeedsListView->Select(selected);
}
2021-02-18 23:16:45 -06:00
void
FeedsView::_UpdateProgress(BMessage* msg, int8 status)
2021-02-18 23:16:45 -06:00
{
BString feedName, feedUrl;
if (msg->FindString("feed_url", &feedUrl) != B_OK)
return;
2021-03-21 18:47:58 -05:00
if (msg->FindString("feed_name", &feedName) != B_OK || feedName.IsEmpty())
feedName = feedUrl;
if (status == kDownloadingStatus) {
2021-03-21 18:47:58 -05:00
BString label(B_TRANSLATE("Fetching %source%…"));
label.ReplaceAll("%source%", feedName);
fProgressLabel->SetText(label);
}
for (int i = 0; i < fFeedsListView->CountItems(); i++) {
FeedListItem* item = (FeedListItem*)fFeedsListView->ItemAt(i);
2021-03-20 18:45:37 -05:00
if (item->FeedUrl().UrlString() == feedUrl) {
item->SetStatus(status);
fFeedsListView->InvalidateItem(i);
}
}
2021-02-18 23:16:45 -06:00
}