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