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
11 #include "PrefletWin.h"
14 #include <Application.h>
17 #include <FindDirectory.h>
18 #include <Notification.h>
20 #include <SeparatorView.h>
22 #include <notification/Notifications.h>
24 #include "NotificationsConstants.h"
25 #include "PrefletView.h"
28 #undef B_TRANSLATION_CONTEXT
29 #define B_TRANSLATION_CONTEXT "PrefletWin"
31 const BString
kSampleMessageID("NotificationsSample");
34 PrefletWin::PrefletWin()
36 BWindow(BRect(0, 0, 160 + 20 * be_plain_font
->Size(), 300),
37 B_TRANSLATE_SYSTEM_NAME("Notifications"),
38 B_TITLED_WINDOW
, B_NOT_ZOOMABLE
| B_ASYNCHRONOUS_CONTROLS
39 | B_AUTO_UPDATE_SIZE_LIMITS
)
41 // Preflet container view
42 fMainView
= new PrefletView(this);
43 fMainView
->SetBorder(B_NO_BORDER
);
44 fMainView
->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNLIMITED
));
47 fDefaults
= new BButton("defaults", B_TRANSLATE("Defaults"),
48 new BMessage(kDefaults
));
49 fDefaults
->SetEnabled(false);
52 fRevert
= new BButton("revert", B_TRANSLATE("Revert"),
53 new BMessage(kRevert
));
54 fRevert
->SetEnabled(false);
57 fButtonsView
= new BGroupView();
58 BLayoutBuilder::Group
<>(fButtonsView
, B_VERTICAL
, 0)
59 .AddGroup(B_HORIZONTAL
)
63 .SetInsets(B_USE_WINDOW_SPACING
, 0, B_USE_WINDOW_SPACING
,
66 fButtonsLayout
= fButtonsView
->GroupLayout();
68 BLayoutBuilder::Group
<>(this, B_VERTICAL
, 0)
69 .SetInsets(0, B_USE_DEFAULT_SPACING
, 0, 0)
73 fMainView
->SetExplicitMinSize(BSize(Frame().Width(), B_SIZE_UNSET
));
77 // Center this window on screen and show it
84 PrefletWin::MessageReceived(BMessage
* msg
)
88 case kApplyWithExample
:
93 ret
= find_directory(B_USER_SETTINGS_DIRECTORY
, &path
);
97 path
.Append(kSettingsFile
);
99 BMessage settingsStore
;
100 int32 count
= fMainView
->CountTabs();
101 for (int32 i
= 0; i
< count
; i
++) {
103 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
105 if (pane
->Save(settingsStore
) == B_OK
) {
106 fDefaults
->SetEnabled(_DefaultsPossible());
107 fRevert
->SetEnabled(_RevertPossible());
113 // Save settings file
114 BFile
file(path
.Path(), B_WRITE_ONLY
| B_CREATE_FILE
| B_ERASE_FILE
);
115 ret
= settingsStore
.Flatten(&file
);
117 BAlert
* alert
= new BAlert("",
118 B_TRANSLATE("An error occurred saving the preferences.\n"
119 "It's possible you are running out of disk space."),
120 B_TRANSLATE("OK"), NULL
, NULL
, B_WIDTH_AS_USUAL
,
122 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
127 if (msg
->what
== kApplyWithExample
)
128 _SendExampleNotification();
133 fDefaults
->SetEnabled(false);
138 fRevert
->SetEnabled(false);
143 bool show
= msg
->GetBool(kShowButtonsKey
, true);
144 fButtonsLayout
->SetVisible(show
);
148 BWindow::MessageReceived(msg
);
154 PrefletWin::QuitRequested()
156 be_app_messenger
.SendMessage(B_QUIT_REQUESTED
);
162 PrefletWin::SettingChanged(bool showExample
)
165 PostMessage(kApplyWithExample
);
172 PrefletWin::ReloadSettings()
176 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) != B_OK
)
179 // FIXME don't load this again here, share with other tabs!
180 path
.Append(kSettingsFile
);
183 BFile
file(path
.Path(), B_READ_ONLY
);
184 settings
.Unflatten(&file
);
186 int32 count
= fMainView
->CountTabs();
187 for (int32 i
= 0; i
< count
; i
++) {
189 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
191 pane
->Load(settings
);
193 fDefaults
->SetEnabled(_DefaultsPossible());
198 PrefletWin::_Revert()
200 int32 count
= fMainView
->CountTabs();
201 for (int32 i
= 0; i
< count
; i
++) {
203 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
212 PrefletWin::_RevertPossible()
214 int32 count
= fMainView
->CountTabs();
215 for (int32 i
= 0; i
< count
; i
++) {
217 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
218 if (pane
&& pane
->RevertPossible())
226 PrefletWin::_Defaults()
228 int32 count
= fMainView
->CountTabs();
229 for (int32 i
= 0; i
< count
; i
++) {
231 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
240 PrefletWin::_DefaultsPossible()
242 int32 count
= fMainView
->CountTabs();
243 for (int32 i
= 0; i
< count
; i
++) {
245 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
246 if (pane
&& pane
->DefaultsPossible())
254 PrefletWin::_SendExampleNotification()
256 BNotification
notification(B_INFORMATION_NOTIFICATION
);
257 notification
.SetMessageID(kSampleMessageID
);
258 notification
.SetGroup(B_TRANSLATE("Notifications"));
259 notification
.SetTitle(B_TRANSLATE("Notifications preflet sample"));
260 notification
.SetContent(B_TRANSLATE("This is a test notification message"));