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
18 const type_code kTypeCode = 'ipnt';
19
20
NotificationReceived()21 NotificationReceived::NotificationReceived()
22 :
23 fTitle(""),
24 fType(B_INFORMATION_NOTIFICATION),
25 fEnabled(false),
26 fLastReceived(time(NULL))
27 {
28 }
29
30
NotificationReceived(const char * title,notification_type type,bool enabled)31 NotificationReceived::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
~NotificationReceived()42 NotificationReceived::~NotificationReceived()
43 {
44 }
45
46
47 bool
AllowsTypeCode(type_code code) const48 NotificationReceived::AllowsTypeCode(type_code code) const
49 {
50 return code == kTypeCode;
51 }
52
53
54 status_t
Flatten(void * buffer,ssize_t numBytes) const55 NotificationReceived::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
70 ssize_t
FlattenedSize() const71 NotificationReceived::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
83 bool
IsFixedSize() const84 NotificationReceived::IsFixedSize() const
85 {
86 return false;
87 }
88
89
90 type_code
TypeCode() const91 NotificationReceived::TypeCode() const
92 {
93 return kTypeCode;
94 }
95
96
97 status_t
Unflatten(type_code code,const void * buffer,ssize_t numBytes)98 NotificationReceived::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
118 const char*
Title()119 NotificationReceived::Title()
120 {
121 return fTitle.String();
122 }
123
124
125 notification_type
Type()126 NotificationReceived::Type()
127 {
128 return fType;
129 }
130
131
132 void
SetType(notification_type type)133 NotificationReceived::SetType(notification_type type)
134 {
135 fType = type;
136 }
137
138
139 time_t
LastReceived()140 NotificationReceived::LastReceived()
141 {
142 return fLastReceived;
143 }
144
145
146 bool
Allowed()147 NotificationReceived::Allowed()
148 {
149 return fEnabled;
150 }
151
152
153 void
UpdateTimeStamp()154 NotificationReceived::UpdateTimeStamp()
155 {
156 fLastReceived = time(NULL);
157 }
158
159
160 void
SetTimeStamp(time_t time)161 NotificationReceived::SetTimeStamp(time_t time)
162 {
163 fLastReceived = time;
164 }
165