Make 'Updates' tab settings functional

This commit is contained in:
Jaidyn Ann 2021-01-25 19:39:31 -06:00
parent 0066b9a522
commit afcbea6fba
10 changed files with 223 additions and 60 deletions

View File

@ -9,7 +9,7 @@
#include <StorageKit.h>
#include <String.h>
#include <getopt.h>
#include <iostream>
#include "AtomFeed.h"
#include "Entry.h"
@ -42,14 +42,21 @@ App::App() : BApplication("application/x-vnd.Pogger")
fPreferences->Load();
fMainWindow = new MainWindow();
fNotifier = new Notifier();
fFeedController = new FeedController();
fMainWindow->Show();
fNotifier = new Notifier();
fFeedController = new FeedController();
BMessage* updateMessage = new BMessage(kUpdateSubscribed);
// MessageReceived(updateMessage);
fUpdateRunner = new BMessageRunner(this, updateMessage,
fPreferences->updateInterval);
int64 interval = fPreferences->UpdateInterval();
int32 count = -1;
if (interval == -1)
count = 0;
// else
// MessageReceived(updateMessage);
fUpdateRunner = new BMessageRunner(this, updateMessage, interval, count);
}

View File

@ -5,7 +5,6 @@
#ifndef APP_H
#define APP_H
#include <SupportDefs.h>
#include <Application.h>
@ -24,7 +23,6 @@ public:
App(void);
void MessageReceived(BMessage* msg);
void ArgvReceived(int32 argc, char** argv);
MainWindow* fMainWindow;
Preferences* fPreferences;
@ -35,7 +33,8 @@ private:
FeedController* fFeedController;
};
int main ( int, char** );
int main(int argc, char** argv);
extern const char* configPath;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef ENTRIESVIEW_H

View File

@ -150,7 +150,7 @@ FeedController::_ParseLoop(void* ignored)
BList entries;
BString feedTitle;
BUrl feedUrl = feedBuffer->GetXmlUrl();
BDirectory outDir = BDirectory(((App*)be_app)->fPreferences->outDir);
BDirectory outDir = BDirectory(((App*)be_app)->fPreferences->fEntryDir);
if (feedBuffer->IsAtom()) {
AtomFeed* feed = (AtomFeed*)malloc(sizeof(AtomFeed));

View File

@ -73,6 +73,9 @@ Notifier::MessageReceived(BMessage* msg)
void
Notifier::_NewEntryNotification(BString feedName, int32 entryCount)
{
if (((App*)be_app)->fPreferences->NotifyOnNew() == false)
return;
BNotification notifyNew(B_INFORMATION_NOTIFICATION);
BString notifyLabel("New Feed Entries");
BString notifyText("%n% new entries from %source%");
@ -91,6 +94,9 @@ Notifier::_NewEntryNotification(BString feedName, int32 entryCount)
void
Notifier::_ParseFailNotification(BString feedUrl)
{
if (((App*)be_app)->fPreferences->NotifyOnFailure() == false)
return;
BNotification notifyError(B_ERROR_NOTIFICATION);
BString notifyText("Failed to parse feed from %url%");
@ -108,6 +114,9 @@ Notifier::_ParseFailNotification(BString feedUrl)
void
Notifier::_DownloadFailNotification(BString feedUrl)
{
if (((App*)be_app)->fPreferences->NotifyOnFailure() == false)
return;
BNotification notifyError(B_ERROR_NOTIFICATION);
BString notifyText("Failed to download feed from %url%");

View File

@ -22,29 +22,32 @@ Preferences::Preferences(Preferences* pref) {
void
Preferences::Load()
{
configDir = BString("/boot/home/config/settings/Pogger/");
BString configDir("/boot/home/config/settings/Pogger/");
BString filename = BString(configDir);
filename.Append("settings");
filename.Append("Settings");
BFile file(filename.String(), B_READ_ONLY);
status_t result = file.InitCheck();
BMessage storage;
storage.Unflatten(&file);
updateInterval = storage.GetInt64("updateInterval", 3600000000);
outDir = BString(storage.GetString("outDir", "/boot/home/feeds/"));
cacheDir = BString(storage.GetString("cacheDir",
"/boot/home/config/cache/Pogger/"));
fEntryDir = BString(storage.GetString("entryDir", "/boot/home/feeds/"));
fEntryFileExt = BString(storage.GetString("entryExt", ""));
fOpenAsHtml = storage.GetBool("openAsHtml", false);
fOpenWith = BString(storage.GetString("openWith", "WebPositive"));
fNewNotify = storage.GetBool("notifyNew", true);
fFailureNotify = storage.GetBool("notifyFailure", true);
fUpdateInterval = storage.GetInt8("updateInterval", 1);
}
// !! handle file status
void
Preferences::Save ()
Preferences::Save()
{
if (configDir == NULL)
configDir = BString("/boot/home/config/settings/Pogger/");
BString configDir = BString("/boot/home/config/settings/Pogger/");
BPath* cfgPath = new BPath(configDir.String(), NULL, true);
BEntry* cfgEntry = new BEntry(cfgPath->Path());
@ -56,16 +59,81 @@ Preferences::Save ()
cfgDir->CreateDirectory(cfgPath->Path(), NULL);
BMessage storage;
BString filename = BString(configDir).Append("/settings");
BString filename = BString(configDir).Append("/Settings");
BFile* file = new BFile(filename.String(), B_WRITE_ONLY | B_CREATE_FILE
| B_ERASE_FILE);
status_t result = file->InitCheck();
storage.AddFloat("updateInterval", updateInterval);
storage.AddString("outDir", outDir.String());
storage.AddString("cacheDir", cacheDir.String());
storage.AddString("entryDir", fEntryDir.String());
storage.AddString("entryExt", fEntryFileExt.String());
storage.AddBool("openAsHtml", fOpenAsHtml);
storage.AddString("openWith", fOpenWith.String());
storage.AddBool("notifyNew", fNewNotify);
storage.AddBool("notifyFailure", fFailureNotify);
storage.AddInt8("updateInterval", fUpdateInterval);
storage.Flatten(file);
}
int64
Preferences::UpdateInterval()
{
int hours = fUpdateInterval - 1;
switch (hours)
{
case -1:
return -1;
case 0:
return HOUR_IN_MICROSECONDS / 2;
default:
return HOUR_IN_MICROSECONDS * hours;
}
}
int
Preferences::UpdateIntervalIndex()
{
return fUpdateInterval;
}
void
Preferences::SetUpdateIntervalIndex(int8 index)
{
fUpdateInterval = index;
}
bool
Preferences::NotifyOnFailure()
{
return fFailureNotify;
}
bool
Preferences::NotifyOnNew()
{
return fNewNotify;
}
void
Preferences::SetNotifyOnFailure(bool value)
{
fFailureNotify = value;
}
void
Preferences::SetNotifyOnNew(bool value)
{
fNewNotify = value;
}

View File

@ -11,6 +11,9 @@
#include <StorageKit.h>
const int64 HOUR_IN_MICROSECONDS = 3600000000;
class Preferences {
public:
Preferences();
@ -18,17 +21,28 @@ public:
void Load();
void Save();
int64 UpdateInterval();
int UpdateIntervalIndex();
void SetUpdateIntervalIndex(int8 index);
bool NotifyOnFailure();
bool NotifyOnNew();
void SetNotifyOnFailure(bool value);
void SetNotifyOnNew(bool value);
bool verbose;
bool daemon;
BString outDir;
BString fEntryDir;
BString fEntryFileExt;
bool fOpenAsHtml;
BString fOpenWith;
int64 updateInterval;
BString configDir;
BString cacheDir;
bool will_save;
bool updateFeeds;
private:
int8 fUpdateInterval;
bool fNewNotify;
bool fFailureNotify;
};

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
@ -8,11 +8,14 @@
#include <Box.h>
#include <CheckBox.h>
#include <Message.h>
#include <MessageRunner.h>
#include <LayoutBuilder.h>
#include <Slider.h>
#include <cstdio>
#include "App.h"
UpdatesView::UpdatesView(const char* name)
:
@ -26,7 +29,7 @@ void
UpdatesView::AttachedToWindow()
{
fNotifyNewCheck->SetTarget(this);
fNotifyErrorCheck->SetTarget(this);
fNotifyFailCheck->SetTarget(this);
fIntervalSlider->SetTarget(this);
}
@ -36,14 +39,31 @@ UpdatesView::MessageReceived(BMessage* msg)
{
switch (msg->what)
{
case 'iiii':
case kNotifyNewCheckbox:
{
if (fNotifyNewCheck->Value() == B_CONTROL_ON)
((App*)be_app)->fPreferences->SetNotifyOnNew(true);
else
((App*)be_app)->fPreferences->SetNotifyOnNew(false);
break;
}
case kNotifyFailCheckbox:
{
if (fNotifyFailCheck->Value() == B_CONTROL_ON)
((App*)be_app)->fPreferences->SetNotifyOnFailure(true);
else
((App*)be_app)->fPreferences->SetNotifyOnFailure(false);
break;
}
case kIntervalChanged:
{
_UpdateIntervalPreference();
_UpdateIntervalLabel();
break;
}
default:
{
// BWindow::MessageReceived(msg);
BGroupView::MessageReceived(msg);
break;
}
}
@ -53,32 +73,44 @@ UpdatesView::MessageReceived(BMessage* msg)
void
UpdatesView::_InitInterface()
{
fNotifyNewCheck = new BCheckBox("newNotify", "Notify about new entries",
new BMessage('nnnc'));
fNotifyErrorCheck = new BCheckBox("errorNotify",
"Notify about update failures", new BMessage('nnnc'));
// Notifications
fNotificationsBox = new BBox("notifications");
fNotificationsBox->SetLabel("Notifications");
fNotifyNewCheck = new BCheckBox("newNotify", "Notify about new entries",
new BMessage(kNotifyNewCheckbox));
fNotifyFailCheck = new BCheckBox("errorNotify",
"Notify about update failures", new BMessage(kNotifyFailCheckbox));
// Update scheduling
fSchedulingBox = new BBox("scheduling");
fSchedulingBox->SetLabel("Scheduling");
fIntervalSlider =
new BSlider("interval", "Never automatically update",
new BMessage('iiii'), 0, 25, B_HORIZONTAL);
new BMessage(kIntervalChanged), 0, 25, B_HORIZONTAL);
fIntervalSlider->SetHashMarkCount(26);
fIntervalSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
fIntervalSlider->SetLimitLabels("Never", "24 hours");
fIntervalSlider->SetModificationMessage(new BMessage('iiii'));
fSchedulingBox = new BBox("scheduling");
fSchedulingBox->SetLabel("Scheduling");
// Display current settings
if (((App*)be_app)->fPreferences->NotifyOnNew() == true)
fNotifyNewCheck->SetValue(B_CONTROL_ON);
if (((App*)be_app)->fPreferences->NotifyOnFailure() == true)
fNotifyFailCheck->SetValue(B_CONTROL_ON);
fIntervalSlider->SetValue(((App*)be_app)->fPreferences->UpdateIntervalIndex());
_UpdateIntervalLabel();
BLayoutBuilder::Group<>(fNotificationsBox, B_VERTICAL, B_USE_HALF_ITEM_SPACING)
.SetInsets(B_USE_ITEM_INSETS)
.AddStrut(B_USE_ITEM_SPACING)
.Add(fNotifyNewCheck)
.Add(fNotifyErrorCheck)
.Add(fNotifyFailCheck)
.End();
BLayoutBuilder::Group<>(fSchedulingBox, B_VERTICAL, B_USE_HALF_ITEM_SPACING)
@ -94,29 +126,53 @@ UpdatesView::_InitInterface()
.Add(fSchedulingBox)
.AddGlue()
.End();
}
_UpdateIntervalLabel();
void
UpdatesView::_UpdateIntervalPreference()
{
int32 limit;
fIntervalSlider->GetLimits(NULL, &limit);
int8 index = fIntervalSlider->Position() / (1/(float)limit);
int8 oldIndex = ((App*)be_app)->fPreferences->UpdateIntervalIndex();
((App*)be_app)->fPreferences->SetUpdateIntervalIndex(index);
if (oldIndex == 0)
((App*)be_app)->fUpdateRunner->SetCount(-1);
else if (index == 0)
((App*)be_app)->fUpdateRunner->SetCount(0);
((App*)be_app)->fUpdateRunner->SetInterval(
((App*)be_app)->fPreferences->UpdateInterval());
}
void
UpdatesView::_UpdateIntervalLabel()
{
int32 limit;
fIntervalSlider->GetLimits(NULL, &limit);
int index = fIntervalSlider->Position() / (1/(float)limit);
int hours = index - 1;
int8 hours = ((App*)be_app)->fPreferences->UpdateIntervalIndex() - 1;
BString newLabel;
BString strHour;
strHour << (int)hours;
strHour << hours;
if (hours == -1 )
newLabel = "Never automatically update";
else if (hours == 0)
newLabel = "Update every 30 minutes";
else
newLabel = "Update every %hour% hours";
switch (hours)
{
case -1:
{
newLabel = "Never automatically update";
break;
}
case 0:
{
newLabel = "Update every 30 minutes";
break;
}
default:
newLabel = "Update every %hour% hours";
}
newLabel.ReplaceAll("%hour%", strHour);
fIntervalSlider->SetLabel(newLabel.String());

View File

@ -1,5 +1,5 @@
/*
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef UPDATESVIEW_H
@ -15,6 +15,14 @@ class BMessage;
class BSlider;
enum
{
kIntervalChanged = 'intu',
kNotifyNewCheckbox = 'chnn',
kNotifyFailCheckbox = 'chnf'
};
class UpdatesView : public BGroupView {
public:
UpdatesView(const char* name);
@ -24,13 +32,15 @@ public:
private:
void _InitInterface();
void _UpdateIntervalPreference();
void _UpdateIntervalLabel();
BBox* fNotificationsBox;
BBox* fSchedulingBox;
BCheckBox* fNotifyNewCheck;
BCheckBox* fNotifyErrorCheck;
BCheckBox* fNotifyFailCheck;
BSlider* fIntervalSlider;
};