xref: /haiku/src/servers/notification/AppUsage.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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 
16 #include <AppUsage.h>
17 #include <NotificationReceived.h>
18 
19 const type_code kTypeCode = 'ipau';
20 
21 
22 AppUsage::AppUsage()
23 	:
24 	fName(""),
25 	fAllow(true)
26 {
27 }
28 
29 
30 AppUsage::AppUsage(const char* name, bool allow)
31 	:
32 	fName(name),
33 	fAllow(allow)
34 {
35 }
36 
37 
38 AppUsage::~AppUsage()
39 {
40 	notification_t::iterator nIt;
41 	for (nIt = fNotifications.begin(); nIt != fNotifications.end(); nIt++)
42 		delete nIt->second;
43 }
44 
45 
46 bool
47 AppUsage::AllowsTypeCode(type_code code) const
48 {
49 	return code == kTypeCode;
50 }
51 
52 
53 status_t
54 AppUsage::Flatten(void* buffer, ssize_t numBytes) const
55 {
56 	BMessage msg;
57 	msg.AddString("signature", fName);
58 	msg.AddBool("allow", fAllow);
59 
60 	notification_t::const_iterator nIt;
61 	for (nIt = fNotifications.begin(); nIt != fNotifications.end(); nIt++)
62 		msg.AddFlat("notification", nIt->second);
63 
64 	if (numBytes < msg.FlattenedSize())
65 		return B_ERROR;
66 
67 	return msg.Flatten((char*)buffer, numBytes);
68 }
69 
70 
71 ssize_t
72 AppUsage::FlattenedSize() const
73 {
74 	BMessage msg;
75 	msg.AddString("signature", fName);
76 	msg.AddBool("allow", fAllow);
77 
78 	notification_t::const_iterator nIt;
79 	for (nIt = fNotifications.begin(); nIt != fNotifications.end(); nIt++)
80 		msg.AddFlat("notification", nIt->second);
81 
82 	return msg.FlattenedSize();
83 }
84 
85 
86 bool
87 AppUsage::IsFixedSize() const
88 {
89 	return false;
90 }
91 
92 
93 type_code
94 AppUsage::TypeCode() const
95 {
96 	return kTypeCode;
97 }
98 
99 
100 status_t
101 AppUsage::Unflatten(type_code code, const void* buffer,
102 	ssize_t numBytes)
103 {
104 	if (code != kTypeCode)
105 		return B_ERROR;
106 
107 	BMessage msg;
108 	status_t status = B_ERROR;
109 
110 	status = msg.Unflatten((const char*)buffer);
111 
112 	if (status == B_OK) {
113 		msg.FindString("signature", &fName);
114 		msg.FindBool("allow", &fAllow);
115 
116 		type_code type;
117 		int32 count = 0;
118 
119 		status = msg.GetInfo("notification", &type, &count);
120 		if (status != B_OK)
121 			return status;
122 
123 		for (int32 i = 0; i < count; i++) {
124 			NotificationReceived *notification = new NotificationReceived();
125 			msg.FindFlat("notification", i, notification);
126 			fNotifications[notification->Title()] = notification;
127 		}
128 
129 		status = B_OK;
130 	}
131 
132 	return status;
133 }
134 
135 
136 const char*
137 AppUsage::Name()
138 {
139 	return fName.String();
140 }
141 
142 
143 bool
144 AppUsage::Allowed(const char* title, notification_type type)
145 {
146 	bool allowed = fAllow;
147 
148 	if (allowed) {
149 		notification_t::iterator nIt = fNotifications.find(title);
150 		if (nIt == fNotifications.end()) {
151 			allowed = true;
152 			fNotifications[title] = new NotificationReceived(title, type);
153 		} else {
154 			allowed = nIt->second->Allowed();
155 			nIt->second->UpdateTimeStamp();
156 			nIt->second->SetType(type);
157 		}
158 	}
159 
160 	return allowed;
161 }
162 
163 
164 bool
165 AppUsage::Allowed()
166 {
167 	return fAllow;
168 }
169 
170 
171 NotificationReceived*
172 AppUsage::NotificationAt(int32 index)
173 {
174 	notification_t::iterator nIt = fNotifications.begin();
175 	for (int32 i = 0; i < index; i++)
176 		nIt++;
177 
178 	return nIt->second;
179 }
180 
181 
182 int32
183 AppUsage::Notifications()
184 {
185 	return fNotifications.size();
186 }
187 
188 
189 void
190 AppUsage::AddNotification(NotificationReceived* notification)
191 {
192 	fNotifications[notification->Title()] = notification;
193 }
194