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 kPositionChanged
= '_NPC';
47 const uint32 kServerChangeTriggered
= '_SCT';
48 const BString
kSampleMessageID("NotificationsSample");
52 notification_position_to_index(uint32 notification_position
) {
53 if (notification_position
== B_FOLLOW_NONE
)
55 else if (notification_position
== (B_FOLLOW_RIGHT
| B_FOLLOW_BOTTOM
))
57 else if (notification_position
== (B_FOLLOW_LEFT
| B_FOLLOW_BOTTOM
))
59 else if (notification_position
== (B_FOLLOW_RIGHT
| B_FOLLOW_TOP
))
61 else if (notification_position
== (B_FOLLOW_LEFT
| B_FOLLOW_TOP
))
67 GeneralView::GeneralView(SettingsHost
* host
)
69 SettingsPane("general", host
)
71 // Notification server
72 fNotificationBox
= new BCheckBox("server",
73 B_TRANSLATE("Enable notifications"),
74 new BMessage(kToggleNotifications
));
75 BBox
* box
= new BBox("box");
76 box
->SetLabel(fNotificationBox
);
79 int32 minWidth
= int32(kMinimumWidth
/ kWidthStep
);
80 int32 maxWidth
= int32(kMaximumWidth
/ kWidthStep
);
81 fWidthSlider
= new BSlider("width", B_TRANSLATE("Window width:"),
82 new BMessage(kWidthChanged
), minWidth
, maxWidth
, B_HORIZONTAL
);
83 fWidthSlider
->SetHashMarks(B_HASH_MARKS_BOTTOM
);
84 fWidthSlider
->SetHashMarkCount(maxWidth
- minWidth
+ 1);
85 BString minWidthLabel
;
86 minWidthLabel
<< int32(kMinimumWidth
);
87 BString maxWidthLabel
;
88 maxWidthLabel
<< int32(kMaximumWidth
);
89 fWidthSlider
->SetLimitLabels(
90 B_TRANSLATE_COMMENT(minWidthLabel
.String(), "Slider low text"),
91 B_TRANSLATE_COMMENT(maxWidthLabel
.String(), "Slider high text"));
94 fDurationSlider
= new BSlider("duration", B_TRANSLATE("Duration:"),
95 new BMessage(kTimeoutChanged
), kMinimumTimeout
, kMaximumTimeout
,
97 fDurationSlider
->SetHashMarks(B_HASH_MARKS_BOTTOM
);
98 fDurationSlider
->SetHashMarkCount(kMaximumTimeout
- kMinimumTimeout
+ 1);
100 minLabel
<< kMinimumTimeout
;
102 maxLabel
<< kMaximumTimeout
;
103 fDurationSlider
->SetLimitLabels(
104 B_TRANSLATE_COMMENT(minLabel
.String(), "Slider low text"),
105 B_TRANSLATE_COMMENT(maxLabel
.String(), "Slider high text"));
107 // Notification Position
108 fPositionMenu
= new BPopUpMenu(B_TRANSLATE("Follow Deskbar"));
109 const char* positionLabels
[] = {
110 B_TRANSLATE_MARK("Follow Deskbar"),
111 B_TRANSLATE_MARK("Lower right"),
112 B_TRANSLATE_MARK("Lower left"),
113 B_TRANSLATE_MARK("Upper right"),
114 B_TRANSLATE_MARK("Upper left")
116 const uint32 positions
[] = {
117 B_FOLLOW_DESKBAR
, // Follow Deskbar
118 B_FOLLOW_BOTTOM
| B_FOLLOW_RIGHT
, // Lower right
119 B_FOLLOW_BOTTOM
| B_FOLLOW_LEFT
, // Lower left
120 B_FOLLOW_TOP
| B_FOLLOW_RIGHT
, // Upper right
121 B_FOLLOW_TOP
| B_FOLLOW_LEFT
// Upper left
123 for (int i
=0; i
< 5; i
++) {
124 BMessage
* message
= new BMessage(kPositionChanged
);
125 message
->AddInt32(kNotificationPositionName
, positions
[i
]);
127 fPositionMenu
->AddItem(new BMenuItem(B_TRANSLATE_NOCOLLECT(
128 positionLabels
[i
]), message
));
130 BMenuField
* positionField
= new BMenuField(B_TRANSLATE("Position:"),
133 box
->AddChild(BLayoutBuilder::Group
<>(B_VERTICAL
)
134 .SetInsets(B_USE_DEFAULT_SPACING
)
136 .Add(fDurationSlider
)
141 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
142 .SetInsets(B_USE_WINDOW_SPACING
)
149 GeneralView::AttachedToWindow()
151 BView::AttachedToWindow();
152 fNotificationBox
->SetTarget(this);
153 fWidthSlider
->SetTarget(this);
154 fDurationSlider
->SetTarget(this);
155 fPositionMenu
->SetTargetForItems(this);
160 GeneralView::MessageReceived(BMessage
* msg
)
163 case kToggleNotifications
:
165 SettingsPane::SettingsChanged(false);
169 case kWidthChanged
: {
170 int32 value
= fWidthSlider
->Value() * 50;
171 _SetWidthLabel(value
);
172 SettingsPane::SettingsChanged(true);
175 case kTimeoutChanged
:
177 int32 value
= fDurationSlider
->Value();
178 _SetTimeoutLabel(value
);
179 SettingsPane::SettingsChanged(true);
182 case kPositionChanged
:
185 if (msg
->FindInt32(kNotificationPositionName
, &position
) == B_OK
) {
186 fNewPosition
= position
;
187 SettingsPane::SettingsChanged(true);
192 BView::MessageReceived(msg
);
199 GeneralView::Load(BMessage
& settings
)
201 bool autoStart
= settings
.GetBool(kAutoStartName
, true);
202 fNotificationBox
->SetValue(autoStart
? B_CONTROL_ON
: B_CONTROL_OFF
);
204 if (settings
.FindFloat(kWidthName
, &fOriginalWidth
) != B_OK
205 || fOriginalWidth
> kMaximumWidth
206 || fOriginalWidth
< kMinimumWidth
)
207 fOriginalWidth
= kDefaultWidth
;
209 if (settings
.FindInt32(kTimeoutName
, &fOriginalTimeout
) != B_OK
210 || fOriginalTimeout
> kMaximumTimeout
211 || fOriginalTimeout
< kMinimumTimeout
)
212 fOriginalTimeout
= kDefaultTimeout
;
213 // TODO need to save again if values outside of expected range
215 if (settings
.FindInt32(kIconSizeName
, &setting
) != B_OK
)
216 fOriginalIconSize
= kDefaultIconSize
;
218 fOriginalIconSize
= (icon_size
)setting
;
221 if (settings
.FindInt32(kNotificationPositionName
, &position
) != B_OK
)
222 fOriginalPosition
= kDefaultNotificationPosition
;
224 fOriginalPosition
= position
;
233 GeneralView::Save(BMessage
& settings
)
235 bool autoStart
= (fNotificationBox
->Value() == B_CONTROL_ON
);
236 settings
.AddBool(kAutoStartName
, autoStart
);
238 int32 timeout
= fDurationSlider
->Value();
239 settings
.AddInt32(kTimeoutName
, timeout
);
241 float width
= fWidthSlider
->Value() * 50;
242 settings
.AddFloat(kWidthName
, width
);
244 icon_size iconSize
= B_LARGE_ICON
;
245 settings
.AddInt32(kIconSizeName
, (int32
)iconSize
);
247 settings
.AddInt32(kNotificationPositionName
, (int32
)fNewPosition
);
254 GeneralView::Revert()
256 fDurationSlider
->SetValue(fOriginalTimeout
);
257 _SetTimeoutLabel(fOriginalTimeout
);
259 fWidthSlider
->SetValue(fOriginalWidth
/ 50);
260 _SetWidthLabel(fOriginalWidth
);
262 fNewPosition
= fOriginalPosition
;
263 BMenuItem
* item
= fPositionMenu
->ItemAt(
264 notification_position_to_index(fNewPosition
));
266 item
->SetMarked(true);
273 GeneralView::RevertPossible()
275 int32 timeout
= fDurationSlider
->Value();
276 if (fOriginalTimeout
!= timeout
)
279 int32 width
= fWidthSlider
->Value() * 50;
280 if (fOriginalWidth
!= width
)
283 if (fOriginalPosition
!= fNewPosition
)
291 GeneralView::Defaults()
293 fDurationSlider
->SetValue(kDefaultTimeout
);
294 _SetTimeoutLabel(kDefaultTimeout
);
296 fWidthSlider
->SetValue(kDefaultWidth
/ 50);
297 _SetWidthLabel(kDefaultWidth
);
299 fNewPosition
= kDefaultNotificationPosition
;
300 BMenuItem
* item
= fPositionMenu
->ItemAt(
301 notification_position_to_index(fNewPosition
));
303 item
->SetMarked(true);
310 GeneralView::DefaultsPossible()
312 int32 timeout
= fDurationSlider
->Value();
313 if (kDefaultTimeout
!= timeout
)
316 int32 width
= fWidthSlider
->Value() * 50;
317 if (kDefaultWidth
!= width
)
320 if (kDefaultNotificationPosition
!= fNewPosition
)
328 GeneralView::UseDefaultRevertButtons()
335 GeneralView::_EnableControls()
337 bool enabled
= fNotificationBox
->Value() == B_CONTROL_ON
;
338 fWidthSlider
->SetEnabled(enabled
);
339 fDurationSlider
->SetEnabled(enabled
);
340 BMenuItem
* item
= fPositionMenu
->ItemAt(
341 notification_position_to_index(fOriginalPosition
));
343 item
->SetMarked(true);
348 GeneralView::_SetTimeoutLabel(int32 value
)
350 BString
label(B_TRANSLATE("Timeout:"));
353 label
.Append(" ").Append(B_TRANSLATE("seconds"));
354 fDurationSlider
->SetLabel(label
.String());
359 GeneralView::_SetWidthLabel(int32 value
)
361 BString
label(B_TRANSLATE("Width:"));
364 label
.Append(" ").Append(B_TRANSLATE("pixels"));
365 fWidthSlider
->SetLabel(label
.String());
370 GeneralView::_IsServerRunning()
372 return be_roster
->IsRunning(kNotificationServerSignature
);