2 * Copyright 2010-2013, 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
16 #include <GroupLayout.h>
17 #include <GroupLayoutBuilder.h>
22 #include <StringView.h>
23 #include <TextControl.h>
26 #include <FindDirectory.h>
28 #include <notification/Notifications.h>
31 #include <Directory.h>
32 #include <VolumeRoster.h>
37 #include "GeneralView.h"
38 #include "SettingsHost.h"
40 #undef B_TRANSLATION_CONTEXT
41 #define B_TRANSLATION_CONTEXT "GeneralView"
42 const int32 kToggleNotifications
= '_TSR';
44 GeneralView::GeneralView(SettingsHost
* host
)
46 SettingsPane("general", host
)
49 fNotificationBox
= new BCheckBox("server",
50 B_TRANSLATE("Enable notifications"),
51 new BMessage(kToggleNotifications
));
54 fAutoStart
= new BCheckBox("autostart",
55 B_TRANSLATE("Enable notifications at startup"),
56 new BMessage(kSettingChanged
));
59 fTimeout
= new BTextControl(B_TRANSLATE("Hide notifications from screen"
60 " after"), NULL
, new BMessage(kSettingChanged
));
61 BStringView
* displayTimeLabel
= new BStringView("dt_label",
62 B_TRANSLATE("seconds of inactivity"));
65 // TODO: Here will come a screen representation with the four corners
69 float inset
= ceilf(be_plain_font
->Size() * 0.7f
);
71 SetLayout(new BGroupLayout(B_VERTICAL
));
72 AddChild(BGroupLayoutBuilder(B_VERTICAL
, inset
)
73 .AddGroup(B_HORIZONTAL
, inset
)
74 .Add(fNotificationBox
)
78 .AddGroup(B_VERTICAL
, inset
)
80 .AddGroup(B_HORIZONTAL
)
81 .AddGroup(B_HORIZONTAL
, 2)
83 .Add(displayTimeLabel
)
87 .SetInsets(inset
, inset
, inset
, inset
)
94 GeneralView::AttachedToWindow()
96 fNotificationBox
->SetTarget(this);
97 fAutoStart
->SetTarget(this);
98 fTimeout
->SetTarget(this);
103 GeneralView::MessageReceived(BMessage
* msg
)
106 case kToggleNotifications
:
110 // Check if server is available
111 if (!_CanFindServer(&ref
)) {
112 BAlert
* alert
= new BAlert(B_TRANSLATE("Notifications"),
113 B_TRANSLATE("The notifications server cannot be"
114 " found, this means your InfoPopper installation was"
115 " not successfully completed."), B_TRANSLATE("OK"),
116 NULL
, NULL
, B_WIDTH_AS_USUAL
, B_STOP_ALERT
);
117 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
122 if (fNotificationBox
->Value() == B_CONTROL_OFF
) {
124 team_id team
= be_roster
->TeamFor(kNotificationServerSignature
);
126 // Establish a connection to infopopper_server
127 status_t ret
= B_ERROR
;
128 BMessenger
messenger(kNotificationServerSignature
, team
, &ret
);
130 BAlert
* alert
= new BAlert(B_TRANSLATE(
131 "Notifications"), B_TRANSLATE("Notifications "
132 "cannot be stopped, because the server can't be"
133 " reached."), B_TRANSLATE("OK"), NULL
, NULL
,
134 B_WIDTH_AS_USUAL
, B_STOP_ALERT
);
135 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
141 if (messenger
.SendMessage(B_QUIT_REQUESTED
) != B_OK
) {
142 BAlert
* alert
= new BAlert(B_TRANSLATE(
143 "Notifications"), B_TRANSLATE("Cannot disable"
144 " notifications because the server can't be "
145 "reached."), B_TRANSLATE("OK"),
146 NULL
, NULL
, B_WIDTH_AS_USUAL
, B_STOP_ALERT
);
147 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
151 } else if (!_IsServerRunning()) {
153 status_t err
= be_roster
->Launch(kNotificationServerSignature
);
155 BAlert
* alert
= new BAlert(B_TRANSLATE(
156 "Notifications"), B_TRANSLATE("Cannot enable"
157 " notifications because the server cannot be "
158 "found.\nThis means your InfoPopper installation"
159 " was not successfully completed."),
160 B_TRANSLATE("OK"), NULL
, NULL
, B_WIDTH_AS_USUAL
,
162 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
169 case kSettingChanged
:
170 SettingsPane::MessageReceived(msg
);
173 BView::MessageReceived(msg
);
180 GeneralView::Load(BMessage
& settings
)
184 fNotificationBox
->SetValue(_IsServerRunning() ? B_CONTROL_ON
: B_CONTROL_OFF
);
187 if (settings
.FindBool(kAutoStartName
, &autoStart
) != B_OK
)
188 autoStart
= kDefaultAutoStart
;
189 fAutoStart
->SetValue(autoStart
? B_CONTROL_ON
: B_CONTROL_OFF
);
192 if (settings
.FindInt32(kTimeoutName
, &timeout
) != B_OK
)
193 timeout
= kDefaultTimeout
;
194 (void)sprintf(buffer
, "%" B_PRId32
, timeout
);
195 fTimeout
->SetText(buffer
);
202 GeneralView::Save(BMessage
& settings
)
204 bool autoStart
= (fAutoStart
->Value() == B_CONTROL_ON
);
205 settings
.AddBool(kAutoStartName
, autoStart
);
207 int32 timeout
= atol(fTimeout
->Text());
208 settings
.AddInt32(kTimeoutName
, timeout
);
215 GeneralView::Revert()
222 GeneralView::_CanFindServer(entry_ref
* ref
)
224 // Try searching with be_roster
225 if (be_roster
->FindApp(kNotificationServerSignature
, ref
) == B_OK
)
228 // Try with a query and take the first result
229 BVolumeRoster vroster
;
231 char volName
[B_FILE_NAME_LENGTH
];
235 while (vroster
.GetNextVolume(&volume
) == B_OK
) {
236 if ((volume
.InitCheck() != B_OK
) || !volume
.KnowsQuery())
239 volume
.GetName(volName
);
241 BQuery
*query
= new BQuery();
242 query
->SetPredicate("(BEOS:APP_SIG==\"" kNotificationServerSignature
244 query
->SetVolume(&volume
);
247 if (query
->GetNextRef(ref
) == B_OK
)
256 GeneralView::_IsServerRunning()
258 return be_roster
->IsRunning(kNotificationServerSignature
);