Pogger/src/UpdatesView.cpp

181 lines
3.9 KiB
C++
Raw Normal View History

/*
2021-01-25 19:39:31 -06:00
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "UpdatesView.h"
#include <Box.h>
#include <CheckBox.h>
#include <Message.h>
2021-01-25 19:39:31 -06:00
#include <MessageRunner.h>
#include <LayoutBuilder.h>
#include <Slider.h>
2021-01-25 19:39:31 -06:00
#include "App.h"
UpdatesView::UpdatesView(const char* name)
:
BGroupView(name, B_VERTICAL, B_USE_DEFAULT_SPACING)
{
_InitInterface();
}
void
UpdatesView::AttachedToWindow()
{
fNotifyNewCheck->SetTarget(this);
2021-01-25 19:39:31 -06:00
fNotifyFailCheck->SetTarget(this);
fIntervalSlider->SetTarget(this);
}
void
UpdatesView::MessageReceived(BMessage* msg)
{
switch (msg->what)
{
2021-01-25 19:39:31 -06:00
case kNotifyNewCheckbox:
{
if (fNotifyNewCheck->Value() == B_CONTROL_ON)
2021-01-28 00:19:36 -06:00
((App*)be_app)->fPreferences->fNewNotify = true;
2021-01-25 19:39:31 -06:00
else
2021-01-28 00:19:36 -06:00
((App*)be_app)->fPreferences->fNewNotify = false;
2021-01-25 19:39:31 -06:00
break;
}
case kNotifyFailCheckbox:
{
2021-01-25 19:39:31 -06:00
if (fNotifyFailCheck->Value() == B_CONTROL_ON)
2021-01-28 00:19:36 -06:00
((App*)be_app)->fPreferences->fFailureNotify = true;
2021-01-25 19:39:31 -06:00
else
2021-01-28 00:19:36 -06:00
((App*)be_app)->fPreferences->fFailureNotify = false;
2021-01-25 19:39:31 -06:00
break;
}
case kIntervalChanged:
{
_UpdateIntervalPreference();
_UpdateIntervalLabel();
break;
}
default:
{
2021-01-25 19:39:31 -06:00
BGroupView::MessageReceived(msg);
break;
}
}
}
void
UpdatesView::_InitInterface()
{
2021-01-25 19:39:31 -06:00
// Notifications
fNotificationsBox = new BBox("notifications");
fNotificationsBox->SetLabel("Notifications");
2021-01-25 19:39:31 -06:00
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",
2021-01-25 19:39:31 -06:00
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(kIntervalChanged));
2021-01-28 00:19:36 -06:00
2021-01-25 19:39:31 -06:00
// Display current settings
2021-01-28 00:19:36 -06:00
Preferences* prefs = ((App*)be_app)->fPreferences;
if (prefs->fNewNotify == true)
2021-01-25 19:39:31 -06:00
fNotifyNewCheck->SetValue(B_CONTROL_ON);
2021-01-28 00:19:36 -06:00
if (prefs->fFailureNotify == true)
2021-01-25 19:39:31 -06:00
fNotifyFailCheck->SetValue(B_CONTROL_ON);
2021-01-28 00:19:36 -06:00
fIntervalSlider->SetValue(prefs->UpdateIntervalIndex());
2021-01-25 19:39:31 -06:00
_UpdateIntervalLabel();
BLayoutBuilder::Group<>(fNotificationsBox, B_VERTICAL, B_USE_HALF_ITEM_SPACING)
.SetInsets(B_USE_ITEM_INSETS)
.AddStrut(B_USE_ITEM_SPACING)
.Add(fNotifyNewCheck)
2021-01-25 19:39:31 -06:00
.Add(fNotifyFailCheck)
.End();
BLayoutBuilder::Group<>(fSchedulingBox, B_VERTICAL, B_USE_HALF_ITEM_SPACING)
.SetInsets(B_USE_ITEM_INSETS)
.AddStrut(B_USE_ITEM_SPACING)
.Add(fIntervalSlider)
.End();
BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
.SetInsets(B_USE_DEFAULT_SPACING)
.Add(fNotificationsBox)
.AddGlue()
.Add(fSchedulingBox)
.AddGlue()
.End();
}
void
2021-01-25 19:39:31 -06:00
UpdatesView::_UpdateIntervalPreference()
{
int32 limit;
fIntervalSlider->GetLimits(NULL, &limit);
2021-01-25 19:39:31 -06:00
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()
{
int8 hours = ((App*)be_app)->fPreferences->UpdateIntervalIndex() - 1;
BString newLabel;
BString strHour;
2021-01-25 19:39:31 -06:00
strHour << 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());
}