xref: /haiku/src/preferences/notifications/PrefletWin.cpp (revision 90c3b9bf9fe633e4c6a59d42c11c3f523183c553)
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.
5  *
6  * Authors:
7  *		Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
8  */
9 
10 #include "PrefletWin.h"
11 
12 #include <Alert.h>
13 #include <Application.h>
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <FindDirectory.h>
17 #include <GroupLayout.h>
18 #include <GroupLayoutBuilder.h>
19 #include <Path.h>
20 
21 #include <notification/Notifications.h>
22 
23 #include "PrefletView.h"
24 
25 
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "PrefletWin"
28 
29 
30 const int32 kRevert = '_RVT';
31 const int32 kApply = '_APY';
32 
33 
34 PrefletWin::PrefletWin()
35 	:
36 	BWindow(BRect(0, 0, 1, 1), B_TRANSLATE_SYSTEM_NAME("Notifications"),
37 		B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_NOT_RESIZABLE
38 		| B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS)
39 {
40 	// Preflet container view
41 	fMainView = new PrefletView(this);
42 
43 	// Apply and revert buttons
44 	fRevert = new BButton("revert", B_TRANSLATE("Revert"),
45 		new BMessage(kRevert));
46 	fRevert->SetEnabled(false);
47 	fApply = new BButton("apply", B_TRANSLATE("Apply"), new BMessage(kApply));
48 	fApply->SetEnabled(false);
49 
50 	// Calculate inset
51 	float inset = ceilf(be_plain_font->Size() * 0.7f);
52 
53 	// Build the layout
54 	SetLayout(new BGroupLayout(B_VERTICAL));
55 
56 	// Add childs
57 	AddChild(BGroupLayoutBuilder(B_VERTICAL, inset)
58 		.Add(fMainView)
59 
60 		.AddGroup(B_HORIZONTAL, inset)
61 			.Add(fRevert)
62 			.AddGlue()
63 			.Add(fApply)
64 		.End()
65 
66 		.SetInsets(inset, inset, inset, inset)
67 	);
68 
69 	ReloadSettings();
70 
71 	// Center this window on screen and show it
72 	CenterOnScreen();
73 	Show();
74 }
75 
76 
77 void
78 PrefletWin::MessageReceived(BMessage* msg)
79 {
80 	switch (msg->what) {
81 		case kApply:
82 		{
83 			BPath path;
84 
85 			status_t ret = B_OK;
86 			ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
87 			if (ret != B_OK)
88 				return;
89 
90 			path.Append(kSettingsFile);
91 
92 			BMessage settingsStore;
93 			for (int32 i = 0; i < fMainView->CountPages(); i++) {
94 				SettingsPane* pane =
95 					dynamic_cast<SettingsPane*>(fMainView->PageAt(i));
96 				if (pane) {
97 					if (pane->Save(settingsStore) == B_OK) {
98 						fApply->SetEnabled(false);
99 						fRevert->SetEnabled(true);
100 					} else
101 						break;
102 				}
103 			}
104 
105 			// Save settings file
106 			BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
107 			ret = settingsStore.Flatten(&file);
108 			if (ret != B_OK) {
109 				BAlert* alert = new BAlert("",
110 					B_TRANSLATE("An error occurred saving the preferences.\n"
111 						"It's possible you are running out of disk space."),
112 					B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL,
113 					B_STOP_ALERT);
114 				alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
115 				(void)alert->Go();
116 			}
117 
118 			break;
119 		}
120 		case kRevert:
121 			for (int32 i = 0; i < fMainView->CountPages(); i++) {
122 				SettingsPane* pane =
123 					dynamic_cast<SettingsPane*>(fMainView->PageAt(i));
124 				if (pane) {
125 					if (pane->Revert() == B_OK)
126 						fRevert->SetEnabled(false);
127 				}
128 			}
129 			break;
130 		default:
131 			BWindow::MessageReceived(msg);
132 	}
133 }
134 
135 
136 bool
137 PrefletWin::QuitRequested()
138 {
139 	be_app_messenger.SendMessage(B_QUIT_REQUESTED);
140 	return true;
141 }
142 
143 
144 void
145 PrefletWin::SettingChanged()
146 {
147 	fApply->SetEnabled(true);
148 }
149 
150 
151 void
152 PrefletWin::ReloadSettings()
153 {
154 	BPath path;
155 
156 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
157 		return;
158 
159 	// FIXME don't load this again here, share with other tabs!
160 	path.Append(kSettingsFile);
161 
162 	BMessage settings;
163 	BFile file(path.Path(), B_READ_ONLY);
164 	settings.Unflatten(&file);
165 
166 	for (int32 i = 0; i < fMainView->CountPages(); i++) {
167 		SettingsPane* pane =
168 			dynamic_cast<SettingsPane*>(fMainView->PageAt(i));
169 		if (pane)
170 			pane->Load(settings);
171 	}
172 }
173