1/*
2 * Copyright 2010, Haiku, Inc. All Rights Reserved.
3 * Copyright 2008-2009, Pier Luigi Fiorini. All Rights Reserved.
4 * Copyright 2004-2008, Michael Davidson. All Rights Reserved.
5 * Copyright 2004-2007, Mikael Eiman. All Rights Reserved.
6 * Distributed under the terms of the MIT License.
7 *
8 * Authors:
9 *		Michael Davidson, slaad@bong.com.au
10 *		Mikael Eiman, mikael@eiman.tv
11 *		Pier Luigi Fiorini, pierluigi.fiorini@gmail.com
12 */
13
14#include <Message.h>
15#include <Notification.h>
16#include <NotificationReceived.h>
17
18const type_code kTypeCode = 'ipnt';
19
20
21NotificationReceived::NotificationReceived()
22	:
23	fTitle(""),
24	fType(B_INFORMATION_NOTIFICATION),
25	fEnabled(false),
26	fLastReceived(time(NULL))
27{
28}
29
30
31NotificationReceived::NotificationReceived(const char* title,
32	notification_type type, bool enabled)
33	:
34	fTitle(title),
35	fType(type),
36	fEnabled(enabled),
37	fLastReceived(time(NULL))
38{
39}
40
41
42NotificationReceived::~NotificationReceived()
43{
44}
45
46
47bool
48NotificationReceived::AllowsTypeCode(type_code code) const
49{
50	return code == kTypeCode;
51}
52
53
54status_t
55NotificationReceived::Flatten(void* buffer, ssize_t numBytes) const
56{
57	BMessage msg;
58	msg.AddString("notify_title", fTitle);
59	msg.AddInt32("notify_type", (int32)fType);
60	msg.AddInt32("notify_lastreceived", (int32)fLastReceived);
61	msg.AddBool("notify_enabled", fEnabled);
62
63	if (numBytes < msg.FlattenedSize())
64		return B_ERROR;
65
66	return msg.Flatten((char*)buffer, numBytes);
67}
68
69
70ssize_t
71NotificationReceived::FlattenedSize() const
72{
73	BMessage msg;
74	msg.AddString("notify_title", fTitle);
75	msg.AddInt32("notify_type", (int32)fType);
76	msg.AddInt32("notify_lastreceived", (int32)fLastReceived);
77	msg.AddBool("notify_enabled", fEnabled);
78
79	return msg.FlattenedSize();
80}
81
82
83bool
84NotificationReceived::IsFixedSize() const
85{
86	return false;
87}
88
89
90type_code
91NotificationReceived::TypeCode() const
92{
93	return kTypeCode;
94}
95
96
97status_t
98NotificationReceived::Unflatten(type_code code, const void* buffer,
99	ssize_t numBytes)
100{
101	if (code != kTypeCode)
102		return B_ERROR;
103
104	BMessage msg;
105	status_t error = msg.Unflatten((const char*)buffer);
106
107	if (error == B_OK) {
108		msg.FindString("notify_title", &fTitle);
109		msg.FindInt32("notify_type", (int32 *)&fType);
110		msg.FindInt32("notify_lastreceived", (int32 *)&fLastReceived);
111		msg.FindBool("notify_enabled", &fEnabled);
112	}
113
114	return error;
115}
116
117
118const char*
119NotificationReceived::Title()
120{
121	return fTitle.String();
122}
123
124
125notification_type
126NotificationReceived::Type()
127{
128	return fType;
129}
130
131
132void
133NotificationReceived::SetType(notification_type type)
134{
135	fType = type;
136}
137
138
139time_t
140NotificationReceived::LastReceived()
141{
142	return fLastReceived;
143}
144
145
146bool
147NotificationReceived::Allowed()
148{
149	return fEnabled;
150}
151
152
153void
154NotificationReceived::UpdateTimeStamp()
155{
156	fLastReceived = time(NULL);
157}
158
159
160void
161NotificationReceived::SetTimeStamp(time_t time)
162{
163	fLastReceived = time;
164}
165