1/*
2 * Copyright 2010-2017, 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 *		Brian Hill, supernova@tycho.email
13 */
14
15#include <Message.h>
16
17#include <AppUsage.h>
18#include <NotificationReceived.h>
19
20const type_code kTypeCode = 'ipau';
21
22
23AppUsage::AppUsage()
24	:
25	fAppName(""),
26	fSignature(""),
27	fAllow(true)
28{
29}
30
31
32AppUsage::AppUsage(const char* name, const char* signature, bool allow)
33	:
34	fAppName(name),
35	fSignature(signature),
36	fAllow(allow)
37{
38}
39
40
41bool
42AppUsage::AllowsTypeCode(type_code code) const
43{
44	return code == kTypeCode;
45}
46
47
48status_t
49AppUsage::Flatten(void* buffer, ssize_t numBytes) const
50{
51	BMessage msg;
52	msg.AddString("name", fAppName);
53	msg.AddString("signature", fSignature);
54	msg.AddBool("allow", fAllow);
55
56	if (numBytes < msg.FlattenedSize())
57		return B_ERROR;
58
59	return msg.Flatten((char*)buffer, numBytes);
60}
61
62
63ssize_t
64AppUsage::FlattenedSize() const
65{
66	BMessage msg;
67	msg.AddString("name", fAppName);
68	msg.AddString("signature", fSignature);
69	msg.AddBool("allow", fAllow);
70
71	return msg.FlattenedSize();
72}
73
74
75bool
76AppUsage::IsFixedSize() const
77{
78	return false;
79}
80
81
82type_code
83AppUsage::TypeCode() const
84{
85	return kTypeCode;
86}
87
88
89status_t
90AppUsage::Unflatten(type_code code, const void* buffer,
91	ssize_t numBytes)
92{
93	if (code != kTypeCode)
94		return B_ERROR;
95
96	BMessage msg;
97	status_t status = B_ERROR;
98
99	status = msg.Unflatten((const char*)buffer);
100
101	if (status == B_OK) {
102		msg.FindString("name", &fAppName);
103		msg.FindString("signature", &fSignature);
104		msg.FindBool("allow", &fAllow);
105	}
106
107	return status;
108}
109
110
111const char*
112AppUsage::AppName()
113{
114	return fAppName.String();
115}
116
117
118const char*
119AppUsage::Signature()
120{
121	return fSignature.String();
122}
123
124
125bool
126AppUsage::Allowed()
127{
128	return fAllow;
129}
130
131
132void
133AppUsage::SetAllowed(bool allow)
134{
135	fAllow = allow;
136}
137