2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 * @author Pawel Misiak (p.misiak@samsung.com)
29 #include <Commons/Exception.h>
30 #include "Recipient.h"
33 using namespace WrtDeviceApis::Commons;
36 const static string emptyString;
43 Recipients::Recipients() :
44 m_validRecipients(false)
48 Recipients::Recipients(const vector<string>& value) :
49 m_validRecipients(false)
54 Recipients::~Recipients()
58 bool Recipients::isValid() const
60 return m_validRecipients;
63 void Recipients::setValid(bool state)
65 m_validRecipients = state;
68 void Recipients::setRecipients(const vector<string>& value)
71 m_validRecipients = false;
74 void Recipients::setRecipients(const std::string& value)
76 vector<string> tmp = split(value, ';');
80 void Recipients::setRecipient(const size_t index,
83 if (index >= m_recipients.size()) {
84 Throw(OutOfRangeException);
87 m_recipients[index] = value;
88 m_validRecipients = false;
91 void Recipients::setRecipientWithExpand(const size_t index,
92 const std::string& value)
94 if (index >= m_recipients.size()) {
95 m_recipients.resize(index + 1, emptyString);
97 m_recipients[index] = value;
98 m_validRecipients = false;
101 vector<string> Recipients::getRecipients() const
106 const vector<string>& Recipients::getRecipientsRef() const
111 string Recipients::getRecipient(const size_t index) const
113 if (index >= m_recipients.size()) {
114 Throw(OutOfRangeException);
117 return m_recipients[index];
120 void Recipients::appendRecipient(const string & value)
122 vector<string> tmp = split(value, ';');
123 m_recipients.insert(m_recipients.end(), tmp.begin(), tmp.end());
124 m_validRecipients = false;
127 void Recipients::removeRecipient(const size_t position)
129 if (position >= m_recipients.size()) {
130 Throw(WrtDeviceApis::Commons::OutOfRangeException);
132 m_recipients.erase(m_recipients.begin() + position);
133 m_validRecipients = false;
137 void Recipients::removeRecipient(const string & value)
140 remove(m_recipients.begin(), m_recipients.end(), value),
142 m_validRecipients = false;
145 string Recipients::join(char delimiter) const
148 for (size_t i = 0; i < m_recipients.size(); i++) {
149 retVal += m_recipients[i] + delimiter;
154 vector<string> Recipients::split(const string& input,
158 stringstream stream(input);
160 while (getline(stream, item, delimiter)) {
166 size_t Recipients::getRecipientSize() const
168 return m_recipients.size();
171 bool Recipients::operator !=(const Recipients& arg) const
173 if (m_recipients == arg.m_recipients) {
179 Recipients & Recipients::operator +=(const Recipients& val)
181 const vector<string>& tmp = val.getRecipientsRef();
182 m_recipients.insert(m_recipients.end(), tmp.begin(), tmp.end());
183 m_validRecipients = false;
187 Recipients & Recipients::operator -=(const Recipients& val)
189 //check if recipients are inside list
190 vector<string> l_recipients = m_recipients;
191 vector<string> l_toDelete = val.getRecipients();
192 sort(l_recipients.begin(), l_recipients.end());
193 sort(l_toDelete.begin(), l_toDelete.end());
194 if (!includes(l_recipients.begin(), l_recipients.end(), l_toDelete.begin(),
196 LogError("lack of recipient to delete from main variable");
197 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
200 vector<string>::const_iterator it = l_toDelete.begin();
201 const vector<string>::const_iterator endIt = l_toDelete.end();
202 while (it != endIt) {
204 remove(m_recipients.begin(),
205 m_recipients.end(), *it), m_recipients.end());
208 m_validRecipients = false;