2 * Copyright 2010-2017, Haiku, Inc. All Rights Reserved.
3 * Copyright 2009, Pier Luigi Fiorini.
4 * Distributed under the terms of the MIT License.
7 * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
8 * Brian Hill, supernova@tycho.email
20 #include <Directory.h>
22 #include <FindDirectory.h>
24 #include <LayoutBuilder.h>
32 #include <VolumeRoster.h>
34 #include <notification/Notifications.h>
36 #include "GeneralView.h"
37 #include "NotificationsConstants.h"
38 #include "SettingsHost.h"
40 #undef B_TRANSLATION_CONTEXT
41 #define B_TRANSLATION_CONTEXT "GeneralView"
43 const uint32 kToggleNotifications
= '_TSR';
44 const uint32 kWidthChanged
= '_WIC';
45 const uint32 kTimeoutChanged
= '_TIC';
46 const uint32 kServerChangeTriggered
= '_SCT';
47 const BString
kSampleMessageID("NotificationsSample");
50 GeneralView::GeneralView(SettingsHost
* host
)
52 SettingsPane("general", host
)
54 // Notification server
55 fNotificationBox
= new BCheckBox("server",
56 B_TRANSLATE("Enable notifications"),
57 new BMessage(kToggleNotifications
));
58 BBox
* box
= new BBox("box");
59 box
->SetLabel(fNotificationBox
);
62 int32 minWidth
= int32(kMinimumWidth
/ kWidthStep
);
63 int32 maxWidth
= int32(kMaximumWidth
/ kWidthStep
);
64 fWidthSlider
= new BSlider("width", B_TRANSLATE("Window width:"),
65 new BMessage(kWidthChanged
), minWidth
, maxWidth
, B_HORIZONTAL
);
66 fWidthSlider
->SetHashMarks(B_HASH_MARKS_BOTTOM
);
67 fWidthSlider
->SetHashMarkCount(maxWidth
- minWidth
+ 1);
68 BString minWidthLabel
;
69 minWidthLabel
<< int32(kMinimumWidth
);
70 BString maxWidthLabel
;
71 maxWidthLabel
<< int32(kMaximumWidth
);
72 fWidthSlider
->SetLimitLabels(
73 B_TRANSLATE_COMMENT(minWidthLabel
.String(), "Slider low text"),
74 B_TRANSLATE_COMMENT(maxWidthLabel
.String(), "Slider high text"));
77 fDurationSlider
= new BSlider("duration", B_TRANSLATE("Duration:"),
78 new BMessage(kTimeoutChanged
), kMinimumTimeout
, kMaximumTimeout
,
80 fDurationSlider
->SetHashMarks(B_HASH_MARKS_BOTTOM
);
81 fDurationSlider
->SetHashMarkCount(kMaximumTimeout
- kMinimumTimeout
+ 1);
83 minLabel
<< kMinimumTimeout
;
85 maxLabel
<< kMaximumTimeout
;
86 fDurationSlider
->SetLimitLabels(
87 B_TRANSLATE_COMMENT(minLabel
.String(), "Slider low text"),
88 B_TRANSLATE_COMMENT(maxLabel
.String(), "Slider high text"));
90 box
->AddChild(BLayoutBuilder::Group
<>(B_VERTICAL
)
91 .SetInsets(B_USE_DEFAULT_SPACING
)
97 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
98 .SetInsets(B_USE_WINDOW_SPACING
)
105 GeneralView::AttachedToWindow()
107 BView::AttachedToWindow();
108 fNotificationBox
->SetTarget(this);
109 fWidthSlider
->SetTarget(this);
110 fDurationSlider
->SetTarget(this);
115 GeneralView::MessageReceived(BMessage
* msg
)
118 case kToggleNotifications
:
120 SettingsPane::SettingsChanged(false);
124 case kWidthChanged
: {
125 int32 value
= fWidthSlider
->Value() * 50;
126 _SetWidthLabel(value
);
127 SettingsPane::SettingsChanged(true);
130 case kTimeoutChanged
:
132 int32 value
= fDurationSlider
->Value();
133 _SetTimeoutLabel(value
);
134 SettingsPane::SettingsChanged(true);
138 BView::MessageReceived(msg
);
145 GeneralView::Load(BMessage
& settings
)
147 bool autoStart
= settings
.GetBool(kAutoStartName
, true);
148 fNotificationBox
->SetValue(autoStart
? B_CONTROL_ON
: B_CONTROL_OFF
);
150 if (settings
.FindFloat(kWidthName
, &fOriginalWidth
) != B_OK
151 || fOriginalWidth
> kMaximumWidth
152 || fOriginalWidth
< kMinimumWidth
)
153 fOriginalWidth
= kDefaultWidth
;
155 if (settings
.FindInt32(kTimeoutName
, &fOriginalTimeout
) != B_OK
156 || fOriginalTimeout
> kMaximumTimeout
157 || fOriginalTimeout
< kMinimumTimeout
)
158 fOriginalTimeout
= kDefaultTimeout
;
159 // TODO need to save again if values outside of expected range
161 if (settings
.FindInt32(kIconSizeName
, &setting
) != B_OK
)
162 fOriginalIconSize
= kDefaultIconSize
;
164 fOriginalIconSize
= (icon_size
)setting
;
173 GeneralView::Save(BMessage
& settings
)
175 bool autoStart
= (fNotificationBox
->Value() == B_CONTROL_ON
);
176 settings
.AddBool(kAutoStartName
, autoStart
);
178 int32 timeout
= fDurationSlider
->Value();
179 settings
.AddInt32(kTimeoutName
, timeout
);
181 float width
= fWidthSlider
->Value() * 50;
182 settings
.AddFloat(kWidthName
, width
);
184 icon_size iconSize
= B_LARGE_ICON
;
185 settings
.AddInt32(kIconSizeName
, (int32
)iconSize
);
192 GeneralView::Revert()
194 fDurationSlider
->SetValue(fOriginalTimeout
);
195 _SetTimeoutLabel(fOriginalTimeout
);
197 fWidthSlider
->SetValue(fOriginalWidth
/ 50);
198 _SetWidthLabel(fOriginalWidth
);
205 GeneralView::RevertPossible()
207 int32 timeout
= fDurationSlider
->Value();
208 if (fOriginalTimeout
!= timeout
)
211 int32 width
= fWidthSlider
->Value() * 50;
212 if (fOriginalWidth
!= width
)
220 GeneralView::Defaults()
222 fDurationSlider
->SetValue(kDefaultTimeout
);
223 _SetTimeoutLabel(kDefaultTimeout
);
225 fWidthSlider
->SetValue(kDefaultWidth
/ 50);
226 _SetWidthLabel(kDefaultWidth
);
233 GeneralView::DefaultsPossible()
235 int32 timeout
= fDurationSlider
->Value();
236 if (kDefaultTimeout
!= timeout
)
239 int32 width
= fWidthSlider
->Value() * 50;
240 if (kDefaultWidth
!= width
)
248 GeneralView::UseDefaultRevertButtons()
255 GeneralView::_EnableControls()
257 bool enabled
= fNotificationBox
->Value() == B_CONTROL_ON
;
258 fWidthSlider
->SetEnabled(enabled
);
259 fDurationSlider
->SetEnabled(enabled
);
264 GeneralView::_SetTimeoutLabel(int32 value
)
266 BString
label(B_TRANSLATE("Timeout:"));
269 label
.Append(" ").Append(B_TRANSLATE("seconds"));
270 fDurationSlider
->SetLabel(label
.String());
275 GeneralView::_SetWidthLabel(int32 value
)
277 BString
label(B_TRANSLATE("Width:"));
280 label
.Append(" ").Append(B_TRANSLATE("pixels"));
281 fWidthSlider
->SetLabel(label
.String());
286 GeneralView::_IsServerRunning()
288 return be_roster
->IsRunning(kNotificationServerSignature
);