2 * Copyright 2010-2014, 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
10 #include "PrefletWin.h"
13 #include <Application.h>
16 #include <FindDirectory.h>
17 #include <LayoutBuilder.h>
20 #include <notification/Notifications.h>
22 #include "PrefletView.h"
25 #undef B_TRANSLATION_CONTEXT
26 #define B_TRANSLATION_CONTEXT "PrefletWin"
29 const int32 kRevert
= '_RVT';
30 const int32 kApply
= '_APY';
33 PrefletWin::PrefletWin()
35 BWindow(BRect(0, 0, 1, 1), B_TRANSLATE_SYSTEM_NAME("Notifications"),
36 B_TITLED_WINDOW
, B_NOT_ZOOMABLE
| B_NOT_RESIZABLE
37 | B_ASYNCHRONOUS_CONTROLS
| B_AUTO_UPDATE_SIZE_LIMITS
)
39 // Preflet container view
40 fMainView
= new PrefletView(this);
42 // Apply and revert buttons
43 fRevert
= new BButton("revert", B_TRANSLATE("Revert"),
44 new BMessage(kRevert
));
45 fRevert
->SetEnabled(false);
46 fApply
= new BButton("apply", B_TRANSLATE("Apply"), new BMessage(kApply
));
47 fApply
->SetEnabled(false);
50 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
51 .SetInsets(B_USE_DEFAULT_SPACING
)
53 .AddGroup(B_HORIZONTAL
)
60 // Center this window on screen and show it
67 PrefletWin::MessageReceived(BMessage
* msg
)
75 ret
= find_directory(B_USER_SETTINGS_DIRECTORY
, &path
);
79 path
.Append(kSettingsFile
);
81 BMessage settingsStore
;
82 for (int32 i
= 0; i
< fMainView
->CountPages(); i
++) {
84 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
86 if (pane
->Save(settingsStore
) == B_OK
) {
87 fApply
->SetEnabled(false);
88 fRevert
->SetEnabled(true);
95 BFile
file(path
.Path(), B_WRITE_ONLY
| B_CREATE_FILE
| B_ERASE_FILE
);
96 ret
= settingsStore
.Flatten(&file
);
98 BAlert
* alert
= new BAlert("",
99 B_TRANSLATE("An error occurred saving the preferences.\n"
100 "It's possible you are running out of disk space."),
101 B_TRANSLATE("OK"), NULL
, NULL
, B_WIDTH_AS_USUAL
,
103 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
110 for (int32 i
= 0; i
< fMainView
->CountPages(); i
++) {
112 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
114 if (pane
->Revert() == B_OK
)
115 fRevert
->SetEnabled(false);
120 BWindow::MessageReceived(msg
);
126 PrefletWin::QuitRequested()
128 be_app_messenger
.SendMessage(B_QUIT_REQUESTED
);
134 PrefletWin::SettingChanged()
136 fApply
->SetEnabled(true);
141 PrefletWin::ReloadSettings()
145 if (find_directory(B_USER_SETTINGS_DIRECTORY
, &path
) != B_OK
)
148 // FIXME don't load this again here, share with other tabs!
149 path
.Append(kSettingsFile
);
152 BFile
file(path
.Path(), B_READ_ONLY
);
153 settings
.Unflatten(&file
);
155 for (int32 i
= 0; i
< fMainView
->CountPages(); i
++) {
157 dynamic_cast<SettingsPane
*>(fMainView
->PageAt(i
));
159 pane
->Load(settings
);