xref: /haiku/src/preferences/time/BaseView.cpp (revision 481f986b59e7782458dcc5fe98ad59a57480e5db)
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 #include "BaseView.h"
11 #include "TimeMessages.h"
12 
13 
14 #include <DateTime.h>
15 #include <OS.h>
16 
17 
18 TTimeBaseView::TTimeBaseView(BRect frame, const char* name)
19 	: BView(frame, name, B_FOLLOW_NONE, B_PULSE_NEEDED),
20 	  fMessage(H_TIME_UPDATE)
21 {
22 }
23 
24 
25 TTimeBaseView::~TTimeBaseView()
26 {
27 }
28 
29 
30 void
31 TTimeBaseView::Pulse()
32 {
33 	if (IsWatched())
34 		_SendNotices();
35 }
36 
37 
38 void
39 TTimeBaseView::AttachedToWindow()
40 {
41 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
42 }
43 
44 
45 void
46 TTimeBaseView::ChangeTime(BMessage* message)
47 {
48 	bool isTime;
49 	if (message->FindBool("time", &isTime) != B_OK)
50 		return;
51 
52 	BDateTime dateTime = BDateTime::CurrentDateTime(B_LOCAL_TIME);
53 
54 	if (isTime) {
55 		BTime time = dateTime.Time();
56 		int32 hour;
57 		if (message->FindInt32("hour", &hour) != B_OK)
58 			hour  = time.Hour();
59 
60 		int32 minute;
61 		if (message->FindInt32("minute", &minute) != B_OK)
62 			minute = time.Minute();
63 
64 		int32 second;
65 		if (message->FindInt32("second", &second) != B_OK)
66 			second = time.Second();
67 
68 		time.SetTime(hour, minute, second);
69 		dateTime.SetTime(time);
70 	} else {
71 		BDate date = dateTime.Date();
72 		int32 day;
73 		if (message->FindInt32("day", &day) != B_OK)
74 			day = date.Day();
75 
76 		int32 year;
77 		if (message->FindInt32("year", &year) != B_OK)
78 			year = date.Year();
79 
80 		int32 month;
81 		if (message->FindInt32("month", &month) != B_OK)
82 			month = date.Month();
83 
84 		if (year >= 1970 && year <= 2037) {
85 			date.SetDate(year, month, day);
86 			dateTime.SetDate(date);
87 		}
88 	}
89 
90 	set_real_time_clock(dateTime.Time_t());
91 }
92 
93 
94 void
95 TTimeBaseView::_SendNotices()
96 {
97 	fMessage.MakeEmpty();
98 
99 	BDate date = BDate::CurrentDate(B_LOCAL_TIME);
100 	fMessage.AddInt32("day", date.Day());
101 	fMessage.AddInt32("year", date.Year());
102 	fMessage.AddInt32("month", date.Month());
103 
104 	BTime time = BTime::CurrentTime(B_LOCAL_TIME);
105 	fMessage.AddInt32("hour", time.Hour());
106 	fMessage.AddInt32("minute", time.Minute());
107 	fMessage.AddInt32("second", time.Second());
108 
109 	SendNotices(H_TM_CHANGED, &fMessage);
110 }
111 
112