From afcbea6fba090357addbc850851bd88422cb3278 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Mon, 25 Jan 2021 19:39:31 -0600 Subject: [PATCH] Make 'Updates' tab settings functional --- src/App.cpp | 19 +++++--- src/App.h | 5 +- src/EntriesView.cpp | 2 +- src/EntriesView.h | 2 +- src/FeedController.cpp | 2 +- src/Notifier.cpp | 9 ++++ src/Preferences.cpp | 94 +++++++++++++++++++++++++++++++----- src/Preferences.h | 30 ++++++++---- src/UpdatesView.cpp | 106 +++++++++++++++++++++++++++++++---------- src/UpdatesView.h | 14 +++++- 10 files changed, 223 insertions(+), 60 deletions(-) diff --git a/src/App.cpp b/src/App.cpp index c804a5e..a11d268 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -9,7 +9,7 @@ #include #include -#include +#include #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); } diff --git a/src/App.h b/src/App.h index 3914267..e5a2caf 100644 --- a/src/App.h +++ b/src/App.h @@ -5,7 +5,6 @@ #ifndef APP_H #define APP_H - #include #include @@ -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; diff --git a/src/EntriesView.cpp b/src/EntriesView.cpp index 689f0df..f0857af 100644 --- a/src/EntriesView.cpp +++ b/src/EntriesView.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2020, Jaidyn Levesque + * Copyright 2021, Jaidyn Levesque * All rights reserved. Distributed under the terms of the MIT license. */ diff --git a/src/EntriesView.h b/src/EntriesView.h index db4592f..f3ddaf1 100644 --- a/src/EntriesView.h +++ b/src/EntriesView.h @@ -1,5 +1,5 @@ /* - * Copyright 2020, Jaidyn Levesque + * Copyright 2021, Jaidyn Levesque * All rights reserved. Distributed under the terms of the MIT license. */ #ifndef ENTRIESVIEW_H diff --git a/src/FeedController.cpp b/src/FeedController.cpp index 4d88d17..02aa593 100644 --- a/src/FeedController.cpp +++ b/src/FeedController.cpp @@ -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)); diff --git a/src/Notifier.cpp b/src/Notifier.cpp index 36e4909..39a107a 100644 --- a/src/Notifier.cpp +++ b/src/Notifier.cpp @@ -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%"); diff --git a/src/Preferences.cpp b/src/Preferences.cpp index 0b3aa38..21ec035 100644 --- a/src/Preferences.cpp +++ b/src/Preferences.cpp @@ -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; +} + + diff --git a/src/Preferences.h b/src/Preferences.h index 3be961d..1c564f9 100644 --- a/src/Preferences.h +++ b/src/Preferences.h @@ -11,6 +11,9 @@ #include +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; }; diff --git a/src/UpdatesView.cpp b/src/UpdatesView.cpp index b599e1a..26c82bb 100644 --- a/src/UpdatesView.cpp +++ b/src/UpdatesView.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2020, Jaidyn Levesque + * Copyright 2021, Jaidyn Levesque * All rights reserved. Distributed under the terms of the MIT license. */ @@ -8,11 +8,14 @@ #include #include #include +#include #include #include #include +#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()); diff --git a/src/UpdatesView.h b/src/UpdatesView.h index 4a2e6e9..d89622c 100644 --- a/src/UpdatesView.h +++ b/src/UpdatesView.h @@ -1,5 +1,5 @@ /* - * Copyright 2020, Jaidyn Levesque + * Copyright 2021, Jaidyn Levesque * 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; };