xref: /haiku/src/preferences/time/BaseView.cpp (revision 44d19f4d32b8f7e9c01f00294c87ca5cc2e057f7)
1 /*
2  * Copyright 2004-2007, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Mike Berg <mike@berg-net.us>
7  *		Julun <host.haiku@gmx.de>
8  */
9 
10 
11 #include "BaseView.h"
12 
13 #include <DateTime.h>
14 #include <OS.h>
15 
16 #include "TimeMessages.h"
17 
18 
19 TTimeBaseView::TTimeBaseView(const char* name)
20 	:
21 	BGroupView(name, B_VERTICAL, 0),
22 	fMessage(H_TIME_UPDATE)
23 {
24 	SetFlags(Flags() | B_PULSE_NEEDED);
25 }
26 
27 
28 TTimeBaseView::~TTimeBaseView()
29 {
30 }
31 
32 
33 void
34 TTimeBaseView::Pulse()
35 {
36 	if (IsWatched())
37 		_SendNotices();
38 }
39 
40 
41 void
42 TTimeBaseView::AttachedToWindow()
43 {
44 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
45 	SetLowUIColor(ViewUIColor());
46 }
47 
48 
49 void
50 TTimeBaseView::ChangeTime(BMessage* message)
51 {
52 	bool isTime;
53 	if (message->FindBool("time", &isTime) != B_OK)
54 		return;
55 
56 	BDateTime dateTime = BDateTime::CurrentDateTime(B_LOCAL_TIME);
57 
58 	if (isTime) {
59 		BTime time = dateTime.Time();
60 		int32 hour;
61 		if (message->FindInt32("hour", &hour) != B_OK)
62 			hour  = time.Hour();
63 
64 		int32 minute;
65 		if (message->FindInt32("minute", &minute) != B_OK)
66 			minute = time.Minute();
67 
68 		int32 second;
69 		if (message->FindInt32("second", &second) != B_OK)
70 			second = time.Second();
71 
72 		time.SetTime(hour, minute, second);
73 		dateTime.SetTime(time);
74 	} else {
75 		BDate date = dateTime.Date();
76 		int32 day;
77 		if (message->FindInt32("day", &day) != B_OK)
78 			day = date.Day();
79 
80 		int32 year;
81 		if (message->FindInt32("year", &year) != B_OK)
82 			year = date.Year();
83 
84 		int32 month;
85 		if (message->FindInt32("month", &month) != B_OK)
86 			month = date.Month();
87 
88 		if (year >= 1970 && year <= 2037) {
89 			date.SetDate(year, month, day);
90 			dateTime.SetDate(date);
91 		}
92 	}
93 
94 	set_real_time_clock(dateTime.Time_t());
95 }
96 
97 
98 void
99 TTimeBaseView::_SendNotices()
100 {
101 	fMessage.MakeEmpty();
102 
103 	BDate date = BDate::CurrentDate(B_LOCAL_TIME);
104 	fMessage.AddInt32("day", date.Day());
105 	fMessage.AddInt32("year", date.Year());
106 	fMessage.AddInt32("month", date.Month());
107 
108 	BTime time = BTime::CurrentTime(B_LOCAL_TIME);
109 	fMessage.AddInt32("hour", time.Hour());
110 	fMessage.AddInt32("minute", time.Minute());
111 	fMessage.AddInt32("second", time.Second());
112 
113 	SendNotices(H_TM_CHANGED, &fMessage);
114 }
115 
116