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
20 const type_code kTypeCode = 'ipau';
21
22
AppUsage()23 AppUsage::AppUsage()
24 :
25 fAppName(""),
26 fSignature(""),
27 fAllow(true)
28 {
29 }
30
31
AppUsage(const char * name,const char * signature,bool allow)32 AppUsage::AppUsage(const char* name, const char* signature, bool allow)
33 :
34 fAppName(name),
35 fSignature(signature),
36 fAllow(allow)
37 {
38 }
39
40
41 bool
AllowsTypeCode(type_code code) const42 AppUsage::AllowsTypeCode(type_code code) const
43 {
44 return code == kTypeCode;
45 }
46
47
48 status_t
Flatten(void * buffer,ssize_t numBytes) const49 AppUsage::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
63 ssize_t
FlattenedSize() const64 AppUsage::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
75 bool
IsFixedSize() const76 AppUsage::IsFixedSize() const
77 {
78 return false;
79 }
80
81
82 type_code
TypeCode() const83 AppUsage::TypeCode() const
84 {
85 return kTypeCode;
86 }
87
88
89 status_t
Unflatten(type_code code,const void * buffer,ssize_t numBytes)90 AppUsage::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
111 const char*
AppName()112 AppUsage::AppName()
113 {
114 return fAppName.String();
115 }
116
117
118 const char*
Signature()119 AppUsage::Signature()
120 {
121 return fSignature.String();
122 }
123
124
125 bool
Allowed()126 AppUsage::Allowed()
127 {
128 return fAllow;
129 }
130
131
132 void
SetAllowed(bool allow)133 AppUsage::SetAllowed(bool allow)
134 {
135 fAllow = allow;
136 }
137