vfs: check userland buffers before reading them.
[haiku.git] / src / preferences / time / TimeWindow.cpp
blobda5c718861d8a86729ef42dc57fb664988f1d092
1 /*
2 * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Andrew McCall <mccall@@digitalparadise.co.uk>
7 * Julun <host.haiku@gmx.de>
8 * Hamish Morrison <hamish@lavabit.com>
9 */
11 #include "TimeWindow.h"
13 #include <Application.h>
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <LayoutBuilder.h>
17 #include <LocaleRoster.h>
18 #include <Message.h>
19 #include <Screen.h>
20 #include <SeparatorView.h>
21 #include <TabView.h>
23 #include "BaseView.h"
24 #include "ClockView.h"
25 #include "DateTimeView.h"
26 #include "NetworkTimeView.h"
27 #include "TimeMessages.h"
28 #include "TimeSettings.h"
29 #include "ZoneView.h"
32 #undef B_TRANSLATION_CONTEXT
33 #define B_TRANSLATION_CONTEXT "Time"
35 TTimeWindow::TTimeWindow()
37 BWindow(BRect(0, 0, 0, 0), B_TRANSLATE_SYSTEM_NAME("Time"), B_TITLED_WINDOW,
38 B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
40 _InitWindow();
41 _AlignWindow();
43 AddShortcut('A', B_COMMAND_KEY, new BMessage(B_ABOUT_REQUESTED));
47 TTimeWindow::~TTimeWindow()
52 bool
53 TTimeWindow::QuitRequested()
55 TimeSettings().SetLeftTop(Frame().LeftTop());
57 fBaseView->StopWatchingAll(fTimeZoneView);
58 fBaseView->StopWatchingAll(fDateTimeView);
60 be_app->PostMessage(B_QUIT_REQUESTED);
62 return BWindow::QuitRequested();
66 void
67 TTimeWindow::MessageReceived(BMessage* message)
69 switch(message->what) {
70 case H_USER_CHANGE:
71 fBaseView->ChangeTime(message);
72 // To make sure no old time message is in the queue
73 _SendTimeChangeFinished();
74 _SetRevertStatus();
75 break;
77 case B_ABOUT_REQUESTED:
78 be_app->PostMessage(B_ABOUT_REQUESTED);
79 break;
81 case B_LOCALE_CHANGED:
83 BLocaleRoster::Default()->Refresh();
84 fDateTimeView->MessageReceived(message);
85 break;
88 case kMsgRevert:
89 fDateTimeView->MessageReceived(message);
90 fTimeZoneView->MessageReceived(message);
91 fNetworkTimeView->MessageReceived(message);
92 fClockView->MessageReceived(message);
93 fRevertButton->SetEnabled(false);
94 break;
96 case kRTCUpdate:
97 fDateTimeView->MessageReceived(message);
98 fTimeZoneView->MessageReceived(message);
99 _SetRevertStatus();
100 break;
102 case kMsgChange:
103 _SetRevertStatus();
104 break;
106 case kSelectClockTab:
107 // focus the clock tab (last one)
108 fTabView->Select(fTabView->CountTabs() - 1);
109 break;
111 case kShowHideTime:
112 fClockView->MessageReceived(message);
113 break;
115 default:
116 BWindow::MessageReceived(message);
117 break;
122 void
123 TTimeWindow::_InitWindow()
125 SetPulseRate(500000);
127 fDateTimeView = new DateTimeView(B_TRANSLATE("Date and time"));
128 fTimeZoneView = new TimeZoneView(B_TRANSLATE("Time zone"));
129 fNetworkTimeView = new NetworkTimeView(B_TRANSLATE("Network time"));
130 fClockView = new ClockView(B_TRANSLATE("Clock"));
132 fBaseView = new TTimeBaseView("baseView");
133 fBaseView->StartWatchingAll(fDateTimeView);
134 fBaseView->StartWatchingAll(fTimeZoneView);
136 fTabView = new BTabView("tabView", B_WIDTH_FROM_WIDEST);
137 fTabView->AddTab(fDateTimeView);
138 fTabView->AddTab(fTimeZoneView);
139 fTabView->AddTab(fNetworkTimeView);
140 fTabView->AddTab(fClockView);
141 fTabView->SetBorder(B_NO_BORDER);
143 fBaseView->AddChild(fTabView);
145 fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
146 new BMessage(kMsgRevert));
147 fRevertButton->SetEnabled(false);
148 fRevertButton->SetTarget(this);
149 fRevertButton->SetExplicitAlignment(
150 BAlignment(B_ALIGN_LEFT, B_ALIGN_MIDDLE));
152 BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
153 .SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0)
154 .Add(fBaseView)
155 .Add(new BSeparatorView(B_HORIZONTAL))
156 .AddGroup(B_HORIZONTAL)
157 .Add(fRevertButton)
158 .SetInsets(B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING,
159 B_USE_DEFAULT_SPACING, B_USE_WINDOW_SPACING);
163 void
164 TTimeWindow::_AlignWindow()
166 MoveTo(TimeSettings().LeftTop());
167 MoveOnScreen();
171 void
172 TTimeWindow::_SendTimeChangeFinished()
174 BMessenger messenger(fDateTimeView);
175 BMessage msg(kChangeTimeFinished);
176 messenger.SendMessage(&msg);
180 void
181 TTimeWindow::_SetRevertStatus()
183 fRevertButton->SetEnabled(fDateTimeView->CheckCanRevert()
184 || fTimeZoneView->CheckCanRevert()
185 || fNetworkTimeView->CheckCanRevert()
186 || fClockView->CheckCanRevert());