fat: Greatly simplify and clean up dosfs_get_file_map().
[haiku.git] / src / preferences / notifications / PrefletWin.cpp
blob64b1e492be743c5014fa1577792117f2605ada0b
1 /*
2 * Copyright 2010-2014, Haiku, Inc. All Rights Reserved.
3 * Copyright 2009, Pier Luigi Fiorini.
4 * Distributed under the terms of the MIT License.
6 * Authors:
7 * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
8 */
10 #include "PrefletWin.h"
12 #include <Alert.h>
13 #include <Application.h>
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <FindDirectory.h>
17 #include <LayoutBuilder.h>
18 #include <Path.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);
49 // Build the layout
50 BLayoutBuilder::Group<>(this, B_VERTICAL)
51 .SetInsets(B_USE_DEFAULT_SPACING)
52 .Add(fMainView)
53 .AddGroup(B_HORIZONTAL)
54 .Add(fRevert)
55 .AddGlue()
56 .Add(fApply);
58 ReloadSettings();
60 // Center this window on screen and show it
61 CenterOnScreen();
62 Show();
66 void
67 PrefletWin::MessageReceived(BMessage* msg)
69 switch (msg->what) {
70 case kApply:
72 BPath path;
74 status_t ret = B_OK;
75 ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
76 if (ret != B_OK)
77 return;
79 path.Append(kSettingsFile);
81 BMessage settingsStore;
82 for (int32 i = 0; i < fMainView->CountPages(); i++) {
83 SettingsPane* pane =
84 dynamic_cast<SettingsPane*>(fMainView->PageAt(i));
85 if (pane) {
86 if (pane->Save(settingsStore) == B_OK) {
87 fApply->SetEnabled(false);
88 fRevert->SetEnabled(true);
89 } else
90 break;
94 // Save settings file
95 BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
96 ret = settingsStore.Flatten(&file);
97 if (ret != B_OK) {
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,
102 B_STOP_ALERT);
103 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
104 (void)alert->Go();
107 break;
109 case kRevert:
110 for (int32 i = 0; i < fMainView->CountPages(); i++) {
111 SettingsPane* pane =
112 dynamic_cast<SettingsPane*>(fMainView->PageAt(i));
113 if (pane) {
114 if (pane->Revert() == B_OK)
115 fRevert->SetEnabled(false);
118 break;
119 default:
120 BWindow::MessageReceived(msg);
125 bool
126 PrefletWin::QuitRequested()
128 be_app_messenger.SendMessage(B_QUIT_REQUESTED);
129 return true;
133 void
134 PrefletWin::SettingChanged()
136 fApply->SetEnabled(true);
140 void
141 PrefletWin::ReloadSettings()
143 BPath path;
145 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
146 return;
148 // FIXME don't load this again here, share with other tabs!
149 path.Append(kSettingsFile);
151 BMessage settings;
152 BFile file(path.Path(), B_READ_ONLY);
153 settings.Unflatten(&file);
155 for (int32 i = 0; i < fMainView->CountPages(); i++) {
156 SettingsPane* pane =
157 dynamic_cast<SettingsPane*>(fMainView->PageAt(i));
158 if (pane)
159 pane->Load(settings);