xref: /haiku/src/preferences/time/BaseView.cpp (revision 93aeb8c3bc3f13cb1f282e3e749258a23790d947)
1 /*
2 	BaseView.cpp
3 		by Mike Berg (inseculous)
4 */
5 
6 #include <Alert.h>
7 #include <OS.h>
8 #include <stdio.h>
9 
10 #include "BaseView.h"
11 #include "TimeMessages.h"
12 
13 
14 TTimeBaseView::TTimeBaseView(BRect frame, const char *name)
15 	: BView(frame, name, B_FOLLOW_ALL_SIDES, B_PULSE_NEEDED)
16 {
17 	InitView();
18 }
19 
20 
21 TTimeBaseView::~TTimeBaseView()
22 {
23 }
24 
25 
26 
27 void
28 TTimeBaseView::InitView()
29 {
30 	f_message = new BMessage(H_TIME_UPDATE);
31 }
32 
33 
34 void
35 TTimeBaseView::Pulse()
36 {
37 	if (IsWatched())
38 		DispatchMessage();
39 }
40 
41 
42 void
43 TTimeBaseView::SetGMTime(bool gmt)
44 {
45 	f_gmtime = gmt;
46 }
47 
48 
49 void
50 TTimeBaseView::DispatchMessage()
51 {
52 	time_t current;
53 	struct tm *ltime;
54 
55 	current = time(0);
56 
57 	if (f_gmtime)
58 		ltime = gmtime(&current);
59 	else
60 		ltime = localtime(&current);
61 
62 	int32 month = ltime->tm_mon;
63 	int32 day = ltime->tm_mday;
64 	int32 year = ltime->tm_year;
65 	int32 hour = ltime->tm_hour;
66 	int32 minute = ltime->tm_min;
67 	int32 second = ltime->tm_sec;
68 
69 	f_message->MakeEmpty();
70 	f_message->AddInt32("month", month);
71 	f_message->AddInt32("day", day);
72 	f_message->AddInt32("year", year);
73 	f_message->AddInt32("hour", hour);
74 	f_message->AddInt32("minute", minute);
75 	f_message->AddInt32("second", second);
76 
77 	SendNotices(H_TM_CHANGED, f_message);
78 }
79 
80 
81 void
82 TTimeBaseView::ChangeTime(BMessage *message)
83 {
84 	bool istime;
85 	if (!(message->FindBool("time", &istime) == B_OK))
86 		return;
87 
88 	time_t atime;
89 	struct tm *_tm;
90 
91 
92 	atime = time(0);
93 	_tm = localtime(&atime);
94 
95 	int32 hour = 0;
96 	int32 minute = 0;
97 	int32 second = 0;
98 	int32 month = 0;
99 	int32 day = 0;
100 	int32 year = 0;
101 	bool isam = false;
102 	if (istime) {
103 		if (message->FindInt32("hour", &hour) == B_OK)
104 			_tm->tm_hour = hour;
105 		if (message->FindInt32("minute", &minute) == B_OK)
106 			_tm->tm_min = minute;
107 		if (message->FindInt32("second", &second) == B_OK)
108 			_tm->tm_sec = second;
109 		if (message->FindBool("isam", &isam) == B_OK) {
110 			if (!isam)
111 				_tm->tm_hour += 12;
112 		}
113 	} else {
114 		if (message->FindInt32("month", &month) == B_OK)
115 			_tm->tm_mon = month;
116 		if (message->FindInt32("day", &day) == B_OK)
117 			_tm->tm_mday = day;
118 		if (message->FindInt32("year", &year) == B_OK)
119 			_tm->tm_year = year;
120 	}
121 
122 	time_t atime2 = mktime(_tm);
123 	set_real_time_clock(atime2);
124 	DispatchMessage();
125 }
126 
127