2021-01-01 12:16:42 -06:00
|
|
|
/*
|
2021-01-22 20:25:54 -06:00
|
|
|
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
|
2021-01-01 12:16:42 -06:00
|
|
|
* 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>
|
2021-01-01 12:16:42 -06:00
|
|
|
#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-01 12:16:42 -06:00
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
2021-01-01 12:16:42 -06:00
|
|
|
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();
|
2021-01-22 20:25:54 -06:00
|
|
|
_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:
|
|
|
|
{
|
2021-01-22 20:25:54 -06:00
|
|
|
bool enabled = msg->GetInt32("index", -1) >= 0;
|
|
|
|
fEditButton->SetEnabled(enabled);
|
|
|
|
fRemoveButton->SetEnabled(enabled);
|
2021-01-15 22:22:33 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kFeedsEdited:
|
|
|
|
{
|
2021-02-20 20:58:51 -06:00
|
|
|
_PopulateFeedList();
|
2021-01-15 22:22:33 -06:00
|
|
|
}
|
2021-02-18 23:16:45 -06:00
|
|
|
case kDownloadStart:
|
|
|
|
{
|
2021-02-27 13:20:03 -06:00
|
|
|
_UpdateProgress(msg, kDownloadingStatus);
|
|
|
|
break;
|
2021-02-18 23:16:45 -06:00
|
|
|
}
|
2021-02-27 13:20:03 -06:00
|
|
|
case kDownloadComplete:
|
2021-01-01 12:16:42 -06:00
|
|
|
{
|
2021-02-27 13:20:03 -06:00
|
|
|
_UpdateProgress(msg, kParsingStatus);
|
2021-01-01 12:16:42 -06:00
|
|
|
break;
|
|
|
|
}
|
2021-02-27 13:20:03 -06:00
|
|
|
case kDownloadFail:
|
|
|
|
case kParseFail:
|
|
|
|
{
|
|
|
|
_UpdateProgress(msg, kErrorStatus);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kParseComplete:
|
|
|
|
{
|
|
|
|
_UpdateProgress(msg, kClearStatus);
|
2021-03-21 12:10:58 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case kProgress:
|
|
|
|
{
|
|
|
|
int32 total,current = 0;
|
|
|
|
|
|
|
|
if (msg->FindInt32("total", &total) == B_OK
|
|
|
|
&& msg->FindInt32("current", ¤t) == B_OK)
|
|
|
|
{
|
2021-03-21 14:21:14 -05:00
|
|
|
if (total == current) {
|
2021-03-21 12:10:58 -05:00
|
|
|
fProgressLabel->SetText("");
|
2021-03-21 14:21:14 -05:00
|
|
|
_PopulateFeedList();
|
|
|
|
}
|
2021-03-21 12:10:58 -05:00
|
|
|
}
|
|
|
|
break;
|
2021-02-27 13:20:03 -06:00
|
|
|
}
|
2021-01-01 12:16:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
2021-01-01 12:16:42 -06:00
|
|
|
|
2021-01-22 20:25:54 -06:00
|
|
|
_PopulateFeedList();
|
2021-01-01 12:16:42 -06:00
|
|
|
|
2021-02-18 23:31:52 -06:00
|
|
|
fProgressLabel = new BStringView("progressLabel", "");
|
2021-02-18 23:16:45 -06:00
|
|
|
|
2021-01-01 12:16:42 -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));
|
2021-03-23 14:24:47 -05:00
|
|
|
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-01 12:16:42 -06:00
|
|
|
|
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);
|
|
|
|
|
2021-01-01 12:16:42 -06:00
|
|
|
fAddButton->SetExplicitSize(charButtonSize);
|
2021-01-15 22:22:33 -06:00
|
|
|
fAddButton->SetEnabled(true);
|
2021-01-01 12:16:42 -06:00
|
|
|
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);
|
2021-01-01 12:16:42 -06:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2021-01-01 12:16:42 -06:00
|
|
|
.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)
|
2021-01-01 12:16:42 -06:00
|
|
|
.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))
|
2021-01-01 12:16:42 -06:00
|
|
|
.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-22 20:25:54 -06:00
|
|
|
void
|
|
|
|
FeedsView::_PopulateFeedList()
|
|
|
|
{
|
2021-03-06 12:36:33 -06:00
|
|
|
BStringList feeds = FeedController::SubscribedFeeds();
|
2021-02-20 20:58:51 -06:00
|
|
|
int32 selected = fFeedsListView->CurrentSelection();
|
2021-01-22 20:25:54 -06:00
|
|
|
|
2021-02-21 13:16:35 -06:00
|
|
|
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);
|
2021-01-22 20:25:54 -06:00
|
|
|
fFeedsListView->AddItem(item);
|
|
|
|
}
|
2021-02-20 20:58:51 -06:00
|
|
|
|
|
|
|
if (fFeedsListView->CountItems() < selected)
|
|
|
|
selected = fFeedsListView->CountItems();
|
|
|
|
fFeedsListView->Select(selected);
|
2021-01-22 20:25:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-18 23:16:45 -06:00
|
|
|
void
|
2021-02-27 13:20:03 -06:00
|
|
|
FeedsView::_UpdateProgress(BMessage* msg, int8 status)
|
2021-02-18 23:16:45 -06:00
|
|
|
{
|
2021-02-27 13:20:03 -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())
|
2021-02-27 13:20:03 -06:00
|
|
|
feedName = feedUrl;
|
|
|
|
|
|
|
|
if (status == kDownloadingStatus) {
|
2021-03-21 18:47:58 -05:00
|
|
|
BString label(B_TRANSLATE("Fetching %source%…"));
|
|
|
|
label.ReplaceAll("%source%", feedName);
|
2021-02-27 13:20:03 -06:00
|
|
|
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) {
|
2021-02-27 13:20:03 -06:00
|
|
|
item->SetStatus(status);
|
|
|
|
fFeedsListView->InvalidateItem(i);
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 23:16:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|