Pogger/src/UpdatesView.cpp

182 lines
4.0 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>
#include <cstdio>
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)
((App*)be_app)->fPreferences->SetNotifyOnNew(true);
else
((App*)be_app)->fPreferences->SetNotifyOnNew(false);
break;
}
case kNotifyFailCheckbox:
{
2021-01-25 19:39:31 -06:00
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:
{
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('iiii'));
2021-01-25 19:39:31 -06:00
// 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)
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());
}