1 /*
2 * Copyright 2015, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
4 */
5 #include "TeamSignalSettings.h"
6
7
8 static const char* skDefaultSignalFieldName = "signal:default_disposition";
9 static const char* skSignalNumberFieldName = "signal:number";
10 static const char* skSignalDispositionFieldName = "signal:disposition";
11 static const char* skSignalSettingName = "signal:setting";
12
13
TeamSignalSettings()14 TeamSignalSettings::TeamSignalSettings()
15 :
16 fValues()
17 {
18 }
19
20
~TeamSignalSettings()21 TeamSignalSettings::~TeamSignalSettings()
22 {
23 }
24
25
26 TeamSignalSettings&
operator =(const TeamSignalSettings & other)27 TeamSignalSettings::operator=(const TeamSignalSettings& other)
28 {
29 fValues = other.fValues;
30
31 return *this;
32 }
33
34
35 const char*
ID() const36 TeamSignalSettings::ID() const
37 {
38 return "Signals";
39 }
40
41
42 status_t
SetTo(const BMessage & archive)43 TeamSignalSettings::SetTo(const BMessage& archive)
44 {
45 try {
46 fValues = archive;
47 } catch (...) {
48 return B_NO_MEMORY;
49 }
50
51 return B_OK;
52 }
53
54
55 status_t
WriteTo(BMessage & archive) const56 TeamSignalSettings::WriteTo(BMessage& archive) const
57 {
58 try {
59 archive = fValues;
60 } catch (...) {
61 return B_NO_MEMORY;
62 }
63
64 return B_OK;
65 }
66
67
68 void
Unset()69 TeamSignalSettings::Unset()
70 {
71 fValues.MakeEmpty();
72 }
73
74
75 void
SetDefaultSignalDisposition(int32 disposition)76 TeamSignalSettings::SetDefaultSignalDisposition(int32 disposition)
77 {
78 fValues.SetInt32(skDefaultSignalFieldName, disposition);
79 }
80
81
82 int32
DefaultSignalDisposition() const83 TeamSignalSettings::DefaultSignalDisposition() const
84 {
85 return fValues.GetInt32(skDefaultSignalFieldName,
86 SIGNAL_DISPOSITION_IGNORE);
87 }
88
89
90 int32
CountCustomSignalDispositions() const91 TeamSignalSettings::CountCustomSignalDispositions() const
92 {
93 type_code type;
94 int32 count = 0;
95
96 if (fValues.GetInfo(skSignalSettingName, &type, &count) == B_OK)
97 return count;
98
99 return 0;
100 }
101
102
103 status_t
AddCustomSignalDisposition(int32 signal,int32 disposition)104 TeamSignalSettings::AddCustomSignalDisposition(int32 signal, int32 disposition)
105 {
106 BMessage setting;
107 if (setting.AddInt32(skSignalNumberFieldName, signal) != B_OK
108 || setting.AddInt32(skSignalDispositionFieldName, disposition) != B_OK
109 || fValues.AddMessage(skSignalSettingName, &setting) != B_OK) {
110 return B_NO_MEMORY;
111 }
112
113 return B_OK;
114 }
115
116
117 status_t
RemoveCustomSignalDispositionAt(int32 index)118 TeamSignalSettings::RemoveCustomSignalDispositionAt(int32 index)
119 {
120 return fValues.RemoveData(skSignalSettingName, index);
121 }
122
123
124 status_t
GetCustomSignalDispositionAt(int32 index,int32 & signal,int32 & disposition) const125 TeamSignalSettings::GetCustomSignalDispositionAt(int32 index, int32& signal,
126 int32& disposition) const
127 {
128 BMessage setting;
129 status_t error = fValues.FindMessage(skSignalSettingName, index, &setting);
130 if (error != B_OK)
131 return error;
132
133 error = setting.FindInt32(skSignalNumberFieldName, &signal);
134 if (error != B_OK)
135 return error;
136
137 return setting.FindInt32(skSignalDispositionFieldName, &disposition);
138 }
139
140
141 TeamSignalSettings*
Clone() const142 TeamSignalSettings::Clone() const
143 {
144 TeamSignalSettings* settings = new(std::nothrow)
145 TeamSignalSettings();
146
147 if (settings == NULL)
148 return NULL;
149
150 if (settings->SetTo(fValues) != B_OK) {
151 delete settings;
152 return NULL;
153 }
154
155 return settings;
156 }
157