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