xref: /haiku/src/preferences/time/BaseView.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
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 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
45 }
46 
47 
48 void
49 TTimeBaseView::ChangeTime(BMessage* message)
50 {
51 	bool isTime;
52 	if (message->FindBool("time", &isTime) != B_OK)
53 		return;
54 
55 	BDateTime dateTime = BDateTime::CurrentDateTime(B_LOCAL_TIME);
56 
57 	if (isTime) {
58 		BTime time = dateTime.Time();
59 		int32 hour;
60 		if (message->FindInt32("hour", &hour) != B_OK)
61 			hour  = time.Hour();
62 
63 		int32 minute;
64 		if (message->FindInt32("minute", &minute) != B_OK)
65 			minute = time.Minute();
66 
67 		int32 second;
68 		if (message->FindInt32("second", &second) != B_OK)
69 			second = time.Second();
70 
71 		time.SetTime(hour, minute, second);
72 		dateTime.SetTime(time);
73 	} else {
74 		BDate date = dateTime.Date();
75 		int32 day;
76 		if (message->FindInt32("day", &day) != B_OK)
77 			day = date.Day();
78 
79 		int32 year;
80 		if (message->FindInt32("year", &year) != B_OK)
81 			year = date.Year();
82 
83 		int32 month;
84 		if (message->FindInt32("month", &month) != B_OK)
85 			month = date.Month();
86 
87 		if (year >= 1970 && year <= 2037) {
88 			date.SetDate(year, month, day);
89 			dateTime.SetDate(date);
90 		}
91 	}
92 
93 	set_real_time_clock(dateTime.Time_t());
94 }
95 
96 
97 void
98 TTimeBaseView::_SendNotices()
99 {
100 	fMessage.MakeEmpty();
101 
102 	BDate date = BDate::CurrentDate(B_LOCAL_TIME);
103 	fMessage.AddInt32("day", date.Day());
104 	fMessage.AddInt32("year", date.Year());
105 	fMessage.AddInt32("month", date.Month());
106 
107 	BTime time = BTime::CurrentTime(B_LOCAL_TIME);
108 	fMessage.AddInt32("hour", time.Hour());
109 	fMessage.AddInt32("minute", time.Minute());
110 	fMessage.AddInt32("second", time.Second());
111 
112 	SendNotices(H_TM_CHANGED, &fMessage);
113 }
114 
115